116 lines
2.2 KiB
C
116 lines
2.2 KiB
C
|
|
#include <stdbool.h>
|
||
|
|
|
||
|
|
#include <app_event_manager.h>
|
||
|
|
|
||
|
|
#define MODULE ble_adv_ctrl_module
|
||
|
|
#include <caf/events/module_state_event.h>
|
||
|
|
#include <caf/events/module_suspend_event.h>
|
||
|
|
|
||
|
|
#include <zephyr/logging/log.h>
|
||
|
|
|
||
|
|
#include "mode_switch_event.h"
|
||
|
|
|
||
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||
|
|
|
||
|
|
static bool initialized;
|
||
|
|
static bool running;
|
||
|
|
static bool ble_adv_suspended = true;
|
||
|
|
|
||
|
|
static void broadcast_ble_adv_req(bool suspend)
|
||
|
|
{
|
||
|
|
if (suspend) {
|
||
|
|
struct module_suspend_req_event *event = new_module_suspend_req_event();
|
||
|
|
|
||
|
|
event->sink_module_id = MODULE_ID(ble_adv);
|
||
|
|
event->src_module_id = MODULE_ID(MODULE);
|
||
|
|
APP_EVENT_SUBMIT(event);
|
||
|
|
} else {
|
||
|
|
struct module_resume_req_event *event = new_module_resume_req_event();
|
||
|
|
|
||
|
|
event->sink_module_id = MODULE_ID(ble_adv);
|
||
|
|
event->src_module_id = MODULE_ID(MODULE);
|
||
|
|
APP_EVENT_SUBMIT(event);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static int module_init(void)
|
||
|
|
{
|
||
|
|
ble_adv_suspended = true;
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int module_start(void)
|
||
|
|
{
|
||
|
|
if (running) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
running = true;
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void module_pause(void)
|
||
|
|
{
|
||
|
|
running = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||
|
|
{
|
||
|
|
bool should_suspend;
|
||
|
|
|
||
|
|
if (!running) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
should_suspend = (event->mode != MODE_SWITCH_BLE);
|
||
|
|
|
||
|
|
if (should_suspend != ble_adv_suspended) {
|
||
|
|
ble_adv_suspended = should_suspend;
|
||
|
|
broadcast_ble_adv_req(should_suspend);
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||
|
|
{
|
||
|
|
if (is_mode_switch_event(aeh)) {
|
||
|
|
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (is_module_state_event(aeh)) {
|
||
|
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
||
|
|
|
||
|
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||
|
|
int err;
|
||
|
|
|
||
|
|
if (!initialized) {
|
||
|
|
err = module_init();
|
||
|
|
if (err) {
|
||
|
|
module_set_state(MODULE_STATE_ERROR);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
initialized = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
err = module_start();
|
||
|
|
if (err) {
|
||
|
|
module_set_state(MODULE_STATE_ERROR);
|
||
|
|
} else {
|
||
|
|
module_set_state(MODULE_STATE_READY);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||
|
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||
|
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|