gateway.c 16.4 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
typedef struct {
Vladislav Rykov's avatar
Vladislav Rykov committed
54
	uint8_t app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE +1];
Vladislav Rykov's avatar
Vladislav Rykov committed
55
56
57
58
59
60
61
62
63
64
65
	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
66
void * connection_handler (void *args);
Vladislav Rykov's avatar
Vladislav Rykov committed
67
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
68
69
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
70
void packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
71
72
73
74
75
	const uint8_t *app_key,
	const uint8_t dev_id, 
	const gateway_protocol_packet_type_t p_type, 
	const uint8_t payload_length,
	const uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
76
77
78
	uint8_t *packet_length,
	uint8_t *packet);
uint8_t packet_decode(
Vladislav Rykov's avatar
Vladislav Rykov committed
79
	uint8_t *app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
80
81
82
83
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
84
85
	const uint8_t packet_length,
	const uint8_t *packet);
Vladislav Rykov's avatar
Vladislav Rykov committed
86

Vladislav Rykov's avatar
Vladislav Rykov committed
87
88
89
90
91
92
93
94
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
95
96
void ctrc_handler (int sig);
static volatile uint8_t working = 1;
Vladislav Rykov's avatar
Vladislav Rykov committed
97
98

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

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

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

Vladislav Rykov's avatar
Vladislav Rykov committed
119
120
121
	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
122

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

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

Vladislav Rykov's avatar
Vladislav Rykov committed
137
138
139
140
141
142
		if (packet_decode(
			gch.app_key,
			&gch.dev_id,
			&packet_type,
			&payload_length, payload,
			buf_len, buf))
Vladislav Rykov's avatar
Vladislav Rykov committed
143
144
145
		{
			if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ) {
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
146
				send_utc(&gch);
Vladislav Rykov's avatar
Vladislav Rykov committed
147
148
149
150
			} 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
151
					prepare_di_query(&gch, &sensor_data, buf, sizeof(buf));
Vladislav Rykov's avatar
Vladislav Rykov committed
152
					filter_query(buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
153
					printf("%s\n", buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
154
					res = PQexec(conn, buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
155
156
157
					if (PQresultStatus(res) == PGRES_COMMAND_OK) {
						PQclear(res);

Vladislav Rykov's avatar
Vladislav Rykov committed
158
						sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
159
160
						res = PQexec(conn, buf);
						if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
Vladislav Rykov's avatar
Vladislav Rykov committed
161
162
163
164
							gateway_protocol_mk_stat(
								&gch, 
								GATEWAY_PROTOCOL_STAT_ACK_PEND,
								buf, &buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
165
166
							printf("ACK_PEND prepared\n");
						} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
167
168
169
170
							gateway_protocol_mk_stat(
								&gch, 
								GATEWAY_PROTOCOL_STAT_ACK,
								buf, &buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
171
172
							printf("ACK prepared\n");
						}
Vladislav Rykov's avatar
Vladislav Rykov committed
173
174
						
						send_gcom_ch(&gch, buf, buf_len);
Vladislav Rykov's avatar
Vladislav Rykov committed
175
176
					} else {
						fprintf(stderr, "database error : %s\n", PQerrorMessage(conn));
Vladislav Rykov's avatar
Vladislav Rykov committed
177
178
					}
					PQclear(res);
Vladislav Rykov's avatar
Vladislav Rykov committed
179
				} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
180
181
182
183
184
185
186
					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
187
					fprintf(stderr, "payload decode error\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
188
189
				}
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ) {
Vladislav Rykov's avatar
Vladislav Rykov committed
190
191
				sprintf(buf, "SELECT * FROM pend_msgs WHERE app_key = %s AND dev_id = %d", 
						(char *)gch.app_key, gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
192
193
194
195
196
197
198
199
200
				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);
Vladislav Rykov's avatar
Vladislav Rykov committed
201
					printf("prepared to send %d bytes : %s\n", payload_length, (char *)payload);
Vladislav Rykov's avatar
Vladislav Rykov committed
202
					
Vladislav Rykov's avatar
Vladislav Rykov committed
203
204
205
206
207
					// send the msg until ack is received
					uint8_t received_ack = 0;
					uint8_t pend_send_retries = PEND_SEND_RETRIES_MAX;
					do {
						packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
208
							gch.app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
209
210
211
212
213
214
215
							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
216

Vladislav Rykov's avatar
Vladislav Rykov committed
217
218
						uint8_t recv_app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE];
						uint8_t recv_dev_id = 0xFF;
Vladislav Rykov's avatar
Vladislav Rykov committed
219
						if (packet_decode(
Vladislav Rykov's avatar
Vladislav Rykov committed
220
221
							recv_app_key,
							&recv_dev_id, 
Vladislav Rykov's avatar
Vladislav Rykov committed
222
223
224
							&packet_type,
							&buf_len, buf,
							buf_len, buf)) 
Vladislav Rykov's avatar
Vladislav Rykov committed
225
						{
Vladislav Rykov's avatar
Vladislav Rykov committed
226
227
228
							if (!memcpy(recv_app_key, gch.app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE) &&
								recv_dev_id == gch.dev_id &&
								packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_STAT &&
Vladislav Rykov's avatar
Vladislav Rykov committed
229
230
231
								buf_len == 1 &&
								buf[0] == GATEWAY_PROTOCOL_STAT_ACK)
							{
Vladislav Rykov's avatar
Vladislav Rykov committed
232
								sprintf(buf, "DELETE FROM pend_msgs WHERE app_key = %s AND dev_id = %d AND msg = '%s'", (char *)gch.app_key, gch.dev_id, msg_cont);
Vladislav Rykov's avatar
Vladislav Rykov committed
233
234
235
236
237
238
239
240
								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
241
242
							}
						}
Vladislav Rykov's avatar
Vladislav Rykov committed
243
					} while (!received_ack && pend_send_retries--);
Vladislav Rykov's avatar
Vladislav Rykov committed
244
				}
