Skip to content

Commit

Permalink
Add --interface-id option to examples/platform/Linux (#15411)
Browse files Browse the repository at this point in the history
* Add --interface-id option to examples/platform

* Use interface-id into the test suite in order for the accessory to broadcast onto the local machine on macos
  • Loading branch information
vivien-apple authored and pull[bot] committed Nov 3, 2023
1 parent ef8a9d5 commit 1245027
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 23 deletions.
4 changes: 3 additions & 1 deletion examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,10 @@ void ChipLinuxAppMainLoop()
unsecurePort = LinuxDeviceOptions::GetInstance().unsecuredCommissionerPort;
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

Inet::InterfaceId interfaceId = LinuxDeviceOptions::GetInstance().interfaceId;

// Init ZCL Data Model and CHIP App Server
Server::GetInstance().Init(nullptr, securePort, unsecurePort);
Server::GetInstance().Init(nullptr, securePort, unsecurePort, interfaceId);

// Now that the server has started and we are done with our startup logging,
// log our discovery/onboarding information again so it's not lost in the
Expand Down
12 changes: 11 additions & 1 deletion examples/platform/linux/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ enum
kDeviceOption_UnsecuredCommissionerPort = 0x100c,
kDeviceOption_Command = 0x100d,
kDeviceOption_PICS = 0x100e,
kDeviceOption_KVS = 0x100f
kDeviceOption_KVS = 0x100f,
kDeviceOption_InterfaceId = 0x1010
};

constexpr unsigned kAppUsageLength = 64;
Expand Down Expand Up @@ -75,6 +76,7 @@ OptionDef sDeviceOptionDefs[] = {
{ "command", kArgumentRequired, kDeviceOption_Command },
{ "PICS", kArgumentRequired, kDeviceOption_PICS },
{ "KVS", kArgumentRequired, kDeviceOption_KVS },
{ "interface-id", kArgumentRequired, kDeviceOption_InterfaceId },
{}
};

Expand Down Expand Up @@ -133,6 +135,9 @@ const char * sDeviceOptionHelp =
"\n"
" --KVS <filepath>\n"
" A file to store Key Value Store items.\n"
"\n"
" --interface-id <interface>\n"
" A interface id to advertise on.\n"
"\n";

bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
Expand Down Expand Up @@ -220,6 +225,11 @@ bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier,
LinuxDeviceOptions::GetInstance().KVS = aValue;
break;

case kDeviceOption_InterfaceId:
LinuxDeviceOptions::GetInstance().interfaceId =
Inet::InterfaceId(static_cast<chip::Inet::InterfaceId::PlatformType>(atoi(aValue)));
break;

default:
PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
retval = false;
Expand Down
20 changes: 11 additions & 9 deletions examples/platform/linux/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@

#include <cstdint>

#include <inet/InetInterface.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPArgParser.hpp>
#include <setup_payload/SetupPayload.h>

struct LinuxDeviceOptions
{
chip::SetupPayload payload;
uint32_t mBleDevice = 0;
bool mWiFi = false;
bool mThread = false;
uint32_t securedDevicePort = CHIP_PORT;
uint32_t securedCommissionerPort = CHIP_PORT + 2;
uint32_t unsecuredCommissionerPort = CHIP_UDC_PORT;
const char * command = nullptr;
const char * PICS = nullptr;
const char * KVS = nullptr;
uint32_t mBleDevice = 0;
bool mWiFi = false;
bool mThread = false;
uint32_t securedDevicePort = CHIP_PORT;
uint32_t securedCommissionerPort = CHIP_PORT + 2;
uint32_t unsecuredCommissionerPort = CHIP_UDC_PORT;
const char * command = nullptr;
const char * PICS = nullptr;
const char * KVS = nullptr;
chip::Inet::InterfaceId interfaceId = chip::Inet::InterfaceId::Null();

static LinuxDeviceOptions & GetInstance();
};
Expand Down
1 change: 1 addition & 0 deletions scripts/tests/chiptest/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def __startServer(self, runner, command, discriminator):
logging.debug(
'Executing application under test with discriminator %s.' % discriminator)
app_cmd = command + ['--discriminator', str(discriminator)]
app_cmd = app_cmd + ['--interface-id', str(-1)]
return runner.RunSubprocess(app_cmd, name='APP ', wait=False)

