gateway.c 11.7 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
16
17
#include<time.h>

#include<errno.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
18

Vladislav Rykov's avatar
Vladislav Rykov committed
19
20
21
#define TIMEDATE_LENGTH			32
#define PEND_SEND_RETRIES_MAX		5
#define GATEWAY_PROTOCOL_APP_KEY_SIZE	8
Vladislav Rykov's avatar
Vladislav Rykov committed
22
#define DEVICE_DATA_MAX_LENGTH		256
Vladislav Rykov's avatar
Vladislav Rykov committed
23

Vladislav Rykov's avatar
Vladislav Rykov committed
24
25
typedef struct {
	uint32_t utc;
Vladislav Rykov's avatar
Vladislav Rykov committed
26
	char timedate[TIMEDATE_LENGTH];
Vladislav Rykov's avatar
Vladislav Rykov committed
27

Vladislav Rykov's avatar
Vladislav Rykov committed
28
29
	uint8_t data[DEVICE_DATA_MAX_LENGTH];
	uint8_t data_length;
Vladislav Rykov's avatar
Vladislav Rykov committed
30
31
} sensor_data_t;

Vladislav Rykov's avatar
Vladislav Rykov committed
32
typedef struct {
Vladislav Rykov's avatar
Vladislav Rykov committed
33
	uint8_t app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE +1];
Vladislav Rykov's avatar
Vladislav Rykov committed
34
35
36
37
38
39
40
41
	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

Vladislav Rykov's avatar
Vladislav Rykov committed
42
43
44
/* for multithreading impl */
void * connection_handler (void *args);

Vladislav Rykov's avatar
Vladislav Rykov committed
45
46
47
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
48
void packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
49
50
51
52
53
	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
54
55
56
	uint8_t *packet_length,
	uint8_t *packet);
uint8_t packet_decode(
Vladislav Rykov's avatar
Vladislav Rykov committed
57
	uint8_t *app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
58
59
60
61
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
62
63
	const uint8_t packet_length,
	const uint8_t *packet);
Vladislav Rykov's avatar
Vladislav Rykov committed
64
65
66
67
void 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

Vladislav Rykov's avatar
Vladislav Rykov committed
69
70
71
72
73
74
75
76
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
77
78
void ctrc_handler (int sig);
static volatile uint8_t working = 1;
Vladislav Rykov's avatar
Vladislav Rykov committed
79
80