Vladislav Rykov's avatar
Vladislav Rykov committed
245
			} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
246
				fprintf(stderr, "packet type error : %02X\n", packet_type);
Vladislav Rykov's avatar
Vladislav Rykov committed
247
248
			}
		} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
249
250
			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
251
		}
Vladislav Rykov's avatar
Vladislav Rykov committed
252
253
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
254
	close(gch.server_desc);
Vladislav Rykov's avatar
Vladislav Rykov committed
255
	PQfinish(conn);
Vladislav Rykov's avatar
Vladislav Rykov committed
256

Vladislav Rykov's avatar
Vladislav Rykov committed
257
258
259
	return EXIT_SUCCESS;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
260
261
262
263
void ctrc_handler (int sig) {
	working = 0;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
264
265
266
267
268
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
269
270
271
272
273
274
275
	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
276
	p_len += TIMEDATE_LENGTH;
Vladislav Rykov's avatar
Vladislav Rykov committed
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


	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
359
	printf("p_len = %d, payload_length = %d\n", p_len, payload_length);
Vladislav Rykov's avatar
Vladislav Rykov committed
360
361
				

Vladislav Rykov's avatar
Vladislav Rykov committed
362
363
364
	return (p_len == payload_length);
}

Vladislav Rykov's avatar
Vladislav Rykov committed
365
366
void prepare_di_query(gcom_ch_t *gch, sensor_data_t *sensor_data, char *q, uint16_t q_size) {
	snprintf(q, q_size, 
Vladislav Rykov's avatar
Vladislav Rykov committed
367
		"INSERT INTO dev_%s_%d VALUES ("
Vladislav Rykov's avatar
Vladislav Rykov committed
368
			"%lu, '%s', "
Vladislav Rykov's avatar
Vladislav Rykov committed
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
			"'{ "
				"\"dht22_t_esp\" : %.2f, "
				"\"dht22_h_esp\" : %.2f, "
				"\"sht85_t_esp\" : %.2f, "
				"\"sht85_h_esp\" : %.2f, "
				"\"hih8121_t_esp\" : %.2f, "
				"\"hih8121_h_esp\" : %.2f, "
				"\"tmp36_0_esp\" : %.2f, "
				"\"tmp36_1_esp\" : %.2f, "
				"\"tmp36_2_esp\" : %.2f, "
				"\"hih4030_esp\" : %.2f, "
				"\"hh10d_esp\" : %.2f, "
				"\"dht22_t_mkr\" : %.2f, "
				"\"dht22_h_mkr\" : %.2f, "
				"\"sht85_t_mkr\" : %.2f, "
				"\"sht85_h_mkr\" : %.2f, "
				"\"hih8121_t_mkr\" : %.2f, "
				"\"hih8121_h_mkr\" : %.2f, "
				"\"hh10d_mkr\" : %.2f, "
				"\"dht22_t_wis\" : %.2f, "
				"\"dht22_h_wis\" : %.2f, "
				"\"sht85_t_wis\" : %.2f, "
				"\"sht85_h_wis\" : %.2f, "
				"\"hih8121_t_wis\" : %.2f, "
				"\"hih8121_h_wis\" : %.2f, "
				"\"tmp102_wis\" : %.2f, "
				"\"hh10d_wis\" : %.2f"
			"}'"	
		")"
		(char *)gch->app_key, gch->dev_id,
		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
415
	);
Vladislav Rykov's avatar
Vladislav Rykov committed
416
417
418
}

