From d82cf421912440833a39843b46b490bff7f93271 Mon Sep 17 00:00:00 2001 From: anschrammh Date: Tue, 14 Mar 2023 08:20:34 +0100 Subject: [PATCH] ble modem is now able to stop the service properly when it's turned of, added more info to the ram usage command in the nano_shell --- app/ble/ble_modem.c | 35 +++++++++++++++++++++++++++++++---- app/ble/ble_modem.h | 4 +++- app/ble/ble_service.c | 11 +---------- app/nano_shell_command.c | 23 +++++++++++++++++------ 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/app/ble/ble_modem.c b/app/ble/ble_modem.c index ed48bfc..79d1dfd 100644 --- a/app/ble/ble_modem.c +++ b/app/ble/ble_modem.c @@ -4,13 +4,15 @@ #include "wm_bt_def.h" #include "wm_bt_app.h" #include "wm_bt_util.h" +#include "ble_service.h" //Is needed for the BT off workaround #include "wm_wifi.h" -bool ble_modem_on(void) +bool ble_modem_on(bool startService) { int status = BLE_HS_ENOERR; + bool serviceStartSuccess = true; uint8_t uart_no = 0xFF; tls_appl_trace_level = TLS_BT_LOG_VERBOSE; //Should be set with a config define @@ -27,19 +29,44 @@ bool ble_modem_on(void) TLS_BT_APPL_TRACE_ERROR("%s, tls_bt_init ret:%s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); } - return status == BLE_HS_ENOERR || status == BLE_HS_EALREADY ? true : false; + // Start the ble service if it was asked and if it is not yet started + if(startService && !ble_service_is_started()) + serviceStartSuccess = ble_service_start(); + + return ((status == BLE_HS_ENOERR || status == BLE_HS_EALREADY) && serviceStartSuccess) ? true : false; } bool ble_modem_off(void) { int status = BLE_HS_ENOERR; + bool serviceStopSuccess = true; TLS_BT_APPL_TRACE_DEBUG("ble modem off"NEW_LINE); - if(bt_adapter_state == WM_BT_STATE_OFF) { + if(bt_adapter_state == WM_BT_STATE_OFF) + { TLS_BT_APPL_TRACE_VERBOSE("ble modem already off"NEW_LINE); return TLS_BT_STATUS_SUCCESS; } + if(ble_service_is_started()) + { + serviceStopSuccess = ble_service_stop(); + } + + // Let's make a busy wait on the status of the ble service: + uint8_t timeout = 0; + while(ble_service_is_started()) + { + tls_os_time_delay(pdMS_TO_TICK(1)); + + // Service is stuck ? + if(++timeout > 10) + { + serviceStopSuccess = false; + break; + } + }; + status = tls_bt_deinit(); if((status != BLE_HS_ENOERR) && (status != BLE_HS_EALREADY)) { @@ -52,7 +79,7 @@ bool ble_modem_off(void) tls_wifi_scan(); } - return status == BLE_HS_ENOERR || status == BLE_HS_EALREADY ? true : false; + return ((status == BLE_HS_ENOERR || status == BLE_HS_EALREADY) && serviceStopSuccess) ? true : false; } bool is_ble_modem_on(void) diff --git a/app/ble/ble_modem.h b/app/ble/ble_modem.h index 58afc5d..9c4c186 100644 --- a/app/ble/ble_modem.h +++ b/app/ble/ble_modem.h @@ -6,13 +6,15 @@ /** * @brief Turns the BLE modem on * + * @param startService to start the ble service when turning the modem on * @return true on success * @return false on failure */ -bool ble_modem_on(void); +bool ble_modem_on(bool startService); /** * @brief Turns the BLE modem off + * @note If the ble service was started, then it will automatically be turned off with the modem * * @return true on success * @return false on failure diff --git a/app/ble/ble_service.c b/app/ble/ble_service.c index acc0d20..89301b1 100644 --- a/app/ble/ble_service.c +++ b/app/ble/ble_service.c @@ -597,6 +597,7 @@ static int ble_gap_event_cb(struct ble_gap_event *event, void *arg) if(_ble_service_state == BLE_SERVICE_MODE_EXITING) { _ble_service_state = BLE_SERVICE_MODE_STOPPED; + if((status = ble_gap_event_listener_unregister(&ble_gap_event_listener)) != BLE_HS_ENOERR) { TLS_BT_APPL_TRACE_WARNING("%s, ble_gap_event_listener_unregister %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); @@ -732,24 +733,14 @@ static int gatt_nus_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, s case BLE_GATT_ACCESS_OP_WRITE_CHR: { struct os_mbuf *om_buf = ctxt->om; - //TLS_BT_APPL_TRACE_VERBOSE("Received data : "NEW_LINE); while(om_buf) { - /*for(uint16_t i = 0; i < om_buf->om_len; i++) - { - if(om_buf->om_data[i] < 32) - printf("[%u]", om_buf->om_data[i]); - else - putchar(om_buf->om_data[i]); - }*/ - // Call the nus rx cb function if one is registered if(_ble_service_nus_data_rx_cb) _ble_service_nus_data_rx_cb(om_buf->om_data, om_buf->om_len); om_buf = SLIST_NEXT(om_buf, om_next); } - printf(NEW_LINE); } break; default: diff --git a/app/nano_shell_command.c b/app/nano_shell_command.c index c3567f8..b97fa4d 100644 --- a/app/nano_shell_command.c +++ b/app/nano_shell_command.c @@ -97,7 +97,7 @@ static void tls_rtc_irq_cb(void *arg) static void nus_data_rx_cb(const uint8_t *data, uint16_t length) { - shell_printf("%s, received data : ", __FUNCTION__); + shell_printf("%s, received data : "NEW_LINE"#", __FUNCTION__); for (uint16_t i = 0; i < length; i++) { @@ -106,8 +106,7 @@ static void nus_data_rx_cb(const uint8_t *data, uint16_t length) else shell_putc(data[i]); } - - shell_puts(NEW_LINE); + shell_puts("#"NEW_LINE); } int _system(const shell_cmd_t *pcmd, int argc, char *const argv[]) @@ -130,7 +129,8 @@ int _system(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else if(strcmp(argv[1], "ram_usage") == 0) { - shell_printf("Free OS heap : %u/%u byte(s)"NEW_LINE"tls heap size : %u"NEW_LINE, xPortGetFreeHeapSize(), configTOTAL_HEAP_SIZE, tls_mem_get_avail_heapsize()); + uint32_t total_mem_size = (unsigned int)&__heap_end - (unsigned int)&__heap_start; + shell_printf("Free OS heap : %u/%u byte(s)"NEW_LINE"tls heap size : %u/%u byte(s)"NEW_LINE, xPortGetFreeHeapSize(), configTOTAL_HEAP_SIZE, tls_mem_get_avail_heapsize(), total_mem_size); } else { @@ -561,7 +561,7 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - shell_printf("Unknown %s action, list of send_ble_notif actions :"NEW_LINE"toast \"msg\""NEW_LINE"bat \"%%\""NEW_LINE"findPhone"NEW_LINE"music play|pause|previous|next"NEW_LINE"notify reply|dismiss_all id \"tel\" \"msg\""NEW_LINE, argv[2]); + shell_printf("Unknown %s action, list of send_ble_notif actions :"NEW_LINE"toast \"msg\""NEW_LINE"bat \"XX%%\" 1|0 X.X"NEW_LINE"findPhone"NEW_LINE"music play|pause|previous|next"NEW_LINE"notify reply|dismiss_all id \"tel\" \"msg\""NEW_LINE, argv[2]); } } else if(strcmp(argv[1], "up_conn_param") == 0) @@ -590,7 +590,18 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) { if(strcmp(argv[1], "enable") == 0) { - shell_printf("Enabling bluetooth : %d"NEW_LINE, ble_modem_on()); + if(argc == 3) + { + bool result = ble_modem_on(true); + shell_printf("Enabling bluetooth modem with ble service : %d"NEW_LINE, result); + + if(result) + ble_service_nus_register_data_rx_cb(&(nus_data_rx_cb)); + } + else + { + shell_printf("Enabling bluetooth modem only : %d"NEW_LINE, ble_modem_on(false)); + } } else if(strcmp(argv[1], "disable") == 0) {