Skip to content

Commit

Permalink
Update UDC messages for 1.3 (#30585)
Browse files Browse the repository at this point in the history
* Draft: update UDC messages for 1.3

* fix build, add missing TXT record values and error messages

* fix build, add callbacks for processing new messages/fields

* fix build

* fix build

* fix build

* fix build

* fix build

* cleanup

* fix build
  • Loading branch information
chrisdecenzo authored and pull[bot] committed Dec 22, 2023
1 parent 04aeb51 commit 2248550
Show file tree
Hide file tree
Showing 9 changed files with 1,169 additions and 26 deletions.
84 changes: 79 additions & 5 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#include <platform/LockTracker.h>
#include <protocols/secure_channel/CASEServer.h>
#include <protocols/secure_channel/MessageCounterManager.h>
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
#include <setup_payload/AdditionalDataPayloadGenerator.h>
#endif
#include <setup_payload/SetupPayload.h>
#include <sys/param.h>
#include <system/SystemPacketBuffer.h>
Expand Down Expand Up @@ -382,6 +385,23 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
}
}

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT // support UDC port for commissioner declaration msgs
mUdcTransportMgr = chip::Platform::New<UdcTransportMgr>();
ReturnErrorOnFailure(mUdcTransportMgr->Init(Transport::UdpListenParameters(DeviceLayer::UDPEndPointManager())
.SetAddressType(Inet::IPAddressType::kIPv6)
.SetListenPort(static_cast<uint16_t>(mCdcListenPort))
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UdpListenParameters(DeviceLayer::UDPEndPointManager())
.SetAddressType(Inet::IPAddressType::kIPv4)
.SetListenPort(static_cast<uint16_t>(mCdcListenPort))
#endif // INET_CONFIG_ENABLE_IPV4
));

gUDCClient = chip::Platform::New<Protocols::UserDirectedCommissioning::UserDirectedCommissioningClient>();
mUdcTransportMgr->SetSessionManager(gUDCClient);
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast<intptr_t>(this));
PlatformMgr().HandleServerStarted();

Expand Down Expand Up @@ -510,6 +530,20 @@ void Server::Shutdown()
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
app::DnssdServer::Instance().SetCommissioningModeProvider(nullptr);
chip::Dnssd::ServiceAdvertiser::Instance().Shutdown();

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
if (mUdcTransportMgr != nullptr)
{
chip::Platform::Delete(mUdcTransportMgr);
mUdcTransportMgr = nullptr;
}
if (gUDCClient != nullptr)
{
chip::Platform::Delete(gUDCClient);
gUDCClient = nullptr;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

chip::Dnssd::Resolver::Instance().Shutdown();
chip::app::InteractionModelEngine::GetInstance()->Shutdown();
mCommissioningWindowManager.Shutdown();
Expand Down Expand Up @@ -546,14 +580,54 @@ CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAdd
}
ChipLogDetail(AppServer, "instanceName=%s", nameBuffer);

chip::System::PacketBufferHandle payloadBuf = chip::MessagePacketBuffer::NewWithData(nameBuffer, strlen(nameBuffer));
if (payloadBuf.IsNull())
Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
id.SetInstanceName(nameBuffer);

uint16_t vendorId = 0;
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetVendorId(vendorId) != CHIP_NO_ERROR)
{
ChipLogDetail(Discovery, "Vendor ID not known");
}
else
{
id.SetVendorId(vendorId);
}

uint16_t productId = 0;
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(productId) != CHIP_NO_ERROR)
{
ChipLogDetail(Discovery, "Product ID not known");
}
else
{
id.SetProductId(productId);
}

char deviceName[chip::Dnssd::kKeyDeviceNameMaxLength + 1] = {};
if (!chip::DeviceLayer::ConfigurationMgr().IsCommissionableDeviceNameEnabled() ||
chip::DeviceLayer::ConfigurationMgr().GetCommissionableDeviceName(deviceName, sizeof(deviceName)) != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Unable to allocate packet buffer\n");
return CHIP_ERROR_NO_MEMORY;
ChipLogDetail(Discovery, "Device Name not known");
}
else
{
id.SetDeviceName(deviceName);
}

#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
char rotatingDeviceIdHexBuffer[RotatingDeviceId::kHexMaxLength];
ReturnErrorOnFailure(
app::DnssdServer::Instance().GenerateRotatingDeviceId(rotatingDeviceIdHexBuffer, ArraySize(rotatingDeviceIdHexBuffer)));

