44
44
#include < zephyr/sys/byteorder.h>
45
45
#include < zephyr/sys/util.h>
46
46
47
+ #include < zephyr/settings/settings.h>
48
+
47
49
#include < array>
48
50
49
51
using namespace ::chip;
@@ -107,7 +109,13 @@ bt_gatt_service sChipoBleService = BT_GATT_SERVICE(sChipoBleAttributes);
107
109
// This value should be adjusted accordingly if the service declaration changes.
108
110
constexpr int kCHIPoBLE_CCC_AttributeIndex = 3 ;
109
111
110
- CHIP_ERROR InitRandomStaticAddress ()
112
+ #ifdef CONFIG_BT_BONDABLE
113
+ constexpr uint8_t kMatterBleIdentity = 1 ;
114
+ #else
115
+ constexpr uint8_t kMatterBleIdentity = 0 ;
116
+ #endif // CONFIG_BT_BONDABLE
117
+
118
+ int InitRandomStaticAddress (bool idPresent, int & id)
111
119
{
112
120
// Generate a random static address for the default identity.
113
121
// This must be done before bt_enable() as after that updating the default identity is not possible.
@@ -122,20 +130,29 @@ CHIP_ERROR InitRandomStaticAddress()
122
130
if (error)
123
131
{
124
132
ChipLogError (DeviceLayer, " Failed to create BLE address: %d" , error);
125
- return System::MapErrorZephyr ( error) ;
133
+ return error;
126
134
}
127
135
128
- error = bt_id_create (&addr, nullptr );
136
+ if (!idPresent)
137
+ {
138
+ id = bt_id_create (&addr, nullptr );
139
+ }
140
+ #if CONFIG_BT_ID_MAX == 2
141
+ else
142
+ {
143
+ id = bt_id_reset (1 , &addr, nullptr );
144
+ }
145
+ #endif // CONFIG_BT_BONDABLE
129
146
130
- if (error < 0 )
147
+ if (id < 0 )
131
148
{
132
149
ChipLogError (DeviceLayer, " Failed to create BLE identity: %d" , error);
133
- return System::MapErrorZephyr (error) ;
150
+ return id ;
134
151
}
135
152
136
153
ChipLogProgress (DeviceLayer, " BLE address: %02X:%02X:%02X:%02X:%02X:%02X" , addr.a .val [5 ], addr.a .val [4 ], addr.a .val [3 ],
137
154
addr.a .val [2 ], addr.a .val [1 ], addr.a .val [0 ]);
138
- return CHIP_NO_ERROR ;
155
+ return 0 ;
139
156
}
140
157
141
158
} // unnamed namespace
@@ -144,17 +161,41 @@ BLEManagerImpl BLEManagerImpl::sInstance;
144
161
145
162
CHIP_ERROR BLEManagerImpl::_Init ()
146
163
{
164
+ int err = 0 ;
165
+ int id = 0 ;
166
+
147
167
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled ;
148
168
mFlags .ClearAll ().Set (Flags::kAdvertisingEnabled , CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
149
169
mFlags .Set (Flags::kFastAdvertisingEnabled , true );
150
170
mGAPConns = 0 ;
151
171
152
172
memset (mSubscribedConns , 0 , sizeof (mSubscribedConns ));
153
173
154
- ReturnErrorOnFailure (InitRandomStaticAddress ());
155
- int err = bt_enable (NULL );
174
+ #ifdef CONFIG_BT_BONDABLE
175
+ bt_addr_le_t idsAddr[CONFIG_BT_ID_MAX];
176
+ size_t idsCount = CONFIG_BT_ID_MAX;
177
+
178
+ err = bt_enable (nullptr );
179
+
156
180
VerifyOrReturnError (err == 0 , MapErrorZephyr (err));
157
181
182
+ settings_load ();
183
+
184
+ bt_id_get (idsAddr, &idsCount);
185
+
186
+ err = InitRandomStaticAddress (idsCount > 1 , id);
187
+
188
+ VerifyOrReturnError (err == 0 && id == kMatterBleIdentity , MapErrorZephyr (err));
189
+
190
+ #else
191
+ err = InitRandomStaticAddress (false , id);
192
+ VerifyOrReturnError (err == 0 && id == kMatterBleIdentity , MapErrorZephyr (err));
193
+ err = bt_enable (nullptr );
194
+ VerifyOrReturnError (err == 0 , MapErrorZephyr (err));
195
+ #endif // CONFIG_BT_BONDABLE
196
+
197
+ BLEAdvertisingArbiter::Init (static_cast <uint8_t >(id));
198
+
158
199
memset (&mConnCallbacks , 0 , sizeof (mConnCallbacks ));
159
200
mConnCallbacks .connected = HandleConnect;
160
201
mConnCallbacks .disconnected = HandleDisconnect;
0 commit comments