1 /******************************************************************************
2  *
3  *  Copyright 1999-2013 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 #define LOG_TAG "bt_srvc"
20 
21 #include <bluetooth/log.h>
22 
23 #include "gatt_api.h"
24 #include "hardware/bt_gatt_types.h"
25 #include "os/log.h"
26 #include "osi/include/allocator.h"
27 #include "osi/include/osi.h"
28 #include "srvc_dis_int.h"
29 #include "srvc_eng_int.h"
30 #include "stack/include/bt_types.h"
31 #include "stack/include/bt_uuid16.h"
32 #include "types/bluetooth/uuid.h"
33 #include "types/raw_address.h"
34 
35 using base::StringPrintf;
36 using namespace bluetooth;
37 
38 #define DIS_MAX_NUM_INC_SVR 0
39 #define DIS_MAX_CHAR_NUM 9
40 #define DIS_MAX_ATTR_NUM (DIS_MAX_CHAR_NUM * 2 + DIS_MAX_NUM_INC_SVR + 1)
41 
42 #ifndef DIS_ATTR_DB_SIZE
43 #define DIS_ATTR_DB_SIZE \
44   GATT_DB_MEM_SIZE(DIS_MAX_NUM_INC_SVR, DIS_MAX_CHAR_NUM, 0)
45 #endif
46 
47 #define uint64_t_TO_STREAM(p, u64)   \
48   {                                  \
49     *(p)++ = (uint8_t)(u64);         \
50     *(p)++ = (uint8_t)((u64) >> 8);  \
51     *(p)++ = (uint8_t)((u64) >> 16); \
52     *(p)++ = (uint8_t)((u64) >> 24); \
53     *(p)++ = (uint8_t)((u64) >> 32); \
54     *(p)++ = (uint8_t)((u64) >> 40); \
55     *(p)++ = (uint8_t)((u64) >> 48); \
56     *(p)++ = (uint8_t)((u64) >> 56); \
57   }
58 
59 static const uint16_t dis_attr_uuid[DIS_MAX_CHAR_NUM] = {
60     GATT_UUID_SYSTEM_ID,
61     GATT_UUID_MODEL_NUMBER_STR,
62     GATT_UUID_SERIAL_NUMBER_STR,
63     GATT_UUID_FW_VERSION_STR,
64     GATT_UUID_HW_VERSION_STR,
65     GATT_UUID_SW_VERSION_STR,
66     GATT_UUID_MANU_NAME,
67     GATT_UUID_IEEE_DATA,
68     GATT_UUID_PNP_ID};
69 
70 tDIS_CB dis_cb;
71 
dis_uuid_to_attr(uint16_t uuid)72 static tDIS_ATTR_MASK dis_uuid_to_attr(uint16_t uuid) {
73   switch (uuid) {
74     case GATT_UUID_SYSTEM_ID:
75       return DIS_ATTR_SYS_ID_BIT;
76     case GATT_UUID_MODEL_NUMBER_STR:
77       return DIS_ATTR_MODEL_NUM_BIT;
78     case GATT_UUID_SERIAL_NUMBER_STR:
79       return DIS_ATTR_SERIAL_NUM_BIT;
80     case GATT_UUID_FW_VERSION_STR:
81       return DIS_ATTR_FW_NUM_BIT;
82     case GATT_UUID_HW_VERSION_STR:
83       return DIS_ATTR_HW_NUM_BIT;
84     case GATT_UUID_SW_VERSION_STR:
85       return DIS_ATTR_SW_NUM_BIT;
86     case GATT_UUID_MANU_NAME:
87       return DIS_ATTR_MANU_NAME_BIT;
88     case GATT_UUID_IEEE_DATA:
89       return DIS_ATTR_IEEE_DATA_BIT;
90     case GATT_UUID_PNP_ID:
91       return DIS_ATTR_PNP_ID_BIT;
92     default:
93       return 0;
94   };
95 }
96 
97 /*******************************************************************************
98  *   dis_valid_handle_range
99  *
100  *   validate a handle to be a DIS attribute handle or not.
101  ******************************************************************************/
dis_valid_handle_range(uint16_t handle)102 bool dis_valid_handle_range(uint16_t handle) {
103   if (handle >= dis_cb.service_handle && handle <= dis_cb.max_handle)
104     return true;
105   else
106     return false;
107 }
108 /*******************************************************************************
109  *   dis_write_attr_value
110  *
111  *   Process write DIS attribute request.
112  ******************************************************************************/
dis_write_attr_value(tGATT_WRITE_REQ *,tGATT_STATUS * p_status)113 uint8_t dis_write_attr_value(tGATT_WRITE_REQ* /* p_data */,
114                              tGATT_STATUS* p_status) {
115   *p_status = GATT_WRITE_NOT_PERMIT;
116   return SRVC_ACT_RSP;
117 }
118 /*******************************************************************************
119  *   DIS Attributes Database Server Request callback
120  ******************************************************************************/
dis_read_attr_value(uint8_t,uint16_t handle,tGATT_VALUE * p_value,bool is_long,tGATT_STATUS * p_status)121 uint8_t dis_read_attr_value(uint8_t /* clcb_idx */, uint16_t handle,
122                             tGATT_VALUE* p_value, bool is_long,
123                             tGATT_STATUS* p_status) {
124   tDIS_DB_ENTRY* p_db_attr = dis_cb.dis_attr;
125   uint8_t *p = p_value->value, i, *pp;
126   uint16_t offset = p_value->offset;
127   uint8_t act = SRVC_ACT_RSP;
128   tGATT_STATUS st = GATT_NOT_FOUND;
129 
130   for (i = 0; i < DIS_MAX_CHAR_NUM; i++, p_db_attr++) {
131     if (handle == p_db_attr->handle) {
132       if ((p_db_attr->uuid == GATT_UUID_PNP_ID ||
133            p_db_attr->uuid == GATT_UUID_SYSTEM_ID) &&
134           is_long) {
135         st = GATT_NOT_LONG;
136         break;
137       }
138       st = GATT_SUCCESS;
139 
140       switch (p_db_attr->uuid) {
141         case GATT_UUID_MANU_NAME:
142         case GATT_UUID_MODEL_NUMBER_STR:
143         case GATT_UUID_SERIAL_NUMBER_STR:
144         case GATT_UUID_FW_VERSION_STR:
145         case GATT_UUID_HW_VERSION_STR:
146         case GATT_UUID_SW_VERSION_STR:
147         case GATT_UUID_IEEE_DATA:
148           pp = dis_cb.dis_value
149                    .data_string[p_db_attr->uuid - GATT_UUID_MODEL_NUMBER_STR];
150           if (pp != NULL) {
151             if (strlen((char*)pp) > GATT_MAX_ATTR_LEN)
152               p_value->len = GATT_MAX_ATTR_LEN;
153             else
154               p_value->len = (uint16_t)strlen((char*)pp);
155           } else
156             p_value->len = 0;
157 
158           if (offset > p_value->len) {
159             st = GATT_INVALID_OFFSET;
160             break;
161           } else {
162             p_value->len -= offset;
163             pp += offset;
164             ARRAY_TO_STREAM(p, pp, p_value->len);
165             log::verbose("GATT_UUID_MANU_NAME len=0x{:x}", p_value->len);
166           }
167           break;
168 
169         case GATT_UUID_SYSTEM_ID:
170           uint64_t_TO_STREAM(p, dis_cb.dis_value.system_id); /* int_min */
171           p_value->len = DIS_SYSTEM_ID_SIZE;
172           break;
173 
174         case GATT_UUID_PNP_ID:
175           UINT8_TO_STREAM(p, dis_cb.dis_value.pnp_id.vendor_id_src);
176           UINT16_TO_STREAM(p, dis_cb.dis_value.pnp_id.vendor_id);
177           UINT16_TO_STREAM(p, dis_cb.dis_value.pnp_id.product_id);
178           UINT16_TO_STREAM(p, dis_cb.dis_value.pnp_id.product_version);
179           p_value->len = DIS_PNP_ID_SIZE;
180           break;
181       }
182       break;
183     }
184   }
185   *p_status = st;
186   return act;
187 }
188 
189 /*******************************************************************************
190  *
191  * Function         dis_gatt_c_read_dis_value_cmpl
192  *
193  * Description      Client read DIS database complete callback.
194  *
195  * Returns          void
196  *
197  ******************************************************************************/
dis_gatt_c_read_dis_value_cmpl(uint16_t conn_id)198 static void dis_gatt_c_read_dis_value_cmpl(uint16_t conn_id) {
199   tSRVC_CLCB* p_clcb = srvc_eng_find_clcb_by_conn_id(conn_id);
200 
201   dis_cb.dis_read_uuid_idx = 0xff;
202 
203   srvc_eng_release_channel(conn_id);
204 
205   if (dis_cb.p_read_dis_cback && p_clcb) {
206     log::info("conn_id:{} attr_mask = 0x{:04x}", conn_id,
207               p_clcb->dis_value.attr_mask);
208 
209     (*dis_cb.p_read_dis_cback)(p_clcb->bda, &p_clcb->dis_value);
210     dis_cb.p_read_dis_cback = NULL;
211   }
212 }
213 
214 /*******************************************************************************
215  *
216  * Function         dis_gatt_c_read_dis_req
217  *
218  * Description      Read remote device DIS attribute request.
219  *
220  * Returns          void
221  *
222  ******************************************************************************/
dis_gatt_c_read_dis_req(uint16_t conn_id)223 bool dis_gatt_c_read_dis_req(uint16_t conn_id) {
224   tGATT_READ_PARAM param;
225 
226   memset(&param, 0, sizeof(tGATT_READ_PARAM));
227 
228   param.service.s_handle = 1;
229   param.service.e_handle = 0xFFFF;
230   param.service.auth_req = 0;
231 
232   while (dis_cb.dis_read_uuid_idx < DIS_MAX_CHAR_NUM) {
233     if (dis_uuid_to_attr(dis_attr_uuid[dis_cb.dis_read_uuid_idx]) &
234         dis_cb.request_mask) {
235       param.service.uuid =
236           bluetooth::Uuid::From16Bit(dis_attr_uuid[dis_cb.dis_read_uuid_idx]);
237 
238       if (GATTC_Read(conn_id, GATT_READ_BY_TYPE, &param) == GATT_SUCCESS)
239         return true;
240 
241       log::error("Read DISInfo: {} GATT_Read Failed", param.service.uuid);
242     }
243 
244     dis_cb.dis_read_uuid_idx++;
245   }
246 
247   dis_gatt_c_read_dis_value_cmpl(conn_id);
248 
249   return (false);
250 }
251 
252 /*******************************************************************************
253  *
254  * Function         dis_c_cmpl_cback
255  *
256  * Description      Client operation complete callback.
257  *
258  * Returns          void
259  *
260  ******************************************************************************/
dis_c_cmpl_cback(tSRVC_CLCB * p_clcb,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)261 void dis_c_cmpl_cback(tSRVC_CLCB* p_clcb, tGATTC_OPTYPE op, tGATT_STATUS status,
262                       tGATT_CL_COMPLETE* p_data) {
263   uint16_t read_type;
264   uint8_t *pp = NULL, *p_str;
265   uint16_t conn_id = p_clcb->conn_id;
266 
267   if (dis_cb.dis_read_uuid_idx >= (sizeof(dis_attr_uuid)/sizeof(dis_attr_uuid[0]))) {
268     log::error("invalid dis_cb.dis_read_uuid_idx");
269     return;
270   }
271 
272   read_type = dis_attr_uuid[dis_cb.dis_read_uuid_idx];
273 
274   log::verbose("op_code: 0x{:02x}  status: 0x{:02x} read_type: 0x{:04x}", op,
275                status, read_type);
276 
277   if (op != GATTC_OPTYPE_READ) return;
278 
279   if (p_data != NULL && status == GATT_SUCCESS) {
280     pp = p_data->att_value.value;
281 
282     switch (read_type) {
283       case GATT_UUID_SYSTEM_ID:
284         log::verbose("DIS_ATTR_SYS_ID_BIT");
285         if (p_data->att_value.len == DIS_SYSTEM_ID_SIZE) {
286           p_clcb->dis_value.attr_mask |= DIS_ATTR_SYS_ID_BIT;
287           /* save system ID*/
288           STREAM_TO_UINT64(p_clcb->dis_value.system_id, pp);
289         }
290         break;
291 
292       case GATT_UUID_PNP_ID:
293         if (p_data->att_value.len == DIS_PNP_ID_SIZE) {
294           p_clcb->dis_value.attr_mask |= DIS_ATTR_PNP_ID_BIT;
295           STREAM_TO_UINT8(p_clcb->dis_value.pnp_id.vendor_id_src, pp);
296           STREAM_TO_UINT16(p_clcb->dis_value.pnp_id.vendor_id, pp);
297           STREAM_TO_UINT16(p_clcb->dis_value.pnp_id.product_id, pp);
298           STREAM_TO_UINT16(p_clcb->dis_value.pnp_id.product_version, pp);
299         }
300         break;
301 
302       case GATT_UUID_MODEL_NUMBER_STR:
303       case GATT_UUID_SERIAL_NUMBER_STR:
304       case GATT_UUID_FW_VERSION_STR:
305       case GATT_UUID_HW_VERSION_STR:
306       case GATT_UUID_SW_VERSION_STR:
307       case GATT_UUID_MANU_NAME:
308       case GATT_UUID_IEEE_DATA:
309         p_str = p_clcb->dis_value
310                     .data_string[read_type - GATT_UUID_MODEL_NUMBER_STR];
311         osi_free(p_str);
312         p_str = (uint8_t*)osi_malloc(p_data->att_value.len + 1);
313         p_clcb->dis_value.attr_mask |= dis_uuid_to_attr(read_type);
314         memcpy(p_str, p_data->att_value.value, p_data->att_value.len);
315         p_str[p_data->att_value.len] = 0;
316         p_clcb->dis_value.data_string[read_type - GATT_UUID_MODEL_NUMBER_STR] =
317             p_str;
318         break;
319 
320       default:
321         break;
322 
323         break;
324     } /* end switch */
325   }   /* end if */
326 
327   dis_cb.dis_read_uuid_idx++;
328 
329   dis_gatt_c_read_dis_req(conn_id);
330 }
331 
332 /*******************************************************************************
333  *
334  * Function         DIS_SrInit
335  *
336  * Description      Initialize the Device Information Service Server.
337  *
338  ******************************************************************************/
DIS_SrInit(tDIS_ATTR_MASK dis_attr_mask)339 tDIS_STATUS DIS_SrInit(tDIS_ATTR_MASK dis_attr_mask) {
340   tGATT_STATUS status;
341 
342   if (dis_cb.enabled) {
343     log::error("DIS already initalized");
344     return DIS_SUCCESS;
345   }
346 
347   memset(&dis_cb, 0, sizeof(tDIS_CB));
348 
349   btgatt_db_element_t service[DIS_MAX_ATTR_NUM] = {};
350 
351   bluetooth::Uuid svc_uuid =
352       bluetooth::Uuid::From16Bit(UUID_SERVCLASS_DEVICE_INFO);
353   service[0].type = BTGATT_DB_PRIMARY_SERVICE;
354   service[0].uuid = svc_uuid;
355 
356   for (int i = 0; dis_attr_mask != 0 && i < DIS_MAX_CHAR_NUM; i++) {
357     dis_cb.dis_attr[i].uuid = dis_attr_uuid[i];
358 
359     bluetooth::Uuid char_uuid =
360         bluetooth::Uuid::From16Bit(dis_cb.dis_attr[i].uuid);
361     /* index 0 is service, so characteristics start from 1 */
362     service[i + 1].type = BTGATT_DB_CHARACTERISTIC;
363     service[i + 1].uuid = char_uuid;
364     service[i + 1].properties = GATT_CHAR_PROP_BIT_READ;
365     service[i + 1].permissions = GATT_PERM_READ;
366 
367     dis_attr_mask >>= 1;
368   }
369 
370   /* Add a GAP service */
371   status = GATTS_AddService(srvc_eng_cb.gatt_if, service,
372                             sizeof(service) / sizeof(btgatt_db_element_t));
373   if (status != GATT_SERVICE_STARTED) {
374     log::error("Can not create service, DIS_Init failed!");
375     return GATT_ERROR;
376   }
377 
378   dis_cb.service_handle = service[0].attribute_handle;
379   dis_cb.max_handle = dis_cb.service_handle + DIS_MAX_ATTR_NUM;
380 
381   for (int i = 0; i < DIS_MAX_CHAR_NUM; i++) {
382     dis_cb.dis_attr[i].handle = service[i + 1].attribute_handle;
383 
384     log::verbose("handle of new attribute 0x{:04x} = {}",
385                  dis_cb.dis_attr[i].uuid, dis_cb.dis_attr[i].handle);
386   }
387 
388   dis_cb.enabled = true;
389   return (tDIS_STATUS)status;
390 }
391 /*******************************************************************************
392  *
393  * Function         DIS_SrUpdate
394  *
395  * Description      Update the DIS server attribute values
396  *
397  ******************************************************************************/
DIS_SrUpdate(tDIS_ATTR_BIT dis_attr_bit,tDIS_ATTR * p_info)398 tDIS_STATUS DIS_SrUpdate(tDIS_ATTR_BIT dis_attr_bit, tDIS_ATTR* p_info) {
399   uint8_t i = 1;
400   tDIS_STATUS st = DIS_SUCCESS;
401 
402   if (dis_attr_bit & DIS_ATTR_SYS_ID_BIT) {
403     dis_cb.dis_value.system_id = p_info->system_id;
404   } else if (dis_attr_bit & DIS_ATTR_PNP_ID_BIT) {
405     dis_cb.dis_value.pnp_id.vendor_id = p_info->pnp_id.vendor_id;
406     dis_cb.dis_value.pnp_id.vendor_id_src = p_info->pnp_id.vendor_id_src;
407     dis_cb.dis_value.pnp_id.product_id = p_info->pnp_id.product_id;
408     dis_cb.dis_value.pnp_id.product_version = p_info->pnp_id.product_version;
409   } else {
410     st = DIS_ILLEGAL_PARAM;
411 
412     while (dis_attr_bit && i < (DIS_MAX_CHAR_NUM - 1)) {
413       if (dis_attr_bit & (uint16_t)(1 << i)) {
414         osi_free(dis_cb.dis_value.data_string[i - 1]);
415         dis_cb.dis_value.data_string[i - 1] =
416             (uint8_t*)osi_malloc(p_info->data_str.len + 1);
417         memcpy(dis_cb.dis_value.data_string[i - 1], p_info->data_str.p_data,
418                p_info->data_str.len);
419         dis_cb.dis_value.data_string[i - 1][p_info->data_str.len] =
420             0; /* make sure null terminate */
421         st = DIS_SUCCESS;
422 
423         break;
424       }
425       i++;
426     }
427   }
428   return st;
429 }
430 /*******************************************************************************
431  *
432  * Function         DIS_ReadDISInfo
433  *
434  * Description      Read remote device DIS information.
435  *
436  * Returns          true on success, false otherwise
437  *
438  ******************************************************************************/
DIS_ReadDISInfo(const RawAddress & peer_bda,tDIS_READ_CBACK * p_cback,tDIS_ATTR_MASK mask)439 bool DIS_ReadDISInfo(const RawAddress& peer_bda, tDIS_READ_CBACK* p_cback,
440                      tDIS_ATTR_MASK mask) {
441   uint16_t conn_id;
442 
443   /* Initialize the DIS client if it hasn't been initialized already. */
444   srvc_eng_init();
445 
446   /* For now we only handle one at a time */
447   if (dis_cb.dis_read_uuid_idx != 0xff) return (false);
448 
449   if (p_cback == NULL) return (false);
450 
451   dis_cb.p_read_dis_cback = p_cback;
452   /* Mark currently active operation */
453   dis_cb.dis_read_uuid_idx = 0;
454 
455   dis_cb.request_mask = mask;
456 
457   log::verbose("BDA: {} cl_read_uuid: 0x{:04x}", peer_bda,
458                dis_attr_uuid[dis_cb.dis_read_uuid_idx]);
459 
460   if (!GATT_GetConnIdIfConnected(srvc_eng_cb.gatt_if, peer_bda, &conn_id,
461                                  BT_TRANSPORT_LE)) {
462     conn_id = GATT_INVALID_CONN_ID;
463   }
464 
465   /* need to enhance it as multiple service is needed */
466   srvc_eng_request_channel(peer_bda, SRVC_ID_DIS);
467 
468   if (conn_id == GATT_INVALID_CONN_ID) {
469     return GATT_Connect(srvc_eng_cb.gatt_if, peer_bda,
470                         BTM_BLE_DIRECT_CONNECTION, BT_TRANSPORT_LE, false);
471   }
472 
473   return dis_gatt_c_read_dis_req(conn_id);
474 }
475