Skip to content

Commit

Permalink
Merge pull request #420 from NordicSemiconductor/api-improvements
Browse files Browse the repository at this point in the history
Api improvements
  • Loading branch information
philips77 authored Sep 19, 2022
2 parents 062a753 + 474e421 commit b425080
Show file tree
Hide file tree
Showing 58 changed files with 2,466 additions and 2,066 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ profile
DerivedData
*.hmap
*.ipa
# AppCode
.idea
# Bundler
.bundle

.build/

Carthage
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
Expand Down
3,501 changes: 1,748 additions & 1,753 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Example/Tests/AssigningUnicastAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class AssigningUnicastAddress: XCTestCase {

let oldNode = Node(name: "Node 5", unicastAddress: 110, elements: 2)
XCTAssertNoThrow(try meshNetwork.add(node: oldNode))
XCTAssertFalse(meshNetwork.isAddressRangeAvailable(111, elementsCount: 2))
XCTAssertTrue(meshNetwork.isAddressRangeAvailable(111, elementsCount: 2, for: oldNode))
XCTAssertFalse(meshNetwork.isAddress(111, availableForElementsCount: 2))
XCTAssertTrue(meshNetwork.isAddress(111, availableFor: oldNode))
meshNetwork.remove(node: oldNode)

let provisioner = Provisioner(name: "Test provisioner",
Expand All @@ -124,19 +124,19 @@ class AssigningUnicastAddress: XCTestCase {
XCTAssertEqual(address!, 112)

// 110 and 111 cannot be assigned until IV Index changes by 2.
XCTAssertFalse(meshNetwork.isAddressRangeAvailable(110, elementsCount: 3))
XCTAssertTrue(meshNetwork.isAddressRangeAvailable(112, elementsCount: 10))
XCTAssertFalse(meshNetwork.isAddress(110, availableForElementsCount: 3))
XCTAssertTrue(meshNetwork.isAddress(112, availableForElementsCount: 10))

meshNetwork.ivIndex = IvIndex(index: 1, updateActive: true)
XCTAssertFalse(meshNetwork.isAddressRangeAvailable(110, elementsCount: 3))
XCTAssertFalse(meshNetwork.isAddress(110, availableForElementsCount: 3))

meshNetwork.ivIndex = IvIndex(index: 1, updateActive: false)
XCTAssertFalse(meshNetwork.isAddressRangeAvailable(110, elementsCount: 3))
XCTAssertFalse(meshNetwork.isAddress(110, availableForElementsCount: 3))

// When IV Index is incremented by 2 since a Node was removed, the Unicast
// Addresses may be reused.
meshNetwork.ivIndex = IvIndex(index: 2, updateActive: true)
XCTAssertTrue(meshNetwork.isAddressRangeAvailable(110, elementsCount: 3))
XCTAssertTrue(meshNetwork.isAddress(110, availableForElementsCount: 3))
XCTAssertEqual(meshNetwork.nextAvailableUnicastAddress(for: 6, elementsUsing: provisioner), 108)
}

Expand Down
128 changes: 72 additions & 56 deletions Example/Tests/Exporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,65 +99,73 @@ class Exporting: XCTestCase {
allocatedSceneRange: [])
XCTAssertNoThrow(try meshNetwork.add(provisioner: guestProvisioner, withAddress: 0x1000))
XCTAssertNotNil(guestProvisioner.node)
XCTAssertGreaterThanOrEqual(guestProvisioner.node?.elementsCount ?? 0, 1)
XCTAssertEqual(guestProvisioner.node?.primaryElement?.models.count, 4)
let guestGenericOnOffClientModel = Model(sigModelId: .genericOnOffClientModelId)
guestProvisioner.node?.primaryElement?.add(model: guestGenericOnOffClientModel)
XCTAssertEqual(guestProvisioner.node?.primaryElement?.models.count, 5)
guestKey.map { key in
guestGenericOnOffClientModel.bind(applicationKeyWithIndex: key.index)
guestRoomLights.map { group in
guestGenericOnOffClientModel.set(publication: Publish(to: group.address, using: key,
usingFriendshipMaterial: false, ttl: 5,
period: .disabled, retransmit: .disabled))
}
}
let guestSceneClientModel = Model(sigModelId: .sceneClientModelId)
guestProvisioner.node?.primaryElement?.add(model: guestSceneClientModel)
XCTAssertEqual(guestProvisioner.node?.primaryElement?.models.count, 6)

// As the Composition Data have not been obtained from the Guest Provisioner's Node, the
// Primary Element is unknown.
XCTAssertNil(guestProvisioner.node?.primaryElement)

let data = Data(hex: "004600CDAB0001FFFFFFFF01000300000002000110")
guestProvisioner.node?.apply(compositionData: ConfigCompositionDataStatus(parameters: data)!)

// As the Composition Data have been obtained, now the Primary Element should be available.
XCTAssertNotNil(guestProvisioner.node?.primaryElement)

let guestGenericOnOffClientModel = guestProvisioner.node?.primaryElement?
.model(withSigModelId: .genericOnOffClientModelId)
XCTAssertNotNil(guestGenericOnOffClientModel)
guestKey.map { key in
guestSceneClientModel.bind(applicationKeyWithIndex: key.index)
guestGenericOnOffClientModel!.bind(applicationKeyWithIndex: key.index)
guestRoomLights.map { group in
guestSceneClientModel.set(publication: Publish(to: group.address, using: key,
usingFriendshipMaterial: false, ttl: 5,
period: .disabled, retransmit: .disabled))
guestGenericOnOffClientModel!.set(publication: Publish(to: group.address, using: key,
usingFriendshipMaterial: false, ttl: 5,
period: .disabled, retransmit: .disabled))
}
}

// Setup Nodes in the kitchen.
let kitchenLightSwitch = Node(name: "Kitchen Light Switch", uuid: UUID(), deviceKey: Data.random128BitKey(),
andAssignedNetworkKey: primaryNetworkKey!, andAddress: 0x0002)
kitchenLightSwitch.add(elements: [Element(name: "Left button", location: .left,
models: [
Model(sigModelId: .configurationServerModelId),
Model(sigModelId: .healthServerModelId),
Model(sigModelId: .genericOnOffClientModelId)
]),
Element(name: "Right button", location: .right,
models: [
Model(sigModelId: .genericOnOffClientModelId)
])])
kitchenLightSwitch.add(elements: [
Element(name: "Left button", location: .left,
models: [
Model(sigModelId: .configurationServerModelId),
Model(sigModelId: .healthServerModelId),
Model(sigModelId: .genericOnOffClientModelId)
]
),
Element(name: "Right button", location: .right,
models: [
Model(sigModelId: .genericOnOffClientModelId)
]
)
])

let kitchenLight = Node(name: "Kitchen Light", uuid: UUID(), deviceKey: Data.random128BitKey(),
andAssignedNetworkKey: primaryNetworkKey!, andAddress: 0x0004)
kitchenLight.add(element: Element(name: "Main Element", location: .left,
models: [
Model(sigModelId: .configurationServerModelId),
Model(sigModelId: .healthServerModelId),
Model(sigModelId: .sceneServerModelId),
Model(sigModelId: .sceneSetupServerModelId),
Model(sigModelId: .genericOnOffServerModelId)
]))
kitchenLight.add(element:
Element(name: "Main Element", location: .left,
models: [
Model(sigModelId: .configurationServerModelId),
Model(sigModelId: .healthServerModelId),
Model(sigModelId: .sceneServerModelId),
Model(sigModelId: .sceneSetupServerModelId),
Model(sigModelId: .genericOnOffServerModelId)
]
)
)
let led = Node(name: "LED", uuid: UUID(), deviceKey: Data.random128BitKey(),
andAssignedNetworkKey: primaryNetworkKey!, andAddress: 0x0005)
led.add(element: Element(name: "Main Element", location: .left,
models: [
Model(sigModelId: .configurationServerModelId),
Model(sigModelId: .healthServerModelId),
Model(sigModelId: .genericOnOffServerModelId),
Model(sigModelId: .sceneServerModelId),
Model(sigModelId: .sceneSetupServerModelId)
]))
led.add(element:
Element(name: "Main Element", location: .left,
models: [
Model(sigModelId: .configurationServerModelId),
Model(sigModelId: .healthServerModelId),
Model(sigModelId: .genericOnOffServerModelId),
Model(sigModelId: .sceneServerModelId),
Model(sigModelId: .sceneSetupServerModelId)
]
)
)

XCTAssertNoThrow(try meshNetwork.add(node: kitchenLightSwitch))
XCTAssertNoThrow(try meshNetwork.add(node: kitchenLight))
Expand Down Expand Up @@ -187,7 +195,7 @@ class Exporting: XCTestCase {
XCTAssertNotNil(rightButtonModel)
lightsKey.map { key in
rightButtonModel?.bind(applicationKeyWithIndex: key.index)
rightButtonModel?.set(publication: Publish(to: MeshAddress(led.unicastAddress), using: key,
rightButtonModel?.set(publication: Publish(to: MeshAddress(led.primaryUnicastAddress), using: key,
usingFriendshipMaterial: false, ttl: 5,
period: .disabled, retransmit: .disabled))
}
Expand All @@ -210,11 +218,11 @@ class Exporting: XCTestCase {
XCTAssert(kitchenLightModel?.subscriptions.contains(group) ?? false)
}
// Simulate that the Kitchen Light Scene Register was set to use the following scenes:
meshNetwork.scenes[Scene.allOff]?.add(address: kitchenLight.unicastAddress)
meshNetwork.scenes[Scene.allOff]?.add(address: kitchenLight.primaryUnicastAddress)
XCTAssertFalse(meshNetwork.scenes[Scene.allOff]?.nodes.isEmpty ?? true)
meshNetwork.scenes[Scene.kitchenOn]?.add(address: kitchenLight.unicastAddress)
meshNetwork.scenes[Scene.kitchenOn]?.add(address: kitchenLight.primaryUnicastAddress)
XCTAssertFalse(meshNetwork.scenes[Scene.kitchenOn]?.nodes.isEmpty ?? true)
meshNetwork.scenes[Scene.kitchenOff]?.add(address: kitchenLight.unicastAddress)
meshNetwork.scenes[Scene.kitchenOff]?.add(address: kitchenLight.primaryUnicastAddress)
XCTAssertFalse(meshNetwork.scenes[Scene.kitchenOff]?.nodes.isEmpty ?? true)
XCTAssertTrue(meshNetwork.scenes.contains(where: { $0.isUsed }))

Expand All @@ -230,11 +238,11 @@ class Exporting: XCTestCase {
ledModel?.bind(applicationKeyWithIndex: key.index)
}
// Simulate that the LED Scene Register was set to use the following scenes:
meshNetwork.scenes[Scene.allOff]?.add(address: led.unicastAddress)
meshNetwork.scenes[Scene.allOff]?.add(address: led.primaryUnicastAddress)
XCTAssertTrue(meshNetwork.scenes[Scene.allOff]?.nodes.contains(led) ?? false)
meshNetwork.scenes[Scene.kitchenOn]?.add(address: led.unicastAddress)
meshNetwork.scenes[Scene.kitchenOn]?.add(address: led.primaryUnicastAddress)
XCTAssertTrue(meshNetwork.scenes[Scene.kitchenOn]?.nodes.contains(led) ?? false)
meshNetwork.scenes[Scene.kitchenOff]?.add(address: led.unicastAddress)
meshNetwork.scenes[Scene.kitchenOff]?.add(address: led.primaryUnicastAddress)
XCTAssertTrue(meshNetwork.scenes[Scene.kitchenOff]?.nodes.contains(led) ?? false)

// Configure Lock.
Expand Down Expand Up @@ -264,7 +272,7 @@ class Exporting: XCTestCase {
XCTAssert(lockModel?.subscriptions.contains(group) ?? false)
}
// Simulate that the Lock's Scene Register was set to use the following scene:
meshNetwork.scenes[Scene.allOff]?.add(address: lock.unicastAddress)
meshNetwork.scenes[Scene.allOff]?.add(address: lock.primaryUnicastAddress)
XCTAssertTrue(meshNetwork.scenes[Scene.allOff]?.nodes.contains(lock) ?? false)

// Setup Nodes in the guest room.
Expand Down Expand Up @@ -337,9 +345,9 @@ class Exporting: XCTestCase {
XCTAssert(guestLightModel?.subscriptions.contains(group) ?? false)
}
// Simulate that the Lock's Scene Register was set to use the following scene:
meshNetwork.scenes[Scene.cozyGuestRoom]?.add(address: guestLight.unicastAddress)
meshNetwork.scenes[Scene.cozyGuestRoom]?.add(address: guestLight.primaryUnicastAddress)
XCTAssertTrue(meshNetwork.scenes[Scene.cozyGuestRoom]?.nodes.contains(guestLight) ?? false)
meshNetwork.scenes[Scene.allOff]?.add(address: guestLight.unicastAddress)
meshNetwork.scenes[Scene.allOff]?.add(address: guestLight.primaryUnicastAddress)
XCTAssertTrue(meshNetwork.scenes[Scene.allOff]?.nodes.contains(guestLight) ?? false)

// Add and remove a Node.
Expand Down Expand Up @@ -513,6 +521,14 @@ class Exporting: XCTestCase {
XCTAssertEqual(copy.networkKeys.count, 1)
XCTAssertEqual(copy.applicationKeys.count, 1)
XCTAssertEqual(copy.provisioners.count, 1)
XCTAssertNotNil(copy.localProvisioner)
XCTAssertNotNil(copy.localProvisioner?.node)
XCTAssertNotNil(copy.localProvisioner?.node?.primaryElement)
let guestGenericOnOffClientModel = copy.localProvisioner?.node?.primaryElement?
.model(withSigModelId: .genericOnOffClientModelId)
XCTAssertNotNil(guestGenericOnOffClientModel)
XCTAssertNotNil(guestGenericOnOffClientModel?.publish)
XCTAssertEqual(guestGenericOnOffClientModel?.publish?.publicationAddress, MeshAddress(0xD001))
XCTAssertEqual(copy.nodes.count, 3)
copy.nodes.forEach {
XCTAssertNil($0.deviceKey)
Expand Down
4 changes: 2 additions & 2 deletions Example/Tests/FastSending.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ class FastSending: XCTestCase, MeshNetworkDelegate {

XCTAssertNotNil(meshNetwork.localProvisioner)
XCTAssertNotNil(meshNetwork.localProvisioner!.meshNetwork)
XCTAssertNotNil(meshNetwork.localProvisioner!.unicastAddress)
XCTAssertEqual(meshNetwork.localProvisioner!.unicastAddress, 0x0001)
XCTAssertNotNil(meshNetwork.localProvisioner!.primaryUnicastAddress)
XCTAssertEqual(meshNetwork.localProvisioner!.primaryUnicastAddress, 0x0001)

XCTAssertEqual(meshNetwork.networkKeys.count, 2)
XCTAssertEqual(meshNetwork.applicationKeys.count, 3)
Expand Down
6 changes: 3 additions & 3 deletions Example/Tests/ManagingProvisioners.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ManagingProvisioners: XCTestCase {
XCTAssertNoThrow(try meshNetwork.add(provisioner: provisioner))
XCTAssertEqual(meshNetwork.provisioners.count, 1)
XCTAssertEqual(meshNetwork.nodes.count, 7)
XCTAssertEqual(meshNetwork.nodes[6].unicastAddress, 11)
XCTAssertEqual(meshNetwork.nodes[6].primaryUnicastAddress, 11)
XCTAssertEqual(meshNetwork.nodes[6].lastUnicastAddress, 11)

// This will throw, as it's not possible to remove the last Provisioner object.
Expand All @@ -81,7 +81,7 @@ class ManagingProvisioners: XCTestCase {
XCTAssertNoThrow(try meshNetwork.add(provisioner: otherProvisioner))
XCTAssertEqual(meshNetwork.provisioners.count, 2)
XCTAssertEqual(meshNetwork.nodes.count, 8)
XCTAssertEqual(meshNetwork.nodes[7].unicastAddress, 100)
XCTAssertEqual(meshNetwork.nodes[7].primaryUnicastAddress, 100)
XCTAssertEqual(meshNetwork.nodes[7].lastUnicastAddress, 100)

XCTAssertNoThrow(try meshNetwork.remove(provisioner: otherProvisioner))
Expand All @@ -105,7 +105,7 @@ class ManagingProvisioners: XCTestCase {
],
allocatedGroupRange: [],
allocatedSceneRange: [])
XCTAssertNoThrow(try provisioner.allocateSceneRange(SceneRange.allScenes))
XCTAssertNoThrow(try provisioner.allocate(sceneRange: SceneRange.allScenes))
// Group ranges not allocated, but that's OK. They are not required.
XCTAssertNoThrow(try meshNetwork.add(provisioner: provisioner))
}
Expand Down
12 changes: 6 additions & 6 deletions Example/Tests/Pdus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ class Pdus: XCTestCase {

XCTAssertNotNil(meshNetwork.localProvisioner)
XCTAssertNotNil(meshNetwork.localProvisioner!.meshNetwork)
XCTAssertNotNil(meshNetwork.localProvisioner!.unicastAddress)
XCTAssertEqual(meshNetwork.localProvisioner!.unicastAddress, 0x0003)
XCTAssertNotNil(meshNetwork.localProvisioner!.primaryUnicastAddress)
XCTAssertEqual(meshNetwork.localProvisioner!.primaryUnicastAddress, 0x0003)

XCTAssertEqual(meshNetwork.networkKeys.count, 1)
XCTAssertEqual(meshNetwork.applicationKeys.count, 2)
Expand All @@ -303,7 +303,7 @@ class Pdus: XCTestCase {
let source = meshNetwork.localProvisioner?.node?.elements.first?.unicastAddress
XCTAssertNotNil(source)
let node = meshNetwork.nodes[1]
let destination = MeshAddress(node.unicastAddress)
let destination = MeshAddress(node.primaryUnicastAddress)
let sequence: UInt32 = 0x3129AB
let keySet = DeviceKeySet(networkKey: networkKey, node: node)!

Expand Down Expand Up @@ -383,7 +383,7 @@ class Pdus: XCTestCase {
let ivIndex = IvIndex(index: 0x12345678, updateActive: false)
let node = meshNetwork.nodes[1]
let source = node.elements.first?.unicastAddress
let destination = MeshAddress(meshNetwork.localProvisioner!.unicastAddress!)
let destination = MeshAddress(meshNetwork.localProvisioner!.primaryUnicastAddress!)
XCTAssertNotNil(destination)
let sequence: UInt32 = 0x000006
let keySet = DeviceKeySet(networkKey: networkKey, node: node)!
Expand Down Expand Up @@ -441,9 +441,9 @@ class Pdus: XCTestCase {
let meshNetwork = manager.meshNetwork!
let networkKey = meshNetwork.networkKeys.first!
let ivIndex = IvIndex(index: 0x12345678, updateActive: false)
let source = meshNetwork.localProvisioner?.unicastAddress
let source = meshNetwork.localProvisioner?.primaryUnicastAddress
let node = meshNetwork.nodes[1]
let destination = node.unicastAddress
let destination = node.primaryUnicastAddress
let sequence: UInt32 = 0x3129AB

// Test begins here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class ConfigurationViewController: ProgressViewController {
if indexPath.isNodeSection {
cell.textLabel?.text = indexPath.title
if indexPath.isUnicastAddress {
cell.detailTextLabel?.text = node.unicastAddress.asString()
cell.detailTextLabel?.text = node.primaryUnicastAddress.asString()
cell.accessoryType = .none
}
if indexPath.isTtl {
Expand Down Expand Up @@ -483,7 +483,7 @@ extension ConfigurationViewController: MeshNetworkDelegate {
return
}
// Is the message targeting the current Node?
guard node.unicastAddress == source else {
guard node.primaryUnicastAddress == source else {
return
}

Expand Down
Loading

0 comments on commit b425080

Please sign in to comment.