Skip to content

Commit

Permalink
Register handler for HAL events before app_setup_and_loop() is called
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeuz committed Jan 1, 2017
1 parent d434c05 commit 3cca785
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,22 +590,34 @@ class LEDDeviceKeyStatus: public LEDStatus {
}
};

// Callback for HAL events
void hal_event_handler(int event, int flags, void* data) {
switch (event) {
case HAL_EVENT_GENERATE_DEVICE_KEY: {
static LEDDeviceKeyStatus status(LED_PRIORITY_IMPORTANT);
if (flags & HAL_EVENT_FLAG_START) {
status.setActive(true);
} else if (flags & HAL_EVENT_FLAG_STOP) {
status.setActive(false);
}
break;
// Handler for HAL events
class HALEventHandler {
public:
HALEventHandler() {
HAL_Set_Event_Callback(handleEvent, nullptr); // Register callback
}
default:
break;

private:
static void handleEvent(int event, int flags, void* data) {
switch (event) {
case HAL_EVENT_GENERATE_DEVICE_KEY: {
static LEDDeviceKeyStatus status(LED_PRIORITY_IMPORTANT);
if (flags & HAL_EVENT_FLAG_START) {
status.setActive(true);
} else if (flags & HAL_EVENT_FLAG_STOP) {
status.setActive(false);
}
break;
}
default:
break;
}
}
}
};

// Certain HAL events can be generated before app_setup_and_loop() is called. Using constructor of a
// global variable allows to register a handler for HAL events early
HALEventHandler halEventHandler;

} // namespace

Expand All @@ -624,9 +636,6 @@ void app_setup_and_loop(void)
// We have running firmware, otherwise we wouldn't have gotten here
DECLARE_SYS_HEALTH(ENTERED_Main);

// Register callback for HAL events
HAL_Set_Event_Callback(hal_event_handler, nullptr);

LED_SIGNAL_START(NETWORK_OFF, BACKGROUND);

#if Wiring_Cellular == 1
Expand Down

0 comments on commit 3cca785

Please sign in to comment.