|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdbool.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <string.h> |
| 10 | +#include <unity.h> |
| 11 | + |
| 12 | +#include <ble.h> |
| 13 | +#include <ble_err.h> |
| 14 | +#include <ble_gatt.h> |
| 15 | +#include <nrf_error.h> |
| 16 | +#include <bm/bluetooth/services/ble_lbs.h> |
| 17 | +#include <bm/bluetooth/services/uuid.h> |
| 18 | + |
| 19 | +#include "cmock_ble.h" |
| 20 | +#include "cmock_ble_gatts.h" |
| 21 | + |
| 22 | +#define SERVICE_HANDLE 0x1234 |
| 23 | +#define BUTTON_VALUE_HANDLE 0xCAFE |
| 24 | +#define BUTTON_CCCD_HANDLE 0xC0DE |
| 25 | +#define LED_VALUE_HANDLE 0xBEEF |
| 26 | +#define UUID_TYPE_VS 0x42 |
| 27 | +#define CONN_HANDLE 0x0055 |
| 28 | + |
| 29 | +/* An arbitrary error, to test forwarding of errors from SoftDevice calls. */ |
| 30 | +#define ERROR 0xbaadf00d |
| 31 | + |
| 32 | +BLE_LBS_DEF(ble_lbs); |
| 33 | + |
| 34 | +static int evt_handler_called; |
| 35 | +static struct ble_lbs_evt last_evt; |
| 36 | +static uint8_t expected_button_state; |
| 37 | +static int hvx_stub_called; |
| 38 | + |
| 39 | +static const ble_uuid128_t expected_base_uuid = { |
| 40 | + .uuid128 = BLE_UUID_LBS_BASE, |
| 41 | +}; |
| 42 | + |
| 43 | +static const struct ble_lbs_config lbs_cfg_template = { |
| 44 | + .evt_handler = NULL, |
| 45 | + .sec_mode = { |
| 46 | + .lbs_button_char = { |
| 47 | + .read = { .lv = 1, .sm = 2 }, |
| 48 | + .cccd_write = { .lv = 3, .sm = 4 }, |
| 49 | + }, |
| 50 | + .lbs_led_char = { |
| 51 | + .read = { .lv = 5, .sm = 6 }, |
| 52 | + .write = { .lv = 7, .sm = 8 }, |
| 53 | + }, |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +static uint32_t stub_sd_ble_uuid_vs_add_success(const ble_uuid128_t *p_vs_uuid, |
| 58 | + uint8_t *p_uuid_type, int calls) |
| 59 | +{ |
| 60 | + TEST_ASSERT_NOT_NULL(p_vs_uuid); |
| 61 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_base_uuid.uuid128, p_vs_uuid->uuid128, |
| 62 | + sizeof(expected_base_uuid.uuid128)); |
| 63 | + TEST_ASSERT_NOT_NULL(p_uuid_type); |
| 64 | + |
| 65 | + *p_uuid_type = UUID_TYPE_VS; |
| 66 | + |
| 67 | + return NRF_SUCCESS; |
| 68 | +} |
| 69 | + |
| 70 | +static uint32_t stub_sd_ble_gatts_service_add_success(uint8_t type, const ble_uuid_t *p_uuid, |
| 71 | + uint16_t *p_handle, int calls) |
| 72 | +{ |
| 73 | + TEST_ASSERT_EQUAL(BLE_GATTS_SRVC_TYPE_PRIMARY, type); |
| 74 | + |
| 75 | + TEST_ASSERT_NOT_NULL(p_uuid); |
| 76 | + TEST_ASSERT_EQUAL(UUID_TYPE_VS, p_uuid->type); |
| 77 | + TEST_ASSERT_EQUAL(BLE_UUID_LBS_SERVICE, p_uuid->uuid); |
| 78 | + |
| 79 | + TEST_ASSERT_NOT_NULL(p_handle); |
| 80 | + *p_handle = SERVICE_HANDLE; |
| 81 | + |
| 82 | + return NRF_SUCCESS; |
| 83 | +} |
| 84 | + |
| 85 | +static uint32_t stub_sd_ble_gatts_characteristic_add_success( |
| 86 | + uint16_t service_handle, const ble_gatts_char_md_t *p_char_md, |
| 87 | + const ble_gatts_attr_t *p_attr_char_value, ble_gatts_char_handles_t *p_handles, int calls) |
| 88 | +{ |
| 89 | + const ble_gap_conn_sec_mode_t expected_button_read = { .lv = 1, .sm = 2 }; |
| 90 | + const ble_gap_conn_sec_mode_t expected_button_cccd = { .lv = 3, .sm = 4 }; |
| 91 | + const ble_gap_conn_sec_mode_t expected_led_read = { .lv = 5, .sm = 6 }; |
| 92 | + const ble_gap_conn_sec_mode_t expected_led_write = { .lv = 7, .sm = 8 }; |
| 93 | + ble_gap_conn_sec_mode_t perm_open; |
| 94 | + |
| 95 | + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&perm_open); |
| 96 | + |
| 97 | + TEST_ASSERT_EQUAL(SERVICE_HANDLE, service_handle); |
| 98 | + |
| 99 | + TEST_ASSERT_NOT_NULL(p_char_md); |
| 100 | + TEST_ASSERT_NOT_NULL(p_attr_char_value); |
| 101 | + TEST_ASSERT_NOT_NULL(p_attr_char_value->p_uuid); |
| 102 | + TEST_ASSERT_NOT_NULL(p_attr_char_value->p_attr_md); |
| 103 | + TEST_ASSERT_NOT_NULL(p_attr_char_value->p_value); |
| 104 | + TEST_ASSERT_NOT_NULL(p_handles); |
| 105 | + |
| 106 | + TEST_ASSERT_EQUAL(UUID_TYPE_VS, p_attr_char_value->p_uuid->type); |
| 107 | + TEST_ASSERT_EQUAL(BLE_GATTS_VLOC_STACK, p_attr_char_value->p_attr_md->vloc); |
| 108 | + TEST_ASSERT_EQUAL(sizeof(uint8_t), p_attr_char_value->init_len); |
| 109 | + TEST_ASSERT_EQUAL(sizeof(uint8_t), p_attr_char_value->max_len); |
| 110 | + TEST_ASSERT_EQUAL(0, *p_attr_char_value->p_value); |
| 111 | + |
| 112 | + if (p_attr_char_value->p_uuid->uuid == BLE_UUID_LBS_BUTTON_CHAR) { |
| 113 | + /* Button characteristic */ |
| 114 | + |
| 115 | + TEST_ASSERT_TRUE(p_char_md->char_props.read); |
| 116 | + TEST_ASSERT_TRUE(p_char_md->char_props.notify); |
| 117 | + TEST_ASSERT_FALSE(p_char_md->char_props.write); |
| 118 | + |
| 119 | + TEST_ASSERT_NOT_NULL(p_char_md->p_cccd_md); |
| 120 | + TEST_ASSERT_EQUAL(BLE_GATTS_VLOC_STACK, p_char_md->p_cccd_md->vloc); |
| 121 | + TEST_ASSERT_EQUAL(perm_open.lv, p_char_md->p_cccd_md->read_perm.lv); |
| 122 | + TEST_ASSERT_EQUAL(perm_open.sm, p_char_md->p_cccd_md->read_perm.sm); |
| 123 | + TEST_ASSERT_EQUAL(expected_button_cccd.lv, p_char_md->p_cccd_md->write_perm.lv); |
| 124 | + TEST_ASSERT_EQUAL(expected_button_cccd.sm, p_char_md->p_cccd_md->write_perm.sm); |
| 125 | + |
| 126 | + TEST_ASSERT_EQUAL(expected_button_read.lv, |
| 127 | + p_attr_char_value->p_attr_md->read_perm.lv); |
| 128 | + TEST_ASSERT_EQUAL(expected_button_read.sm, |
| 129 | + p_attr_char_value->p_attr_md->read_perm.sm); |
| 130 | + |
| 131 | + p_handles->value_handle = BUTTON_VALUE_HANDLE; |
| 132 | + p_handles->cccd_handle = BUTTON_CCCD_HANDLE; |
| 133 | + } else { |
| 134 | + /* LED characteristic */ |
| 135 | + TEST_ASSERT_EQUAL(BLE_UUID_LBS_LED_CHAR, p_attr_char_value->p_uuid->uuid); |
| 136 | + |
| 137 | + TEST_ASSERT_TRUE(p_char_md->char_props.read); |
| 138 | + TEST_ASSERT_TRUE(p_char_md->char_props.write); |
| 139 | + TEST_ASSERT_FALSE(p_char_md->char_props.notify); |
| 140 | + TEST_ASSERT_NULL(p_char_md->p_cccd_md); |
| 141 | + |
| 142 | + TEST_ASSERT_EQUAL(expected_led_read.lv, |
| 143 | + p_attr_char_value->p_attr_md->read_perm.lv); |
| 144 | + TEST_ASSERT_EQUAL(expected_led_read.sm, |
| 145 | + p_attr_char_value->p_attr_md->read_perm.sm); |
| 146 | + TEST_ASSERT_EQUAL(expected_led_write.lv, |
| 147 | + p_attr_char_value->p_attr_md->write_perm.lv); |
| 148 | + TEST_ASSERT_EQUAL(expected_led_write.sm, |
| 149 | + p_attr_char_value->p_attr_md->write_perm.sm); |
| 150 | + |
| 151 | + p_handles->value_handle = LED_VALUE_HANDLE; |
| 152 | + } |
| 153 | + |
| 154 | + return NRF_SUCCESS; |
| 155 | +} |
| 156 | + |
| 157 | +static uint32_t stub_sd_ble_gatts_hvx_check(uint16_t conn_handle, |
| 158 | + const ble_gatts_hvx_params_t *p_hvx_params, int calls) |
| 159 | +{ |
| 160 | + hvx_stub_called++; |
| 161 | + |
| 162 | + TEST_ASSERT_EQUAL(CONN_HANDLE, conn_handle); |
| 163 | + TEST_ASSERT_NOT_NULL(p_hvx_params); |
| 164 | + TEST_ASSERT_EQUAL(BLE_GATT_HVX_NOTIFICATION, p_hvx_params->type); |
| 165 | + TEST_ASSERT_EQUAL(BUTTON_VALUE_HANDLE, p_hvx_params->handle); |
| 166 | + TEST_ASSERT_NOT_NULL(p_hvx_params->p_data); |
| 167 | + TEST_ASSERT_NOT_NULL(p_hvx_params->p_len); |
| 168 | + TEST_ASSERT_EQUAL(sizeof(uint8_t), *p_hvx_params->p_len); |
| 169 | + TEST_ASSERT_EQUAL(expected_button_state, *p_hvx_params->p_data); |
| 170 | + |
| 171 | + return NRF_SUCCESS; |
| 172 | +} |
| 173 | + |
| 174 | +static void lbs_evt_handler(struct ble_lbs *lbs, const struct ble_lbs_evt *evt) |
| 175 | +{ |
| 176 | + evt_handler_called++; |
| 177 | + last_evt = *evt; |
| 178 | +} |
| 179 | + |
| 180 | +void test_ble_lbs_init(void) |
| 181 | +{ |
| 182 | + uint32_t nrf_err; |
| 183 | + struct ble_lbs_config cfg = lbs_cfg_template; |
| 184 | + |
| 185 | + cfg.evt_handler = lbs_evt_handler; |
| 186 | + |
| 187 | + /* NULL parameters return NRF_ERROR_NULL. */ |
| 188 | + nrf_err = ble_lbs_init(NULL, &cfg); |
| 189 | + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, nrf_err); |
| 190 | + TEST_ASSERT_NULL(ble_lbs.evt_handler); |
| 191 | + |
| 192 | + nrf_err = ble_lbs_init(&ble_lbs, NULL); |
| 193 | + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, nrf_err); |
| 194 | + TEST_ASSERT_NULL(ble_lbs.evt_handler); |
| 195 | + |
| 196 | + /* Errors from sd_ble_uuid_vs_add are forwarded. */ |
| 197 | + __cmock_sd_ble_uuid_vs_add_ExpectAnyArgsAndReturn(ERROR); |
| 198 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 199 | + TEST_ASSERT_EQUAL(ERROR, nrf_err); |
| 200 | + |
| 201 | + /* Errors from sd_ble_gatts_service_add are forwarded. */ |
| 202 | + __cmock_sd_ble_uuid_vs_add_ExpectAnyArgsAndReturn(NRF_SUCCESS); |
| 203 | + __cmock_sd_ble_gatts_service_add_ExpectAnyArgsAndReturn(ERROR); |
| 204 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 205 | + TEST_ASSERT_EQUAL(ERROR, nrf_err); |
| 206 | + |
| 207 | + /* Errors from the button characteristic add are forwarded. */ |
| 208 | + __cmock_sd_ble_uuid_vs_add_ExpectAnyArgsAndReturn(NRF_SUCCESS); |
| 209 | + __cmock_sd_ble_gatts_service_add_ExpectAnyArgsAndReturn(NRF_SUCCESS); |
| 210 | + __cmock_sd_ble_gatts_characteristic_add_ExpectAnyArgsAndReturn(ERROR); |
| 211 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 212 | + TEST_ASSERT_EQUAL(ERROR, nrf_err); |
| 213 | + |
| 214 | + /* Errors from the LED characteristic add are forwarded. */ |
| 215 | + __cmock_sd_ble_uuid_vs_add_ExpectAnyArgsAndReturn(NRF_SUCCESS); |
| 216 | + __cmock_sd_ble_gatts_service_add_ExpectAnyArgsAndReturn(NRF_SUCCESS); |
| 217 | + __cmock_sd_ble_gatts_characteristic_add_ExpectAnyArgsAndReturn(NRF_SUCCESS); |
| 218 | + __cmock_sd_ble_gatts_characteristic_add_ExpectAnyArgsAndReturn(ERROR); |
| 219 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 220 | + TEST_ASSERT_EQUAL(ERROR, nrf_err); |
| 221 | + |
| 222 | + /* Success path with full validation of arguments to SoftDevice calls. */ |
| 223 | + __cmock_sd_ble_uuid_vs_add_Stub(stub_sd_ble_uuid_vs_add_success); |
| 224 | + __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add_success); |
| 225 | + __cmock_sd_ble_gatts_characteristic_add_Stub(stub_sd_ble_gatts_characteristic_add_success); |
| 226 | + |
| 227 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 228 | + TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err); |
| 229 | + |
| 230 | + TEST_ASSERT_EQUAL_PTR(lbs_evt_handler, ble_lbs.evt_handler); |
| 231 | + TEST_ASSERT_EQUAL(SERVICE_HANDLE, ble_lbs.service_handle); |
| 232 | + TEST_ASSERT_EQUAL(UUID_TYPE_VS, ble_lbs.uuid_type); |
| 233 | + TEST_ASSERT_EQUAL(BUTTON_VALUE_HANDLE, ble_lbs.button_char_handles.value_handle); |
| 234 | + TEST_ASSERT_EQUAL(BUTTON_CCCD_HANDLE, ble_lbs.button_char_handles.cccd_handle); |
| 235 | + TEST_ASSERT_EQUAL(LED_VALUE_HANDLE, ble_lbs.led_char_handles.value_handle); |
| 236 | +} |
| 237 | + |
| 238 | +void test_ble_lbs_on_ble_evt(void) |
| 239 | +{ |
| 240 | + uint32_t nrf_err; |
| 241 | + ble_evt_t evt = { 0 }; |
| 242 | + struct ble_lbs_config cfg = lbs_cfg_template; |
| 243 | + |
| 244 | + cfg.evt_handler = lbs_evt_handler; |
| 245 | + |
| 246 | + __cmock_sd_ble_uuid_vs_add_Stub(stub_sd_ble_uuid_vs_add_success); |
| 247 | + __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add_success); |
| 248 | + __cmock_sd_ble_gatts_characteristic_add_Stub(stub_sd_ble_gatts_characteristic_add_success); |
| 249 | + |
| 250 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 251 | + TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err); |
| 252 | + |
| 253 | + /* Non-write event id (default branch) is silently ignored. */ |
| 254 | + evt.header.evt_id = BLE_GAP_EVT_CONNECTED; |
| 255 | + ble_lbs_on_ble_evt(&evt, &ble_lbs); |
| 256 | + TEST_ASSERT_EQUAL(0, evt_handler_called); |
| 257 | + |
| 258 | + /* Write event with mismatched handle is ignored. */ |
| 259 | + evt.header.evt_id = BLE_GATTS_EVT_WRITE; |
| 260 | + evt.evt.gatts_evt.conn_handle = CONN_HANDLE; |
| 261 | + evt.evt.gatts_evt.params.write.handle = 0xDEAD; |
| 262 | + evt.evt.gatts_evt.params.write.len = 1; |
| 263 | + evt.evt.gatts_evt.params.write.data[0] = 0xAA; |
| 264 | + ble_lbs_on_ble_evt(&evt, &ble_lbs); |
| 265 | + TEST_ASSERT_EQUAL(0, evt_handler_called); |
| 266 | + |
| 267 | + /* Write event to LED handle but with wrong length is ignored. */ |
| 268 | + evt.evt.gatts_evt.params.write.handle = LED_VALUE_HANDLE; |
| 269 | + evt.evt.gatts_evt.params.write.len = 0; |
| 270 | + ble_lbs_on_ble_evt(&evt, &ble_lbs); |
| 271 | + TEST_ASSERT_EQUAL(0, evt_handler_called); |
| 272 | + |
| 273 | + evt.evt.gatts_evt.params.write.len = 2; |
| 274 | + ble_lbs_on_ble_evt(&evt, &ble_lbs); |
| 275 | + TEST_ASSERT_EQUAL(0, evt_handler_called); |
| 276 | + |
| 277 | + /* Valid LED write delivers a BLE_LBS_EVT_LED_WRITE event to the handler. */ |
| 278 | + evt.evt.gatts_evt.params.write.len = 1; |
| 279 | + evt.evt.gatts_evt.params.write.data[0] = 0x77; |
| 280 | + ble_lbs_on_ble_evt(&evt, &ble_lbs); |
| 281 | + TEST_ASSERT_EQUAL(1, evt_handler_called); |
| 282 | + TEST_ASSERT_EQUAL(BLE_LBS_EVT_LED_WRITE, last_evt.evt_type); |
| 283 | + TEST_ASSERT_EQUAL(CONN_HANDLE, last_evt.conn_handle); |
| 284 | + TEST_ASSERT_EQUAL(0x77, last_evt.led_write.value); |
| 285 | + |
| 286 | + /* Without an event handler the write is silently dropped. */ |
| 287 | + memset(&ble_lbs, 0, sizeof(ble_lbs)); |
| 288 | + cfg.evt_handler = NULL; |
| 289 | + |
| 290 | + __cmock_sd_ble_uuid_vs_add_Stub(stub_sd_ble_uuid_vs_add_success); |
| 291 | + __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add_success); |
| 292 | + __cmock_sd_ble_gatts_characteristic_add_Stub(stub_sd_ble_gatts_characteristic_add_success); |
| 293 | + |
| 294 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 295 | + TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err); |
| 296 | + |
| 297 | + evt_handler_called = 0; |
| 298 | + evt.evt.gatts_evt.params.write.handle = LED_VALUE_HANDLE; |
| 299 | + evt.evt.gatts_evt.params.write.len = 1; |
| 300 | + ble_lbs_on_ble_evt(&evt, &ble_lbs); |
| 301 | + TEST_ASSERT_EQUAL(0, evt_handler_called); |
| 302 | +} |
| 303 | + |
| 304 | +void test_ble_lbs_on_button_change(void) |
| 305 | +{ |
| 306 | + uint32_t nrf_err; |
| 307 | + struct ble_lbs_config cfg = lbs_cfg_template; |
| 308 | + |
| 309 | + /* NULL instance returns NRF_ERROR_NULL. */ |
| 310 | + nrf_err = ble_lbs_on_button_change(NULL, CONN_HANDLE, 1); |
| 311 | + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, nrf_err); |
| 312 | + |
| 313 | + __cmock_sd_ble_uuid_vs_add_Stub(stub_sd_ble_uuid_vs_add_success); |
| 314 | + __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add_success); |
| 315 | + __cmock_sd_ble_gatts_characteristic_add_Stub(stub_sd_ble_gatts_characteristic_add_success); |
| 316 | + |
| 317 | + nrf_err = ble_lbs_init(&ble_lbs, &cfg); |
| 318 | + TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err); |
| 319 | + |
| 320 | + /* Errors from sd_ble_gatts_hvx are forwarded. */ |
| 321 | + __cmock_sd_ble_gatts_hvx_ExpectAnyArgsAndReturn(ERROR); |
| 322 | + nrf_err = ble_lbs_on_button_change(&ble_lbs, CONN_HANDLE, 1); |
| 323 | + TEST_ASSERT_EQUAL(ERROR, nrf_err); |
| 324 | + |
| 325 | + /* Successful notification: validates parameters via the stub. */ |
| 326 | + expected_button_state = 0xCD; |
| 327 | + __cmock_sd_ble_gatts_hvx_Stub(stub_sd_ble_gatts_hvx_check); |
| 328 | + |
| 329 | + nrf_err = ble_lbs_on_button_change(&ble_lbs, CONN_HANDLE, expected_button_state); |
| 330 | + TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err); |
| 331 | + TEST_ASSERT_EQUAL(1, hvx_stub_called); |
| 332 | +} |
| 333 | + |
| 334 | +void setUp(void) |
| 335 | +{ |
| 336 | + memset(&ble_lbs, 0, sizeof(ble_lbs)); |
| 337 | + memset(&last_evt, 0, sizeof(last_evt)); |
| 338 | + evt_handler_called = 0; |
| 339 | + hvx_stub_called = 0; |
| 340 | + expected_button_state = 0; |
| 341 | +} |
| 342 | + |
| 343 | +extern int unity_main(void); |
| 344 | + |
| 345 | +int main(void) |
| 346 | +{ |
| 347 | + return unity_main(); |
| 348 | +} |
0 commit comments