Skip to content

Commit

Permalink
[Dnssd] Refactored DiscoveredNodeData definition and usage (#33025)
Browse files Browse the repository at this point in the history
* Refactored DiscoveredNodeData definition and usage

* Restyled by whitespace

* Restyled by clang-format

* updates based on review feedback

* Restyled by whitespace

* Restyled by clang-format

* fix for build failure

* Updates as per feedback

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
su-shanka and restyled-commits authored Apr 25, 2024
1 parent c4699cf commit f4dbc23
Show file tree
Hide file tree
Showing 54 changed files with 822 additions and 502 deletions.
7 changes: 4 additions & 3 deletions examples/chip-tool/commands/common/DeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ CHIP_ERROR DeviceScanner::Stop()

void DeviceScanner::OnNodeDiscovered(const DiscoveredNodeData & nodeData)
{
auto & commissionData = nodeData.nodeData;
VerifyOrReturn(nodeData.Is<CommissionNodeData>());
auto & commissionData = nodeData.Get<CommissionNodeData>();

auto discriminator = commissionData.longDiscriminator;
auto vendorId = static_cast<VendorId>(commissionData.vendorId);
Expand All @@ -64,7 +65,7 @@ void DeviceScanner::OnNodeDiscovered(const DiscoveredNodeData & nodeData)
ChipLogProgress(chipTool, "OnNodeDiscovered (MDNS): discriminator: %u, vendorId: %u, productId: %u", discriminator, vendorId,
productId);

auto & resolutionData = nodeData.resolutionData;
const CommonResolutionData & resolutionData = commissionData;

auto & instanceData = mDiscoveredResults[commissionData.instanceName];
auto & interfaceData = instanceData[resolutionData.interfaceId.GetPlatformInterface()];
Expand All @@ -76,7 +77,7 @@ void DeviceScanner::OnNodeDiscovered(const DiscoveredNodeData & nodeData)
interfaceData.push_back(result);
}

nodeData.LogDetail();
commissionData.LogDetail();
}

void DeviceScanner::OnBrowseAdd(chip::Dnssd::DnssdService service)
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ CHIP_ERROR LogIssueNOCChain(const char * noc, const char * icac, const char * rc
return gDelegate->LogJSON(valueStr.c_str());
}

CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::DiscoveredNodeData & nodeData)
CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::CommissionNodeData & nodeData)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);

auto & resolutionData = nodeData.resolutionData;
auto & commissionData = nodeData.nodeData;
auto & commissionData = nodeData;
auto & resolutionData = commissionData;

if (!chip::CanCastTo<uint8_t>(resolutionData.numIPs))
{
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/common/RemoteDataModelLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error);
CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value);
CHIP_ERROR LogGetCommissionerRootCertificate(const char * value);
CHIP_ERROR LogIssueNOCChain(const char * noc, const char * icac, const char * rcac, const char * ipk);
CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::DiscoveredNodeData & nodeData);
CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::CommissionNodeData & nodeData);
void SetDelegate(RemoteDataModelLoggerDelegate * delegate);
}; // namespace RemoteDataModelLogger
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

using namespace ::chip;

void DiscoverCommissionablesCommandBase::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
void DiscoverCommissionablesCommandBase::OnDiscoveredDevice(const chip::Dnssd::CommissionNodeData & nodeData)
{
nodeData.LogDetail();
LogErrorOnFailure(RemoteDataModelLogger::LogDiscoveredNodeData(nodeData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DiscoverCommissionablesCommandBase : public CHIPCommand, public chip::Cont
}

/////////// DeviceDiscoveryDelegate Interface /////////
void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override;
void OnDiscoveredDevice(const chip::Dnssd::CommissionNodeData & nodeData) override;

/////////// CHIPCommand Interface /////////
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(30); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void DiscoverCommissionersCommand::Shutdown()
[[maybe_unused]] int commissionerCount = 0;
for (int i = 0; i < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES; i++)
{
const Dnssd::DiscoveredNodeData * commissioner = mCommissionableNodeController.GetDiscoveredCommissioner(i);
const Dnssd::CommissionNodeData * commissioner = mCommissionableNodeController.GetDiscoveredCommissioner(i);
if (commissioner != nullptr)
{
ChipLogProgress(chipTool, "Discovered Commissioner #%d", commissionerCount);
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,12 @@ void PairingCommand::OnICDStayActiveComplete(NodeId deviceId, uint32_t promisedA
ChipLogValueX64(deviceId), promisedActiveDuration);
}

void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::CommissionNodeData & nodeData)
{
// Ignore nodes with closed commissioning window
VerifyOrReturn(nodeData.nodeData.commissioningMode != 0);
VerifyOrReturn(nodeData.commissioningMode != 0);

auto & resolutionData = nodeData.resolutionData;
auto & resolutionData = nodeData;

const uint16_t port = resolutionData.port;
char buf[chip::Inet::IPAddress::kMaxStringLength];
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class PairingCommand : public CHIPCommand,
void OnICDStayActiveComplete(NodeId deviceId, uint32_t promisedActiveDuration) override;

/////////// DeviceDiscoveryDelegate Interface /////////
void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override;
void OnDiscoveredDevice(const chip::Dnssd::CommissionNodeData & nodeData) override;

/////////// DeviceAttestationDelegate /////////
chip::Optional<uint16_t> FailSafeExpiryTimeoutSecs() const override;
Expand Down
5 changes: 2 additions & 3 deletions examples/platform/linux/ControllerShellCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,15 @@ static CHIP_ERROR display(bool printHeader)

for (int i = 0; i < 10; i++)
{
const Dnssd::DiscoveredNodeData * next = GetDeviceCommissioner()->GetDiscoveredDevice(i);
const Dnssd::CommissionNodeData * next = GetDeviceCommissioner()->GetDiscoveredDevice(i);
if (next == nullptr)
{
streamer_printf(sout, " Entry %d null\r\n", i);
}
else
{
streamer_printf(sout, " Entry %d instanceName=%s host=%s longDiscriminator=%d vendorId=%d productId=%d\r\n", i,
next->nodeData.instanceName, next->resolutionData.hostName, next->nodeData.longDiscriminator,
next->nodeData.vendorId, next->nodeData.productId);
next->instanceName, next->hostName, next->longDiscriminator, next->vendorId, next->productId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
}

CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscoveredNodeData,
chip::Dnssd::DiscoveredNodeData & outCppDiscoveredNodeData)
chip::Dnssd::CommissionNodeData & outCppDiscoveredNodeData)
{
ChipLogProgress(AppServer, "convertJDiscoveredNodeDataToCppDiscoveredNodeData called");
VerifyOrReturnError(jDiscoveredNodeData != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
Expand All @@ -316,69 +316,66 @@ CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscovered
jstring jHostName = static_cast<jstring>(env->GetObjectField(jDiscoveredNodeData, getHostNameField));
if (jHostName != nullptr)
{
chip::Platform::CopyString(outCppDiscoveredNodeData.resolutionData.hostName, chip::Dnssd::kHostNameMaxLength + 1,
chip::Platform::CopyString(outCppDiscoveredNodeData.hostName, chip::Dnssd::kHostNameMaxLength + 1,
env->GetStringUTFChars(jHostName, 0));
}

jfieldID getInstanceNameField = env->GetFieldID(jDiscoveredNodeDataClass, "instanceName", "Ljava/lang/String;");
jstring jInstanceName = static_cast<jstring>(env->GetObjectField(jDiscoveredNodeData, getInstanceNameField));
if (jInstanceName != nullptr)
{
chip::Platform::CopyString(outCppDiscoveredNodeData.nodeData.instanceName,
chip::Dnssd::Commission::kInstanceNameMaxLength + 1, env->GetStringUTFChars(jInstanceName, 0));
chip::Platform::CopyString(outCppDiscoveredNodeData.instanceName, chip::Dnssd::Commission::kInstanceNameMaxLength + 1,
env->GetStringUTFChars(jInstanceName, 0));
}

jfieldID jLongDiscriminatorField = env->GetFieldID(jDiscoveredNodeDataClass, "longDiscriminator", "J");
outCppDiscoveredNodeData.nodeData.vendorId =
static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jLongDiscriminatorField));
jfieldID jLongDiscriminatorField = env->GetFieldID(jDiscoveredNodeDataClass, "longDiscriminator", "J");
outCppDiscoveredNodeData.vendorId = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jLongDiscriminatorField));

jfieldID jVendorIdField = env->GetFieldID(jDiscoveredNodeDataClass, "vendorId", "J");
outCppDiscoveredNodeData.nodeData.vendorId = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jVendorIdField));
jfieldID jVendorIdField = env->GetFieldID(jDiscoveredNodeDataClass, "vendorId", "J");
outCppDiscoveredNodeData.vendorId = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jVendorIdField));

jfieldID jProductIdField = env->GetFieldID(jDiscoveredNodeDataClass, "productId", "J");
outCppDiscoveredNodeData.nodeData.productId = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jProductIdField));
jfieldID jProductIdField = env->GetFieldID(jDiscoveredNodeDataClass, "productId", "J");
outCppDiscoveredNodeData.productId = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jProductIdField));

jfieldID jCommissioningModeField = env->GetFieldID(jDiscoveredNodeDataClass, "commissioningMode", "B");
outCppDiscoveredNodeData.nodeData.commissioningMode =
outCppDiscoveredNodeData.commissioningMode =
static_cast<uint8_t>(env->GetByteField(jDiscoveredNodeData, jCommissioningModeField));

