Commit c88d7738 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

cleaning code

parent e4969dc2
No preview for this file type
......@@ -49,9 +49,22 @@ typedef struct {
float hh10d_wis;
} sensor_data_t;
typedef struct {
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);
void * connection_handler (void *args);
uint8_t gateway_protocol_data_send_payload_decode(sensor_data_t *sensor_data, const uint8_t *payload, const uint8_t payload_length);
void filter_nans(char *com);
void prepare_di_query(sensor_data_t *sensor_data, char *q, uint16_t q_size); // data insert
void filter_query(char *com);
void packet_encode(
uint8_t dev_id,
gateway_protocol_packet_type_t p_type,
......@@ -67,18 +80,27 @@ uint8_t packet_decode(
uint8_t packet_length,
uint8_t *packet);
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);
void ctrc_handler (int sig);
static volatile uint8_t working = 1;
int main (int argc, char **argv) {
int server_desc, client_desc;
struct sockaddr_in server, client;
socklen_t client_socklen;
//int server_desc, client_desc;
//struct sockaddr_in server, client;
//socklen_t client_socklen;
gcom_ch_t gch;
uint8_t buf[1024];
uint8_t buf_len = 0;
uint8_t payload[256];
uint8_t payload_length = 0;
int sock_len;
//int sock_len;
PGresult *res;
signal(SIGINT, ctrc_handler);
......@@ -89,158 +111,82 @@ int main (int argc, char **argv) {
return EXIT_FAILURE;
}
if ((server_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
if ((gch.server_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket creation error");
return EXIT_FAILURE;
}
server.sin_family = AF_INET;
server.sin_port = htons(9043);
server.sin_addr.s_addr = INADDR_ANY;
gch.server.sin_family = AF_INET;
gch.server.sin_port = htons(9043);
gch.server.sin_addr.s_addr = INADDR_ANY;
if (bind(server_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
if (bind(gch.server_desc, (struct sockaddr *) &gch.server, sizeof(gch.server)) < 0) {
perror("binding error");
return EXIT_FAILURE;
}
uint8_t dev_id = 0xFF;
gateway_protocol_packet_type_t packet_type;
while (working) {
buf_len = 0;
printf("listenninig...\n");
if ((buf_len = recvfrom(server_desc, (char *)buf, 1024, MSG_WAITALL, (struct sockaddr *)&client, &sock_len)) < 0) {
perror("socket receive error");
}
printf("packet received!\n");
gch.sock_len = sizeof(gch.client);
recv_gcom_ch(&gch, buf, &buf_len, 1024);
for(uint8_t i = 0; i < buf_len; i++) {
printf("%02X : ", buf[i]);
}
printf("\n");
if (gateway_protocol_packet_decode(
&dev_id,
&gch.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;
gettimeofday(&tv, NULL);
packet_encode (
dev_id,
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND,
sizeof(tv.tv_sec), (uint8_t *)&tv.tv_sec,
&buf_len, buf
);
for(uint8_t i = 0; i < buf_len; i++) {
printf("%02X : ", buf[i]);
}
printf("\n");
send_utc(&gch);
} 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 ("
"utc, timedate, "
"dht22_t_esp, dht22_h_esp, "
"sht85_t_esp, sht85_h_esp, "
"hih8121_t_esp, hih8121_h_esp, "
"tmp36_0_esp, tmp36_1_esp, tmp36_2_esp, "
"hih4030_esp, "
"hh10d_esp, "
"dht22_t_mkr, dht22_h_mkr, "
"sht85_t_mkr, sht85_h_mkr, "
"hih8121_t_mkr, hih8121_h_mkr, "
"hh10d_mkr, "
"dht22_t_wis, dht22_h_wis, "
"sht85_t_wis, sht85_h_wis, "
"hih8121_t_wis, hih8121_h_wis, "
"tmp102_wis, "
"hh10d_wis) "
"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
);
filter_nans(buf);
prepare_di_query(&sensor_data, buf, sizeof(buf));
filter_query(buf);
printf("%s\n", buf);
res = PQexec(conn, buf);
if (PQresultStatus(res) == PGRES_COMMAND_OK) {
PQclear(res);
//fprintf(stderr, "%s\n", PQerrorMessage(conn));
sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", dev_id);
sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", gch.dev_id);
res = PQexec(conn, buf);
if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
buf[3] = GATEWAY_PROTOCOL_STAT_ACK_PEND;
gateway_protocol_mk_stat(
&gch,
GATEWAY_PROTOCOL_STAT_ACK_PEND,
buf, &buf_len);
printf("ACK_PEND prepared\n");
} else {
buf[3] = GATEWAY_PROTOCOL_STAT_ACK;
gateway_protocol_mk_stat(
&gch,
GATEWAY_PROTOCOL_STAT_ACK,
buf, &buf_len);
printf("ACK prepared\n");
}
buf[0] = dev_id;
buf[1] = GATEWAY_PROTOCOL_PACKET_TYPE_STAT;
buf[2] = 1;
buf_len = 4;
send_gcom_ch(&gch, buf, buf_len);
} else {
fprintf(stderr, "database error : %s\n", PQerrorMessage(conn));
}
PQclear(res);
} else {
gateway_protocol_stat_t s_type = GATEWAY_PROTOCOL_STAT_NACK;
packet_encode (
dev_id,
GATEWAY_PROTOCOL_PACKET_TYPE_STAT,
1, (uint8_t *)&s_type,
&buf_len, buf);
/*
buf[0] = dev_id;
buf[1] = GATEWAY_PROTOCOL_PACKET_TYPE_STAT;
buf[2] = 1;
buf[3] = GATEWAY_PROTOCOL_STAT_NACK;
buf_len = 4;
*/
gateway_protocol_mk_stat(
&gch,
GATEWAY_PROTOCOL_STAT_NACK,
buf, &buf_len);
send_gcom_ch(&gch, buf, buf_len);
fprintf(stderr, "payload decode error\n");
}
} else if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ) {
sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", dev_id);
sprintf(buf, "SELECT * FROM pend_msgs WHERE dev_id = %d", gch.dev_id);
res = PQexec(conn, buf);
if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
char msg_cont[150];
......@@ -253,27 +199,16 @@ int main (int argc, char **argv) {
printf("prepared to send %d bytes\n", payload_length);
packet_encode(
dev_id,
gch.dev_id,
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND,
payload_length, payload,
&buf_len, buf);
printf("packet %d bytes\n", buf_len);
for(uint8_t i = 0; i < buf_len; i++) {
printf("%02X : ", buf[i]);
}
printf("\n");
if (sendto(server_desc, (char *) buf, buf_len, 0, (struct sockaddr *)&client, sock_len) < 0) {
perror("sendto error");
}
if ((buf_len = recvfrom(server_desc, (char *)buf, 1024, MSG_WAITALL, (struct sockaddr *)&client, &sock_len)) < 0) {
perror("socket receive error");
}
send_gcom_ch(&gch, buf, buf_len);
recv_gcom_ch(&gch, buf, &buf_len, 1024);
if (packet_decode(
&dev_id,
&gch.dev_id,
&packet_type,
&payload_length, payload,
buf_len, buf))
......@@ -282,10 +217,9 @@ int main (int argc, char **argv) {
payload_length == 1 &&
payload[0] == GATEWAY_PROTOCOL_STAT_ACK)
{
sprintf(buf, "DELETE FROM pend_msgs WHERE dev_id = %d AND msg = '%s'", dev_id, msg_cont);
sprintf(buf, "DELETE FROM pend_msgs WHERE dev_id = %d AND msg = '%s'", gch.dev_id, msg_cont);
printf("%s", buf);
res = PQexec(conn, buf);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "error db deleting : %s", PQerrorMessage(conn));
}
......@@ -293,22 +227,17 @@ int main (int argc, char **argv) {
printf("ACK received\n");
}
}
continue;
}
} else {
fprintf(stderr, "packet type error : %02X\n", packet_type);
}
if (sendto(server_desc, (char *) buf, buf_len, 0, (struct sockaddr *)&client, sock_len) < 0) {
perror("sendto error");
}
} else {
fprintf(stderr, "packet decode error (type : %02X, pck_len = %d, pay_len = %d\n",
packet_type, buf_len, payload_length);
}
}
close(server_desc);
close(gch.server_desc);
PQfinish(conn);
return EXIT_SUCCESS;
......@@ -318,7 +247,11 @@ void ctrc_handler (int sig) {
working = 0;
}
uint8_t gateway_protocol_data_send_payload_decode(sensor_data_t *sensor_data, const uint8_t *payload, const uint8_t payload_length) {
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));
......@@ -415,16 +348,70 @@ uint8_t gateway_protocol_data_send_payload_decode(sensor_data_t *sensor_data, co
return (p_len == payload_length);
}
void prepare_di_query(sensor_data_t *sensor_data, char *q, uint16_t q_size) {
snprintf(q, q_size, "INSERT INTO esp32 ("
"utc, timedate, "
"dht22_t_esp, dht22_h_esp, "
"sht85_t_esp, sht85_h_esp, "
"hih8121_t_esp, hih8121_h_esp, "
"tmp36_0_esp, tmp36_1_esp, tmp36_2_esp, "
"hih4030_esp, "
"hh10d_esp, "
"dht22_t_mkr, dht22_h_mkr, "
"sht85_t_mkr, sht85_h_mkr, "
"hih8121_t_mkr, hih8121_h_mkr, "
"hh10d_mkr, "
"dht22_t_wis, dht22_h_wis, "
"sht85_t_wis, sht85_h_wis, "
"hih8121_t_wis, hih8121_h_wis, "
"tmp102_wis, "
"hh10d_wis) "
"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
);
}
void filter_nans(char *com) {
void filter_query(char *q) {
char *pchr;
const char nanstr[] = "'NaN'";
while((pchr = strstr(com, "nan"))) {
while((pchr = strstr(q, "nan"))) {
memmove(&pchr[5], &pchr[3], strlen(pchr)+1);
memcpy(pchr, nanstr, sizeof(nanstr)-1);
}
while(pchr = strchr(com, '-')) {
while(pchr = strchr(q, '-')) {
memmove(pchr, &pchr[1], strlen(pchr));
}
}
......@@ -478,8 +465,62 @@ uint8_t packet_decode(
return p_len == packet_length;
}
void gateway_protocol_mk_stat(
gcom_ch_t *gch,
gateway_protocol_stat_t stat,
uint8_t *pck,
uint8_t *pck_len)
{
pck[0] = gch->dev_id;
pck[1] = GATEWAY_PROTOCOL_PACKET_TYPE_STAT;
pck[2] = 1;
pck[3] = stat;
*pck_len = 4;
}
void send_utc(gcom_ch_t *gch) {
uint8_t buf[10];
uint8_t buf_len = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
packet_encode (
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");
}
/* connection handler for multithreading version */
#ifdef MULTITHREADING_VER
void *connection_handler(void *args) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment