From 3cca785ec21a2a84de125eb35edd3ff99cd0c9c5 Mon Sep 17 00:00:00 2001 From: Sergey Polyakov Date: Mon, 2 Jan 2017 00:43:39 +0200 Subject: [PATCH] Register handler for HAL events before app_setup_and_loop() is called --- system/src/main.cpp | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/system/src/main.cpp b/system/src/main.cpp index 516e26d2af..35ef8e5b82 100644 --- a/system/src/main.cpp +++ b/system/src/main.cpp @@ -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 @@ -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