From 721db527ce85694b539f51d6d2c9d6acb9d2f27b Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sun, 26 Mar 2023 23:21:27 +0200 Subject: [PATCH] Added the ble modem and service source files (at last) --- src/W800_SDK_v1.00.10/Makefile | 1 + src/W800_SDK_v1.00.10/app/ble/Makefile | 15 + src/W800_SDK_v1.00.10/app/ble/ble_modem.c | 91 ++ src/W800_SDK_v1.00.10/app/ble/ble_modem.h | 32 + src/W800_SDK_v1.00.10/app/ble/ble_service.c | 789 ++++++++++++++++++ src/W800_SDK_v1.00.10/app/ble/ble_service.h | 126 +++ .../app/ble/bluetooth_sig_values.h | 9 + .../src/app/bleapp/wm_bt_app.c | 10 +- .../src/app/bleapp/wm_bt_app.h | 5 + .../src/app/bleapp/wm_bt_util.c | 29 +- .../src/app/bleapp/wm_bt_util.h | 5 +- .../porting/w800/include/syscfg/syscfg.h | 10 +- .../bt/blehost/porting/w800/src/tls_nimble.c | 2 +- src/W800_SDK_v1.00.10/tools/w800/inc.mk | 9 +- 14 files changed, 1114 insertions(+), 19 deletions(-) create mode 100644 src/W800_SDK_v1.00.10/app/ble/Makefile create mode 100644 src/W800_SDK_v1.00.10/app/ble/ble_modem.c create mode 100644 src/W800_SDK_v1.00.10/app/ble/ble_modem.h create mode 100644 src/W800_SDK_v1.00.10/app/ble/ble_service.c create mode 100644 src/W800_SDK_v1.00.10/app/ble/ble_service.h create mode 100644 src/W800_SDK_v1.00.10/app/ble/bluetooth_sig_values.h diff --git a/src/W800_SDK_v1.00.10/Makefile b/src/W800_SDK_v1.00.10/Makefile index 4cf33c6..b3059ed 100644 --- a/src/W800_SDK_v1.00.10/Makefile +++ b/src/W800_SDK_v1.00.10/Makefile @@ -34,6 +34,7 @@ COMPONENTS_$(TARGET) = \ $(TOP_DIR)/app/app_drivers/libappdrivers$(LIB_EXT) \ $(TOP_DIR)/app/persistency/libpersistency$(LIB_EXT) \ $(TOP_DIR)/app/translation/libtranslation$(LIB_EXT) \ + $(TOP_DIR)/app/translation/libble$(LIB_EXT) \ $(TOP_DIR)/lvgl/liblvgl$(LIB_EXT) ifeq ($(USE_LIB), 0) diff --git a/src/W800_SDK_v1.00.10/app/ble/Makefile b/src/W800_SDK_v1.00.10/app/ble/Makefile new file mode 100644 index 0000000..8ea9e62 --- /dev/null +++ b/src/W800_SDK_v1.00.10/app/ble/Makefile @@ -0,0 +1,15 @@ +TOP_DIR = ../.. +sinclude $(TOP_DIR)/tools/w800/conf.mk + +ifndef PDIR +GEN_LIBS = libble$(LIB_EXT) +endif + +#DEFINES += + +sinclude $(TOP_DIR)/tools/w800/rules.mk + +INCLUDES := $(INCLUDES) -I $(PDIR)include + +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile \ No newline at end of file diff --git a/src/W800_SDK_v1.00.10/app/ble/ble_modem.c b/src/W800_SDK_v1.00.10/app/ble/ble_modem.c new file mode 100644 index 0000000..f01cce2 --- /dev/null +++ b/src/W800_SDK_v1.00.10/app/ble/ble_modem.c @@ -0,0 +1,91 @@ +#include "ble_modem.h" +#include "app_common.h" +#include "host/ble_hs.h" +#include "FreeRTOS.h" +#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(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 + + if(bt_adapter_state == WM_BT_STATE_ON) { + TLS_BT_APPL_TRACE_VERBOSE("ble modem already on"NEW_LINE); + return true; + } + + TLS_BT_APPL_TRACE_DEBUG("ble modem running, uart_no=%d, log_level=%d"NEW_LINE, uart_no, + tls_appl_trace_level); + status = tls_bt_init(uart_no); + + if((status != BLE_HS_ENOERR) && (status != BLE_HS_EALREADY)) { + TLS_BT_APPL_TRACE_ERROR("%s, tls_bt_init ret:%s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + + // 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) + { + 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_TICKS(5)); + + // Service is stuck ? waiting up to 300 ms for it to stop + if(++timeout > 60) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_service_stop timeout "NEW_LINE, __FUNCTION__); + return serviceStopSuccess; + } + }; + + status = tls_bt_deinit(); + + if((status != BLE_HS_ENOERR) && (status != BLE_HS_EALREADY)) { + TLS_BT_APPL_TRACE_ERROR("%s, tls_bt_deinit ret:%s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + + if(status != BLE_HS_EALREADY) + { + //Starting a wifi scan really stops the BT modem ?? Why ? I don't know + tls_wifi_scan(); + } + + return ((status == BLE_HS_ENOERR || status == BLE_HS_EALREADY) && serviceStopSuccess) ? true : false; +} + +bool is_ble_modem_on(void) +{ + if(bt_adapter_state == WM_BT_STATE_OFF || bt_system_action != WM_BT_SYSTEM_ACTION_IDLE) + return false; + return true; +} \ No newline at end of file diff --git a/src/W800_SDK_v1.00.10/app/ble/ble_modem.h b/src/W800_SDK_v1.00.10/app/ble/ble_modem.h new file mode 100644 index 0000000..9c4c186 --- /dev/null +++ b/src/W800_SDK_v1.00.10/app/ble/ble_modem.h @@ -0,0 +1,32 @@ +#ifndef BLE_MODEM_H +#define BLE_MODEM_H + +#include "wm_type_def.h" + +/** + * @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(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 + */ +bool ble_modem_off(void); + +/** + * @brief Find out if the modem is turned on or not + * + * @return true if turned on + * @return false if turned off + */ +bool is_ble_modem_on(void); + +#endif //BLE_MODEM_H \ No newline at end of file diff --git a/src/W800_SDK_v1.00.10/app/ble/ble_service.c b/src/W800_SDK_v1.00.10/app/ble/ble_service.c new file mode 100644 index 0000000..9c9377f --- /dev/null +++ b/src/W800_SDK_v1.00.10/app/ble/ble_service.c @@ -0,0 +1,789 @@ +#include "ble_service.h" +#include "app_common.h" +#include "ble_modem.h" +#include "host/ble_hs.h" +#include "services/gap/ble_svc_gap.h" +#include "wm_bt_util.h" +#include "bluetooth_sig_values.h" + +#define USABLE_DEFAULT_MTU (20) //23 - 3 of header bytes + +/* ble service internal workings attributes */ +static volatile ble_service_state_e _ble_service_state = BLE_SERVICE_MODE_STOPPED; +static nus_data_rx_fn_t _ble_service_nus_data_rx_cb = NULL; +static ble_service_state_change_fn_t _ble_service_state_change_cb = NULL; + +/* Connection handle to the connected device : only one simultaneous connection */ +static uint16_t ble_device_conn_handle = BLE_HS_CONN_HANDLE_NONE; +static uint16_t usable_mtu = USABLE_DEFAULT_MTU; + +/** + * @brief Structure used to store the various data to carry out a chunked notification or indication data transfer + * + */ +typedef struct +{ + uint16_t length; // The length of the data being sent + uint16_t offset; // The offset used to be ableto send the next chunk of data in the array + uint16_t sent_chunk_size; // The size of the chunk sent + const uint8_t *data; // The address of the data to send + bool transfer_in_progress; // Is a transfer already in progress ? +} data_being_sent_t; + +// Only one transfer of a type () can occur at any given time +static data_being_sent_t notification_data = {.data = NULL, .length = 0, .offset = 0, .transfer_in_progress = false}; +static data_being_sent_t indication_data = {.data = NULL, .length = 0, .offset = 0, .transfer_in_progress = false}; + +static struct ble_gap_event_listener ble_gap_event_listener; + +static int battery_level_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); +static uint16_t battery_level_char_handle = 0; +static uint8_t battery_level_value = 42; + + +static int gatt_nus_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); +static const ble_uuid128_t gatt_nus_service_uuid = BLE_UUID128_INIT(0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E); + +static uint16_t gatt_nus_char_rx_handle = 0; +static const ble_uuid128_t gatt_nus_char_rx_uuid = BLE_UUID128_INIT(0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E); + +static uint16_t gatt_nus_char_tx_handle = 0; +static const ble_uuid128_t gatt_nus_char_tx_uuid = BLE_UUID128_INIT(0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E); + +static struct ble_gatt_svc_def gatt_svc[] = +{ + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_DEVICE_ADV_SERVICE), + .characteristics = (struct ble_gatt_chr_def[]) + { + { + .uuid = BLE_UUID16_DECLARE(0x2A19), + .val_handle = &battery_level_char_handle, + .access_cb = &(battery_level_char_access_cb), + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY + }, + { + .uuid = NULL + } + } + }, + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = &gatt_nus_service_uuid.u, + .characteristics = (struct ble_gatt_chr_def[]) + { + { + .uuid = &gatt_nus_char_rx_uuid.u, + .val_handle = &gatt_nus_char_rx_handle, + .access_cb = &(gatt_nus_char_access_cb), + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP + } + , + { + .uuid = &gatt_nus_char_tx_uuid.u, + .val_handle = &gatt_nus_char_tx_handle, + .access_cb = &(gatt_nus_char_access_cb), + .flags = BLE_GATT_CHR_F_NOTIFY + } + , + { + .uuid = NULL + } + } + }, + { + .type = BLE_GATT_SVC_TYPE_END + } +}; + +static bool ble_service_define_gatt(const struct ble_gatt_svc_def *gatt_svc); +static bool ble_service_advertise(bool enable); +static int ble_gap_event_cb(struct ble_gap_event *event, void *arg); +static int ble_advertise_gap_event_cb(struct ble_gap_event *event, void *arg); +static void print_conn_desc(const struct ble_gap_conn_desc *desc); +// Raw because it doesn't handle payload fragmentation if mtu size is smaller than the payload size +static bool ble_service_send_raw_custom_notification(uint16_t characteristic_handle, const uint8_t *data, uint16_t length); +static bool ble_service_send_custom_notification(uint16_t characteristic_handle, data_being_sent_t * const data); +static void reset_data_being_sent(data_being_sent_t * const data); + +// Needed to get the reponse after a mtu exchange request +static int ble_gatt_mtu_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t mtu, void *arg); + +const char *ble_service_state_2_str(ble_service_state_e state) +{ + switch(state) { + CASE_RETURN_STR(BLE_SERVICE_MODE_STOPPED) + CASE_RETURN_STR(BLE_SERVICE_MODE_IDLE) + CASE_RETURN_STR(BLE_SERVICE_MODE_ADVERTISING) + CASE_RETURN_STR(BLE_SERVICE_MODE_CONNECTED) + CASE_RETURN_STR(BLE_SERVICE_MODE_INDICATING) + CASE_RETURN_STR(BLE_SERVICE_MODE_EXITING) + default: + return "unkown ble service state"; + } +} + +/** + * PUBLIC FUNCTION DEFINITION + */ + +bool ble_service_start(void) +{ + int status = BLE_HS_ENOERR; + + // 1 We first check if the BLE service is stopped + if(_ble_service_state != BLE_SERVICE_MODE_STOPPED) + { + TLS_BT_APPL_TRACE_WARNING("%s, ble service already running (%s)"NEW_LINE, __FUNCTION__, ble_service_state_2_str(_ble_service_state)); + return true; + } + + // 2 We check if the BLE MODEM is turned on + if(!is_ble_modem_on()) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble modem is not turned on"NEW_LINE, __FUNCTION__); + return false; + } + + // 3 We set our device name and appearance + if((status = ble_svc_gap_device_name_set(BLE_DEVICE_NAME)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_WARNING("%s, ble_svc_gap_device_name_set "NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + + if((status = ble_svc_gap_device_appearance_set(BLE_DEVICE_APPEARANCE)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_WARNING("%s, ble_svc_gap_device_appearance_set "NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + + // 4 We register our gatt (service structure) + if(!ble_service_define_gatt(gatt_svc)) + { + TLS_BT_APPL_TRACE_ERROR("%s, failed to define gatt"NEW_LINE, __FUNCTION__); + return false; + } + + // 5 We register a gap event handler callback + if((status = ble_gap_event_listener_register(&ble_gap_event_listener, &(ble_gap_event_cb), NULL)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_event_listener_register %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + // 6 We are now ready to make all registered services available to peers + if((status = ble_gatts_start()) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gatts_start failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + // 7 Finally we can start advertising + if(!ble_service_advertise(true)) + { + return false; + } + + _ble_service_state = BLE_SERVICE_MODE_ADVERTISING; + + return true; +} + +bool ble_service_stop(void) +{ + int status = BLE_HS_ENOERR; + + // 1 We first check if the BLE service is not stopped + if(_ble_service_state == BLE_SERVICE_MODE_STOPPED) + { + TLS_BT_APPL_TRACE_WARNING("%s, ble service already stopped"NEW_LINE, __FUNCTION__); + return true; + } + + // 2 We check if the BLE MODEM is turned on + if(!is_ble_modem_on()) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble modem is not turned on"NEW_LINE, __FUNCTION__); + return false; + } + + switch(_ble_service_state) + { + case BLE_SERVICE_MODE_CONNECTED: + case BLE_SERVICE_MODE_INDICATING: + status = ble_gap_terminate(ble_device_conn_handle, BLE_ERR_REM_USER_CONN_TERM); + if(status == BLE_HS_ENOERR) + { + _ble_service_state = BLE_SERVICE_MODE_EXITING; + } + else //BLE_HS_EDISABLED || BLE_HS_ENOTCONN or any other error + { + TLS_BT_APPL_TRACE_WARNING("%s, ble_gap_terminate %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + _ble_service_state = BLE_SERVICE_MODE_STOPPED; + //Unregister gap event listener + 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)); + } + } + break; + case BLE_SERVICE_MODE_ADVERTISING: + if(!ble_service_advertise(false)) + { + TLS_BT_APPL_TRACE_WARNING("%s, ble_service_advertise failed to stop"NEW_LINE, __FUNCTION__); + } + else + { + _ble_service_state = BLE_SERVICE_MODE_STOPPED; + //Unregister gap event listener + 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)); + } + } + break; + default: //Just to keep compiler happy since BLE_SERVICE_MODE_STOPPED is rulled out earlier + break; + } + + if(_ble_service_state == BLE_SERVICE_MODE_STOPPED) + { + // We finally clean the gatt registered services + if((status = ble_gatts_reset()) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gatts_reset %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + } + + return true; +} + +bool ble_service_is_started(void) +{ + return _ble_service_state != BLE_SERVICE_MODE_STOPPED; +} + +bool ble_service_is_device_connected(void) +{ + return _ble_service_state == BLE_SERVICE_MODE_CONNECTED; +} + +void ble_service_register_state_change_cb(ble_service_state_change_fn_t ble_service_state_change_cb) +{ + _ble_service_state_change_cb = ble_service_state_change_cb; +} + +ble_service_state_e ble_service_get_state(void) +{ + return _ble_service_state; +} + +bool ble_service_update_connection_parameters( + uint16_t itvl_min, + uint16_t itvl_max, + uint16_t latency, + uint16_t supervision_timeout, + uint16_t min_ce_len, + uint16_t max_ce_len) +{ + if(BLE_HS_CONN_HANDLE_NONE == ble_device_conn_handle) + { + TLS_BT_APPL_TRACE_ERROR("%s, no active connection" NEW_LINE, __FUNCTION__); + return false; + } + + int status = BLE_HS_ENOERR; + + struct ble_gap_upd_params gap_params_to_apply = {0}; + gap_params_to_apply.itvl_min = itvl_min; + gap_params_to_apply.itvl_max = itvl_max; + gap_params_to_apply.latency = latency; + gap_params_to_apply.supervision_timeout = supervision_timeout; + gap_params_to_apply.min_ce_len = min_ce_len; + gap_params_to_apply.max_ce_len = max_ce_len; + + if ((status = ble_gap_update_params(ble_device_conn_handle, &gap_params_to_apply)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_update_params failed %s" NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + return true; +} + +bool ble_service_request_mtu_exchange(void) +{ + if(BLE_HS_CONN_HANDLE_NONE == ble_device_conn_handle) + { + TLS_BT_APPL_TRACE_ERROR("%s, no active connection" NEW_LINE, __FUNCTION__); + return false; + } + + int status = BLE_HS_ENOERR; + + if((status = ble_gattc_exchange_mtu(ble_device_conn_handle, &(ble_gatt_mtu_cb), NULL)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gattc_exchange_mtu %s" NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + return true; +} + +bool ble_service_send_nus_data(const uint8_t *data, uint16_t length) +{ + // The NUS is TX is using notifications + if(notification_data.transfer_in_progress) + { + TLS_BT_APPL_TRACE_WARNING("%s, a transfer is already in progress"NEW_LINE, __FUNCTION__); + return false; + } + + notification_data.transfer_in_progress = true; + notification_data.data = data; + notification_data.length = length; + + return ble_service_send_custom_notification(gatt_nus_char_tx_handle, ¬ification_data); +} + +void ble_service_register_nus_data_rx_cb(nus_data_rx_fn_t nus_data_rx_cb) +{ + _ble_service_nus_data_rx_cb = nus_data_rx_cb; +} +/** + * PRIVATE FUNCTION DEFINITION + * Used for the internal workings of the service + */ + +static bool ble_service_send_raw_custom_notification(uint16_t characteristic_handle, const uint8_t *data, uint16_t length) +{ + if(BLE_HS_CONN_HANDLE_NONE == ble_device_conn_handle) + { + TLS_BT_APPL_TRACE_ERROR("%s, no active connection" NEW_LINE, __FUNCTION__); + return false; + } + + if(!data || !length) + { + TLS_BT_APPL_TRACE_ERROR("%s, no data" NEW_LINE, __FUNCTION__); + return false; + } + + int status = BLE_HS_ENOERR; + struct os_mbuf *om_buf = ble_hs_mbuf_from_flat(data, length); + + if(!om_buf) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_hs_mbuf_from_flat" NEW_LINE, __FUNCTION__); + return false; + } + + if((status = ble_gattc_notify_custom(ble_device_conn_handle, characteristic_handle, om_buf)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_hs_mbuf_from_flat %s" NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + return true; +} + +static bool ble_service_send_custom_notification(uint16_t characteristic_handle, data_being_sent_t * const notif_data) +{ + if(!notif_data) + { + TLS_BT_APPL_TRACE_WARNING("%s, notif_data is NULL"NEW_LINE, __FUNCTION__); + return false; + } + + // We compute the maximum size of the data we can send: + uint16_t remaining_data_to_send = notif_data->length - notif_data->offset; + notif_data->sent_chunk_size = remaining_data_to_send <= usable_mtu ? remaining_data_to_send : usable_mtu; + + if(!ble_service_send_raw_custom_notification(characteristic_handle, ¬if_data->data[notif_data->offset], notif_data->sent_chunk_size)) + { + //Transfer failed : + reset_data_being_sent(notif_data); + return false; + } + + return true; +} + +int ble_gatt_mtu_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t mtu, void *arg) +{ + TLS_BT_APPL_TRACE_DEBUG("ble_gatt_mtu_cb"NEW_LINE); + + switch(error->status) + { + case 0: + TLS_BT_APPL_TRACE_DEBUG("mtu exchange complete: conn_handle=%d mtu=%d"NEW_LINE, + conn_handle, mtu); + usable_mtu = mtu - 3; + break; + default: + TLS_BT_APPL_TRACE_ERROR("Update MTU failed...error->status=%d"NEW_LINE, error->status); + break; + } + + return BLE_HS_ENOERR; +} + +static bool ble_service_define_gatt(const struct ble_gatt_svc_def *gatt_svc) +{ + int status = BLE_HS_ENOERR; + + if((status = ble_gatts_count_cfg(gatt_svc)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gatts_count_cfg failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + if((status = ble_gatts_add_svcs(gatt_svc)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gatts_add_svcs failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + return status == BLE_HS_ENOERR ? true : false; +} + +static bool ble_service_advertise(bool enable) +{ + int status = BLE_HS_ENOERR; + + if(enable) + { + struct ble_hs_adv_fields advertisement_fields = {0}; + uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM; + + advertisement_fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP; + + advertisement_fields.appearance = ble_svc_gap_device_appearance(); + advertisement_fields.appearance_is_present = 1; + + // Set the name of a watch supported by GB + #ifdef GADGETBRIDGE_SUPPORT + static const char dev_name[12] = "Bangle.js 2"; + advertisement_fields.name = (uint8_t *)dev_name;//ble_svc_gap_device_name(); + advertisement_fields.name_len = 11;//strlen(ble_svc_gap_device_name()); + #else + advertisement_fields.name = (uint8_t *)ble_svc_gap_device_name(); + advertisement_fields.name_len = strlen(ble_svc_gap_device_name()); + #endif + + advertisement_fields.name_is_complete = 1; + + advertisement_fields.uuids16 = (ble_uuid16_t[]) + { + BLE_UUID16_INIT(BLE_DEVICE_ADV_SERVICE) + }; + advertisement_fields.num_uuids16 = 1; + advertisement_fields.uuids16_is_complete = 1; + + // Lets apply the advertisement data + if((status = ble_gap_adv_set_fields(&advertisement_fields)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_adv_set_fields failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + + // We the device address + uint8_t device_addr[6] = {0}; + extern int tls_get_bt_mac_addr(u8 *mac); + tls_get_bt_mac_addr(device_addr); + + // Make sure the the device address is compliant with the random address specification : + device_addr[5] |= 0xC0; + + if((status = ble_hs_id_set_rnd(device_addr)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_hs_id_set_rnd failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + else + { + TLS_BT_APPL_TRACE_VERBOSE("addr type : %s"NEW_LINE"device addr : %02X:%02X:%02X:%02X:%02X:%02X"NEW_LINE, + tls_bt_addr_type_2_str(own_addr_type), + device_addr[5], + device_addr[4], + device_addr[3], + device_addr[2], + device_addr[1], + device_addr[0]); + } + + // We are now ready to configure the advertisement parameters + struct ble_gap_adv_params advertisement_params = {0}; + + advertisement_params.conn_mode = BLE_GAP_CONN_MODE_UND; + advertisement_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + + advertisement_params.itvl_min = 160; + advertisement_params.itvl_max = 160; //160 / 0.625 = 100 ms + advertisement_params.filter_policy = 0; + advertisement_params.channel_map = 0; // Use sane default + advertisement_params.high_duty_cycle = 0; + + if((status = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER, &advertisement_params, &(ble_advertise_gap_event_cb), NULL)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_adv_start failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + } + else + { + if((status = ble_gap_adv_stop()) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_adv_stop failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + return false; + } + } + + return true; +} + + +// This is the brain of the ble service, here we handle all the possible GAP events +static int ble_gap_event_cb(struct ble_gap_event *event, void *arg) +{ + int status = BLE_HS_ENOERR; + struct ble_gap_conn_desc desc; + bool error = false; + + TLS_BT_APPL_TRACE_EVENT("ble_gap_event_cb : %s"NEW_LINE, tls_bt_gap_evt_2_str(event->type)); + + switch(event->type) + { + case BLE_GAP_EVENT_CONNECT: + if(event->connect.status == BLE_HS_ENOERR) + { + if((status = ble_gap_conn_find(event->connect.conn_handle, &desc)) == BLE_HS_ENOERR) + { + print_conn_desc(&desc); + if(desc.role == BLE_GAP_ROLE_SLAVE) + { + _ble_service_state = BLE_SERVICE_MODE_CONNECTED; + usable_mtu = USABLE_DEFAULT_MTU; + ble_device_conn_handle = event->connect.conn_handle; + + //We call the state change callback if registered + if(_ble_service_state_change_cb)_ble_service_state_change_cb(_ble_service_state); + } + else + { + error = true; + if((status = ble_gap_terminate(event->connect.conn_handle, BLE_ERR_REM_USER_CONN_TERM)) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_WARNING("%s, ble_gap_terminate %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + } + } + } + else + { + error = true; + } + + //Resume advertising : + if(error) + { + TLS_BT_APPL_TRACE_VERBOSE("Remote device failed to connect, advertise again"NEW_LINE); + if(!ble_service_advertise(true)) + { + _ble_service_state = BLE_SERVICE_MODE_IDLE; + return BLE_HS_EUNKNOWN; + } + _ble_service_state = BLE_SERVICE_MODE_ADVERTISING; + } + break; + case BLE_GAP_EVENT_DISCONNECT: + if(event->disconnect.conn.role != BLE_GAP_ROLE_SLAVE) return 0; + + TLS_BT_APPL_TRACE_DEBUG("Server disconnect reason=%d[0x%02x],state=%s"NEW_LINE, event->disconnect.reason,event->disconnect.reason-0x200, + ble_service_state_2_str(_ble_service_state)); + + // Don't forget to invalidate the connection handle : + ble_device_conn_handle = BLE_HS_CONN_HANDLE_NONE; + + 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)); + } + + // We finally clean the gatt registered services + if((status = ble_gatts_reset()) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, ble_gatts_reset %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + } + else + { + // Let's advertise again + TLS_BT_APPL_TRACE_VERBOSE("Service disconnect event, advertise again"NEW_LINE); + if(!ble_service_advertise(true)) + { + _ble_service_state = BLE_SERVICE_MODE_IDLE; + return BLE_HS_EUNKNOWN; + } + _ble_service_state = BLE_SERVICE_MODE_ADVERTISING; + } + + //We call the state change callback if registered + if(_ble_service_state_change_cb)_ble_service_state_change_cb(_ble_service_state); + break; + case BLE_GAP_EVENT_CONN_UPDATE: + TLS_BT_APPL_TRACE_DEBUG("Conn update status : %d"NEW_LINE, event->conn_update.status); + if((status = ble_gap_conn_find(event->connect.conn_handle, &desc)) == BLE_HS_ENOERR) + { + print_conn_desc(&desc); + } + else + { + TLS_BT_APPL_TRACE_WARNING("%s, ble_gap_conn_find %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + break; + case BLE_GAP_EVENT_SUBSCRIBE: + TLS_BT_APPL_TRACE_VERBOSE("Attr handle : %u"NEW_LINE + "cur_indicate : %u"NEW_LINE + "prev_indicate : %u"NEW_LINE + "cur_notify : %u"NEW_LINE + "prev_notify : %u"NEW_LINE, + event->subscribe.attr_handle, + event->subscribe.cur_indicate, + event->subscribe.prev_indicate, + event->subscribe.cur_notify, + event->subscribe.prev_notify + ); + break; + case BLE_GAP_EVENT_MTU: + TLS_BT_APPL_TRACE_VERBOSE("MTU update : %u"NEW_LINE, event->mtu.value); + usable_mtu = event->mtu.value - 3; + break; + case BLE_GAP_EVENT_NOTIFY_TX: + if(event->notify_tx.indication == 0) // Notification + { + TLS_BT_APPL_TRACE_VERBOSE("Type : notification"NEW_LINE); + if(event->notify_tx.status != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_WARNING("%s, notify_tx notification error %d, transfer in progress : %u"NEW_LINE, __FUNCTION__, event->notify_tx.status, notification_data.transfer_in_progress); + reset_data_being_sent(¬ification_data); + return BLE_HS_EUNKNOWN; + } + + TLS_BT_APPL_TRACE_DEBUG("transfer : %u"NEW_LINE"length : %u"NEW_LINE"offset : %u"NEW_LINE, + notification_data.transfer_in_progress, + notification_data.length, + notification_data.offset); + + // We update the offset : + notification_data.offset += notification_data.sent_chunk_size; + + if(notification_data.offset < notification_data.length) // Still data to send ? + { + if(!ble_service_send_custom_notification(event->notify_tx.attr_handle, ¬ification_data)) + { + TLS_BT_APPL_TRACE_ERROR("Failed to send next notification chunk, aborting"NEW_LINE); + reset_data_being_sent(¬ification_data); + return BLE_HS_EUNKNOWN; + } + } + else + { + TLS_BT_APPL_TRACE_VERBOSE("last data chunk sent, end of the transfer"NEW_LINE); + // All data has been sent, end of the transfer + reset_data_being_sent(¬ification_data); + } + } + else // Indication + { + TLS_BT_APPL_TRACE_WARNING("Indication not yet handled"NEW_LINE); + } + break; + default: + TLS_BT_APPL_TRACE_WARNING("unhandled event !"NEW_LINE); + } + + return BLE_HS_ENOERR; +} + +static int ble_advertise_gap_event_cb(struct ble_gap_event *event, void *arg) +{ + return BLE_HS_ENOERR; +} + +static int battery_level_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + int status = BLE_HS_ENOERR; + TLS_BT_APPL_TRACE_EVENT("battery_level_char_access_cb op : %s"NEW_LINE, tls_bt_access_opt_2_str(ctxt->op)); + switch(ctxt->op) + { + case BLE_GATT_ACCESS_OP_READ_CHR: + if(attr_handle == battery_level_char_handle) + { + TLS_BT_APPL_TRACE_VERBOSE("battery level reading"NEW_LINE); + if((status = os_mbuf_append(ctxt->om, &battery_level_value, sizeof(battery_level_value))) != BLE_HS_ENOERR) + { + TLS_BT_APPL_TRACE_ERROR("%s, battery level os_mbuf : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); + } + } + break; + default: + TLS_BT_APPL_TRACE_WARNING("unhandled operation !"NEW_LINE); + } + + return BLE_HS_ENOERR; +} + +static int gatt_nus_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + TLS_BT_APPL_TRACE_EVENT("gatt_nus_char_access_cb op : %s"NEW_LINE, tls_bt_access_opt_2_str(ctxt->op)); + + switch(ctxt->op) + { + case BLE_GATT_ACCESS_OP_WRITE_CHR: + { + struct os_mbuf *om_buf = ctxt->om; + while(om_buf) + { + // 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); + } + } + break; + default: + TLS_BT_APPL_TRACE_WARNING("unhandled operation !"NEW_LINE); + } + + return BLE_HS_ENOERR; +} + +static void print_conn_desc(const struct ble_gap_conn_desc *desc) +{ + TLS_BT_APPL_TRACE_VERBOSE("conn_handle : %u"NEW_LINE"conn_itvl : %u"NEW_LINE"conn_latency : %u"NEW_LINE"master_clock_accuracy : %u"NEW_LINE"role : %s"NEW_LINE"supervision_timeout : %u"NEW_LINE + "encrypted : %u"NEW_LINE"authenticated : %u"NEW_LINE"bonded : %u"NEW_LINE, + desc->conn_handle, + desc->conn_itvl, + desc->conn_latency, + desc->master_clock_accuracy, + desc->role == BLE_GAP_ROLE_MASTER ? "MASTER" : "SLAVE", + desc->supervision_timeout, + desc->sec_state.encrypted, + desc->sec_state.authenticated, + desc->sec_state.bonded); +} + +static void reset_data_being_sent(data_being_sent_t * const data) +{ + if(data) + { + memset(data, 0, sizeof(data_being_sent_t)); + } +} \ No newline at end of file diff --git a/src/W800_SDK_v1.00.10/app/ble/ble_service.h b/src/W800_SDK_v1.00.10/app/ble/ble_service.h new file mode 100644 index 0000000..b45d52a --- /dev/null +++ b/src/W800_SDK_v1.00.10/app/ble/ble_service.h @@ -0,0 +1,126 @@ +#ifndef BLE_SERVICE_H +#define BLE_SERVICE_H + +#include "wm_type_def.h" + +#ifndef CASE_RETURN_STR +#define CASE_RETURN_STR(const) case const: return #const; +#endif + +typedef void (*nus_data_rx_fn_t)(const uint8_t *data, uint16_t length); + +typedef enum +{ + BLE_SERVICE_MODE_STOPPED = 0x00, + BLE_SERVICE_MODE_IDLE, + BLE_SERVICE_MODE_ADVERTISING, + BLE_SERVICE_MODE_CONNECTED, + BLE_SERVICE_MODE_INDICATING, + BLE_SERVICE_MODE_EXITING +} ble_service_state_e; + +typedef void (*ble_service_state_change_fn_t)(ble_service_state_e service_state); + +/** + * @brief Resturns the corresponding enum name as a string + * + * @param state the enum value + * @return const char* the enum name as a string + */ +const char *ble_service_state_2_str(ble_service_state_e state); + +/** + * @brief Configures and starts the BLE service + * + * @return true on success + * @return false on failure + */ +bool ble_service_start(void); + +/** + * @brief Deinits and stops the BLE service + * + * @return true on success + * @return false on failure + */ +bool ble_service_stop(void); + +/** + * @brief Check whether the ble service is running or not + * + * @return true if it is running + * @return false if it is stopped + */ +bool ble_service_is_started(void); + +/** + * @brief Check whether a device is connected to the ble service or not + * + * @return true if a device is connected + * @return false if no device is connected + */ +bool ble_service_is_device_connected(void); + +/** + * @brief Registers a callback function called every time the state of the BLE service changes. + * For example, you can register a callback to know if a device connected to the service, or disconnected + * + * @param ble_service_state_change_cb the function to register as the callback. The ble_service_state parameter is set to the new state. + */ +void ble_service_register_state_change_cb(ble_service_state_change_fn_t ble_service_state_change_cb); + +/** + * @brief Returns the current state of the ble service + * + * @return ble_service_state_e + */ +ble_service_state_e ble_service_get_state(void); + +/** + * @brief Asks to update the current connection parameters + * /!\ A connection should be already active before calling this function. + * + * @param itvl_min Minimum value for connection interval in 1.25ms units + * @param itvl_max Maximum value for connection interval in 1.25ms units + * @param latency Connection latency + * @param supervision_timeout Supervision timeout in 10ms units + * @param min_ce_len Minimum length of connection event in 0.625ms units + * @param max_ce_len Maximum length of connection event in 0.625ms units + * @return true + * @return false + */ +bool ble_service_update_connection_parameters( + uint16_t itvl_min, + uint16_t itvl_max, + uint16_t latency, + uint16_t supervision_timeout, + uint16_t min_ce_len, + uint16_t max_ce_len); + +/** + * @brief Requests a MTU (Maximum Transmission Unit) update in order to hopefully, get something bigger than 20 bytes ... + * + * @return true on success + * @return false on failure + */ +bool ble_service_request_mtu_exchange(void); + +/** + * @brief Sends the provided payload of size length using the NUS (Nordic UART Service) TX characteristic + * + * @param data the data to send through the NUS + * @param length the lenght in byte of the data to send + * @return true on success + * @return false on failure + */ +bool ble_service_send_nus_data(const uint8_t *data, uint16_t length); + +/** + * @brief Registers a function which will be called every time data is received by the nus rx + * @note To unregister a callback, simply pass NULL to the function + * + * @param nus_data_rx_cb a pointer to the function to call of type nus_data_rx_fn_t + */ +void ble_service_register_nus_data_rx_cb(nus_data_rx_fn_t nus_data_rx_cb); + +#endif //BLE_APP_H \ No newline at end of file diff --git a/src/W800_SDK_v1.00.10/app/ble/bluetooth_sig_values.h b/src/W800_SDK_v1.00.10/app/ble/bluetooth_sig_values.h new file mode 100644 index 0000000..f13daab --- /dev/null +++ b/src/W800_SDK_v1.00.10/app/ble/bluetooth_sig_values.h @@ -0,0 +1,9 @@ +#ifndef BLUETOOTH_SIG_VALUES_H +#define BLUETOOTH_SIG_VALUES_H + +#define BLE_DEVICE_APPEARANCE (0x00C2) //Smart Watch +#define BLE_DEVICE_NAME "MDBT42Q_W800SW" + +#define BLE_DEVICE_ADV_SERVICE (0x180F) //Battery Service + +#endif //BLUETOOTH_SIG_VALUES_H \ No newline at end of file diff --git a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.c b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.c index 6403c50..3278dab 100644 --- a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.c +++ b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.c @@ -167,14 +167,14 @@ tls_bt_init(uint8_t uart_idx) ble_hs_cfg.gatts_register_cb = on_svr_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /* Initialize all packages. */ - nimble_port_init(); + nimble_port_init(); // 7 k ram gone --> 112691 /*Application levels code entry*/ - tls_ble_gap_init(); - tls_bt_util_init(); + tls_ble_gap_init(); // 0k ram gone + tls_bt_util_init(); // 0k ram gone /*Initialize the vuart interface and enable controller*/ - ble_hci_vuart_init(uart_idx); + ble_hci_vuart_init(uart_idx); // 47k gone --> 65423 /* As the last thing, process events from default event queue. */ - tls_nimble_start(); + tls_nimble_start(); // 3k gone --> 62175 while(bt_adapter_state == WM_BT_STATE_OFF) { tls_os_time_delay(10); diff --git a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.h b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.h index e41b2e6..90b4d7b 100644 --- a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.h +++ b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_app.h @@ -15,6 +15,11 @@ extern "C" { #endif +/** NOT PART OF THE OFFICIAL SDK **/ +int tls_bt_init(uint8_t uart_idx); +int tls_bt_deinit(void); +/**********************************/ + typedef enum { WM_BT_SYSTEM_ACTION_IDLE, diff --git a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.c b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.c index 576066f..86ae01a 100644 --- a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.c +++ b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.c @@ -109,9 +109,6 @@ const char *tls_bt_gap_evt_2_str(uint32_t event) } } - -#define BLE_HS_ENOERR 0 - const char *tls_bt_rc_2_str(uint32_t event) { switch(event) { @@ -153,7 +150,33 @@ const char *tls_bt_rc_2_str(uint32_t event) } } +const char *tls_bt_addr_type_2_str(uint8_t addr_type) +{ + switch(addr_type) + { + CASE_RETURN_STR(BLE_OWN_ADDR_PUBLIC) + CASE_RETURN_STR(BLE_OWN_ADDR_RANDOM) + CASE_RETURN_STR(BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT) + CASE_RETURN_STR(BLE_OWN_ADDR_RPA_RANDOM_DEFAULT) + + default: + return "unknown addr_type"; + } +} +const char *tls_bt_access_opt_2_str(uint8_t op) +{ + switch(op) + { + CASE_RETURN_STR(BLE_GATT_ACCESS_OP_READ_CHR) + CASE_RETURN_STR(BLE_GATT_ACCESS_OP_WRITE_CHR) + CASE_RETURN_STR(BLE_GATT_ACCESS_OP_READ_DSC) + CASE_RETURN_STR(BLE_GATT_ACCESS_OP_WRITE_DSC) + + default: + return "unknown operation type"; + } +} static void async_evt_func(struct ble_npl_event *ev) { diff --git a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.h b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.h index f3ad7b6..90c0e3c 100644 --- a/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.h +++ b/src/W800_SDK_v1.00.10/src/app/bleapp/wm_bt_util.h @@ -47,12 +47,15 @@ extern tls_bt_log_level_t tls_appl_trace_level; #define TLS_BT_APPL_TRACE_EVENT(...) #define TLS_BT_APPL_TRACE_DEBUG(...) #define TLS_BT_APPL_TRACE_VERBOSE(...) - #endif +#define BLE_HS_ENOERR 0 + void tls_bt_log(uint32_t trace_set_mask, const char *fmt_str, ...); const char *tls_bt_gap_evt_2_str(uint32_t event); const char *tls_bt_rc_2_str(uint32_t event); +const char *tls_bt_addr_type_2_str(uint8_t addr_type); +const char *tls_bt_access_opt_2_str(uint8_t op); extern int tls_bt_util_init(void); extern int tls_bt_util_deinit(void); diff --git a/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/include/syscfg/syscfg.h b/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/include/syscfg/syscfg.h index 9a58cc5..1b21cee 100644 --- a/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/include/syscfg/syscfg.h +++ b/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/include/syscfg/syscfg.h @@ -61,11 +61,11 @@ #endif #ifndef MYNEWT_VAL_MSYS_1_BLOCK_COUNT -#define MYNEWT_VAL_MSYS_1_BLOCK_COUNT (24) +#define MYNEWT_VAL_MSYS_1_BLOCK_COUNT (10) #endif #ifndef MYNEWT_VAL_MSYS_1_BLOCK_SIZE -#define MYNEWT_VAL_MSYS_1_BLOCK_SIZE (292) +#define MYNEWT_VAL_MSYS_1_BLOCK_SIZE (100) // Default 292 #endif #ifndef MYNEWT_VAL_MSYS_1_SANITY_MIN_COUNT @@ -925,7 +925,7 @@ /*** @apache-mynewt-nimble/nimble/transport/socket */ #ifndef MYNEWT_VAL_BLE_ACL_BUF_COUNT -#define MYNEWT_VAL_BLE_ACL_BUF_COUNT (36) +#define MYNEWT_VAL_BLE_ACL_BUF_COUNT (8) // Default 36 #endif #ifndef MYNEWT_VAL_BLE_ACL_BUF_SIZE @@ -947,11 +947,11 @@ #endif #ifndef MYNEWT_VAL_BLE_HCI_EVT_HI_BUF_COUNT -#define MYNEWT_VAL_BLE_HCI_EVT_HI_BUF_COUNT (16) +#define MYNEWT_VAL_BLE_HCI_EVT_HI_BUF_COUNT (5) // Default 16 #endif #ifndef MYNEWT_VAL_BLE_HCI_EVT_LO_BUF_COUNT -#define MYNEWT_VAL_BLE_HCI_EVT_LO_BUF_COUNT (96) +#define MYNEWT_VAL_BLE_HCI_EVT_LO_BUF_COUNT (5) // Default 96 #endif #ifndef MYNEWT_VAL_BLE_SOCK_CLI_SYSINIT_STAGE diff --git a/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/src/tls_nimble.c b/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/src/tls_nimble.c index 2d0b5c2..2848689 100644 --- a/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/src/tls_nimble.c +++ b/src/W800_SDK_v1.00.10/src/bt/blehost/porting/w800/src/tls_nimble.c @@ -17,7 +17,7 @@ void tls_nimble_start(void) { tls_host_task_stack_ptr = (void *)tls_mem_alloc(MYNEWT_VAL(OS_HS_STACK_SIZE) * sizeof(uint32_t)); assert(tls_host_task_stack_ptr != NULL); - tls_os_task_create(&tls_host_task_hdl, "bth", + tls_os_task_create(&tls_host_task_hdl, "ble_svc", nimble_host_task, (void *)0, (void *)tls_host_task_stack_ptr, diff --git a/src/W800_SDK_v1.00.10/tools/w800/inc.mk b/src/W800_SDK_v1.00.10/tools/w800/inc.mk index b6e8437..2791b23 100644 --- a/src/W800_SDK_v1.00.10/tools/w800/inc.mk +++ b/src/W800_SDK_v1.00.10/tools/w800/inc.mk @@ -43,21 +43,22 @@ INCLUDES += -I $(TOP_DIR)/src/os/rtos/include INCLUDES += -I $(TOP_DIR)/src/app/factorycmd INCLUDES += -I $(TOP_DIR)/src/app/bleapp -INCLUDES += -I $(TOP_DIR)/app/app_include + +INCLUDES += -I $(TOP_DIR)/app INCLUDES += -I $(TOP_DIR)/app/app_drivers/lcd INCLUDES += -I $(TOP_DIR)/app/app_drivers/mmc_sdio INCLUDES += -I $(TOP_DIR)/app/app_drivers/i2c INCLUDES += -I $(TOP_DIR)/app/app_drivers/watch_peripherals +INCLUDES += -I $(TOP_DIR)/app/app_include +INCLUDES += -I $(TOP_DIR)/app/ble +INCLUDES += -I $(TOP_DIR)/app/gfx INCLUDES += -I $(TOP_DIR)/app/persistency INCLUDES += -I $(TOP_DIR)/app/translation -INCLUDES += -I $(TOP_DIR)/app/gfx -INCLUDES += -I $(TOP_DIR)/app #lvgl include INCLUDES += -I $(TOP_DIR)/lvgl/lvgl_v8.3 INCLUDES += -I $(TOP_DIR)/lvgl/lvgl_port - #nimble host INCLUDES += -I $(TOP_DIR)/src/bt/blehost/ext/tinycrypt/include INCLUDES += -I $(TOP_DIR)/src/bt/blehost/nimble/host/include