@@ -407,19 +407,10 @@ BLEDeviceManager::setAdvertiseData(uint8_t type, const uint8_t* data, uint8_t le
407407}
408408
409409BLE_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
518591BLE_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+
630713bool 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
648723bool 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