jfieldID jDeviceTypeField = env->GetFieldID(jDiscoveredNodeDataClass, "deviceType", "J");
outCppDiscoveredNodeData.nodeData.deviceType = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jDeviceTypeField));
jfieldID jDeviceTypeField = env->GetFieldID(jDiscoveredNodeDataClass, "deviceType", "J");
outCppDiscoveredNodeData.deviceType = static_cast<uint16_t>(env->GetLongField(jDiscoveredNodeData, jDeviceTypeField));

jfieldID getDeviceNameField = env->GetFieldID(jDiscoveredNodeDataClass, "deviceName", "Ljava/lang/String;");
jstring jDeviceName = static_cast<jstring>(env->GetObjectField(jDiscoveredNodeData, getDeviceNameField));
if (jDeviceName != nullptr)
{
chip::Platform::CopyString(outCppDiscoveredNodeData.nodeData.deviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
chip::Platform::CopyString(outCppDiscoveredNodeData.deviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
env->GetStringUTFChars(jDeviceName, 0));
}

// TODO: map rotating ID
jfieldID jRotatingIdLenField = env->GetFieldID(jDiscoveredNodeDataClass, "rotatingIdLen", "I");
outCppDiscoveredNodeData.nodeData.rotatingIdLen =
static_cast<size_t>(env->GetIntField(jDiscoveredNodeData, jRotatingIdLenField));
jfieldID jRotatingIdLenField = env->GetFieldID(jDiscoveredNodeDataClass, "rotatingIdLen", "I");
outCppDiscoveredNodeData.rotatingIdLen = static_cast<size_t>(env->GetIntField(jDiscoveredNodeData, jRotatingIdLenField));

jfieldID jPairingHintField = env->GetFieldID(jDiscoveredNodeDataClass, "pairingHint", "S");
outCppDiscoveredNodeData.nodeData.pairingHint =
static_cast<uint16_t>(env->GetShortField(jDiscoveredNodeData, jPairingHintField));
jfieldID jPairingHintField = env->GetFieldID(jDiscoveredNodeDataClass, "pairingHint", "S");
outCppDiscoveredNodeData.pairingHint = static_cast<uint16_t>(env->GetShortField(jDiscoveredNodeData, jPairingHintField));

jfieldID getPairingInstructionField = env->GetFieldID(jDiscoveredNodeDataClass, "pairingInstruction", "Ljava/lang/String;");
jstring jPairingInstruction = static_cast<jstring>(env->GetObjectField(jDiscoveredNodeData, getPairingInstructionField));
if (jPairingInstruction != nullptr)
{
chip::Platform::CopyString(outCppDiscoveredNodeData.nodeData.pairingInstruction, chip::Dnssd::kMaxPairingInstructionLen + 1,
chip::Platform::CopyString(outCppDiscoveredNodeData.pairingInstruction, chip::Dnssd::kMaxPairingInstructionLen + 1,
env->GetStringUTFChars(jPairingInstruction, 0));
}

jfieldID jPortField = env->GetFieldID(jDiscoveredNodeDataClass, "port", "I");
outCppDiscoveredNodeData.resolutionData.port = static_cast<uint16_t>(env->GetIntField(jDiscoveredNodeData, jPortField));
jfieldID jPortField = env->GetFieldID(jDiscoveredNodeDataClass, "port", "I");
outCppDiscoveredNodeData.port = static_cast<uint16_t>(env->GetIntField(jDiscoveredNodeData, jPortField));

jfieldID jNumIpsField = env->GetFieldID(jDiscoveredNodeDataClass, "numIPs", "I");
outCppDiscoveredNodeData.resolutionData.numIPs = static_cast<size_t>(env->GetIntField(jDiscoveredNodeData, jNumIpsField));
jfieldID jNumIpsField = env->GetFieldID(jDiscoveredNodeDataClass, "numIPs", "I");
outCppDiscoveredNodeData.numIPs = static_cast<size_t>(env->GetIntField(jDiscoveredNodeData, jNumIpsField));

jfieldID jIPAddressesField = env->GetFieldID(jDiscoveredNodeDataClass, "ipAddresses", "Ljava/util/List;");
jobject jIPAddresses = env->GetObjectField(jDiscoveredNodeData, jIPAddressesField);
if (jIPAddresses == nullptr && outCppDiscoveredNodeData.resolutionData.numIPs > 0)
if (jIPAddresses == nullptr && outCppDiscoveredNodeData.numIPs > 0)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
Expand All @@ -400,11 +397,11 @@ CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscovered
chip::Inet::IPAddress addressInet;
chip::JniUtfString addressJniString(env, jIPAddressStr);
VerifyOrReturnError(chip::Inet::IPAddress::FromString(addressJniString.c_str(), addressInet), CHIP_ERROR_INVALID_ARGUMENT);
outCppDiscoveredNodeData.resolutionData.ipAddress[ipAddressCount] = addressInet;
outCppDiscoveredNodeData.ipAddress[ipAddressCount] = addressInet;

if (ipAddressCount == 0)
{
outCppDiscoveredNodeData.resolutionData.interfaceId = chip::Inet::InterfaceId::FromIPAddress(addressInet);
outCppDiscoveredNodeData.interfaceId = chip::Inet::InterfaceId::FromIPAddress(addressInet);
}
ipAddressCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe
CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * targetVideoPlayerInfo, jobject & outVideoPlayer);

CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscoveredNodeData,
chip::Dnssd::DiscoveredNodeData & cppDiscoveredNodeData);
chip::Dnssd::CommissionNodeData & cppDiscoveredNodeData);
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ JNI_METHOD(jboolean, sendCommissioningRequest)(JNIEnv * env, jobject, jobject jD
chip::DeviceLayer::StackLock lock;
ChipLogProgress(AppServer, "JNI_METHOD sendCommissioningRequest called");

chip::Dnssd::DiscoveredNodeData commissioner;
chip::Dnssd::CommissionNodeData commissioner;
CHIP_ERROR err = convertJDiscoveredNodeDataToCppDiscoveredNodeData(jDiscoveredNodeData, commissioner);
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(AppServer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ - (void)getDiscoveredCommissioner:(int)index
block:^{
chip::Optional<TargetVideoPlayerInfo *> associatedConnectableVideoPlayer;
DiscoveredNodeData * commissioner = nil;
const chip::Dnssd::DiscoveredNodeData * cppDiscoveredNodeData
const chip::Dnssd::CommissionNodeData * cppDiscoveredNodeData
= CastingServer::GetInstance()->GetDiscoveredCommissioner(
index, associatedConnectableVideoPlayer);
if (cppDiscoveredNodeData != nullptr) {
Expand Down Expand Up @@ -530,7 +530,7 @@ - (void)sendUserDirectedCommissioningRequest:(DiscoveredNodeData * _Nonnull)comm
block:^{
bool udcRequestStatus;

chip::Dnssd::DiscoveredNodeData cppCommissioner;
chip::Dnssd::CommissionNodeData cppCommissioner;
if ([ConversionUtils convertToCppDiscoveredNodeDataFrom:commissioner
outDiscoveredNodeData:cppCommissioner]
!= CHIP_NO_ERROR) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class CommissionerDiscoveryDelegateImpl : public chip::Controller::DeviceDiscove
}
}

void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
void OnDiscoveredDevice(const chip::Dnssd::CommissionNodeData & nodeData)
{
ChipLogProgress(AppServer, "CommissionerDiscoveryDelegateImpl().OnDiscoveredDevice() called");
__block const chip::Dnssd::DiscoveredNodeData cppNodeData = nodeData;
__block const chip::Dnssd::CommissionNodeData cppNodeData = nodeData;
dispatch_async(mClientQueue, ^{
DiscoveredNodeData * objCDiscoveredNodeData = [ConversionUtils convertToObjCDiscoveredNodeDataFrom:&cppNodeData];
mDiscoveredCommissioners.push_back(objCDiscoveredNodeData); // add to the list of discovered commissioners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
outTargetVideoPlayerInfo:(TargetVideoPlayerInfo &)outTargetVideoPlayerInfo;

+ (CHIP_ERROR)convertToCppDiscoveredNodeDataFrom:(DiscoveredNodeData * _Nonnull)objCDiscoveredNodeData
outDiscoveredNodeData:(chip::Dnssd::DiscoveredNodeData &)outDiscoveredNodeData;
outDiscoveredNodeData:(chip::Dnssd::CommissionNodeData &)outDiscoveredNodeData;

/**
* @brief C++ to Objective C converters
*/
+ (ContentApp * _Nonnull)convertToObjCContentAppFrom:(TargetEndpointInfo * _Nonnull)cppTargetEndpointInfo;

+ (DiscoveredNodeData * _Nonnull)convertToObjCDiscoveredNodeDataFrom:
(const chip::Dnssd::DiscoveredNodeData * _Nonnull)cppDiscoveredNodedata;
(const chip::Dnssd::CommissionNodeData * _Nonnull)cppDiscoveredNodedata;

+ (VideoPlayer * _Nonnull)convertToObjCVideoPlayerFrom:(TargetVideoPlayerInfo * _Nonnull)cppTargetVideoPlayerInfo;

Expand Down
Loading

0 comments on commit f4dbc23

Please sign in to comment.