-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
345 lines (303 loc) · 9.98 KB
/
main.c
File metadata and controls
345 lines (303 loc) · 9.98 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* author: martin.kramer at amescon.com
license: this file is release under the FreeBSD License */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "mongoose.h"
#include "rpc+.h"
typedef struct {
bool is_debug;
} config_t;
static config_t m_config = { true };
static const char *m_content_files[] = { "index.html", "index_gpio.html", "knockout-3.2.0.js", "jquery-2.1.1.min.js", "bootstrap.min.js", "bootstrap.min.css", "glyphicons-halflings-regular.eot", "glyphicons-halflings-regular.svg", "glyphicons-halflings-regular.ttf", "glyphicons-halflings-regular.woff" };
#define content_file_count (sizeof(m_content_files)/sizeof(char*))
#define uri_index "/"
#define uri_content "/content"
#define uri_gpio_get "/gpio/get"
#define uri_gpio_export "/gpio/export"
#define uri_gpio_unexport "/gpio/unexport"
#define uri_gpio_value_get "/gpio/value/get"
#define uri_gpio_value_set "/gpio/value/set"
#define uri_gpio_direction_get "/gpio/direction/get"
#define uri_gpio_direction_set "/gpio/direction/set"
#define is_request(uri,req) (strncmp(uri, req, sizeof(req)) == 0)
#define starts_with_request(uri,req) (strncmp(uri, req, sizeof(req)-1) == 0)
#define DEBUG 1
#if DEBUG
#define ON_DEBUG(a) do { a; } while (false);
#else
#define ON_DEBUG(a)
#endif
typedef enum {
request_unknown,
request_index,
request_content,
request_gpio_get,
request_gpio_export,
request_gpio_unexport,
request_gpio_value_get,
request_gpio_value_set,
request_gpio_direction_get,
request_gpio_direction_set
} request_t;
static request_t get_request_type(const char *uri)
{
request_t ret;
ON_DEBUG(printf("uri: %s -> ", uri));
if (is_request(uri, uri_index))
{
ret = request_index;
ON_DEBUG(printf("request_index"))
}
else if (starts_with_request(uri, uri_content))
{
ret = request_content;
ON_DEBUG(printf("request_content"));
}
else if (is_request(uri, uri_gpio_get))
{
ret = request_gpio_get;
ON_DEBUG(printf("request_gpio_get"));
}
else if (is_request(uri, uri_gpio_export))
{
ret = request_gpio_export;
ON_DEBUG(printf("request_gpio_export"));
}
else if (is_request(uri, uri_gpio_unexport))
{
ret = request_gpio_unexport;
ON_DEBUG(printf("request_gpio_unexport"));
}
else if (is_request(uri, uri_gpio_value_get))
{
ret = request_gpio_value_get;
ON_DEBUG(printf("request_gpio_value_get"));
}
else if (is_request(uri, uri_gpio_value_set))
{
ret = request_gpio_value_set;
ON_DEBUG(printf("request_gpio_value_set"));
}
else if (is_request(uri, uri_gpio_direction_get))
{
ret = request_gpio_direction_get;
ON_DEBUG(printf("request_gpio_direction_get"));
}
else if (is_request(uri, uri_gpio_direction_set))
{
ret = request_gpio_direction_set;
ON_DEBUG(printf("request_gpio_direction_set"));
}
else
{
ret = request_unknown;
ON_DEBUG(printf("request_unknown"));
}
ON_DEBUG(printf("\n"));
return ret;
}
static int get_query_param(const char *param, const char *query, char *value, int max_count)
{
int i=0, value_index = 0;
int param_length = strlen(param);
/* is it a valid query string? */
if (query != NULL)
{
/* search within the query string */
while (query[i] != '\0')
{
/* if the query starts with the parameter */
if (i == 0 || query[i-1] == '&')
if(strncmp(query+i, param, param_length) == 0)
{
/* if the param is followed by a '=' */
if (query[i+param_length] == '=')
{
/* copy the value */
for (value_index = 0;
value_index < max_count && /* as long as the user has space */
query[i+param_length+1+value_index] != '&' && /* until the next parameter starts */
query[i+param_length+1+value_index] != '\0'; /* or query's end is reached */
value_index++)
{
value[value_index] = query[i+param_length+1+value_index];
}
}
}
i++;
}
}
/* add the string termination */
value[value_index] = '\0';
return value_index;
}
static int get_index_of(const char *string, const char c)
{
int i;
if (string != NULL)
for (i = 0; string[i] != '\0'; i++)
if (string[i] == c)
return i;
return -1;
}
static int get_last_index_of(const char *string, const char c)
{
int i, last_index = -1;
if (string != NULL)
for (i = 0; string[i] != '\0'; i++)
if (string[i] == c)
last_index = i;
return last_index;
}
static int ev_handler(struct mg_connection *conn, enum mg_event ev)
{
static int request_num = 0;
char param_gpio[10], param_value[10], param_direction[10], param_content[20];
int gpio, last_slash; int value = -1; bool input_value; gpio_direction_t direction;
request_t req;
int i;
char filename[255];
switch (ev)
{
case MG_REQUEST:
ON_DEBUG(printf("req# (%i)", request_num++));
if (get_query_param("gpio", conn->query_string, param_gpio, sizeof(param_gpio)) > 0)
{
gpio = atoi(param_gpio);
ON_DEBUG(printf(" param gpio: %i", gpio));
}
if (get_query_param("value", conn->query_string, param_value, sizeof(param_value)) > 0)
{
value = atoi(param_value);
ON_DEBUG(printf(" param value: %i", value));
}
if (get_query_param("direction", conn->query_string, param_direction, sizeof(param_direction)) > 0)
ON_DEBUG(printf(" param direction: %s", param_direction));
if (get_query_param("content", conn->query_string, param_content, sizeof(param_content)) > 0)
ON_DEBUG(printf(" param content: %s", param_content));
ON_DEBUG(printf("\n"));
/* get the type of the request */
req = get_request_type(conn->uri);
switch (req)
{
case request_index:
mg_send_status(conn, 301);
mg_send_header(conn, "Location", "/content/index.html");
return MG_FALSE;
break;
case request_content:
last_slash = get_last_index_of(conn->uri, '/');
//ON_DEBUG(printf("last_slash: %i\n", last_slash));
if (last_slash >= 0)
for (i = 0; i < content_file_count; i++)
if (strcmp(conn->uri + last_slash + 1, m_content_files[i]) == 0)
//if (strcmp(param_content, m_content_files[i]) == 0)
{
snprintf(filename, sizeof(filename)-1, "content/%s", m_content_files[i]);
ON_DEBUG(printf("serving content file: %s\n", filename))
mg_send_file(conn, filename, NULL);
return MG_MORE;
}
mg_send_data(conn, NULL, 0);
break;
case request_gpio_get:
mg_send_data(conn, NULL, 0);
break;
case request_gpio_export:
gpio_export(gpio);
mg_send_data(conn, NULL, 0);
break;
case request_gpio_unexport:
gpio_unexport(gpio);
mg_send_data(conn, NULL, 0);
break;
case request_gpio_value_get:
if (gpio_get_value(gpio, &input_value))
mg_printf_data(conn, input_value ? "1" : "0" );
else {
ON_DEBUG(printf("can't get value of gpio: %i\n", gpio));
mg_send_data(conn, NULL, 0);
}
break;
case request_gpio_value_set:
if (value == 0)
gpio_set_value(gpio, false);
else if (value == 1)
gpio_set_value(gpio, true);
mg_send_data(conn, NULL, 0);
break;
case request_gpio_direction_get:
if (gpio_get_direction(gpio, &direction))
{
switch (direction)
{
case gpio_direction_in:
mg_printf_data(conn, "in");
break;
case gpio_direction_out:
mg_printf_data(conn, "out");
break;
}
}
else
{
ON_DEBUG(printf("can't get direction of gpio %i\n", gpio));
mg_send_data(conn, NULL, 0);
}
break;
case request_gpio_direction_set:
if (strncmp("in", param_direction, 2) == 0)
gpio_set_direction(gpio, gpio_direction_in);
else if (strncmp("out", param_direction, 3) == 0)
gpio_set_direction(gpio, gpio_direction_out);
mg_send_data(conn, NULL, 0);
break;
case request_unknown:
mg_send_data(conn, NULL, 0);
break;
}
return MG_TRUE;
//return MG_MORE; /* return MG_MORE when using mg_send_file() */
break;
case MG_AUTH: return MG_TRUE;
default: return MG_FALSE;
}
}
#define MAX_GPIOS 255
void rpcp_test(void)
{
int gpio_count;
gpio_get_gpio_count(&gpio_count);
printf("gpio_count: %i\n", gpio_count);
int gpios[MAX_GPIOS];
int i;
gpio_count = gpio_get_gpios(gpios, MAX_GPIOS);
printf("gpio_count: %i\n", gpio_count);
for (i = 0; i < MAX_GPIOS && i < gpio_count; i++)
printf("gpio %i: %i\n", i, gpios[i]);
printf("gpio_export(55): %i\n", gpio_export(55));
gpio_direction_t direction;
if (gpio_get_direction(55, &direction))
printf("gpio_get_direction(55) was successful: %i\n", direction);
else
printf("gpio_get_direction(55) failed\n");
bool value;
if (gpio_get_value(55, &value))
printf("gpio_get_value(55) was successful: %i\n", value);
else
printf("gpio_get_value(55) failed\n");
printf("gpio_unexport(55): %i\n", gpio_unexport(55));
}
int main(void)
{
ON_DEBUG(printf("content file count: %i\n", content_file_count));
struct mg_server *server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080");
//rpcp_test();
printf("Starting rpc+webapi on port %s\n", mg_get_option(server, "listening_port"));
for (;;) mg_poll_server(server, 1000);
mg_destroy_server(&server);
return 0;
}