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