void filter_query(char *q) {
Vladislav Rykov's avatar
Vladislav Rykov committed
419
420
421
	char *pchr;
	const char nanstr[] = "'NaN'";

Vladislav Rykov's avatar
Vladislav Rykov committed
422
	while((pchr = strstr(q, "nan"))) {
Vladislav Rykov's avatar
Vladislav Rykov committed
423
424
425
		memmove(&pchr[5], &pchr[3], strlen(pchr)+1);
		memcpy(pchr, nanstr, sizeof(nanstr)-1);
	}
Vladislav Rykov's avatar
Vladislav Rykov committed
426
	while(pchr = strchr(q, '-')) {
Vladislav Rykov's avatar
Vladislav Rykov committed
427
428
429
430
431
432
		memmove(pchr, &pchr[1], strlen(pchr));
	}
}


void packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
433
434
435
436
437
	const uint8_t *app_key,
	const uint8_t dev_id, 
	const gateway_protocol_packet_type_t p_type, 
	const uint8_t payload_length,
	const uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
438
439
440
441
	uint8_t *packet_length,
	uint8_t *packet) 
{
	*packet_length = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
442
443
444
	
	memcpy(&packet[*packet_length], app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE);
	*packet_length += GATEWAY_PROTOCOL_APP_KEY_SIZE;
Vladislav Rykov's avatar
Vladislav Rykov committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

	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(
Vladislav Rykov's avatar
Vladislav Rykov committed
460
	uint8_t *app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
461
462
463
464
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
465
466
	const uint8_t packet_length,
	const uint8_t *packet)
Vladislav Rykov's avatar
Vladislav Rykov committed
467
468
{
	uint8_t p_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
469
470
471
	
	memcpy(app_key, &packet[p_len], GATEWAY_PROTOCOL_APP_KEY_SIZE);
	p_len += GATEWAY_PROTOCOL_APP_KEY_SIZE;
Vladislav Rykov's avatar
Vladislav Rykov committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

	*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
488
489
490
491
492
493
void gateway_protocol_mk_stat(
	gcom_ch_t *gch,
	gateway_protocol_stat_t stat,
	uint8_t *pck,
	uint8_t *pck_len)
{
Vladislav Rykov's avatar
Vladislav Rykov committed
494
495
496
497
498
499
	packet_encode(
		gch->app_key,
		gch->dev_id,
		GATEWAY_PROTOCOL_PACKET_TYPE_STAT,
		1, &stat,
		pck_len, pck);
Vladislav Rykov's avatar
Vladislav Rykov committed
500
501
}

Vladislav Rykov's avatar
Vladislav Rykov committed
502
503


Vladislav Rykov's avatar
Vladislav Rykov committed
504
505
506
507
508
509
510
511
void send_utc(gcom_ch_t *gch) {
	uint8_t buf[10];
	uint8_t buf_len = 0;
	struct timeval tv;
				
	gettimeofday(&tv, NULL);
				
	packet_encode (
Vladislav Rykov's avatar
Vladislav Rykov committed
512
		gch->app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
		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
546
547
/* connection handler for multithreading version */
#ifdef MULTITHREADING_VER
Vladislav Rykov's avatar
Vladislav Rykov committed
548
549
550
551
552
553
554
555
556
557
558
559
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;
		
Vladislav Rykov's avatar
Vladislav Rykov committed
560
		if (gateway_protocol_packet_decode(	
Vladislav Rykov's avatar
Vladislav Rykov committed
561
562
563
564
565
566
					&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
567
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
				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
582
583
			} else {
				perror("packet type error");
Vladislav Rykov's avatar
Vladislav Rykov committed
584
			}
Vladislav Rykov's avatar
Vladislav Rykov committed
585
586
		} else {
			perror("packet decode error");
Vladislav Rykov's avatar
Vladislav Rykov committed
587
588
589
590
		}
	}

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