From 8395d9fac473b9d82fb1e8a75bd63ef6294f7a37 Mon Sep 17 00:00:00 2001 From: TheSomeMan Date: Mon, 21 Oct 2024 13:22:39 +0700 Subject: [PATCH] Add support for BLE extended advertisements (#30) * Add support for BLE extended advertisements * Add RI_ADV_EXTENDED_ENABLED=1 * Add support for ADV_RPRT2 format, configure max ext adv len, refactoring * Add support for ADV_RPRT2 format, configure max ext adv len, refactoring --- .gitignore | 2 + CMakeLists.txt | 19 +++++--- components/ruuvi.endpoints.c | 2 +- src/app/comm_tester.c | 61 ++++++++++++----------- src/lib/api.c | 93 +++++++++++++++++------------------- src/lib/api.h | 9 ++-- src/lib/parser.c | 2 + src/lib/parser.h | 8 ++-- tests/run_tests.sh | 8 ++++ 9 files changed, 114 insertions(+), 90 deletions(-) create mode 100755 tests/run_tests.sh diff --git a/.gitignore b/.gitignore index 6cf19ff..b3dac76 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,5 @@ Mkfile.old dkms.conf output/ build/ +cmake-build* +.idea/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 2754c1c..ea30441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.7) +set(RI_COMM_BLE_PAYLOAD_MAX_LENGTH 48) + if(${ESP_PLATFORM}) string(APPEND CMAKE_C_FLAGS " -DRUUVI_ESP") @@ -21,6 +23,9 @@ idf_component_register( ruuvi.endpoints.c ) +target_compile_definitions(${COMPONENT_LIB} PRIVATE RI_ADV_EXTENDED_ENABLED=1 RI_COMM_BLE_PAYLOAD_MAX_LENGTH=${RI_COMM_BLE_PAYLOAD_MAX_LENGTH}) +target_compile_definitions(__idf_ruuvi.endpoints.c PRIVATE RI_ADV_EXTENDED_ENABLED=1 RI_COMM_BLE_PAYLOAD_MAX_LENGTH=${RI_COMM_BLE_PAYLOAD_MAX_LENGTH}) + else() project(ruuvi_comm_tester) @@ -39,15 +44,15 @@ set(COMPONENTS components) set(RUUVI_LIB_SOURCES src/lib/api.c src/lib/parser.c - src/lib/dbuscontroller.c - src/lib/formated_output.c + src/lib/dbuscontroller.c + src/lib/formated_output.c ${COMPONENTS}/ruuvi.endpoints.c/src/ruuvi_endpoint_ca_uart.c ) include_directories( - /usr/include - /usr/include/dbus-1.0 - /usr/lib/x86_64-linux-gnu/dbus-1.0/include + /usr/include + /usr/include/dbus-1.0 + /usr/lib/x86_64-linux-gnu/dbus-1.0/include src src/app src/lib @@ -68,6 +73,8 @@ set_target_properties(${ProjectId} PROPERTIES target_compile_definitions(${ProjectId} PUBLIC _DEFAULT_SOURCE + RI_ADV_EXTENDED_ENABLED=1 + RI_COMM_BLE_PAYLOAD_MAX_LENGTH=${RI_COMM_BLE_PAYLOAD_MAX_LENGTH} ) target_compile_options(${ProjectId} PUBLIC @@ -81,7 +88,7 @@ target_compile_options(${ProjectId} PUBLIC target_link_libraries(${ProjectId} pthread - dbus-1 + dbus-1 ) endif() diff --git a/components/ruuvi.endpoints.c b/components/ruuvi.endpoints.c index 8b817f0..b72e510 160000 --- a/components/ruuvi.endpoints.c +++ b/components/ruuvi.endpoints.c @@ -1 +1 @@ -Subproject commit 8b817f05cfaad7ed462a3f50b3646af84fdacf86 +Subproject commit b72e510481523839f167fa1143219fa2d0c10a0c diff --git a/src/app/comm_tester.c b/src/app/comm_tester.c index a8f57dd..4ec6d70 100644 --- a/src/app/comm_tester.c +++ b/src/app/comm_tester.c @@ -25,14 +25,14 @@ #define MAX_PARAMS_NUM_SIZE 255 #define IS_PAYLOAD_SET(x) (x->is_set) -#define DEFAULT_FLTR_ID_NUM 0 -#define DEFAULT_FLTR_TAGS_NUM 1 -#define DEFAULT_CODED_PHY_NUM 2 -#define DEFAULT_EXT_PAYLOAD_NUM 3 -#define DEFAULT_SCAN_PHY_NUM 4 -#define DEFAULT_CH_37_NUM 5 -#define DEFAULT_CH_38_NUM 6 -#define DEFAULT_CH_39_NUM 7 +#define DEFAULT_FLTR_ID_NUM 0 +#define DEFAULT_FLTR_TAGS_NUM 1 +#define DEFAULT_USE_CODED_PHY_NUM 2 +#define DEFAULT_USE_2M_PHY_NUM 3 +#define DEFAULT_USE_1M_PHY_NUM 4 +#define DEFAULT_CH_37_NUM 5 +#define DEFAULT_CH_38_NUM 6 +#define DEFAULT_CH_39_NUM 7 static void help(void) @@ -47,11 +47,12 @@ help(void) print_logmsgnofuncnoarg("-c7 : set channel 37 value\n"); print_logmsgnofuncnoarg("-c8 : set channel 38 value\n"); print_logmsgnofuncnoarg("-c9 : set channel 39 value\n"); + print_logmsgnofuncnoarg("-m : set max extended advertisement len\n"); print_logmsgnofuncnoarg("-gi : get device id\n"); print_logmsgnofuncnoarg("-l : turn on LED for \n"); print_logmsgnofuncnoarg("-o : disable report output\n"); print_logmsgnofuncnoarg("-r : run receiver mode\n"); - print_logmsgnofuncnoarg("-b : run receiver in backgroung mode\n"); + print_logmsgnofuncnoarg("-b : run receiver in background mode\n"); print_logmsgnofuncnoarg("-h : help\n"); } @@ -72,23 +73,23 @@ typedef struct { comm_tester_param_input_t fltr_id; comm_tester_param_input_t fltr_tags_state; - comm_tester_param_input_t coded_phy_state; - comm_tester_param_input_t ext_payload_state; - comm_tester_param_input_t scan_phy_state; + comm_tester_param_input_t use_coded_phy_state; + comm_tester_param_input_t use_2m_phy_state; + comm_tester_param_input_t use_1m_phy_state; comm_tester_param_input_t ch_37_state; comm_tester_param_input_t ch_38_state; comm_tester_param_input_t ch_39_state; + comm_tester_param_input_t max_adv_len; uint32_t all_state; } comm_tester_input_t; -comm_tester_input_t in; -comm_tester_param_input_t *in_array[] = { - &in.fltr_id, &in.fltr_tags_state, &in.coded_phy_state, &in.ext_payload_state, - &in.scan_phy_state, &in.ch_37_state, &in.ch_38_state, &in.ch_39_state, -}; +comm_tester_input_t in = { 0 }; +comm_tester_param_input_t *in_array[] = { &in.fltr_id, &in.fltr_tags_state, &in.use_coded_phy_state, + &in.use_2m_phy_state, &in.use_1m_phy_state, &in.ch_37_state, + &in.ch_38_state, &in.ch_39_state }; uint32_t cmd_array[] = { - RE_CA_UART_SET_FLTR_ID, RE_CA_UART_SET_FLTR_TAGS, RE_CA_UART_SET_CODED_PHY, RE_CA_UART_SET_EXT_PAYLOAD, + RE_CA_UART_SET_FLTR_ID, RE_CA_UART_SET_FLTR_TAGS, RE_CA_UART_SET_CODED_PHY, RE_CA_UART_SET_SCAN_2MB_PHY, RE_CA_UART_SET_SCAN_1MB_PHY, RE_CA_UART_SET_CH_37, RE_CA_UART_SET_CH_38, RE_CA_UART_SET_CH_39, }; @@ -149,19 +150,25 @@ main(int argc, char *argv[]) break; case 'p': case 'P': - param_num = DEFAULT_CODED_PHY_NUM; + param_num = DEFAULT_USE_CODED_PHY_NUM; break; case 's': case 'S': - param_num = DEFAULT_SCAN_PHY_NUM; + param_num = DEFAULT_USE_1M_PHY_NUM; break; case 'e': case 'E': - param_num = DEFAULT_EXT_PAYLOAD_NUM; + param_num = DEFAULT_USE_2M_PHY_NUM; break; case 'l': case 'L': led_ctrl = atoi(argv[i + 1]); + break; + case 'm': + case 'M': + in.max_adv_len.payload = atoi(argv[i + 1]); + in.max_adv_len.is_set = PAYLOAD_INPUT_YES; + break; case 'g': case 'G': switch (argv[i][2]) @@ -237,19 +244,19 @@ main(int argc, char *argv[]) { if (terminal_open(deviceCom, false, terminal_task_priority) == 0) { - if (in.all_state >= MAX_PARAMS_NUM_SIZE) { - res = api_send_all( + res = (int)api_send_all( RE_CA_UART_SET_ALL, (uint16_t)in_array[DEFAULT_FLTR_ID_NUM]->payload, (uint8_t)in_array[DEFAULT_FLTR_TAGS_NUM]->payload, - (uint8_t)in_array[DEFAULT_CODED_PHY_NUM]->payload, - (uint8_t)in_array[DEFAULT_EXT_PAYLOAD_NUM]->payload, - (uint8_t)in_array[DEFAULT_SCAN_PHY_NUM]->payload, + (uint8_t)in_array[DEFAULT_USE_CODED_PHY_NUM]->payload, + (uint8_t)in_array[DEFAULT_USE_2M_PHY_NUM]->payload, + (uint8_t)in_array[DEFAULT_USE_1M_PHY_NUM]->payload, (uint8_t)in_array[DEFAULT_CH_37_NUM]->payload, (uint8_t)in_array[DEFAULT_CH_38_NUM]->payload, - (uint8_t)in_array[DEFAULT_CH_39_NUM]->payload); + (uint8_t)in_array[DEFAULT_CH_39_NUM]->payload, + (uint8_t)in.max_adv_len.payload); } else { diff --git a/src/lib/api.c b/src/lib/api.c index d95cce3..2e425f9 100644 --- a/src/lib/api.c +++ b/src/lib/api.c @@ -20,7 +20,6 @@ /***USER_DEFINES***/ /*start*/ -#define BUFFER_PAYLOAD_SIZE 204 /*end*/ /***USER_TYPES**/ @@ -64,10 +63,10 @@ static bool g_flag_dont_print_output_report = false; int8_t api_send_bool_payload(uint32_t cmd, uint8_t state) { - int8_t res = 0; - re_ca_uart_payload_t uart_payload = { 0 }; - uint8_t data[BUFFER_PAYLOAD_SIZE]; - uint8_t data_length; + int8_t res = 0; + re_ca_uart_payload_t uart_payload = { 0 }; + re_ca_uart_mosi_payload_buf_encoded_bool_t data; + uint8_t data_length = sizeof(data.buf); print_dbgmsgnoarg("Enter\n"); @@ -82,13 +81,13 @@ api_send_bool_payload(uint32_t cmd, uint8_t state) } data_length = sizeof(data); - if (RE_SUCCESS != re_ca_uart_encode(data, &data_length, &uart_payload)) + if (RE_SUCCESS != re_ca_uart_encode(data.buf, &data_length, &uart_payload)) { res = (-1); } else { - terminal_send_msg((uint8_t *)data, data_length); + terminal_send_msg((uint8_t *)data.buf, data_length); #ifndef RUUVI_ESP dbus_check_new_messages(); #endif @@ -100,22 +99,21 @@ api_send_bool_payload(uint32_t cmd, uint8_t state) int8_t api_send_get_device_id(uint32_t cmd) { - int8_t res = 0; - re_ca_uart_payload_t uart_payload = { 0 }; - uint8_t data[BUFFER_PAYLOAD_SIZE]; - uint8_t data_length; + int8_t res = 0; + re_ca_uart_payload_t uart_payload = { 0 }; + re_ca_uart_mosi_payload_buf_encoded_get_device_id_t data; + uint8_t data_length = sizeof(data.buf); print_dbgmsgnoarg("Enter\n"); uart_payload.cmd = (re_ca_uart_cmd_t)cmd; - data_length = sizeof(data); - if (RE_SUCCESS != re_ca_uart_encode(data, &data_length, &uart_payload)) + if (RE_SUCCESS != re_ca_uart_encode(data.buf, &data_length, &uart_payload)) { res = (-1); } else { - terminal_send_msg((uint8_t *)data, data_length); + terminal_send_msg((uint8_t *)data.buf, data_length); #ifndef RUUVI_ESP dbus_check_new_messages(); #endif @@ -127,24 +125,23 @@ api_send_get_device_id(uint32_t cmd) int8_t api_send_fltr_id(uint32_t cmd, uint16_t id) { - int8_t res = 0; - re_ca_uart_payload_t uart_payload = { 0 }; - uint8_t data[BUFFER_PAYLOAD_SIZE]; - uint8_t data_length; + int8_t res = 0; + re_ca_uart_payload_t uart_payload = { 0 }; + re_ca_uart_mosi_payload_buf_encoded_set_fltr_id_t data; + uint8_t data_length = sizeof(data.buf); print_dbgmsgnoarg("Enter\n"); uart_payload.cmd = (re_ca_uart_cmd_t)cmd; uart_payload.params.fltr_id_param.id = id; - data_length = sizeof(data); - if (RE_SUCCESS != re_ca_uart_encode(data, &data_length, &uart_payload)) + if (RE_SUCCESS != re_ca_uart_encode(data.buf, &data_length, &uart_payload)) { res = (-1); } else { - terminal_send_msg((uint8_t *)data, data_length); + terminal_send_msg((uint8_t *)data.buf, data_length); #ifndef RUUVI_ESP dbus_check_new_messages(); #endif @@ -158,17 +155,18 @@ api_send_all( uint32_t cmd, uint16_t fltr_id, uint8_t fltr_tags_state, - uint8_t coded_phy_state, - uint8_t ext_payload_state, - uint8_t scan_phy_state, + uint8_t use_coded_phy, + uint8_t use_2m_phy, + uint8_t use_1m_phy, uint8_t ch_37_state, uint8_t ch_38_state, - uint8_t ch_39_state) + uint8_t ch_39_state, + uint8_t max_adv_len) { - int8_t res = 0; - re_ca_uart_payload_t uart_payload = { 0 }; - uint8_t data[BUFFER_PAYLOAD_SIZE]; - uint8_t data_length; + int8_t res = 0; + re_ca_uart_payload_t uart_payload = { 0 }; + re_ca_uart_mosi_payload_buf_encoded_all_params_t data; + uint8_t data_length = sizeof(data.buf); print_dbgmsgnoarg("Enter\n"); @@ -184,31 +182,31 @@ api_send_all( uart_payload.params.all_params.bools.fltr_tags.state = RE_CA_BOOL_DISABLE; } - if ((bool)coded_phy_state == true) + if ((bool)use_coded_phy == true) { - uart_payload.params.all_params.bools.coded_phy.state = RE_CA_BOOL_ENABLE; + uart_payload.params.all_params.bools.use_coded_phy.state = RE_CA_BOOL_ENABLE; } else { - uart_payload.params.all_params.bools.coded_phy.state = RE_CA_BOOL_DISABLE; + uart_payload.params.all_params.bools.use_coded_phy.state = RE_CA_BOOL_DISABLE; } - if ((bool)ext_payload_state == true) + if ((bool)use_2m_phy == true) { - uart_payload.params.all_params.bools.ext_payload.state = RE_CA_BOOL_ENABLE; + uart_payload.params.all_params.bools.use_2m_phy.state = RE_CA_BOOL_ENABLE; } else { - uart_payload.params.all_params.bools.ext_payload.state = RE_CA_BOOL_DISABLE; + uart_payload.params.all_params.bools.use_2m_phy.state = RE_CA_BOOL_DISABLE; } - if ((bool)scan_phy_state == true) + if ((bool)use_1m_phy == true) { - uart_payload.params.all_params.bools.scan_phy.state = RE_CA_BOOL_ENABLE; + uart_payload.params.all_params.bools.use_1m_phy.state = RE_CA_BOOL_ENABLE; } else { - uart_payload.params.all_params.bools.scan_phy.state = RE_CA_BOOL_DISABLE; + uart_payload.params.all_params.bools.use_1m_phy.state = RE_CA_BOOL_DISABLE; } if ((bool)ch_37_state == true) @@ -238,15 +236,15 @@ api_send_all( uart_payload.params.all_params.bools.ch_39.state = RE_CA_BOOL_DISABLE; } - data_length = sizeof(data); + uart_payload.params.all_params.max_adv_len = max_adv_len; - if (RE_SUCCESS != re_ca_uart_encode(data, &data_length, &uart_payload)) + if (RE_SUCCESS != re_ca_uart_encode(data.buf, &data_length, &uart_payload)) { res = (-1); } else { - terminal_send_msg((uint8_t *)data, data_length); + terminal_send_msg((uint8_t *)data.buf, data_length); #ifndef RUUVI_ESP dbus_check_new_messages(); #endif @@ -258,24 +256,23 @@ api_send_all( int8_t api_send_led_ctrl(const uint16_t time_interval_ms) { - int8_t res = 0; - re_ca_uart_payload_t uart_payload = { 0 }; - uint8_t data[BUFFER_PAYLOAD_SIZE]; - uint8_t data_length; + int8_t res = 0; + re_ca_uart_payload_t uart_payload = { 0 }; + re_ca_uart_mosi_payload_buf_encoded_led_ctrl_t data; + uint8_t data_length = sizeof(data.buf); print_dbgmsgnoarg("Enter\n"); uart_payload.cmd = RE_CA_UART_LED_CTRL; uart_payload.params.led_ctrl_param.time_interval_ms = time_interval_ms; - data_length = sizeof(data); - if (RE_SUCCESS != re_ca_uart_encode(data, &data_length, &uart_payload)) + if (RE_SUCCESS != re_ca_uart_encode(data.buf, &data_length, &uart_payload)) { res = (-1); } else { - terminal_send_msg((uint8_t *)data, data_length); + terminal_send_msg((uint8_t *)data.buf, data_length); #ifndef RUUVI_ESP dbus_check_new_messages(); #endif diff --git a/src/lib/api.h b/src/lib/api.h index 528c1af..9a84716 100644 --- a/src/lib/api.h +++ b/src/lib/api.h @@ -36,12 +36,13 @@ api_send_all( uint32_t cmd, uint16_t fltr_id, uint8_t fltr_tags_state, - uint8_t coded_phy_state, - uint8_t ext_payload_state, - uint8_t scan_phy_state, + uint8_t use_coded_phy, + uint8_t use_2m_phy, + uint8_t use_1m_phy, uint8_t ch_37_state, uint8_t ch_38_state, - uint8_t ch_39_state); + uint8_t ch_39_state, + uint8_t max_adv_len); int8_t api_send_led_ctrl(const uint16_t time_interval_ms); diff --git a/src/lib/parser.c b/src/lib/parser.c index a39dbee..615dd06 100644 --- a/src/lib/parser.c +++ b/src/lib/parser.c @@ -76,6 +76,8 @@ parse(const uint8_t *const p_buffer) } break; case RE_CA_UART_ADV_RPRT: + // fallthrough + case RE_CA_UART_ADV_RPRT2: if (NULL != p_parser_callback_func_tbl->ApiReportCallback) { res = p_parser_callback_func_tbl->ApiReportCallback(p_buffer); diff --git a/src/lib/parser.h b/src/lib/parser.h index fba4f0f..98d6c61 100644 --- a/src/lib/parser.h +++ b/src/lib/parser.h @@ -36,14 +36,14 @@ typedef enum parser_state_e } re_ca_uart_parser_state_e; /** - * @brief Standard BLE Broadcast manufacturer specific data payload maximum length + * @brief Parser buffer size */ -#define RE_CA_UART_PARSER_RI_COMM_MESSAGE_MAX_LENGTH 230 +#define RE_CA_UART_PARSER_BUFFER_SIZE (255) /** - * @brief Parser buffer size + * @brief Maximum length of the data in the message */ -#define RE_CA_UART_PARSER_BUFFER_SIZE (3 + RE_CA_UART_PARSER_RI_COMM_MESSAGE_MAX_LENGTH + 2 + 1 + 1) +#define RE_CA_UART_PARSER_RI_COMM_MESSAGE_MAX_LENGTH (RE_CA_UART_PARSER_BUFFER_SIZE - (3 + 2 + 1 + 1)) /** * @brief Parser message buffer diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..b7ca96a --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +mkdir -p cmake-build-unit-tests +cd cmake-build-unit-tests +cmake -G "Ninja" .. +ninja +ctest +