From cb93467941efcd7e97bae76311cb82b7bade29ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20S=2E=20R=C3=B8stad?= Date: Tue, 20 Aug 2019 12:53:11 +0200 Subject: [PATCH] fix: added semaphore, blocks until gps has exectued stop function --- .../src/gps_controller/gps_controller.c | 32 +++++++++++++++---- .../src/mqtt_behaviour/mqtt_behaviour.c | 7 +--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/applications/cat_tracker/src/gps_controller/gps_controller.c b/applications/cat_tracker/src/gps_controller/gps_controller.c index 23169fa0433..0c4a4a63ffd 100644 --- a/applications/cat_tracker/src/gps_controller/gps_controller.c +++ b/applications/cat_tracker/src/gps_controller/gps_controller.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -9,6 +10,8 @@ #include LOG_MODULE_REGISTER(gps_control, CONFIG_GPS_CONTROL_LOG_LEVEL); +#define GPS_THREAD_DEADLINE 60 + /* Structure to hold GPS work information */ static struct { enum { GPS_WORK_START, GPS_WORK_STOP } type; @@ -18,6 +21,11 @@ static struct { u32_t fix_count; } gps_work; +K_SEM_DEFINE(gps_stop_sem, 0, 1); + +struct k_poll_event gps_events[1] = { K_POLL_EVENT_STATIC_INITIALIZER( + K_POLL_TYPE_SEM_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, &gps_stop_sem, 0) }; + static void gps_work_handler(struct k_work *work) { int err; @@ -25,21 +33,23 @@ static void gps_work_handler(struct k_work *work) if (gps_work.type == GPS_WORK_START) { err = gps_start(gps_work.dev); if (err) { - printk("GPS could not be started, error: %d\n", err); + LOG_DBG("GPS could not be started, error: %d\n", err); return; } - printk("GPS started successfully.\nSearching for satellites "); - printk("to get position fix. This may take several minutes.\n"); + LOG_DBG("GPS started successfully.\nSearching for satellites "); + LOG_DBG("to get position fix. This may take several minutes.\n"); return; } else if (gps_work.type == GPS_WORK_STOP) { err = gps_stop(gps_work.dev); if (err) { - printk("GPS could not be stopped, error: %d\n", err); + LOG_DBG("GPS could not be stopped, error: %d\n", err); return; } + k_sem_give(gps_events[0].sem); + return; } } @@ -49,7 +59,15 @@ void gps_control_stop(void) gps_work.type = GPS_WORK_STOP; k_work_submit(&gps_work.work); gps_search_led_stop(); - k_sleep(1000); + k_poll(gps_events, 1, K_SECONDS(GPS_THREAD_DEADLINE)); + + if (gps_events[0].state == K_POLL_STATE_SEM_AVAILABLE) { + k_sem_take(gps_events[0].sem, 0); + } else { + sys_reboot(0); + /*Should be forwarded to an error handler */ + } + gps_events[0].state = K_POLL_STATE_NOT_READY; } void gps_control_start(void) @@ -79,13 +97,13 @@ int gps_control_init(gps_trigger_handler_t handler) gps_dev = device_get_binding(CONFIG_GPS_DEV_NAME); if (gps_dev == NULL) { - printk("Could not get %s device\n", CONFIG_GPS_DEV_NAME); + LOG_DBG("Could not get %s device\n", CONFIG_GPS_DEV_NAME); return -ENODEV; } err = gps_trigger_set(gps_dev, &gps_trig, handler); if (err) { - printk("Could not set trigger, error code: %d\n", err); + LOG_DBG("Could not set trigger, error code: %d\n", err); return err; } diff --git a/applications/cat_tracker/src/mqtt_behaviour/mqtt_behaviour.c b/applications/cat_tracker/src/mqtt_behaviour/mqtt_behaviour.c index 2d1a93d339e..5a71ce77cf6 100644 --- a/applications/cat_tracker/src/mqtt_behaviour/mqtt_behaviour.c +++ b/applications/cat_tracker/src/mqtt_behaviour/mqtt_behaviour.c @@ -10,7 +10,7 @@ #include #include -#define APP_SLEEP_MS 500 +#define APP_SLEEP_MS 5000 #define APP_CONNECT_TRIES 5 struct Sync_data sync_data = { .gps_timeout = 180, @@ -555,11 +555,6 @@ int publish_data(bool syncronization, bool pub_modem_d) printk("Could not connect to client\n"); } - err = mqtt_ping(&client); - if (err != 0) { - printk("Could not ping broker: %d\n", err); - } - if (connected) { publish_data_led();