diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index 9a249b56c92daf..b3583fd6b7c57c 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -305,7 +305,7 @@ void ChipLinuxAppMainLoop() #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE InitCommissioner(); #if defined(ENABLE_CHIP_SHELL) - chip::Shell::RegisterDiscoverCommands(&gCommissioner); + chip::Shell::RegisterControllerCommands(&gCommissioner); #endif // defined(ENABLE_CHIP_SHELL) #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE diff --git a/examples/platform/linux/ControllerShellCommands.cpp b/examples/platform/linux/ControllerShellCommands.cpp index a8268efafcac20..5fe7db4ab303c2 100644 --- a/examples/platform/linux/ControllerShellCommands.cpp +++ b/examples/platform/linux/ControllerShellCommands.cpp @@ -46,7 +46,7 @@ static CHIP_ERROR ResetUDC(bool printHeader) if (printHeader) { - streamer_printf(sout, "resetudc: "); + streamer_printf(sout, "udc-reset: "); } gCommissioner->GetUserDirectedCommissioningServer()->ResetUDCClientProcessingStates(); @@ -56,6 +56,22 @@ static CHIP_ERROR ResetUDC(bool printHeader) return CHIP_NO_ERROR; } +static CHIP_ERROR PrintUDC(bool printHeader) +{ + streamer_t * sout = streamer_get(); + + if (printHeader) + { + streamer_printf(sout, "udc-print: \r\n"); + } + + gCommissioner->GetUserDirectedCommissioningServer()->PrintUDCClients(); + + streamer_printf(sout, "done\r\n"); + + return CHIP_NO_ERROR; +} + static CHIP_ERROR discover(bool printHeader) { streamer_t * sout = streamer_get(); @@ -132,13 +148,12 @@ class PairingCommand : public chip::Controller::DevicePairingDelegate, public ch CHIP_ERROR UpdateNetworkAddress(); }; -NodeId gRemoteId = kTestDeviceNodeId; -uint64_t gFabricId = 0; +NodeId gRemoteId = kTestDeviceNodeId; PairingCommand gPairingCommand; CHIP_ERROR PairingCommand::UpdateNetworkAddress() { - ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " FabricId: %" PRIx64 " ...", gRemoteId, gFabricId); + ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " ...", gRemoteId); return gCommissioner->UpdateDevice(gRemoteId); } @@ -197,8 +212,7 @@ void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err) } } -static CHIP_ERROR pairOnNetwork(bool printHeader, uint64_t fabric, uint32_t pincode, uint16_t disc, - chip::Transport::PeerAddress address) +static CHIP_ERROR pairOnNetwork(bool printHeader, uint32_t pincode, uint16_t disc, chip::Transport::PeerAddress address) { streamer_t * sout = streamer_get(); @@ -217,34 +231,76 @@ static CHIP_ERROR pairOnNetwork(bool printHeader, uint64_t fabric, uint32_t pinc return CHIP_NO_ERROR; } + +static CHIP_ERROR pairUDC(bool printHeader, uint32_t pincode, size_t index) +{ + streamer_t * sout = streamer_get(); + + if (printHeader) + { + streamer_printf(sout, "udc-commission %ld %ld\r\n", pincode, index); + } + + UDCClientState * state = gCommissioner->GetUserDirectedCommissioningServer()->GetUDCClients().GetUDCClientState(index); + if (state == nullptr) + { + streamer_printf(sout, "udc client[%d] null \r\n", index); + } + else + { + Transport::PeerAddress peerAddress = state->GetPeerAddress(); + + state->SetUDCClientProcessingState(UDCClientProcessingState::kCommissioningNode); + + RendezvousParameters params = RendezvousParameters() + .SetSetupPINCode(pincode) + .SetDiscriminator(state->GetLongDiscriminator()) + .SetPeerAddress(peerAddress); + + gCommissioner->RegisterDeviceAddressUpdateDelegate(&gPairingCommand); + gCommissioner->RegisterPairingDelegate(&gPairingCommand); + gCommissioner->PairDevice(gRemoteId, params); + + streamer_printf(sout, "done\r\n"); + } + return CHIP_NO_ERROR; +} #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY static CHIP_ERROR PrintAllCommands() { streamer_t * sout = streamer_get(); - streamer_printf(sout, " help Usage: discover \r\n"); + streamer_printf(sout, " help Usage: controller \r\n"); #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY streamer_printf( - sout, " resetudc Clear all pending UDC sessions from this UDC server. Usage: commission resetudc\r\n"); + sout, " udc-reset Clear all pending UDC sessions from this UDC server. Usage: controller udc-reset\r\n"); + streamer_printf( + sout, " udc-print Print all pending UDC sessions from this UDC server. Usage: controller udc-print\r\n"); #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE - streamer_printf(sout, " commissionable Discover all commissionable nodes. Usage: commission discover\r\n"); + streamer_printf(sout, + " udc-commission Commission given udc-entry using given pincode. Usage: controller " + "udc-commission 34567890 0\r\n"); streamer_printf( sout, - " commissionable-instance Discover all commissionable node with given instance name. Usage: commission " - "commissionable-instance DC514873944A5CFF\r\n"); - streamer_printf(sout, - " display Display all discovered commissionable nodes. Usage: commission display\r\n"); - streamer_printf(sout, - " onnetwork Pair given device. Usage: commission onnetwork 2222 " - "20202021 3840 127.0.0.1 5540\r\n"); + " discover-commissionable Discover all commissionable nodes. Usage: controller discover-commissionable\r\n"); + streamer_printf( + sout, + " discover-commissionable-instance Discover all commissionable node with given instance name. Usage: controller " + "discover-commissionable-instance DC514873944A5CFF\r\n"); + streamer_printf( + sout, " discover-display Display all discovered commissionable nodes. Usage: controller discover-display\r\n"); + streamer_printf( + sout, + " commission-onnetwork Pair given device. Usage: controller commission-onnetwork " + "20202021 3840 127.0.0.1 5540\r\n"); #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE streamer_printf(sout, "\r\n"); return CHIP_NO_ERROR; } -static CHIP_ERROR DiscoverHandler(int argc, char ** argv) +static CHIP_ERROR ControllerHandler(int argc, char ** argv) { CHIP_ERROR error = CHIP_NO_ERROR; @@ -253,42 +309,57 @@ static CHIP_ERROR DiscoverHandler(int argc, char ** argv) return PrintAllCommands(); } #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY - else if (strcmp(argv[0], "resetudc") == 0) + else if (strcmp(argv[0], "udc-reset") == 0) { return error = ResetUDC(true); } + else if (strcmp(argv[0], "udc-print") == 0) + { + return error = PrintUDC(true); + } #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE - else if (strcmp(argv[0], "commissionable") == 0) + else if (strcmp(argv[0], "discover-commissionable") == 0) { return error = discover(true); } - else if (strcmp(argv[0], "commissionable-instance") == 0) + else if (strcmp(argv[0], "discover-commissionable-instance") == 0) { return error = discover(true, argv[1]); } - else if (strcmp(argv[0], "display") == 0) + else if (strcmp(argv[0], "discover-display") == 0) { return error = display(true); } - else if (strcmp(argv[0], "onnetwork") == 0) + else if (strcmp(argv[0], "commission-onnetwork") == 0) { - // onnetwork fabric pincode disc IP port - if (argc < 6) + // onnetwork pincode disc IP port + if (argc < 5) { return PrintAllCommands(); } char * eptr; - gFabricId = (uint64_t) strtol(argv[1], &eptr, 10); - uint32_t pincode = (uint32_t) strtol(argv[2], &eptr, 10); - uint16_t disc = (uint16_t) strtol(argv[3], &eptr, 10); + uint32_t pincode = (uint32_t) strtol(argv[1], &eptr, 10); + uint16_t disc = (uint16_t) strtol(argv[2], &eptr, 10); chip::Inet::IPAddress address; - chip::Inet::IPAddress::FromString(argv[4], address); + chip::Inet::IPAddress::FromString(argv[3], address); - uint16_t port = (uint16_t) strtol(argv[5], &eptr, 10); + uint16_t port = (uint16_t) strtol(argv[4], &eptr, 10); - return error = pairOnNetwork(true, gFabricId, pincode, disc, chip::Transport::PeerAddress::UDP(address, port)); + return error = pairOnNetwork(true, pincode, disc, chip::Transport::PeerAddress::UDP(address, port)); + } + else if (strcmp(argv[0], "udc-commission") == 0) + { + // udc-commission pincode index + if (argc < 3) + { + return PrintAllCommands(); + } + char * eptr; + uint32_t pincode = (uint32_t) strtol(argv[1], &eptr, 10); + size_t index = (size_t) strtol(argv[2], &eptr, 10); + return error = pairUDC(true, pincode, index); } #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE else @@ -298,11 +369,11 @@ static CHIP_ERROR DiscoverHandler(int argc, char ** argv) return error; } -void RegisterDiscoverCommands(chip::Controller::DeviceCommissioner * commissioner) +void RegisterControllerCommands(chip::Controller::DeviceCommissioner * commissioner) { gCommissioner = commissioner; - static const shell_command_t sDeviceComand = { &DiscoverHandler, "discover", - "Discover commands. Usage: discover [command_name]" }; + static const shell_command_t sDeviceComand = { &ControllerHandler, "controller", + "Controller commands. Usage: controller [command_name]" }; // Register the root `device` command with the top-level shell. Engine::Root().RegisterCommands(&sDeviceComand, 1); diff --git a/examples/platform/linux/ControllerShellCommands.h b/examples/platform/linux/ControllerShellCommands.h index 82b65aaa9607f4..064910ce24cf45 100644 --- a/examples/platform/linux/ControllerShellCommands.h +++ b/examples/platform/linux/ControllerShellCommands.h @@ -25,7 +25,7 @@ namespace chip { namespace Shell { -void RegisterDiscoverCommands(chip::Controller::DeviceCommissioner * commissioner); +void RegisterControllerCommands(chip::Controller::DeviceCommissioner * commissioner); } // namespace Shell } // namespace chip diff --git a/examples/tv-app/linux/args.gni b/examples/tv-app/linux/args.gni index 1c8eb57e4d1e09..8d724f9f9d50ac 100644 --- a/examples/tv-app/linux/args.gni +++ b/examples/tv-app/linux/args.gni @@ -25,3 +25,4 @@ chip_project_config_include_dirs = chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ] chip_build_libshell = true +chip_use_clusters_for_ip_commissioning = true diff --git a/examples/tv-casting-app/linux/args.gni b/examples/tv-casting-app/linux/args.gni index 4e1b8258e7abed..54b1b3bb30fabb 100644 --- a/examples/tv-casting-app/linux/args.gni +++ b/examples/tv-casting-app/linux/args.gni @@ -23,3 +23,5 @@ chip_system_project_config_include = "" chip_project_config_include_dirs = [ "${chip_root}/examples/tv-casting-app/tv-casting-common/include" ] chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ] + +chip_build_libshell = true diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 71e213aeeeee1d..68ae2034805474 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -1580,9 +1580,17 @@ void DeviceCommissioner::FindCommissionableNode(char * instanceName) DiscoverCommissionableNodes(filter); } -void DeviceCommissioner::OnUserDirectedCommissioningRequest(const Dnssd::DiscoveredNodeData & nodeData) +void DeviceCommissioner::OnUserDirectedCommissioningRequest(UDCClientState state) { - ChipLogDetail(Controller, "------PROMPT USER!! OnUserDirectedCommissioningRequest instance=%s", nodeData.instanceName); + ChipLogDetail(Controller, "------PROMPT USER!! OnUserDirectedCommissioningRequest instance=%s deviceName=%s", + state.GetInstanceName(), state.GetDeviceName()); + + if (mUdcServer != nullptr) + { + mUdcServer->PrintUDCClients(); + } + + ChipLogDetail(Controller, "------To Accept Enter: discover udc-commission "); } void DeviceCommissioner::OnNodeDiscoveryComplete(const chip::Dnssd::DiscoveredNodeData & nodeData) diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index d5920d5cfaa9bd..444729cffe75d7 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -554,7 +554,7 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, * @param nodeData DNS-SD node information for the client requesting commissioning * */ - void OnUserDirectedCommissioningRequest(const Dnssd::DiscoveredNodeData & nodeData) override; + void OnUserDirectedCommissioningRequest(UDCClientState state) override; /** * @brief diff --git a/src/lib/dnssd/Resolver.h b/src/lib/dnssd/Resolver.h index 977ee0cf5aecfc..24d0f1a27e821c 100644 --- a/src/lib/dnssd/Resolver.h +++ b/src/lib/dnssd/Resolver.h @@ -118,6 +118,7 @@ struct DiscoveredNodeData mrpRetryIntervalIdle = kUndefinedRetryInterval; mrpRetryIntervalActive = kUndefinedRetryInterval; numIPs = 0; + port = 0; for (int i = 0; i < kMaxIPAddresses; ++i) { ipAddress[i] = chip::Inet::IPAddress::Any; diff --git a/src/protocols/user_directed_commissioning/UDCClientState.h b/src/protocols/user_directed_commissioning/UDCClientState.h index a58bca70a31d10..e3565022c23f27 100644 --- a/src/protocols/user_directed_commissioning/UDCClientState.h +++ b/src/protocols/user_directed_commissioning/UDCClientState.h @@ -63,13 +63,18 @@ class UDCClientState UDCClientState & operator=(const UDCClientState &) = default; UDCClientState & operator=(UDCClientState &&) = default; - const PeerAddress & GetPeerAddress() const { return mPeerAddress; } - PeerAddress & GetPeerAddress() { return mPeerAddress; } + const PeerAddress GetPeerAddress() const { return mPeerAddress; } void SetPeerAddress(const PeerAddress & address) { mPeerAddress = address; } const char * GetInstanceName() const { return mInstanceName; } void SetInstanceName(const char * instanceName) { strncpy(mInstanceName, instanceName, sizeof(mInstanceName)); } + const char * GetDeviceName() const { return mDeviceName; } + void SetDeviceName(const char * deviceName) { strncpy(mDeviceName, deviceName, sizeof(mDeviceName)); } + + uint16_t GetLongDiscriminator() const { return mLongDiscriminator; } + void SetLongDiscriminator(uint16_t value) { mLongDiscriminator = value; } + UDCClientProcessingState GetUDCClientProcessingState() const { return mUDCClientProcessingState; } void SetUDCClientProcessingState(UDCClientProcessingState state) { mUDCClientProcessingState = state; } @@ -95,6 +100,8 @@ class UDCClientState private: PeerAddress mPeerAddress; char mInstanceName[Dnssd::Commissionable::kInstanceNameMaxLength + 1]; + char mDeviceName[Dnssd::kMaxDeviceNameLen + 1]; + uint16_t mLongDiscriminator = 0; UDCClientProcessingState mUDCClientProcessingState; uint64_t mExpirationTimeMs = 0; }; diff --git a/src/protocols/user_directed_commissioning/UDCClients.h b/src/protocols/user_directed_commissioning/UDCClients.h index e747bb692e297b..a91f5afcf161a4 100644 --- a/src/protocols/user_directed_commissioning/UDCClients.h +++ b/src/protocols/user_directed_commissioning/UDCClients.h @@ -84,6 +84,30 @@ class UDCClients return err; } + /** + * Get a UDC Client state given a Peer address. + * + * @param address is the connection to find (based on address) + * + * @return the state found, nullptr if not found + */ + CHECK_RETURN_VALUE + UDCClientState * GetUDCClientState(size_t index) + { + if (index >= kMaxClientCount) + { + return nullptr; + } + + const uint64_t currentTime = mTimeSource.GetCurrentMonotonicTimeMs(); + UDCClientState state = mStates[index]; + if (!state.IsInitialized(currentTime)) + { + return nullptr; + } + return &mStates[index]; + } + /** * Get a UDC Client state given a Peer address. * diff --git a/src/protocols/user_directed_commissioning/UserDirectedCommissioning.h b/src/protocols/user_directed_commissioning/UserDirectedCommissioning.h index f996c94796977e..d7750f6c5cb027 100644 --- a/src/protocols/user_directed_commissioning/UserDirectedCommissioning.h +++ b/src/protocols/user_directed_commissioning/UserDirectedCommissioning.h @@ -42,7 +42,7 @@ namespace Protocols { namespace UserDirectedCommissioning { // Cache contains 16 clients. This may need to be tweaked. -constexpr size_t kMaxUDCClients = 16; +constexpr uint8_t kMaxUDCClients = 16; /** * User Directed Commissioning Protocol Message Types @@ -82,7 +82,7 @@ class DLL_EXPORT UserConfirmationProvider * @param nodeData DNS-SD node information for the client requesting commissioning * */ - virtual void OnUserDirectedCommissioningRequest(const Dnssd::DiscoveredNodeData & nodeData) = 0; + virtual void OnUserDirectedCommissioningRequest(UDCClientState state) = 0; virtual ~UserConfirmationProvider() = default; }; @@ -185,6 +185,12 @@ class DLL_EXPORT UserDirectedCommissioningServer : public TransportMgrDelegate */ UDCClients GetUDCClients() { return mUdcClients; } + /** + * Print the cache of UDC Clients + * + */ + void PrintUDCClients(); + private: InstanceNameResolver * mInstanceNameResolver = nullptr; UserConfirmationProvider * mUserConfirmationProvider = nullptr; diff --git a/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp b/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp index 810e036dfa98da..a227b96fff8969 100644 --- a/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp +++ b/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp @@ -109,6 +109,17 @@ void UserDirectedCommissioningServer::SetUDCClientProcessingState(char * instanc void UserDirectedCommissioningServer::OnCommissionableNodeFound(const Dnssd::DiscoveredNodeData & nodeData) { + if (nodeData.numIPs == 0) + { + ChipLogError(AppServer, "SetUDCClientProcessingState no IP addresses returned for instance name=%s", nodeData.instanceName); + return; + } + if (nodeData.port == 0) + { + ChipLogError(AppServer, "SetUDCClientProcessingState no port returned for instance name=%s", nodeData.instanceName); + return; + } + UDCClientState * client = mUdcClients.FindUDCClientState(nodeData.instanceName); if (client != nullptr && client->GetUDCClientProcessingState() == UDCClientProcessingState::kDiscoveringNode) { @@ -116,10 +127,51 @@ void UserDirectedCommissioningServer::OnCommissionableNodeFound(const Dnssd::Dis (int) client->GetUDCClientProcessingState(), (int) UDCClientProcessingState::kPromptingUser); client->SetUDCClientProcessingState(UDCClientProcessingState::kPromptingUser); + // currently IPv6 addresses do not work for some reason + bool foundV4 = false; + for (int i = 0; i < nodeData.numIPs; ++i) + { + if (nodeData.ipAddress[i].IsIPv4()) + { + foundV4 = true; + client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[i], nodeData.port)); + break; + } + } + // use IPv6 as last resort + if (!foundV4) + { + client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[0], nodeData.port)); + } + + // client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[0], nodeData.port)); + client->SetDeviceName(nodeData.deviceName); + client->SetLongDiscriminator(nodeData.longDiscriminator); + // Call the registered mUserConfirmationProvider, if any. if (mUserConfirmationProvider != nullptr) { - mUserConfirmationProvider->OnUserDirectedCommissioningRequest(nodeData); + mUserConfirmationProvider->OnUserDirectedCommissioningRequest(*client); + } + } +} + +void UserDirectedCommissioningServer::PrintUDCClients() +{ + for (uint8_t i = 0; i < kMaxUDCClients; i++) + { + UDCClientState * state = GetUDCClients().GetUDCClientState(i); + if (state == nullptr) + { + ChipLogProgress(AppServer, "UDC Client[%d] null", i); + } + else + { + char addrBuffer[chip::Transport::PeerAddress::kMaxToStringSize]; + state->GetPeerAddress().ToString(addrBuffer); + + ChipLogProgress(AppServer, "UDC Client[%d] instance=%s deviceName=%s address=%s, disc=%d", i, state->GetInstanceName(), + state->GetDeviceName(), addrBuffer, state->GetLongDiscriminator()); } } } diff --git a/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp b/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp index a18964a5ee0dbd..87b7d01106f221 100644 --- a/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp +++ b/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp @@ -19,10 +19,10 @@ using namespace chip::Protocols::UserDirectedCommissioning; class DLL_EXPORT TestCallback : public UserConfirmationProvider, public InstanceNameResolver { public: - void OnUserDirectedCommissioningRequest(const Dnssd::DiscoveredNodeData & nodeData) + void OnUserDirectedCommissioningRequest(UDCClientState state) { mOnUserDirectedCommissioningRequestCalled = true; - mNodeData = nodeData; + mState = state; } void FindCommissionableNode(char * instanceName) @@ -32,7 +32,7 @@ class DLL_EXPORT TestCallback : public UserConfirmationProvider, public Instance } // virtual ~UserConfirmationProvider() = default; - Dnssd::DiscoveredNodeData mNodeData; + UDCClientState mState; char * mInstanceName; bool mOnUserDirectedCommissioningRequestCalled = false; @@ -60,48 +60,67 @@ void TestUDCServerUserConfirmationProvider(nlTestSuite * inSuite, void * inConte TestCallback testCallback; const char * instanceName1 = "servertest1"; const char * instanceName2 = "servertest2"; + const char * deviceName2 = "device1"; + uint16_t disc2 = 1234; UDCClientState * state; + chip::Inet::IPAddress address; + chip::Inet::IPAddress::FromString("127.0.0.1", address); // need to populate with something + // setup for tests udcServer.SetUDCClientProcessingState((char *) instanceName1, UDCClientProcessingState::kUserDeclined); + Dnssd::DiscoveredNodeData nodeData1; + nodeData1.port = 5540; + nodeData1.ipAddress[0] = address; + nodeData1.numIPs = 1; + strncpy((char *) nodeData1.instanceName, instanceName1, sizeof(nodeData1.instanceName)); + + Dnssd::DiscoveredNodeData nodeData2; + nodeData2.port = 5540; + nodeData2.ipAddress[0] = address; + nodeData2.numIPs = 1; + nodeData2.longDiscriminator = disc2; + strncpy((char *) nodeData2.instanceName, instanceName2, sizeof(nodeData2.instanceName)); + strncpy((char *) nodeData2.deviceName, deviceName2, sizeof(nodeData2.deviceName)); + // test empty UserConfirmationProvider - Dnssd::DiscoveredNodeData nodeData; - strncpy((char *) nodeData.instanceName, instanceName2, sizeof(nodeData.instanceName)); - udcServer.OnCommissionableNodeFound(nodeData); - strncpy((char *) nodeData.instanceName, instanceName1, sizeof(nodeData.instanceName)); - udcServer.OnCommissionableNodeFound(nodeData); + udcServer.OnCommissionableNodeFound(nodeData2); + udcServer.OnCommissionableNodeFound(nodeData1); state = udcServer.GetUDCClients().FindUDCClientState(instanceName1); NL_TEST_ASSERT(inSuite, nullptr != state); NL_TEST_ASSERT(inSuite, UDCClientProcessingState::kUserDeclined == state->GetUDCClientProcessingState()); + // test other fields on UDCClientState + NL_TEST_ASSERT(inSuite, 0 == strcmp(state->GetInstanceName(), instanceName1)); + // check that instance2 was found state = udcServer.GetUDCClients().FindUDCClientState(instanceName2); NL_TEST_ASSERT(inSuite, nullptr == state); // test current state check udcServer.SetUDCClientProcessingState((char *) instanceName1, UDCClientProcessingState::kUserDeclined); udcServer.SetUDCClientProcessingState((char *) instanceName2, UDCClientProcessingState::kDiscoveringNode); - strncpy((char *) nodeData.instanceName, instanceName2, sizeof(nodeData.instanceName)); - udcServer.OnCommissionableNodeFound(nodeData); - strncpy((char *) nodeData.instanceName, instanceName1, sizeof(nodeData.instanceName)); - udcServer.OnCommissionableNodeFound(nodeData); + udcServer.OnCommissionableNodeFound(nodeData2); + udcServer.OnCommissionableNodeFound(nodeData1); state = udcServer.GetUDCClients().FindUDCClientState(instanceName1); NL_TEST_ASSERT(inSuite, nullptr != state); NL_TEST_ASSERT(inSuite, UDCClientProcessingState::kUserDeclined == state->GetUDCClientProcessingState()); state = udcServer.GetUDCClients().FindUDCClientState(instanceName2); NL_TEST_ASSERT(inSuite, nullptr != state); NL_TEST_ASSERT(inSuite, UDCClientProcessingState::kPromptingUser == state->GetUDCClientProcessingState()); + // test other fields on UDCClientState + NL_TEST_ASSERT(inSuite, 0 == strcmp(state->GetInstanceName(), instanceName2)); + NL_TEST_ASSERT(inSuite, 0 == strcmp(state->GetDeviceName(), deviceName2)); + NL_TEST_ASSERT(inSuite, state->GetLongDiscriminator() == disc2); // test non-empty UserConfirmationProvider udcServer.SetUserConfirmationProvider(&testCallback); udcServer.SetUDCClientProcessingState((char *) instanceName1, UDCClientProcessingState::kUserDeclined); udcServer.SetUDCClientProcessingState((char *) instanceName2, UDCClientProcessingState::kDiscoveringNode); - strncpy((char *) nodeData.instanceName, instanceName1, sizeof(nodeData.instanceName)); - udcServer.OnCommissionableNodeFound(nodeData); + udcServer.OnCommissionableNodeFound(nodeData1); NL_TEST_ASSERT(inSuite, !testCallback.mOnUserDirectedCommissioningRequestCalled); - strncpy((char *) nodeData.instanceName, instanceName2, sizeof(nodeData.instanceName)); - udcServer.OnCommissionableNodeFound(nodeData); + udcServer.OnCommissionableNodeFound(nodeData2); NL_TEST_ASSERT(inSuite, testCallback.mOnUserDirectedCommissioningRequestCalled); - NL_TEST_ASSERT(inSuite, 0 == strcmp(testCallback.mNodeData.instanceName, instanceName2)); + NL_TEST_ASSERT(inSuite, 0 == strcmp(testCallback.mState.GetInstanceName(), instanceName2)); } void TestUDCServerInstanceNameResolver(nlTestSuite * inSuite, void * inContext)