forked from Azure/iot-edge-v1
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
212 lines (186 loc) · 5.01 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/signalfd.h>
#include <unistd.h>
#include <signal.h>
#include <glib.h>
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/platform.h"
#include "gateway.h"
static void print_usage();
static bool validate_args(int argc, char* argv[]);
static void handle_control_c(GMainLoop* loop);
static gboolean signal_handler(
GIOChannel *channel,
GIOCondition condition,
gpointer user_data
);
guint g_event_source_id = 0;
int main(int argc, char*argv[])
{
// create glib loop and register a ctrl+c handler
GMainLoop* loop = g_main_loop_new(NULL, FALSE);
handle_control_c(loop);
int result;
if (validate_args(argc, argv) == false)
{
result = 1;
}
else
{
if(platform_init() == 0)
{
char *json_path = argv[1];
GATEWAY_HANDLE gateway = Gateway_CreateFromJson(json_path);
if(gateway == NULL)
{
LogError("An error ocurred while creating the gateway.");
result = 1;
}
else
{
result = 0;
printf("Gateway is running.\r\n");
// run the glib loop
g_main_loop_run(loop);
printf("Gateway is quitting\r\n");
Gateway_Destroy(gateway);
}
platform_deinit();
}
else
{
LogError("Failed to initialize the platform");
result = 1;
}
}
return result;
}
static bool validate_args(int argc, char* argv[])
{
bool result;
const int EXPECTED_PARAMS = 2;
// we expect the path to the gateway JSON to be passed in
if (argc < EXPECTED_PARAMS)
{
print_usage();
result = false;
}
else
{
result = true;
for (int i = 1; i < EXPECTED_PARAMS; i++)
{
char* param = argv[i];
if (strlen(param) == 0)
{
result = false;
break;
}
}
}
return result;
}
static void print_usage()
{
printf("\r\nUsage:\r\n"
" ble_gateway <path to json>\r\n\r\n");
}
static void handle_control_c(GMainLoop* loop)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
{
LogError("Failed to set signal mask");
}
else
{
int fd = signalfd(-1, &mask, 0);
if (fd < 0)
{
LogError("Failed to create signal descriptor");
}
else
{
GIOChannel *channel = g_io_channel_unix_new(fd);
if (channel == NULL)
{
close(fd);
LogError("Failed to create IO channel");
}
else
{
g_io_channel_set_close_on_unref(channel, TRUE);
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, FALSE);
g_event_source_id = g_io_add_watch(
channel,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
signal_handler,
loop
);
if (g_event_source_id == 0)
{
LogError("g_io_add_watch failed");
}
g_main_loop_ref(loop);
g_io_channel_unref(channel);
}
}
}
}
static gboolean signal_handler(
GIOChannel *channel,
GIOCondition condition,
gpointer user_data
)
{
static unsigned int terminated = 0;
struct signalfd_siginfo si;
int fd;
GMainLoop* loop = (GMainLoop*)user_data;
gboolean result;
if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
LogInfo("Quitting...");
g_main_loop_unref(loop);
g_source_remove(g_event_source_id);
g_main_loop_quit(loop);
result = FALSE;
}
else
{
fd = g_io_channel_unix_get_fd(channel);
if (read(fd, &si, sizeof(si)) != sizeof(si))
{
LogError("read from fd failed");
result = FALSE;
}
else
{
switch (si.ssi_signo) {
case SIGINT:
LogInfo("Caught ctrl+c - quitting...");
g_main_loop_unref(loop);
g_source_remove(g_event_source_id);
g_main_loop_quit(loop);
break;
case SIGTERM:
if (terminated == 0) {
LogInfo("Caught SIGTERM - quitting...");
g_main_loop_unref(loop);
g_source_remove(g_event_source_id);
g_main_loop_quit(loop);
}
terminated = 1;
break;
}
result = TRUE;
}
}
return result;
}