From b3174c85721e67cdc75843175b730dfeb8b4b739 Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sun, 12 Mar 2023 21:52:03 +0100 Subject: [PATCH] Exposed two functions in the nano shell interface to be able to print characters only or plain strings, added new commands regarding ble in the nano shell for testing purposes --- app/nano_shell_command.c | 83 +++++++++++++++++++++++++----- app_include/nano_shell_interface.h | 2 + 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/app/nano_shell_command.c b/app/nano_shell_command.c index 2dabce9..c3567f8 100644 --- a/app/nano_shell_command.c +++ b/app/nano_shell_command.c @@ -22,14 +22,14 @@ extern int demo_bt_destroy(); extern int demo_ble_server_on(); extern int demo_ble_server_off(); -void tls_wifi_client_event_cb(u8 *mac, enum tls_wifi_client_event_type event) +static void tls_wifi_client_event_cb(u8 *mac, enum tls_wifi_client_event_type event) { struct tls_sta_info_t *mac_addr = (struct tls_sta_info_t *)mac; shell_printf("Client event(%d), MAC : %M"NEW_LINE, event, mac_addr); } -void wifi_scan_result_cb(void) +static void wifi_scan_result_cb(void) { u16 buffer_size = sizeof(struct tls_scan_bss_t) + sizeof(struct tls_bss_info_t) * 20; u8 *buf = tls_mem_alloc(buffer_size); @@ -67,7 +67,7 @@ void wifi_scan_result_cb(void) tls_wifi_scan_result_cb_register(NULL); } -void tls_wifi_data_ext_recv_cb(u8* data, u32 data_len, struct tls_wifi_ext_t *ext) +static void tls_wifi_data_ext_recv_cb(u8* data, u32 data_len, struct tls_wifi_ext_t *ext) { shell_printf("recv packet :"NEW_LINE"rssi : %d\nrate : %u"NEW_LINE, (s8)ext->rssi, ext->rx_rate); for(u32 i = 0; i < data_len; i++) @@ -79,7 +79,7 @@ void tls_wifi_data_ext_recv_cb(u8* data, u32 data_len, struct tls_wifi_ext_t *ex shell_printf(NEW_LINE); } -void tls_rtc_irq_cb(void *arg) +static void tls_rtc_irq_cb(void *arg) { struct tm rtc_time; tls_get_rtc(&rtc_time); @@ -95,6 +95,21 @@ void tls_rtc_irq_cb(void *arg) tls_rtc_timer_stop(); } +static void nus_data_rx_cb(const uint8_t *data, uint16_t length) +{ + shell_printf("%s, received data : ", __FUNCTION__); + + for (uint16_t i = 0; i < length; i++) + { + if (data[i] < 32) + shell_printf("[%u]", data[i]); + else + shell_putc(data[i]); + } + + shell_puts(NEW_LINE); +} + int _system(const shell_cmd_t *pcmd, int argc, char *const argv[]) { if(argc > 1) @@ -513,25 +528,62 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) if(strcmp(argv[1], "send_ble_notif") == 0) { char cmd[200] = ""; + bool found = false; 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"); + found = true; } 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"); + sprintf(cmd, "{\"t\":\"status\",\"bat\":%s,\"chg\":%s,\"volt\":%s} \n", argv[3], argv[4], argv[5]); + found = true; } 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"); + strcpy(cmd, "{\"t\":\"findPhone\",\"n\":true} \n"); + found = true; } + else if(strcmp(argv[2], "music") == 0) + { + sprintf(cmd, "{\"t\":\"music\",\"n\":\"%s\"} \n", argv[3]); + found = true; + } + else if(strcmp(argv[2], "notify") == 0) + { + sprintf(cmd, "{\"t\":\"notify\",\"n\":\"%s\",\"id\":%s,\"tel\":\"%s\",\"msg\":\"%s\"} \n", argv[3], argv[4], argv[5], argv[6]); + found = true; + } + + if(found) + { + shell_printf("Sending ble ntf with content : #%s# -> %s"NEW_LINE, cmd, ble_service_nus_send_data((const uint8_t *)cmd, strlen(cmd)) ? "success" : "failure"); + } + 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]); + } + } + else if(strcmp(argv[1], "up_conn_param") == 0) + { + if(argc != 8) + { + shell_printf("6 arguments expected"NEW_LINE); + return 0; + } + //First we need to retrieve every parameters from the shell + uint16_t itvl_min = strtoul(argv[2], NULL, 10); + uint16_t itvl_max = strtoul(argv[3], NULL, 10); + uint16_t latency = strtoul(argv[4], NULL, 10); + uint16_t supervision_timeout = strtoul(argv[5], NULL, 10); + uint16_t min_ce_len = strtoul(argv[6], NULL, 10); + uint16_t max_ce_len = strtoul(argv[7], NULL, 10); + + shell_printf("BLE connection update request : %d"NEW_LINE, ble_service_update_connection_parameters(itvl_min, itvl_max, latency, supervision_timeout, min_ce_len, max_ce_len)); } 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]); + shell_printf("Unknown %s action"NEW_LINE, argv[0]); } } else if(argc > 1) @@ -546,11 +598,17 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) } 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, ble_service_start() /*demo_ble_server_on()*/); + bool result = ble_service_start(); + shell_printf("Starting demo : %d"NEW_LINE"Use a BLE app to find the device"NEW_LINE, result /*demo_ble_server_on()*/); + if(result) + { + ble_service_nus_register_data_rx_cb(&(nus_data_rx_cb)); + } } else if(strcmp(argv[1], "stop_demo") == 0) { shell_printf("Stopping demo : %d"NEW_LINE, ble_service_stop() /*demo_ble_server_off()*/); + ble_service_nus_register_data_rx_cb(NULL); } else if(strcmp(argv[1], "mtu_exch") == 0) { @@ -563,7 +621,8 @@ int _bluetooth(const shell_cmd_t *pcmd, int argc, char *const argv[]) } else { - 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); + 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|music|notify"NEW_LINE"mtu_exch"NEW_LINE + "up_conn_param itvl_min itvl_max latency supervision_timeout min_ce_len max_ce_len"NEW_LINE); } return 0; } diff --git a/app_include/nano_shell_interface.h b/app_include/nano_shell_interface.h index ab49c99..bceb86b 100644 --- a/app_include/nano_shell_interface.h +++ b/app_include/nano_shell_interface.h @@ -2,5 +2,7 @@ #define NANO_SHELL_INTERFACE_H extern int shell_printf(const char *format, ...); +extern void shell_puts(const char *str); +extern void shell_putc(char ch); #endif //NANO_SHELL_INTERFACE_H \ No newline at end of file