Skip to content

Commit

Permalink
Extend 'set_all_params' command to pass additional parameter max_adv_len
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed Oct 17, 2024
1 parent 67eadd3 commit e9c5e56
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 43 deletions.
81 changes: 50 additions & 31 deletions src/ruuvi_endpoint_ca_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
(flags >> 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);
(flags >> 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);
(flags >> 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_EXT_PLD_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;
Expand Down Expand Up @@ -597,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:
Expand All @@ -617,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:
Expand Down Expand Up @@ -917,22 +924,26 @@ 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;
}
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_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,
Expand All @@ -956,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;
Expand Down Expand Up @@ -1014,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:
Expand All @@ -1034,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:
Expand Down
12 changes: 10 additions & 2 deletions src/ruuvi_endpoint_ca_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,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. */
Expand All @@ -124,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.
Expand Down Expand Up @@ -195,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()

Expand Down Expand Up @@ -223,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. */
Expand Down Expand Up @@ -318,11 +323,14 @@ typedef struct
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. */
Expand Down
102 changes: 92 additions & 10 deletions test/test_ruuvi_endpoint_ca_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,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;
Expand All @@ -355,16 +355,60 @@ void test_ruuvi_endpoint_ca_uart_all_encode (void)
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.coded_phy.state = 1;
params.bools.scan_phy.state = 1;
params.bools.ext_payload.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)
Expand Down Expand Up @@ -1267,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[] =
Expand All @@ -1291,13 +1335,51 @@ void test_ruuvi_endpoint_ca_uart_all_decode (void)
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.coded_phy.state = 1;
expect_params.bools.scan_phy.state = 1;
expect_params.bools.ext_payload.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)
Expand Down

0 comments on commit e9c5e56

Please sign in to comment.