def __waitFor(self, waitForString, server_process, outpipe):
Expand Down
2 changes: 2 additions & 0 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ CHIP_ERROR DnssdServer::AdvertiseOperational()
.SetPeerId(fabricInfo.GetPeerId())
.SetMac(mac)
.SetPort(GetSecuredPort())
.SetInterfaceId(GetInterfaceId())
.SetMRPConfig(GetLocalMRPConfig())
.SetTcpSupported(Optional<bool>(INET_CONFIG_ENABLE_TCP_ENDPOINT))
.EnableIpV4(true);
Expand All @@ -287,6 +288,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
{
auto advertiseParameters = chip::Dnssd::CommissionAdvertisingParameters()
.SetPort(commissionableNode ? GetSecuredPort() : GetUnsecuredPort())
.SetInterfaceId(GetInterfaceId())
.EnableIpV4(true);
advertiseParameters.SetCommissionAdvertiseMode(commissionableNode ? chip::Dnssd::CommssionAdvertiseMode::kCommissionableNode
: chip::Dnssd::CommssionAdvertiseMode::kCommissioner);
Expand Down
11 changes: 9 additions & 2 deletions src/app/server/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class DLL_EXPORT DnssdServer
/// Gets the unsecure Matter port
uint16_t GetUnsecuredPort() { return mUnsecuredPort; }

/// Sets the interface id used for advertising
void SetInterfaceId(Inet::InterfaceId interfaceId) { mInterfaceId = interfaceId; }

/// Gets the interface id used for advertising
Inet::InterfaceId GetInterfaceId() { return mInterfaceId; }

/// Sets the factory-new state commissionable node discovery timeout
void SetDiscoveryTimeoutSecs(int16_t secs) { mDiscoveryTimeoutSecs = secs; }

Expand Down Expand Up @@ -118,8 +124,9 @@ class DLL_EXPORT DnssdServer
// Helper for StartServer.
void StartServer(Optional<Dnssd::CommissioningMode> mode);

uint16_t mSecuredPort = CHIP_PORT;
uint16_t mUnsecuredPort = CHIP_UDC_PORT;
uint16_t mSecuredPort = CHIP_PORT;
uint16_t mUnsecuredPort = CHIP_UDC_PORT;
Inet::InterfaceId mInterfaceId = Inet::InterfaceId::Null();

/// schedule next discovery expiration
CHIP_ERROR ScheduleDiscoveryExpiration();
Expand Down
5 changes: 4 additions & 1 deletion src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ Server::Server() :
mAttributePersister(mDeviceStorage), mAccessControl(Access::Examples::GetAccessControlDelegate(&mDeviceStorage))
{}

CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort)
CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort,
Inet::InterfaceId interfaceId)
{
mSecuredServicePort = secureServicePort;
mUnsecuredServicePort = unsecureServicePort;
mInterfaceId = interfaceId;

CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -230,6 +232,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint
#if CHIP_DEVICE_CONFIG_ENABLE_DNSSD
app::DnssdServer::Instance().SetSecuredPort(mSecuredServicePort);
app::DnssdServer::Instance().SetUnsecuredPort(mUnsecuredServicePort);
app::DnssdServer::Instance().SetInterfaceId(mInterfaceId);
#endif // CHIP_DEVICE_CONFIG_ENABLE_DNSSD

// TODO @bzbarsky-apple @cecille Move to examples
Expand Down
3 changes: 2 additions & 1 deletion src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Server
{
public:
CHIP_ERROR Init(AppDelegate * delegate = nullptr, uint16_t secureServicePort = CHIP_PORT,
uint16_t unsecureServicePort = CHIP_UDC_PORT);
uint16_t unsecureServicePort = CHIP_UDC_PORT, Inet::InterfaceId interfaceId = Inet::InterfaceId::Null());

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CHIP_ERROR SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress commissioner);
Expand Down Expand Up @@ -206,6 +206,7 @@ class Server
// TODO @ceille: Maybe use OperationalServicePort and CommissionableServicePort
uint16_t mSecuredServicePort;
uint16_t mUnsecuredServicePort;
Inet::InterfaceId mInterfaceId;
};

} // namespace chip
9 changes: 9 additions & 0 deletions src/lib/dnssd/Advertiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class BaseAdvertisingParams
}
uint64_t GetPort() const { return mPort; }

