gateway.c 9.58 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
2
3
4
5
6
7
8
9
10
#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
11
#include<gateway_protocol.h>
Vladislav Rykov's avatar
Vladislav Rykov committed
12
13
14

#include<sys/time.h>

Vladislav Rykov's avatar
Vladislav Rykov committed
15
16
#include<libpq-fe.h>

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

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
void * connection_handler (void *args);
Vladislav Rykov's avatar
Vladislav Rykov committed
54
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
55
56
57
58
59

int main (int argc, char **argv) {
	int server_desc, client_desc;
       	struct sockaddr_in server, client;
	socklen_t client_socklen;
Vladislav Rykov's avatar
Vladislav Rykov committed
60
61
62
63
64
	uint8_t buf[1024];
	uint8_t buf_len = 0;
	uint8_t payload[128];
	uint8_t payload_length = 0;
	int sock_len;
Vladislav Rykov's avatar
Vladislav Rykov committed
65

Vladislav Rykov's avatar
Vladislav Rykov committed
66
67
68
69
70
71
	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
72
	if ((server_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
Vladislav Rykov's avatar
Vladislav Rykov committed
73
74
75
76
77
78
79
80
81
82
83
84
85
		perror("socket creation error");
		return EXIT_FAILURE;
	}

	server.sin_family 	= AF_INET;
	server.sin_port		= htons(9043);
	server.sin_addr.s_addr 	= INADDR_ANY;

	if (bind(server_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
		perror("binding error");
		return EXIT_FAILURE;
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
86
	while (1) {
Vladislav Rykov's avatar
Vladislav Rykov committed
87
		buf_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
88
89
90
91
		printf("listenninig...\n");
		if ((buf_len = recvfrom(server_desc, (char *)buf, 1024, MSG_WAITALL, (struct sockaddr *)&client, &sock_len)) < 0) {
			perror("socket receive error");
		}
Vladislav Rykov's avatar
Vladislav Rykov committed
92

Vladislav Rykov's avatar
Vladislav Rykov committed
93
		printf("packet received!\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
94

Vladislav Rykov's avatar
Vladislav Rykov committed
95
96
		for (uint8_t i = 0; i < buf_len; i++) {
			printf("%02X :", buf[i]);
Vladislav Rykov's avatar
Vladislav Rykov committed
97
		}
Vladislav Rykov's avatar
Vladislav Rykov committed
98
		printf("\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
99

Vladislav Rykov's avatar
Vladislav Rykov committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		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) {
				printf("TIME REQ received\n");
				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);
Vladislav Rykov's avatar
Vladislav Rykov committed
123
124
125
126
			} 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
127
					PGresult *res;
Vladislav Rykov's avatar
Vladislav Rykov committed
128
					snprintf(buf, sizeof(buf), "INSERT INTO esp32 VALUES("
Vladislav Rykov's avatar
Vladislav Rykov committed
129
							"%lu, '%s', "
Vladislav Rykov's avatar
Vladislav Rykov committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
							"%.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
162
163
164
165
166
167
					//printf("%s\n", buf);
					res = PQexec(conn, buf);
					if (PQresultStatus(res) != PGRES_COMMAND_OK) {
						fprintf(stderr, "%s\n", PQerrorMessage(conn));
					}
					PQclear(res);
Vladislav Rykov's avatar
Vladislav Rykov committed
168
169
170
171
172
				} else {
					printf("payload decode error\n");
				}
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ) {
			
Vladislav Rykov's avatar
Vladislav Rykov committed
173
			} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
174
				printf("packet type error\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
175
176
177
178
179
180
181
182
			}

			if (sendto(server_desc, (char *) buf, buf_len, 0, (struct sockaddr *)&client, sock_len) < 0) {
				perror("sendto error");
			}
		} else {
			perror("packet decode error");
		}
Vladislav Rykov's avatar
Vladislav Rykov committed
183
184
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
185
	close(server_desc);
Vladislav Rykov's avatar
Vladislav Rykov committed
186
	PQfinish(conn);
Vladislav Rykov's avatar
Vladislav Rykov committed
187

Vladislav Rykov's avatar
Vladislav Rykov committed
188
189
190
	return EXIT_SUCCESS;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
191
192
193
194
195
196
197
198
uint8_t gateway_protocol_data_send_payload_decode(sensor_data_t *sensor_data, const uint8_t *payload, const uint8_t payload_length) {
	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
199
	p_len += TIMEDATE_LENGTH;
Vladislav Rykov's avatar
Vladislav Rykov committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284


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

	return (p_len == payload_length);
}

Vladislav Rykov's avatar
Vladislav Rykov committed
285
286
/* connection handler for multithreading version */
#ifdef MULTITHREADING_VER
Vladislav Rykov's avatar
Vladislav Rykov committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
306
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
				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
321
322
			} else {
				perror("packet type error");
Vladislav Rykov's avatar
Vladislav Rykov committed
323
			}
Vladislav Rykov's avatar
Vladislav Rykov committed
324
325
		} else {
			perror("packet decode error");
Vladislav Rykov's avatar
Vladislav Rykov committed
326
327
328
329
		}
	}

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