-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
416 lines (349 loc) · 10.6 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <curl/curl.h>
#include "util.h"
#include <errno.h>
#include <unistd.h>
#include "jsmn.h"
#include <modbus.h>
#include <time.h>
#include <sys/mman.h>
#include <pthread.h>
#include <sys/wait.h>
#include <signal.h>
enum {
TCP,
TCP_PI,
RTU
};
typedef struct data_selection data_selection_t;
struct data_selection{
char *name;
uint8_t intg_part_size;
uint8_t frac_part_size;
data_selection_t *next;
};
typedef struct {
uint16_t *data;
uint16_t count;
data_selection_t **keys;
} shared_regs;
typedef struct {
char *url;
uint32_t period;
data_selection_t list;
} cfg_struct;
void free_structure(data_selection_t *s)
{
data_selection_t **del;
while (s->next != NULL)
{
del = &(s->next);
while ((*del)->next != NULL)
{
del = &((*del)->next);
}
free((*del)->name);
free(*del);
*del = NULL;
}
}
/* Function realloc_it() is a wrapper function for standard realloc()
* with one difference - it frees old memory pointer in case of realloc
* failure. Thus, DO NOT use old data pointer in anyway after call to
* realloc_it(). If your code has some kind of fallback algorithm if
* memory can't be re-allocated - use standard realloc() instead.
*/
static inline void *realloc_it(void *ptrmem, size_t size) {
void *p = realloc(ptrmem, size);
if (!p) {
free(ptrmem);
fprintf(stderr, "realloc(): errno=%d\n", errno);
}
return p;
}
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
int extract(const char *js, jsmntok_t *t, size_t count, void *data) {
int i;
int r = count;
shared_regs *mb = (shared_regs*)data;
if (mb->data == NULL) return EXIT_FAILURE;
uint16_t index = 0;
/* Assume the top-level element is an object */
if (r < 1 || t[0].type != JSMN_OBJECT) {
printf("Object expected\n");
return 1;
}
/* Loop over all keys of the root object */
for (i = 1; i < r; i++) {
nextkey:;
data_selection_t **current = mb->keys;
while(*current != NULL)
{
if (jsoneq(js, &t[i], (*current)->name) == 0) {
int64_t i_val = 0;
if((*current)->frac_part_size)
{
double d_val = strtod(js + t[++i].start, NULL);
d_val *= (double)(1 << (*current)->frac_part_size);
i_val = (int64_t)(d_val + ((d_val < 0.0) ? -0.5 : 0.5));
}
else
{
i_val = strtol(js + t[++i].start, NULL, 10);
}
if(((*current)->intg_part_size + (*current)->frac_part_size) > 16)
mb->data[index++] = (uint16_t)(i_val >> 16);
mb->data[index++] = (uint16_t)i_val;
if(index > mb->count) return EXIT_FAILURE;
goto nextkey;
}
current = &((*current)->next);
}
}
mb->count = index;
return EXIT_SUCCESS;
}
int assign_values(const char *js, jsmntok_t *t, size_t count, void *data)
{
int i;
int r = count;
cfg_struct *cfg = (cfg_struct *)data;
data_selection_t *list = &(cfg->list);
/* Assume the top-level element is an object */
if (r < 1 || t[0].type != JSMN_OBJECT) {
printf("Object expected\n");
return 1;
}
/* Loop over all keys of the root object */
for (i = 1; i < r; i++) {
if (jsoneq(js, &t[i], "url") == 0) {
cfg->url = strndup(js + t[i+1].start, t[i+1].end - t[i+1].start);
i++;
} else if (jsoneq(js, &t[i], "period") == 0) {
cfg->period = strtol(js + t[i+1].start, NULL, 10);
i++;
} else if (t[i].type == JSMN_ARRAY)
{
data_selection_t **add = &(list->next);
while(*add != NULL)
{
add = &((*add)->next);
}
*add = (data_selection_t *) malloc(sizeof(data_selection_t));
(*add)->name = strndup(js + t[i-1].start, t[i-1].end - t[i-1].start);
(*add)->intg_part_size = strtol(js + t[++i].start, NULL, 10);
(*add)->frac_part_size = strtol(js + t[++i].start, NULL, 10);
(*add)->next = NULL;
}
}
return EXIT_SUCCESS;
}
int read_json( char *js, size_t jslen, void *cb_data, int callback(const char *js, jsmntok_t *t, size_t count, void *data))
{
jsmn_parser p;
jsmntok_t *tok;
size_t tokcount = 2;
/* Prepare parser */
jsmn_init(&p);
/* Allocate some tokens as a start */
tok = malloc(sizeof(*tok) * tokcount);
if (tok == NULL) {
fprintf(stderr, "malloc(): errno=%d\n", errno);
return EXIT_FAILURE;
}
again:;
int r = jsmn_parse(&p, js, jslen, tok, tokcount);
if (r < 0) {
if (r == JSMN_ERROR_NOMEM) {
tokcount = tokcount * 2;
tok = realloc_it(tok, sizeof(*tok) * tokcount);
if (tok == NULL) {
return EXIT_FAILURE;
}
goto again;
}
} else {
callback(js, tok, p.toknext, cb_data);
}
free(tok);
return EXIT_SUCCESS;
}
int create_structure(FILE *config, cfg_struct *data)
{
int r;
char *js = NULL;
size_t jslen = 0;
char buf[BUFSIZ];
for (;;) {
/* Read another chunk */
r = fread(buf, 1, sizeof(buf), config);
if (r < 0) {
fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
return 1;
}
if (r == 0) {
return 0;
}
js = realloc_it(js, jslen + r + 1);
if (js == NULL) {
return 3;
}
strncpy(js + jslen, buf, r);
jslen = jslen + r;
read_json(js,jslen,(void *)data,&assign_values);
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
pid_t process;
int s = -1;
uint8_t mb_address = 1;
modbus_t *ctx;
modbus_mapping_t *mb_mapping;
int rc;
int use_backend;
uint8_t *query;
if (argc > 1) {
if (strcmp(argv[1], "tcp") == 0) {
use_backend = TCP;
} else if (strcmp(argv[1], "tcppi") == 0) {
use_backend = TCP_PI;
} else if (strcmp(argv[1], "rtu") == 0) {
use_backend = RTU;
} else {
printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for JSON web-api data retrieval\n\n", argv[0]);
return -1;
}
} else {
/* By default */
use_backend = TCP;
}
cfg_struct cfg_data = {.url = NULL, .period = 3600, .list = {0} };
FILE *cfg_file;
cfg_file = fopen("config.json","r");
create_structure(cfg_file,&cfg_data);
fclose(cfg_file);
if(cfg_data.url == NULL) return EXIT_FAILURE;
if(cfg_data.list.next == NULL) return EXIT_FAILURE;
pthread_mutex_t mutex;
pthread_mutex_init(&mutex,NULL);
shared_regs mbr;
mbr.keys = &(cfg_data.list.next);
mbr.count = 2048; //whole 4096 page will be allocated anyways
mbr.data = mmap(NULL,mbr.count*sizeof(uint16_t),PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if(mbr.data == NULL) return EXIT_FAILURE;
process = fork();
if(process < 0) return EXIT_FAILURE;
if(process == 0) { // child - the slow stuff
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_handle = curl_easy_init();
if(curl_handle) {
curl_easy_setopt(curl_handle, CURLOPT_URL, cfg_data.url);
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
for (;;) {
res = curl_easy_perform(curl_handle);
if(res != CURLE_OK) {
fprintf(stderr, "error: %s\n", curl_easy_strerror(res));
} else {
while(pthread_mutex_trylock(&mutex)); // blocking while accessed by the fast process
read_json(chunk.memory,chunk.size,(void*)&mbr,&extract);
pthread_mutex_unlock(&mutex);
printf("Data successfuly fetched from JSON.\n");
}
sleep(cfg_data.period);
}
curl_easy_cleanup(curl_handle);
}
free(chunk.memory);
free(cfg_data.url);
free_structure(&(cfg_data.list));
} else { // parent - the fast process
if (use_backend == TCP) {
ctx = modbus_new_tcp("127.0.0.1", 1502);
query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
} else if (use_backend == TCP_PI) {
ctx = modbus_new_tcp_pi("::0", "1502");
query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
} else {
ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
modbus_set_slave(ctx, mb_address);
query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
}
modbus_set_debug(ctx, FALSE);
mb_mapping = modbus_mapping_new(0, 0, 0, mbr.count);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
if (use_backend == TCP) {
s = modbus_tcp_listen(ctx, 1);
modbus_tcp_accept(ctx, &s);
} else if (use_backend == TCP_PI) {
s = modbus_tcp_pi_listen(ctx, 1);
modbus_tcp_pi_accept(ctx, &s);
} else {
rc = modbus_connect(ctx);
if (rc == -1) {
fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
}
printf("Modbus connected.\n");
for (;;) {
do {
rc = modbus_receive(ctx, query);
/* Filtered queries return 0 */
} while (rc == 0);
/* The connection is not closed on errors which require on reply such as
bad CRC in RTU. */
if (rc == -1 && errno != EMBBADCRC) {
/* Quit */
break;
}
if(0 == pthread_mutex_trylock(&mutex)) { // skipping while accessed by the slow process
memcpy(mb_mapping->tab_input_registers, mbr.data, mbr.count * sizeof(uint16_t));
pthread_mutex_unlock(&mutex);
}
rc = modbus_reply(ctx, query, rc, mb_mapping);
if (rc == -1) {
break;
}
}
printf("Quit the loop: %s\n", modbus_strerror(errno));
if (use_backend == TCP) {
if (s != -1) {
close(s);
}
}
modbus_mapping_free(mb_mapping);
free(query);
/* For RTU */
modbus_close(ctx);
modbus_free(ctx);
kill(process, SIGTERM);
printf("Waiting for child process.\n");
wait(NULL);
}
munmap(mbr.data,mbr.count*sizeof(uint16_t));
printf("Bye!\n");
return EXIT_SUCCESS;
}