Derived & SetInterfaceId(Inet::InterfaceId interfaceId)
{
mInterfaceId = interfaceId;
return *reinterpret_cast<Derived *>(this);
}

Inet::InterfaceId GetInterfaceId() const { return mInterfaceId; }

Derived & EnableIpV4(bool enable)
{
mEnableIPv4 = enable;
Expand Down Expand Up @@ -95,6 +103,7 @@ class BaseAdvertisingParams

private:
uint16_t mPort = CHIP_PORT;
Inet::InterfaceId mInterfaceId = Inet::InterfaceId::Null();
bool mEnableIPv4 = true;
uint8_t mMacStorage[kMaxMacSize] = {};
size_t mMacLength = 0;
Expand Down
13 changes: 7 additions & 6 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,21 +447,22 @@ CHIP_ERROR DiscoveryImplPlatform::PublishService(const char * serviceType, TextE
const char ** subTypes, size_t subTypeSize,
const OperationalAdvertisingParameters & params)
{
return PublishService(serviceType, textEntries, textEntrySize, subTypes, subTypeSize, params.GetPort(), params.GetMac(),
DnssdServiceProtocol::kDnssdProtocolTcp, params.GetPeerId());
return PublishService(serviceType, textEntries, textEntrySize, subTypes, subTypeSize, params.GetPort(), params.GetInterfaceId(),
params.GetMac(), DnssdServiceProtocol::kDnssdProtocolTcp, params.GetPeerId());
}

CHIP_ERROR DiscoveryImplPlatform::PublishService(const char * serviceType, TextEntry * textEntries, size_t textEntrySize,
const char ** subTypes, size_t subTypeSize,
const CommissionAdvertisingParameters & params)
{
return PublishService(serviceType, textEntries, textEntrySize, subTypes, subTypeSize, params.GetPort(), params.GetMac(),
DnssdServiceProtocol::kDnssdProtocolUdp, PeerId());
return PublishService(serviceType, textEntries, textEntrySize, subTypes, subTypeSize, params.GetPort(), params.GetInterfaceId(),
params.GetMac(), DnssdServiceProtocol::kDnssdProtocolUdp, PeerId());
}

CHIP_ERROR DiscoveryImplPlatform::PublishService(const char * serviceType, TextEntry * textEntries, size_t textEntrySize,
const char ** subTypes, size_t subTypeSize, uint16_t port,
const chip::ByteSpan & mac, DnssdServiceProtocol protocol, PeerId peerId)
Inet::InterfaceId interfaceId, const chip::ByteSpan & mac,
DnssdServiceProtocol protocol, PeerId peerId)
{
ReturnErrorCodeIf(mDnssdInitialized == false, CHIP_ERROR_INCORRECT_STATE);

Expand All @@ -472,7 +473,7 @@ CHIP_ERROR DiscoveryImplPlatform::PublishService(const char * serviceType, TextE
: GetCommissionableInstanceName(service.mName, sizeof(service.mName)));
strncpy(service.mType, serviceType, sizeof(service.mType));
service.mAddressType = Inet::IPAddressType::kAny;
service.mInterface = Inet::InterfaceId::Null();
service.mInterface = interfaceId;
service.mProtocol = protocol;
service.mPort = port;
service.mTextEntries = textEntries;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dnssd/Discovery_ImplPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
CHIP_ERROR PublishService(const char * serviceType, TextEntry * textEntries, size_t textEntrySize, const char ** subTypes,
size_t subTypeSize, const CommissionAdvertisingParameters & params);
CHIP_ERROR PublishService(const char * serviceType, TextEntry * textEntries, size_t textEntrySize, const char ** subTypes,
size_t subTypeSize, uint16_t port, const chip::ByteSpan & mac, DnssdServiceProtocol procotol,
PeerId peerId);
size_t subTypeSize, uint16_t port, Inet::InterfaceId interfaceId, const chip::ByteSpan & mac,
DnssdServiceProtocol procotol, PeerId peerId);

OperationalAdvertisingParameters mOperationalNodeAdvertisingParams;
CommissionAdvertisingParameters mCommissionableNodeAdvertisingParams;
Expand Down

0 comments on commit 1245027

Please sign in to comment.