uint8_t * rotatingId = reinterpret_cast<uint8_t *>(rotatingDeviceIdHexBuffer);
size_t rotatingIdLen = strlen(rotatingDeviceIdHexBuffer);
id.SetRotatingId(rotatingId, rotatingIdLen);
#endif

id.SetCdPort(mCdcListenPort);

err = gUDCClient->SendUDCMessage(&mTransports, id, commissioner);

err = gUDCClient.SendUDCMessage(&mTransports, std::move(payloadBuf), commissioner);
if (err == CHIP_NO_ERROR)
{
ChipLogDetail(AppServer, "Send UDC request success");
Expand Down
16 changes: 15 additions & 1 deletion src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <lib/core/CHIPConfig.h>
#include <lib/support/SafeInt.h>
#include <messaging/ExchangeMgr.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/KeyValueStoreManager.h>
#include <platform/KvsPersistentStorageDelegate.h>
#include <protocols/secure_channel/CASEServer.h>
Expand Down Expand Up @@ -91,6 +92,15 @@ using ServerTransportMgr = chip::TransportMgr<chip::Transport::UDP
#endif
>;

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
using UdcTransportMgr = TransportMgr<Transport::UDP /* IPv6 */
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UDP /* IPv4 */
#endif
>;
#endif

struct ServerInitParams
{
ServerInitParams() = default;
Expand Down Expand Up @@ -592,7 +602,11 @@ class Server
FabricTable mFabrics;
secure_channel::MessageCounterManager mMessageCounterManager;
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
chip::Protocols::UserDirectedCommissioning::UserDirectedCommissioningClient gUDCClient;
chip::Protocols::UserDirectedCommissioning::UserDirectedCommissioningClient * gUDCClient = nullptr;
// mUdcTransportMgr is for insecure communication (ex. user directed commissioning)
// specifically, the commissioner declaration message (sent by commissioner to commissionee)
UdcTransportMgr * mUdcTransportMgr = nullptr;
uint16_t mCdcListenPort = CHIP_UDC_COMMISSIONEE_PORT;
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CommissioningWindowManager mCommissioningWindowManager;

Expand Down
1 change: 1 addition & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ CHIP_ERROR DeviceCommissioner::Init(CommissionerInitParams params)

mUdcServer = chip::Platform::New<UserDirectedCommissioningServer>();
mUdcTransportMgr->SetSessionManager(mUdcServer);
mUdcServer->SetTransportManager(mUdcTransportMgr);

mUdcServer->SetInstanceNameResolver(this);
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
Expand Down
13 changes: 12 additions & 1 deletion src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,24 @@
* @def CHIP_UDC_PORT
*
* @brief
* chip TCP/UDP port for unsecured user-directed-commissioning traffic.
* chip TCP/UDP port on commissioner for unsecured user-directed-commissioning traffic.
*
*/
#ifndef CHIP_UDC_PORT
#define CHIP_UDC_PORT CHIP_PORT + 10
#endif // CHIP_UDC_PORT

/**
* @def CHIP_UDC_COMMISSIONEE_PORT
*
* @brief
* chip TCP/UDP port on commisionee for unsecured user-directed-commissioning traffic.
*
*/
#ifndef CHIP_UDC_COMMISSIONEE_PORT
#define CHIP_UDC_COMMISSIONEE_PORT CHIP_UDC_PORT + 10
#endif // CHIP_UDC_COMMISSIONEE_PORT

/**
* @def CHIP_CONFIG_SECURITY_TEST_MODE
*
Expand Down
84 changes: 78 additions & 6 deletions src/protocols/user_directed_commissioning/UDCClientState.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class UDCClientState
uint16_t GetProductId() const { return mProductId; }
void SetProductId(uint16_t value) { mProductId = value; }

uint16_t GetCdPort() const { return mCdPort; }
void SetCdPort(uint16_t port) { mCdPort = port; }

const uint8_t * GetRotatingId() const { return mRotatingId; }
size_t GetRotatingIdLength() const { return mRotatingIdLen; }
void SetRotatingId(const uint8_t * rotatingId, size_t rotatingIdLen)
Expand All @@ -90,6 +93,33 @@ class UDCClientState
memcpy(mRotatingId, rotatingId, mRotatingIdLen);
}

const char * GetPairingInst() const { return mPairingInst; }
void SetPairingInst(const char * pairingInst) { Platform::CopyString(mPairingInst, pairingInst); }

uint16_t GetPairingHint() const { return mPairingHint; }
void SetPairingHint(uint16_t pairingHint) { mPairingHint = pairingHint; }

bool GetAppVendorId(size_t index, uint16_t & vid) const
{
if (index < mNumAppVendorIds)
{
vid = mAppVendorIds[index];
return true;
}
return false;
}
size_t GetNumAppVendorIds() const { return mNumAppVendorIds; }

void AddAppVendorId(uint16_t vid)
{
if (mNumAppVendorIds >= sizeof(mAppVendorIds))
{
// already at max
return;
}
mAppVendorIds[mNumAppVendorIds++] = vid;
}

UDCClientProcessingState GetUDCClientProcessingState() const { return mUDCClientProcessingState; }
void SetUDCClientProcessingState(UDCClientProcessingState state) { mUDCClientProcessingState = state; }

Expand All @@ -102,25 +132,67 @@ class UDCClientState
return (mUDCClientProcessingState != UDCClientProcessingState::kNotInitialized && mExpirationTime > currentTime);
}

void SetNoPasscode(bool newValue) { mNoPasscode = newValue; };
bool GetNoPasscode() const { return mNoPasscode; };

void SetCdUponPasscodeDialog(bool newValue) { mCdUponPasscodeDialog = newValue; };
bool GetCdUponPasscodeDialog() const { return mCdUponPasscodeDialog; };

void SetCommissionerPasscode(bool newValue) { mCommissionerPasscode = newValue; };
bool GetCommissionerPasscode() const { return mCommissionerPasscode; };

void SetCommissionerPasscodeReady(bool newValue) { mCommissionerPasscodeReady = newValue; };
bool GetCommissionerPasscodeReady() const { return mCommissionerPasscodeReady; };

void SetCancelPasscode(bool newValue) { mCancelPasscode = newValue; };
bool GetCancelPasscode() const { return mCancelPasscode; };

/**
* Reset the connection state to a completely uninitialized status.
*/
void Reset()
{
mPeerAddress = PeerAddress::Uninitialized();
mExpirationTime = System::Clock::kZero;
mUDCClientProcessingState = UDCClientProcessingState::kNotInitialized;
mPeerAddress = PeerAddress::Uninitialized();
mLongDiscriminator = 0;
mVendorId = 0;
mProductId = 0;
mRotatingIdLen = 0;
mCdPort = 0;
mDeviceName[0] = '\0';
mPairingInst[0] = '\0';
mPairingHint = 0;
mNoPasscode = false;
mCdUponPasscodeDialog = false;
mCommissionerPasscode = false;
mCommissionerPasscodeReady = false;
mCancelPasscode = false;
mExpirationTime = System::Clock::kZero;
mUDCClientProcessingState = UDCClientProcessingState::kNotInitialized;
}

private:
PeerAddress mPeerAddress;
char mInstanceName[Dnssd::Commission::kInstanceNameMaxLength + 1];
char mDeviceName[Dnssd::kMaxDeviceNameLen + 1];
uint16_t mLongDiscriminator = 0;
uint16_t mVendorId;
uint16_t mProductId;
uint16_t mVendorId = 0;
uint16_t mProductId = 0;
uint16_t mCdPort = 0;
uint8_t mRotatingId[chip::Dnssd::kMaxRotatingIdLen];
size_t mRotatingIdLen = 0;
size_t mRotatingIdLen = 0;
char mPairingInst[chip::Dnssd::kMaxPairingInstructionLen + 1] = {};
uint16_t mPairingHint = 0;

constexpr static size_t kMaxAppVendorIds = 10;
size_t mNumAppVendorIds = 0; // number of vendor Ids
uint16_t mAppVendorIds[kMaxAppVendorIds];

bool mNoPasscode = false;
bool mCdUponPasscodeDialog = false;
bool mCommissionerPasscode = false;
bool mCommissionerPasscodeReady = false;
bool mCancelPasscode = false;

UDCClientProcessingState mUDCClientProcessingState;
System::Clock::Timestamp mExpirationTime = System::Clock::kZero;
};
Expand Down
Loading

0 comments on commit 2248550

Please sign in to comment.