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 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], 11); 363 } 364 365 366 TEST(ATT_SERVER, att_packet_handler_ATT_UNKNOWN_PACKET){ 367 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 368 uint8_t buffer[] = {1, 0}; 369 370 uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer); 371 mock_call_att_server_packet_handler(0xFF, att_con_handle, &att_request[0], att_request_len); 372 } 373 374 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_L2CAP_EVENT_CAN_SEND_NOW){ 375 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 376 // local cid 377 uint8_t buffer[] = {1, 0}; 378 379 uint16_t att_request_len = att_write_request(L2CAP_EVENT_CAN_SEND_NOW, value_handle, sizeof(buffer), buffer); 380 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len); 381 } 382 383 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_MTU_EXCHANGE_COMPLETE){ 384 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 385 uint8_t buffer[4]; 386 387 // invalid con handle 388 memset(buffer, 0, sizeof(buffer)); 389 little_endian_store_16(buffer, 2, 256); 390 391 uint16_t att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer); 392 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, 0, &att_request[0], att_request_len); 393 394 little_endian_store_16(buffer, 0, att_con_handle); 395 att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer); 396 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len); 397 } 398 399 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_UNKNOWN){ 400 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 401 uint8_t buffer[] = {1, 0}; 402 403 // unknown subevent 404 uint16_t att_request_len = att_write_request(0x7bu, value_handle, sizeof(buffer), buffer); 405 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len); 406 } 407 408 TEST(ATT_SERVER, connection_complete_event){ 409 uint8_t buffer[21]; 410 buffer[0] = HCI_EVENT_LE_META; 411 buffer[1] = 19; 412 buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 413 414 // call with wrong con_handle 415 little_endian_store_16(buffer,4,HCI_CON_HANDLE_INVALID); 416 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 417 418 // call with correct con_handle 419 little_endian_store_16(buffer,4,att_con_handle); 420 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 421 422 // use max MTU > ATT_REQUEST_BUFFER_SIZE 423 mock_l2cap_set_max_mtu(ATT_REQUEST_BUFFER_SIZE + 10); 424 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 425 426 // wrong subevent 427 buffer[2] = 0xFF; 428 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 429 } 430 431 TEST(ATT_SERVER, unknown_packet_type){ 432 uint8_t buffer[21]; 433 buffer[0] = HCI_EVENT_LE_META; 434 buffer[1] = 1; 435 buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 436 437 mock_call_att_packet_handler(0xFF, 0, &buffer[0], sizeof(buffer)); 438 } 439 440 441 TEST(ATT_SERVER, connection_disconnect_complete_event) { 442 uint8_t buffer[6]; 443 buffer[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 444 buffer[1] = 19; 445 buffer[2] = 0; 446 447 // call with wrong con_handle 448 hci_setup_le_connection(att_con_handle); 449 little_endian_store_16(buffer, 3, HCI_CON_HANDLE_INVALID); 450 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 451 452 // call with correct con_handle, classic connection 453 hci_setup_classic_connection(att_con_handle); 454 little_endian_store_16(buffer, 3, att_con_handle); 455 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 456 457 // call with correct con_handle, le connection 458 hci_setup_le_connection(att_con_handle); 459 little_endian_store_16(buffer, 3, att_con_handle); 460 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 461 462 463 hci_setup_le_connection(att_con_handle); 464 static uint8_t value[] = {0x55}; 465 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 466 l2cap_can_send_fixed_channel_packet_now_set_status(1); 467 // correct command 468 uint8_t status = att_server_indicate(att_con_handle, value_handle, &value[0], 0); 469 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 470 471 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer)); 472 } 473 474 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){ 475 // call with wrong con_handle 476 hci_setup_le_connection(con_handle); 477 little_endian_store_16(buffer, con_handle_index, HCI_CON_HANDLE_INVALID); 478 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size); 479 480 // call with correct con_handle, classic connection 481 hci_setup_classic_connection(con_handle); 482 little_endian_store_16(buffer, con_handle_index, con_handle); 483 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size); 484 485 // call with correct con_handle, le connection 486 hci_setup_le_connection(con_handle); 487 little_endian_store_16(buffer, con_handle_index, con_handle); 488 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size); 489 } 490 491 TEST(ATT_SERVER, hci_event_encryption_key_refresh_complete_event) { 492 uint8_t buffer[5]; 493 buffer[0] = HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE; 494 buffer[1] = 3; 495 buffer[2] = 0; 496 // con_handle (2) 497 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 498 } 499 500 TEST(ATT_SERVER, hci_event_encryption_change_v2_event) { 501 uint8_t buffer[7]; 502 buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE_V2; 503 buffer[1] = 5; 504 buffer[2] = 0; 505 // con_handle (2) 506 // encryption_key_size 507 buffer[6] = 1; 508 // encryption_enabled (1) 509 buffer[5] = 0; 510 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 511 512 // encryption_enabled (1) 513 buffer[5] = 1; 514 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 515 } 516 517 TEST(ATT_SERVER, hci_event_encryption_change_event) { 518 uint8_t buffer[6]; 519 buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE; 520 buffer[1] = 4; 521 buffer[2] = 0; 522 // con_handle (2)att_packet_handler_ATT_UNKNOWN_PACKET 523 524 // encryption_enabled (1) 525 buffer[5] = 0; 526 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 527 528 // encryption_enabled (1) 529 buffer[5] = 1; 530 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer)); 531 } 532 533 TEST(ATT_SERVER, sm_event_identity_resolving_started_event) { 534 uint8_t buffer[11]; 535 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_STARTED; 536 buffer[1] = 9; 537 538 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 539 } 540 541 TEST(ATT_SERVER, sm_event_identity_resolving_succeeded_event) { 542 uint8_t buffer[20]; 543 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED; 544 buffer[1] = 18; // H1B1B2 545 546 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 547 } 548 549 TEST(ATT_SERVER, sm_event_identity_resolving_failed_event) { 550 uint8_t buffer[11]; 551 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_FAILED; 552 buffer[1] = 9; 553 554 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 555 } 556 557 // 558 559 TEST(ATT_SERVER, sm_event_just_works_request_event) { 560 uint8_t buffer[11]; 561 buffer[0] = SM_EVENT_JUST_WORKS_REQUEST; 562 buffer[1] = 9; 563 564 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 565 } 566 567 TEST(ATT_SERVER, sm_event_passkey_display_number_event) { 568 uint8_t buffer[11]; 569 buffer[0] = SM_EVENT_PASSKEY_DISPLAY_NUMBER; 570 buffer[1] = 9; 571 572 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 573 } 574 575 TEST(ATT_SERVER, sm_event_passkey_input_number_event) { 576 uint8_t buffer[11]; 577 buffer[0] = SM_EVENT_PASSKEY_INPUT_NUMBER; 578 buffer[1] = 9; 579 580 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 581 } 582 583 TEST(ATT_SERVER, sm_event_numeric_comparison_request_event) { 584 uint8_t buffer[11]; 585 buffer[0] = SM_EVENT_NUMERIC_COMPARISON_REQUEST; 586 buffer[1] = 9; 587 588 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 589 } 590 591 TEST(ATT_SERVER, sm_event_identity_created_event) { 592 uint8_t buffer[20]; 593 buffer[0] = SM_EVENT_IDENTITY_CREATED; 594 buffer[1] = 9; 595 596 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 597 } 598 599 TEST(ATT_SERVER, sm_event_pairing_complete_event) { 600 uint8_t buffer[11]; 601 buffer[0] = SM_EVENT_PAIRING_COMPLETE; 602 buffer[1] = 9; 603 604 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 605 } 606 607 TEST(ATT_SERVER, sm_event_authorization_result_event) { 608 uint8_t buffer[12]; 609 buffer[0] = SM_EVENT_AUTHORIZATION_RESULT; 610 buffer[1] = 9; 611 612 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 613 } 614 615 TEST(ATT_SERVER, unknown_event) { 616 uint8_t buffer[11]; 617 buffer[0] = 0xFF; 618 buffer[1] = 9; 619 620 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer)); 621 } 622 623 TEST(ATT_SERVER, att_server_register_service_handler) { 624 att_service_handler_t test_service; 625 test_service.read_callback = att_read_callback; 626 test_service.write_callback = att_write_callback; 627 628 uint16_t start_handle = 0; 629 uint16_t end_handle = 0xffff; 630 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle); 631 CHECK_EQUAL(service_found, 1); 632 633 test_service.start_handle = start_handle; 634 test_service.end_handle = end_handle; 635 att_server_register_service_handler(&test_service); 636 637 test_service.start_handle = start_handle; 638 test_service.end_handle = 0; 639 att_server_register_service_handler(&test_service); 640 641 test_service.start_handle = 0; 642 test_service.end_handle = end_handle; 643 att_server_register_service_handler(&test_service); 644 645 test_service.start_handle = 0; 646 test_service.end_handle = 0; 647 att_server_register_service_handler(&test_service); 648 } 649 650 int main (int argc, const char * argv[]){ 651 return CommandLineTestRunner::RunAllTests(argc, argv); 652 } 653