From d18e7ab2d6df09f3e91e729d45b41824848315be Mon Sep 17 00:00:00 2001 From: rbaron Date: Fri, 18 Nov 2022 01:14:58 +0100 Subject: [PATCH] Kconfigure BLE service data length --- code/nrf-connect/Kconfig | 11 +++++++++-- code/nrf-connect/src/prst/ble/ble.c | 2 +- code/nrf-connect/src/prst/ble/encoding.c | 22 ++++++++-------------- code/nrf-connect/src/prst/ble/encoding.h | 2 -- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/code/nrf-connect/Kconfig b/code/nrf-connect/Kconfig index 1c80f85..112c4bb 100644 --- a/code/nrf-connect/Kconfig +++ b/code/nrf-connect/Kconfig @@ -12,7 +12,7 @@ config PRST_NETWORK_BLE bool "Uses BLE as the network" depends on BT -endchoice +endchoice # PRST_NETWORK ### ### BLE configs @@ -29,4 +29,11 @@ config PRST_BLE_ENCODING_BPARASITE_V2 config PRST_BLE_ENCODING_BTHOME bool "Uses the BTHome (bthome.io) BLE encoding" -endchoice \ No newline at end of file +endchoice # PRST_BLE_ENCODING + +config PRST_BLE_ENCODING_SERVICE_DATA_LEN + int + help + Size of the service data buffer + default 20 if PRST_BLE_ENCODING_BPARASITE_V2 + default 18 if PRST_BLE_ENCODING_BTHOME diff --git a/code/nrf-connect/src/prst/ble/ble.c b/code/nrf-connect/src/prst/ble/ble.c index e86803c..0201491 100644 --- a/code/nrf-connect/src/prst/ble/ble.c +++ b/code/nrf-connect/src/prst/ble/ble.c @@ -9,7 +9,7 @@ LOG_MODULE_REGISTER(ble, LOG_LEVEL_DBG); -static uint8_t service_data[PRST_BLE_ENCODING_SERVICE_DATA_LEN] = {0}; +static uint8_t service_data[CONFIG_PRST_BLE_ENCODING_SERVICE_DATA_LEN] = {0}; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), diff --git a/code/nrf-connect/src/prst/ble/encoding.c b/code/nrf-connect/src/prst/ble/encoding.c index 3970ba8..d961778 100644 --- a/code/nrf-connect/src/prst/ble/encoding.c +++ b/code/nrf-connect/src/prst/ble/encoding.c @@ -9,46 +9,40 @@ LOG_MODULE_DECLARE(ble, LOG_LEVEL_DBG); int prst_ble_encode_service_data(const prst_sensors_t* sensors, const bt_addr_le_t* bt_addr, uint8_t* out, uint8_t out_len) { - RET_CHECK(out_len >= PRST_BLE_ENCODING_SERVICE_DATA_LEN, + RET_CHECK(out_len >= CONFIG_PRST_BLE_ENCODING_SERVICE_DATA_LEN, "Buffer is not large enough"); - // 0x181c - BTHome Service UUID. - out[0] = 0x1c; +#if CONFIG_PRST_BLE_ENCODING_BPARASITE_V2 + // 0x181a - Environmental sensing service UUID. + out[0] = 0x1a; out[1] = 0x18; - - // Soil Moisture. - // // Four bits for the protocol version. out[2] |= (2 << 4) & 0xf0; - // Bit 0 of byte 0 specifies whether or not ambient light data exists in the // payload. out[2] |= 1; - // 4 bits for a small wrap-around counter for deduplicating messages on the // receiver. // out[3] = sensors->run_counter & 0x0f; - out[4] = sensors->batt.millivolts >> 8; out[5] = sensors->batt.millivolts & 0xff; - int16_t temp_centicelsius = 100 * sensors->shtc3.temp_c; out[6] = temp_centicelsius >> 8; out[7] = temp_centicelsius & 0xff; - uint16_t humi = sensors->shtc3.rel_humi * UINT16_MAX; out[8] = humi >> 8; out[9] = humi & 0xff; - uint16_t soil_moisture = sensors->soil.percentage * UINT16_MAX; out[10] = soil_moisture >> 8; out[11] = soil_moisture & 0xff; - // MAC address in big-endian. memcpy(out + 12, bt_addr->a.val, BT_ADDR_SIZE); - out[18] = sensors->photo.brightness >> 8; out[19] = sensors->photo.brightness & 0xff; +#elif CONFIG_PRST_BLE_ENCODING_BTHOME + // TODO. + memset(out, 0xab, out_len); +#endif // Enccoding protocols return 0; } \ No newline at end of file diff --git a/code/nrf-connect/src/prst/ble/encoding.h b/code/nrf-connect/src/prst/ble/encoding.h index ff1e715..3054a09 100644 --- a/code/nrf-connect/src/prst/ble/encoding.h +++ b/code/nrf-connect/src/prst/ble/encoding.h @@ -5,8 +5,6 @@ #include "prst/data.h" -#define PRST_BLE_ENCODING_SERVICE_DATA_LEN 20 - int prst_ble_encode_service_data(const prst_sensors_t* sensors, const bt_addr_le_t* bt_addr, uint8_t* out, uint8_t out_len);