|
6 | 6 |
|
7 | 7 | #include "bridge_storage_manager.h" |
8 | 8 | #include "bridge_manager.h" |
| 9 | +#include "platform/ConfigurationManager.h" |
9 | 10 |
|
10 | 11 | #include <zephyr/logging/log.h> |
11 | 12 |
|
@@ -196,76 +197,150 @@ bool BridgeStorageManager::Init() |
196 | 197 | return MigrateData(); |
197 | 198 | } |
198 | 199 |
|
199 | | -bool BridgeStorageManager::MigrateData() |
| 200 | +bool BridgeStorageManager::MigrateDataOldScheme(uint8_t bridgedDeviceIndex) |
200 | 201 | { |
201 | | - uint8_t version; |
202 | | - |
203 | | - /* Check if version key is present in settings to provide backward compatibility between post-2.6.0 releases and |
204 | | - * the previous one. If the version key is present it means that the new keys structure is used (only version |
205 | | - * equal to 1 is for now valid). Otherwise, the deprecated structure is used, so the migration has to be done. |
206 | | - */ |
207 | | - if (!LoadDataToObject(&mVersion, version)) { |
208 | | - uint8_t count; |
209 | | - uint8_t indexes[BridgeManager::kMaxBridgedDevices] = { 0 }; |
210 | | - size_t indexesCount = 0; |
211 | | - |
212 | | - if (LoadBridgedDevicesCount(count) && |
213 | | - LoadBridgedDevicesIndexes(indexes, BridgeManager::kMaxBridgedDevices, indexesCount)) { |
214 | | - /* Load all devices based on the read count number. */ |
215 | | - for (size_t i = 0; i < indexesCount; i++) { |
216 | | - BridgedDevice device; |
217 | | - if (!LoadBridgedDeviceEndpointId(device.mEndpointId, i)) { |
218 | | - return false; |
219 | | - } |
| 202 | + BridgedDevice device; |
| 203 | + if (!LoadBridgedDeviceEndpointId(device.mEndpointId, bridgedDeviceIndex)) { |
| 204 | + return false; |
| 205 | + } |
220 | 206 |
|
221 | | - /* Ignore an error, as node label is optional, so it may not be found. */ |
222 | | - if (!LoadBridgedDeviceNodeLabel(device.mNodeLabel, sizeof(device.mNodeLabel), |
223 | | - device.mNodeLabelLength, i)) { |
224 | | - device.mNodeLabelLength = 0; |
225 | | - } |
| 207 | + /* Ignore an error, as node label is optional, so it may not be found. */ |
| 208 | + if (!LoadBridgedDeviceNodeLabel(device.mNodeLabel, sizeof(device.mNodeLabel), device.mNodeLabelLength, |
| 209 | + bridgedDeviceIndex)) { |
| 210 | + device.mNodeLabelLength = 0; |
| 211 | + } |
226 | 212 |
|
227 | | - if (!LoadBridgedDeviceType(device.mDeviceType, i)) { |
228 | | - return false; |
229 | | - } |
| 213 | + if (!LoadBridgedDeviceType(device.mDeviceType, bridgedDeviceIndex)) { |
| 214 | + return false; |
| 215 | + } |
230 | 216 |
|
231 | 217 | #ifdef CONFIG_BRIDGED_DEVICE_BT |
232 | | - bt_addr_le_t btAddr; |
| 218 | + bt_addr_le_t btAddr; |
233 | 219 |
|
234 | | - if (!LoadBtAddress(btAddr, i)) { |
235 | | - return false; |
236 | | - } |
| 220 | + if (!LoadBtAddress(btAddr, bridgedDeviceIndex)) { |
| 221 | + return false; |
| 222 | + } |
237 | 223 |
|
238 | | - /* Insert Bluetooth LE address as a part of implementation specific user data. */ |
239 | | - device.mUserDataSize = sizeof(btAddr); |
240 | | - device.mUserData = reinterpret_cast<uint8_t *>(&btAddr); |
| 224 | + /* Insert Bluetooth LE address as a part of implementation specific user data. */ |
| 225 | + device.mUserDataSize = sizeof(btAddr); |
| 226 | + device.mUserData = reinterpret_cast<uint8_t *>(&btAddr); |
241 | 227 | #endif |
242 | 228 |
|
243 | | - /* Store all information using a new scheme. */ |
244 | | - if (!StoreBridgedDevice(device, i)) { |
245 | | - return false; |
246 | | - } |
| 229 | + /* Generate UniqueID */ |
| 230 | + CHIP_ERROR result = |
| 231 | + chip::DeviceLayer::ConfigurationMgrImpl().GenerateUniqueId(device.mUniqueID, sizeof(device.mUniqueID)); |
| 232 | + if (result != CHIP_NO_ERROR) { |
| 233 | + return false; |
| 234 | + } |
| 235 | + device.mUniqueIDLength = strlen(device.mUniqueID); |
| 236 | + |
| 237 | + /* Store all information using a new scheme. */ |
| 238 | + if (!StoreBridgedDevice(device, bridgedDeviceIndex)) { |
| 239 | + return false; |
| 240 | + } |
247 | 241 |
|
248 | | - /* Remove all information described using an old scheme. */ |
249 | | - RemoveBridgedDeviceEndpointId(i); |
250 | | - RemoveBridgedDeviceNodeLabel(i); |
251 | | - RemoveBridgedDeviceType(i); |
| 242 | + /* Remove all information described using an old scheme. */ |
| 243 | + RemoveBridgedDeviceEndpointId(bridgedDeviceIndex); |
| 244 | + RemoveBridgedDeviceNodeLabel(bridgedDeviceIndex); |
| 245 | + RemoveBridgedDeviceType(bridgedDeviceIndex); |
252 | 246 |
|
253 | 247 | #ifdef CONFIG_BRIDGED_DEVICE_BT |
254 | | - RemoveBtAddress(i); |
| 248 | + RemoveBtAddress(bridgedDeviceIndex); |
255 | 249 | #endif |
256 | | - } |
257 | | - } |
258 | 250 |
|
259 | | - version = 1; |
260 | | - Nrf::GetPersistentStorage().NonSecureStore(&mVersion, &version, sizeof(version)); |
| 251 | + return true; |
| 252 | +} |
| 253 | + |
| 254 | +bool BridgeStorageManager::MigrateDataVersion1(uint8_t bridgedDeviceIndex) |
| 255 | +{ |
| 256 | + BridgedDeviceV1 v1; |
| 257 | + BridgedDevice device; |
| 258 | + |
| 259 | +#ifdef CONFIG_BRIDGED_DEVICE_BT |
| 260 | + bt_addr_le_t btAddr; |
261 | 261 |
|
262 | | - } else if (version != 1) { |
263 | | - /* Currently only no-version or version equal to 1 is supported. */ |
| 262 | + /* Insert Bluetooth LE address as a part of implementation specific user data. */ |
| 263 | + v1.mUserDataSize = sizeof(btAddr); |
| 264 | + v1.mUserData = reinterpret_cast<uint8_t *>(&btAddr); |
| 265 | +#endif |
| 266 | + |
| 267 | + /* Load all information from old scheme */ |
| 268 | + if (!LoadBridgedDevice(v1, bridgedDeviceIndex)) { |
| 269 | + return false; |
| 270 | + } |
| 271 | + |
| 272 | + /* Copy all information to new scheme */ |
| 273 | + device.mEndpointId = v1.mEndpointId; |
| 274 | + device.mDeviceType = v1.mDeviceType; |
| 275 | + device.mNodeLabelLength = v1.mNodeLabelLength; |
| 276 | + memcpy(device.mNodeLabel, v1.mNodeLabel, v1.mNodeLabelLength); |
| 277 | + device.mUserDataSize = v1.mUserDataSize; |
| 278 | + device.mUserData = v1.mUserData; |
| 279 | + |
| 280 | + /* Generate UniqueID */ |
| 281 | + CHIP_ERROR result = |
| 282 | + chip::DeviceLayer::ConfigurationMgrImpl().GenerateUniqueId(device.mUniqueID, sizeof(device.mUniqueID)); |
| 283 | + if (result != CHIP_NO_ERROR) { |
| 284 | + return false; |
| 285 | + } |
| 286 | + device.mUniqueIDLength = strlen(device.mUniqueID); |
| 287 | + |
| 288 | + /* Store all information using new scheme */ |
| 289 | + if (!StoreBridgedDevice(device, bridgedDeviceIndex)) { |
264 | 290 | return false; |
265 | 291 | } |
| 292 | + |
266 | 293 | return true; |
267 | 294 | } |
268 | 295 |
|
| 296 | +bool BridgeStorageManager::MigrateData() |
| 297 | +{ |
| 298 | + /* Check if migration is needed to provide backward compatibility between releases. |
| 299 | + * Perform migration in following cases: |
| 300 | + * 1) If the version key is missing - it means that the pre-2.7.0 release structure is used. |
| 301 | + * 2) If the version key is present but the version number does not match kCurrentVersion. |
| 302 | + */ |
| 303 | + uint8_t version; |
| 304 | + const bool versionPresent = LoadDataToObject(&mVersion, version); |
| 305 | + const bool migrationNeeded = !versionPresent || version != kCurrentVersion; |
| 306 | + |
| 307 | + if (!migrationNeeded) { |
| 308 | + /* No migration needed */ |
| 309 | + return true; |
| 310 | + } |
| 311 | + |
| 312 | + if (versionPresent && (version < 1 || version > kCurrentVersion)) { |
| 313 | + /* Not supported version */ |
| 314 | + return false; |
| 315 | + } |
| 316 | + |
| 317 | + uint8_t count; |
| 318 | + uint8_t indexes[BridgeManager::kMaxBridgedDevices] = { 0 }; |
| 319 | + size_t indexesCount = 0; |
| 320 | + |
| 321 | + if (LoadBridgedDevicesCount(count) && |
| 322 | + LoadBridgedDevicesIndexes(indexes, BridgeManager::kMaxBridgedDevices, indexesCount)) { |
| 323 | + /* Migrate all devices */ |
| 324 | + for (size_t i = 0; i < indexesCount; i++) { |
| 325 | + if (!versionPresent) { |
| 326 | + if (!MigrateDataOldScheme(indexes[i])) { |
| 327 | + return false; |
| 328 | + } |
| 329 | + } else if (version == 1) { |
| 330 | + if (!MigrateDataVersion1(indexes[i])) { |
| 331 | + return false; |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + /* Store current version */ |
| 338 | + version = kCurrentVersion; |
| 339 | + const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureStore(&mVersion, &version, sizeof(version)); |
| 340 | + |
| 341 | + return status == PSErrorCode::Success; |
| 342 | +} |
| 343 | + |
269 | 344 | bool BridgeStorageManager::StoreBridgedDevicesCount(uint8_t count) |
270 | 345 | { |
271 | 346 | return Nrf::GetPersistentStorage().NonSecureStore(&mBridgedDevicesCount, &count, sizeof(count)); |
|
0 commit comments