gateway.c 15.6 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
2
3
4
5
6
7
8
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
#include<stdint.h>
#include<pthread.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
9
#include<gateway_protocol.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
10
#include<sys/time.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
11
#include<libpq-fe.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
12
13
14
#include"base64.h"
#include<math.h>
#include<signal.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
15

Vladislav Rykov's avatar
Vladislav Rykov committed
16
#define TIMEDATE_LENGTH		32
Vladislav Rykov's avatar
Vladislav Rykov committed
17
#define PEND_SEND_RETRIES_MAX	5
Vladislav Rykov's avatar
Vladislav Rykov committed
18

Vladislav Rykov's avatar
Vladislav Rykov committed
19
20
typedef struct {
	uint32_t utc;
Vladislav Rykov's avatar
Vladislav Rykov committed
21
	char timedate[TIMEDATE_LENGTH];
Vladislav Rykov's avatar
Vladislav Rykov committed
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

	float dht22_t_esp;
	float dht22_h_esp;
	float sht85_t_esp;
	float sht85_h_esp;
	float hih8121_t_esp;
	float hih8121_h_esp;
	float tmp36_0_esp;
	float tmp36_1_esp;
	float tmp36_2_esp;
	float hih4030_esp;
	float hh10d_esp;

	float dht22_t_mkr;
	float dht22_h_mkr;
	float sht85_t_mkr;
	float sht85_h_mkr;
	float hih8121_t_mkr;
	float hih8121_h_mkr;
	float hh10d_mkr;

	float dht22_t_wis;
	float dht22_h_wis;
	float sht85_t_wis;
	float sht85_h_wis;
	float hih8121_t_wis;
	float hih8121_h_wis;
	float tmp102_wis;
	float hh10d_wis;
} sensor_data_t;

Vladislav Rykov's avatar
Vladislav Rykov committed
53
54
55
56
57
58
59
60
61
62
63
64
typedef struct {
	uint8_t dev_id;
	int server_desc;
	int client_desc;
	struct sockaddr_in server;
	struct sockaddr_in client;
	int sock_len;
} gcom_ch_t; // gateway communication channel

int send_gcom_ch(gcom_ch_t *gch, uint8_t *pck, uint8_t pck_size);
int recv_gcom_ch(gcom_ch_t *gch, uint8_t *pck, uint8_t *pck_length, uint16_t pck_size);

Vladislav Rykov's avatar
Vladislav Rykov committed
65
void * connection_handler (void *args);
Vladislav Rykov's avatar
Vladislav Rykov committed
66
uint8_t gateway_protocol_data_send_payload_decode(sensor_data_t *sensor_data, const uint8_t *payload, const uint8_t payload_length);
Vladislav Rykov's avatar
Vladislav Rykov committed
67
68
void prepare_di_query(sensor_data_t *sensor_data, char *q, uint16_t q_size); // data insert
void filter_query(char *com);
Vladislav Rykov's avatar
Vladislav Rykov committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void packet_encode(
	uint8_t dev_id, 
	gateway_protocol_packet_type_t p_type, 
	uint8_t payload_length,
	uint8_t *payload,
	uint8_t *packet_length,
	uint8_t *packet);
uint8_t packet_decode(
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
	uint8_t packet_length,
	uint8_t *packet);

Vladislav Rykov's avatar
Vladislav Rykov committed
84
85
86
87
88
89
90
91
void gateway_protocol_mk_stat(
	gcom_ch_t *gch,
	gateway_protocol_stat_t stat,
	uint8_t *pck,
	uint8_t *pck_len);

void send_utc(gcom_ch_t *pch);

Vladislav Rykov's avatar
Vladislav Rykov committed
92
93
void ctrc_handler (int sig);
static volatile uint8_t working = 1;
Vladislav Rykov's avatar
Vladislav Rykov committed
94
95

