@@ -407,19 +407,10 @@ BLEDeviceManager::setAdvertiseData(uint8_t type, const uint8_t* data, uint8_t le
407
407
}
408
408
409
409
BLE_STATUS_T
410
- BLEDeviceManager::_advDataInit ( void )
410
+ BLEDeviceManager::setAdvertiseSolicitService ( )
411
411
{
412
412
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)
423
414
{
424
415
uint8_t type;
425
416
uint8_t length;
@@ -441,9 +432,14 @@ BLEDeviceManager::_advDataInit(void)
441
432
442
433
ret = setAdvertiseData (type, data, length);
443
434
}
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)
447
443
{
448
444
uint8_t type;
449
445
uint8_t length;
@@ -464,26 +460,43 @@ BLEDeviceManager::_advDataInit(void)
464
460
}
465
461
ret = setAdvertiseData (type, data, length);
466
462
}
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 )
470
472
{
471
473
ret = setAdvertiseData (BT_DATA_MANUFACTURER_DATA,
472
474
_manufacturer_data,
473
475
_manufacturer_data_length);
474
476
}
477
+ return ret;
478
+ }
475
479
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 )
478
485
{
479
486
uint8_t length = _local_name.length ();
480
487
ret = setAdvertiseData (BT_DATA_NAME_COMPLETE,
481
488
(const uint8_t *)_local_name.c_str (),
482
489
length);
483
490
}
484
491
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 )
487
500
{
488
501
/* Add Service Data (if it will fit) */
489
502
@@ -511,27 +524,93 @@ BLEDeviceManager::_advDataInit(void)
511
524
adv_tmp += 2 ;
512
525
memcpy (adv_tmp, _service_data, _service_data_length);
513
526
}
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
+ }
514
583
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);
515
588
return ret;
516
589
}
517
590
518
591
BLE_STATUS_T BLEDeviceManager::startAdvertising ()
519
592
{
520
593
int ret;
521
594
BLE_STATUS_T status;
595
+ bt_data_t * scan_rsp_data = NULL ;
522
596
status = _advDataInit ();
523
597
if (BLE_STATUS_SUCCESS != status)
524
598
{
525
599
return status;
526
600
}
527
601
528
- pr_info (LOG_MODULE_BLE, " %s-ad_len%d" , __FUNCTION__, _adv_data_idx);
529
602
if (_state != BLE_PERIPH_STATE_READY)
530
603
return BLE_STATUS_WRONG_STATE;
531
604
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
+
532
611
ret = bt_le_adv_start (&_adv_param,
533
612
_adv_data, _adv_data_idx,
534
- _scan_rsp_data , _scan_rsp_data_idx);
613
+ scan_rsp_data , _scan_rsp_data_idx);
535
614
if (0 != ret)
536
615
{
537
616
pr_error (LOG_MODULE_APP, " [ADV] Start failed. Error: %d" , ret);
@@ -611,11 +690,9 @@ void BLEDeviceManager::_clearAdvertiseBuffer()
611
690
612
691
}
613
692
614
- bool BLEDeviceManager::startScanningWithDuplicates ()
693
+ bool BLEDeviceManager::startScaning ()
615
694
{
616
- _adv_duplicate_filter_enabled = false ;
617
695
_scan_param.filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_ENABLE;
618
-
619
696
_clearAdvertiseBuffer ();
620
697
621
698
int err = bt_le_scan_start (&_scan_param, ble_central_device_found);
@@ -627,29 +704,27 @@ bool BLEDeviceManager::startScanningWithDuplicates()
627
704
return true ;
628
705
}
629
706
707
+ bool BLEDeviceManager::startScanningWithDuplicates ()
708
+ {
709
+ _adv_duplicate_filter_enabled = false ;
710
+ return startScaning ();
711
+ }
712
+
630
713
bool BLEDeviceManager::startScanningNewPeripherals ()
631
714
{
715
+ // Clear the filter old buffer
632
716
_adv_duplicate_filter_enabled = true ;
633
- memset (_peer_duplicate_address_buffer, 0 , sizeof (_peer_duplicate_address_buffer));
634
717
_duplicate_filter_header = _duplicate_filter_tail = 0 ;
718
+ memset (_peer_duplicate_address_buffer, 0 , sizeof (_peer_duplicate_address_buffer));
635
719
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 ();
646
721
}
647
722
648
723
bool BLEDeviceManager::stopScanning ()
649
724
{
650
725
int err = bt_le_scan_stop ();
651
726
652
- if (err) // Sid. TODO: KW detected bt_le_scan_stop return only 0.
727
+ if (err)
653
728
{
654
729
pr_info (LOG_MODULE_BLE, " Stop LE scan failed (err %d)\n " , err);
655
730
return false ;
@@ -765,17 +840,21 @@ bool BLEDeviceManager::hasLocalName(const BLEDevice* device) const
765
840
766
841
const uint8_t * local_name = NULL ;
767
842
uint8_t local_name_len = 0 ;
843
+
844
+ // Get local name
768
845
bool retval = getDataFromAdvertiseByType (device,
769
846
BT_DATA_NAME_COMPLETE,
770
847
local_name,
771
848
local_name_len);
772
- if (false == retval)
849
+ if (true == retval)
773
850
{
774
- retval = getDataFromAdvertiseByType (device,
775
- BT_DATA_NAME_SHORTENED,
776
- local_name,
777
- local_name_len);
851
+ return true ;
778
852
}
853
+ // Get shorten name
854
+ retval = getDataFromAdvertiseByType (device,
855
+ BT_DATA_NAME_SHORTENED,
856
+ local_name,
857
+ local_name_len);
779
858
return retval;
780
859
}
781
860
@@ -936,13 +1015,13 @@ int BLEDeviceManager::advertisedServiceUuidCount(const BLEDevice* device) const
936
1015
return service_cnt;
937
1016
}
938
1017
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. */
942
1021
if (type == BT_DATA_UUID16_ALL ||
943
1022
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)
946
1025
{
947
1026
service_cnt++;
948
1027
}
@@ -959,7 +1038,7 @@ String BLEDeviceManager::localName(const BLEDevice* device) const
959
1038
{
960
1039
return _local_name;
961
1040
}
962
-
1041
+
963
1042
const uint8_t * local_name = NULL ;
964
1043
uint8_t local_name_len = 0 ;
965
1044
String temp (" " );
@@ -1037,8 +1116,8 @@ String BLEDeviceManager::advertisedServiceUuid(const BLEDevice* device, int inde
1037
1116
1038
1117
if (type == BT_DATA_UUID16_ALL ||
1039
1118
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)
1042
1121
{
1043
1122
service_cnt++;
1044
1123
}
0 commit comments