Skip to content

Commit db8f658

Browse files
committed
1. Add set advertising paramter interface
2. Fix a bug when discovery failed
1 parent 84105d2 commit db8f658

File tree

3 files changed

+85
-59
lines changed

3 files changed

+85
-59
lines changed

libraries/CurieBLE/src/BLEPeripheral.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ BLEPeripheral::BLEPeripheral(void) :
2828
_adv_data_idx(0)
2929
{
3030
memset(_adv_data, 0x00, sizeof(_adv_data));
31+
32+
// Default Advertising parameter
33+
setAdvertisingParam(BT_LE_ADV_IND ,
34+
0xA0,
35+
0xF0);
3136
}
3237

3338
BLEPeripheral::~BLEPeripheral(void)
@@ -91,6 +96,15 @@ BLEPeripheral::setAdvertisedServiceData(const struct bt_uuid* serviceDataUuid,
9196
_service_data_length = serviceDataLength;
9297
}
9398

99+
void BLEPeripheral::setAdvertisingParam(uint8_t type,
100+
uint16_t interval_min,
101+
uint16_t interval_max)
102+
{
103+
BLEPeripheralRole::instance()->setAdvertisingParam(type,
104+
interval_min,
105+
interval_max);
106+
}
107+
94108
void
95109
BLEPeripheral::setDeviceName(const char deviceName[])
96110
{
@@ -189,10 +203,10 @@ BLEPeripheral::_advDataInit(void)
189203
_adv_data[_adv_data_idx].data = (const uint8_t*)_local_name;
190204
_adv_data[_adv_data_idx].data_len = strlen(_local_name);
191205
_adv_data_idx++;
192-
lengthTotal += _adv_data[_adv_data_idx].data_len;
193-
194-
pr_info(LOG_MODULE_BLE, "Local Name -%s", _local_name);
195-
pr_info(LOG_MODULE_BLE, "Local Name Len -%d", strlen(_local_name));
206+
207+
lengthTotal += strlen(_local_name);
208+
pr_info(LOG_MODULE_BLE, "Local Name -%s", _local_name);
209+
pr_info(LOG_MODULE_BLE, "Local Name Len -%d", strlen(_local_name));
196210
}
197211

198212
if (_service_data)
@@ -219,9 +233,9 @@ BLEPeripheral::_advDataInit(void)
219233

220234
UINT16_TO_LESTREAM(adv_tmp, (((struct bt_uuid_16 *)_service_data_uuid)->val));
221235
memcpy(adv_tmp, _service_data, _service_data_length);
222-
lengthTotal += block_len;
223-
224-
pr_info(LOG_MODULE_BLE, "SVC Len -%d", block_len);
236+
237+
lengthTotal += block_len;
238+
pr_info(LOG_MODULE_BLE, "SVC Len -%d", block_len);
225239
}
226240
pr_info(LOG_MODULE_BLE, "ADV lengthTotal -%d", lengthTotal);
227241
}
@@ -230,10 +244,6 @@ BleStatus
230244
BLEPeripheral::_startAdvertising()
231245
{
232246
BleStatus status = BLE_STATUS_SUCCESS;
233-
234-
BLEPeripheralRole::instance()->setAdvertisingParam(BT_LE_ADV_IND ,
235-
0xA0,
236-
0xF0);
237247

238248
status = BLEPeripheralRole::instance()->startAdvertising(_adv_data,
239249
_adv_data_idx,

libraries/CurieBLE/src/BLEPeripheral.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,20 @@ class BLEPeripheral{
8888
void setAdvertisedServiceData(const struct bt_uuid* serviceDataUuid,
8989
uint8_t* serviceData,
9090
uint8_t serviceDataLength);
91-
91+
/**
92+
* Set the ADV parameters about the ADV-Type and interval
93+
*
94+
* @param type Advertising types
95+
*
96+
* @param interval_min Minimum Advertising Interval (N * 0.625)
97+
*
98+
* @param interval_max Maximum Advertising Interval (N * 0.625)
99+
*
100+
* @note none
101+
*/
102+
void setAdvertisingParam(uint8_t type,
103+
uint16_t interval_min,
104+
uint16_t interval_max);
92105
/**
93106
* Set the device name for the BLE Peripheral Device
94107
*

libraries/CurieBLE/src/BLEProfile.cpp

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)