Skip to content

Commit 66ff71b

Browse files
committed
1. Add API to allow setup advertising after setup;
2. Add interface to set the device name;
1 parent 0f6e5b2 commit 66ff71b

File tree

9 files changed

+187
-45
lines changed

9 files changed

+187
-45
lines changed

libraries/CurieBLE/src/BLEPeripheral.cpp

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@ bool BLEPeripheral::begin()
5252
}
5353

5454
pr_info(LOG_MODULE_BLE, "%s: %d", __FUNCTION__, 2);
55-
56-
/* Populate advertising data
57-
*/
58-
_advDataInit();
59-
pr_info(LOG_MODULE_BLE, "%s: %d", __FUNCTION__, 3);
6055

61-
return (_startAdvertising() == BLE_STATUS_SUCCESS);
56+
return (startAdvertising() == BLE_STATUS_SUCCESS);
6257
}
6358

6459
void
@@ -154,26 +149,28 @@ void BLEPeripheral::addAttribute(BLEAttribute& attribute)
154149
}
155150

156151

157-
void
152+
BleStatus
158153
BLEPeripheral::_advDataInit(void)
159154
{
160-
uint8_t lengthTotal = 0;
155+
uint8_t lengthTotal = 2; // Flags data length
156+
_adv_data_idx = 0;
157+
161158
/* Add flags */
162159
_adv_type = (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR);
163-
_adv_data[_adv_data_idx].type = BT_DATA_FLAGS;
164-
_adv_data[_adv_data_idx].data = &_adv_type;
165-
_adv_data[_adv_data_idx].data_len = 1;
166-
_adv_data_idx++;
160+
_adv_data[_adv_data_idx].type = BT_DATA_FLAGS;
161+
_adv_data[_adv_data_idx].data = &_adv_type;
162+
_adv_data[_adv_data_idx].data_len = 1;
163+
_adv_data_idx++;
167164

168165
if (_advertise_service_uuid)
169166
{
170-
uint8_t type;
167+
uint8_t type;
171168
uint8_t length;
172169
uint8_t *data = NULL;
173170

174-
pr_info(LOG_MODULE_BLE, "ADV Type-%d", _advertise_service_uuid->type);
171+
pr_info(LOG_MODULE_BLE, "ADV Type-%d", _advertise_service_uuid->type);
175172
if (BT_UUID_TYPE_16 == _advertise_service_uuid->type)
176-
{
173+
{
177174
//UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
178175
data = (uint8_t *)&(((struct bt_uuid_16 *)_advertise_service_uuid)->val);
179176
length = sizeof(uint16_t);
@@ -187,22 +184,23 @@ BLEPeripheral::_advDataInit(void)
187184
}
188185
if (NULL != data)
189186
{
190-
_adv_data[_adv_data_idx].type = type;
191-
_adv_data[_adv_data_idx].data = data;
192-
_adv_data[_adv_data_idx].data_len = length;
193-
_adv_data_idx++;
187+
_adv_data[_adv_data_idx].type = type;
188+
_adv_data[_adv_data_idx].data = data;
189+
_adv_data[_adv_data_idx].data_len = length;
190+
_adv_data_idx++;
194191
lengthTotal += length;
195192

196-
pr_info(LOG_MODULE_BLE, "Service UUID Len -%d", length);
193+
pr_info(LOG_MODULE_BLE, "Service UUID Len -%d", length);
197194
}
198195
}
199196

200-
if (_local_name) {
197+
if (_local_name)
198+
{
201199
/* Add device name (truncated if too long) */
202-
_adv_data[_adv_data_idx].type = BT_DATA_NAME_COMPLETE;
203-
_adv_data[_adv_data_idx].data = (const uint8_t*)_local_name;
204-
_adv_data[_adv_data_idx].data_len = strlen(_local_name);
205-
_adv_data_idx++;
200+
_adv_data[_adv_data_idx].type = BT_DATA_NAME_COMPLETE;
201+
_adv_data[_adv_data_idx].data = (const uint8_t*)_local_name;
202+
_adv_data[_adv_data_idx].data_len = strlen(_local_name);
203+
_adv_data_idx++;
206204

207205
lengthTotal += strlen(_local_name);
208206
pr_info(LOG_MODULE_BLE, "Local Name -%s", _local_name);
@@ -216,39 +214,61 @@ BLEPeripheral::_advDataInit(void)
216214
/* A 128-bit Service Data UUID won't fit in an Advertising packet */
217215
if (BT_UUID_TYPE_16 != _service_data_uuid->type)
218216
{
219-
return; /* We support service data only for 16-bit service UUID */
217+
/* We support service data only for 16-bit service UUID */
218+
return BLE_STATUS_NOT_SUPPORTED;
220219
}
221220

222221
uint8_t block_len = sizeof(uint16_t) + _service_data_length;
223222
if (1 + block_len > BLE_MAX_ADV_SIZE)
224223
{
225-
return; // Service data block is too large.
224+
// Service data block is too large.
225+
return BLE_STATUS_ERROR_PARAMETER;
226226
}
227-
_adv_data[_adv_data_idx].type = BT_DATA_SVC_DATA16;
228-
_adv_data[_adv_data_idx].data = _service_data_buf;
229-
_adv_data[_adv_data_idx].data_len = block_len;
230-
_adv_data_idx++;
227+
228+
_adv_data[_adv_data_idx].type = BT_DATA_SVC_DATA16;
229+
_adv_data[_adv_data_idx].data = _service_data_buf;
230+
_adv_data[_adv_data_idx].data_len = block_len;
231+
_adv_data_idx++;
231232

232233
uint8_t *adv_tmp = _service_data_buf;
233234

234235
UINT16_TO_LESTREAM(adv_tmp, (((struct bt_uuid_16 *)_service_data_uuid)->val));
235236
memcpy(adv_tmp, _service_data, _service_data_length);
236-
237+
237238
lengthTotal += block_len;
238239
pr_info(LOG_MODULE_BLE, "SVC Len -%d", block_len);
239240
}
240-
pr_info(LOG_MODULE_BLE, "ADV lengthTotal -%d", lengthTotal);
241+
if (lengthTotal > BLE_MAX_ADV_SIZE)
242+
{
243+
pr_error(LOG_MODULE_BLE, "ADV Total length-%d", lengthTotal);
244+
// Service data block is too large.
245+
return BLE_STATUS_ERROR_PARAMETER;
246+
}
247+
return BLE_STATUS_SUCCESS;
241248
}
242249

243250
BleStatus
244-
BLEPeripheral::_startAdvertising()
251+
BLEPeripheral::startAdvertising()
245252
{
246253
BleStatus status = BLE_STATUS_SUCCESS;
247-
254+
status = _advDataInit();
255+
if (BLE_STATUS_SUCCESS != status)
256+
{
257+
return status;
258+
}
248259
status = BLEPeripheralRole::instance()->startAdvertising(_adv_data,
249-
_adv_data_idx,
250-
NULL,
251-
0);
260+
_adv_data_idx,
261+
NULL,
262+
0);
263+
return status;
264+
}
265+
266+
BleStatus
267+
BLEPeripheral::stopAdvertising()
268+
{
269+
BleStatus status = BLE_STATUS_SUCCESS;
270+
271+
status = BLEPeripheralRole::instance()->stopAdvertising();
252272
return status;
253273
}
254274

libraries/CurieBLE/src/BLEPeripheral.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,28 @@ class BLEPeripheral{
195195
*/
196196
bool connected(void);
197197

198+
/**
199+
* @brief Init the ADV data and start send advertisement
200+
*
201+
* @param none
202+
*
203+
* @return BleStatus 0 - Success. Others - error code
204+
*
205+
* @note none
206+
*/
207+
BleStatus startAdvertising(void);
208+
209+
/**
210+
* @brief Stop send advertisement
211+
*
212+
* @param none
213+
*
214+
* @return BleStatus 0 - Success. Others - error code
215+
*
216+
* @note none
217+
*/
218+
BleStatus stopAdvertising(void);
219+
198220
protected:
199221
void handleConnectEvent(struct bt_conn *conn, uint8_t err);
200222
void handleDisconnectEvent(struct bt_conn *conn, uint8_t reason);
@@ -205,10 +227,9 @@ class BLEPeripheral{
205227

206228
private:
207229

208-
BleStatus _startAdvertising(void);
209230
BleStatus _stop(void);
210231

211-
void _advDataInit(void);
232+
BleStatus _advDataInit(void);
212233

213234
private:
214235
const char* _local_name;

libraries/CurieBLE/src/BLEPeripheralRole.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ bool BLEPeripheralRole::begin()
6262
}
6363
_state = BLE_PERIPH_STATE_READY;
6464

65+
// Set device name
66+
setDeviceName();
6567
// Register profile
6668
_peripheral.registerProfile();
6769
delay(2); // Temp solution for send data fast will makes ADV data set failed
@@ -84,9 +86,17 @@ BLEPeripheralRole::setDeviceName(const char deviceName[])
8486
if (len > BLE_MAX_DEVICE_NAME)
8587
len = BLE_MAX_DEVICE_NAME;
8688
memcpy(_device_name, deviceName, len);
89+
setDeviceName();
8790
}
8891
}
8992

