Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

Commit

Permalink
fix: added semaphore, blocks until gps has exectued stop function
Browse files Browse the repository at this point in the history
  • Loading branch information
simensrostad committed Aug 20, 2019
1 parent 7c25511 commit cb93467
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
32 changes: 25 additions & 7 deletions applications/cat_tracker/src/gps_controller/gps_controller.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <zephyr.h>
#include <misc/util.h>
#include <misc/reboot.h>
#include <gps.h>
#include <lte_lc.h>
#include <leds.h>
Expand All @@ -9,6 +10,8 @@
#include <logging/log.h>
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;
Expand All @@ -18,28 +21,35 @@ 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;

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;
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 1 addition & 6 deletions applications/cat_tracker/src/mqtt_behaviour/mqtt_behaviour.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <mqtt_codec.h>
#include <leds.h>

#define APP_SLEEP_MS 500
#define APP_SLEEP_MS 5000
#define APP_CONNECT_TRIES 5

struct Sync_data sync_data = { .gps_timeout = 180,
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit cb93467

Please sign in to comment.