Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zerofiyng variables, added gps decoding fuction, graceful exit. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/umdk-m200.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void umdk_m200_command(char *param, char *out, int bufsize) {
}
else if (strstr(param, "iface ") == param) {
param += strlen("iface "); // Skip command
uint8_t interface;
uint8_t interface = 0;
if (strstr(param, "can") == param) {
interface = 2;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/umdk-m230.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void umdk_m230_command(char *param, char *out, int bufsize) {
}
else if (strstr(param, "set mode_tariff ") == param) {
param += strlen("set mode_tariff "); // Skip command
uint8_t mode;
uint8_t mode = 0;
if (strstr(param, "one") == param) {
param += strlen("one"); // Skip command
mode = 1;
Expand Down
2 changes: 1 addition & 1 deletion modules/umdk-pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void umdk_pwm_command(char *param, char *out, int bufsize) {
dev--;
param += strlen(" "); // Skip space

uint8_t mask;
uint8_t mask = 0;
if(strstr(param, "on ") == param) {
param += strlen("on "); // Skip command
mask = 1;
Expand Down
22 changes: 18 additions & 4 deletions mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,11 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
snprintf(pidval, sizeof(pidval), "%d\n", getpid());
write(pidfile, pidval, strlen(pidval));

if (write(pidfile, pidval, strlen(pidval)) < 0)
{
exit(EXIT_FAILURE);
}

sleep(30);
}
Expand Down Expand Up @@ -1491,9 +1495,19 @@ int main(int argc, char *argv[])
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);

if(mosquitto_connect(mosq, host, port, keepalive)){
snprintf(logbuf, sizeof(logbuf), "Unable to connect.\n");
logprint(logbuf);
if (mosquitto_connect(mosq, host, port, keepalive) == MOSQ_ERR_ERRNO) {
snprintf(logbuf, sizeof(logbuf), "Unable to connect");
int errno_saved = errno;
char *errmsg_extracting = strerror_r(errno_saved, errbuf, sizeof(errbuf));
if (errmsg_extracting != errbuf) {
logprint(strcat(logbuf, ".\n"));
} else {
strncat(logbuf, ": ", sizeof(logbuf) - (strlen(logbuf) + 1));
strncat(logbuf, errbuf, sizeof(logbuf) - (strlen(logbuf) + 1));
strncat(logbuf, "\n", sizeof(logbuf) - (strlen(logbuf) + 1));
logprint(logbuf);
}

return 1;
}

Expand Down
34 changes: 33 additions & 1 deletion unwds-mqtt.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2017 Unwired Devices LLC [info@unwds.com]
/* Copyright (c) 2017 Unwired Devices LLC [info@unwds.com]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -260,4 +260,36 @@ int unwds_modid_by_name(char *name) {
}

return -1;
}

/* GPS binary format decoder */
/* format is used by umdk-gps, umdk-idcard and other GPS-enabled devices */
void parse_gps_data(gps_data_t *gps, uint8_t *data, bool decode_nmea) {
memset(gps, 0, sizeof(gps_data_t));

gps->ready = (data[0] & 1);

if (gps->ready) {
int lat, lat_d, lon, lon_d;
lat = data[1] + (data[2] << 8);
lat_d = data[3];
lon = data[4] + (data[5] << 8);
lon_d = data[6];

gps->latitude = (float)lat + (float)lat_d/100.0;
gps->longitude = (float)lon + (float)lon_d/100.0;

/* Apply sign bits from reply */
if ((data[0] >> 5) & 1) {
gps->latitude = -gps->latitude;
}

if ((data[0] >> 6) & 1) {
gps->longitude = -gps->longitude;
}

gps->valid = (data[0] >> 7) & 1;
}

return;
}