int main (int argc, char **argv) {
Vladislav Rykov's avatar
Vladislav Rykov committed
96
	gcom_ch_t gch;
Vladislav Rykov's avatar
Vladislav Rykov committed
97
98
	uint8_t buf[1024];
	uint8_t buf_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
99
	uint8_t payload[256];
Vladislav Rykov's avatar
Vladislav Rykov committed
100
	uint8_t payload_length = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
101
102
103
	PGresult *res;
	
	signal(SIGINT, ctrc_handler);
Vladislav Rykov's avatar
Vladislav Rykov committed
104

Vladislav Rykov's avatar
Vladislav Rykov committed
105
106
107
108
109
110
	PGconn *conn = PQconnectdb("user=root dbname=gateway");
	if (PQstatus(conn) == CONNECTION_BAD) {
		printf("connection to db error: %s\n", PQerrorMessage(conn));
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
111
	if ((gch.server_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
Vladislav Rykov's avatar
Vladislav Rykov committed
112
113
114
115
		perror("socket creation error");
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
116
117
118
	gch.server.sin_family 		= AF_INET;
	gch.server.sin_port		= htons(9043);
	gch.server.sin_addr.s_addr 	= INADDR_ANY;
Vladislav Rykov's avatar
Vladislav Rykov committed
119

Vladislav Rykov's avatar
Vladislav Rykov committed
120
	if (bind(gch.server_desc, (struct sockaddr *) &gch.server, sizeof(gch.server)) < 0) {
Vladislav Rykov's avatar
Vladislav Rykov committed
121
122
123
124
		perror("binding error");
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
125
126
127
	gateway_protocol_packet_type_t packet_type;
	
	while (working) {
Vladislav Rykov's avatar
Vladislav Rykov committed
128
		buf_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
129
		printf("listenninig...\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
130
131
132
		gch.sock_len = sizeof(gch.client);
		
		recv_gcom_ch(&gch, buf, &buf_len, 1024);
Vladislav Rykov's avatar
Vladislav Rykov committed
133

Vladislav Rykov's avatar
Vladislav Rykov committed
134
		if (gateway_protocol_packet_decode(
Vladislav Rykov's avatar
Vladislav Rykov committed
135
					&gch.dev_id,
Vladislav Rykov's avatar
Vladislav Rykov committed
136
137
138
139
140
141
					&packet_type,
					&payload_length, payload,
					buf_len, buf))
		{
			if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ) {
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
142
				send_utc(&gch);
Vladislav Rykov's avatar
Vladislav Rykov committed
143
144
145
146
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_DATA_SEND) {
				sensor_data_t sensor_data;
				printf("DATA SEND received\n");
		        	if (gateway_protocol_data_send_payload_decode(&sensor_data, payload, payload_length)) {
Vladislav Rykov's avatar
Vladislav Rykov committed
147
148
					prepare_di_query(&sensor_data, buf, sizeof(buf));
					filter_query(buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
149
					printf("%s\n", buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
150
					res = PQexec(conn, buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
151
152
153
					if (PQresultStatus(res) == PGRES_COMMAND_OK) {
						PQclear(res);

Vladislav Rykov's avatar
Vladislav Rykov committed
154
						sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
155
156
						res = PQexec(conn, buf);
						if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
Vladislav Rykov's avatar
Vladislav Rykov committed
157
158
159
160
							gateway_protocol_mk_stat(
								&gch, 
								GATEWAY_PROTOCOL_STAT_ACK_PEND,
								buf, &buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
161
162
							printf("ACK_PEND prepared\n");
						} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
163
164
165
166
							gateway_protocol_mk_stat(
								&gch, 
								GATEWAY_PROTOCOL_STAT_ACK,
								buf, &buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
167
168
							printf("ACK prepared\n");
						}
Vladislav Rykov's avatar
Vladislav Rykov committed
169
170
						
						send_gcom_ch(&gch, buf, buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
171
172
					} else {
						fprintf(stderr, "database error : %s\n", PQerrorMessage(conn));
Vladislav Rykov's avatar
Vladislav Rykov committed
173
174
					}
					PQclear(res);
Vladislav Rykov's avatar
Vladislav Rykov committed
175
				} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
176
177
178
179
180
181
182
					gateway_protocol_mk_stat(
						&gch,
						GATEWAY_PROTOCOL_STAT_NACK,
						buf, &buf_len);
					
					send_gcom_ch(&gch, buf, buf_len);
					
Vladislav Rykov's avatar
Vladislav Rykov committed
183
					fprintf(stderr, "payload decode error\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
184
185
				}
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ) {
Vladislav Rykov's avatar
Vladislav Rykov committed
186
				sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
187
188
189
190
191
192
193
194
195
196
197
				res = PQexec(conn, buf);
				if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
					char msg_cont[150];
					strncpy(msg_cont, PQgetvalue(res, 0, 1), sizeof(msg_cont));
					printf("PEND_SEND prepared : %s\n", PQgetvalue(res, 0, 1));

					base64_decode(PQgetvalue(res, 0, 1), strlen(PQgetvalue(res, 0, 1)), payload);
					payload_length = BASE64_DECODE_OUT_SIZE(strlen(PQgetvalue(res, 0, 1)));
					PQclear(res);
					printf("prepared to send %d bytes\n", payload_length);
					
Vladislav Rykov's avatar
Vladislav Rykov committed
198
199
200
201
202
203
204
205
206
207
208
209
					// send the msg until ack is received
					uint8_t received_ack = 0;
					uint8_t pend_send_retries = PEND_SEND_RETRIES_MAX;
					do {
						packet_encode(
							gch.dev_id, 
							GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND,
							payload_length, payload,
							&buf_len, buf);
	
						send_gcom_ch(&gch, buf, buf_len);
						recv_gcom_ch(&gch, buf, &buf_len, 1024);
Vladislav Rykov's avatar
Vladislav Rykov committed
210

Vladislav Rykov's avatar
Vladislav Rykov committed
211
212
213
214
215
						if (packet_decode(
							&gch.dev_id, 
							&packet_type,
							&buf_len, buf,
							buf_len, buf)) 
Vladislav Rykov's avatar
Vladislav Rykov committed
216
						{
Vladislav Rykov's avatar
Vladislav Rykov committed
217
218
219
220
221
222
223
224
225
226
227
228
229
							if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_STAT &&
								buf_len == 1 &&
								buf[0] == GATEWAY_PROTOCOL_STAT_ACK)
							{
								sprintf(buf, "DELETE FROM pend_msgs WHERE dev_id = %d AND msg = '%s'", gch.dev_id, msg_cont);
								printf("%s", buf);
								res = PQexec(conn, buf);
								if (PQresultStatus(res) != PGRES_COMMAND_OK) {
									fprintf(stderr, "error db deleting : %s", PQerrorMessage(conn));
								}
								PQclear(res);
								received_ack = 1;
								printf("ACK received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
230
231
							}
						}
Vladislav Rykov's avatar
Vladislav Rykov committed
232
					} while (!received_ack && pend_send_retries--);
Vladislav Rykov's avatar
Vladislav Rykov committed
233
				}
Vladislav Rykov's avatar
Vladislav Rykov committed
234
			} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
235
				fprintf(stderr, "packet type error : %02X\n", packet_type);
Vladislav Rykov's avatar
Vladislav Rykov committed
236
237
			}
		} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
238
239
			fprintf(stderr, "packet decode error (type : %02X, pck_len = %d, pay_len = %d\n", 
					packet_type, buf_len, payload_length);
Vladislav Rykov's avatar
Vladislav Rykov committed
240
		}
Vladislav Rykov's avatar
Vladislav Rykov committed
241
242
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
243
	close(gch.server_desc);
Vladislav Rykov's avatar
Vladislav Rykov committed
244
	PQfinish(conn);
Vladislav Rykov's avatar
Vladislav Rykov committed
245

Vladislav Rykov's avatar
Vladislav Rykov committed
246
247
248
	return EXIT_SUCCESS;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
249
250
251
252
void ctrc_handler (int sig) {
	working = 0;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
253
254
255
256
257
uint8_t gateway_protocol_data_send_payload_decode(
	sensor_data_t *sensor_data, 
	const uint8_t *payload, 
	const uint8_t payload_length) 
{
Vladislav Rykov's avatar
Vladislav Rykov committed
258
259
260
261
262
263
264
	uint8_t p_len = 0;

	memcpy(&sensor_data->utc, &payload[p_len], sizeof(sensor_data->utc));
	p_len += sizeof(sensor_data->utc);

	memcpy(&sensor_data->timedate, &payload[p_len], sizeof(sensor_data->timedate));
	//p_len += sizeof(sensor_data->timedate);
Vladislav Rykov's avatar
Vladislav Rykov committed
265
	p_len += TIMEDATE_LENGTH;
Vladislav Rykov's avatar
Vladislav Rykov committed
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


	memcpy(&sensor_data->dht22_t_esp, &payload[p_len], sizeof(sensor_data->dht22_t_esp));
	p_len += sizeof(sensor_data->dht22_t_esp);

	memcpy(&sensor_data->dht22_h_esp, &payload[p_len], sizeof(sensor_data->dht22_h_esp));
	p_len += sizeof(sensor_data->dht22_h_esp);

	memcpy(&sensor_data->sht85_t_esp, &payload[p_len], sizeof(sensor_data->sht85_t_esp));
	p_len += sizeof(sensor_data->sht85_t_esp);

	memcpy(&sensor_data->sht85_h_esp, &payload[p_len], sizeof(sensor_data->sht85_h_esp));
	p_len += sizeof(sensor_data->sht85_h_esp);

	memcpy(&sensor_data->hih8121_t_esp, &payload[p_len], sizeof(sensor_data->hih8121_t_esp));
	p_len += sizeof(sensor_data->hih8121_t_esp);

	memcpy(&sensor_data->hih8121_h_esp, &payload[p_len], sizeof(sensor_data->hih8121_h_esp));
	p_len += sizeof(sensor_data->hih8121_h_esp);

	memcpy(&sensor_data->tmp36_0_esp, &payload[p_len], sizeof(sensor_data->tmp36_0_esp));
	p_len += sizeof(sensor_data->tmp36_0_esp);

	memcpy(&sensor_data->tmp36_1_esp, &payload[p_len], sizeof(sensor_data->tmp36_1_esp));
	p_len += sizeof(sensor_data->tmp36_1_esp);

	memcpy(&sensor_data->tmp36_2_esp, &payload[p_len], sizeof(sensor_data->tmp36_2_esp));
	p_len += sizeof(sensor_data->tmp36_2_esp);

	memcpy(&sensor_data->hih4030_esp, &payload[p_len], sizeof(sensor_data->hih4030_esp));
	p_len += sizeof(sensor_data->hih4030_esp);

	memcpy(&sensor_data->hh10d_esp, &payload[p_len], sizeof(sensor_data->hh10d_esp));
	p_len += sizeof(sensor_data->hh10d_esp);


	memcpy(&sensor_data->dht22_t_mkr, &payload[p_len], sizeof(sensor_data->dht22_t_mkr));
	p_len += sizeof(sensor_data->dht22_t_mkr);

	memcpy(&sensor_data->dht22_h_mkr, &payload[p_len], sizeof(sensor_data->dht22_h_mkr));
	p_len += sizeof(sensor_data->dht22_h_mkr);

	memcpy(&sensor_data->sht85_t_mkr, &payload[p_len], sizeof(sensor_data->sht85_t_mkr));
	p_len += sizeof(sensor_data->sht85_t_mkr);

	memcpy(&sensor_data->sht85_h_mkr, &payload[p_len], sizeof(sensor_data->sht85_h_mkr));
	p_len += sizeof(sensor_data->sht85_h_esp);

	memcpy(&sensor_data->hih8121_t_mkr, &payload[p_len], sizeof(sensor_data->hih8121_t_mkr));
	p_len += sizeof(sensor_data->hih8121_t_mkr);

	memcpy(&sensor_data->hih8121_h_mkr, &payload[p_len], sizeof(sensor_data->hih8121_h_mkr));
	p_len += sizeof(sensor_data->hih8121_h_mkr);

	memcpy(&sensor_data->hh10d_mkr, &payload[p_len], sizeof(sensor_data->hh10d_mkr));
	p_len += sizeof(sensor_data->hh10d_mkr);

	
	memcpy(&sensor_data->dht22_t_wis, &payload[p_len], sizeof(sensor_data->dht22_t_wis));
	p_len += sizeof(sensor_data->dht22_t_wis);

	memcpy(&sensor_data->dht22_h_wis, &payload[p_len], sizeof(sensor_data->dht22_h_wis));
	p_len += sizeof(sensor_data->dht22_h_wis);

	memcpy(&sensor_data->sht85_t_wis, &payload[p_len], sizeof(sensor_data->sht85_t_wis));
	p_len += sizeof(sensor_data->sht85_t_wis);

	memcpy(&sensor_data->sht85_h_wis, &payload[p_len], sizeof(sensor_data->sht85_h_wis));
	p_len += sizeof(sensor_data->sht85_h_wis);

	memcpy(&sensor_data->hih8121_t_wis, &payload[p_len], sizeof(sensor_data->hih8121_t_wis));
	p_len += sizeof(sensor_data->hih8121_t_wis);

	memcpy(&sensor_data->hih8121_h_wis, &payload[p_len], sizeof(sensor_data->hih8121_h_wis));
	p_len += sizeof(sensor_data->hih8121_h_wis);

	memcpy(&sensor_data->tmp102_wis, &payload[p_len], sizeof(sensor_data->tmp102_wis));
	p_len += sizeof(sensor_data->tmp102_wis);

	memcpy(&sensor_data->hh10d_wis, &payload[p_len], sizeof(sensor_data->hh10d_wis));
	p_len += sizeof(sensor_data->hh10d_wis);

Vladislav Rykov's avatar
Vladislav Rykov committed
348
349
350
	//printf("p_len = %d, payload_length = %d\n", p_len, payload_length);
				

Vladislav Rykov's avatar
Vladislav Rykov committed
351
352
353
	return (p_len == payload_length);
}

Vladislav Rykov's avatar
Vladislav Rykov committed
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
void prepare_di_query(sensor_data_t *sensor_data, char *q, uint16_t q_size) {
	snprintf(q, q_size, "INSERT INTO esp32 ("
					"utc, timedate, "
					"dht22_t_esp, dht22_h_esp, "
					"sht85_t_esp, sht85_h_esp, "
					"hih8121_t_esp, hih8121_h_esp, "
					"tmp36_0_esp, tmp36_1_esp, tmp36_2_esp, "
					"hih4030_esp, "
					"hh10d_esp, "
					"dht22_t_mkr, dht22_h_mkr, "
					"sht85_t_mkr, sht85_h_mkr, "
					"hih8121_t_mkr, hih8121_h_mkr, "
					"hh10d_mkr, "
					"dht22_t_wis, dht22_h_wis, "
					"sht85_t_wis, sht85_h_wis, "
					"hih8121_t_wis, hih8121_h_wis, "
					"tmp102_wis, "
					"hh10d_wis) "
					"VALUES("
					"%lu, '%s', "
					"%.2f, %.2f, "  // esp
					"%.2f, %.2f, "
					"%.2f, %.2f, "
					"%.2f, %.2f, %.2f, "
					"%.2f, "
					"%.2f, "
					"%.2f, %.2f, "  // mkr
					"%.2f, %.2f, "
					"%.2f, %.2f, "
					"%.2f, "
					"%.2f, %.2f, "  // wis
					"%.2f, %.2f, "
					"%.2f, %.2f, "
					"%.2f, "
					"%.2f)",
					sensor_data->utc, sensor_data->timedate,
					sensor_data->dht22_t_esp, sensor_data->dht22_h_esp,
					sensor_data->sht85_t_esp, sensor_data->sht85_h_esp,
					sensor_data->hih8121_t_esp, sensor_data->hih8121_h_esp,
					sensor_data->tmp36_0_esp, sensor_data->tmp36_1_esp, sensor_data->tmp36_2_esp,
					sensor_data->hih4030_esp,
					sensor_data->hh10d_esp,
					sensor_data->dht22_t_mkr, sensor_data->dht22_h_mkr,
					sensor_data->sht85_t_mkr, sensor_data->sht85_h_mkr,
					sensor_data->hih8121_t_mkr, sensor_data->hih8121_h_mkr,
					sensor_data->hh10d_mkr,
					sensor_data->dht22_t_wis, sensor_data->dht22_h_wis,
					sensor_data->sht85_t_wis, sensor_data->sht85_h_wis,
					sensor_data->hih8121_t_wis, sensor_data->hih8121_h_wis,
					sensor_data->tmp102_wis,
					sensor_data->hh10d_wis
				);
Vladislav Rykov's avatar
Vladislav Rykov committed
406

Vladislav Rykov's avatar
Vladislav Rykov committed
407
408
409
}

void filter_query(char *q) {
Vladislav Rykov's avatar
Vladislav Rykov committed
410
411
412
	char *pchr;
	const char nanstr[] = "'NaN'";

Vladislav Rykov's avatar
Vladislav Rykov committed
413
	while((pchr = strstr(q, "nan"))) {
Vladislav Rykov's avatar
Vladislav Rykov committed
414
415
416
		memmove(&pchr[5], &pchr[3], strlen(pchr)+1);
		memcpy(pchr, nanstr, sizeof(nanstr)-1);
	}
Vladislav Rykov's avatar
Vladislav Rykov committed
417
	while(pchr = strchr(q, '-')) {
Vladislav Rykov's avatar
Vladislav Rykov committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
		memmove(pchr, &pchr[1], strlen(pchr));
	}
}


void packet_encode(
	uint8_t dev_id, 
	gateway_protocol_packet_type_t p_type, 
	uint8_t payload_length,
	uint8_t *payload,
	uint8_t *packet_length,
	uint8_t *packet) 
{
	*packet_length = 0;

	packet[*packet_length] = dev_id;
	(*packet_length)++;

	packet[*packet_length] = p_type;
	(*packet_length)++;

	packet[*packet_length] = payload_length;
	(*packet_length)++;

	memcpy(&packet[*packet_length], payload, payload_length);
	*packet_length += payload_length;
}

uint8_t packet_decode(
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
	uint8_t packet_length,
	uint8_t *packet)
{
	uint8_t p_len = 0;

	*dev_id = packet[p_len];
	p_len++;

	*ptype = (gateway_protocol_packet_type_t) packet[p_len];
	p_len++;

	*payload_length = packet[p_len];
	p_len++;

	memcpy(payload, &packet[p_len], *payload_length);
	p_len += *payload_length;

	return p_len == packet_length;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
471
472
473
474
475
476
477
478
479
480
481
482
483
void gateway_protocol_mk_stat(
	gcom_ch_t *gch,
	gateway_protocol_stat_t stat,
	uint8_t *pck,
	uint8_t *pck_len)
{
	pck[0] = gch->dev_id;
	pck[1] = GATEWAY_PROTOCOL_PACKET_TYPE_STAT;
	pck[2] = 1;
	pck[3] = stat;
	*pck_len = 4;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
484
485


Vladislav Rykov's avatar
Vladislav Rykov committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
void send_utc(gcom_ch_t *gch) {
	uint8_t buf[10];
	uint8_t buf_len = 0;
	struct timeval tv;
				
	gettimeofday(&tv, NULL);
				
	packet_encode (
		gch->dev_id,
		GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND,
		sizeof(tv.tv_sec), (uint8_t *)&tv.tv_sec,
		&buf_len, buf
	);
					
	send_gcom_ch(gch, buf, buf_len);
}

int send_gcom_ch(gcom_ch_t *gch, uint8_t *pck, uint8_t pck_size) {
	int ret;
	
	for(uint8_t i = 0; i < pck_size; i++) {
		printf("%02X : ", pck[i]);
	}
	printf("\n");
	
	if (sendto(gch->server_desc, (char *)pck, pck_size, 0, (struct sockaddr *)&gch->client, gch->sock_len) < 0) {
		perror("sendto error");
	}
	return ret;
}

int recv_gcom_ch(gcom_ch_t *gch, uint8_t *pck, uint8_t *pck_length, uint16_t pck_size) {
	if ((*pck_length = recvfrom(gch->server_desc, (char *)pck, pck_size, MSG_WAITALL, (struct sockaddr *)&gch->client, &gch->sock_len)) < 0) {
		perror("socket receive error");
	}

	for(uint8_t i = 0; i < *pck_length; i++) {
		printf("%02X : ", pck[i]);
	}
	printf("\n");
}
Vladislav Rykov's avatar
Vladislav Rykov committed
527
528
/* connection handler for multithreading version */
#ifdef MULTITHREADING_VER
Vladislav Rykov's avatar
Vladislav Rykov committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
void *connection_handler(void *args) {
	int client_desc = *(int *)args;
	
	uint8_t buf[128] = "";
	uint8_t buf_len = 0;
	uint8_t payload[128];
	uint8_t payload_length = 0;

	if ((buf_len = recv(client_desc, buf, sizeof(buf), 0)) > 0) {
		uint8_t dev_id = 0xFF;
		gateway_protocol_packet_type_t packet_type;
		
		if (gateway_protocol_packet_decode(
					&dev_id,
					&packet_type,
					&payload_length, payload,
					buf_len, buf))
		{
			if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ) {
Vladislav Rykov's avatar
Vladislav Rykov committed
548
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
				struct timeval tv;
				buf_len = 0;
				
				buf[0] = dev_id;
				buf_len++;

				buf[1] = GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND;
				buf_len++;

				gettimeofday(&tv, NULL);
				memcpy(&buf[buf_len], &tv.tv_sec, sizeof(uint32_t));
				buf_len += sizeof(uint32_t);

				write(client_desc, buf, buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
563
564
			} else {
				perror("packet type error");
Vladislav Rykov's avatar
Vladislav Rykov committed
565
			}
Vladislav Rykov's avatar
Vladislav Rykov committed
566
567
		} else {
			perror("packet decode error");
Vladislav Rykov's avatar
Vladislav Rykov committed
568
569
570
571
		}
	}

}
Vladislav Rykov's avatar
Vladislav Rykov committed
572
#endif