Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old node only when provisioning succeeds #579

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,11 @@ class ScannerTableViewController: UITableViewController {
let network = MeshNetworkManager.instance.meshNetwork!
if let oldNode = network.node(withUuid: unprovisionedDevice.uuid) {
let removeAction = UIAlertAction(title: "Just reprovision", style: .default) { _ in
network.remove(node: oldNode)
self.previousNode = nil
self.provision(selectedPeripheral)
}
let reconfigureAction = UIAlertAction(title: "Reprovision and reconfigure", style: .default) { _ in
self.previousNode = oldNode
network.remove(node: oldNode)
self.provision(selectedPeripheral)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
Expand Down
5 changes: 0 additions & 5 deletions nRFMeshProvision/MeshNetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,8 @@ public extension MeshNetworkManager {
func provision(unprovisionedDevice: UnprovisionedDevice,
over bearer: ProvisioningBearer) throws -> ProvisioningManager {
guard let meshNetwork = meshNetwork else {
print("Error: Mesh Network not created")
throw MeshNetworkError.noNetwork
}
guard !meshNetwork.contains(nodeWithUuid: unprovisionedDevice.uuid) &&
!meshNetwork.contains(provisionerWithUuid: unprovisionedDevice.uuid) else {
throw MeshNetworkError.nodeAlreadyExist
}
return ProvisioningManager(for: unprovisionedDevice, over: bearer, in: meshNetwork)
}

Expand Down
9 changes: 9 additions & 0 deletions nRFMeshProvision/Provisioning/ProvisioningManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,15 @@ extension ProvisioningManager: BearerDelegate, BearerDataDelegate {
andAssignedNetworkKey: provisioningData.networkKey,
andAddress: provisioningData.unicastAddress)
do {
// If the node was reprovisioned, remove the old instance.
// Note: Before version 4.0.2 the provisioning would instead end with an error.
// This could cause 2 issues:
// - The device was successfully provisioned and is not being added to the
// network. Instead the library forgets the new Node instance.
// - Removing the Node before provisioning could lead to forgetting the
// old Node if provisioning would fail.
meshNetwork.remove(nodeWithUuid: node.uuid)
// Now it's safe to add the new Node.
try meshNetwork.add(node: node)
state = .complete
} catch {
Expand Down