From 8c37ec0184233eb5cb06403af18d076ca702fd29 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Mon, 28 Jan 2019 19:47:38 +0100 Subject: [PATCH 01/10] Add support for LMIC_FAILURE_HANDLER --- src/hal/hal.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index 6945f932..eabb5590 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -385,6 +385,10 @@ bool hal_init_with_pinmap(const HalPinmap_t *pPinmap) }; // namespace Arduino_LMIC void hal_failed (const char *file, u2_t line) { +#if defined(LMIC_FAILURE_HANDLER) + extern void LMIC_FAILURE_HANDLER(const char *file, u2_t line); + LMIC_FAILURE_HANDLER(file, line); +#else #if defined(LMIC_FAILURE_TO) LMIC_FAILURE_TO.println("FAILURE "); LMIC_FAILURE_TO.print(file); @@ -394,6 +398,7 @@ void hal_failed (const char *file, u2_t line) { #endif hal_disableIRQs(); while(1); +#endif } ostime_t hal_setModuleActive (bit_t val) { @@ -405,4 +410,4 @@ ostime_t hal_setModuleActive (bit_t val) { bit_t hal_queryUsingTcxo(void) { return pHalConfig->queryUsingTcxo(); -} \ No newline at end of file +} From f65a620ede01ddc2fe1c5edde6dadb656f88819e Mon Sep 17 00:00:00 2001 From: frazar Date: Mon, 28 Jan 2019 20:01:55 +0100 Subject: [PATCH 02/10] Inlude explanation of LMIC_FAILURE_HANDLER usage --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 127721e6..a6978ce9 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,23 @@ control the handling of runtime assertion failures. By default, assertion messag the `Serial` object. You can define LMIC_FAILURE_TO to be the name of some other `Print`-like obect. You can also define `DISABLE_LMIC_FAILURE_TO` to any value, in which case assert failures will silently halt execution. +#### Defining custom failure handling + +`#define LMIC_FAILURE_HANDLER my_failure_handler /* function with signature "void my_failure_handler(const char *file, uint16_t line)" */` + +Define a custom function for handling failures of the LMIC library. + +By default when an unrecoverable error occurs in the LMIC library, the code silently halts execution. To implement a different failure handling behaviour, define the variable `LMIC_FAILURE_HANDLER` in "lmic_project_config.h" as shown above and include a failure-handling function in your sketch. +For example + +```C++ +void my_failure_handler(const char *file, uint16_t line) { + Serial.println("MY FAILURE HANDLER WAS TRIGGERED"); + save_sensitive_data_to_nonvolatile_memory(); + reboot(); +} +``` + #### Disabling JOIN `#define DISABLE_JOIN` From 109e5bc4084b9526f7778dce110f7f3b310fc12f Mon Sep 17 00:00:00 2001 From: frazar Date: Mon, 28 Jan 2019 20:14:00 +0100 Subject: [PATCH 03/10] Improve wording of section on `LMIC_FAILURE_HANDLER` --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a6978ce9..976b717b 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,9 @@ also define `DISABLE_LMIC_FAILURE_TO` to any value, in which case assert failure #### Defining custom failure handling -`#define LMIC_FAILURE_HANDLER my_failure_handler /* function with signature "void my_failure_handler(const char *file, uint16_t line)" */` +```C +#define LMIC_FAILURE_HANDLER my_failure_handler /* "my_failure_handler()" defined later +``` Define a custom function for handling failures of the LMIC library. @@ -302,13 +304,15 @@ By default when an unrecoverable error occurs in the LMIC library, the code sile For example ```C++ -void my_failure_handler(const char *file, uint16_t line) { +void my_failure_handler(const char *file_name, uint16_t line) { Serial.println("MY FAILURE HANDLER WAS TRIGGERED"); save_sensitive_data_to_nonvolatile_memory(); reboot(); } ``` +The `file_name` and `line` paramters correspond, respectively, to the name of the file and the line of the file where the failure occured. + #### Disabling JOIN `#define DISABLE_JOIN` From f3b52c3a50875c8546a9a545dd574ccc748086f2 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Wed, 13 Feb 2019 16:15:52 +0100 Subject: [PATCH 04/10] Revert "Add support for LMIC_FAILURE_HANDLER" This reverts commit 8c37ec0184233eb5cb06403af18d076ca702fd29. --- src/hal/hal.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index eabb5590..6945f932 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -385,10 +385,6 @@ bool hal_init_with_pinmap(const HalPinmap_t *pPinmap) }; // namespace Arduino_LMIC void hal_failed (const char *file, u2_t line) { -#if defined(LMIC_FAILURE_HANDLER) - extern void LMIC_FAILURE_HANDLER(const char *file, u2_t line); - LMIC_FAILURE_HANDLER(file, line); -#else #if defined(LMIC_FAILURE_TO) LMIC_FAILURE_TO.println("FAILURE "); LMIC_FAILURE_TO.print(file); @@ -398,7 +394,6 @@ void hal_failed (const char *file, u2_t line) { #endif hal_disableIRQs(); while(1); -#endif } ostime_t hal_setModuleActive (bit_t val) { @@ -410,4 +405,4 @@ ostime_t hal_setModuleActive (bit_t val) { bit_t hal_queryUsingTcxo(void) { return pHalConfig->queryUsingTcxo(); -} +} \ No newline at end of file From 3fc3090134b7421023bcb33d2b0efb0dc531ae21 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Wed, 13 Feb 2019 16:17:23 +0100 Subject: [PATCH 05/10] Revert "Improve wording of section on `LMIC_FAILURE_HANDLER`" This reverts commit 109e5bc4084b9526f7778dce110f7f3b310fc12f. --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d0482518..ad66635f 100644 --- a/README.md +++ b/README.md @@ -294,9 +294,7 @@ also define `DISABLE_LMIC_FAILURE_TO` to any value, in which case assert failure #### Defining custom failure handling -```C -#define LMIC_FAILURE_HANDLER my_failure_handler /* "my_failure_handler()" defined later -``` +`#define LMIC_FAILURE_HANDLER my_failure_handler /* function with signature "void my_failure_handler(const char *file, uint16_t line)" */` Define a custom function for handling failures of the LMIC library. @@ -304,15 +302,13 @@ By default when an unrecoverable error occurs in the LMIC library, the code sile For example ```C++ -void my_failure_handler(const char *file_name, uint16_t line) { +void my_failure_handler(const char *file, uint16_t line) { Serial.println("MY FAILURE HANDLER WAS TRIGGERED"); save_sensitive_data_to_nonvolatile_memory(); reboot(); } ``` -The `file_name` and `line` paramters correspond, respectively, to the name of the file and the line of the file where the failure occured. - #### Disabling JOIN `#define DISABLE_JOIN` From 385416d2316516deb45549dd1b6097ad2b02af0f Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Wed, 13 Feb 2019 16:17:31 +0100 Subject: [PATCH 06/10] Revert "Inlude explanation of LMIC_FAILURE_HANDLER usage" This reverts commit f65a620ede01ddc2fe1c5edde6dadb656f88819e. --- README.md | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/README.md b/README.md index ad66635f..8b032907 100644 --- a/README.md +++ b/README.md @@ -292,23 +292,6 @@ control the handling of runtime assertion failures. By default, assertion messag the `Serial` object. You can define LMIC_FAILURE_TO to be the name of some other `Print`-like obect. You can also define `DISABLE_LMIC_FAILURE_TO` to any value, in which case assert failures will silently halt execution. -#### Defining custom failure handling - -`#define LMIC_FAILURE_HANDLER my_failure_handler /* function with signature "void my_failure_handler(const char *file, uint16_t line)" */` - -Define a custom function for handling failures of the LMIC library. - -By default when an unrecoverable error occurs in the LMIC library, the code silently halts execution. To implement a different failure handling behaviour, define the variable `LMIC_FAILURE_HANDLER` in "lmic_project_config.h" as shown above and include a failure-handling function in your sketch. -For example - -```C++ -void my_failure_handler(const char *file, uint16_t line) { - Serial.println("MY FAILURE HANDLER WAS TRIGGERED"); - save_sensitive_data_to_nonvolatile_memory(); - reboot(); -} -``` - #### Disabling JOIN `#define DISABLE_JOIN` From 219f9f0170a8ff42eb6450db7c0af2bc182f208b Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Wed, 13 Feb 2019 16:20:45 +0100 Subject: [PATCH 07/10] Allow user to specify custom failure handler --- src/hal/hal.cpp | 20 ++++++++++++-------- src/lmic/lmic.c | 5 +++++ src/lmic/lmic.h | 6 ++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index 6945f932..da1f6068 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -385,15 +385,19 @@ bool hal_init_with_pinmap(const HalPinmap_t *pPinmap) }; // namespace Arduino_LMIC void hal_failed (const char *file, u2_t line) { + if (LMIC.hal_failure_handler != NULL) { + (*LMIC.hal_failure_handler)(file, line); + } else { #if defined(LMIC_FAILURE_TO) - LMIC_FAILURE_TO.println("FAILURE "); - LMIC_FAILURE_TO.print(file); - LMIC_FAILURE_TO.print(':'); - LMIC_FAILURE_TO.println(line); - LMIC_FAILURE_TO.flush(); + LMIC_FAILURE_TO.println("FAILURE "); + LMIC_FAILURE_TO.print(file); + LMIC_FAILURE_TO.print(':'); + LMIC_FAILURE_TO.println(line); + LMIC_FAILURE_TO.flush(); #endif - hal_disableIRQs(); - while(1); + hal_disableIRQs(); + while(1); + } } ostime_t hal_setModuleActive (bit_t val) { @@ -405,4 +409,4 @@ ostime_t hal_setModuleActive (bit_t val) { bit_t hal_queryUsingTcxo(void) { return pHalConfig->queryUsingTcxo(); -} \ No newline at end of file +} diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index 5fa21675..5d213517 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -1998,6 +1998,7 @@ void LMIC_reset (void) { LMIC.netDeviceTime = 0; // the "invalid" time. LMIC.netDeviceTimeFrac = 0; #endif // LMIC_ENABLE_DeviceTimeReq + LMIC.hal_failure_handler = NULL; } @@ -2169,3 +2170,7 @@ int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference) { #endif // LMIC_ENABLE_DeviceTimeReq return 0; } + +void LMIC_setHalFailureHandler(hal_failure_handler_t* handler) { + LMIC.hal_failure_handler = handler; +} diff --git a/src/lmic/lmic.h b/src/lmic/lmic.h index bcb5318e..db201299 100644 --- a/src/lmic/lmic.h +++ b/src/lmic/lmic.h @@ -272,6 +272,8 @@ enum lmic_request_time_state_e { typedef u1_t lmic_request_time_state_t; +typedef void LMIC_ABI_STD hal_failure_handler_t(const char *file, uint16_t line); + struct lmic_t { // Radio settings TX/RX (also accessed by HAL) ostime_t txend; @@ -409,6 +411,8 @@ struct lmic_t { #endif u1_t noRXIQinversion; + + hal_failure_handler_t* hal_failure_handler; }; //! \var struct lmic_t LMIC @@ -463,6 +467,8 @@ void LMIC_getSessionKeys (u4_t *netid, devaddr_t *devaddr, xref2u1_t nwkKey, xre void LMIC_requestNetworkTime(lmic_request_network_time_cb_t *pCallbackfn, void *pUserData); int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference); +void LMIC_setHalFailureHandler(hal_failure_handler_t* handler); + // Declare onEvent() function, to make sure any definition will have the // C conventions, even when in a C++ file. DECL_ON_LMIC_EVENT; From 38df23bd6321df2824060bf92099a5a8d8f28daa Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Mon, 18 Feb 2019 10:14:22 +0100 Subject: [PATCH 08/10] Revert "Allow user to specify custom failure handler" This reverts commit 219f9f0170a8ff42eb6450db7c0af2bc182f208b. --- src/hal/hal.cpp | 20 ++++++++------------ src/lmic/lmic.c | 5 ----- src/lmic/lmic.h | 6 ------ 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index da1f6068..6945f932 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -385,19 +385,15 @@ bool hal_init_with_pinmap(const HalPinmap_t *pPinmap) }; // namespace Arduino_LMIC void hal_failed (const char *file, u2_t line) { - if (LMIC.hal_failure_handler != NULL) { - (*LMIC.hal_failure_handler)(file, line); - } else { #if defined(LMIC_FAILURE_TO) - LMIC_FAILURE_TO.println("FAILURE "); - LMIC_FAILURE_TO.print(file); - LMIC_FAILURE_TO.print(':'); - LMIC_FAILURE_TO.println(line); - LMIC_FAILURE_TO.flush(); + LMIC_FAILURE_TO.println("FAILURE "); + LMIC_FAILURE_TO.print(file); + LMIC_FAILURE_TO.print(':'); + LMIC_FAILURE_TO.println(line); + LMIC_FAILURE_TO.flush(); #endif - hal_disableIRQs(); - while(1); - } + hal_disableIRQs(); + while(1); } ostime_t hal_setModuleActive (bit_t val) { @@ -409,4 +405,4 @@ ostime_t hal_setModuleActive (bit_t val) { bit_t hal_queryUsingTcxo(void) { return pHalConfig->queryUsingTcxo(); -} +} \ No newline at end of file diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index 5d213517..5fa21675 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -1998,7 +1998,6 @@ void LMIC_reset (void) { LMIC.netDeviceTime = 0; // the "invalid" time. LMIC.netDeviceTimeFrac = 0; #endif // LMIC_ENABLE_DeviceTimeReq - LMIC.hal_failure_handler = NULL; } @@ -2170,7 +2169,3 @@ int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference) { #endif // LMIC_ENABLE_DeviceTimeReq return 0; } - -void LMIC_setHalFailureHandler(hal_failure_handler_t* handler) { - LMIC.hal_failure_handler = handler; -} diff --git a/src/lmic/lmic.h b/src/lmic/lmic.h index db201299..bcb5318e 100644 --- a/src/lmic/lmic.h +++ b/src/lmic/lmic.h @@ -272,8 +272,6 @@ enum lmic_request_time_state_e { typedef u1_t lmic_request_time_state_t; -typedef void LMIC_ABI_STD hal_failure_handler_t(const char *file, uint16_t line); - struct lmic_t { // Radio settings TX/RX (also accessed by HAL) ostime_t txend; @@ -411,8 +409,6 @@ struct lmic_t { #endif u1_t noRXIQinversion; - - hal_failure_handler_t* hal_failure_handler; }; //! \var struct lmic_t LMIC @@ -467,8 +463,6 @@ void LMIC_getSessionKeys (u4_t *netid, devaddr_t *devaddr, xref2u1_t nwkKey, xre void LMIC_requestNetworkTime(lmic_request_network_time_cb_t *pCallbackfn, void *pUserData); int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference); -void LMIC_setHalFailureHandler(hal_failure_handler_t* handler); - // Declare onEvent() function, to make sure any definition will have the // C conventions, even when in a C++ file. DECL_ON_LMIC_EVENT; From a334e3bc29782781bb7984422272070464149502 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Mon, 18 Feb 2019 10:58:10 +0100 Subject: [PATCH 09/10] Allow user to specify custom failure handler --- src/hal/hal.cpp | 25 ++++++++++++++++++------- src/lmic/hal.h | 13 +++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index 6945f932..82167ad4 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -384,16 +384,27 @@ bool hal_init_with_pinmap(const HalPinmap_t *pPinmap) } }; // namespace Arduino_LMIC + +static hal_failure_handler_t* custom_hal_failure_handler = NULL; + void hal_failed (const char *file, u2_t line) { + if (custom_hal_failure_handler != NULL) { + (*custom_hal_failure_handler)(file, line); + } else { #if defined(LMIC_FAILURE_TO) - LMIC_FAILURE_TO.println("FAILURE "); - LMIC_FAILURE_TO.print(file); - LMIC_FAILURE_TO.print(':'); - LMIC_FAILURE_TO.println(line); - LMIC_FAILURE_TO.flush(); + LMIC_FAILURE_TO.println("FAILURE "); + LMIC_FAILURE_TO.print(file); + LMIC_FAILURE_TO.print(':'); + LMIC_FAILURE_TO.println(line); + LMIC_FAILURE_TO.flush(); #endif - hal_disableIRQs(); - while(1); + hal_disableIRQs(); + while(1); + } +} + +void hal_set_failure_handler(const hal_failure_handler_t* const handler) { + custom_hal_failure_handler = handler; } ostime_t hal_setModuleActive (bit_t val) { diff --git a/src/lmic/hal.h b/src/lmic/hal.h index 5b610f44..dd467075 100644 --- a/src/lmic/hal.h +++ b/src/lmic/hal.h @@ -33,10 +33,17 @@ # include "oslmic_types.h" #endif +#ifndef _lmic_env_h_ +# include "lmic_env.h" +#endif + #ifdef __cplusplus extern "C"{ #endif +// The type of an optional user-defined failure handler routine +typedef void LMIC_ABI_STD hal_failure_handler_t(const char* const file, const uint16_t line); + /* * initialize hardware (IO, SPI, TIMER, IRQ). * This API is deprecated as it uses the const global lmic_pins, @@ -116,6 +123,12 @@ u1_t hal_checkTimer (u4_t targettime); */ void hal_failed (const char *file, u2_t line); +/* + * set a custom hal failure handler routine. The default behaviour, defined in + * hal_failed(), is to halt by looping infintely. + */ +void hal_set_failure_handler(const hal_failure_handler_t* const); + /* * get the calibration value for radio_rssi */ From 06d4cda9d815e15d9e010f6907519b8af7fcd44f Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Mon, 25 Feb 2019 09:35:21 +0100 Subject: [PATCH 10/10] Loop infinitely if the custom failure handler returns --- src/hal/hal.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index 82167ad4..4cbdd278 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -25,6 +25,7 @@ static const Arduino_LMIC::HalPinmap_t *plmic_pins; static Arduino_LMIC::HalConfiguration_t *pHalConfig; static Arduino_LMIC::HalConfiguration_t nullHalConig; +static hal_failure_handler_t* custom_hal_failure_handler = NULL; static void hal_interrupt_init(); // Fwd declaration @@ -385,21 +386,24 @@ bool hal_init_with_pinmap(const HalPinmap_t *pPinmap) }; // namespace Arduino_LMIC -static hal_failure_handler_t* custom_hal_failure_handler = NULL; - void hal_failed (const char *file, u2_t line) { if (custom_hal_failure_handler != NULL) { (*custom_hal_failure_handler)(file, line); - } else { + } + #if defined(LMIC_FAILURE_TO) - LMIC_FAILURE_TO.println("FAILURE "); - LMIC_FAILURE_TO.print(file); - LMIC_FAILURE_TO.print(':'); - LMIC_FAILURE_TO.println(line); - LMIC_FAILURE_TO.flush(); + LMIC_FAILURE_TO.println("FAILURE "); + LMIC_FAILURE_TO.print(file); + LMIC_FAILURE_TO.print(':'); + LMIC_FAILURE_TO.println(line); + LMIC_FAILURE_TO.flush(); #endif - hal_disableIRQs(); - while(1); + + hal_disableIRQs(); + + // Infinite loop + while (1) { + ; } }