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