1 2 // ***************************************************************************** 3 // 4 // test rfcomm query tests 5 // 6 // ***************************************************************************** 7 8 9 #include <stdint.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include "CppUTest/TestHarness.h" 15 #include "CppUTest/CommandLineTestRunner.h" 16 17 #include "hci.h" 18 #include "ble/att_db.h" 19 #include "ble/att_db_util.h" 20 #include "ble/att_server.h" 21 #include "btstack_util.h" 22 #include "bluetooth.h" 23 #include "btstack_tlv.h" 24 #include "mock_btstack_tlv.h" 25 26 #include "bluetooth_gatt.h" 27 28 static uint8_t battery_level = 100; 29 static const uint8_t uuid128_with_bluetooth_base[] = { 0x00, 0x00, 0xBB, 0xBB, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 30 static const uint8_t uuid128_no_bluetooth_base[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAA, 0xAA, 0x00, 0x00 }; 31 32 extern "C" void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status); 33 extern "C" void mock_call_att_server_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 34 extern "C" void hci_setup_le_connection(uint16_t con_handle); 35 extern "C" void mock_call_att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 36 extern "C" void mock_l2cap_set_max_mtu(uint16_t mtu); 37 extern "C" void hci_setup_classic_connection(uint16_t con_handle); 38 extern "C" void set_cmac_ready(int ready); 39 40 static uint8_t att_request[255]; 41 static uint16_t att_write_request(uint16_t request_type, uint16_t attribute_handle, uint16_t value_length, const uint8_t * value){ 42 att_request[0] = request_type; 43 little_endian_store_16(att_request, 1, attribute_handle); 44 (void)memcpy(&att_request[3], value, value_length); 45 return 3 + value_length; 46 } 47 48 static uint16_t att_read_request(uint16_t request_type, uint16_t attribute_handle){ 49 att_request[0] = request_type; 50 little_endian_store_16(att_request, 1, attribute_handle); 51 return 3; 52 } 53 54 static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 55 UNUSED(connection_handle); 56 UNUSED(att_handle); 57 UNUSED(offset); 58 UNUSED(buffer); 59 UNUSED(buffer_size); 60 61 return 0; 62 } 63 64 static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ 65 UNUSED(connection_handle); 66 UNUSED(att_handle); 67 UNUSED(transaction_mode); 68 UNUSED(offset); 69 UNUSED(buffer); 70 UNUSED(buffer_size); 71 72 return 0; 73 } 74 75 static void att_client_indication_callback(void * context){ 76 } 77 static void att_client_notification_callback(void * context){ 78 } 79 static void att_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 80 } 81 82 83 TEST_GROUP(ATT_SERVER){ 84 uint16_t att_con_handle; 85 mock_btstack_tlv_t tlv_context; 86 const btstack_tlv_t * tlv_impl; 87 btstack_context_callback_registration_t indication_callback; 88 btstack_context_callback_registration_t notification_callback; 89 90 void setup(void){ 91 att_con_handle = 0x01; 92 93 hci_setup_le_connection(att_con_handle); 94 95 tlv_impl = mock_btstack_tlv_init_instance(&tlv_context); 96 btstack_tlv_set_instance(tlv_impl, &tlv_context); 97 98 l2cap_can_send_fixed_channel_packet_now_set_status(1); 99 indication_callback.callback = &att_client_indication_callback; 100 notification_callback.callback = &att_client_notification_callback; 101 102 // init att db util and add a service and characteristic 103 att_db_util_init(); 104 // 0x180F 105 att_db_util_add_service_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE); 106 // 0x2A19 107 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL, ATT_PROPERTY_WRITE | ATT_PROPERTY_READ | ATT_PROPERTY_INDICATE, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 108 // 0x2A1B 109 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL_STATE, ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 110 // 0x2A1A 111 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_POWER_STATE, ATT_PROPERTY_READ | ATT_PROPERTY_NOTIFY, ATT_SECURITY_AUTHENTICATED, ATT_SECURITY_AUTHENTICATED, &battery_level, 1); 112 // 0x2A49 113 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BLOOD_PRESSURE_FEATURE, ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_READ | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 114 // 0x2A35 115 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BLOOD_PRESSURE_MEASUREMENT, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_AUTHENTICATED, ATT_SECURITY_AUTHENTICATED, &battery_level, 1); 116 117 118 att_db_util_add_characteristic_uuid128(uuid128_no_bluetooth_base, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 119 // 0x2A38btstack_tlv_set_instance 120 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 121 122 123 att_db_util_add_characteristic_uuid128(uuid128_with_bluetooth_base, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 124 // 0x2AAB 125 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME, ATT_PROPERTY_WRITE_WITHOUT_RESPONSE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 126 // 0x2A5C 127 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_CSC_FEATURE, ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1); 128 // setup ATT server 129 att_server_init(att_db_util_get_address(), att_read_callback, att_write_callback); 130 } 131 132 void teardown(void) { 133 mock_btstack_tlv_deinit(&tlv_context); 134 att_server_deinit(); 135 hci_deinit(); 136 } 137 }; 138 139 TEST(ATT_SERVER, gatt_server_get_value_handle_for_characteristic_with_uuid16){ 140 // att_dump_attributes(); 141 uint16_t value_handle; 142 143 // start handle > value handle 144 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0xf000, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 145 CHECK_EQUAL(0, value_handle); 146 147 // end handle < value handle 148 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0x02, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 149 CHECK_EQUAL(0, value_handle); 150 151 // search value handle for unknown UUID 152 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xffff); 153 CHECK_EQUAL(0, value_handle); 154 155 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0); 156 CHECK_EQUAL(0, value_handle); 157 158 // search value handle after one with uuid128_no_bluetooth_base 159 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION); 160 CHECK_EQUAL(0x0014, value_handle); 161 162 // search value handle after one with uuid128_with_bluetooth_base 163 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME); 164 CHECK_EQUAL(0x001a, value_handle); 165 166 // search value handle registered with bluetooth_base 167 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xbbbb); 168 CHECK_EQUAL(0x0017, value_handle); 169 170 // correct read 171 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 172 CHECK_EQUAL(0x03, value_handle); 173 } 174 175 176 TEST(ATT_SERVER, att_server_indicate){ 177 static uint8_t value[] = {0x55}; 178 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 179 uint8_t status; 180 181 // invalid connection handle 182 status = att_server_indicate(0x50, value_handle, &value[0], 0); 183 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status); 184 185 // L2CAP cannot send 186 l2cap_can_send_fixed_channel_packet_now_set_status(0); 187 status = att_server_indicate(att_con_handle, value_handle, &value[0], 0); 188 CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status); 189 l2cap_can_send_fixed_channel_packet_now_set_status(1); 190 191 // correct command 192 status = att_server_indicate(att_con_handle, value_handle, &value[0], 0); 193 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 194 195 // already in progress 196 status = att_server_indicate(att_con_handle, value_handle, &value[0], 0); 197 CHECK_EQUAL(ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS, status); 198 } 199 200 TEST(ATT_SERVER, att_server_notify){ 201 static uint8_t value[] = {0x55}; 202 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 203 uint8_t status; 204 205 // invalid connection handle 206 status = att_server_notify(0x50, value_handle, &value[0], 0); 207 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status); 208 209 // L2CAP cannot send 210 l2cap_can_send_fixed_channel_packet_now_set_status(0); 211 status = att_server_notify(att_con_handle, value_handle, &value[0], 0); 212 CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status); 213 l2cap_can_send_fixed_channel_packet_now_set_status(1); 214 215 // correct command 216 status = att_server_notify(att_con_handle, value_handle, &value[0], 0); 217 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 218 } 219 220 TEST(ATT_SERVER, att_server_get_mtu){ 221 // invalid connection handle 222 uint8_t mtu = att_server_get_mtu(0x50); 223 CHECK_EQUAL(0, mtu); 224 225 mtu = att_server_get_mtu(att_con_handle); 226 CHECK_EQUAL(23, mtu); 227 } 228 229 TEST(ATT_SERVER, att_server_request_can_send_now_event){ 230 att_server_request_can_send_now_event(att_con_handle); 231 } 232 233 TEST(ATT_SERVER, att_server_can_send_packet_now){ 234 int status = att_server_can_send_packet_now(att_con_handle); 235 CHECK_EQUAL(1, status); 236 237 status = att_server_can_send_packet_now(0x50); 238 CHECK_EQUAL(0, status); 239 } 240 241 TEST(ATT_SERVER, att_server_request_to_send_indication){ 242 int status = att_server_request_to_send_indication(&indication_callback, 0x55); 243 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status); 244 245 l2cap_can_send_fixed_channel_packet_now_set_status(0); 246 247 status = att_server_request_to_send_indication(&indication_callback, att_con_handle); 248 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 249 250 status = att_server_request_to_send_indication(&indication_callback, att_con_handle); 251 CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status); 252 } 253 254 TEST(ATT_SERVER, att_server_request_to_send_notification){ 255 int status = att_server_request_to_send_notification(¬ification_callback, 0x55); 256 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status); 257 258 l2cap_can_send_fixed_channel_packet_now_set_status(0); 259 260 status = att_server_request_to_send_notification(¬ification_callback, att_con_handle); 261 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 262 263 status = att_server_request_to_send_notification(¬ification_callback, att_con_handle); 264 CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status); 265 } 266 267 TEST(ATT_SERVER, att_server_register_can_send_now_callback){ 268 int status = att_server_register_can_send_now_callback(¬ification_callback, 0x55); 269 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status); 270 271 att_server_register_packet_handler(&att_event_packet_handler); 272 } 273 274 TEST(ATT_SERVER, att_packet_handler_ATT_DATA_PACKET){ 275 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 276 uint8_t buffer[] = {1, 0}; 277 278 uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer); 279 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len); 280 } 281 282 TEST(ATT_SERVER, att_packet_handler_ATT_DATA_PACKET1){ 283 uint16_t value_handle; 284 uint16_t att_request_len; 285 286 value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 287 att_request_len = att_read_request(ATT_READ_REQUEST, value_handle); 288 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len); 289 } 290 291 TEST(ATT_SERVER, att_packet_handler_invalid_opcode){ 292 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_POWER_STATE); 293 uint8_t buffer[] = {1, 0}; 294 295 uint16_t att_request_len = att_write_request(0xFF, value_handle, sizeof(buffer), buffer); 296 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len); 297 } 298 299 TEST(ATT_SERVER, att_packet_handler_ATT_HANDLE_VALUE_CONFIRMATION){ 300 hci_setup_le_connection(att_con_handle); 301 static uint8_t value[] = {0x55}; 302 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 303 l2cap_can_send_fixed_channel_packet_now_set_status(1); 304 // correct command 305 uint8_t status = att_server_indicate(att_con_handle, value_handle, &value[0], 0); 306 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 307 308 static uint8_t buffer[0]; 309 uint16_t att_request_len = att_write_request(ATT_HANDLE_VALUE_CONFIRMATION, value_handle, 0, buffer); 310 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len); 311 } 312 313 TEST(ATT_SERVER, att_packet_handler_ATT_WRITE_COMMAND){ 314 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 315 static uint8_t buffer[] = {0x55}; 316 uint16_t att_request_len = att_write_request(ATT_WRITE_COMMAND, value_handle, sizeof(buffer), buffer); 317 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len); 318 } 319 320 TEST(ATT_SERVER, att_packet_handler_ATT_WRITE_COMMAND_wrong_buffer_size){ 321 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 322 static uint8_t buffer[] = {0x55}; 323 att_write_request(ATT_SIGNED_WRITE_COMMAND, value_handle, sizeof(buffer), buffer); 324 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], ATT_REQUEST_BUFFER_SIZE + 100); 325 } 326 327 TEST(ATT_SERVER, att_packet_handler_ATT_WRITE_COMMAND_signed_write_confirmation){ 328 hci_setup_le_connection(att_con_handle); 329 330 uint8_t signed_write_value[] = {0x12}; 331 uint8_t hash[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 332 uint16_t signed_write_characteristic_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_CSC_FEATURE); 333 334 uint8_t att_request[20]; 335 int value_length = sizeof(signed_write_value); 336 att_request[0] = ATT_SIGNED_WRITE_COMMAND; 337 little_endian_store_16(att_request, 1, signed_write_characteristic_handle); 338 memcpy(&att_request[3], signed_write_value, value_length); 339 little_endian_store_32(att_request, 3 + value_length, 0); 340 reverse_64(hash, &att_request[3 + value_length + 4]); 341 342 // wrong size 343 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u); 344 345 set_cmac_ready(0); 346 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u + 3u); 347 348 set_cmac_ready(1); 349 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u + 3u); 350 351 // set ir_lookup_active = 1 352 uint8_t buffer[20]; 353 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_STARTED; 354 buffer[1] = 9; 355 little_endian_store_16(buffer, 2, att_con_handle); 356 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], 11); 357 358 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u + 3u); 359 360 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED; 361 buffer[1] = 18; // H1B1B2 362 little_endian_store_16(buffer, 18, 0); 363 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], 11); 364 } 365 366 367 TEST(ATT_SERVER, att_packet_handler_ATT_UNKNOWN_PACKET){ 368 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 369 uint8_t buffer[] = {1, 0}; 370 371 uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer); 372 mock_call_att_server_packet_handler(0xFF, att_con_handle, &att_request[0], att_request_len); 373 } 374 375 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_L2CAP_EVENT_CAN_SEND_NOW){ 376 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 377 // local cid 378 uint8_t buffer[] = {1, 0}; 379 380 uint16_t att_request_len = att_write_request(L2CAP_EVENT_CAN_SEND_NOW, value_handle, sizeof(buffer), buffer); 381 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len); 382 } 383 384 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_MTU_EXCHANGE_COMPLETE){ 385 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 386 uint8_t buffer[4]; 387 388 // invalid con handle 389 memset(buffer, 0, sizeof(buffer)); 390 little_endian_store_16(buffer, 2, 256); 391 392 uint16_t att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer); 393 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, 0, &att_request[0], att_request_len); 394 395 little_endian_store_16(buffer, 0, att_con_handle); 396 att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer); 397 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len); 398 } 399 400 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_UNKNOWN){ 401 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 402 uint8_t buffer[] = {1, 0}; 403 404 // unknown subevent 405 uint16_t att_request_len = att_write_request(0x7bu, value_handle, sizeof(buffer), buffer); 406 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len); 407 } 408 409 TEST(ATT_SERVER, connection_complete_event){ 410 uint8_t buffer[21]; 411 buffer[0] = HCI_EVENT_LE_META; 412 buffer[1] = 19; 413 buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 414 415 // call with wrong con_handle 416 little_endian_store_16(buffer,4,HCI_CON_HANDLE_INVALID); 417 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 418 419 // call with correct con_handle 420 little_endian_store_16(buffer,4,att_con_handle); 421 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 422 423 // use max MTU > ATT_REQUEST_BUFFER_SIZE 424 mock_l2cap_set_max_mtu(ATT_REQUEST_BUFFER_SIZE + 10); 425 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 426 427 // wrong subevent 428 buffer[2] = 0xFF; 429 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 430 } 431 432 TEST(ATT_SERVER, unknown_packet_type){ 433 uint8_t buffer[21]; 434 buffer[0] = HCI_EVENT_LE_META; 435 buffer[1] = 1; 436 buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 437 438 mock_call_att_packet_handler(0xFF, 0, &buffer[0], sizeof(buffer)); 439 } 440 441 442 TEST(ATT_SERVER, connection_disconnect_complete_event) { 443 uint8_t buffer[6]; 444 buffer[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 445 buffer[1] = 19; 446 buffer[2] = 0; 447 448 // call with wrong con_handle 449 hci_setup_le_connection(att_con_handle); 450 little_endian_store_16(buffer, 3, HCI_CON_HANDLE_INVALID); 451 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 452 453 // call with correct con_handle, classic connection 454 hci_setup_classic_connection(att_con_handle); 455 little_endian_store_16(buffer, 3, att_con_handle); 456 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 457 458 // call with correct con_handle, le connection 459 hci_setup_le_connection(att_con_handle); 460 little_endian_store_16(buffer, 3, att_con_handle); 461 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 462 463 464 hci_setup_le_connection(att_con_handle); 465 static uint8_t value[] = {0x55}; 466 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 467 l2cap_can_send_fixed_channel_packet_now_set_status(1); 468 // correct command 469 uint8_t status = att_server_indicate(att_con_handle, value_handle, &value[0], 0); 470 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 471 472 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 473 } 474 475 static void test_hci_event_encryption_events(hci_con_handle_t con_handle, uint8_t con_handle_index, uint8_t * buffer, uint16_t buffer_size){ 476 // call with wrong con_handle 477 hci_setup_le_connection(con_handle); 478 little_endian_store_16(buffer, con_handle_index, HCI_CON_HANDLE_INVALID); 479 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size); 480 481 // call with correct con_handle, classic connection 482 hci_setup_classic_connection(con_handle); 483 little_endian_store_16(buffer, con_handle_index, con_handle); 484 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size); 485 486 // call with correct con_handle, le connection 487 hci_setup_le_connection(con_handle); 488 little_endian_store_16(buffer, con_handle_index, con_handle); 489 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size); 490 } 491 492 TEST(ATT_SERVER, hci_event_encryption_key_refresh_complete_event) { 493 uint8_t buffer[5]; 494 buffer[0] = HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE; 495 buffer[1] = 3; 496 buffer[2] = 0; 497 // con_handle (2) 498 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 499 } 500 501 TEST(ATT_SERVER, hci_event_encryption_change_v2_event) { 502 uint8_t buffer[7]; 503 buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE_V2; 504 buffer[1] = 5; 505 buffer[2] = 0; 506 // con_handle (2) 507 // encryption_key_size 508 buffer[6] = 1; 509 // encryption_enabled (1) 510 buffer[5] = 0; 511 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 512 513 // encryption_enabled (1) 514 buffer[5] = 1; 515 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 516 } 517 518 TEST(ATT_SERVER, hci_event_encryption_change_event) { 519 uint8_t buffer[6]; 520 buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE; 521 buffer[1] = 4; 522 buffer[2] = 0; 523 // con_handle (2)att_packet_handler_ATT_UNKNOWN_PACKET 524 525 // encryption_enabled (1) 526 buffer[5] = 0; 527 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 528 529 // encryption_enabled (1) 530 buffer[5] = 1; 531 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 532 } 533 534 TEST(ATT_SERVER, sm_event_identity_resolving_started_event) { 535 uint8_t buffer[11]; 536 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_STARTED; 537 buffer[1] = 9; 538 539 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 540 } 541 542 TEST(ATT_SERVER, sm_event_identity_resolving_succeeded_event) { 543 uint8_t buffer[20]; 544 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED; 545 buffer[1] = 18; // H1B1B2 546 little_endian_store_16(buffer, 18, 0); 547 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 548 } 549 550 TEST(ATT_SERVER, sm_event_identity_resolving_failed_event) { 551 uint8_t buffer[11]; 552 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_FAILED; 553 buffer[1] = 9; 554 555 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 556 } 557 558 // 559 560 TEST(ATT_SERVER, sm_event_just_works_request_event) { 561 uint8_t buffer[11]; 562 buffer[0] = SM_EVENT_JUST_WORKS_REQUEST; 563 buffer[1] = 9; 564 565 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 566 } 567 568 TEST(ATT_SERVER, sm_event_passkey_display_number_event) { 569 uint8_t buffer[11]; 570 buffer[0] = SM_EVENT_PASSKEY_DISPLAY_NUMBER; 571 buffer[1] = 9; 572 573 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 574 } 575 576 TEST(ATT_SERVER, sm_event_passkey_input_number_event) { 577 uint8_t buffer[11]; 578 buffer[0] = SM_EVENT_PASSKEY_INPUT_NUMBER; 579 buffer[1] = 9; 580 581 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 582 } 583 584 TEST(ATT_SERVER, sm_event_numeric_comparison_request_event) { 585 uint8_t buffer[11]; 586 buffer[0] = SM_EVENT_NUMERIC_COMPARISON_REQUEST; 587 buffer[1] = 9; 588 589 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 590 } 591 592 TEST(ATT_SERVER, sm_event_identity_created_event) { 593 uint8_t buffer[20]; 594 buffer[0] = SM_EVENT_IDENTITY_CREATED; 595 buffer[1] = 9; 596 597 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 598 } 599 600 TEST(ATT_SERVER, sm_event_pairing_complete_event) { 601 uint8_t buffer[11]; 602 buffer[0] = SM_EVENT_PAIRING_COMPLETE; 603 buffer[1] = 9; 604 605 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 606 } 607 608 TEST(ATT_SERVER, sm_event_authorization_result_event) { 609 uint8_t buffer[12]; 610 buffer[0] = SM_EVENT_AUTHORIZATION_RESULT; 611 buffer[1] = 9; 612 613 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 614 } 615 616 TEST(ATT_SERVER, unknown_event) { 617 uint8_t buffer[11]; 618 buffer[0] = 0xFF; 619 buffer[1] = 9; 620 621 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 622 } 623 624 TEST(ATT_SERVER, att_server_register_service_handler) { 625 att_service_handler_t test_service; 626 test_service.read_callback = att_read_callback; 627 test_service.write_callback = att_write_callback; 628 629 uint16_t start_handle = 0; 630 uint16_t end_handle = 0xffff; 631 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle); 632 CHECK_EQUAL(service_found, 1); 633 634 test_service.start_handle = start_handle; 635 test_service.end_handle = end_handle; 636 att_server_register_service_handler(&test_service); 637 638 test_service.start_handle = start_handle; 639 test_service.end_handle = 0; 640 att_server_register_service_handler(&test_service); 641 642 test_service.start_handle = 0; 643 test_service.end_handle = end_handle; 644 att_server_register_service_handler(&test_service); 645 646 test_service.start_handle = 0; 647 test_service.end_handle = 0; 648 att_server_register_service_handler(&test_service); 649 } 650 651 int main (int argc, const char * argv[]){ 652 return CommandLineTestRunner::RunAllTests(argc, argv); 653 } 654