int main (int argc, char **argv) {
Vladislav Rykov's avatar
Vladislav Rykov committed
81
	gcom_ch_t gch;
Vladislav Rykov's avatar
Vladislav Rykov committed
82
83
	uint8_t buf[1024];
	uint8_t buf_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
84
	uint8_t payload[256];
Vladislav Rykov's avatar
Vladislav Rykov committed
85
	uint8_t payload_length = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
86
87
	PGresult *res;
	
Vladislav Rykov's avatar
Vladislav Rykov committed
88
89
	memset(&gch, 0x0, sizeof(gch));

Vladislav Rykov's avatar
Vladislav Rykov committed
90
	signal(SIGINT, ctrc_handler);
Vladislav Rykov's avatar
Vladislav Rykov committed
91

Vladislav Rykov's avatar
Vladislav Rykov committed
92
	PGconn *conn = PQconnectdb("user=vlad dbname=iotserver password=dev");
Vladislav Rykov's avatar
Vladislav Rykov committed
93
	if (PQstatus(conn) == CONNECTION_BAD) {
Vladislav Rykov's avatar
Vladislav Rykov committed
94
		fprintf(stderr,"connection to db error: %s\n", PQerrorMessage(conn));
Vladislav Rykov's avatar
Vladislav Rykov committed
95
96
97
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
98
	if ((gch.server_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
Vladislav Rykov's avatar
Vladislav Rykov committed
99
100
101
102
		perror("socket creation error");
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
103
	gch.server.sin_family 		= AF_INET;
Vladislav Rykov's avatar
Vladislav Rykov committed
104
	gch.server.sin_port		= htons(54345);
Vladislav Rykov's avatar
Vladislav Rykov committed
105
	gch.server.sin_addr.s_addr 	= INADDR_ANY;
Vladislav Rykov's avatar
Vladislav Rykov committed
106

Vladislav Rykov's avatar
Vladislav Rykov committed
107
	if (bind(gch.server_desc, (struct sockaddr *) &gch.server, sizeof(gch.server)) < 0) {
Vladislav Rykov's avatar
Vladislav Rykov committed
108
109
110
111
		perror("binding error");
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
112
113
114
	gateway_protocol_packet_type_t packet_type;
	
	while (working) {
Vladislav Rykov's avatar
Vladislav Rykov committed
115
		buf_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
116
		printf("listenninig...\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
117
118
		gch.sock_len = sizeof(gch.client);
		
Vladislav Rykov's avatar
Vladislav Rykov committed
119
120
121
122
123
124
125
126
		if (	recv_gcom_ch(&gch, buf, &buf_len, 1024) 
			&
			packet_decode(
				gch.app_key,
				&gch.dev_id,
				&packet_type,
				&payload_length, payload,
				buf_len, buf)			)
Vladislav Rykov's avatar
Vladislav Rykov committed
127
128
129
		{
			if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ) {
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
130
				send_utc(&gch);
Vladislav Rykov's avatar
Vladislav Rykov committed
131
132
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_DATA_SEND) {
				sensor_data_t sensor_data;
Vladislav Rykov's avatar
Vladislav Rykov committed
133
134
				time_t t;

Vladislav Rykov's avatar
Vladislav Rykov committed
135
				printf("DATA SEND received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
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
		        	gateway_protocol_data_send_payload_decode(&sensor_data, payload, payload_length);
				
				if (sensor_data.utc == 0) {
					struct timeval tv;
					gettimeofday(&tv, NULL);
					t = tv.tv_sec;
				} else {
					t = sensor_data.utc;
				}
				
				strftime(sensor_data.timedate, TIMEDATE_LENGTH, "%d/%m/%Y %H:%M:%S", localtime(&t));
				snprintf(buf, sizeof(buf), 
					"INSERT INTO dev_%s_%d VALUES (%d, '%s', $1)", (char *)gch.app_key, gch.dev_id, t, sensor_data.timedate);
				
				const char *params[1];
				int paramlen[1];
				int paramfor[1];
				params[0] = sensor_data.data;
				paramlen[0] = sensor_data.data_length;
				paramfor[0] = 1; // binary

				res = PQexecParams(conn, buf, 1, NULL, params, paramlen, paramfor, 0);
				if (PQresultStatus(res) == PGRES_COMMAND_OK) {
					PQclear(res);

					sprintf(buf, "SELECT * FROM pend_msgs WHERE app_key='%s' and dev_id = %d and ack = False", (char *)gch.app_key, gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
162
					res = PQexec(conn, buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
163
164
165
166
167
168
					if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
						gateway_protocol_mk_stat(
							&gch, 
							GATEWAY_PROTOCOL_STAT_ACK_PEND,
							buf, &buf_len);
						printf("ACK_PEND prepared\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
169
					} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
170
171
172
173
174
						gateway_protocol_mk_stat(
							&gch, 
							GATEWAY_PROTOCOL_STAT_ACK,
							buf, &buf_len);
						printf("ACK prepared\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
175
					}
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
				}
Vladislav Rykov's avatar
Vladislav Rykov committed
181
				PQclear(res);
Vladislav Rykov's avatar
Vladislav Rykov committed
182
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ) {
Vladislav Rykov's avatar
Vladislav Rykov committed
183
				sprintf(buf, "SELECT * FROM pend_msgs WHERE app_key = '%s' AND dev_id = %d AND ack = False", 
Vladislav Rykov's avatar
Vladislav Rykov committed
184
						(char *)gch.app_key, gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
185
186
187
				res = PQexec(conn, buf);
				if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
					char msg_cont[150];
Vladislav Rykov's avatar
Vladislav Rykov committed
188
189
					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
190
					PQclear(res);
Vladislav Rykov's avatar
Vladislav Rykov committed
191
192
193
194
				
					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
195
					
Vladislav Rykov's avatar
Vladislav Rykov committed
196
197
198
					// send the msg until ack is received
					uint8_t received_ack = 0;
					uint8_t pend_send_retries = PEND_SEND_RETRIES_MAX;
Vladislav Rykov's avatar
Vladislav Rykov committed
199
200
201
					struct timeval tv;
					tv.tv_sec = 0;
					tv.tv_usec = 300000; // 300ms ack recv timeout
Vladislav Rykov's avatar
Vladislav Rykov committed
202
203
					do {
						packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
204
							gch.app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
205
206
207
208
							gch.dev_id, 
							GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND,
							payload_length, payload,
							&buf_len, buf);
Vladislav Rykov's avatar
Vladislav Rykov committed
209

Vladislav Rykov's avatar
Vladislav Rykov committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
						send_gcom_ch(&gch, buf, buf_len);	
						// set timeout
						if (setsockopt(gch.server_desc, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
							perror("setsockopt error");
						}
						recv_gcom_ch(&gch, buf, &buf_len, 1024);
						if (buf_len > 9) { /* min packet size. timeout -> -1 */
							uint8_t recv_app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE +1];
							uint8_t recv_dev_id = 0xFF;
							if (packet_decode(
								recv_app_key,
								&recv_dev_id, 
								&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
229
230
231
232
233
234
235
236
237
238
239
240
241
								if (!memcmp(recv_app_key, gch.app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE) &&
									recv_dev_id == gch.dev_id &&
									packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_STAT &&
									buf_len == 1 &&
									buf[0] == GATEWAY_PROTOCOL_STAT_ACK)
								{
									sprintf(buf, "UPDATE pend_msgs SET ack = True WHERE app_key = '%s' AND dev_id = %d AND msg = '%s'", (char *)gch.app_key, gch.dev_id, msg_cont);
									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");
								} else {
									printf("error: packet_type = %02X, not STAT\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
242
								}
Vladislav Rykov's avatar
Vladislav Rykov committed
243
244
							}
						}
Vladislav Rykov's avatar
Vladislav Rykov committed
245
					} while (!received_ack && pend_send_retries--);
Vladislav Rykov's avatar
Vladislav Rykov committed
246
247
248
249
250
					// cancel timeout
					tv.tv_usec = 0;	
					if (setsockopt(gch.server_desc, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
						perror("setsockopt error");
					}
Vladislav Rykov's avatar
Vladislav Rykov committed
251
				} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
252
253
254
255
256
257
258
					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
259
					printf("nothing for app %s dev %d\n", (char *)gch.app_key, gch.dev_id);
Vladislav Rykov's avatar
Vladislav Rykov committed
260
				}
Vladislav Rykov's avatar
Vladislav Rykov committed
261
			} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
262
263
264
265
266
267
268
				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
269
				fprintf(stderr, "packet type error : %02X\n", packet_type);
Vladislav Rykov's avatar
Vladislav Rykov committed
270
271
			}
		} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
272
273
			
			fprintf(stderr, "payload decode error\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
274
		}
Vladislav Rykov's avatar
Vladislav Rykov committed
275

Vladislav Rykov's avatar
Vladislav Rykov committed
276
277
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
278
	close(gch.server_desc);
Vladislav Rykov's avatar
Vladislav Rykov committed
279
	PQfinish(conn);
Vladislav Rykov's avatar
Vladislav Rykov committed
280

Vladislav Rykov's avatar
Vladislav Rykov committed
281
282
283
	return EXIT_SUCCESS;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
284
285
286
287
void ctrc_handler (int sig) {
	working = 0;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
288
void gateway_protocol_data_send_payload_decode(
Vladislav Rykov's avatar
Vladislav Rykov committed
289
290
291
292
	sensor_data_t *sensor_data, 
	const uint8_t *payload, 
	const uint8_t payload_length) 
{
Vladislav Rykov's avatar
Vladislav Rykov committed
293
294
295
296
297
	uint8_t p_len = 0;

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

Vladislav Rykov's avatar
Vladislav Rykov committed
298
299
	memcpy(sensor_data->data, &payload[p_len], payload_length - p_len);
	sensor_data->data_length = payload_length - p_len;
Vladislav Rykov's avatar
Vladislav Rykov committed
300
301
}

Vladislav Rykov's avatar
Vladislav Rykov committed
302
void packet_encode(
Vladislav Rykov's avatar
Vladislav Rykov committed
303
304
305
306
307
	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
308
309
310
311
	uint8_t *packet_length,
	uint8_t *packet) 
{
	*packet_length = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
312
313
314
	
	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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

	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
330
	uint8_t *app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
331
332
333
334
	uint8_t *dev_id,
	gateway_protocol_packet_type_t *ptype,
	uint8_t *payload_length,
	uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
335
336
	const uint8_t packet_length,
	const uint8_t *packet)
Vladislav Rykov's avatar
Vladislav Rykov committed
337
338
{
	uint8_t p_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
339
340
341
	
	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
342
343
	
	app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE] = '\0';
Vladislav Rykov's avatar
Vladislav Rykov committed
344
345
346
347
348
349
350
351
352
353
354
355
356

	*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;

Vladislav Rykov's avatar
Vladislav Rykov committed
357
358
	printf("payload_length = %d , calc = %d, recv = %d\n", *payload_length, p_len, packet_length);

Vladislav Rykov's avatar
Vladislav Rykov committed
359
360
361
	return p_len == packet_length;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
362
363
364
365
366
367
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
368
369
370
371
	packet_encode(
		gch->app_key,
		gch->dev_id,
		GATEWAY_PROTOCOL_PACKET_TYPE_STAT,
Vladislav Rykov's avatar
Vladislav Rykov committed
372
		1, (uint8_t *)&stat,
Vladislav Rykov's avatar
Vladislav Rykov committed
373
		pck_len, pck);
Vladislav Rykov's avatar
Vladislav Rykov committed
374
375
}

Vladislav Rykov's avatar
Vladislav Rykov committed
376
377


Vladislav Rykov's avatar
Vladislav Rykov committed
378
void send_utc(gcom_ch_t *gch) {
Vladislav Rykov's avatar
Vladislav Rykov committed
379
	uint8_t buf[50];
Vladislav Rykov's avatar
Vladislav Rykov committed
380
381
382
383
384
385
	uint8_t buf_len = 0;
	struct timeval tv;
				
	gettimeofday(&tv, NULL);
				
	packet_encode (
Vladislav Rykov's avatar
Vladislav Rykov committed
386
		gch->app_key,
Vladislav Rykov's avatar
Vladislav Rykov committed
387
388
		gch->dev_id,
		GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND,
Vladislav Rykov's avatar
Vladislav Rykov committed
389
		sizeof(uint32_t), (uint8_t *)&tv.tv_sec,
Vladislav Rykov's avatar
Vladislav Rykov committed
390
391
392
393
394
395
396
		&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) {
Vladislav Rykov's avatar
Vladislav Rykov committed
397
	int ret, i;
Vladislav Rykov's avatar
Vladislav Rykov committed
398
399
400
401
402
403
404
405
	
	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) {
Vladislav Rykov's avatar
Vladislav Rykov committed
406
407
	int i;

Vladislav Rykov's avatar
Vladislav Rykov committed
408
409
410
411
	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");
	}
}
Vladislav Rykov's avatar
Vladislav Rykov committed
412
413
/* connection handler for multithreading version */
#ifdef MULTITHREADING_VER
Vladislav Rykov's avatar
Vladislav Rykov committed
414
415
416
417
418
419
420
421
422
423
424
425
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
426
		if (gateway_protocol_packet_decode(	
Vladislav Rykov's avatar
Vladislav Rykov committed
427
428
429
430
431
432
					&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
433
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
				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
448
449
			} else {
				perror("packet type error");
Vladislav Rykov's avatar
Vladislav Rykov committed
450
			}
Vladislav Rykov's avatar
Vladislav Rykov committed
451
452
		} else {
			perror("packet decode error");
Vladislav Rykov's avatar
Vladislav Rykov committed
453
454
455
456
		}
	}

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