gateway.c 9.57 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
17
18
19
20
21
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
#include<libpq-fe.h>

typedef struct {
	uint32_t utc;
	char timedate[32];

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

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
58
59
60
61
62
	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
63

Vladislav Rykov's avatar
Vladislav Rykov committed
64
65
66
67
68
69
	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
70
	if ((server_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
Vladislav Rykov's avatar
Vladislav Rykov committed
71
72
73
74
75
76
77
78
79
80
81
82
83
		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
84
	while (1) {
Vladislav Rykov's avatar
Vladislav Rykov committed
85
		buf_len = 0;
Vladislav Rykov's avatar
Vladislav Rykov committed
86
87
88
89
		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
90

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

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

Vladislav Rykov's avatar
Vladislav Rykov committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		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
121
122
123
124
125
126
127
128
129
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
162
163
164
165
			} 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)) {
					snprintf(buf, sizeof(buf), "INSERT INTO esp32 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
						);
					printf("%.2f, %.2f, %.2f\n", sensor_data.hih8121_h_wis, sensor_data.tmp102_wis, sensor_data.hh10d_wis);
					printf("%s\n", buf);
				} else {
					printf("payload decode error\n");
				}
			} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ) {
			
Vladislav Rykov's avatar
Vladislav Rykov committed
166
			} else {
Vladislav Rykov's avatar
Vladislav Rykov committed
167
				printf("packet type error\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
168
169
170
171
172
173
174
175
			}

			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
176
177
	}

Vladislav Rykov's avatar
Vladislav Rykov committed
178
	close(server_desc);
Vladislav Rykov's avatar
Vladislav Rykov committed
179
	PQfinish(conn);
Vladislav Rykov's avatar
Vladislav Rykov committed
180

Vladislav Rykov's avatar
Vladislav Rykov committed
181
182
183
	return EXIT_SUCCESS;
}

Vladislav Rykov's avatar
Vladislav Rykov committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
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);
	p_len += 32;


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

	printf("p_len = %d, payload_length = %d (float size %d)\n", p_len, payload_length, sizeof(float));

	return (p_len == payload_length);
}

Vladislav Rykov's avatar
Vladislav Rykov committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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;

	//strncpy(buf, "connection handler greetings!", sizeof(buf));

	//write(client_desc, buf, strlen(buf));

	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
303
				printf("TIME REQ received\n");
Vladislav Rykov's avatar
Vladislav Rykov committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
				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
318
319
			} else {
				perror("packet type error");
Vladislav Rykov's avatar
Vladislav Rykov committed
320
			}
Vladislav Rykov's avatar
Vladislav Rykov committed
321
322
		} else {
			perror("packet decode error");
Vladislav Rykov's avatar
Vladislav Rykov committed
323
324
325
326
		}
	}

}