@@ -241,53 +241,56 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
241241 attribute = _attributes[i];
242242 if (attribute->discovering ())
243243 {
244- switch (_discover_params.type )
245- {
246- case BT_GATT_DISCOVER_CHARACTERISTIC:
247- {
248- struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
249- attr_dec++;
250- attr_dec->handle = attr->handle + 1 ;
251- break ;
252- }
253- case BT_GATT_DISCOVER_DESCRIPTOR:
254- {
255- BLECharacteristic *chrc = (BLECharacteristic *)attribute;
256- struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
257- struct bt_gatt_attr *attr_chrc = attr_dec + 1 ;
258- struct bt_gatt_attr *attr_cccd = attr_dec + 2 ;
259- struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams ();
260- struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
261- struct bt_conn *conn = bt_conn_lookup_addr_le (_peripheral->bt_le_address ());
262- if (NULL == conn)
263- {
264- // Link lost
265- return ;
266- }
267-
268- _sub_param_idx++;
269- attr_cccd->handle = attr->handle ;
270- memcpy (sub_param, sub_param_tmp, sizeof (struct bt_gatt_subscribe_params ));
271- sub_param->ccc_handle = attr_cccd->handle ;
272- sub_param->value_handle = attr_chrc->handle ;
273-
274- // Enable CCCD to allow peripheral send Notification/Indication
275- err = bt_gatt_subscribe (conn, sub_param);
276- bt_conn_unref (conn);
277- if (err && err != -EALREADY)
278- {
279- pr_debug (LOG_MODULE_APP, " Subscribe failed (err %d)\n " , err);
280- }
281- break ;
282- }
283- case BT_GATT_DISCOVER_PRIMARY:
284- default :
285- {
286- // Do nothing
287- break ;
288- }
289- }
290-
244+ if (NULL != attr)
245+ {
246+ // Discover success
247+ switch (_discover_params.type )
248+ {
249+ case BT_GATT_DISCOVER_CHARACTERISTIC:
250+ {
251+ struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
252+ attr_dec++;
253+ attr_dec->handle = attr->handle + 1 ;
254+ break ;
255+ }
256+ case BT_GATT_DISCOVER_DESCRIPTOR:
257+ {
258+ BLECharacteristic *chrc = (BLECharacteristic *)attribute;
259+ struct bt_gatt_attr *attr_dec = declarationAttr (attribute);
260+ struct bt_gatt_attr *attr_chrc = attr_dec + 1 ;
261+ struct bt_gatt_attr *attr_cccd = attr_dec + 2 ;
262+ struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams ();
263+ struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
264+ struct bt_conn *conn = bt_conn_lookup_addr_le (_peripheral->bt_le_address ());
265+ if (NULL == conn)
266+ {
267+ // Link lost
268+ return ;
269+ }
270+
271+ _sub_param_idx++;
272+ attr_cccd->handle = attr->handle ;
273+ memcpy (sub_param, sub_param_tmp, sizeof (struct bt_gatt_subscribe_params ));
274+ sub_param->ccc_handle = attr_cccd->handle ;
275+ sub_param->value_handle = attr_chrc->handle ;
276+
277+ // Enable CCCD to allow peripheral send Notification/Indication
278+ err = bt_gatt_subscribe (conn, sub_param);
279+ bt_conn_unref (conn);
280+ if (err && err != -EALREADY)
281+ {
282+ pr_debug (LOG_MODULE_APP, " Subscribe failed (err %d)\n " , err);
283+ }
284+ break ;
285+ }
286+ case BT_GATT_DISCOVER_PRIMARY:
287+ default :
288+ {
289+ // Do nothing
290+ break ;
291+ }
292+ }
293+ }
291294 attribute->discover (attr, &_discover_params);
292295 break ;
293296 }
0 commit comments