34
34
#include < lib/support/CodeUtils.h>
35
35
#include < lib/support/logging/CHIPLogging.h>
36
36
#include < platform/internal/BLEManager.h>
37
+ #include < setup_payload/AdditionalDataPayloadGenerator.h>
37
38
#include < system/SystemTimer.h>
38
39
39
40
#include " esp_log.h"
@@ -67,29 +68,26 @@ struct ESP32ChipServiceData
67
68
ChipBLEDeviceIdentificationInfo DeviceIdInfo;
68
69
};
69
70
70
- const ble_uuid128_t UUID_CHIPoBLEService = {
71
- BLE_UUID_TYPE_128, { 0xFB , 0x34 , 0x9B , 0x5F , 0x80 , 0x00 , 0x00 , 0x80 , 0x00 , 0x10 , 0x00 , 0x00 , 0xF6 , 0xFF , 0x00 , 0x00 }
72
- };
73
71
const ble_uuid16_t ShortUUID_CHIPoBLEService = { BLE_UUID_TYPE_16, 0xFFF6 };
74
72
75
73
const ble_uuid128_t UUID128_CHIPoBLEChar_RX = {
76
74
BLE_UUID_TYPE_128, { 0x11 , 0x9D , 0x9F , 0x42 , 0x9C , 0x4F , 0x9F , 0x95 , 0x59 , 0x45 , 0x3D , 0x26 , 0xF5 , 0x2E , 0xEE , 0x18 }
77
75
};
78
76
const ChipBleUUID chipUUID_CHIPoBLEChar_RX = { { 0x18 , 0xEE , 0x2E , 0xF5 , 0x26 , 0x3D , 0x45 , 0x59 , 0x95 , 0x9F , 0x4F , 0x9C , 0x42 , 0x9F ,
79
77
0x9D , 0x11 } };
80
- const ble_uuid128_t UUID_CHIPoBLEChar_RX = {
81
- { BLE_UUID_TYPE_128 }, { 0x18 , 0xEE , 0x2E , 0xF5 , 0x26 , 0x3D , 0x45 , 0x59 , 0x95 , 0x9F , 0x4F , 0x9C , 0x42 , 0x9F , 0x9D , 0x11 }
82
- };
83
78
84
- const ble_uuid128_t UUID128_CHIPoBLEChar_TX = {
85
- BLE_UUID_TYPE_128, { 0x12 , 0x9D , 0x9F , 0x42 , 0x9C , 0x4F , 0x9F , 0x95 , 0x59 , 0x45 , 0x3D , 0x26 , 0xF5 , 0x2E , 0xEE , 0x18 }
86
- };
87
79
const ChipBleUUID chipUUID_CHIPoBLEChar_TX = { { 0x18 , 0xEE , 0x2E , 0xF5 , 0x26 , 0x3D , 0x45 , 0x59 , 0x95 , 0x9F , 0x4F , 0x9C , 0x42 , 0x9F ,
88
80
0x9D , 0x12 } };
89
81
const ble_uuid128_t UUID_CHIPoBLEChar_TX = {
90
82
{ BLE_UUID_TYPE_128 }, { 0x12 , 0x9D , 0x9F , 0x42 , 0x9C , 0x4F , 0x9F , 0x95 , 0x59 , 0x45 , 0x3D , 0x26 , 0xF5 , 0x2E , 0xEE , 0x18 }
91
83
};
92
84
85
+ #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
86
+ const ble_uuid128_t UUID_CHIPoBLEChar_C3 = {
87
+ { BLE_UUID_TYPE_128 }, { 0x04 , 0x8F , 0x21 , 0x83 , 0x8A , 0x74 , 0x7D , 0xB8 , 0xF2 , 0x45 , 0x72 , 0x87 , 0x38 , 0x02 , 0x63 , 0x64 }
88
+ };
89
+ #endif
90
+
93
91
SemaphoreHandle_t semaphoreHandle = NULL ;
94
92
95
93
} // unnamed namespace
@@ -115,6 +113,14 @@ const struct ble_gatt_svc_def BLEManagerImpl::CHIPoBLEGATTAttrs[] = {
115
113
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
116
114
.val_handle = &sInstance .mTXCharCCCDAttrHandle ,
117
115
},
116
+ #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
117
+ {
118
+ .uuid = (ble_uuid_t *) (&UUID_CHIPoBLEChar_C3),
119
+ .access_cb = gatt_svr_chr_access_additional_data,
120
+ .flags = BLE_GATT_CHR_F_READ,
121
+ .val_handle = &sInstance .mC3CharAttrHandle ,
122
+ },
123
+ #endif
118
124
{
119
125
0 , /* No more characteristics in this service */
120
126
},
@@ -133,7 +139,10 @@ CHIP_ERROR BLEManagerImpl::_Init()
133
139
err = BleLayer::Init (this , this , &DeviceLayer::SystemLayer ());
134
140
SuccessOrExit (err);
135
141
136
- mRXCharAttrHandle = 0 ;
142
+ mRXCharAttrHandle = 0 ;
143
+ #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
144
+ mC3CharAttrHandle = 0 ;
145
+ #endif
137
146
mTXCharCCCDAttrHandle = 0 ;
138
147
mFlags .ClearAll ().Set (Flags::kAdvertisingEnabled , CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
139
148
mFlags .Set (Flags::kFastAdvertisingEnabled , true );
@@ -683,6 +692,10 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
683
692
uint8_t advData[MAX_ADV_DATA_LEN];
684
693
uint8_t index = 0 ;
685
694
695
+ constexpr uint8_t kServiceDataTypeSize = 1 ;
696
+
697
+ chip::Ble::ChipBLEDeviceIdentificationInfo deviceIdInfo;
698
+
686
699
// If a custom device name has not been specified, generate a CHIP-standard name based on the
687
700
// bottom digits of the Chip device id.
688
701
uint16_t discriminator;
@@ -706,19 +719,22 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
706
719
advData[index ++] = 0x02 ; // length
707
720
advData[index ++] = CHIP_ADV_DATA_TYPE_FLAGS; // AD type : flags
708
721
advData[index ++] = CHIP_ADV_DATA_FLAGS; // AD value
709
- advData[index ++] = 0x0A ; // length
722
+ advData[index ++] = kServiceDataTypeSize + sizeof (ESP32ChipServiceData); // length
710
723
advData[index ++] = CHIP_ADV_DATA_TYPE_SERVICE_DATA; // AD type: (Service Data - 16-bit UUID)
711
724
advData[index ++] = static_cast <uint8_t >(ShortUUID_CHIPoBLEService.value & 0xFF ); // AD value
712
725
advData[index ++] = static_cast <uint8_t >((ShortUUID_CHIPoBLEService.value >> 8 ) & 0xFF ); // AD value
713
726
714
- chip::Ble::ChipBLEDeviceIdentificationInfo deviceIdInfo;
715
727
err = ConfigurationMgr ().GetBLEDeviceIdentificationInfo (deviceIdInfo);
716
728
if (err != CHIP_NO_ERROR)
717
729
{
718
730
ChipLogError (DeviceLayer, " GetBLEDeviceIdentificationInfo(): %s" , ErrorStr (err));
719
731
ExitNow ();
720
732
}
721
733
734
+ #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
735
+ deviceIdInfo.SetAdditionalDataFlag (true );
736
+ #endif
737
+
722
738
VerifyOrExit (index + sizeof (deviceIdInfo) <= sizeof (advData), err = CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG);
723
739
memcpy (&advData[index ], &deviceIdInfo, sizeof (deviceIdInfo));
724
740
index = static_cast <uint8_t >(index + sizeof (deviceIdInfo));
@@ -1046,6 +1062,69 @@ int BLEManagerImpl::ble_svr_gap_event(struct ble_gap_event * event, void * arg)
1046
1062
return err.AsInteger ();
1047
1063
}
1048
1064
1065
+ #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
1066
+ void BLEManagerImpl::HandleC3CharRead (struct ble_gatt_char_context * param)
1067
+ {
1068
+ CHIP_ERROR err = CHIP_NO_ERROR;
1069
+ chip::System::PacketBufferHandle bufferHandle;
1070
+
1071
+ char serialNumber[ConfigurationManager::kMaxSerialNumberLength + 1 ];
1072
+ uint16_t lifetimeCounter = 0 ;
1073
+ BitFlags<AdditionalDataFields> additionalDataFields;
1074
+
1075
+ #if CHIP_ENABLE_ROTATING_DEVICE_ID
1076
+ err = ConfigurationMgr ().GetSerialNumber (serialNumber, sizeof (serialNumber));
1077
+ SuccessOrExit (err);
1078
+ err = ConfigurationMgr ().GetLifetimeCounter (lifetimeCounter);
1079
+ SuccessOrExit (err);
1080
+
1081
+ additionalDataFields.Set (AdditionalDataFields::RotatingDeviceId);
1082
+ #endif /* CHIP_ENABLE_ROTATING_DEVICE_ID */
1083
+
1084
+ err = AdditionalDataPayloadGenerator ().generateAdditionalDataPayload (lifetimeCounter, serialNumber, strlen (serialNumber),
1085
+ bufferHandle, additionalDataFields);
1086
+ SuccessOrExit (err);
1087
+
1088
+ os_mbuf_append (param->ctxt ->om , bufferHandle->Start (), bufferHandle->DataLength ());
1089
+
1090
+ exit :
1091
+ if (err != CHIP_NO_ERROR)
1092
+ {
1093
+ ChipLogError (DeviceLayer, " Failed to generate TLV encoded Additional Data (%s)" , __func__);
1094
+ }
1095
+ return ;
1096
+ }
1097
+
1098
+ int BLEManagerImpl::gatt_svr_chr_access_additional_data (uint16_t conn_handle, uint16_t attr_handle,
1099
+ struct ble_gatt_access_ctxt * ctxt, void * arg)
1100
+ {
1101
+ struct ble_gatt_char_context param;
1102
+ int err = 0 ;
1103
+
1104
+ memset (¶m, 0 , sizeof (struct ble_gatt_char_context ));
1105
+
1106
+ switch (ctxt->op )
1107
+ {
1108
+ case BLE_GATT_ACCESS_OP_READ_CHR:
1109
+
1110
+ param.conn_handle = conn_handle;
1111
+ param.attr_handle = attr_handle;
1112
+ param.ctxt = ctxt;
1113
+ param.arg = arg;
1114
+ sInstance .HandleC3CharRead (¶m);
1115
+ break ;
1116
+
1117
+ default :
1118
+ err = BLE_ATT_ERR_UNLIKELY;
1119
+ break ;
1120
+ }
1121
+
1122
+ PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
1123
+
1124
+ return err;
1125
+ }
1126
+ #endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */
1127
+
1049
1128
int BLEManagerImpl::gatt_svr_chr_access (uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg)
1050
1129
{
1051
1130
struct ble_gatt_char_context param;
0 commit comments