OSDN Git Service

RESTRICT AUTOMERGE: Fixes two bluetooth bugs causing remote overreads (1/2)
[android-x86/system-bt.git] / stack / gatt / gatt_attr.cc
1 /******************************************************************************
2  *
3  *  Copyright (C) 2008-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  this file contains the main GATT server attributes access request
22  *  handling functions.
23  *
24  ******************************************************************************/
25
26 #include "bt_target.h"
27 #include "bt_utils.h"
28
29 #include "btcore/include/uuid.h"
30 #include "gatt_api.h"
31 #include "gatt_int.h"
32 #include "osi/include/osi.h"
33
34 using base::StringPrintf;
35
36 #define GATTP_MAX_NUM_INC_SVR 0
37 #define GATTP_MAX_CHAR_NUM 2
38 #define GATTP_MAX_ATTR_NUM (GATTP_MAX_CHAR_NUM * 2 + GATTP_MAX_NUM_INC_SVR + 1)
39 #define GATTP_MAX_CHAR_VALUE_SIZE 50
40
41 #ifndef GATTP_ATTR_DB_SIZE
42 #define GATTP_ATTR_DB_SIZE                                    \
43   GATT_DB_MEM_SIZE(GATTP_MAX_NUM_INC_SVR, GATTP_MAX_CHAR_NUM, \
44                    GATTP_MAX_CHAR_VALUE_SIZE)
45 #endif
46
47 static void gatt_request_cback(uint16_t conn_id, uint32_t trans_id,
48                                uint8_t op_code, tGATTS_DATA* p_data);
49 static void gatt_connect_cback(UNUSED_ATTR tGATT_IF gatt_if,
50                                const RawAddress& bda, uint16_t conn_id,
51                                bool connected, tGATT_DISCONN_REASON reason,
52                                tBT_TRANSPORT transport);
53 static void gatt_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
54                                 tGATT_DISC_RES* p_data);
55 static void gatt_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
56                                  tGATT_STATUS status);
57 static void gatt_cl_op_cmpl_cback(UNUSED_ATTR uint16_t conn_id,
58                                   UNUSED_ATTR tGATTC_OPTYPE op,
59                                   UNUSED_ATTR tGATT_STATUS status,
60                                   UNUSED_ATTR tGATT_CL_COMPLETE* p_data);
61
62 static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb);
63
64 static tGATT_CBACK gatt_profile_cback = {gatt_connect_cback,
65                                          gatt_cl_op_cmpl_cback,
66                                          gatt_disc_res_cback,
67                                          gatt_disc_cmpl_cback,
68                                          gatt_request_cback,
69                                          NULL,
70                                          NULL,
71                                          NULL,
72                                          NULL};
73
74 /*******************************************************************************
75  *
76  * Function         gatt_profile_find_conn_id_by_bd_addr
77  *
78  * Description      Find the connection ID by remote address
79  *
80  * Returns          Connection ID
81  *
82  ******************************************************************************/
83 uint16_t gatt_profile_find_conn_id_by_bd_addr(const RawAddress& remote_bda) {
84   uint16_t conn_id = GATT_INVALID_CONN_ID;
85   GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &conn_id,
86                             BT_TRANSPORT_LE);
87   return conn_id;
88 }
89
90 /*******************************************************************************
91  *
92  * Function         gatt_profile_find_clcb_by_conn_id
93  *
94  * Description      find clcb by Connection ID
95  *
96  * Returns          Pointer to the found link conenction control block.
97  *
98  ******************************************************************************/
99 static tGATT_PROFILE_CLCB* gatt_profile_find_clcb_by_conn_id(uint16_t conn_id) {
100   uint8_t i_clcb;
101   tGATT_PROFILE_CLCB* p_clcb = NULL;
102
103   for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS;
104        i_clcb++, p_clcb++) {
105     if (p_clcb->in_use && p_clcb->conn_id == conn_id) return p_clcb;
106   }
107
108   return NULL;
109 }
110
111 /*******************************************************************************
112  *
113  * Function         gatt_profile_find_clcb_by_bd_addr
114  *
115  * Description      The function searches all LCBs with macthing bd address.
116  *
117  * Returns          Pointer to the found link conenction control block.
118  *
119  ******************************************************************************/
120 static tGATT_PROFILE_CLCB* gatt_profile_find_clcb_by_bd_addr(
121     const RawAddress& bda, tBT_TRANSPORT transport) {
122   uint8_t i_clcb;
123   tGATT_PROFILE_CLCB* p_clcb = NULL;
124
125   for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS;
126        i_clcb++, p_clcb++) {
127     if (p_clcb->in_use && p_clcb->transport == transport && p_clcb->connected &&
128         p_clcb->bda == bda)
129       return p_clcb;
130   }
131
132   return NULL;
133 }
134
135 /*******************************************************************************
136  *
137  * Function         gatt_profile_clcb_alloc
138  *
139  * Description      The function allocates a GATT profile connection link
140  *                  control block
141  *
142  * Returns          NULL if not found. Otherwise pointer to the connection link
143  *                  block.
144  *
145  ******************************************************************************/
146 tGATT_PROFILE_CLCB* gatt_profile_clcb_alloc(uint16_t conn_id,
147                                             const RawAddress& bda,
148                                             tBT_TRANSPORT tranport) {
149   uint8_t i_clcb = 0;
150   tGATT_PROFILE_CLCB* p_clcb = NULL;
151
152   for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS;
153        i_clcb++, p_clcb++) {
154     if (!p_clcb->in_use) {
155       p_clcb->in_use = true;
156       p_clcb->conn_id = conn_id;
157       p_clcb->connected = true;
158       p_clcb->transport = tranport;
159       p_clcb->bda = bda;
160       break;
161     }
162   }
163   if (i_clcb < GATT_MAX_APPS) return p_clcb;
164
165   return NULL;
166 }
167
168 /*******************************************************************************
169  *
170  * Function         gatt_profile_clcb_dealloc
171  *
172  * Description      The function deallocates a GATT profile connection link
173  *                  control block
174  *
175  * Returns          void
176  *
177  ******************************************************************************/
178 void gatt_profile_clcb_dealloc(tGATT_PROFILE_CLCB* p_clcb) {
179   memset(p_clcb, 0, sizeof(tGATT_PROFILE_CLCB));
180 }
181
182 /*******************************************************************************
183  *
184  * Function         gatt_request_cback
185  *
186  * Description      GATT profile attribute access request callback.
187  *
188  * Returns          void.
189  *
190  ******************************************************************************/
191 static void gatt_request_cback(uint16_t conn_id, uint32_t trans_id,
192                                tGATTS_REQ_TYPE type, tGATTS_DATA* p_data) {
193   uint8_t status = GATT_INVALID_PDU;
194   tGATTS_RSP rsp_msg;
195   bool ignore = false;
196
197   memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
198
199   switch (type) {
200     case GATTS_REQ_TYPE_READ_CHARACTERISTIC:
201     case GATTS_REQ_TYPE_READ_DESCRIPTOR:
202       status = GATT_READ_NOT_PERMIT;
203       break;
204
205     case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC:
206     case GATTS_REQ_TYPE_WRITE_DESCRIPTOR:
207       status = GATT_WRITE_NOT_PERMIT;
208       break;
209
210     case GATTS_REQ_TYPE_WRITE_EXEC:
211     case GATT_CMD_WRITE:
212       ignore = true;
213       VLOG(1) << StringPrintf("Ignore GATT_REQ_EXEC_WRITE/WRITE_CMD");
214       break;
215
216     case GATTS_REQ_TYPE_MTU:
217       VLOG(1) << StringPrintf("Get MTU exchange new mtu size: %d", p_data->mtu);
218       ignore = true;
219       break;
220
221     default:
222       VLOG(1) << StringPrintf("Unknown/unexpected LE GAP ATT request: 0x%02x",
223                               type);
224       break;
225   }
226
227   if (!ignore) GATTS_SendRsp(conn_id, trans_id, status, &rsp_msg);
228 }
229
230 /*******************************************************************************
231  *
232  * Function         gatt_connect_cback
233  *
234  * Description      Gatt profile connection callback.
235  *
236  * Returns          void
237  *
238  ******************************************************************************/
239 static void gatt_connect_cback(UNUSED_ATTR tGATT_IF gatt_if,
240                                const RawAddress& bda, uint16_t conn_id,
241                                bool connected, tGATT_DISCONN_REASON reason,
242                                tBT_TRANSPORT transport) {
243   VLOG(1) << __func__ << ": from " << bda
244           << StringPrintf(" connected:%d conn_id=%d reason = 0x%04x", connected,
245                           conn_id, reason);
246
247   tGATT_PROFILE_CLCB* p_clcb =
248       gatt_profile_find_clcb_by_bd_addr(bda, transport);
249   if (p_clcb == NULL) return;
250
251   if (connected) {
252     p_clcb->conn_id = conn_id;
253     p_clcb->connected = true;
254
255     if (p_clcb->ccc_stage == GATT_SVC_CHANGED_CONNECTING) {
256       p_clcb->ccc_stage++;
257       gatt_cl_start_config_ccc(p_clcb);
258     }
259   } else {
260     gatt_profile_clcb_dealloc(p_clcb);
261   }
262 }
263
264 /*******************************************************************************
265  *
266  * Function         gatt_profile_db_init
267  *
268  * Description      Initializa the GATT profile attribute database.
269  *
270  ******************************************************************************/
271 void gatt_profile_db_init(void) {
272   tBT_UUID app_uuid = {LEN_UUID_128, {0}};
273   uint16_t service_handle = 0;
274
275   /* Fill our internal UUID with a fixed pattern 0x81 */
276   memset(&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
277
278   /* Create a GATT profile service */
279   gatt_cb.gatt_if = GATT_Register(&app_uuid, &gatt_profile_cback);
280   GATT_StartIf(gatt_cb.gatt_if);
281
282   bt_uuid_t service_uuid;
283   uuid_128_from_16(&service_uuid, UUID_SERVCLASS_GATT_SERVER);
284
285   bt_uuid_t char_uuid;
286   uuid_128_from_16(&char_uuid, GATT_UUID_GATT_SRV_CHGD);
287
288   btgatt_db_element_t service[] = {
289       {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = service_uuid},
290       {.type = BTGATT_DB_CHARACTERISTIC,
291        .uuid = char_uuid,
292        .properties = GATT_CHAR_PROP_BIT_INDICATE,
293        .permissions = 0}};
294
295   GATTS_AddService(gatt_cb.gatt_if, service,
296                    sizeof(service) / sizeof(btgatt_db_element_t));
297
298   service_handle = service[0].attribute_handle;
299   gatt_cb.handle_of_h_r = service[1].attribute_handle;
300
301   LOG(ERROR) << StringPrintf("gatt_profile_db_init:  gatt_if=%d",
302                              gatt_cb.gatt_if);
303 }
304
305 /*******************************************************************************
306  *
307  * Function         gatt_disc_res_cback
308  *
309  * Description      Gatt profile discovery result callback
310  *
311  * Returns          void
312  *
313  ******************************************************************************/
314 static void gatt_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
315                                 tGATT_DISC_RES* p_data) {
316   tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
317
318   if (p_clcb == NULL) return;
319
320   switch (disc_type) {
321     case GATT_DISC_SRVC_BY_UUID: /* stage 1 */
322       p_clcb->e_handle = p_data->value.group_value.e_handle;
323       p_clcb->ccc_result++;
324       break;
325
326     case GATT_DISC_CHAR: /* stage 2 */
327       p_clcb->s_handle = p_data->value.dclr_value.val_handle;
328       p_clcb->ccc_result++;
329       break;
330
331     case GATT_DISC_CHAR_DSCPT: /* stage 3 */
332       if (p_data->type.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG) {
333         p_clcb->s_handle = p_data->handle;
334         p_clcb->ccc_result++;
335       }
336       break;
337   }
338 }
339
340 /*******************************************************************************
341  *
342  * Function         gatt_disc_cmpl_cback
343  *
344  * Description      Gatt profile discovery complete callback
345  *
346  * Returns          void
347  *
348  ******************************************************************************/
349 static void gatt_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
350                                  tGATT_STATUS status) {
351   tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
352
353   if (p_clcb == NULL) return;
354
355   if (status == GATT_SUCCESS && p_clcb->ccc_result > 0) {
356     p_clcb->ccc_result = 0;
357     p_clcb->ccc_stage++;
358     gatt_cl_start_config_ccc(p_clcb);
359   } else {
360     LOG(ERROR) << StringPrintf(
361         "%s() - Unable to register for service changed indication", __func__);
362   }
363 }
364
365 /*******************************************************************************
366  *
367  * Function         gatt_cl_op_cmpl_cback
368  *
369  * Description      Gatt profile client operation complete callback
370  *
371  * Returns          void
372  *
373  ******************************************************************************/
374 static void gatt_cl_op_cmpl_cback(UNUSED_ATTR uint16_t conn_id,
375                                   UNUSED_ATTR tGATTC_OPTYPE op,
376                                   UNUSED_ATTR tGATT_STATUS status,
377                                   UNUSED_ATTR tGATT_CL_COMPLETE* p_data) {}
378
379 /*******************************************************************************
380  *
381  * Function         gatt_cl_start_config_ccc
382  *
383  * Description      Gatt profile start configure service change CCC
384  *
385  * Returns          void
386  *
387  ******************************************************************************/
388 static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb) {
389   tGATT_DISC_PARAM srvc_disc_param;
390   tGATT_VALUE ccc_value;
391
392   VLOG(1) << StringPrintf("%s() - stage: %d", __func__, p_clcb->ccc_stage);
393
394   memset(&srvc_disc_param, 0, sizeof(tGATT_DISC_PARAM));
395   memset(&ccc_value, 0, sizeof(tGATT_VALUE));
396
397   switch (p_clcb->ccc_stage) {
398     case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */
399       srvc_disc_param.s_handle = 1;
400       srvc_disc_param.e_handle = 0xffff;
401       srvc_disc_param.service.len = 2;
402       srvc_disc_param.service.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
403       GATTC_Discover(p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, &srvc_disc_param);
404       break;
405
406     case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */
407       srvc_disc_param.s_handle = 1;
408       srvc_disc_param.e_handle = p_clcb->e_handle;
409       srvc_disc_param.service.len = 2;
410       srvc_disc_param.service.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
411       GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR, &srvc_disc_param);
412       break;
413
414     case GATT_SVC_CHANGED_DESCRIPTOR: /* discover service change ccc */
415       srvc_disc_param.s_handle = p_clcb->s_handle;
416       srvc_disc_param.e_handle = p_clcb->e_handle;
417       GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR_DSCPT, &srvc_disc_param);
418       break;
419
420     case GATT_SVC_CHANGED_CONFIGURE_CCCD: /* write ccc */
421       ccc_value.handle = p_clcb->s_handle;
422       ccc_value.len = 2;
423       ccc_value.value[0] = GATT_CLT_CONFIG_INDICATION;
424       GATTC_Write(p_clcb->conn_id, GATT_WRITE, &ccc_value);
425       break;
426   }
427 }
428
429 /*******************************************************************************
430  *
431  * Function         GATT_ConfigServiceChangeCCC
432  *
433  * Description      Configure service change indication on remote device
434  *
435  * Returns          none
436  *
437  ******************************************************************************/
438 void GATT_ConfigServiceChangeCCC(const RawAddress& remote_bda, bool enable,
439                                  tBT_TRANSPORT transport) {
440   tGATT_PROFILE_CLCB* p_clcb =
441       gatt_profile_find_clcb_by_bd_addr(remote_bda, transport);
442
443   if (p_clcb == NULL)
444     p_clcb = gatt_profile_clcb_alloc(0, remote_bda, transport);
445
446   if (p_clcb == NULL) return;
447
448   if (GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &p_clcb->conn_id,
449                                 transport)) {
450     p_clcb->connected = true;
451   }
452   /* hold the link here */
453   GATT_Connect(gatt_cb.gatt_if, remote_bda, true, transport, true);
454   p_clcb->ccc_stage = GATT_SVC_CHANGED_CONNECTING;
455
456   if (!p_clcb->connected) {
457     /* wait for connection */
458     return;
459   }
460
461   p_clcb->ccc_stage++;
462   gatt_cl_start_config_ccc(p_clcb);
463 }