93+
void
94+
BLEPeripheralRole::setDeviceName()
95+
{
96+
int len = strlen(_device_name);
97+
bt_le_set_device_name(_device_name, len);
98+
}
99+
90100
void
91101
BLEPeripheralRole::setConnectionInterval(const unsigned short minConnInterval, const unsigned short maxConnInterval)
92102
{
@@ -153,11 +163,30 @@ void BLEPeripheralRole::addAttribute(BLEAttribute& attribute)
153163
_peripheral.addAttribute(attribute);
154164
}
155165

166+
BleStatus
167+
BLEPeripheralRole::stopAdvertising()
168+
{
169+
int err_code = 0;
170+
BleStatus status = BLE_STATUS_WRONG_STATE;
171+
172+
if (BLE_PERIPH_STATE_ADVERTISING == _state)
173+
{
174+
err_code = bt_le_adv_stop();
175+
status = errorno_to_ble_status(err_code);
176+
}
177+
178+
if (BLE_STATUS_SUCCESS != status)
179+
return status;
180+
181+
_state = BLE_PERIPH_STATE_READY;
182+
return BLE_STATUS_SUCCESS;
183+
}
184+
156185
BleStatus
157186
BLEPeripheralRole::startAdvertising(const struct bt_data *ad,
158187
size_t ad_len,
159-
const struct bt_data *sd,
160-
size_t sd_len)
188+
const struct bt_data *sd,
189+
size_t sd_len)
161190
{
162191
int ret;
163192

@@ -188,11 +217,13 @@ void BLEPeripheralRole::setAdvertisingParam(uint8_t type,
188217
BleStatus
189218
BLEPeripheralRole::stop(void)
190219
{
220+
int err_code;
191221
BleStatus status;
192222

193223
if (BLE_PERIPH_STATE_ADVERTISING == _state)
194224
{
195-
status = bt_le_adv_stop();// Fix me: posix to BLEStatus
225+
err_code = bt_le_adv_stop();
226+
status = errorno_to_ble_status(err_code);
196227
}
197228
else
198229
status = disconnect();

libraries/CurieBLE/src/BLEPeripheralRole.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,18 @@ class BLEPeripheralRole: public BLERoleBase{
139139
size_t ad_len,
140140
const struct bt_data *sd,
141141
size_t sd_len);
142-
142+
143+
/**
144+
* @brief Stop send advertisement
145+
*
146+
* @param none
147+
*
148+
* @return none
149+
*
150+
* @note none
151+
*/
152+
BleStatus stopAdvertising();
153+
143154
/**
144155
* @brief Set advertising parameter
145156
*
@@ -218,6 +229,15 @@ class BLEPeripheralRole: public BLERoleBase{
218229

219230
private:
220231

232+
/**
233+
* Set the device name to Nordic BLE's profile
234+
*
235+
* @param none
236+
*
237+
* @note none
238+
*/
239+
void setDeviceName();
240+
221241
enum BLEPeripheralState {
222242
BLE_PERIPH_STATE_NOT_READY = 0,
223243
BLE_PERIPH_STATE_READY,

libraries/CurieBLE/src/internal/ble_client.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828
* POSSIBILITY OF SUCH DAMAGE.
2929
*/
30+
31+
#include <errno.h>
3032

3133
#include <string.h>
3234
#include "cfw/cfw.h"
@@ -50,7 +52,6 @@
5052

5153
#include "infra/log.h"
5254

53-
5455
// APP callback
5556
static ble_client_connect_event_cb_t ble_client_connect_event_cb = NULL;
5657
static void *ble_client_connect_event_param;
@@ -197,6 +198,37 @@ void ble_client_init(ble_client_connect_event_cb_t connect_cb, void* connect_par
197198
return;
198199
}
199200

201+
BleStatus errorno_to_ble_status(int err)
202+
{
203+
BleStatus err_code;
204+
err = 0 - err;
205+
206+
switch(err) {
207+
case 0:
208+
err_code = BLE_STATUS_SUCCESS;
209+
break;
210+
case EIO:
211+
err_code = BLE_STATUS_WRONG_STATE;
212+
break;
213+
case EBUSY:
214+
err_code = BLE_STATUS_TIMEOUT;
215+
break;
216+
case EFBIG:
217+
case ENOTSUP:
218+
err_code = BLE_STATUS_NOT_SUPPORTED;
219+
break;
220+
case EPERM:
221+
case EACCES:
222+
err_code = BLE_STATUS_NOT_ALLOWED;
223+
break;
224+
case ENOMEM: // No memeory
225+
default:
226+
err_code = BLE_STATUS_ERROR;
227+
break;
228+
}
229+
return err_code;
230+
}
231+
200232

201233
#ifdef __cplusplus
202234
}

libraries/CurieBLE/src/internal/ble_client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ void ble_client_init(ble_client_connect_event_cb_t connect_cb, void* connect_par
106106
ble_client_update_param_event_cb_t update_param_cb, void* update_param_param);
107107
void ble_client_get_factory_config(bt_addr_le_t *bda, char *name);
108108
void ble_gap_set_tx_power(int8_t tx_power);
109+
BleStatus errorno_to_ble_status(int err);
110+
109111
#ifdef __cplusplus
110112
}
111113
#endif

system/libarc32_arduino101/drivers/bluetooth/bluetooth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ int bt_br_set_discoverable(bool enable);
344344
int bt_br_set_connectable(bool enable);
345345
#endif
346346

347+
void bt_le_set_device_name(char *device_name, int len);
348+
347349
#ifdef __cplusplus
348350
}
349351
#endif

0 commit comments

Comments
 (0)