diff --git a/CMock b/CMock index 8b59a0d..4f94b71 160000 --- a/CMock +++ b/CMock @@ -1 +1 @@ -Subproject commit 8b59a0d868ec418682960d5ae696637fdf20c0aa +Subproject commit 4f94b7195f69194e465dec1a95c6415d6966890b diff --git a/Makefile b/Makefile index 7faf0d2..9aa9bb0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,3 @@ -# Source, copyright: https://github.com/viva64/pvs-studio-makefile-examples -# commit 82a0f0a, /example-1 -# Modified for C on mac, added Doxygen # Apache License # Version 2.0, January 2004 # http://www.apache.org/licenses/ @@ -214,7 +211,7 @@ OFLAGS = -g3 LDFLAGS = DFLAGS = -INCLUDES += src/ +INCLUDES += src INCLUDES += ./CMock/vendor/unity/src INCLUDES += ./build/test/mocks INC_PARAMS = $(foreach d, $(INCLUDES), -I$d) @@ -242,13 +239,31 @@ UNITY_DIR = ${CMOCK_DIR}/vendor/unity DISABLE_CMOCK_TEST_SUMMARY_PER_PROJECT=1 TEST_MAKEFILE = ${TEST_BUILD_DIR}/MakefileTestSupport +TEST_MAKEFILE_EXT_ADV_48 = ${BUILD_DIR}_ext_adv_48/MakefileTestSupport +TEST_MAKEFILE_EXT_ADV_MAX = ${BUILD_DIR}_ext_adv_max/MakefileTestSupport -include ${TEST_MAKEFILE} +-include ${TEST_MAKEFILE_EXT_ADV_48} +-include ${TEST_MAKEFILE_EXT_ADV_MAX} .PHONY: all clean doxygen sonar astyle all: clean astyle doxygen sonar +# Specify all tests as dependencies of 'all' (workaround for JetBrains CLion) +# It is needed because on the first scan of Makefile the $(TEST_MAKEFILE) does not exist and it is not included. +all: test_ruuvi_endpoint_3 \ + test_ruuvi_endpoint_5 \ + test_ruuvi_endpoint_6 \ + test_ruuvi_endpoint_8 \ + test_ruuvi_endpoint_c5 \ + test_ruuvi_endpoint_ca_uart \ + test_ruuvi_endpoint_e0 \ + test_ruuvi_endpoint_f0 \ + test_ruuvi_endpoint_fa \ + test_ruuvi_endpoint_ibeacon \ + test_ruuvi_endpoints + doxygen: clean doxygen @@ -267,21 +282,31 @@ clean: rm -rf $(DOXYGEN_DIR)/latex rm -f *.gcov rm -rf $(BUILD_DIR) + rm -rf $(BUILD_DIR)_ext_adv_48 + rm -rf $(BUILD_DIR)_ext_adv_max make setup_test make generate_cmock_mocks_and_runners test_all: rm -rf build_ceedling CEEDLING_MAIN_PROJECT_FILE=./project.yml ceedling test:all + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_48.yml ceedling test:all + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_max.yml ceedling test:all test_all_gcov: rm -rf build_ceedling CEEDLING_MAIN_PROJECT_FILE=./project.yml ceedling test:all CEEDLING_MAIN_PROJECT_FILE=./project.yml ceedling gcov:all utils:gcov + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_48.yml ceedling test:all + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_48.yml ceedling gcov:all utils:gcov + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_max.yml ceedling test:all + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_max.yml ceedling gcov:all utils:gcov gcov -b -c build_ceedling/gcov/out/*.gcno test: @UNITY_DIR=${UNITY_DIR} BUILD_DIR=${BUILD_DIR} TEST_BUILD_DIR= ruby ${CMOCK_DIR}/scripts/test_summary.rb + @UNITY_DIR=${UNITY_DIR} BUILD_DIR=${BUILD_DIR}_ext_adv_48 TEST_BUILD_DIR= ruby ${CMOCK_DIR}/scripts/test_summary.rb + @UNITY_DIR=${UNITY_DIR} BUILD_DIR=${BUILD_DIR}_ext_adv_max TEST_BUILD_DIR= ruby ${CMOCK_DIR}/scripts/test_summary.rb setup_test: mkdir -p ${BUILD_DIR} @@ -291,3 +316,17 @@ setup_test: TEST_OUT_DIR=${TEST_OUT_DIR} \ TEST_MAKEFILE=${TEST_MAKEFILE} \ ruby ${CMOCK_DIR}/scripts/create_makefile.rb + mkdir -p ${BUILD_DIR}_ext_adv_48 + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_48.yml \ + BUILD_DIR=${BUILD_DIR}_ext_adv_48 \ + TEST_BUILD_DIR=${BUILD_DIR}_ext_adv_48/test \ + TEST_OUT_DIR=${BUILD_DIR}_ext_adv_48/out \ + TEST_MAKEFILE=${TEST_MAKEFILE_EXT_ADV_48} \ + ruby ${CMOCK_DIR}/scripts/create_makefile.rb + mkdir -p ${BUILD_DIR}_ext_adv_max + CEEDLING_MAIN_PROJECT_FILE=./project_ext_adv_max.yml \ + BUILD_DIR=${BUILD_DIR}_ext_adv_max \ + TEST_BUILD_DIR=${BUILD_DIR}_ext_adv_max/test \ + TEST_OUT_DIR=${BUILD_DIR}_ext_adv_max/out \ + TEST_MAKEFILE=${TEST_MAKEFILE_EXT_ADV_MAX} \ + ruby ${CMOCK_DIR}/scripts/create_makefile.rb diff --git a/project.yml b/project.yml index 601cfca..a62bf72 100644 --- a/project.yml +++ b/project.yml @@ -68,6 +68,11 @@ - *common_defines - TEST - RE_6_ENABLED + :test_ruuvi_endpoint_ca_uart: + - *common_defines + - TEST +# - RI_ADV_EXTENDED_ENABLED=0 +# - RI_COMM_BLE_PAYLOAD_MAX_LENGTH=31 :cmock: :mock_prefix: mock_ diff --git a/project_ext_adv_48.yml b/project_ext_adv_48.yml new file mode 100644 index 0000000..eac8031 --- /dev/null +++ b/project_ext_adv_48.yml @@ -0,0 +1,139 @@ +--- + +# Notes: +# Sample project C code is not presently written to produce a release artifact. +# As such, release build options are disabled. +# This sample, therefore, only demonstrates running a collection of unit tests. + +:project: + :use_exceptions: FALSE + :use_test_preprocessor: TRUE + :use_auxiliary_dependencies: TRUE + :build_root: build_ceedling +# :release_build: TRUE + :test_file_prefix: test_ + :which_ceedling: gem + :default_tasks: + - test:all + +#:test_build: +# :use_assembly: TRUE + +#:release_build: +# :output: MyApp.out +# :use_assembly: FALSE + +:environment: + +:extension: + :executable: .out + +:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + :test_linker: + :executable: gcc #absolute file path + :name: 'gcc linker' + :arguments: + - ${1} #list of object files to link (Ruby method call param list sub) + - -lm #link with math header + - -o ${2} #executable file output (Ruby method call param list sub) + +:tools_gcov_linker: + :arguments: + - -lm + +:paths: + :test: + - +:test/** + - -:test/support + :source: + - src/** + :support: + - test/support + +:defines: + # in order to add common defines: + # 1) remove the trailing [] from the :common: section + # 2) add entries to the :common: section (e.g. :test: has TEST defined) + :common: &common_defines [] + :test: + - *common_defines + - TEST + :test_preprocess: + - *common_defines + - TEST + :test_ruuvi_endpoint_6: + - *common_defines + - TEST + - RE_6_ENABLED + :test_ruuvi_endpoint_ca_uart: + - *common_defines + - TEST + - RI_ADV_EXTENDED_ENABLED=1 + - RI_COMM_BLE_PAYLOAD_MAX_LENGTH=48 + +:cmock: + :mock_prefix: mock_ + :when_no_prototypes: :warn + :enforce_strict_ordering: TRUE + :plugins: + - :ignore + - :ignore_arg + - :callback + - :return_thru_ptr + - :array + - :expect_any_args + :treat_as: + uint8: HEX8 + uint16: HEX16 + uint32: UINT32 + int8: INT8 + bool: UINT8 + +# Add -gcov to the plugins list to make sure of the gcov plugin +# You will need to have gcov and gcovr both installed to make it work. +# For more information on these options, see docs in plugins/gcov +:gcov: + :html_report: TRUE + :html_report_type: detailed + :html_medium_threshold: 75 + :html_high_threshold: 90 + :xml_report: FALSE + +#:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + +# LIBRARIES +# These libraries are automatically injected into the build process. Those specified as +# common will be used in all types of builds. Otherwise, libraries can be injected in just +# tests or releases. These options are MERGED with the options in supplemental yaml files. +:libraries: + :placement: :end + :flag: "${1}" # or "-L ${1}" for example + :test: [] + :release: [] + +:plugins: + :load_paths: + - "#{Ceedling.load_path}" + :enabled: + - stdout_pretty_tests_report + - module_generator + - gcov + +:flags: + :test: + :compile: + :*: + - -Wall + - -std=c11 + :gcov: + :compile: + :*: + - -Wall + - -std=c11 +... diff --git a/project_ext_adv_max.yml b/project_ext_adv_max.yml new file mode 100644 index 0000000..5097cbf --- /dev/null +++ b/project_ext_adv_max.yml @@ -0,0 +1,139 @@ +--- + +# Notes: +# Sample project C code is not presently written to produce a release artifact. +# As such, release build options are disabled. +# This sample, therefore, only demonstrates running a collection of unit tests. + +:project: + :use_exceptions: FALSE + :use_test_preprocessor: TRUE + :use_auxiliary_dependencies: TRUE + :build_root: build_ceedling +# :release_build: TRUE + :test_file_prefix: test_ + :which_ceedling: gem + :default_tasks: + - test:all + +#:test_build: +# :use_assembly: TRUE + +#:release_build: +# :output: MyApp.out +# :use_assembly: FALSE + +:environment: + +:extension: + :executable: .out + +:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + :test_linker: + :executable: gcc #absolute file path + :name: 'gcc linker' + :arguments: + - ${1} #list of object files to link (Ruby method call param list sub) + - -lm #link with math header + - -o ${2} #executable file output (Ruby method call param list sub) + +:tools_gcov_linker: + :arguments: + - -lm + +:paths: + :test: + - +:test/** + - -:test/support + :source: + - src/** + :support: + - test/support + +:defines: + # in order to add common defines: + # 1) remove the trailing [] from the :common: section + # 2) add entries to the :common: section (e.g. :test: has TEST defined) + :common: &common_defines [] + :test: + - *common_defines + - TEST + :test_preprocess: + - *common_defines + - TEST + :test_ruuvi_endpoint_6: + - *common_defines + - TEST + - RE_6_ENABLED + :test_ruuvi_endpoint_ca_uart: + - *common_defines + - TEST + - RI_ADV_EXTENDED_ENABLED=1 + - RI_COMM_BLE_PAYLOAD_MAX_LENGTH=235 + +:cmock: + :mock_prefix: mock_ + :when_no_prototypes: :warn + :enforce_strict_ordering: TRUE + :plugins: + - :ignore + - :ignore_arg + - :callback + - :return_thru_ptr + - :array + - :expect_any_args + :treat_as: + uint8: HEX8 + uint16: HEX16 + uint32: UINT32 + int8: INT8 + bool: UINT8 + +# Add -gcov to the plugins list to make sure of the gcov plugin +# You will need to have gcov and gcovr both installed to make it work. +# For more information on these options, see docs in plugins/gcov +:gcov: + :html_report: TRUE + :html_report_type: detailed + :html_medium_threshold: 75 + :html_high_threshold: 90 + :xml_report: FALSE + +#:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + +# LIBRARIES +# These libraries are automatically injected into the build process. Those specified as +# common will be used in all types of builds. Otherwise, libraries can be injected in just +# tests or releases. These options are MERGED with the options in supplemental yaml files. +:libraries: + :placement: :end + :flag: "${1}" # or "-L ${1}" for example + :test: [] + :release: [] + +:plugins: + :load_paths: + - "#{Ceedling.load_path}" + :enabled: + - stdout_pretty_tests_report + - module_generator + - gcov + +:flags: + :test: + :compile: + :*: + - -Wall + - -std=c11 + :gcov: + :compile: + :*: + - -Wall + - -std=c11 +... diff --git a/src/ruuvi_endpoint_ca_uart.c b/src/ruuvi_endpoint_ca_uart.c index 52b7750..1cca10b 100644 --- a/src/ruuvi_endpoint_ca_uart.c +++ b/src/ruuvi_endpoint_ca_uart.c @@ -365,12 +365,15 @@ static re_status_t re_ca_uart_decode_get_all (const uint8_t * const buffer, return err_code; } -static re_status_t re_ca_uart_decode_all (const uint8_t * const buffer, - re_ca_uart_payload_t * const payload) +static re_status_t re_ca_uart_decode_set_all_params ( + const uint8_t * const buffer, + re_ca_uart_payload_t * const payload) { re_status_t err_code = RE_SUCCESS; + const uint8_t len = buffer[RE_CA_UART_LEN_INDEX]; - if (buffer[RE_CA_UART_LEN_INDEX] != RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS()) + if ( (len != RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS()) && + (len != RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS_WITH_MAX_ADV_LEN())) { err_code |= RE_ERROR_DECODING_LEN; } @@ -379,34 +382,38 @@ static re_status_t re_ca_uart_decode_all (const uint8_t * const buffer, payload->cmd = buffer[RE_CA_UART_CMD_INDEX]; payload->params.all_params.fltr_id.id = * ( (uint16_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX]); + const uint8_t flags = buffer[RE_CA_UART_PAYLOAD_INDEX + + RE_CA_UART_DELIMITER_LEN + + RE_CA_UART_CMD_FLTR_ID_LEN]; payload->params.all_params.bools.fltr_tags.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_FLTR_TAG_BIT) & 1U); - payload->params.all_params.bools.coded_phy.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_CODED_PHY_BIT) & 1U); - payload->params.all_params.bools.scan_phy.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_SCAN_PHY_BIT) & 1U); - payload->params.all_params.bools.ext_payload.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_EXT_PLD_BIT) & 1U); + (flags >> RE_CA_UART_ALL_FLTR_TAG_BIT) & 1U; + payload->params.all_params.bools.use_coded_phy.state = + (flags >> RE_CA_UART_ALL_USE_CODED_PHY_BIT) & 1U; + payload->params.all_params.bools.use_1m_phy.state = + (flags >> RE_CA_UART_ALL_USE_1M_PHY_BIT) & 1U; + payload->params.all_params.bools.use_2m_phy.state = + (flags >> RE_CA_UART_ALL_USE_2M_PHY_BIT) & 1U; payload->params.all_params.bools.ch_37.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_CH_37_BIT) & 1U); + (flags >> RE_CA_UART_ALL_CH_37_BIT) & 1U; payload->params.all_params.bools.ch_38.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_CH_38_BIT) & 1U); + (flags >> RE_CA_UART_ALL_CH_38_BIT) & 1U; payload->params.all_params.bools.ch_39.state = - ( (* ( (uint8_t *) &buffer[RE_CA_UART_PAYLOAD_INDEX - + RE_CA_UART_DELIMITER_LEN - + RE_CA_UART_CMD_FLTR_ID_LEN]) >> RE_CA_UART_ALL_CH_39_BIT) & 1U); + (flags >> RE_CA_UART_ALL_CH_39_BIT) & 1U; + + if (len == RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS_WITH_MAX_ADV_LEN()) + { + payload->params.all_params.max_adv_len = + buffer[RE_CA_UART_PAYLOAD_INDEX + + RE_CA_UART_DELIMITER_LEN + + RE_CA_UART_CMD_FLTR_ID_LEN + + RE_CA_UART_DELIMITER_LEN + + RE_CA_UART_CMD_ALL_BOOL_LEN]; + } + else + { + payload->params.all_params.max_adv_len = + RE_CA_UART_BLE_ALL_PARAMS_MAX_ADV_LEN_NO_LIMIT; + } } return err_code; @@ -415,51 +422,122 @@ static re_status_t re_ca_uart_decode_all (const uint8_t * const buffer, static re_status_t re_ca_uart_decode_adv_rprt (const uint8_t * const buffer, re_ca_uart_payload_t * const payload) { - re_status_t err_code = RE_SUCCESS; - const uint8_t adv_len = buffer[RE_CA_UART_LEN_INDEX] - - RE_CA_UART_MAC_BYTES - - RE_CA_UART_RSSI_BYTES - - (RE_CA_UART_MAXFIELDS) * RE_CA_UART_DELIMITER_LEN; - const uint8_t * const p_rssi = buffer - + RE_CA_UART_PAYLOAD_INDEX - + buffer[RE_CA_UART_LEN_INDEX] - - RE_CA_UART_RSSI_BYTES - - RE_CA_UART_DELIMITER_LEN; - const uint8_t * const p_data = buffer - + RE_CA_UART_PAYLOAD_INDEX + const uint8_t packet_len = buffer[RE_CA_UART_LEN_INDEX]; + _Static_assert (RE_CA_UART_PAYLOAD_ADV_RPRT_MAX_LEN <= 255, + "Payload too long for uint8_t"); + const uint8_t packet_overhead = RE_CA_UART_PAYLOAD_ADV_RPRT_MAX_LEN - + RE_CA_UART_ADV_BYTES; + + if (packet_len < packet_overhead) + { + return RE_ERROR_DECODING_LEN; + } + + const uint8_t adv_len = packet_len - packet_overhead; + const size_t max_len = RE_CA_UART_ADV_BYTES; + + if (adv_len > max_len) + { + return RE_ERROR_DATA_SIZE; + } + + const uint8_t * const p_mac = buffer + RE_CA_UART_PAYLOAD_INDEX; + const uint8_t * const p_data = p_mac + RE_CA_UART_MAC_BYTES + RE_CA_UART_DELIMITER_LEN; + const uint8_t * const p_rssi = p_data + adv_len + RE_CA_UART_DELIMITER_LEN; - if (adv_len > RE_CA_UART_ADV_BYTES) + if (RE_CA_UART_FIELD_DELIMITER != * (p_data - RE_CA_UART_DELIMITER_LEN)) { - err_code |= RE_ERROR_DECODING_LEN; + return RE_ERROR_DECODING_DELIMITER; } - else if (RE_CA_UART_FIELD_DELIMITER != * (p_data - RE_CA_UART_DELIMITER_LEN)) + + if (RE_CA_UART_FIELD_DELIMITER != * (p_rssi - RE_CA_UART_DELIMITER_LEN)) { - err_code |= RE_ERROR_DECODING_DELIMITER; + return RE_ERROR_DECODING_DELIMITER; } - else if (RE_CA_UART_FIELD_DELIMITER != * (p_rssi - RE_CA_UART_DELIMITER_LEN)) + + if (RE_CA_UART_FIELD_DELIMITER != * (p_rssi + RE_CA_UART_RSSI_BYTES)) { - err_code |= RE_ERROR_DECODING_DELIMITER; + return RE_ERROR_DECODING_DELIMITER; } - else if (RE_CA_UART_FIELD_DELIMITER != * (p_rssi + RE_CA_UART_RSSI_BYTES)) + + memcpy (payload->params.adv.mac, p_mac, RE_CA_UART_MAC_BYTES); + memcpy (payload->params.adv.adv, p_data, adv_len); + payload->cmd = RE_CA_UART_ADV_RPRT; + payload->params.adv.rssi_db = u8toi8 (*p_rssi); + payload->params.adv.adv_len = adv_len; + payload->params.adv.is_coded_phy = false; + payload->params.adv.primary_phy = RE_CA_UART_BLE_PHY_NOT_SET; + payload->params.adv.secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET; + payload->params.adv.ch_index = RE_CA_UART_BLE_GAP_CHANNEL_INDEX_INVALID; + payload->params.adv.tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID; + return RE_SUCCESS; +} + +static re_status_t re_ca_uart_decode_adv_rprt2 (const uint8_t * const buffer, + re_ca_uart_payload_t * const payload) +{ + const uint8_t packet_len = buffer[RE_CA_UART_LEN_INDEX]; + _Static_assert (RE_CA_UART_PAYLOAD_ADV_RPRT2_MAX_LEN <= 255, + "Payload too long for uint8_t"); + const uint8_t packet_overhead = RE_CA_UART_PAYLOAD_ADV_RPRT2_MAX_LEN - + RE_CA_UART_ADV_BYTES; + + if (packet_len < packet_overhead) { - err_code |= RE_ERROR_DECODING_DELIMITER; + return RE_ERROR_DECODING_LEN; } - else + + const uint8_t adv_len = packet_len - packet_overhead; + const size_t max_len = RE_CA_UART_ADV_BYTES; + + if (adv_len > max_len) { - memcpy (payload->params.adv.mac, - buffer + RE_CA_UART_PAYLOAD_INDEX, - RE_CA_UART_MAC_BYTES); - memcpy (payload->params.adv.adv, - p_data, - adv_len); - payload->cmd = RE_CA_UART_ADV_RPRT; - payload->params.adv.rssi_db = u8toi8 (*p_rssi); - payload->params.adv.adv_len = adv_len; + return RE_ERROR_DATA_SIZE; } - return err_code; + const uint8_t * const p_mac = buffer + RE_CA_UART_PAYLOAD_INDEX; + const uint8_t * const p_data = p_mac + + RE_CA_UART_MAC_BYTES + + RE_CA_UART_DELIMITER_LEN; + const uint8_t * const p_rssi = p_data + adv_len + RE_CA_UART_DELIMITER_LEN; + const uint8_t * const p_extra_info = p_rssi + RE_CA_UART_RSSI_BYTES + + RE_CA_UART_DELIMITER_LEN; + + if (RE_CA_UART_FIELD_DELIMITER != * (p_data - RE_CA_UART_DELIMITER_LEN)) + { + return RE_ERROR_DECODING_DELIMITER; + } + + if (RE_CA_UART_FIELD_DELIMITER != * (p_rssi - RE_CA_UART_DELIMITER_LEN)) + { + return RE_ERROR_DECODING_DELIMITER; + } + + if (RE_CA_UART_FIELD_DELIMITER != * (p_extra_info - RE_CA_UART_DELIMITER_LEN)) + { + return RE_ERROR_DECODING_DELIMITER; + } + + if (RE_CA_UART_FIELD_DELIMITER != * (p_extra_info + + RE_CA_UART_ADV_RPRT2_EXTRA_INFO_BYTES)) + { + return RE_ERROR_DECODING_DELIMITER; + } + + memcpy (payload->params.adv.mac, p_mac, RE_CA_UART_MAC_BYTES); + memcpy (payload->params.adv.adv, p_data, adv_len); + payload->cmd = RE_CA_UART_ADV_RPRT2; + payload->params.adv.rssi_db = u8toi8 (*p_rssi); + payload->params.adv.adv_len = adv_len; + payload->params.adv.primary_phy = p_extra_info[0] >> 4; + payload->params.adv.secondary_phy = p_extra_info[0] & 0x0F; + payload->params.adv.ch_index = p_extra_info[1]; + payload->params.adv.is_coded_phy = (0 != ( (p_extra_info[2] >> 7U) & 0x01U)) ? true : + false; + payload->params.adv.tx_power = p_extra_info[2] & 0x7FU; + return RE_SUCCESS; } re_status_t re_ca_uart_decode (const uint8_t * const buffer, @@ -526,7 +604,7 @@ re_status_t re_ca_uart_decode (const uint8_t * const buffer, case RE_CA_UART_SET_FLTR_TAGS: case RE_CA_UART_SET_CODED_PHY: case RE_CA_UART_SET_SCAN_1MB_PHY: - case RE_CA_UART_SET_EXT_PAYLOAD: + case RE_CA_UART_SET_SCAN_2MB_PHY: case RE_CA_UART_SET_CH_37: case RE_CA_UART_SET_CH_38: case RE_CA_UART_SET_CH_39: @@ -546,7 +624,7 @@ re_status_t re_ca_uart_decode (const uint8_t * const buffer, break; case RE_CA_UART_SET_ALL: - err_code |= re_ca_uart_decode_all (buffer, payload); + err_code |= re_ca_uart_decode_set_all_params (buffer, payload); break; case RE_CA_UART_ADV_RPRT: @@ -557,6 +635,10 @@ re_status_t re_ca_uart_decode (const uint8_t * const buffer, err_code |= re_ca_uart_decode_device_id (buffer, payload); break; + case RE_CA_UART_ADV_RPRT2: + err_code |= re_ca_uart_decode_adv_rprt2 (buffer, payload); + break; + case RE_CA_UART_GET_DEVICE_ID: err_code |= re_ca_uart_decode_get_device_id (buffer, payload); break; @@ -580,9 +662,11 @@ static re_status_t re_ca_uart_encode_adv_rprt (uint8_t * const buffer, { re_status_t err_code = RE_SUCCESS; uint32_t written = 0; + const uint32_t tx_len = RE_CA_UART_TX_BUF_LEN (\ + RE_CA_UART_TX_DATA_LEN_CMD_ADV_RPRT (\ + payload->params.adv.adv_len)); - if ( (RE_CA_UART_TX_BUF_LEN (RE_CA_UART_TX_DATA_LEN_CMD_ADV_RPRT ( - payload->params.adv.adv_len))) > *buf_len) + if (tx_len > *buf_len) { err_code |= RE_ERROR_DATA_SIZE; } @@ -614,6 +698,52 @@ static re_status_t re_ca_uart_encode_adv_rprt (uint8_t * const buffer, return err_code; } +static re_status_t re_ca_uart_encode_adv_rprt2 (uint8_t * const buffer, + uint8_t * const buf_len, + const re_ca_uart_payload_t * const payload) +{ + re_status_t err_code = RE_SUCCESS; + uint32_t written = 0; + + if ( (RE_CA_UART_TX_BUF_LEN (RE_CA_UART_TX_DATA_LEN_CMD_ADV_RPRT2 ( + payload->params.adv.adv_len))) > *buf_len) + { + err_code |= RE_ERROR_DATA_SIZE; + } + else + { + buffer[RE_CA_UART_STX_INDEX] = RE_CA_UART_STX; + // Payload length is different from total message length. + buffer[RE_CA_UART_LEN_INDEX] = RE_CA_UART_TX_DATA_LEN_CMD_ADV_RPRT2 ( + payload->params.adv.adv_len); + buffer[RE_CA_UART_CMD_INDEX] = RE_CA_UART_ADV_RPRT2; + written += RE_CA_UART_HEADER_SIZE; + memcpy (buffer + RE_CA_UART_PAYLOAD_INDEX, + payload->params.adv.mac, + RE_CA_UART_MAC_BYTES); + written += RE_CA_UART_MAC_BYTES; + buffer[written++] = RE_CA_UART_FIELD_DELIMITER; + memcpy (buffer + written, + payload->params.adv.adv, + payload->params.adv.adv_len); + written += payload->params.adv.adv_len; + buffer[written++] = RE_CA_UART_FIELD_DELIMITER; + buffer[written++] = i8tou8 (payload->params.adv.rssi_db); + buffer[written++] = RE_CA_UART_FIELD_DELIMITER; + buffer[written++] = ( (payload->params.adv.primary_phy & 0x0FU) << 4U) | + (payload->params.adv.secondary_phy & 0x0FU); + buffer[written++] = payload->params.adv.ch_index; + buffer[written++] = ( (payload->params.adv.is_coded_phy & 0x01U) << 7U) | + (payload->params.adv.tx_power & 0x7FU); + buffer[written++] = RE_CA_UART_FIELD_DELIMITER; + add_crc16 (buffer, &written); + buffer[written++] = RE_CA_UART_ETX; + *buf_len = written; + } + + return err_code; +} + static re_status_t re_ca_uart_encode_bool_payload (uint8_t * const buffer, uint8_t * const buf_len, const re_ca_uart_payload_t * const payload) @@ -794,14 +924,18 @@ static re_status_t re_ca_uart_encode_get_device_id (uint8_t * const buffer, return err_code; } -static re_status_t re_ca_uart_encode_all (uint8_t * const buffer, +static re_status_t re_ca_uart_encode_set_all_params (uint8_t * const buffer, uint8_t * const buf_len, const re_ca_uart_payload_t * const payload) { re_status_t err_code = RE_SUCCESS; uint32_t written = 0; + const uint8_t data_len = + (0 == payload->params.all_params.max_adv_len) + ? RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS() + : RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS_WITH_MAX_ADV_LEN(); - if (RE_CA_UART_TX_BUF_LEN (RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS()) > *buf_len) + if (RE_CA_UART_TX_BUF_LEN (data_len) > *buf_len) { err_code |= RE_ERROR_DATA_SIZE; } @@ -809,7 +943,7 @@ static re_status_t re_ca_uart_encode_all (uint8_t * const buffer, { buffer[RE_CA_UART_STX_INDEX] = RE_CA_UART_STX; // Payload length is different from total message length. - buffer[RE_CA_UART_LEN_INDEX] = RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS(); + buffer[RE_CA_UART_LEN_INDEX] = data_len; buffer[RE_CA_UART_CMD_INDEX] = payload->cmd; written += RE_CA_UART_HEADER_SIZE; memcpy (buffer + RE_CA_UART_PAYLOAD_INDEX, @@ -819,12 +953,12 @@ static re_status_t re_ca_uart_encode_all (uint8_t * const buffer, buffer[written++] = RE_CA_UART_FIELD_DELIMITER; buffer[written] = ( ( (payload->params.all_params.bools.fltr_tags.state & 1U) << RE_CA_UART_ALL_FLTR_TAG_BIT) - | ( (payload->params.all_params.bools.coded_phy.state & 1U) - << RE_CA_UART_ALL_CODED_PHY_BIT) - | ( (payload->params.all_params.bools.scan_phy.state & 1U) - << RE_CA_UART_ALL_SCAN_PHY_BIT) - | ( (payload->params.all_params.bools.ext_payload.state & 1U) - << RE_CA_UART_ALL_EXT_PLD_BIT) + | ( (payload->params.all_params.bools.use_coded_phy.state & 1U) + << RE_CA_UART_ALL_USE_CODED_PHY_BIT) + | ( (payload->params.all_params.bools.use_1m_phy.state & 1U) + << RE_CA_UART_ALL_USE_1M_PHY_BIT) + | ( (payload->params.all_params.bools.use_2m_phy.state & 1U) + << RE_CA_UART_ALL_USE_2M_PHY_BIT) | ( (payload->params.all_params.bools.ch_37.state & 1U) << RE_CA_UART_ALL_CH_37_BIT) | ( (payload->params.all_params.bools.ch_38.state & 1U) @@ -833,6 +967,14 @@ static re_status_t re_ca_uart_encode_all (uint8_t * const buffer, << RE_CA_UART_ALL_CH_39_BIT)); written += RE_CA_UART_CMD_ALL_BOOL_LEN; buffer[written++] = RE_CA_UART_FIELD_DELIMITER; + + if (0 != payload->params.all_params.max_adv_len) + { + buffer[written] = payload->params.all_params.max_adv_len; + written += RE_CA_UART_CMD_ALL_UINT8_LEN; + buffer[written++] = RE_CA_UART_FIELD_DELIMITER; + } + add_crc16 (buffer, &written); buffer[written++] = RE_CA_UART_ETX; *buf_len = written; @@ -891,7 +1033,7 @@ re_status_t re_ca_uart_encode (uint8_t * const buffer, uint8_t * const buf_len, case RE_CA_UART_SET_FLTR_TAGS: case RE_CA_UART_SET_CODED_PHY: case RE_CA_UART_SET_SCAN_1MB_PHY: - case RE_CA_UART_SET_EXT_PAYLOAD: + case RE_CA_UART_SET_SCAN_2MB_PHY: case RE_CA_UART_SET_CH_37: case RE_CA_UART_SET_CH_38: case RE_CA_UART_SET_CH_39: @@ -911,7 +1053,7 @@ re_status_t re_ca_uart_encode (uint8_t * const buffer, uint8_t * const buf_len, break; case RE_CA_UART_SET_ALL: - err_code |= re_ca_uart_encode_all (buffer, buf_len, payload); + err_code |= re_ca_uart_encode_set_all_params (buffer, buf_len, payload); break; case RE_CA_UART_ADV_RPRT: @@ -922,6 +1064,10 @@ re_status_t re_ca_uart_encode (uint8_t * const buffer, uint8_t * const buf_len, err_code |= re_ca_uart_encode_device_id (buffer, buf_len, payload); break; + case RE_CA_UART_ADV_RPRT2: + err_code |= re_ca_uart_encode_adv_rprt2 (buffer, buf_len, payload); + break; + case RE_CA_UART_GET_DEVICE_ID: err_code |= re_ca_uart_encode_get_device_id (buffer, buf_len, payload); break; diff --git a/src/ruuvi_endpoint_ca_uart.h b/src/ruuvi_endpoint_ca_uart.h index 014aa62..9b22da0 100644 --- a/src/ruuvi_endpoint_ca_uart.h +++ b/src/ruuvi_endpoint_ca_uart.h @@ -2,6 +2,7 @@ #define RUUVI_ENDPOINT_CA_UART_H #include "ruuvi_endpoints.h" +#include #define RE_CA_CRC_DEFAULT 0xFFFF #define RE_CA_CRC_INVALID 0 @@ -20,13 +21,14 @@ # if defined(RI_COMM_BLE_PAYLOAD_MAX_LENGTH) # define RE_CA_UART_ADV_BYTES (RI_COMM_BLE_PAYLOAD_MAX_LENGTH) //!< Number of bytes in Advertisement. # else -# define RE_CA_UART_ADV_BYTES (238) //!< Number of bytes in Advertisement. +# error "RI_COMM_BLE_PAYLOAD_MAX_LENGTH must be defined when RI_ADV_EXTENDED_ENABLED=1" # endif #else # define RE_CA_UART_ADV_BYTES (31U) //!< Number of bytes in Advertisement. #endif #define RE_CA_UART_RSSI_BYTES (1U) //!< Number of bytes in RSSI report. +#define RE_CA_UART_ADV_RPRT2_EXTRA_INFO_BYTES (3) //!< Number of bytes in BLE PHY extra info. #if RE_CA_UART_LEGACY_MODE # define RE_CA_UART_STX (0x02U) //!< Start UART Command. ASCII STX. # define RE_CA_UART_ETX (0x03U) //!< End UART Command. ASCII ETX. @@ -38,18 +40,23 @@ # define RE_CA_UART_RSSI_MAXLEN (RE_CA_UART_RSSI_BYTES) //!< i8. # define CMD_IN_LEN (0U) //!< Command is not included in data length. #endif -#define RE_CA_UART_MAC_MAXLEN (RE_CA_UART_MAC_BYTES) //!< Length of MAC in UART. -#define RE_CA_UART_ADV_MAXLEN (RE_CA_UART_ADV_BYTES) //!< Length of adv. -#define RE_CA_UART_ADV_FIELDS (3U) //!< On scan: mac, data, rssi. -#define RE_CA_UART_MAXFIELDS (RE_CA_UART_ADV_FIELDS) //!< Maximum delimited fields. -#define RE_CA_UART_PAYLOAD_MAX_LEN (RE_CA_UART_MAC_MAXLEN \ - + RE_CA_UART_ADV_MAXLEN \ - + RE_CA_UART_RSSI_MAXLEN \ - + RE_CA_UART_MAXFIELDS) //!< data + delimiters +#define RE_CA_UART_ADV_RPRT_FIELDS (3U) //!< On scan: mac, data, rssi. +#define RE_CA_UART_ADV_RPRT2_FIELDS (4U) //!< On scan: mac, data, rssi, extra_info. +#define RE_CA_UART_ADV_RPRT2_EXTRA_INFO_LEN (3U) //!< 3 bytes for PHY, ch_index, tx_power. #define RE_CA_UART_FIELD_DELIMITER (0x2CU) //!< ',' #define RE_CA_UART_DELIMITER_LEN (1U) //!< 1 byte delimiter. /** @brief STX, LEN, CMD, Payload, CRC, ETX */ +#define RE_CA_UART_PAYLOAD_ADV_RPRT_MAX_LEN (RE_CA_UART_MAC_BYTES \ + + RE_CA_UART_ADV_BYTES \ + + RE_CA_UART_RSSI_MAXLEN \ + + RE_CA_UART_ADV_RPRT_FIELDS * RE_CA_UART_DELIMITER_LEN) //!< ADV_RPRT_LEN: data + delimiters +#define RE_CA_UART_PAYLOAD_ADV_RPRT2_MAX_LEN (RE_CA_UART_MAC_BYTES \ + + RE_CA_UART_ADV_BYTES \ + + RE_CA_UART_RSSI_MAXLEN \ + + RE_CA_UART_ADV_RPRT2_EXTRA_INFO_LEN \ + + RE_CA_UART_ADV_RPRT2_FIELDS * RE_CA_UART_DELIMITER_LEN) //!< ADV_RPRT2_LEN: data + delimiters + #define RE_CA_UART_BLE_NOFILTER (0x0000U) //!< Do not apply filter to ID. #define RE_CA_UART_HEADER_SIZE (3U) //!< STX + len + CMD @@ -82,15 +89,15 @@ #define RE_CA_UART_ACK_BYTE (1U) //!< Byte of bool params, starting from 0. #define RE_CA_UART_ACK_BIT (0U) //!< Bit of bool params, starting from 0. -#define RE_CA_UART_FLTR_ID_BYTE (2U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_BOOL_BYTE (1U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_FLTR_TAG_BIT (0U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_CODED_PHY_BIT (1U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_SCAN_PHY_BIT (2U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_EXT_PLD_BIT (3U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_CH_37_BIT (4U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_CH_38_BIT (5U) //!< Byte of bool params, starting from 0. -#define RE_CA_UART_ALL_CH_39_BIT (6U) //!< Byte of bool params, starting from 0. +#define RE_CA_UART_FLTR_ID_BYTE (2U) //!< Byte of bool params, starting from 0. +#define RE_CA_UART_ALL_BOOL_BYTE (1U) //!< Byte of bool params, starting from 0. +#define RE_CA_UART_ALL_FLTR_TAG_BIT (0U) //!< Bit of bool params, starting from 0. +#define RE_CA_UART_ALL_USE_CODED_PHY_BIT (1U) //!< Bit of bool params, starting from 0. +#define RE_CA_UART_ALL_USE_1M_PHY_BIT (2U) //!< Bit of bool params, starting from 0. +#define RE_CA_UART_ALL_USE_2M_PHY_BIT (3U) //!< Bit of bool params, starting from 0. +#define RE_CA_UART_ALL_CH_37_BIT (4U) //!< Bit of bool params, starting from 0. +#define RE_CA_UART_ALL_CH_38_BIT (5U) //!< Bit of bool params, starting from 0. +#define RE_CA_UART_ALL_CH_39_BIT (6U) //!< Bit of bool params, starting from 0. #define RE_CA_UART_STX_ETX_LEN (1U) //!< Length of cmd with bool payload #define RE_CA_UART_DEVICE_ID_LEN (8U) //!< Length of device id payload @@ -100,6 +107,7 @@ #define RE_CA_UART_CMD_LED_CTRL_LEN (2U) //!< Length of led_ctrl payload #define RE_CA_UART_CMD_ACK_LEN (1U) //!< Length of ack payload #define RE_CA_UART_CMD_ALL_BOOL_LEN (1U) //!< Length of cmd with bool payload +#define RE_CA_UART_CMD_ALL_UINT8_LEN (1U) //!< Length of cmd with uint8 payload //!< Length of all command payload /** @breif Command types. */ @@ -117,7 +125,7 @@ typedef enum RE_CA_UART_SET_FLTR_ID = 6, //!< Set manufacturer ID filter. RE_CA_UART_SET_CODED_PHY = 7, //!< Set coded PHY. RE_CA_UART_SET_SCAN_1MB_PHY = 8, //!< Set scan 1MBbit/PHY. - RE_CA_UART_SET_EXT_PAYLOAD = 9, //!< Set extended payload. + RE_CA_UART_SET_SCAN_2MB_PHY = 9, //!< Set scan 2MBbit/PHY. RE_CA_UART_SET_CH_37 = 10, //!< Set channel 37. RE_CA_UART_SET_CH_38 = 11, //!< Set channel 38. RE_CA_UART_SET_CH_39 = 12, //!< Set channel 39. @@ -125,6 +133,7 @@ typedef enum RE_CA_UART_SET_ALL = 15,//!< Set all config. RE_CA_UART_ADV_RPRT = 16,//!< Advertisement report. ACK no need. RE_CA_UART_DEVICE_ID = 17,//!< Send device id. ACK no need. + RE_CA_UART_ADV_RPRT2 = 18,//!< Advertisement report with extra info. ACK no need. RE_CA_UART_GET_DEVICE_ID = 24,//!< Get device id. Expect RE_CA_UART_DEVICE_ID. RE_CA_UART_GET_ALL = 25,//!< Get all config. RE_CA_UART_ACK = 32,//!< ACK @@ -147,8 +156,24 @@ typedef enum (param2_len) + RE_CA_UART_DELIMITER_LEN + \ (param3_len) + RE_CA_UART_DELIMITER_LEN) +#define RE_CA_UART_TX_DATA_LEN_4_PARAMS(param1_len, param2_len, param3_len, param4_len) ( \ + (param1_len) + RE_CA_UART_DELIMITER_LEN + \ + (param2_len) + RE_CA_UART_DELIMITER_LEN + \ + (param3_len) + RE_CA_UART_DELIMITER_LEN + \ + (param4_len) + RE_CA_UART_DELIMITER_LEN) + #define RE_CA_UART_TX_DATA_LEN_CMD_ADV_RPRT(data_len) \ - RE_CA_UART_TX_DATA_LEN_3_PARAMS(data_len, RE_CA_UART_RSSI_BYTES, RE_CA_UART_MAC_BYTES) + RE_CA_UART_TX_DATA_LEN_3_PARAMS( \ + RE_CA_UART_MAC_BYTES, \ + data_len, \ + RE_CA_UART_RSSI_BYTES) + +#define RE_CA_UART_TX_DATA_LEN_CMD_ADV_RPRT2(data_len) \ + RE_CA_UART_TX_DATA_LEN_4_PARAMS( \ + RE_CA_UART_MAC_BYTES, \ + data_len, \ + RE_CA_UART_RSSI_BYTES, \ + RE_CA_UART_ADV_RPRT2_EXTRA_INFO_BYTES) #define RE_CA_UART_TX_DATA_LEN_CMD_BOOL() \ RE_CA_UART_TX_DATA_LEN_1_PARAM(RE_CA_UART_CMD_BOOL_LEN) @@ -171,6 +196,9 @@ typedef enum #define RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS() \ RE_CA_UART_TX_DATA_LEN_2_PARAMS(RE_CA_UART_CMD_FLTR_ID_LEN, RE_CA_UART_CMD_ALL_BOOL_LEN) +#define RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS_WITH_MAX_ADV_LEN() \ + RE_CA_UART_TX_DATA_LEN_3_PARAMS(RE_CA_UART_CMD_FLTR_ID_LEN, RE_CA_UART_CMD_ALL_BOOL_LEN, RE_CA_UART_CMD_ALL_UINT8_LEN) + #define RE_CA_UART_TX_DATA_LEN_CMD_GET_ALL_PARAMS() \ RE_CA_UART_TX_DATA_LEN_0_PARAMS() @@ -199,7 +227,8 @@ typedef struct re_ca_uart_mosi_payload_buf_encoded_get_device_id_t typedef struct re_ca_uart_mosi_payload_buf_encoded_all_params_t { /** Buffer for the encoded 'all_params' payload. */ - uint8_t buf[RE_CA_UART_TX_BUF_LEN (RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS())]; + uint8_t buf[RE_CA_UART_TX_BUF_LEN ( + RE_CA_UART_TX_DATA_LEN_CMD_ALL_PARAMS_WITH_MAX_ADV_LEN())]; } re_ca_uart_mosi_payload_buf_encoded_all_params_t; /** @brief Buffer for the encoded 'led_ctrl' payload. */ @@ -286,19 +315,22 @@ typedef struct typedef struct { re_ca_uart_ble_bool_t fltr_tags; //!< Filter tag bool state. - re_ca_uart_ble_bool_t coded_phy; //!< Coded phy bool state. - re_ca_uart_ble_bool_t scan_phy; //!< Scanned phy bool state. - re_ca_uart_ble_bool_t ext_payload; //!< Pyload extension bool state. + re_ca_uart_ble_bool_t use_coded_phy;//!< Use BLE Coded PHY bool state. + re_ca_uart_ble_bool_t use_1m_phy; //!< Use BLE 1M PHY bool state. + re_ca_uart_ble_bool_t use_2m_phy; //!< Use BLE 2M PHY bool state. re_ca_uart_ble_bool_t ch_37; //!< Channel 37 bool state. re_ca_uart_ble_bool_t ch_38; //!< Channel 38 bool state. re_ca_uart_ble_bool_t ch_39; //!< Channel 39 bool state. } re_ca_uart_ble_all_bools_t; +#define RE_CA_UART_BLE_ALL_PARAMS_MAX_ADV_LEN_NO_LIMIT (0U) //!< No limit on max advertisement length. + /** @brief BLE config struct. */ typedef struct { re_ca_uart_ble_fltr_id_t fltr_id; //!< Filter id. re_ca_uart_ble_all_bools_t bools; //!< All bool state in payload. + uint8_t max_adv_len; //!< Max advertisement length. } re_ca_uart_ble_all_t; /** @brief BLE device id struct. */ @@ -308,13 +340,57 @@ typedef struct uint64_t addr; //!< DEVICE_ADDR, 64bits } re_ca_uart_ble_id_t; +/** @brief BLE PHY */ +typedef enum re_ca_uart_ble_phy_e +{ + RE_CA_UART_BLE_PHY_AUTO = 0, //!< Automatic PHY selection (BLE_GAP_PHY_AUTO). + RE_CA_UART_BLE_PHY_1MBPS = 1, //!< 1 Mbps PHY (BLE_GAP_PHY_1MBPS). + RE_CA_UART_BLE_PHY_2MBPS = 2, //!< 2 Mbps PHY (BLE_GAP_PHY_2MBPS). + RE_CA_UART_BLE_PHY_CODED = 4, //!< Coded PHY (BLE_GAP_PHY_CODED). + RE_CA_UART_BLE_PHY_NOT_SET = 0x0F, //!< PHY is not configured (RE_CA_UART_BLE_PHY_NOT_SET). +} re_ca_uart_ble_phy_e; + +#define RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID (-64) /*!< Invalid TX power level (BLE_GAP_POWER_LEVEL_INVALID). */ + +#define RE_CA_UART_BLE_GAP_CHANNEL_INDEX_INVALID (0xFF) /*!< Invalid channel index. */ + /** @brief Advertisement payload. */ typedef struct { - uint8_t mac[RE_CA_UART_MAC_BYTES]; //!< MAC address, always 6 bytes. MSB first. - uint8_t adv[RE_CA_UART_ADV_BYTES]; //!< Advertisement, variable length. - uint8_t adv_len; //!< Length of advertisement. - int8_t rssi_db; //!< RSSI. + uint8_t mac[RE_CA_UART_MAC_BYTES]; //!< MAC address, always 6 bytes. MSB first. + uint8_t adv[RE_CA_UART_ADV_BYTES]; //!< Advertisement, variable length. + uint8_t adv_len; //!< Length of advertisement. + int8_t rssi_db; //!< RSSI. + + /** + * @brief Indicates the PHY on which the primary advertising + * packet was received. See BLE_GAP_PHYS. + */ + re_ca_uart_ble_phy_e primary_phy: 4; + + /** + * @brief Indicates the PHY on which the secondary advertising packet + * was received. See BLE_GAP_PHYS. + * This field is set to RE_CA_UART_BLE_PHY_NOT_SET if no packets were + * received on a secondary advertising channel. + */ + re_ca_uart_ble_phy_e secondary_phy: 4; + + /** + * @brief Channel Index on which the last advertising packet + * is received (0-39). + */ + uint8_t ch_index; + + bool is_coded_phy: 1; //!< True if Coded PHY was used + + /** + * @brief TX Power reported by the advertiser in the last packet header + * received. This field is set to RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID + * if the last received packet did not contain the Tx Power field. + * @note TX Power is only included in extended advertising packets. + */ + int8_t tx_power: 7; } re_ca_uart_ble_adv_t; #if 0 diff --git a/test/test_ruuvi_endpoint_ca_uart.c b/test/test_ruuvi_endpoint_ca_uart.c index f9258b5..e072934 100644 --- a/test/test_ruuvi_endpoint_ca_uart.c +++ b/test/test_ruuvi_endpoint_ca_uart.c @@ -7,6 +7,8 @@ #define TX_MAX_LEN (255) +_Static_assert (TX_MAX_LEN <= UINT8_MAX, "TX_MAX_LEN > UINT8_MAX"); + void setUp (void) { // No action needed. @@ -327,7 +329,7 @@ void test_ruuvi_endpoint_ca_uart_ack_encode_invalid (void) TEST_ASSERT (memcmp (expected, buffer, sizeof (expected))); } -void test_ruuvi_endpoint_ca_uart_all_encode (void) +void test_ruuvi_endpoint_ca_uart_set_all_params_encode (void) { re_status_t err_code = RE_SUCCESS; const uint8_t expected_size = 5 + CMD_IN_LEN; @@ -347,22 +349,66 @@ void test_ruuvi_endpoint_ca_uart_all_encode (void) re_ca_uart_ble_all_t params = {0}; params.fltr_id.id = 0x101; params.bools.fltr_tags.state = 1; - params.bools.coded_phy.state = 1; - params.bools.scan_phy.state = 1; - params.bools.ext_payload.state = 1; + params.bools.use_coded_phy.state = 1; + params.bools.use_1m_phy.state = 1; + params.bools.use_2m_phy.state = 1; params.bools.ch_37.state = 1; params.bools.ch_38.state = 1; params.bools.ch_39.state = 1; + params.max_adv_len = 0; re_ca_uart_payload_t payload = {0}; payload.cmd = cmd; payload.params.all_params = params; uint8_t buffer[sizeof (re_ca_uart_mosi_payload_buf_encoded_all_params_t)] = {0}; uint8_t buffer_len = sizeof (buffer); err_code = re_ca_uart_encode (buffer, &buffer_len, &payload); - TEST_ASSERT (RE_SUCCESS == err_code); - TEST_ASSERT (buffer_len == sizeof (expected)); - TEST_ASSERT (buffer_len == sizeof (buffer)); - TEST_ASSERT (!memcmp (expected, buffer, sizeof (expected))); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL (sizeof (expected), buffer_len); + TEST_ASSERT (sizeof (buffer) >= sizeof (expected)); + TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, buffer, sizeof (expected)); +} + +void test_ruuvi_endpoint_ca_uart_set_all_params_with_max_adv_len_encode (void) +{ + re_status_t err_code = RE_SUCCESS; + const uint8_t expected_size = 7 + CMD_IN_LEN; + const uint8_t max_adv_len = 48; + uint8_t expected[] = + { + RE_CA_UART_STX, + expected_size, + RE_CA_UART_SET_ALL, + 0x01U, 0x01U, + RE_CA_UART_FIELD_DELIMITER, + 0x7FU, + RE_CA_UART_FIELD_DELIMITER, + max_adv_len, + RE_CA_UART_FIELD_DELIMITER, + 0x02U, 0x8BU, //crc + RE_CA_UART_ETX + }; + re_ca_uart_cmd_t cmd = RE_CA_UART_SET_ALL; + re_ca_uart_ble_all_t params = {0}; + params.fltr_id.id = 0x101; + params.bools.fltr_tags.state = 1; + params.bools.use_coded_phy.state = 1; + params.bools.use_1m_phy.state = 1; + params.bools.use_2m_phy.state = 1; + params.bools.ch_37.state = 1; + params.bools.ch_38.state = 1; + params.bools.ch_39.state = 1; + params.bools.ch_39.state = 1; + params.max_adv_len = max_adv_len; + re_ca_uart_payload_t payload = {0}; + payload.cmd = cmd; + payload.params.all_params = params; + uint8_t buffer[sizeof (re_ca_uart_mosi_payload_buf_encoded_all_params_t)] = {0}; + uint8_t buffer_len = sizeof (buffer); + err_code = re_ca_uart_encode (buffer, &buffer_len, &payload); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL (sizeof (expected), buffer_len); + TEST_ASSERT (sizeof (buffer) >= sizeof (expected)); + TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, buffer, sizeof (expected)); } void test_ruuvi_endpoint_ca_uart_all_encode_invalid (void) @@ -385,9 +431,9 @@ void test_ruuvi_endpoint_ca_uart_all_encode_invalid (void) re_ca_uart_ble_all_t params = {0}; params.fltr_id.id = 0x101; params.bools.fltr_tags.state = 1; - params.bools.coded_phy.state = 1; - params.bools.scan_phy.state = 1; - params.bools.ext_payload.state = 1; + params.bools.use_coded_phy.state = 1; + params.bools.use_1m_phy.state = 1; + params.bools.use_2m_phy.state = 1; params.bools.ch_37.state = 1; params.bools.ch_38.state = 1; params.bools.ch_39.state = 1; @@ -632,46 +678,351 @@ void test_ruuvi_endpoint_ca_uart_bool_encode_invalid (void) TEST_ASSERT (memcmp (expected, buffer, sizeof (expected))); } -void test_ruuvi_endpoint_ca_uart_adv_encode (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt_encode (void) { re_status_t err_code = RE_SUCCESS; + re_ca_uart_cmd_t cmd = RE_CA_UART_ADV_RPRT; +#if !defined(RI_ADV_EXTENDED_ENABLED) || (!RI_ADV_EXTENDED_ENABLED) const uint8_t expected_size = 41 + CMD_IN_LEN; - uint8_t expected[] = + const uint8_t expected[] = { RE_CA_UART_STX, expected_size, - RE_CA_UART_ADV_RPRT, + cmd, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0x50U, 0x00U, 0xC8U, 0xFCU, - 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, - 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< Data + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x2CU, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, + RE_CA_UART_FIELD_DELIMITER, //!< Data 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI 0xB3U, 0x0CU, //crc16 RE_CA_UART_ETX }; - re_ca_uart_cmd_t cmd = RE_CA_UART_ADV_RPRT; - re_ca_uart_ble_adv_t params = + const re_ca_uart_ble_adv_t params = { { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0x50U, 0x00U, 0xC8U, 0xFCU, - 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, - 0x8DU + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x2CU, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< Data - 31, //!< Data length + RE_CA_UART_ADV_BYTES, //!< Data length -40//RSSI }; - re_ca_uart_payload_t payload = {0};; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 48) + const uint8_t expected_size = 58U + CMD_IN_LEN; + const uint8_t expected[] = + { + RE_CA_UART_STX, + expected_size, + cmd, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI + 0xE3U, 0xFFU,//crc16 + RE_CA_UART_ETX + }; + const re_ca_uart_ble_adv_t params = + { + { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC + { + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + }, //!< Data + RE_CA_UART_ADV_BYTES, //!< Data length + -40//RSSI + }; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAY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 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + }, //!< Data + RE_CA_UART_ADV_BYTES, //!< Data length + -40//RSSI + }; +#else +#error "Invalid configuration" +#endif + TEST_ASSERT_EQUAL (16 + RE_CA_UART_ADV_BYTES, sizeof (expected)); + TEST_ASSERT_EQUAL (sizeof (expected) - 6, expected[1]); + re_ca_uart_payload_t payload = {0}; payload.cmd = cmd; payload.params.adv = params; uint8_t buffer[TX_MAX_LEN] = {0}; + _Static_assert (sizeof (buffer) <= UINT8_MAX, "Buffer too large"); uint8_t buffer_len = sizeof (buffer); err_code = re_ca_uart_encode (buffer, &buffer_len, &payload); - TEST_ASSERT (RE_SUCCESS == err_code); - TEST_ASSERT (buffer_len == sizeof (expected)); - TEST_ASSERT (!memcmp (expected, buffer, sizeof (expected))); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL (sizeof (expected), buffer_len); + TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, buffer, sizeof (expected)); +} + +void test_ruuvi_endpoint_ca_uart_adv_rprt2_encode (void) +{ + re_status_t err_code = RE_SUCCESS; + re_ca_uart_cmd_t cmd = RE_CA_UART_ADV_RPRT2; +#if !defined(RI_ADV_EXTENDED_ENABLED) || (!RI_ADV_EXTENDED_ENABLED) + const uint8_t expected_size = 45 + CMD_IN_LEN; + const uint8_t expected[] = + { + RE_CA_UART_STX, + expected_size, + cmd, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x2CU, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI + 0x1FU, 0x25U, 4, RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0xF0U, 0x25U, //crc16 + RE_CA_UART_ETX + }; + const re_ca_uart_ble_adv_t params = + { + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, + .adv = { + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x2CU, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU + }, + .adv_len = RE_CA_UART_ADV_BYTES, + .rssi_db = -40, + .primary_phy = RE_CA_UART_BLE_PHY_1MBPS, + .secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .ch_index = 37, + .is_coded_phy = false, + .tx_power = 4, + }; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 48) + const uint8_t expected_size = 62U + CMD_IN_LEN; + const uint8_t expected[] = + { + RE_CA_UART_STX, + expected_size, + cmd, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI + 0x12U, 0x27U, -5 & 0x7F, RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0xC5U, 0x18U,//crc16 + RE_CA_UART_ETX + }; + const re_ca_uart_ble_adv_t params = + { + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, + .adv = { + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + }, + .adv_len = RE_CA_UART_ADV_BYTES, + .rssi_db = -40, + .primary_phy = RE_CA_UART_BLE_PHY_1MBPS, + .secondary_phy = RE_CA_UART_BLE_PHY_2MBPS, + .ch_index = 39, + .is_coded_phy = false, + .tx_power = -5, + }; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 235) + const uint8_t expected_size = 249 + CMD_IN_LEN; + const uint8_t expected[] = + { + RE_CA_UART_STX, + expected_size, + cmd, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0xEEU, 0xFFU, 0x99U, 0x04U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFF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FFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI + 0x44U, 0x29U, (1 << 7) | (RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID & 0x7F), RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0x0DU, 0x8DU, //crc16 + RE_CA_UART_ETX + }; + const re_ca_uart_ble_adv_t params = + { + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, + .adv = { + 0xEEU, 0xFFU, 0x99U, 0x04U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0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 0xFFU, + }, + .adv_len = RE_CA_UART_ADV_BYTES, + .rssi_db = -40, + .primary_phy = RE_CA_UART_BLE_PHY_CODED, + .secondary_phy = RE_CA_UART_BLE_PHY_CODED, + .ch_index = 41, + .is_coded_phy = true, + .tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, + }; +#else +#error "Invalid configuration" +#endif + _Static_assert (sizeof (expected) <= UINT8_MAX, "Buffer too large"); + TEST_ASSERT_EQUAL (20 + RE_CA_UART_ADV_BYTES, sizeof (expected)); + TEST_ASSERT_EQUAL (sizeof (expected) - 6, expected[1]); + re_ca_uart_payload_t payload = {0}; + payload.cmd = cmd; + payload.params.adv = params; + uint8_t buffer[TX_MAX_LEN] = {0}; + _Static_assert (sizeof (buffer) <= UINT8_MAX, "Buffer too large"); + uint8_t buffer_len = sizeof (buffer); + err_code = re_ca_uart_encode (buffer, &buffer_len, &payload); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL (sizeof (expected), buffer_len); + TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, buffer, sizeof (expected)); } void test_ruuvi_endpoint_ca_uart_adv_encode_invalid (void) @@ -960,7 +1311,7 @@ void test_ruuvi_endpoint_ca_uart_ack_decode_invalid (void) TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); } -void test_ruuvi_endpoint_ca_uart_all_decode (void) +void test_ruuvi_endpoint_ca_uart_set_all_params_decode (void) { re_status_t err_code = RE_SUCCESS; uint8_t data[] = @@ -978,19 +1329,57 @@ void test_ruuvi_endpoint_ca_uart_all_decode (void) re_ca_uart_ble_all_t expect_params = {0}; expect_params.fltr_id.id = 0x101; expect_params.bools.fltr_tags.state = 1; - expect_params.bools.coded_phy.state = 1; - expect_params.bools.scan_phy.state = 1; - expect_params.bools.ext_payload.state = 1; + expect_params.bools.use_coded_phy.state = 1; + expect_params.bools.use_1m_phy.state = 1; + expect_params.bools.use_2m_phy.state = 1; expect_params.bools.ch_37.state = 1; expect_params.bools.ch_38.state = 1; expect_params.bools.ch_39.state = 1; + expect_params.max_adv_len = 0; re_ca_uart_cmd_t expect_cmd = RE_CA_UART_SET_ALL; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_SUCCESS == err_code); - TEST_ASSERT (!memcmp (&expect_params, &payload.params.all_params, - sizeof (expect_params))); - TEST_ASSERT (!memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL_HEX8_ARRAY (&expect_params, &payload.params.all_params, + sizeof (expect_params)); + TEST_ASSERT_EQUAL (expect_cmd, payload.cmd); +} + +void test_ruuvi_endpoint_ca_uart_set_all_params_with_max_adv_len_decode (void) +{ + const uint8_t max_adv_len = 48; + re_status_t err_code = RE_SUCCESS; + uint8_t data[] = + { + RE_CA_UART_STX, + 7 + CMD_IN_LEN, + RE_CA_UART_SET_ALL, + 0x01U, 0x01U, + RE_CA_UART_FIELD_DELIMITER, + 0x7FU, + RE_CA_UART_FIELD_DELIMITER, + max_adv_len, + RE_CA_UART_FIELD_DELIMITER, + 0x02U, 0x8BU, //crc + RE_CA_UART_ETX + }; + re_ca_uart_ble_all_t expect_params = {0}; + expect_params.fltr_id.id = 0x101; + expect_params.bools.fltr_tags.state = 1; + expect_params.bools.use_coded_phy.state = 1; + expect_params.bools.use_1m_phy.state = 1; + expect_params.bools.use_2m_phy.state = 1; + expect_params.bools.ch_37.state = 1; + expect_params.bools.ch_38.state = 1; + expect_params.bools.ch_39.state = 1; + expect_params.max_adv_len = max_adv_len; + re_ca_uart_cmd_t expect_cmd = RE_CA_UART_SET_ALL; + re_ca_uart_payload_t payload = {0}; + err_code = re_ca_uart_decode (data, &payload); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL_HEX8_ARRAY (&expect_params, &payload.params.all_params, + sizeof (expect_params)); + TEST_ASSERT_EQUAL (expect_cmd, payload.cmd); } void test_ruuvi_endpoint_ca_uart_all_decode_invalid (void) @@ -1011,9 +1400,9 @@ void test_ruuvi_endpoint_ca_uart_all_decode_invalid (void) re_ca_uart_ble_all_t expect_params = {0}; expect_params.fltr_id.id = 0x101; expect_params.bools.fltr_tags.state = 1; - expect_params.bools.coded_phy.state = 0; - expect_params.bools.scan_phy.state = 0; - expect_params.bools.ext_payload.state = 0; + expect_params.bools.use_coded_phy.state = 0; + expect_params.bools.use_1m_phy.state = 0; + expect_params.bools.use_2m_phy.state = 0; expect_params.bools.ch_37.state = 0; expect_params.bools.ch_38.state = 0; expect_params.bools.ch_39.state = 0; @@ -1298,7 +1687,7 @@ void test_ruuvi_endpoint_ca_uart_bool_decode_invalid (void) re_ca_uart_cmd_t expect_cmd = RE_CA_UART_SET_CH_37; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_LEN == err_code); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_LEN, err_code); TEST_ASSERT (memcmp (&expect_params, &payload.params.bool_param, sizeof (expect_params))); TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); } @@ -1320,24 +1709,9 @@ void test_ruuvi_endpoint_ca_uart_decode_invalid_crc (void) 0x61U, 0xF0U, //crc16 RE_CA_UART_ETX }; - re_ca_uart_ble_adv_t expect_params = - { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0xE3U, - 0x8DU - }, //!< Data - 31U, //!< Data length - -40 //RSSI - }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_CRC == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_CRC, err_code); } @@ -1357,28 +1731,13 @@ void test_ruuvi_endpoint_ca_uart_decode_no_stx (void) 0x61U, 0xF2U, //crc16 RE_CA_UART_ETX }; - re_ca_uart_ble_adv_t expect_params = - { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0xE3U, - 0x8DU - }, //!< Data - 31U, //!< Data length - -40 //RSSI - }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_STX == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_STX, err_code); } -void test_ruuvi_endpoint_ca_uart_decode_noetx (void) +void test_ruuvi_endpoint_ca_uart_decode_no_etx (void) { re_status_t err_code = RE_SUCCESS; uint8_t data[] = @@ -1394,64 +1753,363 @@ void test_ruuvi_endpoint_ca_uart_decode_noetx (void) 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI 0x61U, 0xF2U //crc16 }; + re_ca_uart_payload_t payload = {0}; + err_code = re_ca_uart_decode (data, &payload); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_ETX, err_code); +} + +void test_ruuvi_endpoint_ca_uart_adv_rprt_decode (void) +{ + re_status_t err_code = RE_SUCCESS; +#if !defined(RI_ADV_EXTENDED_ENABLED) || (!RI_ADV_EXTENDED_ENABLED) + uint8_t data[] = + { + RE_CA_UART_STX, + 41U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x2CU, 0xE3U, 0x8DU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI + 0x61U, 0xF2U,//crc16 + RE_CA_UART_ETX + }; re_ca_uart_ble_adv_t expect_params = { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0xE3U, - 0x8DU + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC + .adv = { + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x2CU, 0xE3U, 0x8DU }, //!< Data - 31U, //!< Data length - -40 //RSSI + .adv_len = RE_CA_UART_ADV_BYTES, //!< Data length + .rssi_db = -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .ch_index = RE_CA_UART_BLE_GAP_CHANNEL_INDEX_INVALID, + .is_coded_phy = false, + .tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, + }; + TEST_ASSERT_EQUAL (47, sizeof (data)); +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 48) + uint8_t data[] = + { + RE_CA_UART_STX, + 58U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0xE3U, 0xFFU,//crc16 + RE_CA_UART_ETX }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; + TEST_ASSERT_EQUAL (64, sizeof (data)); + re_ca_uart_ble_adv_t expect_params = + { + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC + .adv = { + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + }, //!< Data + .adv_len = RE_CA_UART_ADV_BYTES, //!< Data length + .rssi_db = -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .ch_index = RE_CA_UART_BLE_GAP_CHANNEL_INDEX_INVALID, + .is_coded_phy = false, + .tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, + }; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 235) + const uint8_t data[] = + { + RE_CA_UART_STX, + RI_COMM_BLE_PAYLOAD_MAX_LENGTH + 10 + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0xEFU, 0xFFU, 0x99U, 0x04U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFF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FFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0x51U, 0x7EU,//crc16 + RE_CA_UART_ETX + }; + TEST_ASSERT_EQUAL (251, sizeof (data)); + const re_ca_uart_ble_adv_t expect_params = + { + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC + .adv = { + 0xEFU, 0xFFU, 0x99U, 0x04U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU,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U, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + }, //!< Data + .adv_len = RE_CA_UART_ADV_BYTES, //!< Data length + .rssi_db = -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .ch_index = RE_CA_UART_BLE_GAP_CHANNEL_INDEX_INVALID, + .is_coded_phy = false, + .tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, + }; +#else +#error "Invalid configuration" +#endif + TEST_ASSERT_EQUAL (16 + RE_CA_UART_ADV_BYTES, sizeof (data)); + TEST_ASSERT_EQUAL (sizeof (data) - 6, data[1]); re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_ETX == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL_HEX8_ARRAY (&expect_params, &payload.params.adv, + sizeof (expect_params)); + TEST_ASSERT_EQUAL (RE_CA_UART_ADV_RPRT, payload.cmd); } -void test_ruuvi_endpoint_ca_uart_adv_decode (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt2_decode (void) { re_status_t err_code = RE_SUCCESS; +#if !defined(RI_ADV_EXTENDED_ENABLED) || (!RI_ADV_EXTENDED_ENABLED) uint8_t data[] = { RE_CA_UART_STX, - 41U + CMD_IN_LEN, - RE_CA_UART_ADV_RPRT, + 45U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT2, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0xE3U, 0x8DU, + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x2CU, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< Data 0xD8U, RE_CA_UART_FIELD_DELIMITER, //RSSI - 0x61U, 0xF2U,//crc16 + 0x1FU, 0x25U, (0 << 7) | (4 & 0x7F), RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0xF0U, 0x25U, //crc16 RE_CA_UART_ETX }; re_ca_uart_ble_adv_t expect_params = { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, RE_CA_UART_FIELD_DELIMITER, 0xE3U, - 0x8DU + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC + .adv = { + 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x2CU, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< Data - 31U, //!< Data length - -40 //RSSI + .adv_len = RE_CA_UART_ADV_BYTES, //!< Data length + .rssi_db = -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_1MBPS, + .secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .ch_index = 37, + .is_coded_phy = false, + .tx_power = 4, + }; + TEST_ASSERT_EQUAL (51, sizeof (data)); +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 48) + uint8_t data[] = + { + RE_CA_UART_STX, + 62U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT2, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0x12U, 0x27U, (0 << 7) | (-5 & 0x7F), RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0xC5U, 0x18U,//crc16 + RE_CA_UART_ETX }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; + TEST_ASSERT_EQUAL (68, sizeof (data)); + re_ca_uart_ble_adv_t expect_params = + { + .mac = { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC + .adv = { + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x03U, 0x03U, 0x98U, 0xFCU, + }, //!< Data + .adv_len = RE_CA_UART_ADV_BYTES, //!< Data length + .rssi_db = -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_1MBPS, + .secondary_phy = RE_CA_UART_BLE_PHY_2MBPS, + .ch_index = 39, + .is_coded_phy = false, + .tx_power = -5, + }; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 235) + const uint8_t data[] = + { + RE_CA_UART_STX, + RI_COMM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FFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0x44U, 0x29U, (1 << 7) | RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0x0DU, 0x8DU, //crc16 + RE_CA_UART_ETX + }; + TEST_ASSERT_EQUAL (255, sizeof (data)); + const re_ca_uart_ble_adv_t expect_params = + { + .m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0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + }, //!< Data + .adv_len = RE_CA_UART_ADV_BYTES, //!< Data length + .rssi_db = -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_CODED, + .secondary_phy = RE_CA_UART_BLE_PHY_CODED, + .ch_index = 41, + .is_coded_phy = true, + .tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, + }; +#else +#error "Invalid configuration" +#endif + TEST_ASSERT_EQUAL (20 + RE_CA_UART_ADV_BYTES, sizeof (data)); + TEST_ASSERT_EQUAL (sizeof (data) - 6, data[1]); re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_SUCCESS == err_code); - TEST_ASSERT (!memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (!memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL_HEX8_ARRAY (&expect_params, &payload.params.adv, + sizeof (expect_params)); + TEST_ASSERT_EQUAL (RE_CA_UART_ADV_RPRT2, payload.cmd); } -void test_ruuvi_endpoint_ca_uart_adv_decode_nodelimiter1 (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt_decode_no_delimiter1 (void) { re_status_t err_code = RE_SUCCESS; uint8_t data[] = @@ -1467,26 +2125,12 @@ void test_ruuvi_endpoint_ca_uart_adv_decode_nodelimiter1 (void) 0x9EU, 0x63U,//crc16 RE_CA_UART_ETX }; - re_ca_uart_ble_adv_t expect_params = - { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU - }, //!< Data - 31U, //!< Data length - -40 //RSSI - }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_DELIMITER == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_DELIMITER, err_code); } -void test_ruuvi_endpoint_ca_uart_adv_decode_nodelimiter2 (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt_decode_no_delimiter2 (void) { re_status_t err_code = RE_SUCCESS; uint8_t data[] = @@ -1502,26 +2146,12 @@ void test_ruuvi_endpoint_ca_uart_adv_decode_nodelimiter2 (void) 0xF0U, 0xFCU,//crc16 RE_CA_UART_ETX }; - re_ca_uart_ble_adv_t expect_params = - { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU - }, //!< Data - 31U, //!< Data length - -40 //RSSI - }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_DELIMITER == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_DELIMITER, err_code); } -void test_ruuvi_endpoint_ca_uart_adv_decode_nodelimiter3 (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt_decode_no_delimiter3 (void) { re_status_t err_code = RE_SUCCESS; uint8_t data[] = @@ -1538,65 +2168,199 @@ void test_ruuvi_endpoint_ca_uart_adv_decode_nodelimiter3 (void) 0xC4U, 0xF5U, //crc16 RE_CA_UART_ETX }; - re_ca_uart_ble_adv_t expect_params = + re_ca_uart_payload_t payload = {0}; + err_code = re_ca_uart_decode (data, &payload); + TEST_ASSERT_EQUAL (RE_ERROR_DECODING_DELIMITER, err_code); +} + +void test_ruuvi_endpoint_ca_uart_adv_rprt_decode_too_long (void) +{ + re_status_t err_code = RE_SUCCESS; +#if !defined(RI_ADV_EXTENDED_ENABLED) || (!RI_ADV_EXTENDED_ENABLED) + uint8_t data[] = { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU - }, //!< Data - 31U, //!< Data length - -40 //RSSI + RE_CA_UART_STX, + 42U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x02U, 0x01U, 0x04U, 0x1CU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, + 0x02U, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0x50U, 0x11,//crc16 + RE_CA_UART_ETX }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 48) + uint8_t data[] = + { + RE_CA_UART_STX, + 59U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x04U, 0x03U, 0x98U, 0xFCU, + 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0xFEU, 0x55U,//crc16 + RE_CA_UART_ETX + }; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 235) + uint8_t data[] = + { + RE_CA_UART_STX, + RI_COMM_BLE_PAYLOAD_MAX_LENGTH + 1 + 10 + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0xF0U, 0xFFU, 0x99U, 0x04U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xF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xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0xBEU, 0x11U,//crc16 + RE_CA_UART_ETX + }; +#else +#error "Invalid configuration" +#endif + TEST_ASSERT_EQUAL (17 + RE_CA_UART_ADV_BYTES, sizeof (data)); + TEST_ASSERT_EQUAL (sizeof (data) - 6, data[1]); re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_DELIMITER == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_ERROR_DATA_SIZE, err_code); } -void test_ruuvi_endpoint_ca_uart_adv_decode_toolong (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt2_decode_too_long (void) { re_status_t err_code = RE_SUCCESS; +#if !defined(RI_ADV_EXTENDED_ENABLED) || (!RI_ADV_EXTENDED_ENABLED) uint8_t data[] = { RE_CA_UART_STX, - 72U + CMD_IN_LEN, - RE_CA_UART_ADV_RPRT, + 46U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT2, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, + 0x02U, 0x01U, 0x04U, 0x1CU, 0xFFU, 0x99U, 0x04U, 0x05U, + 0x0FU, 0x27U, 0x40U, 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, + 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, 0xF0U, 0x30U, + 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, + 0x02U, RE_CA_UART_FIELD_DELIMITER, //!< Data 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI - 0x1DU, 0xD6U,//crc16 + 0x1FU, 0x25U, (0 << 7) | (4 & 0x7F), RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0x8AU, 0xA8,//crc16 RE_CA_UART_ETX }; - re_ca_uart_ble_adv_t expect_params = +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 48) + uint8_t data[] = { - { 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU }, //!< MAC - { - 0x02U, 0x01U, 0x04U, 0x1BU, 0xFFU, 0x99U, 0x04U, 0x05U, 0x0FU, 0x27U, 0x40U, - 0x35U, 0xC4U, 0x54U, 0x00U, 0x50U, 0x00U, 0xC8U, 0xFCU, 0x20U, 0xA4U, 0x56U, - 0xF0U, 0x30U, 0xE5U, 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU - }, //!< Data - 31U, //!< Data length - -40 //RSSI + RE_CA_UART_STX, + 63U + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT2, + 0xC9U, 0x44U, 0x54U, 0x29U, 0xE3U, 0x8DU, RE_CA_UART_FIELD_DELIMITER, //!< MAC + 0x2BU, 0xFFU, 0x99U, 0x04U, 0xE0U, 0x14U, 0xC9U, 0x55U, + 0xA4U, 0xC5U, 0x78U, 0x00U, 0xCFU, 0x00U, 0xD8U, 0x00U, + 0xD8U, 0x00U, 0xD8U, 0x02U, 0xA8U, 0x00U, 0x38U, 0x00U, + 0x01U, 0x00U, 0x00U, 0x00U, 0x00U, 0x08U, 0x1FU, 0x2CU, + 0x01U, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xD0U, 0xEEU, + 0x40U, 0xC1U, 0x60U, 0xDCU, 0x04U, 0x03U, 0x98U, 0xFCU, + 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0x12U, 0x27U, (0 << 7) | (-5 & 0x7F), RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0x91U, 0xABU,//crc16 + RE_CA_UART_ETX }; - re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; +#elif defined(RI_ADV_EXTENDED_ENABLED) && RI_ADV_EXTENDED_ENABLED && (RI_COMM_BLE_PAYLOAD_MAX_LENGTH == 235) + uint8_t data[] = + { + RE_CA_UART_STX, + RI_COMM_BLE_PAYLOAD_MAX_LENGTH + 1 + 14 + CMD_IN_LEN, + RE_CA_UART_ADV_RPRT2, + 0xC9U, 0x44U, 0x54U, 0x29U,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0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, + 0xFFU, 0xFFU, 0xFFU, + 0xFFU, + RE_CA_UART_FIELD_DELIMITER, //!< Data + 0xD8U, RE_CA_UART_FIELD_DELIMITER, //!< Data//RSSI + 0x44U, 0x29U, (1 << 7) | RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, RE_CA_UART_FIELD_DELIMITER, // Primary/secondary PHY, ch index, Tx power + 0x99U, 0x9BU,//crc16 + RE_CA_UART_ETX + }; +#else +#error "Invalid configuration" +#endif + TEST_ASSERT_EQUAL (21 + RE_CA_UART_ADV_BYTES, sizeof (data)); + TEST_ASSERT_EQUAL (sizeof (data) - 6, data[1]); re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_ERROR_DECODING_LEN == err_code); - TEST_ASSERT (memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_ERROR_DATA_SIZE, err_code); } -void test_ruuvi_endpoint_ca_uart_adv_decode_nodata (void) +void test_ruuvi_endpoint_ca_uart_adv_rprt_decode_no_data (void) { re_status_t err_code = RE_SUCCESS; uint8_t data[] = @@ -1616,12 +2380,18 @@ void test_ruuvi_endpoint_ca_uart_adv_decode_nodata (void) { }, //!< Data 0U, //!< Data length - -40 //RSSI + -40, //RSSI + .primary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .secondary_phy = RE_CA_UART_BLE_PHY_NOT_SET, + .ch_index = RE_CA_UART_BLE_GAP_CHANNEL_INDEX_INVALID, + .is_coded_phy = false, + .tx_power = RE_CA_UART_BLE_GAP_POWER_LEVEL_INVALID, }; re_ca_uart_cmd_t expect_cmd = RE_CA_UART_ADV_RPRT; re_ca_uart_payload_t payload = {0}; err_code = re_ca_uart_decode (data, &payload); - TEST_ASSERT (RE_SUCCESS == err_code); - TEST_ASSERT (!memcmp (&expect_params, &payload.params.adv, sizeof (expect_params))); - TEST_ASSERT (!memcmp (&expect_cmd, &payload.cmd, sizeof (expect_cmd))); + TEST_ASSERT_EQUAL (RE_SUCCESS, err_code); + TEST_ASSERT_EQUAL_HEX8_ARRAY (&expect_params, &payload.params.adv, + sizeof (expect_params)); + TEST_ASSERT_EQUAL_HEX8_ARRAY (&expect_cmd, &payload.cmd, sizeof (expect_cmd)); }