LCOV - code coverage report
Current view: top level - protocols/user_directed_commissioning - UserDirectedCommissioning.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 1 98 1.0 %
Date: 2024-02-15 08:20:41 Functions: 1 26 3.8 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2021 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 defines objects for a User-Directed Commissioning unsolicited
      22             :  *      initiator (client) and recipient (server).
      23             :  *
      24             :  */
      25             : 
      26             : #pragma once
      27             : 
      28             : #include "UDCClients.h"
      29             : #include <lib/core/CHIPCore.h>
      30             : #include <lib/dnssd/Resolver.h>
      31             : #include <lib/support/CodeUtils.h>
      32             : #include <lib/support/DLLUtil.h>
      33             : #include <lib/support/logging/CHIPLogging.h>
      34             : #include <messaging/ExchangeContext.h>
      35             : #include <messaging/ExchangeMgr.h>
      36             : #include <messaging/Flags.h>
      37             : #include <protocols/Protocols.h>
      38             : #include <transport/TransportMgr.h>
      39             : 
      40             : namespace chip {
      41             : namespace Protocols {
      42             : namespace UserDirectedCommissioning {
      43             : 
      44             : inline constexpr char kProtocolName[] = "UserDirectedCommissioning";
      45             : 
      46             : // Cache contains 16 clients. This may need to be tweaked.
      47             : inline constexpr uint8_t kMaxUDCClients = 16;
      48             : 
      49             : /**
      50             :  * User Directed Commissioning Protocol Message Types
      51             :  */
      52             : enum class MsgType : uint8_t
      53             : {
      54             :     IdentificationDeclaration = 0x00,
      55             : };
      56             : 
      57             : /**
      58             :  * Represents the Identification Delaration message
      59             :  * sent by a UDC client to a UDC server.
      60             :  *
      61             :  * ### IdentificationDeclaration format
      62             :  *
      63             :  * <pre>
      64             :  *  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
      65             :  *  ┃ instance name '\n'        ┃ ignore   ┃ additional data TLV ┃
      66             :  *  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━┛
      67             :  *  │← · · kInstanceNameMaxLength + 1 · · →│← TLV DataLength()  →│
      68             :  *
      69             :  * Commissioning kInstanceNameMaxLength is 16
      70             :  * </pre>
      71             :  *
      72             :  */
      73             : class DLL_EXPORT IdentificationDeclaration
      74             : {
      75             : public:
      76             :     constexpr static size_t kUdcTLVDataMaxBytes = 500;
      77             : 
      78           0 :     const char * GetInstanceName() const { return mInstanceName; }
      79             :     void SetInstanceName(const char * instanceName) { Platform::CopyString(mInstanceName, instanceName); }
      80             : 
      81           0 :     bool HasDiscoveryInfo()
      82             :     {
      83           0 :         return mVendorId != 0 || mProductId != 0 || mCdPort != 0 || strlen(mDeviceName) > 0 || GetRotatingIdLength() > 0 ||
      84           0 :             mNumTargetAppInfos > 0 || mNoPasscode || mCdUponPasscodeDialog || mCommissionerPasscode || mCommissionerPasscodeReady;
      85             :     }
      86             : 
      87           0 :     const char * GetDeviceName() const { return mDeviceName; }
      88             :     void SetDeviceName(const char * deviceName) { Platform::CopyString(mDeviceName, deviceName); }
      89             : 
      90           0 :     uint16_t GetCdPort() const { return mCdPort; }
      91             :     void SetCdPort(uint16_t port) { mCdPort = port; }
      92             : 
      93           0 :     uint16_t GetVendorId() const { return mVendorId; }
      94             :     void SetVendorId(uint16_t vendorId) { mVendorId = vendorId; }
      95             : 
      96           0 :     uint16_t GetProductId() const { return mProductId; }
      97             :     void SetProductId(uint16_t productId) { mProductId = productId; }
      98             : 
      99           0 :     const uint8_t * GetRotatingId() const { return mRotatingId; }
     100           0 :     size_t GetRotatingIdLength() const { return mRotatingIdLen; }
     101             :     void SetRotatingId(const uint8_t * rotatingId, size_t rotatingIdLen)
     102             :     {
     103             :         size_t maxSize = ArraySize(mRotatingId);
     104             :         mRotatingIdLen = (maxSize < rotatingIdLen) ? maxSize : rotatingIdLen;
     105             :         memcpy(mRotatingId, rotatingId, mRotatingIdLen);
     106             :     }
     107             : 
     108           0 :     bool GetTargetAppInfo(uint8_t index, TargetAppInfo & info) const
     109             :     {
     110           0 :         if (index < mNumTargetAppInfos)
     111             :         {
     112           0 :             info.vendorId  = mTargetAppInfos[index].vendorId;
     113           0 :             info.productId = mTargetAppInfos[index].productId;
     114           0 :             return true;
     115             :         }
     116           0 :         return false;
     117             :     }
     118           0 :     uint8_t GetNumTargetAppInfos() const { return mNumTargetAppInfos; }
     119             : 
     120             :     bool AddTargetAppInfo(TargetAppInfo vid)
     121             :     {
     122             :         if (mNumTargetAppInfos >= sizeof(mTargetAppInfos))
     123             :         {
     124             :             // already at max
     125             :             return false;
     126             :         }
     127             :         mTargetAppInfos[mNumTargetAppInfos].vendorId  = vid.vendorId;
     128             :         mTargetAppInfos[mNumTargetAppInfos].productId = vid.productId;
     129             :         mNumTargetAppInfos++;
     130             :         return true;
     131             :     }
     132             : 
     133           0 :     const char * GetPairingInst() const { return mPairingInst; }
     134             :     void SetPairingInst(const char * pairingInst) { Platform::CopyString(mPairingInst, pairingInst); }
     135             : 
     136           0 :     uint16_t GetPairingHint() const { return mPairingHint; }
     137             :     void SetPairingHint(uint16_t pairingHint) { mPairingHint = pairingHint; }
     138             : 
     139             :     void SetNoPasscode(bool newValue) { mNoPasscode = newValue; };
     140           0 :     bool GetNoPasscode() const { return mNoPasscode; };
     141             : 
     142             :     void SetCdUponPasscodeDialog(bool newValue) { mCdUponPasscodeDialog = newValue; };
     143           0 :     bool GetCdUponPasscodeDialog() const { return mCdUponPasscodeDialog; };
     144             : 
     145             :     void SetCommissionerPasscode(bool newValue) { mCommissionerPasscode = newValue; };
     146           0 :     bool GetCommissionerPasscode() const { return mCommissionerPasscode; };
     147             : 
     148             :     void SetCommissionerPasscodeReady(bool newValue) { mCommissionerPasscodeReady = newValue; };
     149           0 :     bool GetCommissionerPasscodeReady() const { return mCommissionerPasscodeReady; };
     150             : 
     151             :     void SetCancelPasscode(bool newValue) { mCancelPasscode = newValue; };
     152           0 :     bool GetCancelPasscode() const { return mCancelPasscode; };
     153             : 
     154             :     /**
     155             :      *  Writes the IdentificationDeclaration message to the given buffer.
     156             :      *
     157             :      * @return Total number of bytes written or 0 if an error occurred.
     158             :      */
     159             :     uint32_t WritePayload(uint8_t * payloadBuffer, size_t payloadBufferSize);
     160             : 
     161             :     /**
     162             :      *  Reads the IdentificationDeclaration message from the given buffer.
     163             :      */
     164             :     CHIP_ERROR ReadPayload(uint8_t * payloadBuffer, size_t payloadBufferSize);
     165             : 
     166             :     /**
     167             :      *  Assigns fields from this Identification Declaration to the given UDC client state.
     168             :      */
     169           0 :     void UpdateClientState(UDCClientState * client)
     170             :     {
     171           0 :         client->SetDeviceName(GetDeviceName());
     172           0 :         client->SetVendorId(GetVendorId());
     173           0 :         client->SetProductId(GetProductId());
     174           0 :         client->SetRotatingId(GetRotatingId(), GetRotatingIdLength());
     175           0 :         client->SetPairingInst(GetPairingInst());
     176           0 :         client->SetPairingHint(GetPairingHint());
     177           0 :         for (uint8_t i = 0; i < GetNumTargetAppInfos(); i++)
     178             :         {
     179           0 :             TargetAppInfo info;
     180           0 :             if (GetTargetAppInfo(i, info))
     181             :             {
     182           0 :                 client->AddTargetAppInfo(info);
     183             :             }
     184             :         }
     185             : 
     186           0 :         client->SetCdPort(GetCdPort());
     187           0 :         client->SetNoPasscode(GetNoPasscode());
     188           0 :         client->SetCdUponPasscodeDialog(GetCdUponPasscodeDialog());
     189           0 :         client->SetCommissionerPasscode(GetCommissionerPasscode());
     190           0 :         client->SetCommissionerPasscodeReady(GetCommissionerPasscodeReady());
     191           0 :         client->SetCancelPasscode(GetCancelPasscode());
     192           0 :     }
     193             : 
     194           0 :     void DebugLog()
     195             :     {
     196           0 :         ChipLogDetail(AppServer, "---- Identification Declaration Start ----");
     197             : 
     198           0 :         ChipLogDetail(AppServer, "\tinstance: %s", mInstanceName);
     199           0 :         if (strlen(mDeviceName) != 0)
     200             :         {
     201           0 :             ChipLogDetail(AppServer, "\tdevice Name: %s", mDeviceName);
     202             :         }
     203           0 :         if (mVendorId != 0)
     204             :         {
     205           0 :             ChipLogDetail(AppServer, "\tvendor id: %d", mVendorId);
     206             :         }
     207           0 :         if (mProductId != 0)
     208             :         {
     209           0 :             ChipLogDetail(AppServer, "\tproduct id: %d", mProductId);
     210             :         }
     211           0 :         if (mCdPort != 0)
     212             :         {
     213           0 :             ChipLogDetail(AppServer, "\tcd port: %d", mCdPort);
     214             :         }
     215           0 :         if (mRotatingIdLen > 0)
     216             :         {
     217           0 :             char rotatingIdString[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
     218           0 :             Encoding::BytesToUppercaseHexString(mRotatingId, mRotatingIdLen, rotatingIdString, sizeof(rotatingIdString));
     219           0 :             ChipLogDetail(AppServer, "\trotating id: %s", rotatingIdString);
     220             :         }
     221           0 :         for (uint8_t i = 0; i < mNumTargetAppInfos; i++)
     222             :         {
     223           0 :             ChipLogDetail(AppServer, "\tapp vendor id / product id [%d]: %u/%u", i, mTargetAppInfos[i].vendorId,
     224             :                           mTargetAppInfos[i].productId);
     225             :         }
     226           0 :         if (strlen(mPairingInst) != 0)
     227             :         {
     228           0 :             ChipLogDetail(AppServer, "\tpairing instruction: %s", mPairingInst);
     229             :         }
     230           0 :         if (mPairingHint != 0)
     231             :         {
     232           0 :             ChipLogDetail(AppServer, "\tpairing hint: %d", mPairingHint);
     233             :         }
     234             : 
     235           0 :         if (mNoPasscode)
     236             :         {
     237           0 :             ChipLogDetail(AppServer, "\tno passcode: true");
     238             :         }
     239           0 :         if (mCdUponPasscodeDialog)
     240             :         {
     241           0 :             ChipLogDetail(AppServer, "\tcd upon passcode dialog: true");
     242             :         }
     243           0 :         if (mCommissionerPasscode)
     244             :         {
     245           0 :             ChipLogDetail(AppServer, "\tcommissioner passcode: true");
     246             :         }
     247           0 :         if (mCommissionerPasscodeReady)
     248             :         {
     249           0 :             ChipLogDetail(AppServer, "\tcommissioner passcode ready: true");
     250             :         }
     251           0 :         if (mCancelPasscode)
     252             :         {
     253           0 :             ChipLogDetail(AppServer, "\tcancel passcode: true");
     254             :         }
     255           0 :         ChipLogDetail(AppServer, "---- Identification Declaration End ----");
     256           0 :     }
     257             : 
     258             : private:
     259             :     // TODO: update spec per the latest tags
     260             :     enum IdentificationDeclarationTLVTag
     261             :     {
     262             :         kVendorIdTag = 1,
     263             :         kProductIdTag,
     264             :         kDeviceNameTag,
     265             :         kDeviceTypeTag,
     266             :         kPairingInstTag,
     267             :         kPairingHintTag,
     268             :         kRotatingIdTag,
     269             :         kCdPortTag,
     270             :         kTargetAppListTag,
     271             :         kTargetAppTag,
     272             :         kAppVendorIdTag,
     273             :         kAppProductIdTag,
     274             :         kNoPasscodeTag,
     275             :         kCdUponPasscodeDialogTag,
     276             :         kCommissionerPasscodeTag,
     277             :         kCommissionerPasscodeReadyTag,
     278             :         kCancelPasscodeTag,
     279             : 
     280             :         kMaxNum = UINT8_MAX
     281             :     };
     282             : 
     283             :     char mInstanceName[Dnssd::Commission::kInstanceNameMaxLength + 1] = {};
     284             :     char mDeviceName[Dnssd::kMaxDeviceNameLen + 1]                    = {};
     285             :     uint16_t mCdPort                                                  = 0;
     286             : 
     287             :     uint16_t mVendorId  = 0;
     288             :     uint16_t mProductId = 0;
     289             :     uint8_t mRotatingId[chip::Dnssd::kMaxRotatingIdLen];
     290             :     size_t mRotatingIdLen = 0;
     291             : 
     292             :     constexpr static size_t kMaxTargetAppInfos = 10;
     293             :     uint8_t mNumTargetAppInfos                 = 0; // number of vendor Ids
     294             :     TargetAppInfo mTargetAppInfos[kMaxTargetAppInfos];
     295             : 
     296             :     char mPairingInst[chip::Dnssd::kMaxPairingInstructionLen + 1] = {};
     297             :     uint16_t mPairingHint                                         = 0;
     298             : 
     299             :     bool mNoPasscode                = false;
     300             :     bool mCdUponPasscodeDialog      = false;
     301             :     bool mCommissionerPasscode      = false;
     302             :     bool mCommissionerPasscodeReady = false;
     303             :     bool mCancelPasscode            = false;
     304             : };
     305             : 
     306             : /**
     307             :  * Represents the Commissioner Delaration message
     308             :  * sent by a UDC server to a UDC client.
     309             :  */
     310             : class DLL_EXPORT CommissionerDeclaration
     311             : {
     312             : public:
     313             :     enum class CdError : uint16_t
     314             :     {
     315             :         kNoError                                = 0,
     316             :         kCommissionableDiscoveryFailed          = 1,
     317             :         kPaseConnectionFailed                   = 2,
     318             :         kPaseAuthFailed                         = 3,
     319             :         kDacValidationFailed                    = 4,
     320             :         kAlreadyOnFabric                        = 5,
     321             :         kOperationalDiscoveryFailed             = 6,
     322             :         kCaseConnectionFailed                   = 7,
     323             :         kCaseAuthFailed                         = 8,
     324             :         kConfigurationFailed                    = 9,
     325             :         kBindingConfigurationFailed             = 10,
     326             :         kCommissionerPasscodeNotSupported       = 11,
     327             :         kInvalidIdentificationDeclarationParams = 12,
     328             :         kAppInstallConsentPending               = 13,
     329             :         kAppInstalling                          = 14,
     330             :         kAppInstallFailed                       = 15,
     331             :         kAppInstalledRetryNeeded                = 16,
     332             :         kCommissionerPasscodeDisabled           = 17,
     333             :         kUnexpectedCommissionerPasscodeReady    = 18
     334             :     };
     335             : 
     336             :     constexpr static size_t kUdcTLVDataMaxBytes = 500;
     337             : 
     338           0 :     void SetErrorCode(CdError newValue) { mErrorCode = newValue; };
     339           0 :     CdError GetErrorCode() const { return mErrorCode; };
     340             : 
     341           0 :     void SetNeedsPasscode(bool newValue) { mNeedsPasscode = newValue; };
     342             :     bool GetNeedsPasscode() const { return mNeedsPasscode; };
     343             : 
     344             :     void SetNoAppsFound(bool newValue) { mNoAppsFound = newValue; };
     345             :     bool GetNoAppsFound() const { return mNoAppsFound; };
     346             : 
     347             :     void SetPasscodeDialogDisplayed(bool newValue) { mPasscodeDialogDisplayed = newValue; };
     348             :     bool GetPasscodeDialogDisplayed() const { return mPasscodeDialogDisplayed; };
     349             : 
     350             :     void SetCommissionerPasscode(bool newValue) { mCommissionerPasscode = newValue; };
     351             :     bool GetCommissionerPasscode() const { return mCommissionerPasscode; };
     352             : 
     353             :     void SetQRCodeDisplayed(bool newValue) { mQRCodeDisplayed = newValue; };
     354             :     bool GetQRCodeDisplayed() const { return mQRCodeDisplayed; };
     355             : 
     356             :     /**
     357             :      *  Writes the CommissionerDeclaration message to the given buffer.
     358             :      *
     359             :      * @return Total number of bytes written or 0 if an error occurred.
     360             :      */
     361             :     uint32_t WritePayload(uint8_t * payloadBuffer, size_t payloadBufferSize);
     362             : 
     363             :     /**
     364             :      *  Reads the CommissionerDeclaration message from the given buffer.
     365             :      */
     366             :     CHIP_ERROR ReadPayload(uint8_t * payloadBuffer, size_t payloadBufferSize);
     367             : 
     368           0 :     void DebugLog()
     369             :     {
     370           0 :         ChipLogDetail(AppServer, "---- Commissioner Declaration Start ----");
     371             : 
     372           0 :         if (mErrorCode != CdError::kNoError)
     373             :         {
     374           0 :             ChipLogDetail(AppServer, "\terror code: %d", static_cast<uint16_t>(mErrorCode));
     375             :         }
     376             : 
     377           0 :         if (mNeedsPasscode)
     378             :         {
     379           0 :             ChipLogDetail(AppServer, "\tneeds passcode: true");
     380             :         }
     381           0 :         if (mNoAppsFound)
     382             :         {
     383           0 :             ChipLogDetail(AppServer, "\tno apps found: true");
     384             :         }
     385           0 :         if (mPasscodeDialogDisplayed)
     386             :         {
     387           0 :             ChipLogDetail(AppServer, "\tpasscode dialog displayed: true");
     388             :         }
     389           0 :         if (mCommissionerPasscode)
     390             :         {
     391           0 :             ChipLogDetail(AppServer, "\tcommissioner passcode: true");
     392             :         }
     393           0 :         if (mQRCodeDisplayed)
     394             :         {
     395           0 :             ChipLogDetail(AppServer, "\tQR code displayed: true");
     396             :         }
     397           0 :         ChipLogDetail(AppServer, "---- Commissioner Declaration End ----");
     398           0 :     }
     399             : 
     400             : private:
     401             :     // TODO: update spec per the latest tags
     402             :     enum CommissionerDeclarationTLVTag
     403             :     {
     404             :         kErrorCodeTag = 1,
     405             :         kNeedsPasscodeTag,
     406             :         kNoAppsFoundTag,
     407             :         kPasscodeDialogDisplayedTag,
     408             :         kCommissionerPasscodeTag,
     409             :         kQRCodeDisplayedTag,
     410             : 
     411             :         kMaxNum = UINT8_MAX
     412             :     };
     413             : 
     414             :     CdError mErrorCode            = CdError::kNoError;
     415             :     bool mNeedsPasscode           = false;
     416             :     bool mNoAppsFound             = false;
     417             :     bool mPasscodeDialogDisplayed = false;
     418             :     bool mCommissionerPasscode    = false;
     419             :     bool mQRCodeDisplayed         = false;
     420             : };
     421             : 
     422             : class DLL_EXPORT InstanceNameResolver
     423             : {
     424             : public:
     425             :     /**
     426             :      * @brief
     427             :      *   Called when a UDC message is received specifying the given instanceName
     428             :      * This method indicates that UDC Server needs the Commissionable Node corresponding to
     429             :      * the given instance name to be found. UDC Server will wait for OnCommissionableNodeFound.
     430             :      *
     431             :      * @param instanceName DNS-SD instance name for the client requesting commissioning
     432             :      *
     433             :      */
     434             :     virtual void FindCommissionableNode(char * instanceName) = 0;
     435             : 
     436             :     virtual ~InstanceNameResolver() = default;
     437             : };
     438             : 
     439             : class DLL_EXPORT UserConfirmationProvider
     440             : {
     441             : public:
     442             :     /**
     443             :      * @brief
     444             :      *   Called when an Identification Declaration UDC message has been received
     445             :      * and corresponding nodeData has been found.
     446             :      * It is expected that the implementer will prompt the user to confirm their intention to
     447             :      * commission the given node, and obtain the setup code to allow commissioning to proceed,
     448             :      * and then invoke commissioning on the given Node (using CHIP Device Controller, for example)
     449             :      *
     450             :      *  @param[in]    state           The state for the UDC Client.
     451             :      *
     452             :      */
     453             :     virtual void OnUserDirectedCommissioningRequest(UDCClientState state) = 0;
     454             : 
     455             :     virtual ~UserConfirmationProvider() = default;
     456             : };
     457             : 
     458             : class DLL_EXPORT CommissionerDeclarationHandler
     459             : {
     460             : public:
     461             :     /**
     462             :      * @brief
     463             :      *   Called when a Commissioner Declaration UDC message has been received.
     464             :      * It is expected that the implementer will de-dup messages received from the
     465             :      * same source within a short (1 second) time window.
     466             :      *
     467             :      *  @param[in]    source       The source of the Commissioner Declaration Message.
     468             :      *  @param[in]    cd           The Commissioner Declaration Message.
     469             :      *
     470             :      */
     471             :     virtual void OnCommissionerDeclarationMessage(const chip::Transport::PeerAddress & source, CommissionerDeclaration cd) = 0;
     472             : 
     473             :     virtual ~CommissionerDeclarationHandler() = default;
     474             : };
     475             : 
     476             : /**
     477             :  * TODO:
     478             :  * - add processing of Commissioner Declaration flags
     479             :  */
     480             : class DLL_EXPORT UserDirectedCommissioningClient : public TransportMgrDelegate
     481             : {
     482             : public:
     483             :     /**
     484             :      * Send a User Directed Commissioning message to a CHIP node.
     485             :      *
     486             :      * @param transportMgr  A transport to use for sending the message.
     487             :      * @param idMessage     The Identification Declaration message.
     488             :      * @param peerAddress   Address of destination.
     489             :      *
     490             :      * @return CHIP_ERROR_NO_MEMORY if allocation fails.
     491             :      *         Other CHIP_ERROR codes as returned by the lower layers.
     492             :      *
     493             :      */
     494             : 
     495             :     CHIP_ERROR SendUDCMessage(TransportMgrBase * transportMgr, IdentificationDeclaration idMessage,
     496             :                               chip::Transport::PeerAddress peerAddress);
     497             : 
     498             :     /**
     499             :      * Encode a User Directed Commissioning message.
     500             :      *
     501             :      * @param payload       A PacketBufferHandle with the payload.
     502             :      *
     503             :      * @return CHIP_ERROR_NO_MEMORY if allocation fails.
     504             :      *         Other CHIP_ERROR codes as returned by the lower layers.
     505             :      *
     506             :      */
     507             : 
     508             :     CHIP_ERROR EncodeUDCMessage(const System::PacketBufferHandle & payload);
     509             : 
     510             :     /**
     511             :      * Set the listener to be called when a Commissioner Declaration UDC request is received.
     512             :      *
     513             :      *  @param[in]    commissionerDeclarationHandler    The callback function to handle the message.
     514             :      *
     515             :      */
     516             :     void SetCommissionerDeclarationHandler(CommissionerDeclarationHandler * commissionerDeclarationHandler)
     517             :     {
     518             :         mCommissionerDeclarationHandler = commissionerDeclarationHandler;
     519             :     }
     520             : 
     521             : private:
     522             :     void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf) override;
     523             : 
     524             :     CommissionerDeclarationHandler * mCommissionerDeclarationHandler = nullptr;
     525             : };
     526             : 
     527             : /**
     528             :  * TODO:
     529             :  * - add processing of Identification Declaration flags
     530             :  */
     531             : class DLL_EXPORT UserDirectedCommissioningServer : public TransportMgrDelegate
     532             : {
     533             : public:
     534             :     /**
     535             :      * Set the listener to be called when a UDC request is received
     536             :      * and the Instance Name provided needs to be resolved.
     537             :      *
     538             :      * The resolver should call OnCommissionableNodeFound when the instance is found
     539             :      *
     540             :      *  @param[in]    instanceNameResolver    The callback function to receive UDC request instance name.
     541             :      *
     542             :      */
     543             :     void SetInstanceNameResolver(InstanceNameResolver * instanceNameResolver) { mInstanceNameResolver = instanceNameResolver; }
     544             : 
     545             :     /**
     546             :      * Set the listener to be called when a UDC request is received
     547             :      * and the Instance Name has been resolved.
     548             :      *
     549             :      * The provider should prompt the user to allow commissioning of the node and provide the setup code.
     550             :      *
     551             :      *  @param[in]    userConfirmationProvider    The callback function to obtain user confirmation.
     552             :      *
     553             :      */
     554             :     void SetUserConfirmationProvider(UserConfirmationProvider * userConfirmationProvider)
     555             :     {
     556             :         mUserConfirmationProvider = userConfirmationProvider;
     557             :     }
     558             : 
     559             :     /**
     560             :      * Update the processing state for a UDC Client based upon instance name.
     561             :      *
     562             :      * This can be used by the UX to set the state to one of the following values:
     563             :      * - kUserDeclined
     564             :      * - kObtainingOnboardingPayload
     565             :      * - kCommissioningNode
     566             :      * - kCommissioningFailed
     567             :      *
     568             :      *  @param[in]    instanceName    The instance name for the UDC Client.
     569             :      *  @param[in]    state           The state for the UDC Client.
     570             :      *
     571             :      */
     572             :     void SetUDCClientProcessingState(char * instanceName, UDCClientProcessingState state);
     573             : 
     574             :     /**
     575             :      * Reset the processing states for all UDC Clients
     576             :      *
     577             :      */
     578             :     void ResetUDCClientProcessingStates() { mUdcClients.ResetUDCClientStates(); }
     579             : 
     580             :     /**
     581             :      * Called when a CHIP Node in commissioning mode is found.
     582             :      *
     583             :      * Lookup instanceName from nodeData in the active UDC Client states
     584             :      * and if current state is kDiscoveringNode then change to kPromptingUser and
     585             :      * call UX Prompt callback
     586             :      *
     587             :      *  @param[in]    nodeData        DNS-SD response data.
     588             :      *
     589             :      */
     590             :     void OnCommissionableNodeFound(const Dnssd::DiscoveredNodeData & nodeData);
     591             : 
     592             :     /**
     593             :      * Get the cache of UDC Clients
     594             :      *
     595             :      */
     596           0 :     UDCClients<kMaxUDCClients> & GetUDCClients() { return mUdcClients; }
     597             : 
     598             :     /**
     599             :      * Print the cache of UDC Clients
     600             :      *
     601             :      */
     602             :     void PrintUDCClients();
     603             : 
     604             :     /**
     605             :      * Send a Commissioner Declaration message to the given peer address
     606             :      *
     607             :      * Only one message will be sent.
     608             :      * Clients should follow spec and send up to 5 times with 100ms sleep between each call.
     609             :      */
     610             :     CHIP_ERROR SendCDCMessage(CommissionerDeclaration cdMessage, chip::Transport::PeerAddress peerAddress);
     611             : 
     612             :     /**
     613             :      * Encode a User Directed Commissioning message.
     614             :      *
     615             :      * @param payload       A PacketBufferHandle with the payload.
     616             :      *
     617             :      * @return CHIP_ERROR_NO_MEMORY if allocation fails.
     618             :      *         Other CHIP_ERROR codes as returned by the lower layers.
     619             :      *
     620             :      */
     621             :     CHIP_ERROR EncodeUDCMessage(const System::PacketBufferHandle & payload);
     622             : 
     623             :     /**
     624             :      * Assign the transport manager to use for Commissioner Declaration messages
     625             :      */
     626             :     void SetTransportManager(TransportMgrBase * transportMgr) { mTransportMgr = transportMgr; }
     627             : 
     628             : private:
     629             :     InstanceNameResolver * mInstanceNameResolver         = nullptr;
     630             :     UserConfirmationProvider * mUserConfirmationProvider = nullptr;
     631             : 
     632             :     void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf) override;
     633             : 
     634             :     UDCClients<kMaxUDCClients> mUdcClients; // < Active UDC clients
     635             : 
     636             :     TransportMgrBase * mTransportMgr = nullptr;
     637             : };
     638             : 
     639             : } // namespace UserDirectedCommissioning
     640             : 
     641             : template <>
     642             : struct MessageTypeTraits<UserDirectedCommissioning::MsgType>
     643             : {
     644           1 :     static constexpr const Protocols::Id & ProtocolId() { return UserDirectedCommissioning::Id; }
     645             : 
     646           0 :     static auto GetTypeToNameTable()
     647             :     {
     648             :         static const std::array<MessageTypeNameLookup, 1> typeToNameTable = {
     649             :             {
     650             :                 { UserDirectedCommissioning::MsgType::IdentificationDeclaration, "IdentificationDeclaration" },
     651             :             },
     652             :         };
     653             : 
     654           0 :         return &typeToNameTable;
     655             :     }
     656             : };
     657             : 
     658             : } // namespace Protocols
     659             : } // namespace chip

Generated by: LCOV version 1.14