Skip to content

Commit

Permalink
feat: ts swap to GPS once fix acquired.
Browse files Browse the repository at this point in the history
  • Loading branch information
theBASTI0N committed Mar 17, 2020
1 parent 16429d7 commit 5d35261
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 83 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ add_subdirectory(src/gps_controller)
add_subdirectory(src/modem_controller)
add_subdirectory(src/http_controller)
add_subdirectory(src/data_parser)
add_subdirectory(src/time_handler)
#add_subdirectory(src/watchdog)
21 changes: 3 additions & 18 deletions src/data_parser/data_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
#include "cJSON.h"
#include "cJSON_os.h"

#include "modem_controller.h"
#include "time_handler.h"
#include "ruuvinode.h"

// Post Buffer
cJSON *tags = NULL;
time_t json_ts_buf;
u32_t json_ts_buf_tk;

int encode_tags(struct ble_report *r, int count){
int err = 0;
int index = 0;

tags = cJSON_CreateObject();
if (tags == NULL)
{
Expand Down Expand Up @@ -50,19 +47,13 @@ int encode_tags(struct ble_report *r, int count){

cJSON_AddItemToObject(tags, r[index].tag_mac, tag);
}
printk("Tags: %d\n", index);
return err;
}

int encode_json(struct msg_buf *output, double la, double lo, char *imei)
{
int err = 0;
u32_t now;
time_t now_ts;

now = k_uptime_get_32();
now = now - json_ts_buf_tk;
now = now / 1000;
now_ts = json_ts_buf + now;

cJSON *root_obj = cJSON_CreateObject();
if (root_obj == NULL){
Expand Down Expand Up @@ -96,7 +87,7 @@ int encode_json(struct msg_buf *output, double la, double lo, char *imei)
}
cJSON_AddItemToObject(gw_obj, "longitude", gwLong);

cJSON *gwTime = cJSON_CreateNumber(now_ts);
cJSON *gwTime = cJSON_CreateNumber(get_ts());
if (gwTime == NULL)
{
printk("Error: Creating gwTime");
Expand Down Expand Up @@ -124,10 +115,4 @@ int encode_json(struct msg_buf *output, double la, double lo, char *imei)
end:
cJSON_Delete(root_obj);
return err;
}

time_t json_prepare_time(void){
json_ts_buf = modem_ts();
json_ts_buf_tk = k_uptime_get_32();
return json_ts_buf;
}
3 changes: 1 addition & 2 deletions src/data_parser/data_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ struct msg_buf {
};

int encode_tags(struct ble_report *r, int count);
int encode_json(struct msg_buf *output, double la, double lo, char *imei);
time_t json_prepare_time(void);
int encode_json(struct msg_buf *output, double la, double lo, char *imei);
18 changes: 17 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "http_controller.h"
#include "ruuvinode.h"
#include "data_parser.h"
#include "time_handler.h"

#include <logging/log.h>
LOG_MODULE_REGISTER(ruuvi_node, CONFIG_RUUVI_NODE_LOG_LEVEL);
Expand Down Expand Up @@ -80,7 +81,16 @@ static void gps_trigger_handler(struct device *dev, struct gps_trigger *trigger)
atomic_set(&GPS_FIRST_FIX, 1);
longT = gps_data.pvt.longitude;
latT = gps_data.pvt.latitude;
struct tm gps_time;
gps_time.tm_year = gps_data.pvt.datetime.year - 1900;
gps_time.tm_mon = gps_data.pvt.datetime.month -1;
gps_time.tm_mday = gps_data.pvt.datetime.day;
gps_time.tm_hour = gps_data.pvt.datetime.hour;
gps_time.tm_min = gps_data.pvt.datetime.minute;
gps_time.tm_sec = gps_data.pvt.datetime.seconds;
gps_time.tm_isdst = -1;
LOG_INF("GPS Coordinate Updated\n");
update_ts_gps(&gps_time);
}
else{
LOG_ERR("GPS Update Failure\n");
Expand Down Expand Up @@ -155,7 +165,6 @@ static void sensors_init(void)
LOG_ERR("modem_info_string_get(MODEM FW), error: %d", err);
}
LOG_INF("Modem FW Version : %s", log_strdup(modem_fw_buf));


if(USE_LTE){
//Modem LTE Connection
Expand All @@ -170,6 +179,10 @@ static void sensors_init(void)
}
}

k_sleep(K_SECONDS(2));
update_ts_modem();
k_sleep(K_SECONDS(2));

//GPS
if(USE_GPS){
gp = gps_control_init(&application_work_q, gps_trigger_handler);
Expand Down Expand Up @@ -240,6 +253,9 @@ void main(void)
}
k_sleep(K_SECONDS(2));
socket_toggle(false);
if(!GPS_FIRST_FIX){
update_ts_modem();
}
}
k_sleep(K_SECONDS(ADV_POST_INTERVAL-4));
}
Expand Down
45 changes: 0 additions & 45 deletions src/modem_controller/modem_controller.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#include <zephyr.h>
#include <lte_lc.h>
#include <net/bsdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <modem_info.h>
#include <time.h>

#include "ruuvinode.h"
#include "modem_controller.h"

void setup_psm(void)
Expand Down Expand Up @@ -90,43 +84,4 @@ int lte_connect(enum modem_actions action)
printk("LTE link could not be established, or maintained\n");

return 0;
}

