diff --git a/CHANGELOG.md b/CHANGELOG.md index 27369008..c9361c85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), * Add user access to the `lorawan_api_set_no_rx_packet_threshold` function. * Add user access to the beacon statistics data with `smtc_modem_class_b_beacon_get_statistics` function. * Add user access to the `smtc_modem_factory_reset` function. +* Add multi-radio support. NOTE: not fully tested. Samples are not updated or tested. ## [v4.9.0] 2025-10-15 diff --git a/lbm_applications/3_geolocation_on_lora_edge/main_full_almanac_update/main_full_almanac_update.c b/lbm_applications/3_geolocation_on_lora_edge/main_full_almanac_update/main_full_almanac_update.c index 4afea274..8b18cf2b 100644 --- a/lbm_applications/3_geolocation_on_lora_edge/main_full_almanac_update/main_full_almanac_update.c +++ b/lbm_applications/3_geolocation_on_lora_edge/main_full_almanac_update/main_full_almanac_update.c @@ -78,7 +78,7 @@ */ /** - * Stack id value (multistacks modem is not yet available) + * Stack id value (multistacks modem is not yet available) - EvaTODO: Not yet supported */ #define STACK_ID ( 0 ) @@ -240,13 +240,13 @@ static bool check_lr11xx_fw_version( void ) lr11xx_system_version_t lr11xx_fw_version; /* suspend modem to get access to the radio */ - smtc_modem_suspend_radio_communications( true ); + smtc_modem_suspend_radio_communications( STACK_ID, true ); status = lr11xx_system_get_version( NULL, &lr11xx_fw_version ); if( status != LR11XX_STATUS_OK ) { SMTC_HAL_TRACE_ERROR( "Failed to get LR11XX firmware version\n" ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } @@ -255,7 +255,7 @@ static bool check_lr11xx_fw_version( void ) { SMTC_HAL_TRACE_ERROR( "Wrong LR1110 firmware version, expected 0x%04X, got 0x%04X\n", LR1110_FW_VERSION, lr11xx_fw_version.fw ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } if( ( lr11xx_fw_version.type == LR11XX_SYSTEM_VERSION_TYPE_LR1120 ) && @@ -263,12 +263,12 @@ static bool check_lr11xx_fw_version( void ) { SMTC_HAL_TRACE_ERROR( "Wrong LR1120 firmware version, expected 0x%04X, got 0x%04X\n", LR1120_FW_VERSION, lr11xx_fw_version.fw ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } /* release radio to the modem */ - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); SMTC_HAL_TRACE_INFO( "LR11XX FW: 0x%04X, type: 0x%02X\n", lr11xx_fw_version.fw, lr11xx_fw_version.type ); return true; } diff --git a/lbm_applications/3_geolocation_on_lora_edge/main_geolocation/main_geolocation.c b/lbm_applications/3_geolocation_on_lora_edge/main_geolocation/main_geolocation.c index 9534841b..e5f5f8e7 100644 --- a/lbm_applications/3_geolocation_on_lora_edge/main_geolocation/main_geolocation.c +++ b/lbm_applications/3_geolocation_on_lora_edge/main_geolocation/main_geolocation.c @@ -82,7 +82,7 @@ */ /** - * Stack id value (multistacks modem is not yet available) + * Stack id value (multistacks modem is not yet available) - EvaTODO: Not yet supported */ #define STACK_ID 0 @@ -451,13 +451,13 @@ static bool check_lr11xx_fw_version( void ) lr11xx_system_version_t lr11xx_fw_version; /* suspend modem to get access to the radio */ - smtc_modem_suspend_radio_communications( true ); + smtc_modem_suspend_radio_communications( STACK_ID, true ); status = lr11xx_system_get_version( NULL, &lr11xx_fw_version ); if( status != LR11XX_STATUS_OK ) { SMTC_HAL_TRACE_ERROR( "Failed to get LR11XX firmware version\n" ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } @@ -466,7 +466,7 @@ static bool check_lr11xx_fw_version( void ) { SMTC_HAL_TRACE_ERROR( "Wrong LR1110 firmware version, expected 0x%04X, got 0x%04X\n", LR1110_FW_VERSION, lr11xx_fw_version.fw ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } if( ( lr11xx_fw_version.type == LR11XX_SYSTEM_VERSION_TYPE_LR1120 ) && @@ -474,12 +474,12 @@ static bool check_lr11xx_fw_version( void ) { SMTC_HAL_TRACE_ERROR( "Wrong LR1120 firmware version, expected 0x%04X, got 0x%04X\n", LR1120_FW_VERSION, lr11xx_fw_version.fw ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } /* release radio to the modem */ - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); SMTC_HAL_TRACE_INFO( "LR11XX FW: 0x%04X, type: 0x%02X\n", lr11xx_fw_version.fw, lr11xx_fw_version.type ); return true; } diff --git a/lbm_applications/3_geolocation_on_lora_edge/main_wifi_region_detection/main_wifi_region_detection.c b/lbm_applications/3_geolocation_on_lora_edge/main_wifi_region_detection/main_wifi_region_detection.c index a61dc755..0f97b29f 100644 --- a/lbm_applications/3_geolocation_on_lora_edge/main_wifi_region_detection/main_wifi_region_detection.c +++ b/lbm_applications/3_geolocation_on_lora_edge/main_wifi_region_detection/main_wifi_region_detection.c @@ -82,7 +82,7 @@ */ /** - * Stack id value (multistacks modem is not yet available) + * Stack id value (multistacks modem is not yet available) - EvaTODO: Not yet supported */ #define STACK_ID 0 @@ -364,13 +364,13 @@ static bool check_lr11xx_fw_version( void ) lr11xx_system_version_t lr11xx_fw_version; /* suspend modem to get access to the radio */ - smtc_modem_suspend_radio_communications( true ); + smtc_modem_suspend_radio_communications( STACK_ID, true ); status = lr11xx_system_get_version( NULL, &lr11xx_fw_version ); if( status != LR11XX_STATUS_OK ) { SMTC_HAL_TRACE_ERROR( "Failed to get LR11XX firmware version\n" ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } @@ -379,7 +379,7 @@ static bool check_lr11xx_fw_version( void ) { SMTC_HAL_TRACE_ERROR( "Wrong LR1110 firmware version, expected 0x%04X, got 0x%04X\n", LR1110_FW_VERSION, lr11xx_fw_version.fw ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } if( ( lr11xx_fw_version.type == LR11XX_SYSTEM_VERSION_TYPE_LR1120 ) && @@ -387,12 +387,12 @@ static bool check_lr11xx_fw_version( void ) { SMTC_HAL_TRACE_ERROR( "Wrong LR1120 firmware version, expected 0x%04X, got 0x%04X\n", LR1120_FW_VERSION, lr11xx_fw_version.fw ); - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); return false; } /* release radio to the modem */ - smtc_modem_suspend_radio_communications( false ); + smtc_modem_suspend_radio_communications( STACK_ID, false ); SMTC_HAL_TRACE_INFO( "LR11XX FW: 0x%04X, type: 0x%02X\n", lr11xx_fw_version.fw, lr11xx_fw_version.type ); return true; } diff --git a/lbm_examples/hw_modem/cmd_parser.c b/lbm_examples/hw_modem/cmd_parser.c index e32e3857..0a034e3a 100644 --- a/lbm_examples/hw_modem/cmd_parser.c +++ b/lbm_examples/hw_modem/cmd_parser.c @@ -72,7 +72,7 @@ * --- PRIVATE CONSTANTS ------------------------------------------------------- */ -#define STACK_ID 0 +#define STACK_ID 0 // EvaTODO: Not yet supported #if defined( STM32L073xx ) #define FILE_UPLOAD_MAX_SIZE 4096 #else @@ -1824,7 +1824,7 @@ cmd_parse_status_t parse_cmd( cmd_input_t* cmd_input, cmd_response_t* cmd_output } case CMD_SUSPEND_RADIO_COMMUNICATIONS: { - cmd_output->return_code = rc_lut[smtc_modem_suspend_radio_communications( cmd_input->buffer[0] )]; + cmd_output->return_code = rc_lut[smtc_modem_suspend_radio_communications( STACK_ID, cmd_input->buffer[0] )]; break; } case CMD_DM_HANDLE_ALCSYNC: diff --git a/lbm_lib/smtc_modem_api/smtc_modem_api.h b/lbm_lib/smtc_modem_api/smtc_modem_api.h index 5d17f605..6a00e2b2 100755 --- a/lbm_lib/smtc_modem_api/smtc_modem_api.h +++ b/lbm_lib/smtc_modem_api/smtc_modem_api.h @@ -1060,19 +1060,21 @@ smtc_modem_return_code_t smtc_modem_get_suspend_radio_communications( uint8_t st /** * @brief Suspend the radio communications initiated by the modem * - * @param [in] suspend The configuration to be applied (true: suspend communications / false: resume communications) + * @param [in] stack_id Stack identifier + * @param [in] suspend The configuration to be applied (true: suspend communications / false: resume communications) * * @return Modem return code as defined in @ref smtc_modem_return_code_t * @retval SMTC_MODEM_RC_OK Command executed without errors * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode */ -smtc_modem_return_code_t smtc_modem_suspend_radio_communications( bool suspend ); +smtc_modem_return_code_t smtc_modem_suspend_radio_communications( uint8_t stack_id, bool suspend ); /** * @brief Set and start the alarm timer (up to 864000s ie 10 days) * * @remark When the timer expires, an alarm event is generated * + * @param [in] stack_id Stack identifier * @param [in] alarm_timer_in_s Alarm timer in second * * @return Modem return code as defined in @ref smtc_modem_return_code_t @@ -1080,7 +1082,7 @@ smtc_modem_return_code_t smtc_modem_suspend_radio_communications( bool suspend ) * @retval SMTC_MODEM_RC_INVALID \p alarm_timer_in_s exceed max value of 864000s (10 days) * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode */ -smtc_modem_return_code_t smtc_modem_alarm_start_timer( uint32_t alarm_timer_in_s ); +smtc_modem_return_code_t smtc_modem_alarm_start_timer( uint8_t stack_id, uint32_t alarm_timer_in_s ); /** * @brief Stop and clear the alarm timer @@ -1090,11 +1092,12 @@ smtc_modem_return_code_t smtc_modem_alarm_start_timer( uint32_t alarm_timer_in_s * @retval SMTC_MODEM_RC_NOT_INIT No alarm timer currently running * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode */ -smtc_modem_return_code_t smtc_modem_alarm_clear_timer( void ); +smtc_modem_return_code_t smtc_modem_alarm_clear_timer( uint8_t stack_id ); /** * @brief Get the number of seconds remaining before the alarm triggers an event * + * @param [in] stack_id Stack identifier * @param [out] remaining_time_in_s Number of seconds remaining before the alarm triggers an event * * @return Modem return code as defined in @ref smtc_modem_return_code_t @@ -1103,7 +1106,7 @@ smtc_modem_return_code_t smtc_modem_alarm_clear_timer( void ); * @retval SMTC_MODEM_RC_INVALID \p remaining_time_in_s is NULL * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode */ -smtc_modem_return_code_t smtc_modem_alarm_get_remaining_time( uint32_t* remaining_time_in_s ); +smtc_modem_return_code_t smtc_modem_alarm_get_remaining_time( uint8_t stack_id, uint32_t* remaining_time_in_s ); /* * ----------------------------------------------------------------------------- @@ -1489,6 +1492,7 @@ smtc_modem_return_code_t smtc_modem_csma_get_parameters( uint8_t stack_id, uint8 /** * @brief Get the total charge counter of the modem in mAh * + * @param [in] stack_id Stack identifier * @param [out] charge_mah Accumulated charge in mAh * * @return Modem return code as defined in @ref smtc_modem_return_code_t @@ -1496,25 +1500,28 @@ smtc_modem_return_code_t smtc_modem_csma_get_parameters( uint8_t stack_id, uint8 * @retval SMTC_MODEM_RC_INVALID Parameter \p charge_mah is NULL * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode */ -smtc_modem_return_code_t smtc_modem_get_charge( uint32_t* charge_mah ); +smtc_modem_return_code_t smtc_modem_get_charge( uint8_t stack_id, uint32_t* charge_mah ); /** * @brief Get the Radio Planner statistics in array * + * @param stack_id * @param stats_array * @param stats_array_length * @return smtc_modem_return_code_t */ -smtc_modem_return_code_t smtc_modem_get_rp_stats_to_array( uint8_t* stats_array, uint16_t* stats_array_length ); +smtc_modem_return_code_t smtc_modem_get_rp_stats_to_array( uint8_t stack_id, uint8_t* stats_array, uint16_t* stats_array_length ); /** * @brief Reset the total charge counter of the modem * + * @param [in] stack_id Stack identifier + * * @return Modem return code as defined in @ref smtc_modem_return_code_t * @retval SMTC_MODEM_RC_OK Command executed without errors * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode */ -smtc_modem_return_code_t smtc_modem_reset_charge( void ); +smtc_modem_return_code_t smtc_modem_reset_charge( uint8_t stack_id ); /* * ----------------------------------------------------------------------------- @@ -2279,6 +2286,7 @@ smtc_modem_return_code_t smtc_modem_store_and_forward_flash_get_number_of_free_s /** * @brief Enable or disable duty cycle feature * + * @param [in] stack_id Stack identifier * @param [in] enable Status of the duty cycle feature to set (true: enable, false: disable) * * @return Modem return code as defined in @ref smtc_modem_return_code_t @@ -2286,7 +2294,7 @@ smtc_modem_return_code_t smtc_modem_store_and_forward_flash_get_number_of_free_s * @retval SMTC_MODEM_RC_BUSY Modem is currently in test mode * @retval SMTC_MODEM_RC_FAIL Duty cycle feature is not ready */ -smtc_modem_return_code_t smtc_modem_debug_set_duty_cycle_state( bool enable ); +smtc_modem_return_code_t smtc_modem_debug_set_duty_cycle_state( uint8_t stack_id, bool enable ); /** * @brief join network in ABP diff --git a/lbm_lib/smtc_modem_api/smtc_modem_utilities.h b/lbm_lib/smtc_modem_api/smtc_modem_utilities.h index ed5dc223..f5507147 100644 --- a/lbm_lib/smtc_modem_api/smtc_modem_utilities.h +++ b/lbm_lib/smtc_modem_api/smtc_modem_utilities.h @@ -64,6 +64,17 @@ extern "C" { * --- PUBLIC TYPES ------------------------------------------------------------ */ +/* HW type */ +typedef enum +{ + SMTC_MODEM_RADIO_NONE = 0, + SMTC_MODEM_RADIO_LR11XX, + SMTC_MODEM_RADIO_LR20XX, + SMTC_MODEM_RADIO_SX126X, + SMTC_MODEM_RADIO_SX127X, + SMTC_MODEM_RADIO_SX128X +} smtc_modem_radio_type_t; + /* * ----------------------------------------------------------------------------- * --- PUBLIC FUNCTIONS PROTOTYPES --------------------------------------------- @@ -71,11 +82,18 @@ extern "C" { /** * @brief Init the soft modem and set the modem event chosen callback - * @remark The callback will be called each time an modem event is raised internally + * @remark The callback will be called each time an modem event is raised internally - for all stacks * * @param [in] event_callback User event callback prototype */ -void smtc_modem_init( void ( *event_callback )( void ) ); +void smtc_modem_init_common( void ( *event_callback )( void ) ); + +/** + * @brief Init the soft modem for a given stack and radio + * + * @param [in] stack_id stack identifier + */ +void smtc_modem_init( uint8_t stack_id ); /** * @brief Run the modem engine @@ -89,29 +107,44 @@ uint32_t smtc_modem_run_engine( void ); /** * @brief Check if some modem irq flags are pending * + * @param [in] stack_id stack identifier + * * @return true if some flags are pending, false otherwise */ -bool smtc_modem_is_irq_flag_pending( void ); +bool smtc_modem_is_irq_flag_pending( uint8_t stack_id ); /** * @brief Set optional user radio context that can be retrieved in radio drivers hal calls * + * @param [in] stack_id stack identifier + * @param [in] radio_type type of the radio * @param [in] radio_ctx pointer on context */ -void smtc_modem_set_radio_context( const void* radio_ctx ); +void smtc_modem_set_radio_context( uint8_t stack_id, smtc_modem_radio_type_t radio_type, const void* radio_ctx ); /** * @brief Get optional user radio context * * @returns Radio context reference */ -const void* smtc_modem_get_radio_context( void ); +const void* smtc_modem_get_radio_context( uint8_t stack_id ); + +/** + * @brief Get the radio type for a given stack + * + * @param[in] stack_id + * @return + */ +smtc_modem_radio_type_t smtc_modem_get_radio_type( uint8_t stack_id ); /** * @brief Check if the radio is used by the radio planner + * + * @param [in] stack_id stack identifier + * * @returns true if the radio isn't used by the radio planner, false otherwise */ -bool smtc_modem_radio_is_free(void); +bool smtc_modem_radio_is_free(uint8_t stack_id); #ifdef __cplusplus } #endif diff --git a/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac.c b/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac.c index c758a661..9f0b6617 100644 --- a/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac.c +++ b/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac.c @@ -59,6 +59,8 @@ #include "lr11xx_system.h" #include "lr11xx_gnss.h" +// EvaTODO: Add multistack support + /* * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- @@ -105,7 +107,7 @@ do \ { \ mw_gnss_almanac_task_obj.self_aborted = true; \ - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS_ALMANAC ); \ + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS_ALMANAC ); \ return; \ } while( 0 ) @@ -381,8 +383,8 @@ void mw_gnss_almanac_services_init( uint8_t* service_id, uint8_t task_id, *on_launch_callback = mw_gnss_almanac_service_on_launch; *on_update_callback = mw_gnss_almanac_service_on_update; *context_callback = ( void* ) service_id; - rp_hook_init( modem_get_rp( ), mw_gnss_almanac_task_obj.rp_hook_id, - ( void ( * )( void* ) )( gnss_almanac_rp_task_done ), modem_get_rp( ) ); + rp_hook_init( modem_get_rp( 0 ), mw_gnss_almanac_task_obj.rp_hook_id, + ( void ( * )( void* ) )( gnss_almanac_rp_task_done ), modem_get_rp( 0 ) ); /* Configuration */ mw_gnss_almanac_task_obj.constellations_enabled = LR11XX_GNSS_GPS_MASK | LR11XX_GNSS_BEIDOU_MASK; @@ -437,7 +439,7 @@ void mw_gnss_almanac_remove_task( void ) } /* Remove any pending task in radio planner */ - radio_planner_t* rp = modem_get_rp( ); + radio_planner_t* rp = modem_get_rp( 0 ); if( rp == NULL ) { SMTC_MODEM_HAL_TRACE_ERROR( "mw_gnss_almanac_remove_task: Failed to get RP\n" ); @@ -446,7 +448,7 @@ void mw_gnss_almanac_remove_task( void ) if( rp->tasks[mw_gnss_almanac_task_obj.rp_hook_id].state != RP_TASK_STATE_FINISHED ) { mw_gnss_almanac_task_obj.self_aborted = true; - rp_task_abort( modem_get_rp( ), mw_gnss_almanac_task_obj.rp_hook_id ); + rp_task_abort( modem_get_rp( 0 ), mw_gnss_almanac_task_obj.rp_hook_id ); SMTC_MODEM_HAL_TRACE_PRINTF( "mw_gnss_almanac_remove_task: aborted RP task_id %d\n", mw_gnss_almanac_task_obj.rp_hook_id ); } @@ -539,7 +541,7 @@ static void mw_gnss_almanac_service_on_launch( void* context_callback ) rp_task.type = RP_TASK_TYPE_GNSS_SNIFF; rp_task.launch_task_callbacks = gnss_almanac_rp_task_launch; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to enqueue RP task for GNSS almanac demodulation\n" ); SMTC_MODEM_HAL_PANIC( ); @@ -644,7 +646,7 @@ static void mw_gnss_almanac_next_rp( uint32_t delay_ms ) rp_task.type = RP_TASK_TYPE_GNSS_SNIFF; rp_task.launch_task_callbacks = gnss_almanac_rp_task_launch; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to enqueue RP task for next GNSS almanac demodulation\n" ); SMTC_MODEM_HAL_PANIC( ); @@ -669,7 +671,7 @@ static void gnss_almanac_rp_task_launch( void* context ) mw_gnss_almanac_task_obj.pending_evt_update_done = false; /* WARNING: Read time at least once every 24h to let the LR11xx handle clock roll-over */ - lr11xx_status = lr11xx_gnss_read_time( modem_get_radio_ctx( ), &lr11xx_time ); + lr11xx_status = lr11xx_gnss_read_time( modem_get_radio_ctx( 0 ), &lr11xx_time ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_rp_task_launch: lr11xx_gnss_read_time failed\n" ); @@ -693,7 +695,7 @@ static void gnss_almanac_rp_task_launch( void* context ) if( ( now_s - mw_gnss_almanac_task_obj.last_read_time_s ) > ALMANAC_READ_TIME_THRESHOLD_MAX ) { SMTC_MODEM_HAL_TRACE_WARNING( "Too long since last read_time: reset LR11xx time\n" ); - lr11xx_status = lr11xx_gnss_reset_time( modem_get_radio_ctx( ) ); + lr11xx_status = lr11xx_gnss_reset_time( modem_get_radio_ctx( 0 ) ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_rp_task_launch: lr11xx_gnss_reset_time failed\n" ); @@ -705,14 +707,14 @@ static void gnss_almanac_rp_task_launch( void* context ) } /* Selected constellation(s) and get corresponding almanac status */ - lr11xx_status = lr11xx_gnss_set_constellations_to_use( modem_get_radio_ctx( ), + lr11xx_status = lr11xx_gnss_set_constellations_to_use( modem_get_radio_ctx( 0 ), mw_gnss_almanac_task_obj.constellations_enabled ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_rp_task_launch: lr11xx_gnss_set_constellations_to_use failed\n" ); RP_TASK_ABORT_AND_RETURN( ); } - lr11xx_status = lr11xx_gnss_read_almanac_status( modem_get_radio_ctx( ), &almanac_status ); + lr11xx_status = lr11xx_gnss_read_almanac_status( modem_get_radio_ctx( 0 ), &almanac_status ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_rp_task_launch: lr11xx_gnss_read_almanac_status failed\n" ); @@ -738,7 +740,7 @@ static void gnss_almanac_rp_task_launch( void* context ) else /* launch scan for almanac */ { /* Prepare for scan */ - lr11xx_status = lr11xx_system_set_dio_irq_params( modem_get_radio_ctx( ), LR11XX_SYSTEM_IRQ_GNSS_SCAN_DONE, + lr11xx_status = lr11xx_system_set_dio_irq_params( modem_get_radio_ctx( 0 ), LR11XX_SYSTEM_IRQ_GNSS_SCAN_DONE, LR11XX_SYSTEM_IRQ_NONE ); if( lr11xx_status != LR11XX_STATUS_OK ) { @@ -749,7 +751,7 @@ static void gnss_almanac_rp_task_launch( void* context ) /* Configure the board for almanac scan */ geolocation_bsp_gnss_prescan_actions( ); - if( mw_radio_configure_for_scan( modem_get_radio_ctx( ) ) == false ) + if( mw_radio_configure_for_scan( modem_get_radio_ctx( 0 ) ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_rp_task_launch: mw_radio_configure_for_scan() failed\n" ); RP_TASK_ABORT_AND_RETURN( ); @@ -758,7 +760,7 @@ static void gnss_almanac_rp_task_launch( void* context ) SMTC_MODEM_HAL_TRACE_INFO( "=> launch almanac update from sat for %s\n", smtc_gnss_constellation_enum2str( mw_gnss_almanac_next_update.constellation ) ); lr11xx_status = lr11xx_gnss_almanac_update_from_sat( - modem_get_radio_ctx( ), mw_gnss_almanac_next_update.constellation, LR11XX_GNSS_OPTION_LOW_EFFORT ); + modem_get_radio_ctx( 0 ), mw_gnss_almanac_next_update.constellation, LR11XX_GNSS_OPTION_LOW_EFFORT ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_rp_task_launch: lr11xx_gnss_almanac_update_from_sat() failed\n" ); @@ -820,7 +822,7 @@ static void gnss_almanac_rp_task_done( void* status ) SMTC_MODEM_HAL_TRACE_PRINTF( "ALMANAC: RP_STATUS_GNSS_SCAN_DONE\n" ); mw_status = gnss_almanac_task_done( ); - mw_radio_set_sleep( modem_get_radio_ctx( ) ); /* Set the radio back to sleep when all radio accesses are done */ + mw_radio_set_sleep( modem_get_radio_ctx( 0 ) ); /* Set the radio back to sleep when all radio accesses are done */ if( mw_status != MW_RC_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_almanac_task_done Failed\n" ); @@ -855,7 +857,7 @@ static mw_return_code_t gnss_almanac_task_done( void ) lr11xx_gnss_solver_assistance_position_t aiding_pos; /* Get power consumption of last almanac demod */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_cumulative_timing( modem_get_radio_ctx( ), &cumulative_timing ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_cumulative_timing( modem_get_radio_ctx( 0 ), &cumulative_timing ) == LR11XX_STATUS_OK ); GNSS_ALMANAC_TRACE_PRINTF_DEBUG( "Cumulative timings: %u s\n", cumulative_timing.total / 32768 ); /* Update statistics */ @@ -869,7 +871,7 @@ static mw_return_code_t gnss_almanac_task_done( void ) GNSS_ALMANAC_TRACE_PRINTF_DEBUG( "Almanac: power consumption: %u nah\n", power_consumption_nah ); /* Get almanac demodulation status */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_demod_status( modem_get_radio_ctx( ), &demod_status, &demod_info ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_demod_status( modem_get_radio_ctx( 0 ), &demod_status, &demod_info ) == LR11XX_STATUS_OK ); if( demod_status < 0 ) { @@ -885,11 +887,11 @@ static mw_return_code_t gnss_almanac_task_done( void ) } /* Get detected SVs (for info) */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_get_nb_detected_satellites( modem_get_radio_ctx( ), &nb_detected_svs ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_get_nb_detected_satellites( modem_get_radio_ctx( 0 ), &nb_detected_svs ) == LR11XX_STATUS_OK ); if( nb_detected_svs > 0 ) { - MW_RETURN_ON_FAILURE( lr11xx_gnss_get_detected_satellites( modem_get_radio_ctx( ), nb_detected_svs, + MW_RETURN_ON_FAILURE( lr11xx_gnss_get_detected_satellites( modem_get_radio_ctx( 0 ), nb_detected_svs, info_svs ) == LR11XX_STATUS_OK ); } GNSS_ALMANAC_TRACE_PRINTF_DEBUG( "ALMANAC SCAN DONE: %u SV detected:\n", nb_detected_svs ); @@ -899,7 +901,7 @@ static mw_return_code_t gnss_almanac_task_done( void ) } /* Check if an aiding position could be computed by the doppler solver (indicates level of almanac update) */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_doppler_solver_result( modem_get_radio_ctx( ), &doppler_solver_results ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_doppler_solver_result( modem_get_radio_ctx( 0 ), &doppler_solver_results ) == LR11XX_STATUS_OK ); GNSS_ALMANAC_TRACE_PRINTF_DEBUG( "Doppler solver result after almanac demod: %s\n", @@ -907,18 +909,18 @@ static mw_return_code_t gnss_almanac_task_done( void ) mw_gnss_almanac_update_status.last_doppler_solver_status = doppler_solver_results.error_code; /* Get current assistance position (could have been reset above) */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_assistance_position( modem_get_radio_ctx( ), &aiding_pos ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_assistance_position( modem_get_radio_ctx( 0 ), &aiding_pos ) == LR11XX_STATUS_OK ); smtc_gnss_trace_print_position( "Almanac: Assistance Position: ", &aiding_pos ); /* Select the constellations enabled for almanac demod */ /* lr11xx_gnss_read_almanac_status() will ignore constellation not selected */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_set_constellations_to_use( modem_get_radio_ctx( ), + MW_RETURN_ON_FAILURE( lr11xx_gnss_set_constellations_to_use( modem_get_radio_ctx( 0 ), mw_gnss_almanac_task_obj.constellations_enabled ) == LR11XX_STATUS_OK ); /* Get almanac status for selected constellation(s) */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_almanac_status( modem_get_radio_ctx( ), &almanac_status ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_almanac_status( modem_get_radio_ctx( 0 ), &almanac_status ) == LR11XX_STATUS_OK ); print_almanac_status( &almanac_status ); diff --git a/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac_full_update.c b/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac_full_update.c index c3eb7d3c..e8c7567a 100644 --- a/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac_full_update.c +++ b/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_almanac_full_update.c @@ -102,7 +102,8 @@ smtc_modem_return_code_t mw_gnss_almanac_full_update( const uint8_t* almanac, ui lr11xx_status_t err; for( int i = 0; i < almanac_size; i += LR11XX_GNSS_SINGLE_ALMANAC_WRITE_SIZE ) { - err = lr11xx_gnss_almanac_update( modem_get_radio_ctx( ), almanac + i, 1 ); + //EvaTODO update with stack_id + err = lr11xx_gnss_almanac_update( modem_get_radio_ctx( 0 ), almanac + i, 1 ); if( err != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "mw_gnss_almanac_full_update: failed at block %d\n", i ); @@ -142,7 +143,8 @@ static uint32_t get_almanac_crc( void ) lr11xx_gnss_context_status_bytestream_t context_status_bytestream; lr11xx_gnss_context_status_t context_status; - err = lr11xx_gnss_get_context_status( modem_get_radio_ctx( ), context_status_bytestream ); + // EvaTODO update with stack_id + err = lr11xx_gnss_get_context_status( modem_get_radio_ctx( 0 ), context_status_bytestream ); if( err != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to get gnss context status\n" ); @@ -159,4 +161,4 @@ static uint32_t get_almanac_crc( void ) return context_status.global_almanac_crc; } -/* --- EOF ------------------------------------------------------------------ */ \ No newline at end of file +/* --- EOF ------------------------------------------------------------------ */ diff --git a/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_scan.c b/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_scan.c index f09975eb..dc26bb8a 100644 --- a/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_scan.c +++ b/lbm_lib/smtc_modem_core/geolocation_services/mw_gnss_scan.c @@ -60,6 +60,8 @@ #include "lr11xx_system.h" #include "lr11xx_gnss.h" +// EvaTODO: Add multistack support + /* * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- @@ -271,8 +273,8 @@ void mw_gnss_scan_services_init( uint8_t* service_id, uint8_t task_id, *on_launch_callback = mw_gnss_scan_service_on_launch; *on_update_callback = mw_gnss_scan_service_on_update; *context_callback = ( void* ) service_id; - rp_hook_init( modem_get_rp( ), mw_gnss_task_obj.rp_hook_id, ( void ( * )( void* ) )( gnss_rp_task_done ), - modem_get_rp( ) ); + rp_hook_init( modem_get_rp( 0 ), mw_gnss_task_obj.rp_hook_id, ( void ( * )( void* ) )( gnss_rp_task_done ), + modem_get_rp( 0 ) ); /* Configuration */ mw_gnss_task_obj.constellations_mask = LR11XX_GNSS_GPS_MASK | LR11XX_GNSS_BEIDOU_MASK; @@ -456,7 +458,7 @@ static void mw_gnss_scan_service_on_launch( void* context_callback ) rp_task.type = RP_TASK_TYPE_GNSS_SNIFF; rp_task.launch_task_callbacks = gnss_rp_task_launch; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to enqueue RP task for GNSS scan\n" ); SMTC_MODEM_HAL_PANIC( ); @@ -500,7 +502,7 @@ static void gnss_scan_next( uint32_t delay_s ) rp_task.type = RP_TASK_TYPE_GNSS_SNIFF; rp_task.launch_task_callbacks = gnss_rp_task_launch; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to enqueue RP task for GNSS scan\n" ); SMTC_MODEM_HAL_PANIC( ); @@ -633,23 +635,23 @@ static void gnss_rp_task_launch( void* context ) } /* Prepare for scan */ - lr11xx_status = lr11xx_system_set_dio_irq_params( modem_get_radio_ctx( ), LR11XX_SYSTEM_IRQ_GNSS_SCAN_DONE, + lr11xx_status = lr11xx_system_set_dio_irq_params( modem_get_radio_ctx( 0 ), LR11XX_SYSTEM_IRQ_GNSS_SCAN_DONE, LR11XX_SYSTEM_IRQ_NONE ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_rp_task_launch: Failed to set irq params\n" ); mw_gnss_task_obj.self_aborted = true; /* Stop the current NAV group */ - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); return; } lr11xx_status = - lr11xx_gnss_set_constellations_to_use( modem_get_radio_ctx( ), mw_gnss_task_obj.constellations_mask ); + lr11xx_gnss_set_constellations_to_use( modem_get_radio_ctx( 0 ), mw_gnss_task_obj.constellations_mask ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_rp_task_launch: Failed to set constellations\n" ); mw_gnss_task_obj.self_aborted = true; /* Stop the current NAV group */ - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); return; } @@ -671,29 +673,29 @@ static void gnss_rp_task_launch( void* context ) GNSS_SCAN_TRACE_PRINTF_DEBUG( " - elapsed time since end of previous valid scan: %u ms\n", time_since_end_of_previous_scan_ms ); mw_gnss_task_obj.self_aborted = true; /* Stop the current NAV group */ - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); return; } } - if( mw_radio_configure_for_scan( modem_get_radio_ctx( ) ) == false ) + if( mw_radio_configure_for_scan( modem_get_radio_ctx( 0 ) ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "gnss_rp_task_launch: mw_radio_configure_for_scan() failed\n" ); mw_gnss_task_obj.self_aborted = true; /* Stop the current NAV group */ - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); return; } /* Start scan */ uint8_t scan_parameters = LR11XX_GNSS_RESULTS_DOPPLER_ENABLE_MASK | LR11XX_GNSS_RESULTS_DOPPLER_MASK | LR11XX_GNSS_RESULTS_DEMODULATE_TIME_MASK; - lr11xx_status = lr11xx_gnss_scan( modem_get_radio_ctx( ), LR11XX_GNSS_OPTION_MID_EFFORT, scan_parameters, + lr11xx_status = lr11xx_gnss_scan( modem_get_radio_ctx( 0 ), LR11XX_GNSS_OPTION_MID_EFFORT, scan_parameters, GNSS_RESULT_NB_SVS_MAX ); if( lr11xx_status != LR11XX_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "lr11xx_gnss_scan() failed\n" ); mw_gnss_task_obj.self_aborted = true; /* Stop the current NAV group */ - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_GNSS ); return; } } @@ -745,7 +747,7 @@ static void gnss_rp_task_done( void* status ) { bool scan_done = false; mw_return_code_t mw_status = gnss_scan_task_done( &scan_done ); - mw_radio_set_sleep( modem_get_radio_ctx( ) ); /* Set the radio back to sleep when all radio accesses are done */ + mw_radio_set_sleep( modem_get_radio_ctx( 0 ) ); /* Set the radio back to sleep when all radio accesses are done */ if( ( mw_status == MW_RC_OK ) && ( scan_done == false ) ) { gnss_scan_next( modes[mw_gnss_task_obj.current_mode_index].scan_group_delay ); @@ -790,7 +792,7 @@ static mw_return_code_t gnss_scan_task_done( bool* navgroup_complete ) uint8_t scan_index = navgroup.nb_scans_valid; /* Get current gps time (nb of seconds modulo 1024 weeks) */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_time( modem_get_radio_ctx( ), &gps_time ) == LR11XX_STATUS_OK ); + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_time( modem_get_radio_ctx( 0 ), &gps_time ) == LR11XX_STATUS_OK ); if( gps_time.error_code == LR11XX_GNSS_READ_TIME_STATUS_NO_ERROR ) { GNSS_SCAN_TRACE_PRINTF_DEBUG( "GPS time: %u (0x%08X)\n", gps_time.gps_time_s, gps_time.gps_time_s ); @@ -809,22 +811,22 @@ static mw_return_code_t gnss_scan_task_done( bool* navgroup_complete ) /* Get scan mode used for this scan (autonomous, assisted... ) */ MW_RETURN_ON_FAILURE( lr11xx_gnss_read_last_scan_mode_launched( - modem_get_radio_ctx( ), &( mw_gnss_task_obj.last_scan_mode ) ) == LR11XX_STATUS_OK ); + modem_get_radio_ctx( 0 ), &( mw_gnss_task_obj.last_scan_mode ) ) == LR11XX_STATUS_OK ); GNSS_SCAN_TRACE_PRINTF_DEBUG( "Last GNSS scan mode launched: %s\n", smtc_gnss_scan_mode_launched_enum2str( mw_gnss_task_obj.last_scan_mode ) ); navgroup.scans[scan_index].scan_mode_launched = mw_gnss_task_obj.last_scan_mode; /* Get current almanac CRC */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_get_context_status( modem_get_radio_ctx( ), context_status_bytestream ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_get_context_status( modem_get_radio_ctx( 0 ), context_status_bytestream ) == LR11XX_STATUS_OK ); MW_RETURN_ON_FAILURE( lr11xx_gnss_parse_context_status_buffer( context_status_bytestream, &context_status ) == LR11XX_STATUS_OK ); mw_gnss_task_obj.current_almanac_crc = context_status.global_almanac_crc; /* Get results */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_get_result_size( modem_get_radio_ctx( ), &scan_result_size ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_get_result_size( modem_get_radio_ctx( 0 ), &scan_result_size ) == LR11XX_STATUS_OK ); - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_results( modem_get_radio_ctx( ), navgroup.scans[scan_index].results_buffer, + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_results( modem_get_radio_ctx( 0 ), navgroup.scans[scan_index].results_buffer, scan_result_size ) == LR11XX_STATUS_OK ); GNSS_SCAN_TRACE_ARRAY_DEBUG( "NAV3", navgroup.scans[scan_index].results_buffer, scan_result_size ); @@ -833,14 +835,14 @@ static mw_return_code_t gnss_scan_task_done( bool* navgroup_complete ) /* Fetch the detected SVs */ MW_RETURN_ON_FAILURE( - lr11xx_gnss_get_nb_detected_satellites( modem_get_radio_ctx( ), &navgroup.scans[scan_index].nb_detected_svs ) == + lr11xx_gnss_get_nb_detected_satellites( modem_get_radio_ctx( 0 ), &navgroup.scans[scan_index].nb_detected_svs ) == LR11XX_STATUS_OK ); /* Get details about all detected SVs (even if not given in NAV) */ if( navgroup.scans[scan_index].nb_detected_svs > 0 ) { MW_RETURN_ON_FAILURE( - lr11xx_gnss_get_detected_satellites( modem_get_radio_ctx( ), navgroup.scans[scan_index].nb_detected_svs, + lr11xx_gnss_get_detected_satellites( modem_get_radio_ctx( 0 ), navgroup.scans[scan_index].nb_detected_svs, navgroup.scans[scan_index].info_svs ) == LR11XX_STATUS_OK ); } @@ -859,7 +861,7 @@ static mw_return_code_t gnss_scan_task_done( bool* navgroup_complete ) SMTC_MODEM_HAL_TRACE_WARNING( "GNSS: ASSISTANCE_POSITION_POSSIBLY_WRONG_BUT_FAILS_TO_UPDATE\n" ); SMTC_MODEM_HAL_TRACE_INFO( "Reset current position\n" ); /* Reset current position and let LR11xx restart with autonomous scan to recover */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_reset_position( modem_get_radio_ctx( ) ) == LR11XX_STATUS_OK ); + MW_RETURN_ON_FAILURE( lr11xx_gnss_reset_position( modem_get_radio_ctx( 0 ) ) == LR11XX_STATUS_OK ); break; default: SMTC_MODEM_HAL_TRACE_INFO( "GNSS message to host: %s\n", @@ -869,13 +871,13 @@ static mw_return_code_t gnss_scan_task_done( bool* navgroup_complete ) } /* Get current assistance position (could have been reset above) */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_assistance_position( modem_get_radio_ctx( ), &aiding_pos ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_assistance_position( modem_get_radio_ctx( 0 ), &aiding_pos ) == LR11XX_STATUS_OK ); smtc_gnss_trace_print_position( "Assistance Position: ", &aiding_pos ); navgroup.scans[scan_index].aiding_position = aiding_pos; /* Get Power consumption */ - MW_RETURN_ON_FAILURE( lr11xx_gnss_read_cumulative_timing( modem_get_radio_ctx( ), &cumulative_timing ) == + MW_RETURN_ON_FAILURE( lr11xx_gnss_read_cumulative_timing( modem_get_radio_ctx( 0 ), &cumulative_timing ) == LR11XX_STATUS_OK ); geolocation_bsp_gnss_get_consumption( &instantaneous_power_consumption_ua ); lr11xx_gnss_compute_power_consumption( &cumulative_timing, &instantaneous_power_consumption_ua, diff --git a/lbm_lib/smtc_modem_core/geolocation_services/mw_wifi_scan.c b/lbm_lib/smtc_modem_core/geolocation_services/mw_wifi_scan.c index 923a63d6..f7d0773d 100644 --- a/lbm_lib/smtc_modem_core/geolocation_services/mw_wifi_scan.c +++ b/lbm_lib/smtc_modem_core/geolocation_services/mw_wifi_scan.c @@ -57,6 +57,8 @@ #include "radio_planner.h" #include "radio_planner_hook_id_defs.h" +// EvaTODO: Add multistack support + /* * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- @@ -235,8 +237,8 @@ void mw_wifi_scan_services_init( uint8_t* service_id, uint8_t task_id, *on_launch_callback = mw_wifi_scan_service_on_launch; *on_update_callback = mw_wifi_scan_service_on_update; *context_callback = ( void* ) service_id; - rp_hook_init( modem_get_rp( ), mw_wifi_task_obj.rp_hook_id, ( void ( * )( void* ) )( wifi_rp_task_done ), - modem_get_rp( ) ); + rp_hook_init( modem_get_rp( 0 ), mw_wifi_task_obj.rp_hook_id, ( void ( * )( void* ) )( wifi_rp_task_done ), + modem_get_rp( 0 ) ); /* Configuration */ mw_wifi_task_obj.current_mw_scan_mode = SMTC_MODEM_WIFI_SCAN_MODE_MAC; @@ -339,7 +341,7 @@ static void mw_wifi_scan_service_on_launch( void* context_callback ) rp_task.type = RP_TASK_TYPE_WIFI_SNIFF; rp_task.launch_task_callbacks = wifi_rp_task_launch; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to enqueue RP task for Wi-Fi scan\n" ); SMTC_MODEM_HAL_PANIC( ); @@ -500,10 +502,10 @@ static void wifi_rp_task_launch( void* context ) mw_wifi_task_obj.scan_start_time = smtc_modem_hal_get_time_in_ms( ); SMTC_MODEM_HAL_TRACE_INFO( "Wi-Fi task launch at %u\n", mw_wifi_task_obj.scan_start_time ); - if( mw_radio_configure_for_scan( modem_get_radio_ctx( ) ) == false ) + if( mw_radio_configure_for_scan( modem_get_radio_ctx( 0 ) ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "wifi_rp_task_launch: mw_radio_configure_for_scan() failed\n" ); - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_WIFI ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_WIFI ); return; } @@ -512,7 +514,7 @@ static void wifi_rp_task_launch( void* context ) if( wifi_settings_idx == -1 ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to select Wi-Fi scan settings for requested scan mode\n" ); - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_WIFI ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_WIFI ); return; } @@ -520,10 +522,10 @@ static void wifi_rp_task_launch( void* context ) smtc_wifi_settings_init( &wifi_settings[wifi_settings_idx] ); /* Start Wi-Fi scan */ - if( smtc_wifi_start_scan( modem_get_radio_ctx( ) ) != MW_RC_OK ) + if( smtc_wifi_start_scan( modem_get_radio_ctx( 0 ) ) != MW_RC_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "RP_TASK_WIFI - failed to start Wi-Fi scan, abort task\n" ); - rp_task_abort( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS_WIFI ); + rp_task_abort( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS_WIFI ); /* When aborting the task, the RP will call the end_task_callback() with SMTC_RP_RADIO_ABORTED status. */ return; } @@ -565,7 +567,7 @@ static void wifi_rp_task_done( void* status ) wifi_scan_task_done( ); /* Set the radio back to sleep when all radio accesses are done */ - mw_radio_set_sleep( modem_get_radio_ctx( ) ); + mw_radio_set_sleep( modem_get_radio_ctx( 0 ) ); /* Notify application */ send_event( SMTC_MODEM_EVENT_WIFI_SCAN_DONE ); @@ -587,10 +589,10 @@ static void wifi_rp_task_done( void* status ) static void wifi_scan_task_done( void ) { /* Wi-Fi scan completed, get and display the results */ - smtc_wifi_get_results( modem_get_radio_ctx( ), &wifi_results ); + smtc_wifi_get_results( modem_get_radio_ctx( 0 ), &wifi_results ); /* Get scan power consumption */ - smtc_wifi_get_power_consumption( modem_get_radio_ctx( ), &wifi_results.power_consumption_nah ); + smtc_wifi_get_power_consumption( modem_get_radio_ctx( 0 ), &wifi_results.power_consumption_nah ); /* Sort results */ smtc_wifi_sort_results_by_rssi( &wifi_results ); diff --git a/lbm_lib/smtc_modem_core/lorawan_api/lorawan_api.c b/lbm_lib/smtc_modem_core/lorawan_api/lorawan_api.c index a52fd797..b774d4bb 100644 --- a/lbm_lib/smtc_modem_core/lorawan_api/lorawan_api.c +++ b/lbm_lib/smtc_modem_core/lorawan_api/lorawan_api.c @@ -156,7 +156,7 @@ status_lorawan_t lorawan_api_set_region( smtc_real_region_types_t region_type, u // Configure duty-cycle object for( int i = 0; i < BAND_EU868_MAX; i++ ) { - smtc_duty_cycle_config( BAND_EU868_MAX, i, duty_cycle_by_band_eu_868[i], + smtc_duty_cycle_config( stack_id, BAND_EU868_MAX, i, duty_cycle_by_band_eu_868[i], frequency_range_by_band_eu_868[i][0], frequency_range_by_band_eu_868[i][1] ); } } @@ -167,7 +167,7 @@ status_lorawan_t lorawan_api_set_region( smtc_real_region_types_t region_type, u // Configure duty-cycle object for( int i = 0; i < BAND_RU864_MAX; i++ ) { - smtc_duty_cycle_config( BAND_RU864_MAX, i, duty_cycle_by_band_ru_864[i], + smtc_duty_cycle_config( stack_id, BAND_RU864_MAX, i, duty_cycle_by_band_ru_864[i], frequency_range_by_band_ru_864[i][0], frequency_range_by_band_ru_864[i][1] ); } } @@ -177,15 +177,11 @@ status_lorawan_t lorawan_api_set_region( smtc_real_region_types_t region_type, u // Remark: In multi-stack EU868 and IN865 there is a bands overlapping, EU868 has a duty-cycle but not IN865, in // this case the duty cycle will be enabled smtc_dtc_enablement_type_t dtc_supported = SMTC_DTC_FULL_DISABLED; - for( uint8_t i = 0; i < NUMBER_OF_STACKS; i++ ) + if( smtc_real_is_dtc_supported( lr1_mac_obj[stack_id].real ) == true ) { - if( smtc_real_is_dtc_supported( lr1_mac_obj[i].real ) == true ) - { - dtc_supported = SMTC_DTC_ENABLED; - break; - } + dtc_supported = SMTC_DTC_ENABLED; } - smtc_duty_cycle_enable_set( dtc_supported ); + smtc_duty_cycle_enable_set( stack_id, dtc_supported ); return ret; } diff --git a/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_join_management.c b/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_join_management.c index 716dbb40..16120be9 100644 --- a/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_join_management.c +++ b/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_join_management.c @@ -200,7 +200,7 @@ static void lorawan_join_management_service_on_update( void* context ) } // In any case store the modem context here to avoid extrem access to nvm near end of battery life - modem_store_modem_context( ); + modem_store_modem_context( STACK_ID_CURRENT_TASK ); } static uint8_t lorawan_join_management_service_downlink_handler( lr1_stack_mac_down_data_t* rx_down_data ) diff --git a/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_send_management.c b/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_send_management.c index d5396619..10934381 100644 --- a/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_send_management.c +++ b/lbm_lib/smtc_modem_core/lorawan_manager/lorawan_send_management.c @@ -151,7 +151,7 @@ void lorawan_send_add_task( uint8_t stack_id, uint8_t f_port, bool send_fport, b if( emergency == true ) { task_send.priority = TASK_HIGH_PRIORITY; - smtc_duty_cycle_enable_set( SMTC_DTC_PARTIAL_DISABLED ); + smtc_duty_cycle_enable_set( stack_id, SMTC_DTC_PARTIAL_DISABLED ); } else { @@ -223,7 +223,7 @@ static void lorawan_send_management_service_on_update( void* context ) stask_manager* task_manager = ( stask_manager* ) context; if( task_manager->modem_task[VIRTUAL_TASK_ID].priority == TASK_HIGH_PRIORITY ) { - smtc_duty_cycle_enable_set( SMTC_DTC_ENABLED ); + smtc_duty_cycle_enable_set( STACK_ID_CURRENT_TASK, SMTC_DTC_ENABLED ); } if( task_manager->modem_task[VIRTUAL_TASK_ID].task_enabled == true ) diff --git a/lbm_lib/smtc_modem_core/lorawan_packages/fragmented_data_block_transport/v2.0.0/lorawan_fragmentation_package_v2.0.0.c b/lbm_lib/smtc_modem_core/lorawan_packages/fragmented_data_block_transport/v2.0.0/lorawan_fragmentation_package_v2.0.0.c index b5bd57d9..279c680a 100644 --- a/lbm_lib/smtc_modem_core/lorawan_packages/fragmented_data_block_transport/v2.0.0/lorawan_fragmentation_package_v2.0.0.c +++ b/lbm_lib/smtc_modem_core/lorawan_packages/fragmented_data_block_transport/v2.0.0/lorawan_fragmentation_package_v2.0.0.c @@ -977,9 +977,16 @@ static uint8_t compute_data_block_integrity_ckeck( uint8_t frag_index, uint8_t s b0[0] = 0x30; #if defined( USE_LR11XX_CE ) - if( smtc_modem_get_data_block_int_key( stack_id, key ) != SMTC_MODEM_RC_OK ) - { - return 1; + if(smtc_modem_get_radio_type( stack_id ) == SMTC_MODEM_RADIO_LR11XX ){ + if( smtc_modem_get_data_block_int_key( stack_id, key ) != SMTC_MODEM_RC_OK ) + { + return 1; + } + } else { + if( smtc_secure_element_aes_encrypt( b0, 16, SMTC_SE_APP_KEY, key, stack_id ) != SMTC_SE_RC_SUCCESS ) + { + return 1; + } } #else if( smtc_secure_element_aes_encrypt( b0, 16, SMTC_SE_APP_KEY, key, stack_id ) != SMTC_SE_RC_SUCCESS ) diff --git a/lbm_lib/smtc_modem_core/lorawan_packages/lorawan_certification/lorawan_certification.c b/lbm_lib/smtc_modem_core/lorawan_packages/lorawan_certification/lorawan_certification.c index 04271aa1..e41b094a 100644 --- a/lbm_lib/smtc_modem_core/lorawan_packages/lorawan_certification/lorawan_certification.c +++ b/lbm_lib/smtc_modem_core/lorawan_packages/lorawan_certification/lorawan_certification.c @@ -63,7 +63,7 @@ * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- */ -#define NUMBER_OF_CERTIF_OBJ 1 +#define NUMBER_OF_CERTIF_OBJ NUMBER_OF_STACKS /*! * \brief Returns the minimum value between a and b @@ -585,8 +585,9 @@ static lorawan_certification_requested_tx_type_t lorawan_certification_parser( if( rx_buffer_length == LORAWAN_CERTIFICATION_DUT_JOIN_REQ_SIZE ) { // leave network - lorawan_api_join_status_clear( lorawan_certification_obj->stack_id ); - lorawan_api_set_activation_mode( ACTIVATION_MODE_OTAA, lorawan_certification_obj->stack_id ); + // EvaTODO: I assumed there was an error in the original code where 'lorawan_certification' was used instead of 'lorawan_certification_obj' - see changes with latest release. + lorawan_api_join_status_clear( lorawan_certification->stack_id ); + lorawan_api_set_activation_mode( ACTIVATION_MODE_OTAA, lorawan_certification->stack_id ); ret = LORAWAN_CERTIFICATION_JOIN_REQ; } else @@ -622,12 +623,12 @@ static lorawan_certification_requested_tx_type_t lorawan_certification_parser( { uint8_t adr_custom_data[16] = { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 }; - smtc_modem_adr_set_profile( lorawan_certification_obj->stack_id, SMTC_MODEM_ADR_PROFILE_CUSTOM, + smtc_modem_adr_set_profile( lorawan_certification->stack_id, SMTC_MODEM_ADR_PROFILE_CUSTOM, adr_custom_data ); } else { - lorawan_api_dr_strategy_set( STATIC_ADR_MODE, lorawan_certification_obj->stack_id ); + lorawan_api_dr_strategy_set( STATIC_ADR_MODE, lorawan_certification->stack_id ); } } else @@ -642,11 +643,11 @@ static lorawan_certification_requested_tx_type_t lorawan_certification_parser( { if( rx_buffer[1] == ( uint8_t ) LORAWAN_CERTIFICATION_DUTY_CYCLE_OFF ) { - smtc_duty_cycle_enable_set( SMTC_DTC_FULL_DISABLED ); + smtc_duty_cycle_enable_set(lorawan_certification->stack_id, SMTC_DTC_FULL_DISABLED ); } else { - smtc_duty_cycle_enable_set( SMTC_DTC_ENABLED ); + smtc_duty_cycle_enable_set(lorawan_certification->stack_id, SMTC_DTC_ENABLED ); } } else @@ -853,7 +854,7 @@ static lorawan_certification_requested_tx_type_t lorawan_certification_parser( { if( rx_buffer[1] == ( uint8_t ) LORAWAN_CERTIFICATION_RELAY_TX_OFF ) { - smtc_modem_relay_tx_disable( lorawan_certification_obj->stack_id ); + smtc_modem_relay_tx_disable( lorawan_certification->stack_id ); } else { @@ -864,7 +865,7 @@ static lorawan_certification_requested_tx_type_t lorawan_certification_parser( user_relay_config.smart_level = 5; user_relay_config.backoff = 4; - smtc_modem_relay_tx_enable( lorawan_certification_obj->stack_id, &user_relay_config ); + smtc_modem_relay_tx_enable( lorawan_certification->stack_id, &user_relay_config ); } } else @@ -899,7 +900,7 @@ static lorawan_certification_requested_tx_type_t lorawan_certification_parser( lorawan_certification->cw_tx_power = rx_buffer[6]; // Leave network - lorawan_api_join_status_clear( lorawan_certification_obj->stack_id ); + lorawan_api_join_status_clear( lorawan_certification->stack_id ); // Start Test mode + tx continuous wave smtc_modem_test_start( ); diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/lr1_stack_mac_layer.c b/lbm_lib/smtc_modem_core/lr1mac/src/lr1_stack_mac_layer.c index 9f217e84..8945d3a8 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/lr1_stack_mac_layer.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/lr1_stack_mac_layer.c @@ -187,13 +187,13 @@ void lr1_stack_mac_region_config( lr1_stack_mac_t* lr1_mac ) #if defined( STORE_JOIN_SESSION ) // If we are keeping join session, use the stored values if(lr1_mac->join_status != JOINED) - { + { lr1_mac->rx2_frequency = real_const.const_rx2_freq; lr1_mac->rx1_dr_offset = 0; lr1_mac->rx2_data_rate = real_const.const_rx2_dr_init; lr1_mac->rx1_delay_s = real_const.const_received_delay1; } -#else +#else lr1_mac->rx2_frequency = real_const.const_rx2_freq; lr1_mac->rx1_dr_offset = 0; lr1_mac->rx2_data_rate = real_const.const_rx2_dr_init; @@ -282,7 +282,7 @@ void lr1_stack_mac_tx_lora_launch_callback_for_rp( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( true ); + smtc_modem_hal_set_ant_switch( rp->stack_id,true ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_tx( &( rp->radio->ral ) ) == RAL_STATUS_OK ); rp_stats_set_tx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); } @@ -302,7 +302,7 @@ void lr1_stack_mac_tx_gfsk_launch_callback_for_rp( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( true ); + smtc_modem_hal_set_ant_switch( rp->stack_id,true ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_tx( &( rp->radio->ral ) ) == RAL_STATUS_OK ); rp_stats_set_tx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); } @@ -332,7 +332,7 @@ void lr1_stack_mac_tx_lr_fhss_launch_callback_for_rp( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( true ); + smtc_modem_hal_set_ant_switch( rp->stack_id,true ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_tx( &( rp->radio->ral ) ) == RAL_STATUS_OK ); rp_stats_set_tx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); } @@ -352,7 +352,7 @@ void lr1_stack_mac_rx_lora_launch_callback_for_rp( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id,false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_rx( &( rp->radio->ral ), rp->radio_params[id].rx.timeout_in_ms ) == RAL_STATUS_OK ); rp_stats_set_rx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); @@ -373,7 +373,7 @@ void lr1_stack_mac_rx_gfsk_launch_callback_for_rp( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id, false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_rx( &( rp->radio->ral ), rp->radio_params[id].rx.timeout_in_ms ) == RAL_STATUS_OK ); rp_stats_set_rx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); @@ -515,7 +515,7 @@ void lr1_stack_mac_tx_radio_start( lr1_stack_mac_t* lr1_mac ) rp_task.start_time_ms = lr1_mac->rtc_target_timer_ms; if( lr1_mac->send_at_time == true ) { - rp_task.start_time_ms -= smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + rp_task.start_time_ms -= smtc_modem_hal_get_radio_tcxo_startup_delay_ms( lr1_mac->stack_id ); lr1_mac->send_at_time = false; // reinit the flag rp_task.state = RP_TASK_STATE_SCHEDULE; } @@ -751,8 +751,8 @@ void lr1_stack_mac_rp_callback( lr1_stack_mac_t* lr1_mac ) ( lr1_mac->isr_tx_done_radio_timestamp + rx_delay_ms + lr1_mac->rx_timeout_symb_in_ms + lr1_mac->rx_offset_ms ) - lr1_mac->fine_tune_board_setting_delay_ms[lr1_mac->rx_data_rate] - - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) - - smtc_modem_hal_get_board_delay_ms( ); + smtc_modem_hal_get_radio_tcxo_startup_delay_ms( lr1_mac->stack_id ) - + smtc_modem_hal_get_board_delay_ms( lr1_mac->stack_id ); SMTC_MODEM_HAL_TRACE_PRINTF_DEBUG( "DR%u Fine tune correction (ms) = %d, error fine tune (ms) = %d, lr1_mac->rx_offset_ms = %d\n", @@ -864,8 +864,8 @@ bool lr1_stack_mac_rx_timer_configure( lr1_stack_mac_t* lr1_mac, const rx_win_ty SMTC_MODEM_HAL_PANIC( "MODULATION NOT SUPPORTED\n" ); } - uint32_t board_delay_ms = smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) + - +smtc_modem_hal_get_board_delay_ms( ) + + uint32_t board_delay_ms = smtc_modem_hal_get_radio_tcxo_startup_delay_ms( lr1_mac->stack_id ) + + +smtc_modem_hal_get_board_delay_ms( lr1_mac->stack_id ) + lr1_mac->fine_tune_board_setting_delay_ms[lr1_mac->rx_data_rate]; #if defined( ADD_RELAY_TX ) diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_beacon_sniff.c b/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_beacon_sniff.c index 4a47e01b..8b5b9c06 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_beacon_sniff.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_beacon_sniff.c @@ -399,7 +399,7 @@ void smtc_beacon_sniff_launch_callback_for_rp( void* rp_void ) { } smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id,false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_rx( &( rp->radio->ral ), rp->radio_params[id].rx.timeout_in_ms ) == RAL_STATUS_OK ); rp_stats_set_rx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); @@ -770,7 +770,7 @@ static void update_beacon_rx_nb_symb( smtc_lr1_beacon_t* lr1_beacon_obj, uint32_ } static uint32_t compute_start_time( smtc_lr1_beacon_t* lr1_beacon_obj ) { - int8_t board_delay_ms = smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) + smtc_modem_hal_get_board_delay_ms( ); + int8_t board_delay_ms = smtc_modem_hal_get_radio_tcxo_startup_delay_ms( lr1_beacon_obj->lr1_mac->stack_id ) + smtc_modem_hal_get_board_delay_ms( lr1_beacon_obj->lr1_mac->stack_id ); int32_t rx_offset_ms; smtc_real_get_rx_start_time_offset_ms( lr1_beacon_obj->lr1_mac->real, BEACON_DATA_RATE( ), board_delay_ms, lr1_beacon_obj->beacon_open_rx_nb_symb, &rx_offset_ms ); diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_ping_slot.c b/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_ping_slot.c index eb5a1529..cd1fea1c 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_ping_slot.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_class_b/smtc_ping_slot.c @@ -416,7 +416,7 @@ void smtc_ping_slot_start( smtc_ping_slot_t* ping_slot_obj ) rp_task.state = RP_TASK_STATE_SCHEDULE; rp_task.schedule_task_low_priority = true; int8_t board_delay_ms = - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) + smtc_modem_hal_get_board_delay_ms( ); + smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ping_slot_obj->lr1_mac->stack_id ) + smtc_modem_hal_get_board_delay_ms( ping_slot_obj->lr1_mac->stack_id ); smtc_real_get_rx_start_time_offset_ms( ping_slot_obj->lr1_mac->real, RX_SESSION_PARAM_CURRENT->rx_data_rate, board_delay_ms, RX_SESSION_PARAM_CURRENT->rx_window_symb, &rx_offset_ms_tmp ); @@ -1208,7 +1208,7 @@ static void ping_slot_mac_rx_lora_launch_callback_for_rp( void* rp_void ) { } smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id,false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_rx( &( rp->radio->ral ), rp->radio_params[id].rx.timeout_in_ms ) == RAL_STATUS_OK ); rp_stats_set_rx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_core.c b/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_core.c index b1fec792..d8fa4be2 100755 --- a/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_core.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/lr1mac_core.c @@ -807,8 +807,10 @@ lr1mac_states_t lr1mac_core_state_get( lr1_stack_mac_t* lr1_mac_obj ) void lr1mac_core_context_save( lr1_stack_mac_t* lr1_mac_obj ) { lr1_mac_nvm_context_t ctx = { 0 }; + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes - smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * sizeof( ctx ), ( uint8_t* ) &ctx, + smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); if( ( ctx.devnonce != lr1_mac_obj->dev_nonce ) || @@ -845,12 +847,12 @@ void lr1mac_core_context_save( lr1_stack_mac_t* lr1_mac_obj ) ctx.join_status = lr1_mac_obj->join_status; #endif ctx.crc = lr1mac_utilities_crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); - smtc_modem_hal_context_store( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * sizeof( ctx ), ( uint8_t* ) &ctx, + smtc_modem_hal_context_store( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // dummy context reading to ensure context store is done before exiting the function lr1_mac_nvm_context_t dummy_context = { 0 }; - smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * sizeof( ctx ), + smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * real_size, ( uint8_t* ) &dummy_context, sizeof( dummy_context ) ); } } @@ -858,7 +860,9 @@ void lr1mac_core_context_save( lr1_stack_mac_t* lr1_mac_obj ) status_lorawan_t lr1mac_core_context_load( lr1_stack_mac_t* lr1_mac_obj ) { lr1_mac_nvm_context_t ctx = { 0 }; - smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * sizeof( ctx ), ( uint8_t* ) &ctx, + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); if( lr1mac_utilities_crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ) == ctx.crc ) @@ -890,16 +894,18 @@ status_lorawan_t lr1mac_core_context_load( lr1_stack_mac_t* lr1_mac_obj ) void lr1mac_core_context_factory_reset( lr1_stack_mac_t* lr1_mac_obj ) { lr1_mac_nvm_context_t ctx = { 0 }; + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes ctx.ctx_version = LORAWAN_NVM_CTX_VERSION; memset( ctx.join_nonce, 0xFF, sizeof( ctx.join_nonce ) ); ctx.crc = lr1mac_utilities_crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); - smtc_modem_hal_context_store( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * sizeof( ctx ), ( uint8_t* ) &ctx, + smtc_modem_hal_context_store( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // dummy context reading to ensure context store is done before exiting the function lr1_mac_nvm_context_t dummy_context = { 0 }; - smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * sizeof( ctx ), + smtc_modem_hal_context_restore( CONTEXT_LORAWAN_STACK, lr1_mac_obj->stack_id * real_size, ( uint8_t* ) &dummy_context, sizeof( dummy_context ) ); } @@ -1242,13 +1248,13 @@ status_lorawan_t lr1mac_core_update_join_channel( lr1_stack_mac_t* lr1_mac_obj ) smtc_real_get_next_tx_dr( lr1_mac_obj->real, lr1_mac_obj->join_status, &lr1_mac_obj->adr_mode_select, &lr1_mac_obj->tx_data_rate, lr1_mac_obj->tx_data_rate_adr, &lr1_mac_obj->adr_enable ); - return ( smtc_real_get_join_next_channel( + return ( smtc_real_get_join_next_channel( lr1_mac_obj->stack_id, lr1_mac_obj->real, &( lr1_mac_obj->tx_data_rate ), &( lr1_mac_obj->tx_frequency ), &( lr1_mac_obj->rx1_frequency ), &( lr1_mac_obj->rx2_frequency ), &( lr1_mac_obj->nb_available_tx_channel ) ) ); } status_lorawan_t lr1mac_core_update_next_tx_channel( lr1_stack_mac_t* lr1_mac_obj ) { - return ( smtc_real_get_next_channel( lr1_mac_obj->real, lr1_mac_obj->tx_data_rate, &( lr1_mac_obj->tx_frequency ), + return ( smtc_real_get_next_channel( lr1_mac_obj->stack_id, lr1_mac_obj->real, lr1_mac_obj->tx_data_rate, &( lr1_mac_obj->tx_frequency ), &( lr1_mac_obj->rx1_frequency ), &( lr1_mac_obj->nb_available_tx_channel ) ) ); } uint32_t lr1mac_core_get_time_of_nwk_ans( lr1_stack_mac_t* lr1_mac_obj ) @@ -1363,7 +1369,7 @@ static void lr1mac_mac_update( lr1_stack_mac_t* lr1_mac_obj ) if( ( lr1_mac_obj->type_of_ans_to_send == NWKFRAME_TOSEND ) || ( lr1_mac_obj->type_of_ans_to_send == USRFRAME_TORETRANSMIT ) ) { // @note ack send during the next tx|| ( packet.IsFrameToSend == USERACK_TOSEND ) ) { - if( smtc_real_get_next_channel( lr1_mac_obj->real, lr1_mac_obj->tx_data_rate, &lr1_mac_obj->tx_frequency, + if( smtc_real_get_next_channel( lr1_mac_obj->stack_id, lr1_mac_obj->real, lr1_mac_obj->tx_data_rate, &lr1_mac_obj->tx_frequency, &lr1_mac_obj->rx1_frequency, &lr1_mac_obj->nb_available_tx_channel ) != OKLORAWAN ) { diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.c b/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.c index 336e63a7..03fcfa87 100755 --- a/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.c @@ -245,7 +245,7 @@ void wor_ral_callback_start_tx( void* rp_void ) // Do nothing } smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( true ); + smtc_modem_hal_set_ant_switch( rp->stack_id, true ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_tx( &( rp->radio->ral ) ) == RAL_STATUS_OK ); rp_stats_set_tx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); } @@ -277,7 +277,7 @@ void wor_ral_callback_start_rx( void* rp_void ) { } smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id, false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_rx( &( rp->radio->ral ), rp->radio_params[id].rx.timeout_in_ms + 1000 ) == RAL_STATUS_OK ); @@ -330,7 +330,7 @@ void wor_ral_init_rx_wor( smtc_real_t* real, uint8_t dr, uint32_t freq_hz, wor_c } } -void wor_ral_init_cad( const ralf_t* radio, smtc_real_t* real, uint8_t dr, wor_cad_periodicity_t cad_period, +void wor_ral_init_cad( uint8_t stack_id, const ralf_t* radio, smtc_real_t* real, uint8_t dr, wor_cad_periodicity_t cad_period, bool is_first, uint32_t wor_toa_ms, ral_lora_cad_params_t* param ) { uint8_t sf; @@ -344,7 +344,7 @@ void wor_ral_init_cad( const ralf_t* radio, smtc_real_t* real, uint8_t dr, wor_c if( is_first == true ) { - if( smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) <= 1 ) + if( smtc_modem_hal_get_radio_tcxo_startup_delay_ms( stack_id) <= 1 ) { param->cad_exit_mode = RAL_LORA_CAD_ONLY; param->cad_symb_nb = RAL_LORA_CAD_01_SYMB; @@ -380,7 +380,7 @@ void wor_ral_init_cad( const ralf_t* radio, smtc_real_t* real, uint8_t dr, wor_c &( param->cad_det_peak_in_symb ) ) == RAL_STATUS_OK ); } -void wor_ral_init_rx_msg( smtc_real_t* real, uint8_t max_payload, uint8_t dr, uint32_t freq_hz, +void wor_ral_init_rx_msg( uint8_t stack_id, smtc_real_t* real, uint8_t max_payload, uint8_t dr, uint32_t freq_hz, rp_radio_params_t* param ) { modulation_type_t modulation_type = smtc_real_get_modulation_type_from_datarate( real, dr ); @@ -402,7 +402,7 @@ void wor_ral_init_rx_msg( smtc_real_t* real, uint8_t max_payload, uint8_t dr, ui else { lora->sync_word = smtc_real_get_sync_word( real ); - lora->symb_nb_timeout = 10 + 1000 * smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) / symb_time_us; + lora->symb_nb_timeout = 10 + 1000 * smtc_modem_hal_get_radio_tcxo_startup_delay_ms( stack_id ) / symb_time_us; lora->rf_freq_in_hz = freq_hz; lora->pkt_params.pld_len_in_bytes = max_payload; @@ -468,7 +468,7 @@ void wor_ral_callback_start_cad( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id, false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_lora_cad( &( rp->radio->ral ) ) == RAL_STATUS_OK ); rp_stats_set_rx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.h b/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.h index 7ee71c4a..a0602d6a 100755 --- a/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.h +++ b/lbm_lib/smtc_modem_core/lr1mac/src/relay/common/wake_on_radio_ral.h @@ -172,18 +172,20 @@ void wor_ral_callback_start_rx( void* rp_void ); /** * @brief Fill rp_radio_params_t struct to received LoRaWAN Uplink after a WOR * + * @param[in] stack_id Stack ID * @param[in] real Regional Abstraction Layer object * @param[in] max_payload Maximum payload to be received * @param[in] dr Datarate of the uplink * @param[in] freq_hz Frequency of the uplink * @param[out] param Radio parameter structure with LoRaWAN Uplink */ -void wor_ral_init_rx_msg( smtc_real_t* real, uint8_t max_payload, uint8_t dr, uint32_t freq_hz, +void wor_ral_init_rx_msg( uint8_t stack_id, smtc_real_t* real, uint8_t max_payload, uint8_t dr, uint32_t freq_hz, rp_radio_params_t* param ); /** * @brief Fill rp_radio_params_t struct for periodic CAD * + * @param[in] stack_id Stack ID * @param[in] radio radio pointer * @param[in] lr1_mac Lr1mac object * @param[in] dr Datarate of the CAD @@ -192,7 +194,7 @@ void wor_ral_init_rx_msg( smtc_real_t* real, uint8_t max_payload, uint8_t dr, ui * @param[in] wor_toa_ms TOA of the WOR to be received * @param[out] param Radio parameter structure with CAD infos */ -void wor_ral_init_cad( const ralf_t* radio, smtc_real_t* lr1_mac, uint8_t dr, wor_cad_periodicity_t cad_period, +void wor_ral_init_cad( uint8_t stack_id, const ralf_t* radio, smtc_real_t* lr1_mac, uint8_t dr, wor_cad_periodicity_t cad_period, bool is_first, uint32_t wor_toa_ms, ral_lora_cad_params_t* param ); #ifdef _cplusplus } diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_rx/relay_rx.c b/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_rx/relay_rx.c index 2c3fbe5b..4da9cee5 100755 --- a/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_rx/relay_rx.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_rx/relay_rx.c @@ -396,11 +396,11 @@ bool relay_stop( bool stop_to_fwd ) if( stop_to_fwd == true ) { - relay_info.dtc_state = smtc_duty_cycle_enable_get( ); + relay_info.dtc_state = smtc_duty_cycle_enable_get( relay_info.stack_id ); if( relay_info.dtc_state != SMTC_DTC_FULL_DISABLED ) { - smtc_duty_cycle_enable_set( SMTC_DTC_PARTIAL_DISABLED ); + smtc_duty_cycle_enable_set( relay_info.stack_id, SMTC_DTC_PARTIAL_DISABLED ); } } @@ -415,10 +415,10 @@ bool relay_start( void ) relay_stop( true ); } - const smtc_dtc_enablement_type_t dtc_state = smtc_duty_cycle_enable_get( ); + const smtc_dtc_enablement_type_t dtc_state = smtc_duty_cycle_enable_get( relay_info.stack_id ); if( dtc_state == SMTC_DTC_PARTIAL_DISABLED ) { - smtc_duty_cycle_enable_set( relay_info.dtc_state ); + smtc_duty_cycle_enable_set( relay_info.stack_id, relay_info.dtc_state ); } relay_info.is_started = true; @@ -456,8 +456,8 @@ void relay_fwd_dl( uint8_t stack_id, const uint8_t* buffer, uint8_t len ) const uint8_t ul_dr = get_ul_dr( &relay_wor_info ); const uint8_t dl_dr = smtc_real_get_rx1_datarate_config( real, ul_dr, 0 ); const uint32_t dl_freq = relay_config.channel_cfg[relay_info.current_ch_idx].freq_hz; - smtc_duty_cycle_update( ); - if( smtc_duty_cycle_is_channel_free( dl_freq ) == false ) + smtc_duty_cycle_update( stack_id ); + if( smtc_duty_cycle_is_channel_free( stack_id, dl_freq ) == false ) { SMTC_MODEM_HAL_TRACE_PRINTF( "no more duty cycle to forward downlink on RxR\n" ); relay_start( ); @@ -468,7 +468,7 @@ void relay_fwd_dl( uint8_t stack_id, const uint8_t* buffer, uint8_t len ) rp_task_t rp_task = { .start_time_ms = relay_info.rx_uplink_timestamp_ms + RXR_WINDOWS_DELAY_S * 1000 - - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ), + smtc_modem_hal_get_radio_tcxo_startup_delay_ms( stack_id ), .hook_id = RP_HOOK_ID_RELAY_FORWARD_RXR, .state = RP_TASK_STATE_SCHEDULE, .launch_task_callbacks = relay_rxr_tx_launch_callback, @@ -966,8 +966,8 @@ static void manage_fsm( const uint32_t irq_timestamp_ms, const bool cad_success, if( mic_is_valid == true ) { // check duty cycle before to transmit wor_ack - smtc_duty_cycle_update( ); - if( smtc_duty_cycle_is_channel_free( + smtc_duty_cycle_update( relay_info.stack_id ); + if( smtc_duty_cycle_is_channel_free( relay_info.stack_id, relay_config.channel_cfg[relay_info.current_ch_idx].ack_freq_hz ) == true ) { // Send ACK @@ -1064,7 +1064,7 @@ static void config_enqueue_next_cad( const relay_config_t* config, relay_infos_t const uint32_t cad_period_ms = wor_convert_cad_period_in_ms( config->cad_period ) / config->nb_wor_channel; uint32_t next_cad_start_ms = info->last_cad_ms; - while( ( ( int ) ( next_cad_start_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) - actual_ms ) <= 0 ) ) + while( ( ( int ) ( next_cad_start_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_info.lr1mac->stack_id) - actual_ms ) <= 0 ) ) { next_cad_start_ms += cad_period_ms; info->current_ch_idx += 1; @@ -1086,7 +1086,7 @@ static void config_enqueue_next_cad( const relay_config_t* config, relay_infos_t const relay_channel_config_t* channel_cfg = &config->channel_cfg[info->current_ch_idx]; wor_ral_init_rx_wor( relay_info.lr1mac->real, channel_cfg->dr, channel_cfg->freq_hz, cad_period, MAX( ( uint8_t ) WOR_JOINREQ_LENGTH, ( uint8_t ) WOR_UPLINK_LENGTH ), &rx_param ); - wor_ral_init_cad( relay_info.radio, relay_info.lr1mac->real, channel_cfg->dr, cad_period, true, + wor_ral_init_cad( relay_info.lr1mac->stack_id, relay_info.radio, relay_info.lr1mac->real, channel_cfg->dr, cad_period, true, relay_info.wor_toa_ms[info->current_ch_idx], &rx_param.rx.cad ); const rp_task_t rp_task_cad = { @@ -1095,7 +1095,7 @@ static void config_enqueue_next_cad( const relay_config_t* config, relay_infos_t .state = RP_TASK_STATE_SCHEDULE, .schedule_task_low_priority = true, .duration_time_ms = 20000, - .start_time_ms = info->next_cad_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ), + .start_time_ms = info->next_cad_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_info.lr1mac->stack_id ), .launch_task_callbacks = wor_ral_callback_start_cad, }; @@ -1116,7 +1116,7 @@ static void config_cad_to_rx_wor( const relay_config_t* config, relay_infos_t* i ral_lora_cad_params_t cad_param = { 0 }; - wor_ral_init_cad( relay_info.radio, relay_info.lr1mac->real, channel_cfg->dr, cad_period, false, + wor_ral_init_cad( relay_info.lr1mac->stack_id, relay_info.radio, relay_info.lr1mac->real, channel_cfg->dr, cad_period, false, relay_info.wor_toa_ms[info->current_ch_idx], &cad_param ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_lora_cad_params( &( relay_info.radio->ral ), &cad_param ) == @@ -1151,12 +1151,12 @@ static void config_enqueue_rx_msg( const relay_config_t* config, const wor_infos } rp_radio_params_t rp_radio_params = { 0 }; - wor_ral_init_rx_msg( relay_info.lr1mac->real, max_rx, ul_dr, ul_freq, &rp_radio_params ); + wor_ral_init_rx_msg( relay_info.lr1mac->stack_id, relay_info.lr1mac->real, max_rx, ul_dr, ul_freq, &rp_radio_params ); rp_task_t rx_task = { .hook_id = RP_HOOK_ID_RELAY_RX_CAD, .state = RP_TASK_STATE_SCHEDULE, - .start_time_ms = timestamp_lr1_ul - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ), + .start_time_ms = timestamp_lr1_ul - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_info.lr1mac->stack_id ), .launch_task_callbacks = wor_ral_callback_start_rx, }; @@ -1236,7 +1236,7 @@ static void config_enqueue_ack( const relay_config_t* config, const wor_infos_t* &radio_params ); const rp_task_t rp_task = { - .start_time_ms = time_rx + DELAY_WOR_TO_WORACK_MS - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ), + .start_time_ms = time_rx + DELAY_WOR_TO_WORACK_MS - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_info.lr1mac->stack_id ), .hook_id = RP_HOOK_ID_RELAY_RX_CAD, .state = RP_TASK_STATE_SCHEDULE, .launch_task_callbacks = wor_ral_callback_start_tx, @@ -1619,7 +1619,7 @@ static void relay_rxr_tx_launch_callback( void* rp_void ) } // At this time only tcxo startup delay is remaining smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( true ); + smtc_modem_hal_set_ant_switch( rp->stack_id, true ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_tx( &( rp->radio->ral ) ) == RAL_STATUS_OK ); rp_stats_set_tx_timestamp( &rp->stats, smtc_modem_hal_get_time_in_ms( ) ); } diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_tx/relay_tx.c b/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_tx/relay_tx.c index a3b92b4c..ccb4279c 100755 --- a/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_tx/relay_tx.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/relay/relay_tx/relay_tx.c @@ -171,7 +171,7 @@ bool smtc_relay_tx_init( uint8_t relay_stack_id, radio_planner_t* rp, smtc_real_ { SMTC_MODEM_HAL_PANIC( "smtc_relay_tx_init bad init\n" ); } - if( smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ) >= DELAY_WOR_TO_WORACK_MS ) + if( smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_stack_id ) >= DELAY_WOR_TO_WORACK_MS ) { SMTC_MODEM_HAL_PANIC( "TCXO delay not compatible with relay mode \n" ); } @@ -366,8 +366,8 @@ bool smtc_relay_tx_prepare_wor( uint8_t relay_stack_id, uint32_t target_time, co // If last channel used was the default -> remove half cad period ref_timestamp -= cad_period_ms >> 1; } - smtc_duty_cycle_update( ); - if( smtc_duty_cycle_is_channel_free( infos->relay_tx_config.second_ch.freq_hz ) == false ) + smtc_duty_cycle_update( relay_stack_id); + if( smtc_duty_cycle_is_channel_free( relay_stack_id, infos->relay_tx_config.second_ch.freq_hz ) == false ) { SMTC_MODEM_HAL_TRACE_PRINTF( "no more duty cycle for second_ch wor channel\n" ); return false; @@ -486,7 +486,7 @@ bool smtc_relay_tx_prepare_wor( uint8_t relay_stack_id, uint32_t target_time, co } wor_tx->freq_hz = conf->freq_hz; - wor_tx->target_time_ms = infos->last_timestamp_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + wor_tx->target_time_ms = infos->last_timestamp_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_stack_id ); wor_tx->dr = conf->dr; smtc_real_lora_dr_to_sf_bw( infos->real, conf->dr, &wor_tx->sf, ( lr1mac_bandwidth_t* ) &wor_tx->bw ); return true; @@ -598,7 +598,7 @@ int32_t smtc_relay_tx_free_duty_cycle_ms_get( uint8_t relay_stack_id ) tx_freq_list = infos->relay_tx_config.second_ch.freq_hz; } int32_t relay_tx_duty_cycle = - smtc_relay_tx_is_enable( relay_stack_id ) ? smtc_duty_cycle_get_next_free_time_ms( 1, &tx_freq_list ) : 0; + smtc_relay_tx_is_enable( relay_stack_id ) ? smtc_duty_cycle_get_next_free_time_ms( relay_stack_id, 1, &tx_freq_list ) : 0; SMTC_MODEM_HAL_TRACE_PRINTF_DEBUG( "relay_tx_duty_cycle = %d\n", relay_tx_duty_cycle ); return ( relay_tx_duty_cycle ); } @@ -671,7 +671,7 @@ static void relay_tx_callback_rp( uint8_t* stack_id ) ( infos->last_ch_idx == 0 ) ? &infos->default_ch_config : &( infos->relay_tx_config.second_ch ); wor_ack.start_time_ms = - infos->time_tx_done + DELAY_WOR_TO_WORACK_MS - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + infos->time_tx_done + DELAY_WOR_TO_WORACK_MS - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_stack_id ); wor_ack.toa = infos->toa_ack[infos->last_ch_idx]; wor_ack.freq_hz = conf->ack_freq_hz; wor_ack.payload = infos->buffer; @@ -699,9 +699,9 @@ static void relay_tx_callback_rp( uint8_t* stack_id ) } infos->target_timer_lr1 = infos->time_tx_done + infos->toa_ack[infos->last_ch_idx] + DELAY_WOR_TO_WORACK_MS + - DELAY_WORACK_TO_UPLINK_MS; // don't add "- smtc_modem_hal_get_radio_tcxo_startup_delay_ms( )" due to + DELAY_WORACK_TO_UPLINK_MS; // don't add "- smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_stack_id)" due to // the fact that lr1mac do it by itself- - // smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + // smtc_modem_hal_get_radio_tcxo_startup_delay_ms( relay_stack_id); } else { diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.c b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.c index 6a8c8b9b..1369755f 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.c @@ -67,9 +67,9 @@ static struct { - smtc_dtc_t* dtc_obj_ptr; + smtc_dtc_t* dtc_obj_ptr[NUMBER_OF_STACKS]; #if defined( REGION_EU_868 ) || ( REGION_RU_864 ) - smtc_dtc_t dtc_obj_ctx; + smtc_dtc_t dtc_obj_ctx[NUMBER_OF_STACKS]; #endif } dtc_context; #define dtc_obj_ptr dtc_context.dtc_obj_ptr @@ -133,21 +133,21 @@ static void smtc_duty_cycle_put_band_in_array( smtc_dtc_t* dtc_obj, uint8_t* tmp * ----------------------------------------------------------------------------- * --- PUBLIC FUNCTIONS DEFINITION --------------------------------------------- */ -void smtc_duty_cycle_init( void ) +void smtc_duty_cycle_init( uint8_t stack_id ) { #if defined( REGION_EU_868 ) || ( REGION_RU_864 ) - dtc_obj_ptr = &dtc_obj_ctx; + dtc_obj_ptr[stack_id] = &dtc_obj_ctx[stack_id]; // Set to 0 the dtc_obj - memset( dtc_obj_ptr, 0, sizeof( smtc_dtc_t ) ); + memset( dtc_obj_ptr[stack_id], 0, sizeof( smtc_dtc_t ) ); #else - dtc_obj_ptr = NULL; + dtc_obj_ptr[stack_id] = NULL; #endif } -void smtc_duty_cycle_config( uint8_t number_of_bands, uint8_t band_idx, uint16_t duty_cycle_regulation, +void smtc_duty_cycle_config( uint8_t stack_id, uint8_t number_of_bands, uint8_t band_idx, uint16_t duty_cycle_regulation, uint32_t freq_min, uint32_t freq_max ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return; } @@ -155,69 +155,69 @@ void smtc_duty_cycle_config( uint8_t number_of_bands, uint8_t band_idx, uint16_t { SMTC_MODEM_HAL_PANIC( ); } - dtc_obj_ptr->number_of_bands = number_of_bands; - if( ( band_idx >= dtc_obj_ptr->number_of_bands ) || ( dtc_obj_ptr->number_of_bands == 0 ) ) + dtc_obj_ptr[stack_id]->number_of_bands = number_of_bands; + if( ( band_idx >= dtc_obj_ptr[stack_id]->number_of_bands ) || ( dtc_obj_ptr[stack_id]->number_of_bands == 0 ) ) { SMTC_MODEM_HAL_PANIC( ); } - dtc_obj_ptr->bands[band_idx].duty_cycle_regulation = duty_cycle_regulation; - dtc_obj_ptr->bands[band_idx].freq_min = freq_min; - dtc_obj_ptr->bands[band_idx].freq_max = freq_max; + dtc_obj_ptr[stack_id]->bands[band_idx].duty_cycle_regulation = duty_cycle_regulation; + dtc_obj_ptr[stack_id]->bands[band_idx].freq_min = freq_min; + dtc_obj_ptr[stack_id]->bands[band_idx].freq_max = freq_max; } -smtc_dtc_rc_t smtc_duty_cycle_enable_set( smtc_dtc_enablement_type_t enable ) +smtc_dtc_rc_t smtc_duty_cycle_enable_set( uint8_t stack_id, smtc_dtc_enablement_type_t enable ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return SMTC_DTC_ERR; } if( enable != SMTC_DTC_FULL_DISABLED ) { - if( dtc_obj_ptr->number_of_bands == 0 ) + if( dtc_obj_ptr[stack_id]->number_of_bands == 0 ) { return SMTC_DTC_ERR; } } - dtc_obj_ptr->enabled = enable; + dtc_obj_ptr[stack_id]->enabled = enable; return SMTC_DTC_OK; } -smtc_dtc_enablement_type_t smtc_duty_cycle_enable_get( void ) +smtc_dtc_enablement_type_t smtc_duty_cycle_enable_get( uint8_t stack_id ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return SMTC_DTC_FULL_DISABLED; } - return dtc_obj_ptr->enabled; + return dtc_obj_ptr[stack_id]->enabled; } -void smtc_duty_cycle_sum( uint32_t freq_hz, uint32_t toa_ms ) +void smtc_duty_cycle_sum( uint8_t stack_id, uint32_t freq_hz, uint32_t toa_ms ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return; } - if( dtc_obj_ptr->number_of_bands == 0 ) + if( dtc_obj_ptr[stack_id]->number_of_bands == 0 ) { return; } - if( dtc_obj_ptr->enabled == SMTC_DTC_FULL_DISABLED ) + if( dtc_obj_ptr[stack_id]->enabled == SMTC_DTC_FULL_DISABLED ) { return; } uint32_t rtc_time_now = smtc_modem_hal_get_time_in_ms( ); uint8_t band; - if( smtc_duty_cycle_get_band( dtc_obj_ptr, freq_hz, &band ) == false ) + if( smtc_duty_cycle_get_band( dtc_obj_ptr[stack_id], freq_hz, &band ) == false ) { return; } - uint8_t idx_previous = dtc_obj_ptr->bands[band].index_previous; + uint8_t idx_previous = dtc_obj_ptr[stack_id]->bands[band].index_previous; // compute index by delta to manage rtc_ms wrapping - uint32_t timestamp_diff = smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr->bands[band].toa_timestamp_ms ); + uint32_t timestamp_diff = smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms ); uint8_t idx_new = smtc_duty_cycle_compute_index( timestamp_diff, idx_previous ); // Convert TOA to the resolution @@ -226,10 +226,10 @@ void smtc_duty_cycle_sum( uint32_t freq_hz, uint32_t toa_ms ) : ( toa_ms / smtc_dtc_resolution_ms ) + 1; // More than SMTC_DTC_PERIOD_MS since the last timestamp - if( smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr->bands[band].toa_timestamp_ms ) >= SMTC_DTC_PERIOD_MS ) + if( smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms ) >= SMTC_DTC_PERIOD_MS ) { // Erase band cumulated TOA - memset( dtc_obj_ptr->bands[band].toa_sum_ms, 0, sizeof( dtc_obj_ptr->bands[band].toa_sum_ms ) ); + memset( dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms, 0, sizeof( dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms ) ); } else { @@ -237,7 +237,7 @@ void smtc_duty_cycle_sum( uint32_t freq_hz, uint32_t toa_ms ) if( ( idx_new == idx_previous ) && ( timestamp_diff < ( SMTC_DTC_SECONDS_BY_UNIT * 1000 ) ) ) { // Sum TOA in same buffer - toa_ms += dtc_obj_ptr->bands[band].toa_sum_ms[idx_new]; + toa_ms += dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms[idx_new]; } else { @@ -250,42 +250,42 @@ void smtc_duty_cycle_sum( uint32_t freq_hz, uint32_t toa_ms ) { i = 0; } - dtc_obj_ptr->bands[band].toa_sum_ms[i] = 0; + dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms[i] = 0; } } } // Save the new TOA - dtc_obj_ptr->bands[band].toa_sum_ms[idx_new] = toa_ms; - dtc_obj_ptr->bands[band].toa_timestamp_ms = rtc_time_now; - dtc_obj_ptr->bands[band].index_previous = idx_new; + dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms[idx_new] = toa_ms; + dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms = rtc_time_now; + dtc_obj_ptr[stack_id]->bands[band].index_previous = idx_new; } -void smtc_duty_cycle_update( void ) +void smtc_duty_cycle_update( uint8_t stack_id ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return; } - if( dtc_obj_ptr->number_of_bands == 0 ) + if( dtc_obj_ptr[stack_id]->number_of_bands == 0 ) { return; } uint32_t rtc_time_now = smtc_modem_hal_get_time_in_ms( ); - for( uint8_t band = 0; band < dtc_obj_ptr->number_of_bands; band++ ) + for( uint8_t band = 0; band < dtc_obj_ptr[stack_id]->number_of_bands; band++ ) { - uint8_t idx_previous = dtc_obj_ptr->bands[band].index_previous; + uint8_t idx_previous = dtc_obj_ptr[stack_id]->bands[band].index_previous; // compute index by delta to manage rtc_ms wrapping - uint32_t timestamp_diff = smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr->bands[band].toa_timestamp_ms ); + uint32_t timestamp_diff = smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms ); uint8_t idx_new = smtc_duty_cycle_compute_index( timestamp_diff, idx_previous ); // More than SMTC_DTC_PERIOD_MS since the last timestamp - if( smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr->bands[band].toa_timestamp_ms ) >= SMTC_DTC_PERIOD_MS ) + if( smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms ) >= SMTC_DTC_PERIOD_MS ) { // Erase band cumulated TOA, it's been over 1h - memset( dtc_obj_ptr->bands[band].toa_sum_ms, 0, sizeof( dtc_obj_ptr->bands[band].toa_sum_ms ) ); - dtc_obj_ptr->bands[band].toa_timestamp_ms = rtc_time_now; - dtc_obj_ptr->bands[band].index_previous = idx_new; + memset( dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms, 0, sizeof( dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms ) ); + dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms = rtc_time_now; + dtc_obj_ptr[stack_id]->bands[band].index_previous = idx_new; } else { @@ -302,16 +302,16 @@ void smtc_duty_cycle_update( void ) if( ( i != idx_new ) || ( ( i == idx_new ) && ( timestamp_diff >= ( SMTC_DTC_SECONDS_BY_UNIT * 1000 ) ) ) ) { - dtc_obj_ptr->bands[band].toa_sum_ms[i] = 0; + dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms[i] = 0; } } } } } -bool smtc_duty_cycle_is_toa_accepted( smtc_dtc_t* dtc_obj, uint32_t freq_hz, uint32_t toa_ms ) +bool smtc_duty_cycle_is_toa_accepted( uint8_t stack_id, smtc_dtc_t* dtc_obj, uint32_t freq_hz, uint32_t toa_ms ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return true; } @@ -343,9 +343,9 @@ bool smtc_duty_cycle_is_toa_accepted( smtc_dtc_t* dtc_obj, uint32_t freq_hz, uin return true; } -int32_t smtc_duty_cycle_band_get_available_toa_ms( smtc_dtc_t* dtc_obj, uint8_t band ) +int32_t smtc_duty_cycle_band_get_available_toa_ms( uint8_t stack_id, smtc_dtc_t* dtc_obj, uint8_t band ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return true; } @@ -361,33 +361,33 @@ int32_t smtc_duty_cycle_band_get_available_toa_ms( smtc_dtc_t* dtc_obj, uint8_t return toa; } -bool smtc_duty_cycle_is_channel_free( uint32_t freq_hz ) +bool smtc_duty_cycle_is_channel_free( uint8_t stack_id, uint32_t freq_hz ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return true; } - if( ( dtc_obj_ptr->enabled != SMTC_DTC_ENABLED ) || ( dtc_obj_ptr->number_of_bands == 0 ) ) + if( ( dtc_obj_ptr[stack_id]->enabled != SMTC_DTC_ENABLED ) || ( dtc_obj_ptr[stack_id]->number_of_bands == 0 ) ) { return true; } uint8_t band; - if( smtc_duty_cycle_get_band( dtc_obj_ptr, freq_hz, &band ) == false ) + if( smtc_duty_cycle_get_band( dtc_obj_ptr[stack_id], freq_hz, &band ) == false ) { return true; } - if( smtc_duty_cycle_band_get_available_toa_ms( dtc_obj_ptr, band ) > 0 ) + if( smtc_duty_cycle_band_get_available_toa_ms( stack_id, dtc_obj_ptr[stack_id], band ) > 0 ) { return true; } return false; } -bool smtc_duty_cycle_is_band_free( smtc_dtc_t* dtc_obj, uint8_t band ) +bool smtc_duty_cycle_is_band_free( uint8_t stack_id, smtc_dtc_t* dtc_obj, uint8_t band ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return true; } @@ -396,20 +396,20 @@ bool smtc_duty_cycle_is_band_free( smtc_dtc_t* dtc_obj, uint8_t band ) return true; } - if( smtc_duty_cycle_band_get_available_toa_ms( dtc_obj, band ) > 0 ) + if( smtc_duty_cycle_band_get_available_toa_ms( stack_id, dtc_obj, band ) > 0 ) { return true; } return false; } -int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t number_of_tx_freq, uint32_t* tx_freq_list ) +int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t stack_id, uint8_t number_of_tx_freq, uint32_t* tx_freq_list ) { - if( dtc_obj_ptr == NULL ) + if( dtc_obj_ptr[stack_id] == NULL ) { return 0; } - if( ( dtc_obj_ptr->enabled != SMTC_DTC_ENABLED ) || ( dtc_obj_ptr->number_of_bands == 0 ) ) + if( ( dtc_obj_ptr[stack_id]->enabled != SMTC_DTC_ENABLED ) || ( dtc_obj_ptr[stack_id]->number_of_bands == 0 ) ) { return 0; } @@ -428,26 +428,26 @@ int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t number_of_tx_freq, uint32 memset( tmp_band_dtc_full, 0xFF, SMTC_DTC_BANDS_MAX ); // Update duty-cycle timing - smtc_duty_cycle_update( ); + smtc_duty_cycle_update( stack_id ); uint8_t band_prev = 0xFF; for( uint8_t i = 0; i < number_of_tx_freq; i++ ) { uint8_t band; - if( smtc_duty_cycle_get_band( dtc_obj_ptr, tx_freq_list[i], &band ) == true ) + if( smtc_duty_cycle_get_band( dtc_obj_ptr[stack_id], tx_freq_list[i], &band ) == true ) { if( band_prev != band ) { band_prev = band; - if( smtc_duty_cycle_is_band_free( dtc_obj_ptr, band ) == true ) + if( smtc_duty_cycle_is_band_free( stack_id, dtc_obj_ptr[stack_id], band ) == true ) { // Put unique band in free array - smtc_duty_cycle_put_band_in_array( dtc_obj_ptr, tmp_band_dtc_free, band, &tmp_band_dtc_free_index ); + smtc_duty_cycle_put_band_in_array( dtc_obj_ptr[stack_id], tmp_band_dtc_free, band, &tmp_band_dtc_free_index ); } else { // Put unique band in full array - smtc_duty_cycle_put_band_in_array( dtc_obj_ptr, tmp_band_dtc_full, band, &tmp_band_dtc_full_index ); + smtc_duty_cycle_put_band_in_array( dtc_obj_ptr[stack_id], tmp_band_dtc_full, band, &tmp_band_dtc_full_index ); } } } @@ -464,7 +464,7 @@ int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t number_of_tx_freq, uint32 // return negative value if time available for( uint8_t i = 0; i < tmp_band_dtc_free_index; i++ ) { - ret -= smtc_duty_cycle_band_get_available_toa_ms( dtc_obj_ptr, tmp_band_dtc_free[i] ); + ret -= smtc_duty_cycle_band_get_available_toa_ms( stack_id, dtc_obj_ptr[stack_id], tmp_band_dtc_free[i] ); } } else @@ -477,10 +477,10 @@ int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t number_of_tx_freq, uint32 { uint8_t band = tmp_band_dtc_full[j]; - uint8_t idx_previous = dtc_obj_ptr->bands[band].index_previous; + uint8_t idx_previous = dtc_obj_ptr[stack_id]->bands[band].index_previous; // compute index by delta to manage rtc_ms wrapping uint32_t timestamp_diff = - smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr->bands[band].toa_timestamp_ms ); + smtc_duty_cycle_time_diff( rtc_time_now, dtc_obj_ptr[stack_id]->bands[band].toa_timestamp_ms ); uint8_t idx_new = smtc_duty_cycle_compute_index( timestamp_diff, idx_previous ); // compute time between now and the end of this index @@ -497,7 +497,7 @@ int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t number_of_tx_freq, uint32 { i = 0; } - if( dtc_obj_ptr->bands[band].toa_sum_ms[i] != 0 ) + if( dtc_obj_ptr[stack_id]->bands[band].toa_sum_ms[i] != 0 ) { break; } diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.h b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.h index 45199626..2f74c90e 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.h +++ b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_duty_cycle.h @@ -126,19 +126,22 @@ static const uint32_t smtc_dtc_resolution_ms = ( ( ( SMTC_DTC_SECONDS_BY_UNIT * /** * @brief Duty cycle initialization * + * @param stack_id Stack id + * */ -void smtc_duty_cycle_init( void ); +void smtc_duty_cycle_init( uint8_t stack_id ); /** * @brief Duty cycle configuration * + * @param stack_id Stack id * @param number_of_bands Number of bands in this region * @param band_idx Index bands to store configuration * @param duty_cycle_regulation Duty cycle limitation on this band * @param freq_min Frequency min on this band * @param freq_max Frequency max on this band */ -void smtc_duty_cycle_config( uint8_t number_of_bands, uint8_t band_idx, uint16_t duty_cycle_regulation, +void smtc_duty_cycle_config( uint8_t stack_id, uint8_t number_of_bands, uint8_t band_idx, uint16_t duty_cycle_regulation, uint32_t freq_min, uint32_t freq_max ); /** @@ -146,90 +149,101 @@ void smtc_duty_cycle_config( uint8_t number_of_bands, uint8_t band_idx, uint16_t * * @remark When disabled the TOA is take into account, but no restriction if DTC full consumed * + * @param stack_id Stack id * @param enable Enable: true, Disabled: false, * @return smtc_dtc_rc_t */ -smtc_dtc_rc_t smtc_duty_cycle_enable_set( smtc_dtc_enablement_type_t enable ); +smtc_dtc_rc_t smtc_duty_cycle_enable_set( uint8_t stack_id, smtc_dtc_enablement_type_t enable ); /** * @brief Duty cycle enablement status * + * @param stack_id Stack id + * * @return smtc_dtc_enablement_type_t */ -smtc_dtc_enablement_type_t smtc_duty_cycle_enable_get( void ); +smtc_dtc_enablement_type_t smtc_duty_cycle_enable_get( uint8_t stack_id ); /** * @brief Sum Time On Air for a specified freq (inside a band) * * @remark smtc_duty_cycle_update() must be called before this function to have a right value * + * @param stack_id Stack id * @param freq_hz Frequency used for the packet * @param toa_ms Packet Time On Air in milliseconds */ -void smtc_duty_cycle_sum( uint32_t freq_hz, uint32_t toa_ms ); +void smtc_duty_cycle_sum( uint8_t stack_id, uint32_t freq_hz, uint32_t toa_ms ); /** * @brief Update Time On Air * + * @param stack_id Stack id + * * @remark smtc_duty_cycle_update() must be called before check Duty Cycle available * */ -void smtc_duty_cycle_update( void ); +void smtc_duty_cycle_update( uint8_t stack_id ); /** * @brief Check if Time On Air is not greater than TOA available for a frequency * * @remark smtc_duty_cycle_update() must be called before this function to have a right value * + * @param stack_id Stack id * @param dtc_obj Contains the duty cycle context * @param freq_hz Frequency used for the packet * @param toa_ms Packet Time On Air in milliseconds * @return bool */ -bool smtc_duty_cycle_is_toa_accepted( smtc_dtc_t* dtc_obj, uint32_t freq_hz, uint32_t toa_ms ); +bool smtc_duty_cycle_is_toa_accepted( uint8_t stack_id, smtc_dtc_t* dtc_obj, uint32_t freq_hz, uint32_t toa_ms ); /** * @brief Get Time On Air available in a band * * @remark smtc_duty_cycle_update() must be called before this function to have a right value * + * @param stack_id Stack id * @param dtc_obj Contains the duty cycle context * @param band Band id * @return int32_t return Time On Air available */ -int32_t smtc_duty_cycle_band_get_available_toa_ms( smtc_dtc_t* dtc_obj, uint8_t band ); +int32_t smtc_duty_cycle_band_get_available_toa_ms( uint8_t stack_id, smtc_dtc_t* dtc_obj, uint8_t band ); /** * @brief Check if a channel is Duty Cycle free * * @remark smtc_duty_cycle_update() must be called before this function to have a right value * + * @param stack_id Stack id * @param freq_hz Frequency that need a check * @return bool */ -bool smtc_duty_cycle_is_channel_free( uint32_t freq_hz ); +bool smtc_duty_cycle_is_channel_free( uint8_t stack_id, uint32_t freq_hz ); /** * @brief Check if a band is Duty Cycle free * * @remark smtc_duty_cycle_update() must be called before this function to have a right value * + * @param stack_id Stack id * @param dtc_obj Contains the duty cycle context * @param band Band that need a check * @return bool */ -bool smtc_duty_cycle_is_band_free( smtc_dtc_t* dtc_obj, uint8_t band ); +bool smtc_duty_cycle_is_band_free( uint8_t stack_id, smtc_dtc_t* dtc_obj, uint8_t band ); /** * @brief Get the next available slot with free duty cycle * * @remark smtc_duty_cycle_update() must be called before this function to have a right value * + * @param stack_id Stack id * @param number_of_tx_freq number of tx freq in list * @param tx_freq_list tx frequency list used by the app to check only duty cycle in these bands * @return int32_t milliseconds, if > 0: the next slot availble, else the available time */ -int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t number_of_tx_freq, uint32_t* tx_freq_list ); +int32_t smtc_duty_cycle_get_next_free_time_ms( uint8_t stack_id, uint8_t number_of_tx_freq, uint32_t* tx_freq_list ); #ifdef __cplusplus } #endif diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lbt.c b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lbt.c index ad81ccc2..863e26c1 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lbt.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lbt.c @@ -106,7 +106,7 @@ void smtc_lbt_launch_callback_for_rp( void* rp_void ) uint8_t id = rp->radio_task_id; int16_t rssi_tmp; smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id, false ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_pkt_type( &( rp->radio->ral ), rp->radio_params[id].pkt_type ) == RAL_STATUS_OK ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( @@ -187,7 +187,7 @@ void smtc_lbt_listen_channel( smtc_lbt_t* lbt_obj, uint32_t freq, bool is_at_tim if( is_at_time == true ) { rp_task.start_time_ms = - target_time_ms - lbt_obj->listen_duration_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + target_time_ms - lbt_obj->listen_duration_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( lbt_obj->rp->stack_id ); rp_task.state = RP_TASK_STATE_SCHEDULE; } else @@ -247,4 +247,4 @@ smtc_lbt_t* smtc_lbt_get_obj( uint8_t stack_id ) { return NULL; } -} \ No newline at end of file +} diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lora_cad_bt.c b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lora_cad_bt.c index fa6dbac1..62253ec8 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lora_cad_bt.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/services/smtc_lora_cad_bt.c @@ -265,7 +265,7 @@ void smtc_lora_cad_bt_listen_channel( smtc_lora_cad_bt_t* cad_obj, uint32_t freq if( cad_obj->nb_bo > 0 ) { listen_duration_ms += ( cad_obj->nb_bo * ( ( 1 << NB_CAD_SYMBOLS_IN_BO ) * symbol_duration_us ) ) / 1000; - listen_duration_ms += cad_obj->nb_bo * smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + listen_duration_ms += cad_obj->nb_bo * smtc_modem_hal_get_radio_tcxo_startup_delay_ms( cad_obj->rp->stack_id); } lora_cad_param.ral_lora_cad_params.cad_det_min_in_symb = 10; @@ -312,7 +312,7 @@ void smtc_lora_cad_bt_listen_channel( smtc_lora_cad_bt_t* cad_obj, uint32_t freq rp_task.duration_time_ms = listen_duration_ms + tx_duration_ms; rp_task.launch_task_callbacks = smtc_cad_bt_launch_radio_callback_for_rp; rp_task.schedule_task_low_priority = false; - rp_task.start_time_ms = target_time_ms - listen_duration_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + rp_task.start_time_ms = target_time_ms - listen_duration_ms - smtc_modem_hal_get_radio_tcxo_startup_delay_ms( cad_obj->rp->stack_id ); if( is_at_time == true ) { rp_task.state = RP_TASK_STATE_SCHEDULE; diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.c b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.c index f28ee4e8..ab5160e7 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.c @@ -179,24 +179,24 @@ void region_eu_868_config( smtc_real_t* real ) memset( &unwrapped_channel_mask[0], 0xFF, BANK_MAX_EU868 ); } -status_lorawan_t region_eu_868_get_join_next_channel( smtc_real_t* real, uint8_t tx_data_rate, +status_lorawan_t region_eu_868_get_join_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ) { - return region_eu_868_get_next_channel( real, tx_data_rate, out_tx_frequency, out_rx1_frequency, active_channel_nb ); + return region_eu_868_get_next_channel( stack_id, real, tx_data_rate, out_tx_frequency, out_rx1_frequency, active_channel_nb ); } -status_lorawan_t region_eu_868_get_next_channel( smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t region_eu_868_get_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ) { *active_channel_nb = 0; uint8_t active_channel_index[NUMBER_OF_CHANNEL_EU_868]; - smtc_duty_cycle_update( ); + smtc_duty_cycle_update( stack_id ); for( uint8_t i = 0; i < real_const.const_number_of_tx_channel; i++ ) { if( SMTC_GET_BIT8( channel_index_enabled, i ) == CHANNEL_ENABLED ) { - if( ( smtc_duty_cycle_is_channel_free( tx_frequency_channel[i] ) == true ) && + if( ( smtc_duty_cycle_is_channel_free( stack_id, tx_frequency_channel[i] ) == true ) && ( SMTC_GET_BIT16( &dr_bitfield_tx_channel[i], tx_data_rate ) == 1 ) ) { active_channel_index[*active_channel_nb] = i; diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.h b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.h index 5c02eb90..2a9d3485 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.h +++ b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_eu_868.h @@ -89,10 +89,11 @@ void region_eu_868_config( smtc_real_t* real ); /** * @brief Get the next channel for the future uplink * + * @param stack_id Stack id * @param real * @return status_lorawan_t */ -status_lorawan_t region_eu_868_get_next_channel( smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t region_eu_868_get_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ); /** @@ -101,7 +102,7 @@ status_lorawan_t region_eu_868_get_next_channel( smtc_real_t* real, uint8_t tx_d * @param real * @return status_lorawan_t */ -status_lorawan_t region_eu_868_get_join_next_channel( smtc_real_t* real, uint8_t tx_data_rate, +status_lorawan_t region_eu_868_get_join_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ); diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.c b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.c index 2ce46be8..a835b146 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.c @@ -178,25 +178,25 @@ void region_ru_864_config( smtc_real_t* real ) memset( &unwrapped_channel_mask[0], 0xFF, BANK_MAX_RU864 ); } -status_lorawan_t region_ru_864_get_join_next_channel( smtc_real_t* real, uint8_t tx_data_rate, +status_lorawan_t region_ru_864_get_join_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ) { - return region_ru_864_get_next_channel( real, tx_data_rate, out_tx_frequency, out_rx1_frequency, active_channel_nb ); + return region_ru_864_get_next_channel( stack_id, real, tx_data_rate, out_tx_frequency, out_rx1_frequency, active_channel_nb ); } -status_lorawan_t region_ru_864_get_next_channel( smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t region_ru_864_get_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ) { *active_channel_nb = 0; uint8_t active_channel_index[NUMBER_OF_CHANNEL_RU_864]; - smtc_duty_cycle_update( ); + smtc_duty_cycle_update( stack_id); for( uint8_t i = 0; i < real_const.const_number_of_tx_channel; i++ ) { if( SMTC_GET_BIT8( channel_index_enabled, i ) == CHANNEL_ENABLED ) { - if( ( smtc_duty_cycle_is_channel_free( tx_frequency_channel[i] ) == true ) && + if( ( smtc_duty_cycle_is_channel_free( stack_id, tx_frequency_channel[i] ) == true ) && ( SMTC_GET_BIT16( &dr_bitfield_tx_channel[i], tx_data_rate ) == 1 ) ) { active_channel_index[*active_channel_nb] = i; diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.h b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.h index 966919bc..45b6fe6f 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.h +++ b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/region_ru_864.h @@ -91,7 +91,7 @@ void region_ru_864_config( smtc_real_t* real ); * \param [IN] none * \param [OUT] return */ -status_lorawan_t region_ru_864_get_next_channel( smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t region_ru_864_get_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ); /** * \brief @@ -99,7 +99,7 @@ status_lorawan_t region_ru_864_get_next_channel( smtc_real_t* real, uint8_t tx_d * \param [IN] none * \param [OUT] return */ -status_lorawan_t region_ru_864_get_join_next_channel( smtc_real_t* real, uint8_t tx_data_rate, +status_lorawan_t region_ru_864_get_join_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* active_channel_nb ); /** diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.c b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.c index 79270fc2..27f2a5d0 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.c +++ b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.c @@ -669,7 +669,7 @@ uint8_t smtc_real_get_number_of_chmask_in_cflist( smtc_real_t* real ) return ERRORLORAWAN; // never reach => avoid warning } -status_lorawan_t smtc_real_get_next_channel( smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t smtc_real_get_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* out_nb_available_tx_channel ) { switch( real->region_type ) @@ -684,7 +684,7 @@ status_lorawan_t smtc_real_get_next_channel( smtc_real_t* real, uint8_t tx_data_ #if defined( REGION_EU_868 ) case SMTC_REAL_REGION_EU_868: { - return region_eu_868_get_next_channel( real, tx_data_rate, out_tx_frequency, out_rx1_frequency, + return region_eu_868_get_next_channel( stack_id, real, tx_data_rate, out_tx_frequency, out_rx1_frequency, out_nb_available_tx_channel ); } #endif @@ -745,7 +745,7 @@ status_lorawan_t smtc_real_get_next_channel( smtc_real_t* real, uint8_t tx_data_ #if defined( REGION_RU_864 ) case SMTC_REAL_REGION_RU_864: { - return region_ru_864_get_next_channel( real, tx_data_rate, out_tx_frequency, out_rx1_frequency, + return region_ru_864_get_next_channel( stack_id, real, tx_data_rate, out_tx_frequency, out_rx1_frequency, out_nb_available_tx_channel ); } #endif @@ -756,7 +756,7 @@ status_lorawan_t smtc_real_get_next_channel( smtc_real_t* real, uint8_t tx_data_ return ERRORLORAWAN; // never reach => avoid warning } -status_lorawan_t smtc_real_get_join_next_channel( smtc_real_t* real, uint8_t* tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t smtc_real_get_join_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t* tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint32_t* out_rx2_frequency, uint8_t* out_nb_available_tx_channel ) { @@ -772,7 +772,7 @@ status_lorawan_t smtc_real_get_join_next_channel( smtc_real_t* real, uint8_t* tx #if defined( REGION_EU_868 ) case SMTC_REAL_REGION_EU_868: { - return region_eu_868_get_join_next_channel( real, *tx_data_rate, out_tx_frequency, out_rx1_frequency, + return region_eu_868_get_join_next_channel( stack_id, real, *tx_data_rate, out_tx_frequency, out_rx1_frequency, out_nb_available_tx_channel ); } #endif @@ -833,7 +833,7 @@ status_lorawan_t smtc_real_get_join_next_channel( smtc_real_t* real, uint8_t* tx #if defined( REGION_RU_864 ) case SMTC_REAL_REGION_RU_864: { - return region_ru_864_get_join_next_channel( real, *tx_data_rate, out_tx_frequency, out_rx1_frequency, + return region_ru_864_get_join_next_channel( stack_id, real, *tx_data_rate, out_tx_frequency, out_rx1_frequency, out_nb_available_tx_channel ); } #endif diff --git a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.h b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.h index 96c81456..80cfe79a 100644 --- a/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.h +++ b/lbm_lib/smtc_modem_core/lr1mac/src/smtc_real/src/smtc_real.h @@ -170,7 +170,7 @@ uint8_t smtc_real_get_number_of_chmask_in_cflist( smtc_real_t* real ); * \param [IN] none * \param [OUT] return */ -status_lorawan_t smtc_real_get_next_channel( smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t smtc_real_get_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint8_t* out_nb_available_tx_channel ); /** @@ -179,7 +179,7 @@ status_lorawan_t smtc_real_get_next_channel( smtc_real_t* real, uint8_t tx_data_ * \param [IN] none * \param [OUT] return */ -status_lorawan_t smtc_real_get_join_next_channel( smtc_real_t* real, uint8_t* tx_data_rate, uint32_t* out_tx_frequency, +status_lorawan_t smtc_real_get_join_next_channel( uint8_t stack_id, smtc_real_t* real, uint8_t* tx_data_rate, uint32_t* out_tx_frequency, uint32_t* out_rx1_frequency, uint32_t* out_rx2_frequency, uint8_t* out_nb_available_tx_channel ); diff --git a/lbm_lib/smtc_modem_core/modem_services/almanac_packages/almanac.c b/lbm_lib/smtc_modem_core/modem_services/almanac_packages/almanac.c index c18c7557..6fbb2b2a 100644 --- a/lbm_lib/smtc_modem_core/modem_services/almanac_packages/almanac.c +++ b/lbm_lib/smtc_modem_core/modem_services/almanac_packages/almanac.c @@ -50,6 +50,9 @@ #include "device_management_defs.h" #include "lr11xx_gnss.h" #include "radio_planner_hook_id_defs.h" + +// EvaTODO: add multistack support + /* * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- @@ -155,8 +158,8 @@ void almanac_services_init( uint8_t* service_id, uint8_t task_id, almanac_obj.up_count = 0; almanac_obj.get_almanac_status_from_lr11xx = false; almanac_obj.rp_hook_id = RP_HOOK_ID_DIRECT_RP_ACCESS_4_ALMANAC + CURRENT_STACK; - rp_hook_init( modem_get_rp( ), almanac_obj.rp_hook_id, ( void ( * )( void* ) )( rp_end_almanac_callback ), - modem_get_rp( ) ); + rp_hook_init( modem_get_rp( 0 ), almanac_obj.rp_hook_id, ( void ( * )( void* ) )( rp_end_almanac_callback ), + modem_get_rp( 0 ) ); } void almanac_service_on_launch( void* context ) @@ -280,16 +283,16 @@ static void rp_start_almanac_callback( void* context ) if( almanac_obj.almanac_dw_buffer_size > 0 ) { SMTC_MODEM_HAL_TRACE_PRINTF( "=> Push almanac update to LR11xx\n" ); - lr11xx_gnss_push_dmc_msg( modem_get_radio_ctx( ), almanac_obj.almanac_dw_buffer, + lr11xx_gnss_push_dmc_msg( modem_get_radio_ctx( 0 ), almanac_obj.almanac_dw_buffer, almanac_obj.almanac_dw_buffer_size ); almanac_obj.almanac_dw_buffer_size = 0; } - lr11xx_gnss_get_context_status( modem_get_radio_ctx( ), &almanac_obj.almanac_status_from_lr11xx[0] ); + lr11xx_gnss_get_context_status( modem_get_radio_ctx( 0 ), &almanac_obj.almanac_status_from_lr11xx[0] ); almanac_obj.get_almanac_status_from_lr11xx = true; lr11xx_gnss_parse_context_status_buffer( almanac_obj.almanac_status_from_lr11xx, &context_status ); SMTC_MODEM_HAL_TRACE_PRINTF( "=> Almanac CRC: 0x%08X\n", context_status.global_almanac_crc ); - rp_task_abort( modem_get_rp( ), almanac_obj.rp_hook_id ); + rp_task_abort( modem_get_rp( 0 ), almanac_obj.rp_hook_id ); } static void request_access_to_rp_4_almanac_update( void ) @@ -304,7 +307,7 @@ static void request_access_to_rp_4_almanac_update( void ) rp_task.type = RP_TASK_TYPE_NONE; rp_task.launch_task_callbacks = rp_start_almanac_callback; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_ERROR( "Failed to enqueue RP task for almanac update\n" ); } diff --git a/lbm_lib/smtc_modem_core/modem_services/beacon_tx_service/lorawan_beacon_tx_service_example.c b/lbm_lib/smtc_modem_core/modem_services/beacon_tx_service/lorawan_beacon_tx_service_example.c index 0d90da13..65a62f1f 100644 --- a/lbm_lib/smtc_modem_core/modem_services/beacon_tx_service/lorawan_beacon_tx_service_example.c +++ b/lbm_lib/smtc_modem_core/modem_services/beacon_tx_service/lorawan_beacon_tx_service_example.c @@ -49,6 +49,9 @@ #include "radio_planner.h" #include "radio_planner_hook_id_defs.h" #include "modem_tx_protocol_manager.h" + +// EvaTODO: add multistack support + /* * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- @@ -127,8 +130,8 @@ void lorawan_beacon_tx_example_service_init( uint8_t* service_id, uint8_t task_i *on_launch_callback = lorawan_beacon_tx_example_service_on_launch; *on_update_callback = lorawan_beacon_tx_example_service_on_update; *context_callback = ( void* ) service_id; - rp_hook_init( modem_get_rp( ), RP_HOOK_ID_DIRECT_RP_ACCESS, ( void ( * )( void* ) )( end_user_beacon_callback ), - modem_get_rp( ) ); + rp_hook_init( modem_get_rp( 0 ), RP_HOOK_ID_DIRECT_RP_ACCESS, ( void ( * )( void* ) )( end_user_beacon_callback ), + modem_get_rp( 0 ) ); // lorawan_beacon_tx_example_add_task (CURRENT_STACK); } @@ -223,9 +226,9 @@ static void start_user_beacon_callback( void* context ) lora_param.pkt_params.preamble_len_in_symb = 10; smtc_modem_hal_start_radio_tcxo( ); - smtc_modem_hal_set_ant_switch( true ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( ralf_setup_lora( ( modem_get_rp( )->radio ), &lora_param ) == RAL_STATUS_OK ); - ral_set_dio_irq_params( &( modem_get_rp( )->radio->ral ), RAL_IRQ_TX_DONE ); + smtc_modem_hal_set_ant_switch( lorawan_beacon_tx_example_obj.stack_id, true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( ralf_setup_lora( ( modem_get_rp( 0 )->radio ), &lora_param ) == RAL_STATUS_OK ); + ral_set_dio_irq_params( &( modem_get_rp( 0 )->radio->ral ), RAL_IRQ_TX_DONE ); uint8_t payload[17]; memset( payload, 0, 17 ); @@ -236,8 +239,8 @@ static void start_user_beacon_callback( void* context ) uint16_t computed_crc = crc16_tx_beacon( payload, 6 ); payload[6] = ( computed_crc ) & 0x00FF; payload[7] = ( computed_crc >> 8 ) & 0x00FF; - ral_set_pkt_payload( &( modem_get_rp( )->radio->ral ), payload, 17 ); - ral_set_tx( &( modem_get_rp( )->radio->ral ) ); + ral_set_pkt_payload( &( modem_get_rp( 0 )->radio->ral ), payload, 17 ); + ral_set_tx( &( modem_get_rp( 0 )->radio->ral ) ); } static void end_user_beacon_callback( void* status ) @@ -264,7 +267,7 @@ static void start_user_beacon( void ) rp_task.type = RP_TASK_TYPE_TX_LORA; rp_task.launch_task_callbacks = start_user_beacon_callback; rp_radio_params_t fake_rp_radio_params = { 0 }; - if( rp_task_enqueue( modem_get_rp( ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) + if( rp_task_enqueue( modem_get_rp( 0 ), &rp_task, NULL, 0, &fake_rp_radio_params ) != RP_HOOK_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_PRINTF( "beacon START ERROR \n" ); } diff --git a/lbm_lib/smtc_modem_core/modem_services/cloud_dm_package/cloud_dm_package.c b/lbm_lib/smtc_modem_core/modem_services/cloud_dm_package/cloud_dm_package.c index 351343e3..e152eb7f 100644 --- a/lbm_lib/smtc_modem_core/modem_services/cloud_dm_package/cloud_dm_package.c +++ b/lbm_lib/smtc_modem_core/modem_services/cloud_dm_package/cloud_dm_package.c @@ -1212,7 +1212,7 @@ static dm_rc_t dm_reset( cloud_dm_t* ctx, dm_reset_code_t reset_code, uint16_t r { dm_rc_t ret = DM_OK; - uint32_t nb_reset = modem_get_reset_counter( ); + uint32_t nb_reset = modem_get_reset_counter( ctx->stack_id); if( reset_session == nb_reset ) { @@ -1406,7 +1406,7 @@ static bool dm_status_payload( cloud_dm_t* ctx, uint8_t stack_id, uint8_t* dm_up break; case DM_INFO_CHARGE: { uint32_t charge; - smtc_modem_get_charge( &charge ); + smtc_modem_get_charge(stack_id, &charge ); *p_tmp = charge & 0xFF; *( p_tmp + 1 ) = ( charge >> 8 ) & 0xFF; break; @@ -1486,7 +1486,7 @@ static bool dm_status_payload( cloud_dm_t* ctx, uint8_t stack_id, uint8_t* dm_up break; case DM_INFO_RSTCOUNT: { - uint32_t nb_reset = modem_get_reset_counter( ); + uint32_t nb_reset = modem_get_reset_counter( stack_id ); *p_tmp = nb_reset & 0xFF; *( p_tmp + 1 ) = nb_reset >> 8; break; @@ -1509,7 +1509,9 @@ static bool dm_status_payload( cloud_dm_t* ctx, uint8_t stack_id, uint8_t* dm_up case DM_INFO_CHIPEUI: { uint8_t p_tmp_chip_eui[8] = { 0 }; #if defined( USE_LR11XX_CE ) - lr11xx_system_read_uid( modem_get_radio_ctx( ), ( uint8_t* ) &p_tmp_chip_eui ); + if( modem_get_radio_type( stack_id ) == MODEM_RADIO_TYPE_LR11XX ) { + lr11xx_system_read_uid( modem_get_radio_ctx( stack_id ), ( uint8_t* ) &p_tmp_chip_eui ); + } #endif // USE_LR11XX_CE memcpy1_r( p_tmp, p_tmp_chip_eui, 8 ); break; diff --git a/lbm_lib/smtc_modem_core/modem_services/store_and_forward/store_and_forward.c b/lbm_lib/smtc_modem_core/modem_services/store_and_forward/store_and_forward.c index 76c4e725..1b952234 100644 --- a/lbm_lib/smtc_modem_core/modem_services/store_and_forward/store_and_forward.c +++ b/lbm_lib/smtc_modem_core/modem_services/store_and_forward/store_and_forward.c @@ -297,7 +297,7 @@ store_and_forward_rc_t store_and_forward_add_data( uint8_t fport, const uint8_t* SMTC_MODEM_HAL_TRACE_WARNING( "store and forward oldest data will be lost\n" ); } - if( fifo_ctrl_set( &ctx->fifo_ctrl_obj, data, data_len, &metadata, sizeof( store_and_forward_metadata_t ) ) != + if( fifo_ctrl_set( stack_id, &ctx->fifo_ctrl_obj, data, data_len, &metadata, sizeof( store_and_forward_metadata_t ) ) != FIFO_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_PRINTF( "store and forward fifo problem\n" ); @@ -364,7 +364,7 @@ static void store_and_forward_service_on_launch( void* service_id ) if( ( store_and_forward_obj[idx].sending_data_len == 0 ) && ( nb_of_data > 0 ) ) { - fifo_ctrl_get( &store_and_forward_obj[idx].fifo_ctrl_obj, store_and_forward_obj[idx].sending_data, + fifo_ctrl_get( stack_id, &store_and_forward_obj[idx].fifo_ctrl_obj, store_and_forward_obj[idx].sending_data, &store_and_forward_obj[idx].sending_data_len, SMTC_MODEM_MAX_LORAWAN_PAYLOAD_LENGTH, &store_and_forward_obj[idx].sending_metadata, &store_and_forward_obj[idx].sending_metadata_len, sizeof( store_and_forward_metadata_t ) ); diff --git a/lbm_lib/smtc_modem_core/modem_supervisor/modem_supervisor_light.c b/lbm_lib/smtc_modem_core/modem_supervisor/modem_supervisor_light.c index ae1835f9..6926beea 100644 --- a/lbm_lib/smtc_modem_core/modem_supervisor/modem_supervisor_light.c +++ b/lbm_lib/smtc_modem_core/modem_supervisor/modem_supervisor_light.c @@ -118,6 +118,7 @@ struct /* clang-format off */ #define task_manager modem_supervisor_context.task_manager +/* EvaTODO: do we need cb for each stack? */ #define supervisor_context_callback modem_supervisor_context.supervisor_context_callback #define supervisor_on_launch_func modem_supervisor_context.supervisor_on_launch_func #define supervisor_on_update_func modem_supervisor_context.supervisor_on_update_func @@ -243,6 +244,8 @@ task_valid_t modem_supervisor_add_task( smodem_task* task ) task_manager.modem_task[task_index].task_context = task->task_context; task_manager.modem_task[task_index].task_enabled = true; task_manager.modem_task[task_index].updated_locked = task->updated_locked; + SMTC_MODEM_HAL_TRACE_WARNING( "modem_supervisor_add_task id = %d , stack_id = %d , time_to_execute_s = %lu\n", + task->id, task->stack_id, task->time_to_execute_s ); return TASK_VALID; } SMTC_MODEM_HAL_TRACE_ERROR( "modem_supervisor_add_task id = %d unknown\n", task->id ); @@ -256,6 +259,7 @@ stask_manager* modem_supervisor_get_task( void ) // backoff_mobile_static( ); @todo// // todo check_class_b_to_generate_event( ); +// EvaTODO: Should we add mutex? uint32_t modem_supervisor_engine( void ) { @@ -302,7 +306,7 @@ uint32_t modem_supervisor_engine( void ) supervisor_on_launch_func[CURRENT_TASK_ID]( supervisor_context_callback[CURRENT_TASK_ID] ); task_manager.modem_task[task_manager.next_task_id].priority = TASK_FINISH; } - uint32_t alarm = modem_get_user_alarm( ); + uint32_t alarm = modem_get_user_alarm( task_manager.modem_task[task_manager.next_task_id].stack_id ); int32_t user_alarm_in_seconds = MODEM_MAX_ALARM_S / 1000; if( alarm != 0 ) { @@ -346,22 +350,31 @@ void modem_supervisor_set_modem_mute_with_priority_parameter( task_priority_t pr static uint32_t supervisor_check_user_alarm( void ) { - uint32_t alarm = modem_get_user_alarm( ); - int32_t user_alarm_in_seconds = MODEM_MAX_ALARM_S; - // manage the user alarm - if( alarm != 0 ) + int32_t min_user_alarm_in_seconds = MODEM_MAX_ALARM_S; + // Loop on all stacks to find the nearest user alarm + for ( uint8_t stack_id = 0; stack_id < NUMBER_OF_STACKS; stack_id++ ) { - user_alarm_in_seconds = ( int32_t ) ( alarm - smtc_modem_hal_get_time_in_s( ) ); - - if( user_alarm_in_seconds <= 0 ) - { - modem_set_user_alarm( 0 ); - user_alarm_in_seconds = MODEM_MAX_ALARM_S; - increment_asynchronous_msgnumber( SMTC_MODEM_EVENT_ALARM, 0, 0xFF ); - } + uint32_t alarm = modem_get_user_alarm( stack_id ); + int32_t user_alarm_in_seconds = MODEM_MAX_ALARM_S; + // manage the user alarm + if( alarm != 0 ) + { + user_alarm_in_seconds = ( int32_t ) ( alarm - smtc_modem_hal_get_time_in_s( ) ); + + if( user_alarm_in_seconds <= 0 ) + { + modem_set_user_alarm( stack_id, 0 ); + user_alarm_in_seconds = MODEM_MAX_ALARM_S; + increment_asynchronous_msgnumber( SMTC_MODEM_EVENT_ALARM, 0, stack_id ); + } + } + if( min_user_alarm_in_seconds > user_alarm_in_seconds ) + { + min_user_alarm_in_seconds = user_alarm_in_seconds; + } } - return ( uint32_t ) user_alarm_in_seconds; + return ( uint32_t ) min_user_alarm_in_seconds; } static uint32_t supervisor_run_lorawan_engine( uint8_t stack_id ) diff --git a/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.c b/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.c index db01ac25..030d2501 100755 --- a/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.c +++ b/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.c @@ -137,6 +137,8 @@ typedef enum tx_protocol_manager_state * --- PRIVATE VARIABLES ------------------------------------------------------- */ #define NB_REQUEST_ACCEPTED 5 + +//EvaTODO - we probably need a copy of this structure per stack if we want to support multi stack, for now only have multiple instances of rp and handle all in same tx manager static struct { tx_protocol_manager_tx_type_t current_tpm_request_type; @@ -144,7 +146,7 @@ static struct tx_protocol_manager_state_t tpm_list_of_state_to_execute[MAX_LIST_LENGTH]; bool current_tpm_transaction_is_a_retransmit; - radio_planner_t* current_tpm_rp_target; + radio_planner_t* current_tpm_rp_target[NUMBER_OF_STACKS]; uint8_t current_tpm_fport; bool current_tpm_fport_enabled; uint8_t current_tpm_data[242]; @@ -272,50 +274,54 @@ static status_lorawan_t ( *launch_tpm_func[TPM_NUMBER_OF_STATE] )( void ) = { * ----------------------------------------------------------------------------- * --- PUBLIC FUNCTIONS DEFINITION --------------------------------------------- */ + +void modem_tx_protocol_manager_init_common(void) +{ + memset( &modem_tpm_context, 0, sizeof( modem_tpm_context ) ); + current_tpm_transaction_is_a_retransmit = false; + current_tpm_transmit_at_time = false; + + reset_tpm_list( ); +} + /** * @brief call by smtc_modem_init to init the tx protocol manager * * @param rp pointer to the radioplanner object */ -void modem_tx_protocol_manager_init( radio_planner_t* rp ) +void modem_tx_protocol_manager_init( uint8_t stack_id, radio_planner_t* rp ) { - memset( &modem_tpm_context, 0, sizeof( modem_tpm_context ) ); - current_tpm_rp_target = rp; - current_tpm_transaction_is_a_retransmit = false; - current_tpm_transmit_at_time = false; - for( int i = 0; i < NUMBER_OF_STACKS; i++ ) - { - smtc_lbt_init( smtc_lbt_get_obj( i ), current_tpm_rp_target, RP_HOOK_ID_LBT + i, + + current_tpm_rp_target[stack_id] = rp; + smtc_lbt_init( smtc_lbt_get_obj( stack_id ), current_tpm_rp_target[stack_id], RP_HOOK_ID_LBT + stack_id, ( void ( * )( void* ) ) modem_tpm_radio_free_lbt, NULL, ( void ( * )( void* ) ) modem_tpm_radio_busy_lbt, NULL, ( void ( * )( void* ) ) modem_tpm_radio_abort_lbt, NULL ); - if( lorawan_api_stack_mac_get( i )->real->real_const.const_lbt_supported == true ) + if( lorawan_api_stack_mac_get( stack_id )->real->real_const.const_lbt_supported == true ) { - smtc_lbt_set_parameters( smtc_lbt_get_obj( lorawan_api_stack_mac_get( i )->stack_id ), - smtc_real_get_lbt_duration_ms( lorawan_api_stack_mac_get( i )->real ), - smtc_real_get_lbt_threshold_dbm( lorawan_api_stack_mac_get( i )->real ), - smtc_real_get_lbt_bw_hz( lorawan_api_stack_mac_get( i )->real ) ); - smtc_lbt_set_state( smtc_lbt_get_obj( lorawan_api_stack_mac_get( i )->stack_id ), true ); + smtc_lbt_set_parameters( smtc_lbt_get_obj( stack_id ), + smtc_real_get_lbt_duration_ms( lorawan_api_stack_mac_get( stack_id )->real ), + smtc_real_get_lbt_threshold_dbm( lorawan_api_stack_mac_get( stack_id )->real ), + smtc_real_get_lbt_bw_hz( lorawan_api_stack_mac_get( stack_id )->real ) ); + smtc_lbt_set_state( smtc_lbt_get_obj( lorawan_api_stack_mac_get( stack_id )->stack_id ), true ); } #if defined( ADD_CSMA ) - smtc_lora_cad_bt_init( smtc_cad_get_obj( i ), current_tpm_rp_target, RP_HOOK_ID_CAD + i, + smtc_lora_cad_bt_init( smtc_cad_get_obj( stack_id ), current_tpm_rp_target[stack_id], RP_HOOK_ID_CAD + stack_id, ( void ( * )( void* ) ) modem_tpm_radio_free_csma, NULL, ( void ( * )( void* ) ) modem_tpm_radio_busy_csma, NULL, ( void ( * )( void* ) ) modem_tpm_radio_free_cad_keep_channel, NULL, ( void ( * )( void* ) ) modem_tpm_radio_abort_csma, NULL ); #if defined( ENABLE_CSMA_BY_DEFAULT ) - smtc_lora_cad_bt_set_state( smtc_cad_get_obj( i ), true ); + smtc_lora_cad_bt_set_state( smtc_cad_get_obj( stack_id ), true ); #endif #endif #if defined( ADD_RELAY_TX ) - smtc_relay_tx_init( i, current_tpm_rp_target, lorawan_api_stack_mac_get( i )->real, + smtc_relay_tx_init( stack_id, current_tpm_rp_target[stack_id], lorawan_api_stack_mac_get( stack_id )->real, ( void ( * )( void* ) ) modem_tpm_radio_free_relay_tx, NULL, NULL, NULL, ( void ( * )( void* ) ) modem_tpm_radio_abort_relay_tx, NULL ); #endif - } - reset_tpm_list( ); } /** @@ -1185,7 +1191,7 @@ static uint32_t update_add_delay_ms( void ) uint32_t bw_hz; smtc_lbt_get_parameters( smtc_lbt_get_obj( current_tpm_stack_id ), &listen_duration_ms, &threshold_dbm, &bw_hz ); - current_tpm_add_delay_ms += ( listen_duration_ms ) + smtc_modem_hal_get_radio_tcxo_startup_delay_ms( ); + current_tpm_add_delay_ms += ( listen_duration_ms ) + smtc_modem_hal_get_radio_tcxo_startup_delay_ms( current_tpm_stack_id ); } current_tpm_add_delay_ms += LOG_MARGIN_DELAY; diff --git a/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.h b/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.h index 011d443c..ea59b96e 100644 --- a/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.h +++ b/lbm_lib/smtc_modem_core/modem_supervisor/modem_tx_protocol_manager.h @@ -71,12 +71,18 @@ typedef enum tx_protocol_manager_tx_type * --- PUBLIC FUNCTIONS PROTOTYPES --------------------------------------------- */ +/** + * @brief Init common functionalities of the tx protocol manager + * + */ +void modem_tx_protocol_manager_init_common( void ); + /*! - * \brief TPM Initialization + * \brief TPM Initialization by stack and radio planner * \remark This function is called by the modem's upper layer itself, it shouldn't be useful at the application layer * \retval None */ -void modem_tx_protocol_manager_init( radio_planner_t* rp ); +void modem_tx_protocol_manager_init( uint8_t stack_id, radio_planner_t* rp ); /*! * @brief tx_protocol_manager_request function is called each time a LoRaWAN transmission will be processed diff --git a/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.c b/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.c index 24a5ccf3..6939f905 100644 --- a/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.c +++ b/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.c @@ -118,24 +118,25 @@ uint16_t fifo_ctrl_get_free_space( const fifo_ctrl_t* ctrl ) return ctrl->free_space; } -fifo_return_status_t fifo_ctrl_get( fifo_ctrl_t* ctrl, uint8_t* buffer, uint16_t* data_len, +fifo_return_status_t fifo_ctrl_get( uint8_t stack_id, fifo_ctrl_t* ctrl, uint8_t* buffer, uint16_t* data_len, const uint16_t data_buffer_size, void* metadata, uint8_t* metadata_len, const uint8_t metadata_buffer_size ) { - smtc_modem_hal_disable_modem_irq( ); + smtc_modem_hal_disable_modem_irq( stack_id ); fifo_return_status_t ret = ctrl_get( ctrl, buffer, data_len, data_buffer_size, metadata, metadata_len, metadata_buffer_size ); - smtc_modem_hal_enable_modem_irq( ); + smtc_modem_hal_enable_modem_irq( stack_id ); return ret; } -fifo_return_status_t fifo_ctrl_set( fifo_ctrl_t* ctrl, const uint8_t* buffer, const uint16_t buffer_len, +fifo_return_status_t fifo_ctrl_set( uint8_t stack_id, fifo_ctrl_t* ctrl, const uint8_t* buffer, const uint16_t buffer_len, const void* metadata, const uint8_t metadata_len ) { - smtc_modem_hal_disable_modem_irq( ); + smtc_modem_hal_disable_modem_irq( stack_id ); fifo_return_status_t ret = ctrl_set( ctrl, buffer, buffer_len, metadata, metadata_len ); - smtc_modem_hal_enable_modem_irq( ); + smtc_modem_hal_enable_modem_irq( stack_id ); + return ret; } diff --git a/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.h b/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.h index 69f1f294..b69e3b61 100644 --- a/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.h +++ b/lbm_lib/smtc_modem_core/modem_utilities/fifo_ctrl.h @@ -134,6 +134,7 @@ uint16_t fifo_ctrl_get_free_space( const fifo_ctrl_t* ctrl ); /** * @brief Read oldest element in fifo * + * @param stack_id Stack identifier * @param ctrl fifo manager * @param buffer buffer to save data * @param data_len length of read data @@ -143,7 +144,7 @@ uint16_t fifo_ctrl_get_free_space( const fifo_ctrl_t* ctrl ); * @param metadata_buffer_size size of metadata buffer * @return fifo_return_status_t return status */ -fifo_return_status_t fifo_ctrl_get( fifo_ctrl_t* ctrl, uint8_t* buffer, uint16_t* data_len, +fifo_return_status_t fifo_ctrl_get( uint8_t stack_id, fifo_ctrl_t* ctrl, uint8_t* buffer, uint16_t* data_len, const uint16_t data_buffer_size, void* metadata, uint8_t* metadata_len, const uint8_t metadata_buffer_size ); @@ -151,6 +152,7 @@ fifo_return_status_t fifo_ctrl_get( fifo_ctrl_t* ctrl, uint8_t* buffer, uint16_t * @brief Save a new element in the fifo * If there is not enough free space, the oldest element will be removed * + * @param stack_id Stack identifier * @param ctrl fifo manager * @param buffer buffer to save * @param buffer_len size of buffer @@ -158,7 +160,7 @@ fifo_return_status_t fifo_ctrl_get( fifo_ctrl_t* ctrl, uint8_t* buffer, uint16_t * @param metadata_len length of metadata * @return fifo_return_status_t return status */ -fifo_return_status_t fifo_ctrl_set( fifo_ctrl_t* ctrl, const uint8_t* buffer, const uint16_t buffer_len, +fifo_return_status_t fifo_ctrl_set( uint8_t stack_id, fifo_ctrl_t* ctrl, const uint8_t* buffer, const uint16_t buffer_len, const void* metadata, const uint8_t metadata_len ); #ifdef __cplusplus diff --git a/lbm_lib/smtc_modem_core/modem_utilities/modem_core.c b/lbm_lib/smtc_modem_core/modem_utilities/modem_core.c index 6ee9a9a3..35b0f33d 100644 --- a/lbm_lib/smtc_modem_core/modem_utilities/modem_core.c +++ b/lbm_lib/smtc_modem_core/modem_utilities/modem_core.c @@ -108,7 +108,7 @@ typedef struct modem_ctx_s * ----------------------------------------------------------------------------- * --- PRIVATE VARIABLES ------------------------------------------------------- */ -struct +typedef struct modem_ctx_light_s { modem_downlink_msg_t modem_dwn_pkt; radio_planner_t* modem_rp; @@ -117,22 +117,14 @@ struct uint32_t user_alarm; fifo_ctrl_t fifo_ctrl_obj; uint8_t fifo_buffer[FIFO_LORAWAN_SIZE]; - uint8_t ( *downlink_services_callback[NUMBER_OF_SERVICES + NUMBER_OF_LORAWAN_MANAGEMENT_TASKS] )( - lr1_stack_mac_down_data_t* rx_down_data ); uint32_t modem_reset_counter; bool report_all_downlinks_to_user; -} modem_ctx_light; - -#define modem_dwn_pkt modem_ctx_light.modem_dwn_pkt -#define modem_rp modem_ctx_light.modem_rp -#define modem_radio_ctx modem_ctx_light.modem_radio_ctx -#define is_modem_in_test_mode modem_ctx_light.is_modem_in_test_mode -#define user_alarm modem_ctx_light.user_alarm -#define fifo_ctrl_obj modem_ctx_light.fifo_ctrl_obj -#define fifo_buffer modem_ctx_light.fifo_buffer -#define downlink_services_callback modem_ctx_light.downlink_services_callback -#define modem_reset_counter modem_ctx_light.modem_reset_counter -#define report_all_downlinks_to_user modem_ctx_light.report_all_downlinks_to_user +} modem_ctx_light_t; + +modem_ctx_light_t modem_ctx_light[NUMBER_OF_STACKS]; + +uint8_t ( *downlink_services_callback[NUMBER_OF_SERVICES + NUMBER_OF_LORAWAN_MANAGEMENT_TASKS] )( + lr1_stack_mac_down_data_t* rx_down_data ); /* * ----------------------------------------------------------------------------- @@ -146,129 +138,142 @@ static void modem_downlink_callback( lr1_stack_mac_down_data_t* rx_down_data ); * --- PUBLIC FUNCTIONS DEFINITION --------------------------------------------- */ -void modem_context_init_light( void ( *callback )( void ), radio_planner_t* rp ) +void modem_context_init_common( void ( *callback )( void ) ) { + modem_event_init( callback ); + void ( *callback_on_launch_temp )( void* ); void ( *callback_on_update_temp )( void* ); void* context_callback_tmp; - modem_rp = rp; - modem_event_init( callback ); - - // Init duty-cycle object to 0 - smtc_duty_cycle_init( ); - - for( uint8_t stack_id = 0; stack_id < NUMBER_OF_STACKS; stack_id++ ) - { - lorawan_api_init( rp, stack_id, ( void ( * )( lr1_stack_mac_down_data_t* ) ) modem_downlink_callback ); - - lorawan_api_dr_strategy_set( STATIC_ADR_MODE, stack_id ); - -#if defined ( STORE_JOIN_SESSION ) - SMTC_MODEM_HAL_TRACE_PRINTF( "Trying to restore join session.\n" ); - lorawan_api_join_session_restore( stack_id ); -#else - lorawan_api_join_status_clear( stack_id ); -#endif - - // to init duty cycle - smtc_real_region_types_t region = lorawan_api_get_region( stack_id ); - lorawan_api_set_region( region, stack_id ); - } - uint8_t index_tmp = 0; lorawan_send_management_services_init( ( uint8_t* ) UNUSED_VALUE, UNUSED_VALUE, - &downlink_services_callback[index_tmp++], &callback_on_launch_temp, - &callback_on_update_temp, &context_callback_tmp ); + &downlink_services_callback[index_tmp++], &callback_on_launch_temp, + &callback_on_update_temp, &context_callback_tmp ); modem_supervisor_init_callback( SEND_TASK, callback_on_launch_temp, callback_on_update_temp, context_callback_tmp ); lorawan_join_management_services_init( ( uint8_t* ) UNUSED_VALUE, UNUSED_VALUE, - &downlink_services_callback[index_tmp++], &callback_on_launch_temp, - &callback_on_update_temp, &context_callback_tmp ); + &downlink_services_callback[index_tmp++], &callback_on_launch_temp, + &callback_on_update_temp, &context_callback_tmp ); modem_supervisor_init_callback( JOIN_TASK, callback_on_launch_temp, callback_on_update_temp, context_callback_tmp ); lorawan_dwn_ack_management_init( ( uint8_t* ) UNUSED_VALUE, UNUSED_VALUE, &downlink_services_callback[index_tmp++], - &callback_on_launch_temp, &callback_on_update_temp, &context_callback_tmp ); + &callback_on_launch_temp, &callback_on_update_temp, &context_callback_tmp ); modem_supervisor_init_callback( RETRIEVE_DL_TASK, callback_on_launch_temp, callback_on_update_temp, - context_callback_tmp ); + context_callback_tmp ); lorawan_cid_request_management_init( ( uint8_t* ) UNUSED_VALUE, UNUSED_VALUE, - &downlink_services_callback[index_tmp++], &callback_on_launch_temp, - &callback_on_update_temp, &context_callback_tmp ); + &downlink_services_callback[index_tmp++], &callback_on_launch_temp, + &callback_on_update_temp, &context_callback_tmp ); modem_supervisor_init_callback( CID_REQ_TASK, callback_on_launch_temp, callback_on_update_temp, - context_callback_tmp ); + context_callback_tmp ); #ifdef ADD_CLASS_B lorawan_class_b_management_services_init( ( uint8_t* ) UNUSED_VALUE, UNUSED_VALUE, - &downlink_services_callback[index_tmp++], &callback_on_launch_temp, - &callback_on_update_temp, &context_callback_tmp ); + &downlink_services_callback[index_tmp++], &callback_on_launch_temp, + &callback_on_update_temp, &context_callback_tmp ); modem_supervisor_init_callback( CLASS_B_MANAGEMENT_TASK, callback_on_launch_temp, callback_on_update_temp, - context_callback_tmp ); + context_callback_tmp ); #endif - task_id_t task_id_tmp; - uint8_t cpt_of_services_init = SERVICE_ID0_TASK; - for( uint8_t i = 0; i < NUMBER_OF_SERVICES; i++ ) - { - task_id_tmp = cpt_of_services_init + ( NUMBER_OF_TASKS * modem_service_config[i].stack_id ); - modem_service_config[i].callbacks_init_service( - &modem_service_config[i].service_id, task_id_tmp, - &downlink_services_callback[i + NUMBER_OF_LORAWAN_MANAGEMENT_TASKS], &callback_on_launch_temp, - &callback_on_update_temp, &context_callback_tmp ); + for (uint8_t stack = 0; stack < NUMBER_OF_STACKS; stack++) + { + for (size_t i = 0; i < SERVICE_CONFIG_COUNT; i++) + { + size_t idx = stack * SERVICE_CONFIG_COUNT + i; + modem_service_config[idx] = service_config_template[i]; + modem_service_config[idx].stack_id = stack; + } + } + + task_id_t task_id_tmp; + uint8_t cpt_of_services_init = SERVICE_ID0_TASK; + for( uint8_t i = 0; i < NUMBER_OF_SERVICES; i++ ) + { + task_id_tmp = cpt_of_services_init + ( NUMBER_OF_TASKS * modem_service_config[i].stack_id ); + + modem_service_config[i].callbacks_init_service( + &modem_service_config[i].service_id, task_id_tmp, + &downlink_services_callback[i + NUMBER_OF_LORAWAN_MANAGEMENT_TASKS], &callback_on_launch_temp, + &callback_on_update_temp, &context_callback_tmp ); + + modem_supervisor_init_callback( cpt_of_services_init, callback_on_launch_temp, callback_on_update_temp, + context_callback_tmp ); + cpt_of_services_init++; + } +} - modem_supervisor_init_callback( cpt_of_services_init, callback_on_launch_temp, callback_on_update_temp, - context_callback_tmp ); - cpt_of_services_init++; - } +void modem_context_init_light( uint8_t stack_id, radio_planner_t* rp ) +{ + modem_ctx_light[stack_id].modem_rp = rp; - // save radio planner pointer for suspend/resume features + // Init duty-cycle object to 0 + smtc_duty_cycle_init( stack_id ); - is_modem_in_test_mode = false; - user_alarm = 0x7FFFFFFF; - fifo_ctrl_init( &fifo_ctrl_obj, fifo_buffer, FIFO_LORAWAN_SIZE ); + lorawan_api_init( rp, stack_id, ( void ( * )( lr1_stack_mac_down_data_t* ) ) modem_downlink_callback ); + + lorawan_api_dr_strategy_set( STATIC_ADR_MODE, stack_id ); + +#if defined ( STORE_JOIN_SESSION ) + SMTC_MODEM_HAL_TRACE_PRINTF( "Trying to restore join session.\n" ); + lorawan_api_join_session_restore( stack_id ); +#else + lorawan_api_join_status_clear( stack_id ); +#endif + + // EvaTODO check if cbs should be stack dependant + // to init duty cycle + smtc_real_region_types_t region = lorawan_api_get_region( stack_id ); + lorawan_api_set_region( region, stack_id ); + + modem_ctx_light[stack_id].is_modem_in_test_mode = false; + modem_ctx_light[stack_id].user_alarm = 0x7FFFFFFF; + + // save radio planner pointer for suspend/resume features + fifo_ctrl_init( &modem_ctx_light[stack_id].fifo_ctrl_obj, modem_ctx_light[stack_id].fifo_buffer, FIFO_LORAWAN_SIZE ); // load modem context - modem_load_modem_context( ); + modem_load_modem_context( stack_id ); // Increment reset counter - modem_reset_counter++; - report_all_downlinks_to_user = false; + modem_ctx_light[stack_id].modem_reset_counter++; + modem_ctx_light[stack_id].report_all_downlinks_to_user = false; // Save modem context - to keep the reset counter - modem_store_modem_context( ); + modem_store_modem_context( stack_id ); } -fifo_ctrl_t* modem_context_get_fifo_obj( void ) +fifo_ctrl_t* modem_context_get_fifo_obj( uint8_t stack_id ) { - return &fifo_ctrl_obj; + return &modem_ctx_light[stack_id].fifo_ctrl_obj; } -void modem_set_test_mode_status( bool enable ) +void modem_set_test_mode_status( uint8_t stack_id, bool enable ) { - is_modem_in_test_mode = enable; + modem_ctx_light[stack_id].is_modem_in_test_mode = enable; } -bool modem_get_test_mode_status( void ) +bool modem_get_test_mode_status( uint8_t stack_id ) { - return is_modem_in_test_mode; + return modem_ctx_light[stack_id].is_modem_in_test_mode; } -uint32_t modem_get_user_alarm( void ) +uint32_t modem_get_user_alarm( uint8_t stack_id ) { - return ( user_alarm ); + return ( modem_ctx_light[stack_id].user_alarm ); } -void modem_set_user_alarm( uint32_t alarm ) +void modem_set_user_alarm( uint8_t stack_id, uint32_t alarm ) { - user_alarm = alarm; + modem_ctx_light[stack_id].user_alarm = alarm; } -void modem_set_radio_ctx( const void* radio_ctx ) +void modem_set_radio_ctx( uint8_t stack_id, const void* radio_ctx ) { - modem_radio_ctx = radio_ctx; + modem_ctx_light[stack_id].modem_radio_ctx = radio_ctx; } -const void* modem_get_radio_ctx( void ) +const void* modem_get_radio_ctx( uint8_t stack_id ) { - return modem_radio_ctx; + return modem_ctx_light[stack_id].modem_radio_ctx; } -radio_planner_t* modem_get_rp( void ) +radio_planner_t* modem_get_rp( uint8_t stack_id ) { - return modem_rp; + return modem_ctx_light[stack_id].modem_rp; } void modem_empty_callback( void* ctx ) @@ -276,7 +281,7 @@ void modem_empty_callback( void* ctx ) // Use for suspend/resume radio access } -bool modem_suspend_radio_access( void ) +bool modem_suspend_radio_access( uint8_t stack_id ) { rp_radio_params_t fake_radio_params = { 0 }; @@ -291,7 +296,7 @@ bool modem_suspend_radio_access( void ) }; - rp_hook_status_t status = rp_task_enqueue( modem_rp, &rp_task, NULL, 0, &fake_radio_params ); + rp_hook_status_t status = rp_task_enqueue( modem_ctx_light[stack_id].modem_rp, &rp_task, NULL, 0, &fake_radio_params ); if( status != RP_HOOK_STATUS_OK ) { @@ -301,14 +306,14 @@ bool modem_suspend_radio_access( void ) return true; } -bool modem_resume_radio_access( void ) +bool modem_resume_radio_access( uint8_t stack_id ) { bool status = true; - if( rp_task_abort( modem_rp, RP_HOOK_ID_SUSPEND ) == RP_HOOK_STATUS_OK ) + if( rp_task_abort( modem_ctx_light[stack_id].modem_rp, RP_HOOK_ID_SUSPEND ) == RP_HOOK_STATUS_OK ) { // force a call of rp_callback to re-arbitrate the radio planner before the next loop - rp_callback( modem_rp ); + rp_callback( modem_ctx_light[stack_id].modem_rp ); } else { @@ -340,7 +345,7 @@ int32_t modem_duty_cycle_get_status( uint8_t stack_id ) int32_t region_dtc = 0; int32_t nwk_dtc = lorawan_api_next_network_free_duty_cycle_ms_get( stack_id ); - if( smtc_duty_cycle_enable_get( ) == SMTC_DTC_ENABLED ) + if( smtc_duty_cycle_enable_get( stack_id ) == SMTC_DTC_ENABLED ) { uint8_t number_of_freq = 0; uint32_t freq_list[16] = { 0 }; // Generally region with duty cycle support 16 channels only @@ -348,7 +353,7 @@ int32_t modem_duty_cycle_get_status( uint8_t stack_id ) if( lorawan_api_get_current_enabled_frequencies_list( &number_of_freq, freq_list, sizeof( freq_list ) / sizeof( freq_list[0] ), stack_id ) == true ) { - region_dtc = smtc_duty_cycle_get_next_free_time_ms( number_of_freq, freq_list ); + region_dtc = smtc_duty_cycle_get_next_free_time_ms( stack_id, number_of_freq, freq_list ); #if defined( ADD_RELAY_TX ) int32_t relay_region_dtc = smtc_relay_tx_free_duty_cycle_ms_get( stack_id ); @@ -412,62 +417,75 @@ uint8_t modem_get_status( uint8_t stack_id ) return ( modem_status ); } -void modem_store_modem_context( void ) +void modem_store_modem_context( uint8_t stack_id ) { modem_ctx_t ctx = { 0 }; + SMTC_MODEM_HAL_TRACE_INFO_DEBUG( "Storing modem context for stack id: %d with reset counter %lu on offset %d \n", stack_id, + modem_ctx_light[stack_id].modem_reset_counter, stack_id * sizeof( ctx ) ); + + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + // Restore current saved context - smtc_modem_hal_context_restore( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_restore( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // Check if some values have changed - if( ctx.reset_counter != modem_reset_counter ) + if( ctx.reset_counter != modem_ctx_light[stack_id].modem_reset_counter ) { - ctx.reset_counter = modem_reset_counter; + ctx.reset_counter = modem_ctx_light[stack_id].modem_reset_counter; ctx.crc = crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); - smtc_modem_hal_context_store( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_store( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // dummy context reading to ensure context store is done before exiting the function - smtc_modem_hal_context_restore( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_restore( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); } } -void modem_load_modem_context( void ) +void modem_load_modem_context( uint8_t stack_id ) { modem_ctx_t ctx = { 0 }; - smtc_modem_hal_context_restore( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + + smtc_modem_hal_context_restore( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); if( crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ) != ctx.crc ) { memset( &ctx, 0, sizeof( ctx ) ); ctx.crc = crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); - smtc_modem_hal_context_store( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_store( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // dummy context reading to ensure context store is done before exiting the function - smtc_modem_hal_context_restore( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_restore( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); } - modem_reset_counter = ctx.reset_counter; + SMTC_MODEM_HAL_TRACE_WARNING( "Modem context for stack id: %d loaded with reset counter %lu\n", stack_id, ctx.reset_counter ); + + modem_ctx_light[stack_id].modem_reset_counter = ctx.reset_counter; } -void modem_reset_modem_context( void ) +void modem_reset_modem_context( uint8_t stack_id ) { modem_ctx_t ctx = { 0 }; - smtc_modem_hal_context_store( CONTEXT_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + smtc_modem_hal_context_store( CONTEXT_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); } -uint32_t modem_get_reset_counter( void ) +uint32_t modem_get_reset_counter( uint8_t stack_id ) { - return modem_reset_counter; + return modem_ctx_light[stack_id].modem_reset_counter; } -void modem_set_report_all_downlinks_to_user( bool report_all_downlinks ) +void modem_set_report_all_downlinks_to_user( uint8_t stack_id, bool report_all_downlinks ) { - report_all_downlinks_to_user = report_all_downlinks; + modem_ctx_light[stack_id].report_all_downlinks_to_user = report_all_downlinks; } -bool modem_get_report_all_downlinks_to_user( void ) +bool modem_get_report_all_downlinks_to_user( uint8_t stack_id ) { - return report_all_downlinks_to_user; + return modem_ctx_light[stack_id].report_all_downlinks_to_user; } /* @@ -512,10 +530,10 @@ void modem_downlink_callback( lr1_stack_mac_down_data_t* rx_down_data ) } // none services used the downlink data for itself then push it into the user fifo - if( ( report_all_downlinks_to_user == true ) || + if( ( modem_ctx_light[rx_down_data->stack_id].report_all_downlinks_to_user == true ) || ( ( downlink_used_by_services == 0 ) && ( rx_down_data->rx_metadata.rx_fport != 0 ) ) ) { - if( fifo_ctrl_set( &fifo_ctrl_obj, rx_down_data->rx_payload, rx_down_data->rx_payload_size, &metadata, + if( fifo_ctrl_set( rx_down_data->stack_id, &modem_ctx_light[rx_down_data->stack_id].fifo_ctrl_obj, rx_down_data->rx_payload, rx_down_data->rx_payload_size, &metadata, sizeof( smtc_modem_dl_metadata_t ) ) != FIFO_STATUS_OK ) { SMTC_MODEM_HAL_TRACE_PRINTF( "Fifo problem\n" ); @@ -524,7 +542,7 @@ void modem_downlink_callback( lr1_stack_mac_down_data_t* rx_down_data ) else { increment_asynchronous_msgnumber( SMTC_MODEM_EVENT_DOWNDATA, 0, rx_down_data->stack_id ); - fifo_ctrl_print_stat( &fifo_ctrl_obj ); + fifo_ctrl_print_stat( &modem_ctx_light[rx_down_data->stack_id].fifo_ctrl_obj ); } } } diff --git a/lbm_lib/smtc_modem_core/modem_utilities/modem_core.h b/lbm_lib/smtc_modem_core/modem_utilities/modem_core.h index 1a0003e5..932d06bb 100644 --- a/lbm_lib/smtc_modem_core/modem_utilities/modem_core.h +++ b/lbm_lib/smtc_modem_core/modem_utilities/modem_core.h @@ -117,21 +117,28 @@ typedef struct modem_downlink_msg_s * ----------------------------------------------------------------------------- * --- PUBLIC FUNCTIONS PROTOTYPES --------------------------------------------- */ + /*! - * \brief Init modem context + * \brief Init modem context - common for all radios and stacks * \retval void */ -void modem_context_init_light( void ( *callback )( void ), radio_planner_t* rp ); +void modem_context_init_common( void ( *callback )( void ) ); + +/*! + * \brief Init modem context - stack and rp dependant + * \retval void + */ +void modem_context_init_light( uint8_t stack_id, radio_planner_t* rp ); /*! * \brief get modem fifo * \retval void */ -fifo_ctrl_t* modem_context_get_fifo_obj( void ); +fifo_ctrl_t* modem_context_get_fifo_obj( uint8_t stack_id ); -uint32_t modem_get_user_alarm( void ); +uint32_t modem_get_user_alarm( uint8_t stack_id ); -void modem_set_user_alarm( uint32_t alarm ); +void modem_set_user_alarm( uint8_t stack_id, uint32_t alarm ); /** * @brief Set the AppKey of the device @@ -148,50 +155,61 @@ modem_ctx_rc_t modem_set_appkey( const uint8_t app_key[16], uint8_t stack_id ); * * @param [in] enable */ -void modem_set_test_mode_status( bool enable ); +void modem_set_test_mode_status( uint8_t stack_id, bool enable ); /** * @brief Get test mode status * + * @param [in] stack_id Stack identifier + * * @return true * @return false */ -bool modem_get_test_mode_status( void ); +bool modem_get_test_mode_status( uint8_t stack_id ); /** * @brief set modem radio context * + * @param [in] stack_id Stack identifier * @param [in] radio_ctx Radio context */ -void modem_set_radio_ctx( const void* radio_ctx ); +void modem_set_radio_ctx( uint8_t stack_id, const void* radio_ctx ); /** * @brief get modem radio context * + * @param [in] stack_id Stack identifier + * * @return the pointer on radio context */ -const void* modem_get_radio_ctx( void ); +const void* modem_get_radio_ctx( uint8_t stack_id ); /** * @brief get modem rp * + * @param [in] stack_id Stack identifier + * * @return the pointer on radio planner */ -radio_planner_t* modem_get_rp( void ); +radio_planner_t* modem_get_rp( uint8_t stack_id ); /** * @brief Suspend radio access * + * @param [in] stack_id Stack identifier + * * @return true if operation was ok, false otherwise */ -bool modem_suspend_radio_access( void ); +bool modem_suspend_radio_access( uint8_t stack_id ); /** * @brief Resume radio access * + * @param [in] stack_id Stack identifier + * * @return true if operation was ok, false otherwise */ -bool modem_resume_radio_access( void ); +bool modem_resume_radio_access( uint8_t stack_id ); /*! * \brief Compute crc @@ -224,37 +242,46 @@ uint8_t modem_get_status( uint8_t stack_id ); /** * @brief Load current modem context */ -void modem_load_modem_context( void ); +void modem_load_modem_context( uint8_t stack_id ); /** * @brief Store modem context + * + * @param [in] stack_id Stack identifier */ -void modem_store_modem_context( void ); +void modem_store_modem_context( uint8_t stack_id ); /** * @brief Reset modem context + * + * @param [in] stack_id Stack identifier */ -void modem_reset_modem_context( void ); +void modem_reset_modem_context( uint8_t stack_id ); /** * @brief Get current reset counter value + * + * @param [in] stack_id Stack identifier */ -uint32_t modem_get_reset_counter( void ); +uint32_t modem_get_reset_counter( uint8_t stack_id ); /** * @brief Set a flag to report all received downlinks to the user with a Downlink event * + * @param [in] stack_id Stack identifier * @param report_all_downlinks */ -void modem_set_report_all_downlinks_to_user( bool report_all_downlinks ); +void modem_set_report_all_downlinks_to_user( uint8_t stack_id, bool report_all_downlinks ); /** * @brief Get flag status that report all received downlinks to the user with a Downlink event * + * @param [in] stack_id Stack identifier + * * @return true * @return false */ -bool modem_get_report_all_downlinks_to_user( void ); +bool modem_get_report_all_downlinks_to_user( uint8_t stack_id ); #ifdef __cplusplus } diff --git a/lbm_lib/smtc_modem_core/modem_utilities/modem_services_config.h b/lbm_lib/smtc_modem_core/modem_utilities/modem_services_config.h index d1dc14c3..65c1d894 100644 --- a/lbm_lib/smtc_modem_core/modem_utilities/modem_services_config.h +++ b/lbm_lib/smtc_modem_core/modem_utilities/modem_services_config.h @@ -111,55 +111,59 @@ typedef struct modem_service_config_s } modem_service_config_t; -static modem_service_config_t modem_service_config[] = { - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_certification_services_init }, +#define SERVICE_CONFIG_COUNT ( sizeof(service_config_template) / sizeof(service_config_template[0]) ) + +static modem_service_config_t service_config_template[] = { + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_certification_services_init }, #ifdef ADD_RELAY_RX - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_relay_rx_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_relay_rx_services_init }, #endif #if defined( ADD_RELAY_TX ) - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_relay_tx_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_relay_tx_services_init }, #endif #ifdef ADD_SMTC_ALC_SYNC - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_alcsync_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_alcsync_services_init }, #endif #ifdef ADD_FUOTA - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_fragmentation_package_services_init }, - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_remote_multicast_setup_package_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_fragmentation_package_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_remote_multicast_setup_package_services_init }, #ifdef ENABLE_FUOTA_FMP - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_fmp_package_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_fmp_package_services_init }, #endif #ifdef ENABLE_FUOTA_MPA - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_mpa_package_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_mpa_package_services_init }, #endif #endif #ifdef ADD_ALMANAC - { .service_id = 0, .stack_id = 0, .callbacks_init_service = almanac_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = almanac_services_init }, #endif #ifdef ADD_SMTC_STREAM - { .service_id = 0, .stack_id = 0, .callbacks_init_service = stream_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = stream_services_init }, #endif #ifdef ADD_SMTC_CLOUD_DEVICE_MANAGEMENT - { .service_id = 0, .stack_id = 0, .callbacks_init_service = cloud_dm_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = cloud_dm_services_init }, #endif #ifdef ADD_SMTC_LFU - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lfu_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lfu_services_init }, #endif #ifdef ADD_LBM_GEOLOCATION - { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_gnss_scan_services_init }, - { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_gnss_send_services_init }, - { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_gnss_almanac_services_init }, - { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_wifi_scan_services_init }, - { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_wifi_send_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_gnss_scan_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_gnss_send_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_gnss_almanac_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_wifi_scan_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = mw_wifi_send_services_init }, #endif #ifdef MODEM_BEACON_APP - { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_beacon_tx_example_service_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = lorawan_beacon_tx_example_service_init }, #endif #ifdef ADD_SMTC_STORE_AND_FORWARD - { .service_id = 0, .stack_id = 0, .callbacks_init_service = store_and_forward_flash_services_init }, + { .service_id = 0, .stack_id = 0, .callbacks_init_service = store_and_forward_flash_services_init }, // { .service_id = 0, .stack_id = 0, .callbacks_init_service = store_and_forward_services_init }, #endif }; +static modem_service_config_t modem_service_config[SERVICE_CONFIG_COUNT * NUMBER_OF_STACKS]; + #define NUMBER_OF_SERVICES ( sizeof modem_service_config / sizeof modem_service_config[0] ) #ifdef __cplusplus diff --git a/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.c b/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.c index 810b4824..0a83120e 100644 --- a/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.c +++ b/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.c @@ -206,10 +206,11 @@ static void rp_task_print( const radio_planner_t* rp, const rp_task_t* task ); * --- PUBLIC FUNCTIONS DEFINITION --------------------------------------------- */ -void rp_init( radio_planner_t* rp, const ralf_t* radio ) +void rp_init( uint8_t stack_id, radio_planner_t* rp, const ralf_t* radio ) { memset( rp, 0, sizeof( radio_planner_t ) ); rp->radio = radio; + rp->stack_id = stack_id; for( int32_t i = 0; i < RP_NB_HOOKS; i++ ) { @@ -520,7 +521,7 @@ void rp_callback( radio_planner_t* rp ) SMTC_MODEM_HAL_TRACE_PRINTF( " radio planner it but no more task activated\n" ); } - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id, false ); // Shut Down the TCXO smtc_modem_hal_stop_radio_tcxo( ); } @@ -687,13 +688,13 @@ static void rp_task_arbiter( radio_planner_t* rp, const char* caller_func_name ) rp->radio_irq_flag = false; - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( rp->stack_id, false ); // Shut Down the TCXO smtc_modem_hal_stop_radio_tcxo( ); rp_consumption_statistics_updated( rp, rp->radio_task_id, smtc_modem_hal_get_time_in_ms( ) ); rp->radio_task_id = rp->priority_task.hook_id; - if( smtc_modem_external_stack_currently_use_radio( ) == true ) + if( smtc_modem_external_stack_currently_use_radio( rp->stack_id ) == true ) { rp->tasks[rp->radio_task_id].state = RP_TASK_STATE_ABORTED; } @@ -707,7 +708,7 @@ static void rp_task_arbiter( radio_planner_t* rp, const char* caller_func_name ) else { // Radio is sleeping start priority task on radio rp->radio_task_id = rp->priority_task.hook_id; - if( smtc_modem_external_stack_currently_use_radio( ) == true ) + if( smtc_modem_external_stack_currently_use_radio( rp->stack_id ) == true ) { rp->tasks[rp->radio_task_id].state = RP_TASK_STATE_ABORTED; } @@ -1084,8 +1085,8 @@ rp_hook_status_t rp_get_pkt_payload( radio_planner_t* rp, const rp_task_t* task static void rp_set_alarm( radio_planner_t* rp, const uint32_t alarm_in_ms ) { - smtc_modem_hal_stop_timer( ); - smtc_modem_hal_start_timer( alarm_in_ms, rp_timer_irq_callback, rp ); + smtc_modem_hal_stop_timer( rp->stack_id ); + smtc_modem_hal_start_timer( rp->stack_id, alarm_in_ms, rp_timer_irq_callback, rp ); } static void rp_timer_irq( radio_planner_t* rp ) @@ -1228,7 +1229,7 @@ static void rp_consumption_statistics_updated( radio_planner_t* rp, const uint8_ rp_stats_update( &rp->stats, time, hook_id, micro_ampere_radio ); if( tx_timestamp_tmp != 0 ) { - smtc_duty_cycle_sum( tx_freq_hz, rp->stats.tx_last_toa_ms[hook_id] ); + smtc_duty_cycle_sum( rp->stack_id, tx_freq_hz, rp->stats.tx_last_toa_ms[hook_id] ); } } } diff --git a/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.h b/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.h index 135f4b9c..99e86a02 100644 --- a/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.h +++ b/lbm_lib/smtc_modem_core/radio_planner/src/radio_planner.h @@ -98,6 +98,7 @@ typedef struct radio_planner_s const ralf_t* radio; const ralf_t* radio_target_attached_to_this_hook[RP_NB_HOOKS]; uint32_t margin_delay; + uint8_t stack_id; } radio_planner_t; /* @@ -120,7 +121,7 @@ void rp_callback( radio_planner_t* rp ); /*! * */ -void rp_init( radio_planner_t* rp, const ralf_t* radio ); +void rp_init( uint8_t stack_id, radio_planner_t* rp, const ralf_t* radio ); /*! * diff --git a/lbm_lib/smtc_modem_core/smtc_modem.c b/lbm_lib/smtc_modem_core/smtc_modem.c index 06c49f9f..57b592ae 100644 --- a/lbm_lib/smtc_modem_core/smtc_modem.c +++ b/lbm_lib/smtc_modem_core/smtc_modem.c @@ -76,13 +76,17 @@ #if defined( SX128X ) #include "ralf_sx128x.h" -#elif defined( SX126X ) +#endif +#if defined( SX126X ) #include "ralf_sx126x.h" -#elif defined( LR11XX ) +#endif +#if defined( LR11XX ) #include "ralf_lr11xx.h" -#elif defined( SX127X ) +#endif +#if defined( SX127X ) #include "ralf_sx127x.h" -#elif defined( LR20XX ) +#endif +#if defined( LR20XX ) #include "ralf_lr20xx.h" #endif @@ -122,13 +126,14 @@ * ----------------------------------------------------------------------------- * --- PRIVATE MACROS----------------------------------------------------------- */ +// EvaTODO stack_id! #if defined( BYPASS_CHECK_TEST_MODE ) #define RETURN_BUSY_IF_TEST_MODE( ) #else #define RETURN_BUSY_IF_TEST_MODE( ) \ do \ { \ - if( true == modem_get_test_mode_status( ) ) \ + if( true == modem_get_test_mode_status( 0 ) ) \ { \ return SMTC_MODEM_RC_BUSY; \ } \ @@ -187,24 +192,15 @@ typedef struct modem_key_ctx_s * --- PRIVATE VARIABLES ------------------------------------------------------- */ -radio_planner_t modem_radio_planner; - -#if defined( SX128X ) -ralf_t modem_radio = RALF_SX128X_INSTANTIATE( NULL ); -#elif defined( SX126X ) -ralf_t modem_radio = RALF_SX126X_INSTANTIATE( NULL ); -#elif defined( LR11XX ) -ralf_t modem_radio = RALF_LR11XX_INSTANTIATE( NULL ); -#elif defined( LR20XX ) -ralf_t modem_radio = RALF_LR20XX_INSTANTIATE( NULL ); -#elif defined( SX127X ) +static radio_planner_t modem_radio_planner[NUMBER_OF_STACKS]; +static ralf_t modem_radio[NUMBER_OF_STACKS]; +static smtc_modem_radio_type_t modem_radio_type[NUMBER_OF_STACKS]; +#if defined( SX127X ) #include "sx127x.h" -static sx127x_t sx127x; -ralf_t modem_radio = RALF_SX127X_INSTANTIATE( &sx127x ); -#else -#error "Please select radio board.." +static sx127x_t sx127x[NUMBER_OF_STACKS]; #endif + struct { uint8_t modem_appkey_status; @@ -214,6 +210,7 @@ struct uint8_t modem_data_block_int_key[16]; } smtc_modem_key_ctx; +/* EvaTODO add multiple instances - for now this is applicable only for 1 lr11xx */ #define modem_appkey_status smtc_modem_key_ctx.modem_appkey_status #define modem_appkey_crc smtc_modem_key_ctx.modem_appkey_crc #define modem_gen_appkey_status smtc_modem_key_ctx.modem_gen_appkey_status @@ -237,8 +234,8 @@ void empty_callback( void* ctx ) } #if defined( USE_LR11XX_CE ) -static void modem_store_key_context( void ); -static void modem_load_appkey_context( void ); +static void modem_store_key_context( uint8_t stack_id ); +static void modem_load_appkey_context( uint8_t stack_id ); #endif /* @@ -247,64 +244,142 @@ static void modem_load_appkey_context( void ); */ /* ------------ Modem Utilities ------------*/ +void smtc_modem_init_common( void ( *callback_event )( void ) ) +{ + SMTC_MODEM_HAL_TRACE_INFO( "Modem Common Initialization\n" ); -void smtc_modem_init( void ( *callback_event )( void ) ) + modem_supervisor_init( ); + modem_context_init_common( callback_event ); + modem_tx_protocol_manager_init_common(); +} + +void smtc_modem_init( uint8_t stack_id ) { - SMTC_MODEM_HAL_TRACE_INFO( "Modem Initialization\n" ); + + SMTC_MODEM_HAL_TRACE_INFO( "Modem Initialization stack ID: %d\n", stack_id ); // init radio and put it in sleep mode - SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_reset( &( modem_radio.ral ) ) == RAL_STATUS_OK ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_init( &( modem_radio.ral ) ) == RAL_STATUS_OK ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_sleep( &( modem_radio.ral ), true ) == RAL_STATUS_OK ); - smtc_modem_hal_set_ant_switch( false ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_reset( &( modem_radio[stack_id].ral ) ) == RAL_STATUS_OK ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_init( &( modem_radio[stack_id].ral ) ) == RAL_STATUS_OK ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_sleep( &( modem_radio[stack_id].ral ), true ) == RAL_STATUS_OK ); + + smtc_modem_hal_set_ant_switch( stack_id, false ); // init radio planner and attach corresponding radio irq - rp_init( &modem_radio_planner, &modem_radio ); + rp_init( stack_id, &modem_radio_planner[stack_id], &modem_radio[stack_id] ); - smtc_modem_hal_irq_config_radio_irq( rp_radio_irq_callback, &modem_radio_planner ); + smtc_modem_hal_irq_config_radio_irq( stack_id, rp_radio_irq_callback, &modem_radio_planner[stack_id] ); - rp_hook_init( &modem_radio_planner, RP_HOOK_ID_SUSPEND, ( void ( * )( void* ) )( empty_callback ), - &modem_radio_planner ); + rp_hook_init( &modem_radio_planner[stack_id], RP_HOOK_ID_SUSPEND, ( void ( * )( void* ) )( empty_callback ), + &modem_radio_planner[stack_id] ); - smtc_secure_element_init( ); - modem_supervisor_init( ); - modem_context_init_light( callback_event, &modem_radio_planner ); - modem_tx_protocol_manager_init( &modem_radio_planner ); - // If lr11xx crypto engine is used for crypto + // EvaTODO -revisit, for now only soft ce is implemented + smtc_secure_element_init( stack_id ); + modem_context_init_light( stack_id, &modem_radio_planner[stack_id] ); + modem_tx_protocol_manager_init( stack_id, &modem_radio_planner[stack_id] ); + + // If lr11xx crypto engine is used for crypto and we have lr11xx radio, load appkey context #if defined( USE_LR11XX_CE ) - modem_load_appkey_context( ); + if( smtc_modem_get_radio_type( stack_id ) == SMTC_MODEM_RADIO_LR11XX ) + { + modem_load_appkey_context( stack_id ); + } #endif + // Is this going to work? // Event EVENT_RESET must be done at the end of init !! - increment_asynchronous_msgnumber( SMTC_MODEM_EVENT_RESET, 0, 0xFF ); + increment_asynchronous_msgnumber( SMTC_MODEM_EVENT_RESET, 0, stack_id ); + } uint32_t smtc_modem_run_engine( void ) { - rp_callback( &modem_radio_planner ); + // Loop over all stacks and update their radio planner + for( uint8_t i = 0; i < NUMBER_OF_STACKS; i++ ) + { + rp_callback( &modem_radio_planner[i] ); + } + return modem_supervisor_engine( ); } -void smtc_modem_set_radio_context( const void* radio_ctx ) +void smtc_modem_set_radio_context( uint8_t stack_id, smtc_modem_radio_type_t radio_type, const void* radio_ctx ) { -#if defined( SX1272 ) || defined( SX1276 ) - // update modem_radio context with provided one - ( ( sx127x_t* ) modem_radio.ral.context )->hal_context = radio_ctx; + switch( radio_type ) + { + case SMTC_MODEM_RADIO_LR11XX: +#if defined( LR11XX ) + modem_radio[stack_id] = (ralf_t)RALF_LR11XX_INSTANTIATE( NULL ); + // update modem_radio context with provided one + modem_radio[stack_id].ral.context = radio_ctx; + modem_radio_type[stack_id] = SMTC_MODEM_RADIO_LR11XX; + break; #else - // update modem_radio context with provided one - modem_radio.ral.context = radio_ctx; + SMTC_MODEM_HAL_TRACE_ERROR( "LR11XX radio type not supported in this build\n" ); + return; #endif - // Save modem radio context in case of direct access to radio by the modem - modem_set_radio_ctx( modem_radio.ral.context ); + case SMTC_MODEM_RADIO_LR20XX: +#if defined( LR20XX ) + modem_radio[stack_id] = (ralf_t)RALF_LR20XX_INSTANTIATE( NULL ); + // update modem_radio context with provided one + modem_radio[stack_id].ral.context = radio_ctx; + modem_radio_type[stack_id] = SMTC_MODEM_RADIO_LR20XX; + break; +#else + SMTC_MODEM_HAL_TRACE_ERROR( "LR20XX radio type not supported in this build\n" ); + return; +#endif + case SMTC_MODEM_RADIO_SX126X: +#if defined( SX126X ) + modem_radio[stack_id] = (ralf_t)RALF_SX126X_INSTANTIATE( NULL ); + // update modem_radio context with provided one + modem_radio[stack_id].ral.context = radio_ctx; + modem_radio_type[stack_id] = SMTC_MODEM_RADIO_SX126X; + break; +#else + SMTC_MODEM_HAL_TRACE_ERROR( "SX126X radio type not supported in this build\n" ); + return; +#endif + case SMTC_MODEM_RADIO_SX127X: +#if defined( SX127X ) + modem_radio[stack_id] = (ralf_t)RALF_SX127X_INSTANTIATE( &sx127x[i] ); + // update modem_radio context with provided one + ( ( sx127x_t* ) modem_radio[stack_id].ral.context )->hal_context = radio_ctx; + modem_radio_type[stack_id] = SMTC_MODEM_RADIO_SX127X; + break; +#else + SMTC_MODEM_HAL_TRACE_ERROR( "SX127X radio type not supported in this build\n" ); + return; +#endif + case SMTC_MODEM_RADIO_SX128X: +#if defined( SX128X ) + modem_radio[stack_id] = (ralf_t)RALF_SX128X_INSTANTIATE( NULL ); + // update modem_radio context with provided one + modem_radio[stack_id].ral.context = radio_ctx; + modem_radio_type[stack_id] = SMTC_MODEM_RADIO_SX128X; + break; +#else + SMTC_MODEM_HAL_TRACE_ERROR( "SX128X radio type not supported in this build\n" ); + return; +#endif + default: + SMTC_MODEM_HAL_TRACE_ERROR( "Unknown radio type\n" ); + return; + } } -const void* smtc_modem_get_radio_context( void ) +const void* smtc_modem_get_radio_context( uint8_t stack_id ) { // Get radio context - return modem_radio.ral.context; + return modem_radio[stack_id].ral.context; } -bool smtc_modem_is_irq_flag_pending( void ) +smtc_modem_radio_type_t smtc_modem_get_radio_type( uint8_t stack_id ) { - return rp_get_irq_flag( &modem_radio_planner ); + return modem_radio_type[stack_id]; +} + +bool smtc_modem_is_irq_flag_pending( uint8_t stack_id ) +{ + return rp_get_irq_flag( &modem_radio_planner[stack_id] ); } /* ------------ Modem Generic Api ------------*/ @@ -392,6 +467,10 @@ smtc_modem_return_code_t smtc_modem_set_deveui( uint8_t stack_id, const uint8_t smtc_modem_return_code_t smtc_modem_get_data_block_int_key( uint8_t stack_id, uint8_t data_block_int_key[SMTC_MODEM_KEY_LENGTH] ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } RETURN_BUSY_IF_TEST_MODE( ); RETURN_INVALID_IF_NULL( data_block_int_key ); @@ -405,6 +484,10 @@ smtc_modem_return_code_t smtc_modem_get_data_block_int_key( uint8_t stack_id, smtc_modem_return_code_t smtc_modem_derive_and_set_data_block_int_key( uint8_t stack_id, const uint8_t gen_appkey[SMTC_MODEM_KEY_LENGTH] ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } RETURN_BUSY_IF_TEST_MODE( ); RETURN_INVALID_IF_NULL( gen_appkey ); @@ -441,32 +524,39 @@ smtc_modem_return_code_t smtc_modem_set_appkey( uint8_t stack_id, const uint8_t // To prevent too much flash access first check crc on key in case of Hardware Secure element #if defined( USE_LR11XX_CE ) - uint32_t new_crc = crc( appkey, SMTC_MODEM_KEY_LENGTH ); - - if( ( modem_gen_appkey_status == MODEM_KEY_CRC_STATUS_INVALID ) || ( modem_gen_appkey_crc != new_crc ) ) + if(modem_radio_type[stack_id] == SMTC_MODEM_RADIO_LR11XX ) { - modem_gen_appkey_crc = new_crc; - modem_gen_appkey_status = MODEM_KEY_CRC_STATUS_VALID; -#endif - if( lorawan_api_set_appkey( appkey, stack_id ) != OKLORAWAN ) - { - return_code = SMTC_MODEM_RC_FAIL; - } -#if defined( USE_LR11XX_CE ) - else + uint32_t new_crc = crc( appkey, SMTC_MODEM_KEY_LENGTH ); + + if( ( modem_gen_appkey_status == MODEM_KEY_CRC_STATUS_INVALID ) || ( modem_gen_appkey_crc != new_crc ) ) { -#if defined( ADD_FUOTA ) && ( ADD_FUOTA == 2 ) - if( smtc_modem_derive_and_set_data_block_int_key( stack_id, appkey ) != SMTC_MODEM_RC_OK ) + modem_gen_appkey_crc = new_crc; + modem_gen_appkey_status = MODEM_KEY_CRC_STATUS_VALID; + if( lorawan_api_set_appkey( appkey, stack_id ) != OKLORAWAN ) { return_code = SMTC_MODEM_RC_FAIL; } + else + { +#if defined( ADD_FUOTA ) && ( ADD_FUOTA == 2 ) + if( smtc_modem_derive_and_set_data_block_int_key( stack_id, appkey ) != SMTC_MODEM_RC_OK ) + { + return_code = SMTC_MODEM_RC_FAIL; + } #endif - // Store appkey crc and status - modem_store_key_context( ); + // Store appkey crc and status + modem_store_key_context( stack_id ); + } } + return return_code; } #endif + + if( lorawan_api_set_appkey( appkey, stack_id ) != OKLORAWAN ) + { + return_code = SMTC_MODEM_RC_FAIL; + } return return_code; } @@ -488,25 +578,31 @@ smtc_modem_return_code_t smtc_modem_set_nwkkey( uint8_t stack_id, const uint8_t // To prevent too much flash access first check crc on key in case of Hardware Secure element #if defined( USE_LR11XX_CE ) - uint32_t new_crc = crc( nwkkey, SMTC_MODEM_KEY_LENGTH ); - - if( ( modem_appkey_status == MODEM_KEY_CRC_STATUS_INVALID ) || ( modem_appkey_crc != new_crc ) ) + if(modem_radio_type[stack_id] == SMTC_MODEM_RADIO_LR11XX ) { - modem_appkey_crc = new_crc; - modem_appkey_status = MODEM_KEY_CRC_STATUS_VALID; -#endif - if( lorawan_api_set_nwkkey( nwkkey, stack_id ) != OKLORAWAN ) - { - return_code = SMTC_MODEM_RC_FAIL; - } -#if defined( USE_LR11XX_CE ) - else + uint32_t new_crc = crc( nwkkey, SMTC_MODEM_KEY_LENGTH ); + + if( ( modem_appkey_status == MODEM_KEY_CRC_STATUS_INVALID ) || ( modem_appkey_crc != new_crc ) ) { - // Store appkey crc and status - modem_store_key_context( ); + modem_appkey_crc = new_crc; + modem_appkey_status = MODEM_KEY_CRC_STATUS_VALID; + if( lorawan_api_set_nwkkey( nwkkey, stack_id ) != OKLORAWAN ) + { + return_code = SMTC_MODEM_RC_FAIL; + } + else + { + // Store appkey crc and status + modem_store_key_context( stack_id ); + } } + return return_code; + } +#endif // USE_LR11XX_CE + if( lorawan_api_set_nwkkey( nwkkey, stack_id ) != OKLORAWAN ) + { + return_code = SMTC_MODEM_RC_FAIL; } -#endif return return_code; } @@ -805,7 +901,7 @@ smtc_modem_return_code_t smtc_modem_get_event( smtc_modem_event_t* event, uint8_ switch( event->event_type ) { case SMTC_MODEM_EVENT_RESET: - event->event_data.reset.count = ( uint16_t ) modem_get_reset_counter( ); + event->event_data.reset.count = ( uint16_t ) modem_get_reset_counter( stack_id ); break; case SMTC_MODEM_EVENT_TXDONE: event->event_data.txdone.status = @@ -925,7 +1021,8 @@ smtc_modem_return_code_t smtc_modem_get_downlink_data( uint8_t buff[SMTC_MODEM_ RETURN_INVALID_IF_NULL( remaining_data_nb ); smtc_modem_return_code_t rc = SMTC_MODEM_RC_OK; - fifo_ctrl_t* fifo_obj = modem_context_get_fifo_obj( ); + //EvaTODO support stack_id + fifo_ctrl_t* fifo_obj = modem_context_get_fifo_obj( metadata->stack_id ); // get_nb of data in fifo uint16_t nb_of_data = fifo_ctrl_get_nb_elt( fifo_obj ); @@ -939,7 +1036,7 @@ smtc_modem_return_code_t smtc_modem_get_downlink_data( uint8_t buff[SMTC_MODEM_ { uint8_t metadata_len; uint16_t fifo_buff_length = 0; - fifo_ctrl_get( fifo_obj, buff, &fifo_buff_length, SMTC_MODEM_MAX_LORAWAN_PAYLOAD_LENGTH, metadata, + fifo_ctrl_get( metadata->stack_id, fifo_obj, buff, &fifo_buff_length, SMTC_MODEM_MAX_LORAWAN_PAYLOAD_LENGTH, metadata, &metadata_len, sizeof( smtc_modem_dl_metadata_t ) ); // Length of LoRaWAN packet cannot exceed 242 *length = ( uint8_t ) fifo_buff_length; @@ -952,7 +1049,6 @@ smtc_modem_return_code_t smtc_modem_get_downlink_data( uint8_t buff[SMTC_MODEM_ smtc_modem_return_code_t smtc_modem_get_status( uint8_t stack_id, smtc_modem_status_mask_t* status_mask ) { - UNUSED( stack_id ); RETURN_BUSY_IF_TEST_MODE( ); RETURN_INVALID_IF_NULL( status_mask ); @@ -1135,79 +1231,77 @@ smtc_modem_return_code_t smtc_modem_get_suspend_radio_communications( uint8_t st return SMTC_MODEM_RC_OK; } -smtc_modem_return_code_t smtc_modem_suspend_radio_communications( bool suspend ) +smtc_modem_return_code_t smtc_modem_suspend_radio_communications( uint8_t stack_id, bool suspend ) { RETURN_BUSY_IF_TEST_MODE( ); bool local_rc = false; - for( uint8_t i = 0; i < NUMBER_OF_STACKS; i++ ) - { - modem_supervisor_set_modem_is_suspended( suspend, i ); - } + + modem_supervisor_set_modem_is_suspended( suspend, stack_id ); if( suspend == true ) { - for( uint8_t i = 0; i < NUMBER_OF_STACKS; i++ ) - { - smtc_modem_set_class( i, SMTC_MODEM_CLASS_A ); - lorawan_api_core_abort( i ); - } + + smtc_modem_set_class( stack_id, SMTC_MODEM_CLASS_A ); + lorawan_api_core_abort( stack_id ); + local_rc = modem_suspend_radio_access( stack_id ); + local_rc = modem_resume_radio_access( stack_id ); + local_rc = modem_suspend_radio_access( stack_id ); // First disable failsafe check for radio planner as the suspended task can be longer than failsafe value - rp_disable_failsafe( &modem_radio_planner, true ); - local_rc = modem_suspend_radio_access( ); - local_rc = modem_resume_radio_access( ); - local_rc = modem_suspend_radio_access( ); + rp_disable_failsafe( &modem_radio_planner[stack_id], true ); + } else { - local_rc = modem_resume_radio_access( ); + + local_rc = modem_resume_radio_access( stack_id ); // Re enable failsafe on radio planner - rp_disable_failsafe( &modem_radio_planner, false ); + rp_disable_failsafe( &modem_radio_planner[stack_id], false ); } return ( local_rc == true ) ? SMTC_MODEM_RC_OK : SMTC_MODEM_RC_FAIL; } -smtc_modem_return_code_t smtc_modem_alarm_start_timer( uint32_t alarm_s ) +smtc_modem_return_code_t smtc_modem_alarm_start_timer( uint8_t stack_id, uint32_t alarm_s ) { RETURN_BUSY_IF_TEST_MODE( ); if( alarm_s > MODEM_MAX_ALARM_VALUE_S ) { return SMTC_MODEM_RC_INVALID; } - modem_set_user_alarm( ( alarm_s > 0 ) ? ( smtc_modem_hal_get_time_in_s( ) + alarm_s ) : 0 ); + modem_set_user_alarm( stack_id, ( alarm_s > 0 ) ? ( smtc_modem_hal_get_time_in_s( ) + alarm_s ) : 0 ); return SMTC_MODEM_RC_OK; } -smtc_modem_return_code_t smtc_modem_alarm_clear_timer( void ) +smtc_modem_return_code_t smtc_modem_alarm_clear_timer( uint8_t stack_id ) { RETURN_BUSY_IF_TEST_MODE( ); - if( modem_get_user_alarm( ) == 0 ) + if( modem_get_user_alarm( stack_id ) == 0 ) { SMTC_MODEM_HAL_TRACE_WARNING( "Alarm clear timer impossible: no alarm timer is currently running\n" ) return SMTC_MODEM_RC_NOT_INIT; } else { - modem_set_user_alarm( 0 ); + modem_set_user_alarm( stack_id, 0 ); return SMTC_MODEM_RC_OK; } } -smtc_modem_return_code_t smtc_modem_alarm_get_remaining_time( uint32_t* remaining_time_in_s ) +smtc_modem_return_code_t smtc_modem_alarm_get_remaining_time( uint8_t stack_id, uint32_t* remaining_time_in_s ) { RETURN_BUSY_IF_TEST_MODE( ); RETURN_INVALID_IF_NULL( remaining_time_in_s ); - if( modem_get_user_alarm( ) == 0 ) + if( modem_get_user_alarm( stack_id ) == 0 ) { SMTC_MODEM_HAL_TRACE_WARNING( "Alarm get remaining impossible: no alarm timer is currently running\n" ) return SMTC_MODEM_RC_NOT_INIT; } else { - int32_t abs_remaining_time = ( int32_t ) ( modem_get_user_alarm( ) - smtc_modem_hal_get_time_in_s( ) ); + int32_t abs_remaining_time = ( int32_t ) ( modem_get_user_alarm( stack_id ) - smtc_modem_hal_get_time_in_s( ) ); *remaining_time_in_s = ( abs_remaining_time > 0 ) ? ( abs_remaining_time ) : 0; return SMTC_MODEM_RC_OK; @@ -1219,6 +1313,10 @@ smtc_modem_return_code_t smtc_modem_alarm_get_remaining_time( uint32_t* remainin #if defined( USE_LR11XX_CE ) smtc_modem_return_code_t smtc_modem_get_pin( uint8_t stack_id, uint8_t chip_pin[4] ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } RETURN_BUSY_IF_TEST_MODE( ); smtc_modem_status_mask_t status_mask = modem_get_status( stack_id ); if( ( ( status_mask & SMTC_MODEM_STATUS_JOINED ) == SMTC_MODEM_STATUS_JOINED ) || @@ -1240,13 +1338,13 @@ smtc_modem_return_code_t smtc_modem_get_pin( uint8_t stack_id, uint8_t chip_pin[ return SMTC_MODEM_RC_FAIL; } - lr11xx_status_t status = lr11xx_system_read_pin_custom_eui( modem_get_radio_ctx( ), deveui, joineui, 0, chip_pin ); + lr11xx_status_t status = lr11xx_system_read_pin_custom_eui( modem_get_radio_ctx( stack_id ), deveui, joineui, 0, chip_pin ); // when pin code is read, a new key derivation is done in lr11xx so a external key is used it will be lost // and shall be updated once more. Corrupt the key crc so that update is possible modem_appkey_status = MODEM_KEY_CRC_STATUS_INVALID; modem_gen_appkey_status = MODEM_KEY_CRC_STATUS_INVALID; - modem_store_key_context( ); + modem_store_key_context( stack_id ); if( status != LR11XX_STATUS_OK ) { @@ -1257,9 +1355,13 @@ smtc_modem_return_code_t smtc_modem_get_pin( uint8_t stack_id, uint8_t chip_pin[ smtc_modem_return_code_t smtc_modem_get_chip_eui( uint8_t stack_id, uint8_t chip_eui[8] ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } RETURN_BUSY_IF_TEST_MODE( ); - lr11xx_status_t status = lr11xx_system_read_uid( modem_get_radio_ctx( ), chip_eui ); + lr11xx_status_t status = lr11xx_system_read_uid( modem_get_radio_ctx( stack_id ), chip_eui ); if( status != LR11XX_STATUS_OK ) { @@ -1270,6 +1372,10 @@ smtc_modem_return_code_t smtc_modem_get_chip_eui( uint8_t stack_id, uint8_t chip smtc_modem_return_code_t smtc_modem_derive_keys( uint8_t stack_id ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } RETURN_BUSY_IF_TEST_MODE( ); smtc_modem_status_mask_t status_mask = modem_get_status( stack_id ); if( ( ( status_mask & SMTC_MODEM_STATUS_JOINED ) == SMTC_MODEM_STATUS_JOINED ) || @@ -1293,13 +1399,13 @@ smtc_modem_return_code_t smtc_modem_derive_keys( uint8_t stack_id ) } // Read pin code with current EUIs forces a key derivation - lr11xx_status_t status = lr11xx_system_read_pin_custom_eui( modem_get_radio_ctx( ), deveui, joineui, 0, pin ); + lr11xx_status_t status = lr11xx_system_read_pin_custom_eui( modem_get_radio_ctx( stack_id ), deveui, joineui, 0, pin ); // when pin code is read, a new key derivation is done in lr11xx so a external key is used it will be lost // and shall be updated once more. Corrupt the key crc so that update is possible modem_appkey_status = MODEM_KEY_CRC_STATUS_INVALID; modem_gen_appkey_status = MODEM_KEY_CRC_STATUS_INVALID; - modem_store_key_context( ); + modem_store_key_context( stack_id ); if( status != LR11XX_STATUS_OK ) { @@ -1310,6 +1416,10 @@ smtc_modem_return_code_t smtc_modem_derive_keys( uint8_t stack_id ) smtc_modem_return_code_t smtc_modem_secure_element_restore_context(uint8_t stack_id) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } smtc_modem_return_code_t return_code = SMTC_MODEM_RC_OK; if( smtc_secure_element_restore_context(stack_id) != SMTC_SE_RC_SUCCESS ) @@ -1322,6 +1432,10 @@ smtc_modem_return_code_t smtc_modem_secure_element_restore_context(uint8_t stack smtc_modem_return_code_t smtc_modem_secure_element_store_context(uint8_t stack_id) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return SMTC_MODEM_RC_INVALID; + } smtc_modem_return_code_t return_code = SMTC_MODEM_RC_OK; if( smtc_secure_element_store_context(stack_id) != SMTC_SE_RC_SUCCESS ) @@ -1415,7 +1529,7 @@ smtc_modem_return_code_t smtc_modem_get_adr_ack_limit_delay( uint8_t stack_id, u smtc_modem_return_code_t smtc_modem_set_report_all_downlinks_to_user( uint8_t stack_id, bool report_all_downlinks ) { RETURN_BUSY_IF_TEST_MODE( ); - modem_set_report_all_downlinks_to_user( report_all_downlinks ); + modem_set_report_all_downlinks_to_user( stack_id, report_all_downlinks ); return SMTC_MODEM_RC_OK; } @@ -1423,7 +1537,7 @@ smtc_modem_return_code_t smtc_modem_get_report_all_downlinks_to_user( uint8_t st { RETURN_BUSY_IF_TEST_MODE( ); RETURN_INVALID_IF_NULL( report_all_downlinks ); - *report_all_downlinks = modem_get_report_all_downlinks_to_user( ); + *report_all_downlinks = modem_get_report_all_downlinks_to_user( stack_id ); return SMTC_MODEM_RC_OK; } @@ -1530,16 +1644,16 @@ smtc_modem_return_code_t smtc_modem_csma_get_parameters( uint8_t stack_id, uint8 } #endif // ADD_CSMA -smtc_modem_return_code_t smtc_modem_get_charge( uint32_t* charge_mah ) +smtc_modem_return_code_t smtc_modem_get_charge( uint8_t stack_id, uint32_t* charge_mah ) { RETURN_INVALID_IF_NULL( charge_mah ); - *charge_mah = rp_stats_get_charge_mah( &modem_radio_planner.stats ); + *charge_mah = rp_stats_get_charge_mah( &modem_radio_planner[stack_id].stats ); return SMTC_MODEM_RC_OK; } -smtc_modem_return_code_t smtc_modem_get_rp_stats_to_array( uint8_t* stats_array, uint16_t* stats_array_length ) +smtc_modem_return_code_t smtc_modem_get_rp_stats_to_array( uint8_t stack_id, uint8_t* stats_array, uint16_t* stats_array_length ) { RETURN_INVALID_IF_NULL( stats_array ); RETURN_INVALID_IF_NULL( stats_array_length ); @@ -1548,45 +1662,45 @@ smtc_modem_return_code_t smtc_modem_get_rp_stats_to_array( uint8_t* stats_array, for( uint8_t i = 0; i < RP_NB_HOOKS; i++ ) { - stats_array[*stats_array_length + 0] = ( modem_radio_planner.stats.tx_last_toa_ms[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 1] = ( modem_radio_planner.stats.tx_last_toa_ms[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 2] = ( modem_radio_planner.stats.tx_last_toa_ms[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 3] = ( modem_radio_planner.stats.tx_last_toa_ms[i] & 0xFF ); - - stats_array[*stats_array_length + 4] = ( modem_radio_planner.stats.rx_last_toa_ms[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 5] = ( modem_radio_planner.stats.rx_last_toa_ms[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 6] = ( modem_radio_planner.stats.rx_last_toa_ms[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 7] = ( modem_radio_planner.stats.rx_last_toa_ms[i] & 0xFF ); - - stats_array[*stats_array_length + 8] = ( modem_radio_planner.stats.tx_consumption_ms[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 9] = ( modem_radio_planner.stats.tx_consumption_ms[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 10] = ( modem_radio_planner.stats.tx_consumption_ms[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 11] = ( modem_radio_planner.stats.tx_consumption_ms[i] & 0xFF ); - - stats_array[*stats_array_length + 12] = ( modem_radio_planner.stats.rx_consumption_ms[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 13] = ( modem_radio_planner.stats.rx_consumption_ms[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 14] = ( modem_radio_planner.stats.rx_consumption_ms[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 15] = ( modem_radio_planner.stats.rx_consumption_ms[i] & 0xFF ); - - stats_array[*stats_array_length + 16] = ( modem_radio_planner.stats.none_consumption_ms[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 17] = ( modem_radio_planner.stats.none_consumption_ms[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 18] = ( modem_radio_planner.stats.none_consumption_ms[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 19] = ( modem_radio_planner.stats.none_consumption_ms[i] & 0xFF ); - - stats_array[*stats_array_length + 20] = ( modem_radio_planner.stats.tx_consumption_ma[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 21] = ( modem_radio_planner.stats.tx_consumption_ma[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 22] = ( modem_radio_planner.stats.tx_consumption_ma[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 23] = ( modem_radio_planner.stats.tx_consumption_ma[i] & 0xFF ); - - stats_array[*stats_array_length + 24] = ( modem_radio_planner.stats.rx_consumption_ma[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 25] = ( modem_radio_planner.stats.rx_consumption_ma[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 26] = ( modem_radio_planner.stats.rx_consumption_ma[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 27] = ( modem_radio_planner.stats.rx_consumption_ma[i] & 0xFF ); - - stats_array[*stats_array_length + 28] = ( modem_radio_planner.stats.none_consumption_ma[i] >> 24 ) & 0xFF; - stats_array[*stats_array_length + 29] = ( modem_radio_planner.stats.none_consumption_ma[i] >> 16 ) & 0xFF; - stats_array[*stats_array_length + 30] = ( modem_radio_planner.stats.none_consumption_ma[i] >> 8 ) & 0xFF; - stats_array[*stats_array_length + 31] = ( modem_radio_planner.stats.none_consumption_ma[i] & 0xFF ); + stats_array[*stats_array_length + 0] = ( modem_radio_planner[stack_id].stats.tx_last_toa_ms[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 1] = ( modem_radio_planner[stack_id].stats.tx_last_toa_ms[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 2] = ( modem_radio_planner[stack_id].stats.tx_last_toa_ms[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 3] = ( modem_radio_planner[stack_id].stats.tx_last_toa_ms[i] & 0xFF ); + + stats_array[*stats_array_length + 4] = ( modem_radio_planner[stack_id].stats.rx_last_toa_ms[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 5] = ( modem_radio_planner[stack_id].stats.rx_last_toa_ms[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 6] = ( modem_radio_planner[stack_id].stats.rx_last_toa_ms[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 7] = ( modem_radio_planner[stack_id].stats.rx_last_toa_ms[i] & 0xFF ); + + stats_array[*stats_array_length + 8] = ( modem_radio_planner[stack_id].stats.tx_consumption_ms[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 9] = ( modem_radio_planner[stack_id].stats.tx_consumption_ms[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 10] = ( modem_radio_planner[stack_id].stats.tx_consumption_ms[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 11] = ( modem_radio_planner[stack_id].stats.tx_consumption_ms[i] & 0xFF ); + + stats_array[*stats_array_length + 12] = ( modem_radio_planner[stack_id].stats.rx_consumption_ms[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 13] = ( modem_radio_planner[stack_id].stats.rx_consumption_ms[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 14] = ( modem_radio_planner[stack_id].stats.rx_consumption_ms[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 15] = ( modem_radio_planner[stack_id].stats.rx_consumption_ms[i] & 0xFF ); + + stats_array[*stats_array_length + 16] = ( modem_radio_planner[stack_id].stats.none_consumption_ms[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 17] = ( modem_radio_planner[stack_id].stats.none_consumption_ms[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 18] = ( modem_radio_planner[stack_id].stats.none_consumption_ms[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 19] = ( modem_radio_planner[stack_id].stats.none_consumption_ms[i] & 0xFF ); + + stats_array[*stats_array_length + 20] = ( modem_radio_planner[stack_id].stats.tx_consumption_ma[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 21] = ( modem_radio_planner[stack_id].stats.tx_consumption_ma[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 22] = ( modem_radio_planner[stack_id].stats.tx_consumption_ma[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 23] = ( modem_radio_planner[stack_id].stats.tx_consumption_ma[i] & 0xFF ); + + stats_array[*stats_array_length + 24] = ( modem_radio_planner[stack_id].stats.rx_consumption_ma[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 25] = ( modem_radio_planner[stack_id].stats.rx_consumption_ma[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 26] = ( modem_radio_planner[stack_id].stats.rx_consumption_ma[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 27] = ( modem_radio_planner[stack_id].stats.rx_consumption_ma[i] & 0xFF ); + + stats_array[*stats_array_length + 28] = ( modem_radio_planner[stack_id].stats.none_consumption_ma[i] >> 24 ) & 0xFF; + stats_array[*stats_array_length + 29] = ( modem_radio_planner[stack_id].stats.none_consumption_ma[i] >> 16 ) & 0xFF; + stats_array[*stats_array_length + 30] = ( modem_radio_planner[stack_id].stats.none_consumption_ma[i] >> 8 ) & 0xFF; + stats_array[*stats_array_length + 31] = ( modem_radio_planner[stack_id].stats.none_consumption_ma[i] & 0xFF ); *stats_array_length += 32; } @@ -1594,9 +1708,9 @@ smtc_modem_return_code_t smtc_modem_get_rp_stats_to_array( uint8_t* stats_array, return SMTC_MODEM_RC_OK; } -smtc_modem_return_code_t smtc_modem_reset_charge( void ) +smtc_modem_return_code_t smtc_modem_reset_charge( uint8_t stack_id ) { - rp_stats_init( &modem_radio_planner.stats ); + rp_stats_init( &modem_radio_planner[stack_id].stats ); return SMTC_MODEM_RC_OK; } @@ -2270,22 +2384,21 @@ smtc_modem_return_code_t smtc_modem_gnss_send_mode( uint8_t stack_id, smtc_modem smtc_modem_return_code_t smtc_modem_almanac_full_update( uint8_t stack_id, const uint8_t* almanac, uint16_t almanac_size ) { - UNUSED( stack_id ); RETURN_BUSY_IF_TEST_MODE( ); RETURN_INVALID_IF_NULL( almanac ); /* Suspend the modem to get radio access */ - if( modem_suspend_radio_access( ) == false ) + if( modem_suspend_radio_access( stack_id ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "smtc_modem_almanac_full_update: failed to suspend modem\n" ); return SMTC_MODEM_RC_FAIL; } - if( modem_resume_radio_access( ) == false ) + if( modem_resume_radio_access( stack_id ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "smtc_modem_almanac_full_update: failed to resume modem\n" ); return SMTC_MODEM_RC_FAIL; } - if( modem_suspend_radio_access( ) == false ) + if( modem_suspend_radio_access( stack_id ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "smtc_modem_almanac_full_update: failed to suspend modem\n" ); return SMTC_MODEM_RC_FAIL; @@ -2299,7 +2412,7 @@ smtc_modem_return_code_t smtc_modem_almanac_full_update( uint8_t stack_id, const } /* Resume the modem */ - if( modem_resume_radio_access( ) == false ) + if( modem_resume_radio_access( stack_id ) == false ) { SMTC_MODEM_HAL_TRACE_ERROR( "smtc_modem_almanac_full_update: failed to resume modem\n" ); return SMTC_MODEM_RC_FAIL; @@ -2448,11 +2561,11 @@ void smtc_modem_wifi_set_payload_format( uint8_t stack_id, smtc_modem_wifi_paylo * ---------------------- DEBUG PURPOSE FUNCTIONS ----------------------------- */ -smtc_modem_return_code_t smtc_modem_debug_set_duty_cycle_state( bool enable ) +smtc_modem_return_code_t smtc_modem_debug_set_duty_cycle_state( uint8_t stack_id, bool enable ) { RETURN_BUSY_IF_TEST_MODE( ); - if( smtc_duty_cycle_enable_set( ( enable == false ) ? SMTC_DTC_FULL_DISABLED : SMTC_DTC_ENABLED ) != SMTC_DTC_OK ) + if( smtc_duty_cycle_enable_set( stack_id, ( enable == false ) ? SMTC_DTC_FULL_DISABLED : SMTC_DTC_ENABLED ) != SMTC_DTC_OK ) { return SMTC_MODEM_RC_FAIL; } @@ -2967,12 +3080,19 @@ static smtc_modem_return_code_t smtc_modem_custom_dr_distribution_to_tab( } #if defined( USE_LR11XX_CE ) -static void modem_store_key_context( void ) +static void modem_store_key_context( uint8_t stack_id ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return; + } modem_key_ctx_t ctx = { 0 }; + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + // Restore current saved context - smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // Check if some values have changed if( ( ctx.appkey_crc != modem_appkey_crc ) || ( ctx.appkey_crc_status != modem_appkey_status ) || @@ -2985,16 +3105,22 @@ static void modem_store_key_context( void ) memcpy( ctx.data_block_int_key, modem_data_block_int_key, SMTC_MODEM_KEY_LENGTH ); ctx.crc = crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); - smtc_modem_hal_context_store( CONTEXT_KEY_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_store( CONTEXT_KEY_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // dummy context reading to ensure context store is done before exiting the function - smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); } } -static void modem_load_appkey_context( void ) +static void modem_load_appkey_context( uint8_t stack_id ) { + if(modem_radio_type[stack_id] != SMTC_MODEM_RADIO_LR11XX) + { + return; + } modem_key_ctx_t ctx; - smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); if( crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ) == ctx.crc ) { @@ -3013,12 +3139,12 @@ static void modem_load_appkey_context( void ) ctx.gen_appkey_crc_status = MODEM_KEY_CRC_STATUS_INVALID; ctx.crc = crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); - smtc_modem_hal_context_store( CONTEXT_KEY_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_store( CONTEXT_KEY_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); // dummy context reading to ensure context store is done before exiting the function - smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_restore( CONTEXT_KEY_MODEM, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); } } -#endif +#endif // USE_LR11XX_CE #if defined( ADD_RELAY_TX ) smtc_modem_return_code_t smtc_modem_relay_tx_get_activation_mode( uint8_t stack_id, @@ -3082,8 +3208,8 @@ smtc_modem_return_code_t smtc_modem_relay_tx_disable( uint8_t stack_id ) #endif -bool smtc_modem_radio_is_free( void ) +bool smtc_modem_radio_is_free( uint8_t stack_id ) { - return rp_radio_is_free( &modem_radio_planner ); + return rp_radio_is_free( &modem_radio_planner[stack_id] ); } /* --- EOF ------------------------------------------------------------------ */ diff --git a/lbm_lib/smtc_modem_core/smtc_modem_crypto/lr11xx_crypto_engine/lr11xx_ce.c b/lbm_lib/smtc_modem_core/smtc_modem_crypto/lr11xx_crypto_engine/lr11xx_ce.c index 06eda352..c4311d07 100644 --- a/lbm_lib/smtc_modem_core/smtc_modem_crypto/lr11xx_crypto_engine/lr11xx_ce.c +++ b/lbm_lib/smtc_modem_core/smtc_modem_crypto/lr11xx_crypto_engine/lr11xx_ce.c @@ -114,8 +114,9 @@ typedef struct lr11xx_ce_context_nvm_s * --- PRIVATE VARIABLES ------------------------------------------------------- */ -static lr11xx_ce_data_t lr11xx_ce_data; -static const void* lr11xx_ctx; +/* EvaTODO: implement multi chip support - for now just disable lr11xxce */ +static lr11xx_ce_data_t lr11xx_ce_data[NUMBER_OF_STACKS] = { 0 }; +static const void* lr11xx_ctx[NUMBER_OF_STACKS]; /* * ----------------------------------------------------------------------------- @@ -144,30 +145,30 @@ uint32_t lr11xx_ce_crc( const uint8_t* buf, int len ); * --- PUBLIC FUNCTIONS DEFINITION --------------------------------------------- */ -smtc_se_return_code_t smtc_secure_element_init( void ) +smtc_se_return_code_t smtc_secure_element_init( uint8_t stack_id ) { SMTC_MODEM_HAL_TRACE_INFO( "Use lr11xx crypto engine for cryptographic functionalities\n" ); lr11xx_crypto_status_t lr11xx_crypto_status = LR11XX_CRYPTO_STATUS_ERROR; // Initialize data structure to 0 - memset( &lr11xx_ce_data, 0, sizeof( lr11xx_ce_data_t ) ); + memset( &lr11xx_ce_data[stack_id], 0, sizeof( lr11xx_ce_data_t ) ); // get radio context - lr11xx_ctx = modem_get_radio_ctx( ); + lr11xx_ctx[stack_id] = modem_get_radio_ctx( stack_id ); // Restore lr11xx crypto data from flash memory into RAM - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_restore_from_flash( lr11xx_ctx, &lr11xx_crypto_status ) == + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_restore_from_flash( lr11xx_ctx[stack_id], &lr11xx_crypto_status ) == LR11XX_STATUS_OK ); #if defined( USE_PRE_PROVISIONED_FEATURES ) SMTC_MODEM_HAL_TRACE_WARNING( "Use lr11xx preprovisioned EUIs and keys\n" ); // Read LR11XX pre-provisioned identity - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_system_read_uid( lr11xx_ctx, lr11xx_ce_data.deveui ) == LR11XX_STATUS_OK ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_system_read_join_eui( lr11xx_ctx, lr11xx_ce_data.joineui ) == + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_system_read_uid( lr11xx_ctx[stack_id], lr11xx_ce_data[stack_id].deveui ) == LR11XX_STATUS_OK ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_system_read_join_eui( lr11xx_ctx[stack_id], lr11xx_ce_data[stack_id].joineui ) == LR11XX_STATUS_OK ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_system_read_pin( lr11xx_ctx, lr11xx_ce_data.pin ) == LR11XX_STATUS_OK ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_system_read_pin( lr11xx_ctx[stack_id], lr11xx_ce_data[stack_id].pin ) == LR11XX_STATUS_OK ); #endif // Return codes are in line between secure element definition and lr11xx internal definition @@ -191,31 +192,31 @@ smtc_se_return_code_t smtc_secure_element_set_key( smtc_se_key_identifier_t key_ lr11xx_crypto_keys_idx_t lr11xx_key_id = convert_key_id_from_se_to_lr11xx( key_id ); // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); if( ( key_id == SMTC_SE_MC_KEY_0 ) || ( key_id == SMTC_SE_MC_KEY_1 ) || ( key_id == SMTC_SE_MC_KEY_2 ) || ( key_id == SMTC_SE_MC_KEY_3 ) ) { // Decrypt the key if its a Mckey SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_derive_key( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_derive_key( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_MC_KE_KEY ), lr11xx_key_id, key ) == LR11XX_STATUS_OK ); } else { - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_set_key( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_set_key( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, lr11xx_key_id, key ) == LR11XX_STATUS_OK ); } if( status == SMTC_SE_RC_SUCCESS ) { SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_store_to_flash( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); + lr11xx_crypto_store_to_flash( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); } // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id) == true ); return status; } @@ -238,7 +239,7 @@ smtc_se_return_code_t smtc_secure_element_compute_aes_cmac( const uint8_t* mic_b } // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); if( mic_bx_buffer != NULL ) { @@ -251,20 +252,20 @@ smtc_se_return_code_t smtc_secure_element_compute_aes_cmac( const uint8_t* mic_b memcpy( ( mic_buff + MIC_BLOCK_BX_SIZE ), buffer, size ); // local_buffer = mic_buff; SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_compute_aes_cmac( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_compute_aes_cmac( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( key_id ), mic_buff, cur_size, ( uint8_t* ) cmac ) == LR11XX_STATUS_OK ); } else { SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_compute_aes_cmac( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_compute_aes_cmac( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( key_id ), buffer, size, ( uint8_t* ) cmac ) == LR11XX_STATUS_OK ); } // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id) == true ); return status; } @@ -280,15 +281,15 @@ smtc_se_return_code_t smtc_secure_element_verify_aes_cmac( uint8_t* buffer, uint } // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_verify_aes_cmac( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_verify_aes_cmac( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( key_id ), buffer, size, ( uint8_t* ) &expected_cmac ) == LR11XX_STATUS_OK ); // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id) == true ); return status; } @@ -305,23 +306,23 @@ smtc_se_return_code_t smtc_secure_element_aes_encrypt( const uint8_t* buffer, ui } // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); if( key_id == SMTC_SE_SLOT_RAND_ZERO_KEY ) { - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_aes_encrypt( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_aes_encrypt( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, LR11XX_CRYPTO_KEYS_IDX_GP0, buffer, size, enc_buffer ) == LR11XX_STATUS_OK ); } else { - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_aes_encrypt_01( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_aes_encrypt_01( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( key_id ), buffer, size, enc_buffer ) == LR11XX_STATUS_OK ); } // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id) == true ); return status; } @@ -338,18 +339,18 @@ smtc_se_return_code_t smtc_secure_element_derive_and_store_key( uint8_t* input, } // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_derive_key( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_derive_key( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( rootkey_id ), convert_key_id_from_se_to_lr11xx( targetkey_id ), input ) == LR11XX_STATUS_OK ); - SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_store_to_flash( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status ) == + SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_store_to_flash( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id) == true ); return status; } @@ -364,23 +365,23 @@ smtc_se_return_code_t smtc_secure_element_derive_relay_session_keys( uint32_t de block[0] = 0x01; // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); // key = RELAY_ROOT_WOR_S_KEY SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_aes_encrypt_01( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_aes_encrypt_01( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_NWK_S_ENC_KEY ), block, 16, key ) == LR11XX_STATUS_OK ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_set_key( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_set_key( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_RELAY_ROOT_WOR_S_KEY ), key ) == LR11XX_STATUS_OK ); if( status == SMTC_SE_RC_SUCCESS ) { SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_store_to_flash( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); + lr11xx_crypto_store_to_flash( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); } memset( block, 0, sizeof( block ) ); @@ -392,19 +393,19 @@ smtc_se_return_code_t smtc_secure_element_derive_relay_session_keys( uint32_t de // key = SMTC_SE_RELAY_WOR_S_INT_KEY SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_aes_encrypt( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_aes_encrypt( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_RELAY_ROOT_WOR_S_KEY ), block, 16, key ) == LR11XX_STATUS_OK ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_set_key( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_set_key( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_RELAY_WOR_S_INT_KEY ), key ) == LR11XX_STATUS_OK ); if( status == SMTC_SE_RC_SUCCESS ) { SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_store_to_flash( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); + lr11xx_crypto_store_to_flash( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); } memset( block, 0, sizeof( block ) ); @@ -416,29 +417,29 @@ smtc_se_return_code_t smtc_secure_element_derive_relay_session_keys( uint32_t de // key = SMTC_SE_RELAY_WOR_S_ENC_KEY SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_aes_encrypt( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_aes_encrypt( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_RELAY_ROOT_WOR_S_KEY ), block, 16, key ) == LR11XX_STATUS_OK ); if( status == SMTC_SE_RC_SUCCESS ) { SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_store_to_flash( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); + lr11xx_crypto_store_to_flash( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); } SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_set_key( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, + lr11xx_crypto_set_key( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( SMTC_SE_RELAY_WOR_S_ENC_KEY ), key ) == LR11XX_STATUS_OK ); if( status == SMTC_SE_RC_SUCCESS ) { SMTC_MODEM_HAL_PANIC_ON_FAILURE( - lr11xx_crypto_store_to_flash( lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); + lr11xx_crypto_store_to_flash( lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status ) == LR11XX_STATUS_OK ); } // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id ) == true ); return status; } @@ -478,18 +479,18 @@ smtc_se_return_code_t smtc_secure_element_process_join_accept( smtc_se_join_req_ uint8_t mic_header_10x[1] = { 0x20 }; // lr11xx crypto operation needed: suspend modem radio access to secure this direct access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_suspend_radio_access( stack_id ) == true ); // cmac = aes128_cmac(NwkKey, MHDR | JoinNonce | NetID | DevAddr | DLSettings | RxDelay | CFList | // CFListType) SMTC_MODEM_HAL_PANIC_ON_FAILURE( lr11xx_crypto_process_join_accept( - lr11xx_ctx, ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( enckey_id ), + lr11xx_ctx[stack_id], ( lr11xx_crypto_status_t* ) &status, convert_key_id_from_se_to_lr11xx( enckey_id ), convert_key_id_from_se_to_lr11xx( SMTC_SE_NWK_KEY ), ( lr11xx_crypto_lorawan_version_t ) 0, mic_header_10x, enc_join_accept + 1, enc_join_accept_size - 1, dec_join_accept + 1 ) == LR11XX_STATUS_OK ); // lr11xx crypto operation done: resume modem radio access - SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( ) == true ); + SMTC_MODEM_HAL_PANIC_ON_FAILURE( modem_resume_radio_access( stack_id ) == true ); if( status == SMTC_SE_RC_SUCCESS ) { @@ -510,7 +511,7 @@ smtc_se_return_code_t smtc_secure_element_set_deveui( const uint8_t deveui[SMTC_ { return SMTC_SE_RC_ERROR_NPE; } - memcpy( lr11xx_ce_data.deveui, deveui, SMTC_SE_EUI_SIZE ); + memcpy( lr11xx_ce_data[stack_id].deveui, deveui, SMTC_SE_EUI_SIZE ); return SMTC_SE_RC_SUCCESS; } @@ -520,7 +521,7 @@ smtc_se_return_code_t smtc_secure_element_get_deveui( uint8_t deveui[SMTC_SE_EUI { return SMTC_SE_RC_ERROR_NPE; } - memcpy( deveui, lr11xx_ce_data.deveui, SMTC_SE_EUI_SIZE ); + memcpy( deveui, lr11xx_ce_data[stack_id].deveui, SMTC_SE_EUI_SIZE ); return SMTC_SE_RC_SUCCESS; } @@ -530,7 +531,7 @@ smtc_se_return_code_t smtc_secure_element_set_joineui( const uint8_t joineui[SMT { return SMTC_SE_RC_ERROR_NPE; } - memcpy( lr11xx_ce_data.joineui, joineui, SMTC_SE_EUI_SIZE ); + memcpy( lr11xx_ce_data[stack_id].joineui, joineui, SMTC_SE_EUI_SIZE ); return SMTC_SE_RC_SUCCESS; } @@ -540,7 +541,7 @@ smtc_se_return_code_t smtc_secure_element_get_joineui( uint8_t joineui[SMTC_SE_E { return SMTC_SE_RC_ERROR_NPE; } - memcpy( joineui, lr11xx_ce_data.joineui, SMTC_SE_EUI_SIZE ); + memcpy( joineui, lr11xx_ce_data[stack_id].joineui, SMTC_SE_EUI_SIZE ); return SMTC_SE_RC_SUCCESS; } @@ -550,7 +551,7 @@ smtc_se_return_code_t smtc_secure_element_set_pin( const uint8_t pin[SMTC_SE_PIN { return SMTC_SE_RC_ERROR_NPE; } - memcpy( lr11xx_ce_data.pin, pin, SMTC_SE_PIN_SIZE ); + memcpy( lr11xx_ce_data[stack_id].pin, pin, SMTC_SE_PIN_SIZE ); return SMTC_SE_RC_SUCCESS; } @@ -561,7 +562,7 @@ smtc_se_return_code_t smtc_secure_element_get_pin( uint8_t pin[SMTC_SE_PIN_SIZE] return SMTC_SE_RC_ERROR_NPE; } - memcpy( pin, lr11xx_ce_data.pin, SMTC_SE_PIN_SIZE ); + memcpy( pin, lr11xx_ce_data[stack_id].pin, SMTC_SE_PIN_SIZE ); return SMTC_SE_RC_SUCCESS; } @@ -570,17 +571,19 @@ smtc_se_return_code_t smtc_secure_element_store_context( uint8_t stack_id ) // Note: Multistack is not suported in lr11xx crypto element lr11xx_ce_context_nvm_t ctx_old = { 0 }; - smtc_modem_hal_context_restore( CONTEXT_SECURE_ELEMENT, 0, ( uint8_t* ) &ctx_old, sizeof( ctx_old ) ); + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx_old ) + 8 - ( sizeof( ctx_old ) % 8 ); // align to 8 bytes + smtc_modem_hal_context_restore( CONTEXT_SECURE_ELEMENT, stack_id * real_size, ( uint8_t* ) &ctx_old, sizeof( ctx_old ) ); lr11xx_ce_context_nvm_t ctx = { - .data = lr11xx_ce_data, + .data = lr11xx_ce_data[stack_id], }; ctx.crc = lr11xx_ce_crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); if( ctx.crc != ctx_old.crc ) { - smtc_modem_hal_context_store( CONTEXT_SECURE_ELEMENT, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_store( CONTEXT_SECURE_ELEMENT, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); smtc_secure_element_restore_context( stack_id ); } return SMTC_SE_RC_SUCCESS; @@ -589,17 +592,19 @@ smtc_se_return_code_t smtc_secure_element_store_context( uint8_t stack_id ) smtc_se_return_code_t smtc_secure_element_restore_context( uint8_t stack_id ) { lr11xx_ce_context_nvm_t ctx; - smtc_modem_hal_context_restore( CONTEXT_SECURE_ELEMENT, 0, ( uint8_t* ) &ctx, sizeof( ctx ) ); + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + smtc_modem_hal_context_restore( CONTEXT_SECURE_ELEMENT, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); if( lr11xx_ce_crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ) == ctx.crc ) { - lr11xx_ce_data = ctx.data; + lr11xx_ce_data[stack_id] = ctx.data; return SMTC_SE_RC_SUCCESS; } else { SMTC_MODEM_HAL_TRACE_ERROR( "Restore of Secure Element context fails => Return to init values\n" ); // Initialize data structure to 0 - memset( &lr11xx_ce_data, 0, sizeof( lr11xx_ce_data_t ) ); + memset( &lr11xx_ce_data[stack_id], 0, sizeof( lr11xx_ce_data_t ) ); return SMTC_SE_RC_ERROR; } } diff --git a/lbm_lib/smtc_modem_core/smtc_modem_crypto/smtc_secure_element/smtc_secure_element.h b/lbm_lib/smtc_modem_core/smtc_modem_crypto/smtc_secure_element/smtc_secure_element.h index 10829a0a..308bce2b 100644 --- a/lbm_lib/smtc_modem_core/smtc_modem_crypto/smtc_secure_element/smtc_secure_element.h +++ b/lbm_lib/smtc_modem_core/smtc_modem_crypto/smtc_secure_element/smtc_secure_element.h @@ -158,9 +158,11 @@ typedef enum smtc_se_key_identifier_e /** * @brief Initialization of Secure Element driver * + * @param [in] stack_id The Stack Identifier + * * @return Secure element return code as defined in @ref smtc_se_return_code_t */ -smtc_se_return_code_t smtc_secure_element_init( void ); +smtc_se_return_code_t smtc_secure_element_init( uint8_t stack_id ); /** * @brief Sets a key diff --git a/lbm_lib/smtc_modem_core/smtc_modem_crypto/soft_secure_element/soft_se.c b/lbm_lib/smtc_modem_core/smtc_modem_crypto/soft_secure_element/soft_se.c index 29073ad3..9d56a412 100644 --- a/lbm_lib/smtc_modem_core/smtc_modem_crypto/soft_secure_element/soft_se.c +++ b/lbm_lib/smtc_modem_core/smtc_modem_crypto/soft_secure_element/soft_se.c @@ -402,17 +402,14 @@ uint32_t soft_ce_crc( const uint8_t* buf, int len ); * --- PUBLIC FUNCTIONS DEFINITION --------------------------------------------- */ -smtc_se_return_code_t smtc_secure_element_init( void ) +smtc_se_return_code_t smtc_secure_element_init( uint8_t stack_id ) { soft_se_data_t local_data = { .deveui = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, .joineui = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, .pin = { 0x00, 0x00, 0x00, 0x00 }, .key_list = SOFT_SE_KEY_LIST }; // init soft secure element data euis and pin to 0 and key_list with empty lut - for( uint8_t stack_id = 0; stack_id < NUMBER_OF_STACKS; stack_id++ ) - { - memcpy( ( uint8_t* ) &soft_se_data[stack_id], ( uint8_t* ) &local_data, sizeof( local_data ) ); - } + memcpy( ( uint8_t* ) &soft_se_data[stack_id], ( uint8_t* ) &local_data, sizeof( local_data ) ); SMTC_MODEM_HAL_TRACE_INFO( "Use soft secure element for cryptographic functionalities\n" ); return SMTC_SE_RC_SUCCESS; @@ -730,6 +727,8 @@ smtc_se_return_code_t smtc_secure_element_get_pin( uint8_t pin[SMTC_SE_PIN_SIZE] smtc_se_return_code_t smtc_secure_element_store_context( uint8_t stack_id ) { soft_se_context_nvm_t ctx = { 0 }; + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes soft_se_data_t* data_current_ctx = &soft_se_data[stack_id]; @@ -739,7 +738,7 @@ smtc_se_return_code_t smtc_secure_element_store_context( uint8_t stack_id ) ctx.crc = soft_ce_crc( ( uint8_t* ) &ctx, sizeof( ctx ) - sizeof( ctx.crc ) ); // Store the current context related to stack_id - smtc_modem_hal_context_store( CONTEXT_SECURE_ELEMENT, stack_id * sizeof( ctx ), ( uint8_t* ) &ctx, sizeof( ctx ) ); + smtc_modem_hal_context_store( CONTEXT_SECURE_ELEMENT, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); smtc_secure_element_restore_context( stack_id ); return SMTC_SE_RC_SUCCESS; } @@ -747,7 +746,9 @@ smtc_se_return_code_t smtc_secure_element_store_context( uint8_t stack_id ) smtc_se_return_code_t smtc_secure_element_restore_context( uint8_t stack_id ) { soft_se_context_nvm_t ctx = { 0 }; - smtc_modem_hal_context_restore( CONTEXT_SECURE_ELEMENT, stack_id * sizeof( ctx ), ( uint8_t* ) &ctx, + /* EvaTODO: this is now copied "bad" implementation from lbm_zephyr.... */ + uint32_t real_size = sizeof( ctx ) + 8 - ( sizeof( ctx ) % 8 ); // align to 8 bytes + smtc_modem_hal_context_restore( CONTEXT_SECURE_ELEMENT, stack_id * real_size, ( uint8_t* ) &ctx, sizeof( ctx ) ); soft_se_data_t* data_ctx = &soft_se_data[stack_id]; diff --git a/lbm_lib/smtc_modem_core/smtc_modem_test.c b/lbm_lib/smtc_modem_core/smtc_modem_test.c index 6af80b8b..93370982 100644 --- a/lbm_lib/smtc_modem_core/smtc_modem_test.c +++ b/lbm_lib/smtc_modem_core/smtc_modem_test.c @@ -80,7 +80,7 @@ * --- PRIVATE MACROS----------------------------------------------------------- */ -#define TEST_STACK_ID_0 0 +#define TEST_STACK_ID_0 0 // EvaTODO: Only one stack supported for test mode /* * ----------------------------------------------------------------------------- * --- PRIVATE CONSTANTS ------------------------------------------------------- @@ -148,7 +148,7 @@ void test_mode_cw_callback_for_rp( void* rp_void ); smtc_modem_return_code_t smtc_modem_test_start( void ) { - if( modem_get_test_mode_status( ) == true ) + if( modem_get_test_mode_status(TEST_STACK_ID_0 ) == true ) { SMTC_MODEM_HAL_TRACE_WARNING( "TST MODE: ALREADY STARTED\n" ); return SMTC_MODEM_RC_BUSY; @@ -160,13 +160,13 @@ smtc_modem_return_code_t smtc_modem_test_start( void ) return SMTC_MODEM_RC_FAIL; } - modem_set_test_mode_status( true ); + modem_set_test_mode_status(TEST_STACK_ID_0, true ); SMTC_MODEM_HAL_TRACE_INFO( "TST MODE: START\n" ); memset( &modem_test_context, 0, sizeof( modem_test_context_t ) ); memset( &rp_task, 0, sizeof( rp_task_t ) ); memset( &rp_radio_params, 0, sizeof( rp_radio_params_t ) ); - modem_test_context.rp = modem_get_rp( ); + modem_test_context.rp = modem_get_rp( TEST_STACK_ID_0 ); // modem_test_context.lr1_mac_obj = lorawan_api_stack_mac_get( TEST_STACK_ID_0 ); modem_test_context.hook_id = RP_HOOK_ID_TEST_MODE; @@ -181,7 +181,7 @@ smtc_modem_return_code_t smtc_modem_test_start( void ) smtc_modem_return_code_t smtc_modem_test_stop( void ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -192,7 +192,7 @@ smtc_modem_return_code_t smtc_modem_test_stop( void ) return SMTC_MODEM_RC_FAIL; } rp_release_hook( modem_test_context.rp, modem_test_context.hook_id ); - modem_set_test_mode_status( false ); + modem_set_test_mode_status(TEST_STACK_ID_0, false ); // Re enable failsafe on radio planner rp_disable_failsafe( modem_test_context.rp, false ); @@ -202,7 +202,7 @@ smtc_modem_return_code_t smtc_modem_test_stop( void ) smtc_modem_return_code_t smtc_modem_test_tx_hop( void ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -212,7 +212,7 @@ smtc_modem_return_code_t smtc_modem_test_tx_hop( void ) smtc_modem_return_code_t smtc_modem_test_nop( bool reset_radio ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -236,7 +236,7 @@ smtc_modem_return_code_t smtc_modem_test_tx_lora( uint8_t* payload, uint8_t payl bool invert_iq, bool crc_is_on, ral_lora_pkt_len_modes_t header_type, uint32_t preamble_size, uint32_t nb_of_tx, uint32_t delay_ms ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -308,7 +308,7 @@ smtc_modem_return_code_t smtc_modem_test_tx_lora( uint8_t* payload, uint8_t payl smtc_modem_return_code_t smtc_modem_test_tx_fsk( uint8_t* payload, uint8_t payload_length, uint32_t frequency_hz, int8_t tx_power_dbm, uint32_t nb_of_tx, uint32_t delay_ms ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -372,7 +372,7 @@ smtc_modem_return_code_t smtc_modem_test_tx_lrfhss( uint8_t* payload, uint8_t pa lr_fhss_v1_grid_t tx_grid, bool enable_hopping, uint32_t nb_of_tx, uint32_t delay_ms ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -450,7 +450,7 @@ smtc_modem_return_code_t smtc_modem_test_tx_lrfhss( uint8_t* payload, uint8_t pa smtc_modem_return_code_t smtc_modem_test_tx_cw( uint32_t frequency_hz, int8_t tx_power_dbm ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -511,7 +511,7 @@ smtc_modem_return_code_t smtc_modem_test_rx_lora( uint32_t frequency_hz, ral_lor bool invert_iq, bool crc_is_on, ral_lora_pkt_len_modes_t header_type, uint32_t preamble_size, uint8_t symb_nb_timeout ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -579,7 +579,7 @@ smtc_modem_return_code_t smtc_modem_test_rx_lora( uint32_t frequency_hz, ral_lor smtc_modem_return_code_t smtc_modem_test_rx_fsk_continuous( uint32_t frequency_hz ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -641,7 +641,7 @@ smtc_modem_return_code_t smtc_modem_test_rx_fsk_continuous( uint32_t frequency_h smtc_modem_return_code_t smtc_modem_test_get_nb_rx_packets( uint32_t* nb_rx_packets ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -652,7 +652,7 @@ smtc_modem_return_code_t smtc_modem_test_get_nb_rx_packets( uint32_t* nb_rx_pack smtc_modem_return_code_t smtc_modem_test_get_last_rx_packets( int16_t* rssi, int16_t* snr, uint8_t* rx_payload, uint8_t* rx_payload_length ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -666,7 +666,7 @@ smtc_modem_return_code_t smtc_modem_test_get_last_rx_packets( int16_t* rssi, int smtc_modem_return_code_t smtc_modem_test_rssi_lbt( uint32_t frequency_hz, uint32_t bw_hz, uint16_t time_ms ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -692,7 +692,7 @@ smtc_modem_return_code_t smtc_modem_test_rssi_lbt( uint32_t frequency_hz, uint32 smtc_modem_return_code_t smtc_modem_test_get_rssi( int8_t* rssi ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -720,7 +720,7 @@ void modem_test_set_rssi( int16_t rssi ) smtc_modem_return_code_t smtc_modem_test_radio_reset( void ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -737,7 +737,7 @@ smtc_modem_return_code_t smtc_modem_test_radio_reset( void ) { return SMTC_MODEM_RC_FAIL; } - smtc_modem_hal_set_ant_switch( false ); + smtc_modem_hal_set_ant_switch( TEST_STACK_ID_0, false ); smtc_modem_hal_stop_radio_tcxo( ); return SMTC_MODEM_RC_OK; @@ -746,7 +746,7 @@ smtc_modem_return_code_t smtc_modem_test_radio_reset( void ) smtc_modem_return_code_t smtc_modem_test_direct_radio_write( uint8_t* command, uint16_t command_length, uint8_t* data, uint16_t data_length ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -778,7 +778,7 @@ smtc_modem_return_code_t smtc_modem_test_direct_radio_write( uint8_t* command, u smtc_modem_return_code_t smtc_modem_test_direct_radio_read( uint8_t* command, uint16_t command_length, uint8_t* data, uint16_t data_length ) { - if( modem_get_test_mode_status( ) == false ) + if( modem_get_test_mode_status( TEST_STACK_ID_0 ) == false ) { SMTC_MODEM_HAL_TRACE_WARNING( "TEST FUNCTION CANNOT BE CALLED: NOT IN TEST MODE\n" ); return SMTC_MODEM_RC_INVALID; @@ -999,7 +999,7 @@ void test_mode_cw_callback_for_rp( void* rp_void ) radio_planner_t* rp = ( radio_planner_t* ) rp_void; uint8_t id = rp->radio_task_id; SMTC_MODEM_HAL_PANIC_ON_FAILURE( ralf_setup_lora( rp->radio, &rp->radio_params[id].tx.lora ) == RAL_STATUS_OK ); - smtc_modem_hal_set_ant_switch( true ); + smtc_modem_hal_set_ant_switch( TEST_STACK_ID_0, true ); SMTC_MODEM_HAL_PANIC_ON_FAILURE( ral_set_tx_cw( &( rp->radio->ral ) ) == RAL_STATUS_OK ); } diff --git a/lbm_lib/smtc_modem_hal/smtc_modem_hal.h b/lbm_lib/smtc_modem_hal/smtc_modem_hal.h index 4decdc70..697e58d0 100644 --- a/lbm_lib/smtc_modem_hal/smtc_modem_hal.h +++ b/lbm_lib/smtc_modem_hal/smtc_modem_hal.h @@ -166,28 +166,36 @@ void smtc_modem_hal_set_offset_to_test_wrapping( const uint32_t offset_to_test_w /** * @brief Starts the provided timer objet for the given time * + * @param [in] stack_id Stack identifier * @param [in] milliseconds Number of milliseconds (timer value) * @param [in] callback Callback that will be called in case of timer irq * @param [in] context Context that will be passed on callback argument */ -void smtc_modem_hal_start_timer( const uint32_t milliseconds, void ( *callback )( void* context ), void* context ); +void smtc_modem_hal_start_timer( uint8_t stack_id, const uint32_t milliseconds, void ( *callback )( void* context ), void* context ); /** + * * @brief Stop the provided timer + * + * @param [in] stack_id Stack identifier */ -void smtc_modem_hal_stop_timer( void ); +void smtc_modem_hal_stop_timer( uint8_t stack_id ); /* ------------ IRQ management ------------*/ /** * @brief Disables interruptions used in Modem (radio_dio and timer) + * + * @param [in] stack_id Stack identifier */ -void smtc_modem_hal_disable_modem_irq( void ); +void smtc_modem_hal_disable_modem_irq( uint8_t stack_id ); /** * @brief Enables interruptions used in Modem (radio_dio and timer) + * + * @param [in] stack_id Stack identifier */ -void smtc_modem_hal_enable_modem_irq( void ); +void smtc_modem_hal_enable_modem_irq( uint8_t stack_id ); /* ------------ Context saving management ------------*/ @@ -259,11 +267,12 @@ uint32_t smtc_modem_hal_get_random_nb_in_range( const uint32_t val_1, const uint /** * @brief Config the radio interruption callback * + * @param [in] stack_id Stack identifier * @param [in] callback Callback that will be called in case of timer irq * @param [in] context Context that will be passed on callback argument * */ -void smtc_modem_hal_irq_config_radio_irq( void ( *callback )( void* context ), void* context ); +void smtc_modem_hal_irq_config_radio_irq( uint8_t stack_id, void ( *callback )( void* context ), void* context ); @@ -274,7 +283,7 @@ void smtc_modem_hal_irq_config_radio_irq( void ( *callback )( void* context ), v * * @return bool False if the radio is free, false otherwise */ -bool smtc_modem_external_stack_currently_use_radio( void ); +bool smtc_modem_external_stack_currently_use_radio( uint8_t stack_id ); /** * @brief Start radio tcxo * @@ -296,14 +305,15 @@ void smtc_modem_hal_stop_radio_tcxo( void ); * * @return uint32_t TCXO startup delay in ms */ -uint32_t smtc_modem_hal_get_radio_tcxo_startup_delay_ms( void ); +uint32_t smtc_modem_hal_get_radio_tcxo_startup_delay_ms( uint8_t stack_id ); /** * @brief Set antenna switch for Tx operation or not. * + * @param [in] stack_id Stack identifier * @param [in] is_tx_on Indicates if the antenna switch must be set for Tx operation or not */ -void smtc_modem_hal_set_ant_switch( bool is_tx_on ); +void smtc_modem_hal_set_ant_switch( uint8_t stack_id, bool is_tx_on ); /* ------------ Environment management ------------*/ @@ -323,9 +333,11 @@ uint8_t smtc_modem_hal_get_battery_level( void ); /** * @brief Return board wake up delay in ms * + * @param [in] stack_id Stack identifier + * * @return uint8_t Board wake up delay in ms */ -int8_t smtc_modem_hal_get_board_delay_ms( void ); +int8_t smtc_modem_hal_get_board_delay_ms( uint8_t stack_id ); /* ------------ Trace management ------------*/