Matter SDK Coverage Report
Current view: top level - protocols/user_directed_commissioning - UserDirectedCommissioningServer.cpp (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 50.2 % 277 139
Test Date: 2025-01-17 19:00:11 Functions: 36.4 % 11 4

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2021-2022 Project CHIP Authors
       4              :  *    All rights reserved.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : 
      19              : /**
      20              :  *    @file
      21              :  *      This file implements an object for a Matter User Directed Commissioning unsolicited
      22              :  *      recipient (server).
      23              :  *
      24              :  */
      25              : 
      26              : #include "UserDirectedCommissioning.h"
      27              : #include <lib/core/CHIPSafeCasts.h>
      28              : #include <system/TLVPacketBufferBackingStore.h>
      29              : #include <transport/raw/Base.h>
      30              : 
      31              : #include <unistd.h>
      32              : 
      33              : namespace chip {
      34              : namespace Protocols {
      35              : namespace UserDirectedCommissioning {
      36              : 
      37            0 : void UserDirectedCommissioningServer::OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msg,
      38              :                                                         Transport::MessageTransportContext * ctxt)
      39              : {
      40              :     char addrBuffer[chip::Transport::PeerAddress::kMaxToStringSize];
      41            0 :     source.ToString(addrBuffer);
      42            0 :     ChipLogProgress(AppServer, "UserDirectedCommissioningServer::OnMessageReceived from %s", addrBuffer);
      43              : 
      44            0 :     PacketHeader packetHeader;
      45              : 
      46            0 :     ReturnOnFailure(packetHeader.DecodeAndConsume(msg));
      47              : 
      48            0 :     if (packetHeader.IsEncrypted())
      49              :     {
      50            0 :         ChipLogError(AppServer, "UDC encryption flag set - ignoring");
      51            0 :         return;
      52              :     }
      53              : 
      54            0 :     PayloadHeader payloadHeader;
      55            0 :     ReturnOnFailure(payloadHeader.DecodeAndConsume(msg));
      56              : 
      57            0 :     ChipLogProgress(AppServer, "IdentityDeclaration DataLength()=%" PRIu32, static_cast<uint32_t>(msg->DataLength()));
      58              : 
      59              :     uint8_t udcPayload[IdentificationDeclaration::kUdcTLVDataMaxBytes];
      60            0 :     size_t udcPayloadLength = std::min<size_t>(msg->DataLength(), sizeof(udcPayload));
      61            0 :     msg->Read(udcPayload, udcPayloadLength);
      62              : 
      63            0 :     IdentificationDeclaration id;
      64            0 :     id.ReadPayload(udcPayload, sizeof(udcPayload));
      65              : 
      66            0 :     if (id.GetCancelPasscode())
      67              :     {
      68            0 :         HandleUDCCancel(id);
      69            0 :         return;
      70              :     }
      71              : 
      72            0 :     if (id.GetCommissionerPasscodeReady())
      73              :     {
      74            0 :         HandleUDCCommissionerPasscodeReady(id);
      75            0 :         return;
      76              :     }
      77              : 
      78            0 :     HandleNewUDC(source, id);
      79            0 : }
      80              : 
      81            0 : void UserDirectedCommissioningServer::HandleNewUDC(const Transport::PeerAddress & source, IdentificationDeclaration & id)
      82              : {
      83            0 :     char * instanceName = (char *) id.GetInstanceName();
      84            0 :     ChipLogProgress(AppServer, "HandleNewUDC instance=%s ", id.GetInstanceName());
      85              : 
      86            0 :     UDCClientState * client = mUdcClients.FindUDCClientState(instanceName);
      87            0 :     if (client == nullptr)
      88              :     {
      89            0 :         ChipLogProgress(AppServer, "UDC new instance state received");
      90              : 
      91            0 :         id.DebugLog();
      92              : 
      93              :         CHIP_ERROR err;
      94            0 :         err = mUdcClients.CreateNewUDCClientState(instanceName, &client);
      95            0 :         if (err != CHIP_NO_ERROR)
      96              :         {
      97            0 :             ChipLogError(AppServer, "UDC error creating new connection state");
      98            0 :             return;
      99              :         }
     100              : 
     101            0 :         if (id.HasDiscoveryInfo())
     102              :         {
     103              :             // if we received mDNS info, skip the commissionable lookup
     104            0 :             ChipLogDetail(AppServer, "UDC discovery info provided");
     105            0 :             mUdcClients.MarkUDCClientActive(client);
     106              : 
     107            0 :             client->SetUDCClientProcessingState(UDCClientProcessingState::kPromptingUser);
     108            0 :             client->SetPeerAddress(source);
     109              : 
     110            0 :             id.UpdateClientState(client);
     111              : 
     112              :             // Call the registered mUserConfirmationProvider, if any.
     113            0 :             if (mUserConfirmationProvider != nullptr)
     114              :             {
     115            0 :                 mUserConfirmationProvider->OnUserDirectedCommissioningRequest(*client);
     116              :             }
     117            0 :             return;
     118              :         }
     119              : 
     120              :         // Call the registered InstanceNameResolver, if any.
     121            0 :         if (mInstanceNameResolver != nullptr)
     122              :         {
     123            0 :             mInstanceNameResolver->FindCommissionableNode(instanceName);
     124              :         }
     125              :         else
     126              :         {
     127            0 :             ChipLogError(AppServer, "UserDirectedCommissioningServer::OnMessageReceived no mInstanceNameResolver registered");
     128              :         }
     129              :     }
     130            0 :     mUdcClients.MarkUDCClientActive(client);
     131              : }
     132              : 
     133            0 : void UserDirectedCommissioningServer::HandleUDCCancel(IdentificationDeclaration & id)
     134              : {
     135            0 :     char * instanceName = (char *) id.GetInstanceName();
     136            0 :     ChipLogProgress(AppServer, "HandleUDCCancel instance=%s ", id.GetInstanceName());
     137              : 
     138            0 :     UDCClientState * client = mUdcClients.FindUDCClientState(instanceName);
     139            0 :     if (client == nullptr)
     140              :     {
     141            0 :         ChipLogProgress(AppServer, "UDC no matching instance found");
     142            0 :         return;
     143              :     }
     144            0 :     id.DebugLog();
     145            0 :     mUdcClients.MarkUDCClientActive(client);
     146              : 
     147              :     // Call the registered mUserConfirmationProvider, if any.
     148            0 :     if (mUserConfirmationProvider != nullptr)
     149              :     {
     150            0 :         mUserConfirmationProvider->OnCancel(*client);
     151              :     }
     152              : 
     153              :     // reset this entry so that the client can try again without waiting an hour
     154            0 :     client->Reset();
     155              : }
     156              : 
     157            0 : void UserDirectedCommissioningServer::HandleUDCCommissionerPasscodeReady(IdentificationDeclaration & id)
     158              : {
     159            0 :     char * instanceName = (char *) id.GetInstanceName();
     160            0 :     ChipLogProgress(AppServer, "HandleUDCCommissionerPasscodeReady instance=%s ", id.GetInstanceName());
     161              : 
     162            0 :     UDCClientState * client = mUdcClients.FindUDCClientState(instanceName);
     163            0 :     if (client == nullptr)
     164              :     {
     165            0 :         ChipLogProgress(AppServer, "UDC no matching instance found");
     166            0 :         return;
     167              :     }
     168            0 :     if (client->GetUDCClientProcessingState() != UDCClientProcessingState::kWaitingForCommissionerPasscodeReady)
     169              :     {
     170            0 :         ChipLogProgress(AppServer, "UDC instance not in waiting for passcode ready state");
     171            0 :         return;
     172              :     }
     173            0 :     id.DebugLog();
     174            0 :     mUdcClients.MarkUDCClientActive(client);
     175            0 :     client->SetUDCClientProcessingState(UDCClientProcessingState::kObtainingOnboardingPayload);
     176              : 
     177              :     // Call the registered mUserConfirmationProvider, if any.
     178            0 :     if (mUserConfirmationProvider != nullptr)
     179              :     {
     180            0 :         mUserConfirmationProvider->OnCommissionerPasscodeReady(*client);
     181              :     }
     182              : }
     183              : 
     184            0 : CHIP_ERROR UserDirectedCommissioningServer::SendCDCMessage(CommissionerDeclaration cd, chip::Transport::PeerAddress peerAddress)
     185              : {
     186            0 :     if (mTransportMgr == nullptr)
     187              :     {
     188            0 :         ChipLogError(AppServer, "CDC: No transport manager\n");
     189            0 :         return CHIP_ERROR_INCORRECT_STATE;
     190              :     }
     191              :     uint8_t idBuffer[IdentificationDeclaration::kUdcTLVDataMaxBytes];
     192            0 :     uint32_t length = cd.WritePayload(idBuffer, sizeof(idBuffer));
     193            0 :     if (length == 0)
     194              :     {
     195            0 :         ChipLogError(AppServer, "CDC: error writing payload\n");
     196            0 :         return CHIP_ERROR_INTERNAL;
     197              :     }
     198              : 
     199            0 :     chip::System::PacketBufferHandle payload = chip::MessagePacketBuffer::NewWithData(idBuffer, length);
     200            0 :     if (payload.IsNull())
     201              :     {
     202            0 :         ChipLogError(AppServer, "Unable to allocate packet buffer\n");
     203            0 :         return CHIP_ERROR_NO_MEMORY;
     204              :     }
     205            0 :     ReturnErrorOnFailure(EncodeUDCMessage(payload));
     206              : 
     207            0 :     cd.DebugLog();
     208            0 :     ChipLogProgress(Inet, "Sending CDC msg");
     209              : 
     210            0 :     auto err = mTransportMgr->SendMessage(peerAddress, std::move(payload));
     211            0 :     if (err != CHIP_NO_ERROR)
     212              :     {
     213            0 :         ChipLogError(AppServer, "CDC SendMessage failed: %" CHIP_ERROR_FORMAT, err.Format());
     214            0 :         return err;
     215              :     }
     216              : 
     217            0 :     ChipLogProgress(Inet, "CDC msg sent");
     218            0 :     return CHIP_NO_ERROR;
     219            0 : }
     220              : 
     221            0 : CHIP_ERROR UserDirectedCommissioningServer::EncodeUDCMessage(const System::PacketBufferHandle & payload)
     222              : {
     223            0 :     PayloadHeader payloadHeader;
     224            0 :     PacketHeader packetHeader;
     225              : 
     226            0 :     payloadHeader.SetMessageType(MsgType::IdentificationDeclaration).SetInitiator(true).SetNeedsAck(false);
     227              : 
     228            0 :     VerifyOrReturnError(!payload.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
     229            0 :     VerifyOrReturnError(!payload->HasChainedBuffer(), CHIP_ERROR_INVALID_MESSAGE_LENGTH);
     230            0 :     VerifyOrReturnError(payload->TotalLength() <= kMaxAppMessageLen, CHIP_ERROR_MESSAGE_TOO_LONG);
     231              : 
     232            0 :     ReturnErrorOnFailure(payloadHeader.EncodeBeforeData(payload));
     233              : 
     234            0 :     ReturnErrorOnFailure(packetHeader.EncodeBeforeData(payload));
     235              : 
     236            0 :     return CHIP_NO_ERROR;
     237            0 : }
     238              : 
     239            1 : CHIP_ERROR IdentificationDeclaration::ReadPayload(uint8_t * udcPayload, size_t payloadBufferSize)
     240              : {
     241            1 :     size_t i = 0;
     242           12 :     while (i < std::min<size_t>(sizeof(mInstanceName), payloadBufferSize) && udcPayload[i] != '\0')
     243              :     {
     244           11 :         mInstanceName[i] = (char) udcPayload[i];
     245           11 :         i++;
     246              :     }
     247            1 :     mInstanceName[i] = '\0';
     248              : 
     249            1 :     if (payloadBufferSize <= sizeof(mInstanceName))
     250              :     {
     251            0 :         ChipLogProgress(AppServer, "UDC - No TLV information in Identification Declaration");
     252            0 :         return CHIP_NO_ERROR;
     253              :     }
     254              :     // advance i to the end of the fixed length block containing instance name
     255            1 :     i = sizeof(mInstanceName);
     256              : 
     257              :     CHIP_ERROR err;
     258              : 
     259            1 :     TLV::TLVReader reader;
     260            1 :     reader.Init(udcPayload + i, payloadBufferSize - i);
     261              : 
     262              :     // read the envelope
     263            1 :     ReturnErrorOnFailure(reader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag()));
     264              : 
     265            1 :     chip::TLV::TLVType outerContainerType = chip::TLV::kTLVType_Structure;
     266            1 :     ReturnErrorOnFailure(reader.EnterContainer(outerContainerType));
     267              : 
     268           14 :     while ((err = reader.Next()) == CHIP_NO_ERROR)
     269              :     {
     270           13 :         chip::TLV::Tag containerTag = reader.GetTag();
     271           13 :         if (!TLV::IsContextTag(containerTag))
     272              :         {
     273            0 :             ChipLogError(AppServer, "Unexpected non-context TLV tag.");
     274            0 :             return CHIP_ERROR_INVALID_TLV_TAG;
     275              :         }
     276           13 :         uint8_t tagNum = static_cast<uint8_t>(chip::TLV::TagNumFromTag(containerTag));
     277              : 
     278           13 :         switch (tagNum)
     279              :         {
     280            1 :         case kVendorIdTag:
     281              :             // vendorId
     282            1 :             err = reader.Get(mVendorId);
     283            1 :             break;
     284            1 :         case kProductIdTag:
     285              :             // productId
     286            1 :             err = reader.Get(mProductId);
     287            1 :             break;
     288            1 :         case kCdPortTag:
     289              :             // port
     290            1 :             err = reader.Get(mCdPort);
     291            1 :             break;
     292            1 :         case kDeviceNameTag:
     293              :             // deviceName
     294            1 :             err = reader.GetString(mDeviceName, sizeof(mDeviceName));
     295            1 :             break;
     296            1 :         case kPairingInstTag:
     297              :             // pairingInst
     298            1 :             err = reader.GetString(mPairingInst, sizeof(mPairingInst));
     299            1 :             break;
     300            1 :         case kPairingHintTag:
     301              :             // pairingHint
     302            1 :             err = reader.Get(mPairingHint);
     303            1 :             break;
     304            1 :         case kRotatingIdTag:
     305              :             // rotatingId
     306            1 :             mRotatingIdLen = reader.GetLength();
     307            1 :             err            = reader.GetBytes(mRotatingId, sizeof(mRotatingId));
     308            1 :             break;
     309            1 :         case kTargetAppListTag:
     310              :             // app vendor list
     311              :             {
     312            1 :                 ChipLogProgress(AppServer, "TLV found an applist");
     313            1 :                 chip::TLV::TLVType listContainerType = chip::TLV::kTLVType_List;
     314            1 :                 ReturnErrorOnFailure(reader.EnterContainer(listContainerType));
     315              : 
     316            4 :                 while ((err = reader.Next()) == CHIP_NO_ERROR && mNumTargetAppInfos < sizeof(mTargetAppInfos))
     317              :                 {
     318            3 :                     containerTag = reader.GetTag();
     319            3 :                     if (!TLV::IsContextTag(containerTag))
     320              :                     {
     321            0 :                         ChipLogError(AppServer, "Unexpected non-context TLV tag.");
     322            0 :                         return CHIP_ERROR_INVALID_TLV_TAG;
     323              :                     }
     324            3 :                     tagNum = static_cast<uint8_t>(chip::TLV::TagNumFromTag(containerTag));
     325            3 :                     if (tagNum == kTargetAppTag)
     326              :                     {
     327            3 :                         ReturnErrorOnFailure(reader.EnterContainer(outerContainerType));
     328            3 :                         uint16_t appVendorId  = 0;
     329            3 :                         uint16_t appProductId = 0;
     330              : 
     331            9 :                         while ((err = reader.Next()) == CHIP_NO_ERROR)
     332              :                         {
     333            6 :                             containerTag = reader.GetTag();
     334            6 :                             if (!TLV::IsContextTag(containerTag))
     335              :                             {
     336            0 :                                 ChipLogError(AppServer, "Unexpected non-context TLV tag.");
     337            0 :                                 return CHIP_ERROR_INVALID_TLV_TAG;
     338              :                             }
     339            6 :                             tagNum = static_cast<uint8_t>(chip::TLV::TagNumFromTag(containerTag));
     340            6 :                             if (tagNum == kAppVendorIdTag)
     341              :                             {
     342            3 :                                 err = reader.Get(appVendorId);
     343              :                             }
     344            3 :                             else if (tagNum == kAppProductIdTag)
     345              :                             {
     346            3 :                                 err = reader.Get(appProductId);
     347              :                             }
     348              :                         }
     349            3 :                         if (err == CHIP_END_OF_TLV)
     350              :                         {
     351            3 :                             ChipLogProgress(AppServer, "TLV end of struct TLV");
     352            3 :                             ReturnErrorOnFailure(reader.ExitContainer(outerContainerType));
     353              :                         }
     354            3 :                         if (appVendorId != 0)
     355              :                         {
     356            3 :                             mTargetAppInfos[mNumTargetAppInfos].vendorId  = appVendorId;
     357            3 :                             mTargetAppInfos[mNumTargetAppInfos].productId = appProductId;
     358            3 :                             mNumTargetAppInfos++;
     359              :                         }
     360              :                     }
     361              :                     else
     362              :                     {
     363            0 :                         ChipLogError(AppServer, "unrecognized tag %d", tagNum);
     364              :                     }
     365              :                 }
     366            1 :                 if (err == CHIP_END_OF_TLV)
     367              :                 {
     368            1 :                     ChipLogProgress(AppServer, "TLV end of array");
     369            1 :                     ReturnErrorOnFailure(reader.ExitContainer(listContainerType));
     370            1 :                     err = CHIP_NO_ERROR;
     371              :                 }
     372              :             }
     373            1 :             break;
     374            1 :         case kNoPasscodeTag:
     375            1 :             err = reader.Get(mNoPasscode);
     376            1 :             break;
     377            1 :         case kCdUponPasscodeDialogTag:
     378            1 :             err = reader.Get(mCdUponPasscodeDialog);
     379            1 :             break;
     380            1 :         case kCommissionerPasscodeTag:
     381            1 :             err = reader.Get(mCommissionerPasscode);
     382            1 :             break;
     383            1 :         case kCommissionerPasscodeReadyTag:
     384            1 :             err = reader.Get(mCommissionerPasscodeReady);
     385            1 :             break;
     386            1 :         case kCancelPasscodeTag:
     387            1 :             err = reader.Get(mCancelPasscode);
     388            1 :             break;
     389              :         }
     390           13 :         if (err != CHIP_NO_ERROR)
     391              :         {
     392            0 :             ChipLogError(AppServer, "IdentificationDeclaration::ReadPayload read error %" CHIP_ERROR_FORMAT, err.Format());
     393              :         }
     394              :     }
     395              : 
     396            1 :     if (err == CHIP_END_OF_TLV)
     397              :     {
     398              :         // Exiting container
     399            1 :         ReturnErrorOnFailure(reader.ExitContainer(outerContainerType));
     400              :     }
     401              :     else
     402              :     {
     403            0 :         ChipLogError(AppServer, "IdentificationDeclaration::ReadPayload exiting early error %" CHIP_ERROR_FORMAT, err.Format());
     404              :     }
     405              : 
     406            1 :     ChipLogProgress(AppServer, "UDC TLV parse complete");
     407            1 :     return CHIP_NO_ERROR;
     408              : }
     409              : 
     410              : /**
     411              :  *  Reset the connection state to a completely uninitialized status.
     412              :  */
     413            1 : uint32_t CommissionerDeclaration::WritePayload(uint8_t * payloadBuffer, size_t payloadBufferSize)
     414              : {
     415              :     CHIP_ERROR err;
     416              : 
     417            1 :     chip::TLV::TLVWriter writer;
     418              : 
     419            1 :     writer.Init(payloadBuffer, payloadBufferSize);
     420              : 
     421            1 :     chip::TLV::TLVType outerContainerType = chip::TLV::kTLVType_Structure;
     422            1 :     VerifyOrExit(CHIP_NO_ERROR ==
     423              :                      (err = writer.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, outerContainerType)),
     424              :                  LogErrorOnFailure(err));
     425              : 
     426            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.Put(chip::TLV::ContextTag(kErrorCodeTag), GetErrorCode())), LogErrorOnFailure(err));
     427            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.PutBoolean(chip::TLV::ContextTag(kNeedsPasscodeTag), mNeedsPasscode)),
     428              :                  LogErrorOnFailure(err));
     429            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.PutBoolean(chip::TLV::ContextTag(kNoAppsFoundTag), mNoAppsFound)),
     430              :                  LogErrorOnFailure(err));
     431            1 :     VerifyOrExit(CHIP_NO_ERROR ==
     432              :                      (err = writer.PutBoolean(chip::TLV::ContextTag(kPasscodeDialogDisplayedTag), mPasscodeDialogDisplayed)),
     433              :                  LogErrorOnFailure(err));
     434            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.PutBoolean(chip::TLV::ContextTag(kCommissionerPasscodeTag), mCommissionerPasscode)),
     435              :                  LogErrorOnFailure(err));
     436            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.PutBoolean(chip::TLV::ContextTag(kQRCodeDisplayedTag), mQRCodeDisplayed)),
     437              :                  LogErrorOnFailure(err));
     438            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.PutBoolean(chip::TLV::ContextTag(kCancelPasscodeTag), mCancelPasscode)),
     439              :                  LogErrorOnFailure(err));
     440              : 
     441            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.EndContainer(outerContainerType)), LogErrorOnFailure(err));
     442            1 :     VerifyOrExit(CHIP_NO_ERROR == (err = writer.Finalize()), LogErrorOnFailure(err));
     443              : 
     444            1 :     ChipLogProgress(AppServer, "TLV write done");
     445              : 
     446            1 :     return writer.GetLengthWritten();
     447              : 
     448            0 : exit:
     449            0 :     return 0;
     450              : }
     451              : 
     452            6 : void UserDirectedCommissioningServer::SetUDCClientProcessingState(char * instanceName, UDCClientProcessingState state)
     453              : {
     454            6 :     UDCClientState * client = mUdcClients.FindUDCClientState(instanceName);
     455            6 :     if (client == nullptr)
     456              :     {
     457              :         CHIP_ERROR err;
     458            3 :         err = mUdcClients.CreateNewUDCClientState(instanceName, &client);
     459            3 :         if (err != CHIP_NO_ERROR)
     460              :         {
     461            0 :             ChipLogError(AppServer,
     462              :                          "UserDirectedCommissioningServer::SetUDCClientProcessingState error creating new connection state");
     463            0 :             return;
     464              :         }
     465              :     }
     466              : 
     467            6 :     ChipLogDetail(AppServer, "SetUDCClientProcessingState instance=%s new state=%d", StringOrNullMarker(instanceName), (int) state);
     468              : 
     469            6 :     client->SetUDCClientProcessingState(state);
     470              : 
     471            6 :     mUdcClients.MarkUDCClientActive(client);
     472              : }
     473              : 
     474            6 : void UserDirectedCommissioningServer::OnCommissionableNodeFound(const Dnssd::DiscoveredNodeData & discNodeData)
     475              : {
     476            6 :     if (!discNodeData.Is<Dnssd::CommissionNodeData>())
     477              :     {
     478            0 :         return;
     479              :     }
     480              : 
     481            6 :     const Dnssd::CommissionNodeData & nodeData = discNodeData.Get<Dnssd::CommissionNodeData>();
     482            6 :     if (nodeData.numIPs == 0)
     483              :     {
     484            0 :         ChipLogError(AppServer, "OnCommissionableNodeFound no IP addresses returned for instance name=%s", nodeData.instanceName);
     485            0 :         return;
     486              :     }
     487            6 :     if (nodeData.port == 0)
     488              :     {
     489            0 :         ChipLogError(AppServer, "OnCommissionableNodeFound no port returned for instance name=%s", nodeData.instanceName);
     490            0 :         return;
     491              :     }
     492              : 
     493            6 :     UDCClientState * client = mUdcClients.FindUDCClientState(nodeData.instanceName);
     494            6 :     if (client != nullptr && client->GetUDCClientProcessingState() == UDCClientProcessingState::kDiscoveringNode)
     495              :     {
     496            2 :         ChipLogDetail(AppServer, "OnCommissionableNodeFound instance: name=%s old_state=%d new_state=%d", client->GetInstanceName(),
     497              :                       (int) client->GetUDCClientProcessingState(), (int) UDCClientProcessingState::kPromptingUser);
     498            2 :         client->SetUDCClientProcessingState(UDCClientProcessingState::kPromptingUser);
     499              : 
     500              : #if INET_CONFIG_ENABLE_IPV4
     501              :         // prefer IPv4 if its an option
     502            2 :         bool foundV4 = false;
     503            2 :         for (unsigned i = 0; i < nodeData.numIPs; ++i)
     504              :         {
     505            2 :             if (nodeData.ipAddress[i].IsIPv4())
     506              :             {
     507            2 :                 foundV4 = true;
     508            2 :                 client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[i], nodeData.port));
     509            2 :                 break;
     510              :             }
     511              :         }
     512              :         // use IPv6 as last resort
     513            2 :         if (!foundV4)
     514              :         {
     515            0 :             client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[0], nodeData.port));
     516              :         }
     517              : #else  // INET_CONFIG_ENABLE_IPV4
     518              :        // if we only support V6, then try to find a v6 address
     519              :         bool foundV6 = false;
     520              :         for (unsigned i = 0; i < nodeData.numIPs; ++i)
     521              :         {
     522              :             if (nodeData.ipAddress[i].IsIPv6())
     523              :             {
     524              :                 foundV6 = true;
     525              :                 client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[i], nodeData.port));
     526              :                 break;
     527              :             }
     528              :         }
     529              :         // last resort, try with what we have
     530              :         if (!foundV6)
     531              :         {
     532              :             ChipLogError(AppServer, "OnCommissionableNodeFound no v6 returned for instance name=%s", nodeData.instanceName);
     533              :             client->SetPeerAddress(chip::Transport::PeerAddress::UDP(nodeData.ipAddress[0], nodeData.port));
     534              :         }
     535              : #endif // INET_CONFIG_ENABLE_IPV4
     536              : 
     537            2 :         client->SetDeviceName(nodeData.deviceName);
     538            2 :         client->SetLongDiscriminator(nodeData.longDiscriminator);
     539            2 :         client->SetVendorId(nodeData.vendorId);
     540            2 :         client->SetProductId(nodeData.productId);
     541            2 :         client->SetRotatingId(nodeData.rotatingId, nodeData.rotatingIdLen);
     542              : 
     543              :         // Call the registered mUserConfirmationProvider, if any.
     544            2 :         if (mUserConfirmationProvider != nullptr)
     545              :         {
     546            1 :             mUserConfirmationProvider->OnUserDirectedCommissioningRequest(*client);
     547              :         }
     548              :     }
     549              : }
     550              : 
     551            0 : void UserDirectedCommissioningServer::PrintUDCClients()
     552              : {
     553            0 :     for (uint8_t i = 0; i < kMaxUDCClients; i++)
     554              :     {
     555            0 :         UDCClientState * state = GetUDCClients().GetUDCClientState(i);
     556            0 :         if (state == nullptr)
     557              :         {
     558            0 :             ChipLogProgress(AppServer, "UDC Client[%d] null", i);
     559              :         }
     560              :         else
     561              :         {
     562              :             char addrBuffer[chip::Transport::PeerAddress::kMaxToStringSize];
     563            0 :             state->GetPeerAddress().ToString(addrBuffer);
     564              : 
     565            0 :             char rotatingIdString[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
     566            0 :             Encoding::BytesToUppercaseHexString(state->GetRotatingId(), chip::Dnssd::kMaxRotatingIdLen, rotatingIdString,
     567              :                                                 sizeof(rotatingIdString));
     568              : 
     569            0 :             ChipLogProgress(AppServer, "UDC Client[%d] instance=%s deviceName=%s address=%s, vid/pid=%d/%d disc=%d rid=%s", i,
     570              :                             state->GetInstanceName(), state->GetDeviceName(), addrBuffer, state->GetVendorId(),
     571              :                             state->GetProductId(), state->GetLongDiscriminator(), rotatingIdString);
     572              :         }
     573              :     }
     574            0 : }
     575              : 
     576              : } // namespace UserDirectedCommissioning
     577              : } // namespace Protocols
     578              : } // namespace chip
        

Generated by: LCOV version 2.0-1