Skip to content

Commit efdbe66

Browse files
committed
Refact the code to make it more readable
1 parent 9455e44 commit efdbe66

File tree

3 files changed

+146
-61
lines changed

3 files changed

+146
-61
lines changed

libraries/CurieBLE/src/internal/BLECallbacks.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,11 @@ uint8_t profile_notify_process (bt_conn_t *conn,
129129
bt_gatt_subscribe_params_t *params,
130130
const void *data, uint16_t length)
131131
{
132-
//BLEPeripheralHelper* peripheral = BLECentralRole::instance()->peripheral(conn);// Find peripheral by bt_conn
133-
//BLEAttribute* notifyatt = peripheral->attribute(params); // Find attribute by params
134132
BLECharacteristicImp* chrc = NULL;
135133
BLEDevice bleDevice(bt_conn_get_dst(conn));
136134
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);
137135

138-
//assert(notifyatt->type() == BLETypeCharacteristic);
139-
pr_debug(LOG_MODULE_APP, "%s1", __FUNCTION__);
136+
pr_debug(LOG_MODULE_APP, "%s-%d", __FUNCTION__, __LINE__);
140137
if (NULL != chrc)
141138
{
142139
chrc->setValue((const unsigned char *)data, length);
@@ -152,7 +149,6 @@ uint8_t profile_discover_process(bt_conn_t *conn,
152149
uint8_t ret = BT_GATT_ITER_STOP;
153150
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
154151
ret = BLEProfileManager::instance()->discoverResponseProc(conn, attr, params);
155-
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
156152
return ret;
157153
}
158154

@@ -166,6 +162,7 @@ uint8_t profile_read_rsp_process(bt_conn_t *conn,
166162
BLECharacteristicImp *chrc = NULL;
167163
BLEDevice bleDevice(bt_conn_get_dst(conn));
168164

165+
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
169166
// Get characteristic by handle params->single.handle
170167
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->single.handle);
171168

@@ -188,15 +185,14 @@ uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
188185
BLEDescriptorImp *descriptor = NULL;
189186
BLEDevice bleDevice(bt_conn_get_dst(conn));
190187

188+
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
191189
// Get characteristic by handle params->single.handle
192190
descriptor = BLEProfileManager::instance()->descriptor(bleDevice, params->single.handle);
193191

194-
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
195192
if (descriptor)
196193
{
197194
descriptor->writeValue((const unsigned char *)data, length, params->single.offset);
198195
}
199-
//pr_debug(LOG_MODULE_BLE, "%s-%d: desc len-%d", __FUNCTION__, __LINE__, descriptor->valueLength());
200196
return BT_GATT_ITER_STOP;
201197
}
202198

@@ -269,13 +265,14 @@ void ble_central_device_found(const bt_addr_le_t *addr,
269265
uint8_t len)
270266
{
271267
//char dev[BT_ADDR_LE_STR_LEN];
272-
273268
//bt_addr_le_to_str(addr, dev, sizeof(dev));
274269
//pr_debug(LOG_MODULE_BLE, "[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",
275270
// dev, type, len, rssi);
271+
//pr_debug(LOG_MODULE_BLE, " AD evt type %u, AD data len %u, RSSI %i\n",
272+
// type, len, rssi);
276273

277274
BLEDeviceManager::instance()->handleDeviceFound(addr, rssi, type,
278-
ad, len);
275+
ad, len);
279276
}
280277

281278
void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
@@ -288,7 +285,8 @@ void prfile_cccd_cfg_changed(void *user_data, uint16_t value)
288285
{
289286
if (NULL == user_data)
290287
return;
291-
pr_info(LOG_MODULE_BLE, "%s-%d: ccc userdata %p", __FUNCTION__, __LINE__, user_data);
288+
pr_debug(LOG_MODULE_BLE, "%s-%d: ccc userdata %p", __FUNCTION__, __LINE__, user_data);
289+
292290
BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp *)user_data;
293291
blecharacteritic->cccdValueChanged();
294292
}

libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

Lines changed: 130 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,10 @@ BLEDeviceManager::setAdvertiseData(uint8_t type, const uint8_t* data, uint8_t le
407407
}
408408

