gateway.c 16.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
17
18
#define TIMEDATE_LENGTH			32
#define PEND_SEND_RETRIES_MAX		5
#define GATEWAY_PROTOCOL_APP_KEY_SIZE	8
Vladislav Rykov's avatar
Vladislav Rykov committed
19

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

	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
54
typedef struct {
Vladislav Rykov's avatar
Vladislav Rykov committed
55
	uint8_t app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE +1];
Vladislav Rykov's avatar
Vladislav Rykov committed
56
57
58
59
60
61
62
63
64
65
66
	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
67
void * connection_handler (void *args);
Vladislav Rykov's avatar
Vladislav Rykov committed
68
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
69
void prepare_di_query(gcom_ch_t *gch, sensor_data_t *sensor_data, char *q, uint16_t q_size); // data insert
Vladislav Rykov's avatar
Vladislav Rykov committed
70
void filter_query(char *com);
Vladislav Rykov's avatar
Vladislav Rykov committed
71
void packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
72
73
74
75
76
	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
77
78
79
	uint8_t *packet_length,
	uint8_t *packet);
uint8_t packet_decode(
Vladislav Rykov's avatar
Vladislav Rykov committed
80
	uint8_t *app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
81
82
83
84
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
85
86
	const uint8_t packet_length,
	const uint8_t *packet);
Vladislav Rykov's avatar
Vladislav Rykov committed
87

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

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

Vladislav Rykov's avatar
Vladislav Rykov committed
109
	signal(SIGINT, ctrc_handler);
Vladislav Rykov's avatar
Vladislav Rykov committed
110

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

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

Vladislav Rykov's avatar
Vladislav Rykov committed
122
123
124
	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
125

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

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

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

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

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

Vladislav Rykov's avatar
Vladislav Rykov committed
259
	close(gch.server_desc);
Vladislav Rykov's avatar
Vladislav Rykov committed
260
	PQfinish(conn);
Vladislav Rykov's avatar
Vladislav Rykov committed
261

Vladislav Rykov's avatar
Vladislav Rykov committed
262
263
264
	return EXIT_SUCCESS;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
265
266
267
268
void ctrc_handler (int sig) {
	working = 0;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
269
270
271
272
273
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
274
275
276
277
278
279
280
	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
281
	p_len += TIMEDATE_LENGTH;
Vladislav Rykov's avatar
Vladislav Rykov committed
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


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

Vladislav Rykov's avatar
Vladislav Rykov committed
367
368
369
	return (p_len == payload_length);
}

Vladislav Rykov's avatar
Vladislav Rykov committed
370
371
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
372
		"INSERT INTO dev_%s_%d VALUES ("
Vladislav Rykov's avatar
Vladislav Rykov committed
373
			"%lu, '%s', "
Vladislav Rykov's avatar
Vladislav Rykov committed
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
			"'{ "
				"\"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"
			"}'"	
Vladislav Rykov's avatar
Vladislav Rykov committed
402
		")",
Vladislav Rykov's avatar
Vladislav Rykov committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
		(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
420
	);
Vladislav Rykov's avatar
Vladislav Rykov committed
421
422
423
}

void filter_query(char *q) {
Vladislav Rykov's avatar
Vladislav Rykov committed
424
	char *pchr;
Vladislav Rykov's avatar
Vladislav Rykov committed
425
	const char nanstr[] = "\"NaN\"";
Vladislav Rykov's avatar
Vladislav Rykov committed
426

Vladislav Rykov's avatar
Vladislav Rykov committed
427
	while((pchr = strstr(q, "nan"))) {
Vladislav Rykov's avatar
Vladislav Rykov committed
428
429
430
		memmove(&pchr[5], &pchr[3], strlen(pchr)+1);
		memcpy(pchr, nanstr, sizeof(nanstr)-1);
	}
Vladislav Rykov's avatar
Vladislav Rykov committed
431
	while(pchr = strchr(q, '-')) {
Vladislav Rykov's avatar
Vladislav Rykov committed
432
433
434
435
436
437
		memmove(pchr, &pchr[1], strlen(pchr));
	}
}


void packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
438
439
440
441
442
	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
443
444
445
446
	uint8_t *packet_length,
	uint8_t *packet) 
{
	*packet_length = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
447
448
449
	
	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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

	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
465
	uint8_t *app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
466
467
468
469
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
470
471
	const uint8_t packet_length,
	const uint8_t *packet)
Vladislav Rykov's avatar
Vladislav Rykov committed
472
473
{
	uint8_t p_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
474
475
476
	
	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
477
478
	
	app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE] = '\0';
Vladislav Rykov's avatar
Vladislav Rykov committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

	*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
495
496
497
498
499
500
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
501
502
503
504
	packet_encode(
		gch->app_key,
		gch->dev_id,
		GATEWAY_PROTOCOL_PACKET_TYPE_STAT,
Vladislav Rykov's avatar
Vladislav Rykov committed
505
		1, (uint8_t *)&stat,
Vladislav Rykov's avatar
Vladislav Rykov committed
506
		pck_len, pck);
Vladislav Rykov's avatar
Vladislav Rykov committed
507
508
}

Vladislav Rykov's avatar
Vladislav Rykov committed
509
510


Vladislav Rykov's avatar
Vladislav Rykov committed
511
void send_utc(gcom_ch_t *gch) {
Vladislav Rykov's avatar
Vladislav Rykov committed
512
	uint8_t buf[50];
Vladislav Rykov's avatar
Vladislav Rykov committed
513
514
515
516
517
518
	uint8_t buf_len = 0;
	struct timeval tv;
				
	gettimeofday(&tv, NULL);
				
	packet_encode (
Vladislav Rykov's avatar
Vladislav Rykov committed
519
		gch->app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
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
546
547
548
549
550
551
552
		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
553
554
/* connection handler for multithreading version */
#ifdef MULTITHREADING_VER
Vladislav Rykov's avatar
Vladislav Rykov committed
555
556
557
558
559
560
561
562
563
564
565
566
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
567
		if (gateway_protocol_packet_decode(	
Vladislav Rykov's avatar
Vladislav Rykov committed
568
569
570
571
572
573
					&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
574
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
				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
589
590
			} else {
				perror("packet type error");
Vladislav Rykov's avatar
Vladislav Rykov committed
591
			}
Vladislav Rykov's avatar
Vladislav Rykov committed
592
593
		} else {
			perror("packet decode error");
Vladislav Rykov's avatar
Vladislav Rykov committed
594
595
596
597
		}
	}

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