time_t modem_ts(void){
int err;
static char ts_buf[MODEM_TIME_LEN + 1];
struct tm t;
time_t epoch;
int tz;
char *pch;
err = modem_info_string_get(MODEM_INFO_DATE_TIME, ts_buf);
if (err != MODEM_TIME_LEN) {
printk("modem_info_string_get(MODEM time), error: %d", err);
return -1;
}

char *msg = strdup(ts_buf);

pch = strtok(msg, "/,:+");
t.tm_year = atoi(pch);
t.tm_year = t.tm_year + 2000 -1900;
pch = strtok(NULL, "/,:+");
t.tm_mon = atoi(pch);
t.tm_mon = t.tm_mon -1;
pch = strtok(NULL, "/,:+");
t.tm_mday = atoi(pch);
pch = strtok(NULL, "/,:+");
t.tm_hour = atoi(pch);
pch = strtok(NULL, "/,:+");
t.tm_min = atoi(pch);
pch = strtok(NULL, "/,:+");
t.tm_sec = atoi(pch);
pch = strtok(NULL, "/,:+");
tz = atoi(pch);
t.tm_isdst = -1;
epoch = mktime(&t);
memset(ts_buf, 0, MODEM_TIME_LEN + 1);
free(msg);
free(pch);
return epoch;
}
4 changes: 1 addition & 3 deletions src/modem_controller/modem_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ enum modem_actions {

void setup_psm(void);

int lte_connect(enum modem_actions action);

time_t modem_ts(void);
int lte_connect(enum modem_actions action);
2 changes: 2 additions & 0 deletions src/time_handler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
zephyr_include_directories(.)
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/time_handler.c)
71 changes: 71 additions & 0 deletions src/time_handler/time_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <zephyr.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <modem_info.h>
#include <time.h>

#include "ruuvinode.h"
#include "time_handler.h"

time_t epoch;
u32_t epoch_tk;

int update_ts_modem(void){
int err;
static char ts_buf[MODEM_TIME_LEN + 1];
struct tm t;
int tz;
char *pch;
err = modem_info_string_get(MODEM_INFO_DATE_TIME, ts_buf);
if (err != MODEM_TIME_LEN) {
printk("modem_info_string_get(MODEM time), error: %d", err);
return -1;
}
epoch_tk = k_uptime_get_32();

char *msg = strdup(ts_buf);

pch = strtok(msg, "/,:+");
t.tm_year = atoi(pch);
t.tm_year = t.tm_year + 2000 -1900;
pch = strtok(NULL, "/,:+");
t.tm_mon = atoi(pch);
t.tm_mon = t.tm_mon -1;
pch = strtok(NULL, "/,:+");
t.tm_mday = atoi(pch);
pch = strtok(NULL, "/,:+");
t.tm_hour = atoi(pch);
pch = strtok(NULL, "/,:+");
t.tm_min = atoi(pch);
pch = strtok(NULL, "/,:+");
t.tm_sec = atoi(pch);
pch = strtok(NULL, "/,:+");
tz = atoi(pch);
t.tm_isdst = -1;
epoch = mktime(&t);
memset(ts_buf, 0, MODEM_TIME_LEN + 1);
free(msg);
free(pch);
printk("TS updated by Modem\n");
return 0;
}

void update_ts_gps(struct tm *g){
epoch_tk = k_uptime_get_32();
epoch = mktime(g);
printk("GPS epoch: %lld\n", epoch);
printk("TS updated by GPS\n");
return;

}

time_t get_ts(void){
time_t ts;
u32_t now;
now = k_uptime_get_32();
now = now - epoch_tk;
now = now / 1000;
ts = epoch + now;
return ts;
}
8 changes: 8 additions & 0 deletions src/time_handler/time_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <zephyr.h>
#include <time.h>

int update_ts_modem(void);

void update_ts_gps(struct tm *g);

time_t get_ts(void);
16 changes: 2 additions & 14 deletions src/uart_controller/uart_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "data_parser.h"
#include "uart_controller.h"
#include "modem_controller.h"
#include "time_handler.h"
#include "ruuvinode.h"

//Define the device
Expand All @@ -35,8 +35,6 @@ char rx_buf[UART_RX_BUF_SIZE];

struct ble_report tag[MAX_ADVS_TABLE];
struct ble_report tag_buf[MAX_ADVS_TABLE];
time_t ts_buf;
u32_t ts_buf_tk;
int tag_count;
int tag_count_buf;

Expand All @@ -51,8 +49,6 @@ static void uart_data_parse(char *msg_orig){
char *data;
char *tag_mac;
char *msg = strdup(msg_orig);
u32_t now;
time_t now_ts;

pch = strtok(msg, ",");
while (pch != NULL && i < 4) {
Expand Down Expand Up @@ -92,10 +88,6 @@ static void uart_data_parse(char *msg_orig){
goto end;
}

now = k_uptime_get_32();
now = now - ts_buf_tk;
now = now / 1000;
now_ts = ts_buf + now;
if(tag_count >= MAX_ADVS_TABLE){
//printk("Reached limit\n");
}
Expand All @@ -104,7 +96,7 @@ static void uart_data_parse(char *msg_orig){
tag[tag_count].rssi = rssi;
strcpy(tag[tag_count].data, data);

tag[tag_count].timestamp = now_ts;
tag[tag_count].timestamp = get_ts();
++tag_count;
}
end:
Expand Down Expand Up @@ -151,8 +143,6 @@ void uart_driver_write(char *data)
// Prepares UART data for sending to cloud
void process_uart(void)
{
ts_buf = json_prepare_time();
ts_buf_tk = k_uptime_get_32();
tag_count_buf = tag_count;
tag_count = 0;
memcpy(tag_buf, tag, sizeof tag);
Expand All @@ -173,8 +163,6 @@ u8_t uart_init()
uart_irq_callback_set(uart_dev, uart_fifo_callback);
uart_irq_rx_enable(uart_dev);
printk("UART device loaded.\n");
ts_buf = modem_ts();
ts_buf_tk = k_uptime_get_32();
return 0;
}

Expand Down

0 comments on commit 5d35261

Please sign in to comment.