409409
BLE_STATUS_T
410-
BLEDeviceManager::_advDataInit(void)
410+
BLEDeviceManager::setAdvertiseSolicitService()
411411
{
412412
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
413-
// Clear the indexs
414-
_adv_data_idx = 0;
415-
_scan_rsp_data_idx = 0;
416-
417-
/* Add flags */
418-
_adv_type = (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR);
419-
ret = setAdvertiseData (BT_DATA_FLAGS, &_adv_type, sizeof(_adv_type));
420-
421-
if (_has_service_solicit_uuid &&
422-
(BLE_STATUS_SUCCESS == ret))
413+
if (_has_service_solicit_uuid)
423414
{
424415
uint8_t type;
425416
uint8_t length;
@@ -441,9 +432,14 @@ BLEDeviceManager::_advDataInit(void)
441432

442433
ret = setAdvertiseData(type, data, length);
443434
}
444-
445-
if (_has_service_uuid &&
446-
(BLE_STATUS_SUCCESS == ret))
435+
return ret;
436+
}
437+
438+
BLE_STATUS_T
439+
BLEDeviceManager::setAdvertiseService()
440+
{
441+
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
442+
if (_has_service_uuid)
447443
{
448444
uint8_t type;
449445
uint8_t length;
@@ -464,26 +460,43 @@ BLEDeviceManager::_advDataInit(void)
464460
}
465461
ret = setAdvertiseData(type, data, length);
466462
}
467-
468-
if (_manufacturer_data_length > 0 &&
469-
(BLE_STATUS_SUCCESS == ret))
463+
return ret;
464+
}
465+
466+
BLE_STATUS_T
467+
BLEDeviceManager::setAdvertiseManufacturerData()
468+
{
469+
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
470+
471+
if (_manufacturer_data_length > 0)
470472
{
471473
ret = setAdvertiseData (BT_DATA_MANUFACTURER_DATA,
472474
_manufacturer_data,
473475
_manufacturer_data_length);
474476
}
477+
return ret;
478+
}
475479

476-
if (_local_name.length() > 0 &&
477-
(BLE_STATUS_SUCCESS == ret))
480+
BLE_STATUS_T
481+
BLEDeviceManager::setAdvertiseLocalName()
482+
{
483+
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
484+
if (_local_name.length() > 0)
478485
{
479486
uint8_t length = _local_name.length();
480487
ret = setAdvertiseData (BT_DATA_NAME_COMPLETE,
481488
(const uint8_t*)_local_name.c_str(),
482489
length);
483490
}
484491

485-
if (_service_data_length > 0 &&
486-
(BLE_STATUS_SUCCESS == ret))
492+
return ret;
493+
}
494+
495+
BLE_STATUS_T
496+
BLEDeviceManager::setAdvertiseServiceData()
497+
{
498+
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
499+
if (_service_data_length > 0)
487500
{
488501
/* Add Service Data (if it will fit) */
489502

@@ -511,27 +524,93 @@ BLEDeviceManager::_advDataInit(void)
511524
adv_tmp += 2;
512525
memcpy(adv_tmp, _service_data, _service_data_length);
513526
}
527+
return ret;
528+
}
529+
530+
BLE_STATUS_T
531+
BLEDeviceManager::setAdvertiseFlagData()
532+
{
533+
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
534+
/* Add flags */
535+
_adv_type = (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR);
536+
ret = setAdvertiseData (BT_DATA_FLAGS, &_adv_type, sizeof(_adv_type));
537+
return ret;
538+
}
539+
540+
void BLEDeviceManager::clearPeripheralAdvertiseData()
541+
{
542+
// Clear the indexs
543+
_adv_data_idx = 0;
544+
_scan_rsp_data_idx = 0;
545+
}
546+
547+
BLE_STATUS_T
548+
BLEDeviceManager::_advDataInit(void)
549+
{
550+
BLE_STATUS_T ret = BLE_STATUS_SUCCESS;
551+
552+
clearPeripheralAdvertiseData();
553+
554+
ret = setAdvertiseFlagData();
555+
if (BLE_STATUS_SUCCESS != ret)
556+
{
557+
return ret;
558+
}
559+
560+
ret = setAdvertiseSolicitService();
561+
if (BLE_STATUS_SUCCESS != ret)
562+
{
563+
return ret;
564+
}
565+
566+
ret = setAdvertiseService();
567+
if (BLE_STATUS_SUCCESS != ret)
568+
{
569+
return ret;
570+
}
571+
572+
ret = setAdvertiseManufacturerData();
573+
if (BLE_STATUS_SUCCESS != ret)
574+
{
575+
return ret;
576+
}
577+
578+
ret = setAdvertiseLocalName();
579+
if (BLE_STATUS_SUCCESS != ret)
580+
{
581+
return ret;
582+
}
514583

584+
ret = setAdvertiseServiceData();
585+
586+
pr_debug(LOG_MODULE_BLE, "%s-ad_len:%d, scanrsp_len:%d",
587+
__FUNCTION__, _adv_data_idx, _scan_rsp_data_idx);
515588
return ret;
516589
}
517590

518591
BLE_STATUS_T BLEDeviceManager::startAdvertising()
519592
{
520593
int ret;
521594
BLE_STATUS_T status;
595+
bt_data_t* scan_rsp_data = NULL;
522596
status = _advDataInit();
523597
if (BLE_STATUS_SUCCESS != status)
524598
{
525599
return status;
526600
}
527601

528-
pr_info(LOG_MODULE_BLE, "%s-ad_len%d", __FUNCTION__, _adv_data_idx);
529602
if (_state != BLE_PERIPH_STATE_READY)
530603
return BLE_STATUS_WRONG_STATE;
531604

605+
// The V4.2 stack used the pointer to set the ADV type
606+
if (_scan_rsp_data_idx > 0)
607+
{
608+
scan_rsp_data = _scan_rsp_data;
609+
}
610+
532611
ret = bt_le_adv_start(&_adv_param,
533612
_adv_data, _adv_data_idx,
534-
_scan_rsp_data, _scan_rsp_data_idx);
613+
scan_rsp_data, _scan_rsp_data_idx);
535614
if (0 != ret)
536615
{
537616
pr_error(LOG_MODULE_APP, "[ADV] Start failed. Error: %d", ret);
@@ -611,11 +690,9 @@ void BLEDeviceManager::_clearAdvertiseBuffer()
611690

612691
}
613692

