xref: /btstack/test/gatt_server/gatt_server_test.cpp (revision a0a194464a0b572abd9a62eb24b311648af77ac5)
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 
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_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
49     UNUSED(connection_handle);
50     UNUSED(att_handle);
51     UNUSED(offset);
52     UNUSED(buffer);
53     UNUSED(buffer_size);
54 
55     return 0;
56 }
57 
58 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){
59     UNUSED(connection_handle);
60     UNUSED(att_handle);
61     UNUSED(transaction_mode);
62     UNUSED(offset);
63     UNUSED(buffer);
64     UNUSED(buffer_size);
65 
66     return 0;
67 }
68 
69 static void att_client_indication_callback(void * context){
70 }
71 static void att_client_notification_callback(void * context){
72 }
73 static void att_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
74 }
75 
76 
77 TEST_GROUP(ATT_SERVER){
78     uint16_t att_con_handle;
79     mock_btstack_tlv_t tlv_context;
80     const btstack_tlv_t * tlv_impl;
81     btstack_context_callback_registration_t indication_callback;
82     btstack_context_callback_registration_t notification_callback;
83 
84     void setup(void){
85         att_con_handle = 0x01;
86 
87         hci_setup_le_connection(att_con_handle);
88 
89         tlv_impl = mock_btstack_tlv_init_instance(&tlv_context);
90         btstack_tlv_set_instance(tlv_impl, &tlv_context);
91 
92         l2cap_can_send_fixed_channel_packet_now_set_status(1);
93         indication_callback.callback = &att_client_indication_callback;
94         notification_callback.callback = &att_client_notification_callback;
95 
96         // init att db util and add a service and characteristic
97         att_db_util_init();
98         // 0x180F
99         att_db_util_add_service_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE);
100         // 0x2A19
101         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);
102         // 0x2A1B
103         att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL_STATE, ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
104         // 0x2A1A
105         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);
106         // 0x2A49
107         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);
108         // 0x2A35
109         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);
110 
111 
112         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);
113         // 0x2A38btstack_tlv_set_instance
114         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);
115 
116 
117         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);
118         // 0x2AAB
119         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);
120         // 0x2A5C
121         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);
122         // setup ATT server
123         att_server_init(att_db_util_get_address(), att_read_callback, att_write_callback);
124     }
125 
126     void teardown(void) {
127         mock_btstack_tlv_deinit(&tlv_context);
128         att_server_deinit();
129         hci_deinit();
130     }
131 };
132 
133 
134 TEST(ATT_SERVER, gatt_server_get_value_handle_for_characteristic_with_uuid16){
135     // att_dump_attributes();
136     uint16_t value_handle;
137 
138     // start handle > value handle
139     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0xf000, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
140     CHECK_EQUAL(0, value_handle);
141 
142     // end handle < value handle
143     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0x02, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
144     CHECK_EQUAL(0, value_handle);
145 
146     // search value handle for unknown UUID
147     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xffff);
148     CHECK_EQUAL(0, value_handle);
149 
150     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0);
151     CHECK_EQUAL(0, value_handle);
152 
153     // search value handle after one with uuid128_no_bluetooth_base
154     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION);
155     CHECK_EQUAL(0x0014, value_handle);
156 
157     // search value handle after one with uuid128_with_bluetooth_base
158     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME);
159     CHECK_EQUAL(0x001a, value_handle);
160 
161     // search value handle registered with bluetooth_base
162     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xbbbb);
163     CHECK_EQUAL(0x0017, value_handle);
164 
165     // correct read
166     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
167     CHECK_EQUAL(0x03, value_handle);
168 }
169 
170 
171 TEST(ATT_SERVER, att_server_indicate){
172     static uint8_t value[] = {0x55};
173     uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
174     uint8_t status;
175 
176     // invalid connection handle
177     status = att_server_indicate(0x50, value_handle, &value[0], 0);
178     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
179 
180     // L2CAP cannot send
181     l2cap_can_send_fixed_channel_packet_now_set_status(0);
182     status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
183     CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status);
184     l2cap_can_send_fixed_channel_packet_now_set_status(1);
185 
186     // correct command
187     status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
188     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
189 
190     // already in progress
191     status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
192     CHECK_EQUAL(ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS, status);
193 }
194 
195 TEST(ATT_SERVER, att_server_notify){
196     static uint8_t value[] = {0x55};
197     uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
198     uint8_t status;
199 
200     // invalid connection handle
201     status = att_server_notify(0x50, value_handle, &value[0], 0);
202     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
203 
204     // L2CAP cannot send
205     l2cap_can_send_fixed_channel_packet_now_set_status(0);
206     status = att_server_notify(att_con_handle, value_handle, &value[0], 0);
207     CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status);
208     l2cap_can_send_fixed_channel_packet_now_set_status(1);
209 
210     // correct command
211     status = att_server_notify(att_con_handle, value_handle, &value[0], 0);
212     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
213 }
214 
215 TEST(ATT_SERVER, att_server_get_mtu){
216     // invalid connection handle
217     uint8_t mtu = att_server_get_mtu(0x50);
218     CHECK_EQUAL(0, mtu);
219 
220     mtu = att_server_get_mtu(att_con_handle);
221     CHECK_EQUAL(23, mtu);
222 }
223 
224 TEST(ATT_SERVER, att_server_request_can_send_now_event){
225     att_server_request_can_send_now_event(att_con_handle);
226 }
227 
228 TEST(ATT_SERVER, att_server_can_send_packet_now){
229     int status = att_server_can_send_packet_now(att_con_handle);
230     CHECK_EQUAL(1, status);
231 
232     status = att_server_can_send_packet_now(0x50);
233     CHECK_EQUAL(0, status);
234 }
235 
236 TEST(ATT_SERVER, att_server_request_to_send_indication){
237     int status = att_server_request_to_send_indication(&indication_callback, 0x55);
238     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
239 
240     l2cap_can_send_fixed_channel_packet_now_set_status(0);
241 
242     status = att_server_request_to_send_indication(&indication_callback, att_con_handle);
243     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
244 
245     status = att_server_request_to_send_indication(&indication_callback, att_con_handle);
246     CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status);
247 }
248 
249 TEST(ATT_SERVER, att_server_request_to_send_notification){
250     int status = att_server_request_to_send_notification(&notification_callback, 0x55);
251     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
252 
253     l2cap_can_send_fixed_channel_packet_now_set_status(0);
254 
255     status = att_server_request_to_send_notification(&notification_callback, att_con_handle);
256     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
257 
258     status = att_server_request_to_send_notification(&notification_callback, att_con_handle);
259     CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status);
260 }
261 
262 TEST(ATT_SERVER, att_server_register_can_send_now_callback){
263     int status = att_server_register_can_send_now_callback(&notification_callback, 0x55);
264     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
265 
266     att_server_register_packet_handler(&att_event_packet_handler);
267 }
268 
269 TEST(ATT_SERVER, att_packet_handler_ATT_DATA_PACKET){
270     uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
271     uint8_t buffer[] = {1, 0};
272 
273     uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer);
274     mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
275 }
276 
277 TEST(ATT_SERVER, att_packet_handler_ATT_UNKNOWN_PACKET){
278     uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
279     uint8_t buffer[] = {1, 0};
280 
281     uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer);
282     mock_call_att_server_packet_handler(0xFF, att_con_handle, &att_request[0], att_request_len);
283 }
284 
285 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_L2CAP_EVENT_CAN_SEND_NOW){
286     uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
287     // local cid
288     uint8_t buffer[] = {1, 0};
289 
290     uint16_t att_request_len = att_write_request(L2CAP_EVENT_CAN_SEND_NOW, value_handle, sizeof(buffer), buffer);
291     mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len);
292 }
293 
294 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_MTU_EXCHANGE_COMPLETE){
295     uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
296     uint8_t buffer[4];
297 
298     // invalid con handle
299     memset(buffer, 0, sizeof(buffer));
300     little_endian_store_16(buffer, 2, 256);
301 
302     uint16_t att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer);
303     mock_call_att_server_packet_handler(HCI_EVENT_PACKET, 0, &att_request[0], att_request_len);
304 
305     little_endian_store_16(buffer, 0, att_con_handle);
306     att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer);
307     mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len);
308 }
309 
310 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_UNKNOWN){
311     uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
312     uint8_t buffer[] = {1, 0};
313 
314     // unknown subevent
315     uint16_t att_request_len = att_write_request(0x7bu, value_handle, sizeof(buffer), buffer);
316     mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len);
317 }
318 
319 TEST(ATT_SERVER, connection_complete_event){
320     uint8_t buffer[21];
321     buffer[0] = HCI_EVENT_LE_META;
322     buffer[1] = 19;
323     buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
324 
325     // call with wrong con_handle
326     little_endian_store_16(buffer,4,HCI_CON_HANDLE_INVALID);
327     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
328 
329     // call with correct con_handle
330     little_endian_store_16(buffer,4,att_con_handle);
331     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
332 
333     // use max MTU > ATT_REQUEST_BUFFER_SIZE
334     mock_l2cap_set_max_mtu(ATT_REQUEST_BUFFER_SIZE + 10);
335     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
336 
337     // wrong subevent
338     buffer[2] = 0xFF;
339     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
340 }
341 
342 TEST(ATT_SERVER, connection_disconnect_complete_event) {
343     uint8_t buffer[6];
344     buffer[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
345     buffer[1] = 19;
346     buffer[2] = 0;
347 
348     // call with wrong con_handle
349     hci_setup_le_connection(att_con_handle);
350     little_endian_store_16(buffer, 3, HCI_CON_HANDLE_INVALID);
351     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
352 
353     // call with correct con_handle, classic connection
354     hci_setup_classic_connection(att_con_handle);
355     little_endian_store_16(buffer, 3, att_con_handle);
356     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
357 
358     // call with correct con_handle, le connection
359     hci_setup_le_connection(att_con_handle);
360     little_endian_store_16(buffer, 3, att_con_handle);
361     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
362 
363 
364     hci_setup_le_connection(att_con_handle);
365     static uint8_t value[] = {0x55};
366     uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
367     l2cap_can_send_fixed_channel_packet_now_set_status(1);
368     // correct command
369     uint8_t status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
370     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
371 
372     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
373 }
374 
375 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){
376     // call with wrong con_handle
377     hci_setup_le_connection(con_handle);
378     little_endian_store_16(buffer, con_handle_index, HCI_CON_HANDLE_INVALID);
379     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size);
380 
381     // call with correct con_handle, classic connection
382     hci_setup_classic_connection(con_handle);
383     little_endian_store_16(buffer, con_handle_index, con_handle);
384     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size);
385 
386     // call with correct con_handle, le connection
387     hci_setup_le_connection(con_handle);
388     little_endian_store_16(buffer, con_handle_index, con_handle);
389     mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size);
390 }
391 
392 TEST(ATT_SERVER, hci_event_encryption_key_refresh_complete_event) {
393     uint8_t buffer[5];
394     buffer[0] = HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE;
395     buffer[1] = 3;
396     buffer[2] = 0;
397     // con_handle (2)
398     test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
399 }
400 
401 TEST(ATT_SERVER, hci_event_encryption_change_v2_event) {
402     uint8_t buffer[7];
403     buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE_V2;
404     buffer[1] = 5;
405     buffer[2] = 0;
406     // con_handle (2)
407     // encryption_key_size
408     buffer[6] = 1;
409     // encryption_enabled (1)
410     buffer[5] = 0;
411     test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
412 
413     // encryption_enabled (1)
414     buffer[5] = 1;
415     test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
416 }
417 
418 TEST(ATT_SERVER, hci_event_encryption_change_event) {
419     uint8_t buffer[6];
420     buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE;
421     buffer[1] = 4;
422     buffer[2] = 0;
423     // con_handle (2)
424 
425     // encryption_enabled (1)
426     buffer[5] = 0;
427     test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
428 
429     // encryption_enabled (1)
430     buffer[5] = 1;
431     test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
432 }
433 
434 TEST(ATT_SERVER, sm_event_identity_resolving_started_event) {
435     uint8_t buffer[11];
436     buffer[0] = SM_EVENT_IDENTITY_RESOLVING_STARTED;
437     buffer[1] = 9;
438 
439     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
440 }
441 
442 TEST(ATT_SERVER, sm_event_identity_resolving_succeeded_event) {
443     uint8_t buffer[20];
444     buffer[0] = SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED;
445     buffer[1] = 18; // H1B1B2
446 
447     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
448 }
449 
450 TEST(ATT_SERVER, sm_event_identity_resolving_failed_event) {
451     uint8_t buffer[11];
452     buffer[0] = SM_EVENT_IDENTITY_RESOLVING_FAILED;
453     buffer[1] = 9;
454 
455     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
456 }
457 
458 //
459 
460 TEST(ATT_SERVER, sm_event_just_works_request_event) {
461     uint8_t buffer[11];
462     buffer[0] = SM_EVENT_JUST_WORKS_REQUEST;
463     buffer[1] = 9;
464 
465     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
466 }
467 
468 TEST(ATT_SERVER, sm_event_passkey_display_number_event) {
469     uint8_t buffer[11];
470     buffer[0] = SM_EVENT_PASSKEY_DISPLAY_NUMBER;
471     buffer[1] = 9;
472 
473     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
474 }
475 
476 TEST(ATT_SERVER, sm_event_passkey_input_number_event) {
477     uint8_t buffer[11];
478     buffer[0] = SM_EVENT_PASSKEY_INPUT_NUMBER;
479     buffer[1] = 9;
480 
481     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
482 }
483 
484 TEST(ATT_SERVER, sm_event_numeric_comparison_request_event) {
485     uint8_t buffer[11];
486     buffer[0] = SM_EVENT_NUMERIC_COMPARISON_REQUEST;
487     buffer[1] = 9;
488 
489     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
490 }
491 
492 TEST(ATT_SERVER, sm_event_identity_created_event) {
493     uint8_t buffer[11];
494     buffer[0] = SM_EVENT_IDENTITY_CREATED;
495     buffer[1] = 9;
496 
497     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
498 }
499 
500 TEST(ATT_SERVER, sm_event_pairing_complete_event) {
501     uint8_t buffer[11];
502     buffer[0] = SM_EVENT_PAIRING_COMPLETE;
503     buffer[1] = 9;
504 
505     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
506 }
507 
508 TEST(ATT_SERVER, sm_event_authorization_result_event) {
509     uint8_t buffer[11];
510     buffer[0] = SM_EVENT_AUTHORIZATION_RESULT;
511     buffer[1] = 9;
512 
513     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
514 }
515 
516 TEST(ATT_SERVER, unknown_event) {
517     uint8_t buffer[11];
518     buffer[0] = 0xFF;
519     buffer[1] = 9;
520 
521     test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
522 }
523 
524 TEST(ATT_SERVER, att_server_register_service_handler) {
525     att_service_handler_t test_service;
526     test_service.read_callback = att_read_callback;
527     test_service.write_callback = att_write_callback;
528 
529     uint16_t start_handle = 0;
530     uint16_t end_handle   = 0xffff;
531     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle);
532     CHECK_EQUAL(service_found, 1);
533 
534     test_service.start_handle   = start_handle;
535     test_service.end_handle     = end_handle;
536     att_server_register_service_handler(&test_service);
537 
538     test_service.start_handle   = start_handle;
539     test_service.end_handle     = 0;
540     att_server_register_service_handler(&test_service);
541 
542     test_service.start_handle   = 0;
543     test_service.end_handle     = end_handle;
544     att_server_register_service_handler(&test_service);
545 
546     test_service.start_handle   = 0;
547     test_service.end_handle     = 0;
548     att_server_register_service_handler(&test_service);
549 }
550 
551 int main (int argc, const char * argv[]){
552     return CommandLineTestRunner::RunAllTests(argc, argv);
553 }
554