From 2854c45848b4b95e0979c622ed37ad689f0fbe6a Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sat, 11 Mar 2023 23:34:02 +0100 Subject: [PATCH] Reworked a few existing SDK files to prepare for the ble modem and service addon, updated the nano_shell_command by adding new ones around ble --- Makefile | 2 ++ app/nano_shell_command.c | 49 ++++++++++++++++++++++++++++++------- src/app/bleapp/wm_bt_app.h | 3 +++ src/app/bleapp/wm_bt_util.c | 29 +++++++++++++++++++--- src/app/bleapp/wm_bt_util.h | 5 +++- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b08048b..cdb8755 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ GEN_BINS = $(TARGET).bin SUBDIRS = \ $(TOP_DIR)/app \ $(TOP_DIR)/app/app_lib \ + $(TOP_DIR)/app/ble \ $(TOP_DIR)/app/third_party/nano-shell-master \ $(TOP_DIR)/app/third_party/driver/NRF24L01P endif # } PDIR @@ -34,6 +35,7 @@ endif COMPONENTS_$(TARGET) = \ $(TOP_DIR)/app/libuser$(LIB_EXT) \ $(TOP_DIR)/app/app_lib/libapplib$(LIB_EXT) \ + $(TOP_DIR)/app/app_lib/libble$(LIB_EXT) \ $(TOP_DIR)/app/third_party/nano-shell-master/libnanoshell$(LIB_EXT) \ $(TOP_DIR)/app/third_party/driver/NRF24L01P/libnrf24l01p$(LIB_EXT) diff --git a/app/nano_shell_command.c b/app/nano_shell_command.c index 9a51894..2dabce9 100644 --- a/app/nano_shell_command.c +++ b/app/nano_shell_command.c @@ -10,6 +10,9 @@ #include "nano_shell_interface.h" #include "wm_gpio_afsel.h" +#include "ble_modem.h" +#include "ble_service.h" + extern int wm_printf(const char *fmt,...); extern u32 tls_mem_get_avail_heapsize(void); extern bool disconnect_client(void); @@ -505,34 +508,62 @@ int _rtc(const shell_cmd_t *pcmd, int argc, char *const argv[]) int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) { - if(argc > 1) + if(argc >= 3) + { + if(strcmp(argv[1], "send_ble_notif") == 0) + { + char cmd[200] = ""; + if(strcmp(argv[2], "toast") == 0) + { + sprintf(cmd, "{\"t\":\"info\",\"msg\":\"%s\"} \n", argv[3]); + shell_printf("Sending ble ntf with content : #%s# -> %s"NEW_LINE, cmd, ble_service_send_custom_notification((const uint8_t *)cmd, strlen(cmd)) ? "success" : "failure"); + } + else if(strcmp(argv[2], "bat") == 0) + { + sprintf(cmd, "{\"t\":\"status\",\"bat\":%s} \n", argv[3]); + shell_printf("Sending ble ntf with content : #%s# -> %s"NEW_LINE, cmd, ble_service_send_custom_notification((const uint8_t *)cmd, strlen(cmd)) ? "success" : "failure"); + } + else if(strcmp(argv[2], "findPhone") == 0) + { + char cmd[] = "{\"t\":\"findPhone\",\"n\":true} \n"; + shell_printf("Sending ble ntf with content : #%s# -> %s"NEW_LINE, cmd, ble_service_send_custom_notification((const uint8_t *)cmd, strlen(cmd)) ? "success" : "failure"); + } + } + else + { + shell_printf("Unknown %s action"NEW_LINE"List of send_ble_notif actions :"NEW_LINE"toast \"msg\""NEW_LINE"bat \"%%\""NEW_LINE"findPhone", argv[0]); + } + } + else if(argc > 1) { if(strcmp(argv[1], "enable") == 0) { - shell_printf("Enabling bluetooth : %d"NEW_LINE, demo_bt_enable()); + shell_printf("Enabling bluetooth : %d"NEW_LINE, ble_modem_on()); } else if(strcmp(argv[1], "disable") == 0) { - shell_printf("Disabling bluetooth : %d"NEW_LINE, demo_bt_destroy()); - //Starting a wifi scan really stops the BT modem ?? Why ? I don't know - tls_wifi_scan(); + shell_printf("Disabling bluetooth : %d"NEW_LINE, ble_modem_off()); } else if(strcmp(argv[1], "start_demo") == 0) { - shell_printf("Starting demo : %d"NEW_LINE"Use a BLE app to find the device"NEW_LINE, demo_ble_server_on()); + shell_printf("Starting demo : %d"NEW_LINE"Use a BLE app to find the device"NEW_LINE, ble_service_start() /*demo_ble_server_on()*/); } else if(strcmp(argv[1], "stop_demo") == 0) { - shell_printf("Stopping demo : %d"NEW_LINE, demo_ble_server_off()); + shell_printf("Stopping demo : %d"NEW_LINE, ble_service_stop() /*demo_ble_server_off()*/); + } + else if(strcmp(argv[1], "mtu_exch") == 0) + { + shell_printf("MTU exchange request : %d"NEW_LINE, ble_service_request_mtu_exchange()); } else { shell_printf("Unknown %s action"NEW_LINE, argv[0]); } - } + } else { - shell_printf("List of bluetooth actions :"NEW_LINE"enable"NEW_LINE"disable"NEW_LINE"start_demo"NEW_LINE"stop_demo"NEW_LINE); + shell_printf("List of bluetooth actions :"NEW_LINE"enable"NEW_LINE"disable"NEW_LINE"start_demo"NEW_LINE"stop_demo"NEW_LINE"send_ble_notif toast \"msg\"|bat \"%%\"|findPhone"NEW_LINE"mtu_exch"NEW_LINE); } return 0; } diff --git a/src/app/bleapp/wm_bt_app.h b/src/app/bleapp/wm_bt_app.h index e41b2e6..45dedbb 100644 --- a/src/app/bleapp/wm_bt_app.h +++ b/src/app/bleapp/wm_bt_app.h @@ -15,6 +15,9 @@ extern "C" { #endif +int tls_bt_init(uint8_t uart_idx); +int tls_bt_deinit(void); + typedef enum { WM_BT_SYSTEM_ACTION_IDLE, diff --git a/src/app/bleapp/wm_bt_util.c b/src/app/bleapp/wm_bt_util.c index 576066f..86ae01a 100644 --- a/src/app/bleapp/wm_bt_util.c +++ b/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/app/bleapp/wm_bt_util.h b/src/app/bleapp/wm_bt_util.h index f3ad7b6..90c0e3c 100644 --- a/src/app/bleapp/wm_bt_util.h +++ b/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);