614-
bool BLEDeviceManager::startScanningWithDuplicates()
693+
bool BLEDeviceManager::startScaning()
615694
{
616-
_adv_duplicate_filter_enabled = false;
617695
_scan_param.filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_ENABLE;
618-
619696
_clearAdvertiseBuffer();
620697

621698
int err = bt_le_scan_start(&_scan_param, ble_central_device_found);
@@ -627,29 +704,27 @@ bool BLEDeviceManager::startScanningWithDuplicates()
627704
return true;
628705
}
629706

707+
bool BLEDeviceManager::startScanningWithDuplicates()
708+
{
709+
_adv_duplicate_filter_enabled = false;
710+
return startScaning();
711+
}
712+
630713
bool BLEDeviceManager::startScanningNewPeripherals()
631714
{
715+
// Clear the filter old buffer
632716
_adv_duplicate_filter_enabled = true;
633-
memset(_peer_duplicate_address_buffer, 0, sizeof(_peer_duplicate_address_buffer));
634717
_duplicate_filter_header = _duplicate_filter_tail = 0;
718+
memset(_peer_duplicate_address_buffer, 0, sizeof(_peer_duplicate_address_buffer));
635719

636-
_clearAdvertiseBuffer();
637-
638-
_scan_param.filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_ENABLE;
639-
int err = bt_le_scan_start(&_scan_param, ble_central_device_found);
640-
if (err)
641-
{
642-
pr_info(LOG_MODULE_BLE, "Scanning failed to start (err %d)\n", err);
643-
return false;
644-
}
645-
return true;
720+
return startScaning();
646721
}
647722

648723
bool BLEDeviceManager::stopScanning()
649724
{
650725
int err = bt_le_scan_stop();
651726

652-
if (err) // Sid. TODO: KW detected bt_le_scan_stop return only 0.
727+
if (err)
653728
{
654729
pr_info(LOG_MODULE_BLE, "Stop LE scan failed (err %d)\n", err);
655730
return false;
@@ -765,17 +840,21 @@ bool BLEDeviceManager::hasLocalName(const BLEDevice* device) const
765840

766841
const uint8_t* local_name = NULL;
767842
uint8_t local_name_len = 0;
843+
844+
// Get local name
768845
bool retval = getDataFromAdvertiseByType(device,
769846
BT_DATA_NAME_COMPLETE,
770847
local_name,
771848
local_name_len);
772-
if (false == retval)
849+
if (true == retval)
773850
{
774-
retval = getDataFromAdvertiseByType(device,
775-
BT_DATA_NAME_SHORTENED,
776-
local_name,
777-
local_name_len);
851+
return true;
778852
}
853+
// Get shorten name
854+
retval = getDataFromAdvertiseByType(device,
855+
BT_DATA_NAME_SHORTENED,
856+
local_name,
857+
local_name_len);
779858
return retval;
780859
}
781860

@@ -936,13 +1015,13 @@ int BLEDeviceManager::advertisedServiceUuidCount(const BLEDevice* device) const
9361015
return service_cnt;
9371016
}
9381017

939-
/* Sid, 2/15/2017. Sandeep reported that Apple devices may use
940-
BT_DATA_UUID16_SOME and BT_DATA_UUID128_SOME in addition to ALL.
941-
Practically, these types are same as ALL. */
1018+
/* Sid, 2/15/2017. Sandeep reported that Apple devices may use
1019+
BT_DATA_UUID16_SOME and BT_DATA_UUID128_SOME in addition to ALL.
1020+
Practically, these types are same as ALL. */
9421021
if (type == BT_DATA_UUID16_ALL ||
9431022
type == BT_DATA_UUID128_ALL ||
944-
type == BT_DATA_UUID16_SOME ||
945-
type == BT_DATA_UUID128_SOME)
1023+
type == BT_DATA_UUID16_SOME ||
1024+
type == BT_DATA_UUID128_SOME)
9461025
{
9471026
service_cnt++;
9481027
}
@@ -959,7 +1038,7 @@ String BLEDeviceManager::localName(const BLEDevice* device) const
9591038
{
9601039
return _local_name;
9611040
}
962-
1041+
9631042
const uint8_t* local_name = NULL;
9641043
uint8_t local_name_len = 0;
9651044
String temp("");
@@ -1037,8 +1116,8 @@ String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int inde
10371116

10381117
if (type == BT_DATA_UUID16_ALL ||
10391118
type == BT_DATA_UUID128_ALL ||
1040-
type == BT_DATA_UUID16_SOME ||
1041-
type == BT_DATA_UUID128_SOME)
1119+
type == BT_DATA_UUID16_SOME ||
1120+
type == BT_DATA_UUID128_SOME)
10421121
{
10431122
service_cnt++;
10441123
}

0 commit comments

Comments
 (0)