@@ -31,16 +31,22 @@ ssize_t profile_read_process(struct bt_conn *conn,
3131{
3232 const unsigned char *pvalue;
3333 BLEAttribute *bleattr = (BLEAttribute *)attr->user_data ;
34- BLECharacteristic* blecharacteritic;
3534 BLEAttributeType type = bleattr->type ();
36- if (BLETypeCharacteristic ! = type)
35+ if (BLETypeCharacteristic = = type)
3736 {
38- return 0 ;
37+ BLECharacteristic* blecharacteritic;
38+ blecharacteritic = (BLECharacteristic*)bleattr;
39+ pvalue = blecharacteritic->value ();
40+ return bt_gatt_attr_read (conn, attr, buf, len, offset, pvalue,
41+ blecharacteritic->valueLength ());
3942 }
40- blecharacteritic = (BLECharacteristic*)bleattr;
41- pvalue = blecharacteritic->value ();
42- return bt_gatt_attr_read (conn, attr, buf, len, offset, pvalue,
43- blecharacteritic->valueLength ());
43+ else if (BLETypeDescriptor == type)
44+ {
45+ BLEDescriptor *bledescriptor = (BLEDescriptor *)bleattr;
46+ pvalue = bledescriptor->value ();
47+ return bt_gatt_attr_read (conn, attr, buf, len, offset, pvalue, bledescriptor->valueLength ());
48+ }
49+ return 0 ;
4450}
4551
4652// Only for peripheral
@@ -256,7 +262,18 @@ BLEProfile::addAttribute (BLEAttribute& attribute)
256262 start->read = bt_gatt_attr_read_service;
257263 start->user_data = attribute.uuid ();
258264
259- pr_info (LOG_MODULE_BLE, " service-%p" , start);
265+ pr_debug (LOG_MODULE_BLE, " service-%p" , start);
266+ start++;
267+ _attr_index++;
268+ }
269+ else if (BLETypeDescriptor == type)
270+ {
271+ start->uuid = attribute.uuid ();
272+ start->perm = BT_GATT_PERM_READ;
273+ start->read = profile_read_process;
274+ start->user_data = (void *)&attribute;
275+
276+ pr_debug (LOG_MODULE_BLE, " Descriptor-%p" , start);
260277 start++;
261278 _attr_index++;
262279 }
@@ -297,6 +314,7 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
297314 int err;
298315 int i;
299316 bool send_discover = false ;
317+ const struct bt_gatt_attr *attr_rsp = attr;
300318
301319 for (i = 0 ; i < _num_attributes; i++)
302320 {
@@ -311,37 +329,55 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
311329 case BT_GATT_DISCOVER_CHARACTERISTIC:
312330 {
313331 struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
314- attr_dec++;
315- attr_dec->handle = attr->handle + 1 ;
332+ if (NULL != attr_dec)
333+ {
334+ attr_dec++;
335+ attr_dec->handle = attr->handle + 1 ;
336+ }
316337 break ;
317338 }
318339 case BT_GATT_DISCOVER_DESCRIPTOR:
319340 {
320- BLECharacteristic *chrc = (BLECharacteristic *)attribute;
321- struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
322- struct bt_gatt_attr *attr_chrc = attr_dec + 1 ;
323- struct bt_gatt_attr *attr_cccd = attr_dec + 2 ;
324- struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams ();
325- struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
326- struct bt_conn *conn = bt_conn_lookup_addr_le (_peripheral->bt_le_address ());
327- if (NULL == conn)
341+ if (BLETypeCharacteristic == attribute->type ())
328342 {
329- // Link lost
330- return ;
343+ BLECharacteristic *chrc = (BLECharacteristic *)attribute;
344+ if (bt_uuid_cmp (chrc->getClientCharacteristicConfigUuid (), attr->uuid ) != 0 )
345+ {
346+ // Failed other descriptor. Not CCCD
347+ attr_rsp = NULL ;
348+ break ;
349+ }
350+ struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
351+ struct bt_gatt_attr *attr_chrc = attr_dec + 1 ;
352+ struct bt_gatt_attr *attr_cccd = attr_dec + 2 ;
353+ struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams ();
354+ struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
355+ struct bt_conn *conn = bt_conn_lookup_addr_le (_peripheral->bt_le_address ());
356+ if (NULL == conn)
357+ {
358+ // Link lost
359+ return ;
360+ }
361+
362+ _sub_param_idx++;
363+ attr_cccd->handle = attr->handle ;
364+ memcpy (sub_param, sub_param_tmp, sizeof (struct bt_gatt_subscribe_params ));
365+ sub_param->ccc_handle = attr_cccd->handle ;
366+ sub_param->value_handle = attr_chrc->handle ;
367+
368+ // Enable CCCD to allow peripheral send Notification/Indication
369+ err = bt_gatt_subscribe (conn, sub_param);
370+ bt_conn_unref (conn);
371+ if (err && err != -EALREADY)
372+ {
373+ pr_debug (LOG_MODULE_APP, " Subscribe failed (err %d)\n " , err);
374+ }
331375 }
332-
333- _sub_param_idx++;
334- attr_cccd->handle = attr->handle ;
335- memcpy (sub_param, sub_param_tmp, sizeof (struct bt_gatt_subscribe_params ));
336- sub_param->ccc_handle = attr_cccd->handle ;
337- sub_param->value_handle = attr_chrc->handle ;
338-
339- // Enable CCCD to allow peripheral send Notification/Indication
340- err = bt_gatt_subscribe (conn, sub_param);
341- bt_conn_unref (conn);
342- if (err && err != -EALREADY)
376+ else if (BLETypeDescriptor == attribute->type ())
343377 {
344- pr_debug (LOG_MODULE_APP, " Subscribe failed (err %d)\n " , err);
378+ struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
379+ struct bt_gatt_attr *attr_descriptor = attr_dec++; // The descriptor is separate
380+ attr_descriptor->handle = attr->handle ;
345381 }
346382 break ;
347383 }
@@ -353,7 +389,7 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
353389 }
354390 }
355391 }
356- attribute->discover (attr , &_discover_params);
392+ attribute->discover (attr_rsp , &_discover_params);
357393 break ;
358394 }
359395 }
0 commit comments