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