xref: /btstack/test/gatt_server/gatt_server_test.cpp (revision f0e9886bf1825249aaea7e15f4fe6fb8d065bffb)
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 
24 #include "bluetooth_gatt.h"
25 
26 static uint8_t battery_level = 100;
27 static const uint8_t uuid128_with_bluetooth_base[] = { 0x00, 0x00, 0xBB, 0xBB, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
28 static const uint8_t uuid128_no_bluetooth_base[] =   { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAA, 0xAA, 0x00, 0x00 };
29 
30 extern "C" void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status);
31 
32 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){
33     UNUSED(connection_handle);
34     UNUSED(att_handle);
35     UNUSED(offset);
36     UNUSED(buffer);
37     UNUSED(buffer_size);
38 
39     return 0;
40 }
41 
42 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){
43     UNUSED(connection_handle);
44     UNUSED(att_handle);
45     UNUSED(transaction_mode);
46     UNUSED(offset);
47     UNUSED(buffer);
48     UNUSED(buffer_size);
49 
50     return 0;
51 }
52 
53 
54 TEST_GROUP(ATT_SERVER){
55     uint16_t att_con_handle;
56 
57     void setup(void){
58         att_con_handle = 0x00;
59 
60         // init att db util and add a service and characteristic
61         att_db_util_init();
62         // 0x180F
63         att_db_util_add_service_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE);
64         // 0x2A19
65         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);
66         // 0x2A1B
67         att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL_STATE, ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
68         // 0x2A1A
69         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);
70         // 0x2A49
71         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);
72         // 0x2A35
73         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);
74 
75 
76         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);
77         // 0x2A38
78         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);
79 
80 
81         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);
82         // 0x2AAB
83         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);
84 
85         // setup ATT server
86         att_server_init(att_db_util_get_address(), att_read_callback, att_write_callback);
87     }
88 };
89 
90 TEST(ATT_SERVER, gatt_server_get_value_handle_for_characteristic_with_uuid16){
91     // att_dump_attributes();
92     uint16_t value_handle;
93 
94     // start handle > value handle
95     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0xf000, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
96     CHECK_EQUAL(0, value_handle);
97 
98     // end handle < value handle
99     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0x02, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
100     CHECK_EQUAL(0, value_handle);
101 
102     // search value handle for unknown UUID
103     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xffff);
104     CHECK_EQUAL(0, value_handle);
105 
106     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0);
107     CHECK_EQUAL(0, value_handle);
108 
109     // search value handle after one with uuid128_no_bluetooth_base
110     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION);
111     CHECK_EQUAL(0x0014, value_handle);
112 
113     // search value handle after one with uuid128_with_bluetooth_base
114     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME);
115     CHECK_EQUAL(0x001a, value_handle);
116 
117     // search value handle registered with bluetooth_base
118     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xbbbb);
119     CHECK_EQUAL(0x0017, value_handle);
120 
121     // correct read
122     value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
123     CHECK_EQUAL(0x03, value_handle);
124 }
125 
126 
127 TEST(ATT_SERVER, att_server_indicate){
128     static uint8_t value[] = {0x55};
129     uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
130     uint8_t status;
131 
132     // invalid conneciton handle
133     status = att_server_indicate(0x50, value_handle, &value[0], 0);
134     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
135 
136     // L2CAP cannot send
137     l2cap_can_send_fixed_channel_packet_now_set_status(0);
138     status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
139     CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status);
140     l2cap_can_send_fixed_channel_packet_now_set_status(1);
141 
142     // correct command
143     status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
144     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
145 
146     // already in progress
147     status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
148     CHECK_EQUAL(ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS, status);
149 }
150 
151 TEST(ATT_SERVER, att_server_notify){
152     static uint8_t value[] = {0x55};
153     uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
154     uint8_t status;
155 
156     // invalid conneciton handle
157     status = att_server_notify(0x50, value_handle, &value[0], 0);
158     CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
159 
160     // L2CAP cannot send
161     l2cap_can_send_fixed_channel_packet_now_set_status(0);
162     status = att_server_notify(att_con_handle, value_handle, &value[0], 0);
163     CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status);
164     l2cap_can_send_fixed_channel_packet_now_set_status(1);
165 
166     // correct command
167     status = att_server_notify(att_con_handle, value_handle, &value[0], 0);
168     CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
169 }
170 
171 TEST(ATT_SERVER, att_server_get_mtu){
172     // invalid conneciton handle
173     uint8_t mtu = att_server_get_mtu(0x50);
174     CHECK_EQUAL(0, mtu);
175 
176     mtu = att_server_get_mtu(att_con_handle);
177     CHECK_EQUAL(0, mtu);
178 }
179 
180 
181 TEST(ATT_SERVER, att_server_request_can_send_now_event){
182     att_server_request_can_send_now_event(0x00);
183 }
184 
185 int main (int argc, const char * argv[]){
186     return CommandLineTestRunner::RunAllTests(argc, argv);
187 }
188