diff --git a/include/sync_service.h b/include/sync_service.h index 7a9fad6..d1c25c7 100644 --- a/include/sync_service.h +++ b/include/sync_service.h @@ -8,6 +8,6 @@ #define SYNC_SERVICE_H int sync_service_init(void); -int sync_service_run(void); +uint32_t sync_service_run(void); #endif diff --git a/include/tracing.h b/include/tracing.h index 91a908b..1ef9005 100644 --- a/include/tracing.h +++ b/include/tracing.h @@ -8,6 +8,6 @@ #define TRACING_H int tracing_init(void); -int tracing_run(void); +uint32_t tracing_run(void); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 23ec9e1..4d75344 100644 --- a/src/main.c +++ b/src/main.c @@ -65,7 +65,11 @@ void main(void) { printk("Components initialized! Starting Tracing and Gatt...\n"); do { - tracing_run(); - sync_service_run(); + uint32_t tracing_sleep_ms = tracing_run(); + uint32_t sync_sleep_ms = sync_service_run(); + + uint32_t sleep_ms = MIN(tracing_sleep_ms, sync_sleep_ms); + //printk("Sleeping a bit (%u ms)...\n", sleep_ms); + k_sleep(K_MSEC(sleep_ms)); // TODO: what to put here? } while (1); } \ No newline at end of file diff --git a/src/sync_service.c b/src/sync_service.c index a6ae4c1..2fb24df 100644 --- a/src/sync_service.c +++ b/src/sync_service.c @@ -12,16 +12,35 @@ #include #include -#include -#include -#include -#include +#define SYNC_ADV_INTERVAL_MS (60*1000) +#define SYNC_ADV_DURATION_MS 500 +#define SYNC_CONN_INIT_WAIT_MS 250 +K_TIMER_DEFINE(sync_adv_timer, NULL, NULL); int sync_service_init(void) { - return 0; // TODO! + // We init the timers (which should run periodically!) + // we directly want to advertise ourselfs after the start -> should reduce unwanted delays + k_timer_start(&sync_adv_timer, K_NO_WAIT, K_MSEC(SYNC_ADV_INTERVAL_MS)); + return 0; } -int sync_service_run(void) { - return 0; +void sync_service_handle_connection() { + + // TODO: Implement me! +} + +uint32_t sync_service_run(void) { + + if (k_timer_status_get(&sync_adv_timer) > 0) { + + // TODO: START ADVERTISEMENTS + //printk("Advertising Sync service...!\n"); + k_sleep(K_MSEC(SYNC_ADV_DURATION_MS)); + // TODO: STOP ADVERTISEMENTS + } + // TODO: CHECK IF A CONNECTION HAPPENED UNTIL NOW if so -> handle it! + + // we return the timer time so that main can sleep + return k_timer_remaining_get(&sync_adv_timer); } \ No newline at end of file diff --git a/src/tracing.c b/src/tracing.c index 5f9701c..66daa28 100644 --- a/src/tracing.c +++ b/src/tracing.c @@ -26,10 +26,9 @@ typedef ENIntervalIdentifier ENIntervalIdentifier; #define RPI_ROTATION_MS (11*60*1000) -#define SCAN_INTERVAL_MS (10*1000) +#define SCAN_INTERVAL_MS (60*1000) #define SCAN_DURATION_MS 1000 #define ADV_INTERVAL_MS 220 -#define ADV_DURATION_MS 1000 K_TIMER_DEFINE(rpi_timer, NULL, NULL); @@ -49,14 +48,10 @@ int tracing_init() k_timer_start(&adv_timer, K_MSEC(ADV_INTERVAL_MS), K_MSEC(ADV_INTERVAL_MS)); - - - - return 0; } -int tracing_run() +uint32_t tracing_run() { if (k_timer_status_get(&rpi_timer) > 0) { on_rpi(); @@ -70,13 +65,10 @@ int tracing_run() on_adv(); } - k_sleep(K_MSEC(ADV_INTERVAL_MS)); // TODO: what to put here? - - //printk("covid start\n"); - - - //printk("covid end\n"); - return 0; + // we return the minimum timer time so that main can sleep + return MIN( k_timer_remaining_get(&rpi_timer), + MIN(k_timer_remaining_get(&scan_timer), + k_timer_remaining_get(&adv_timer))); }