Skip to content

Commit

Permalink
Use typesafe network featuremaps instead of re-defining constants (#1…
Browse files Browse the repository at this point in the history
…4700)

* Use typesafe network featuremaps instead of re-defining constants

* Remove unneeded `using`
  • Loading branch information
andy31415 authored and pull[bot] committed Jul 10, 2023
1 parent 3be462a commit 1162867
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
40 changes: 23 additions & 17 deletions src/app/clusters/network-commissioning/network-commissioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,41 @@ void Instance::InvokeCommand(HandlerContext & ctxt)
switch (ctxt.mRequestPath.mCommandId)
{
case Commands::ScanNetworks::Id:
VerifyOrReturn((mType & kFeatureMapWireless) != 0);
VerifyOrReturn(mFeatureFlags.Has(NetworkCommissioningFeature::kWiFiNetworkInterface) ||
mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface));
HandleCommand<Commands::ScanNetworks::DecodableType>(
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleScanNetworks(ctx, req); });
return;

case Commands::AddOrUpdateWiFiNetwork::Id:
VerifyOrReturn(mType == kFeatureMapWiFi);
VerifyOrReturn(mFeatureFlags.Has(NetworkCommissioningFeature::kWiFiNetworkInterface));
HandleCommand<Commands::AddOrUpdateWiFiNetwork::DecodableType>(
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleAddOrUpdateWiFiNetwork(ctx, req); });
return;

case Commands::AddOrUpdateThreadNetwork::Id:
VerifyOrReturn(mType == kFeatureMapThread);
VerifyOrReturn(mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface));
HandleCommand<Commands::AddOrUpdateThreadNetwork::DecodableType>(
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleAddOrUpdateThreadNetwork(ctx, req); });
return;

case Commands::RemoveNetwork::Id:
VerifyOrReturn((mType & kFeatureMapWireless) != 0);
VerifyOrReturn(mFeatureFlags.Has(NetworkCommissioningFeature::kWiFiNetworkInterface) ||
mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface));
HandleCommand<Commands::RemoveNetwork::DecodableType>(
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleRemoveNetwork(ctx, req); });
return;

case Commands::ConnectNetwork::Id:
VerifyOrReturn((mType & kFeatureMapWireless) != 0);
VerifyOrReturn(mFeatureFlags.Has(NetworkCommissioningFeature::kWiFiNetworkInterface) ||
mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface));
HandleCommand<Commands::ConnectNetwork::DecodableType>(
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleConnectNetwork(ctx, req); });
return;

case Commands::ReorderNetwork::Id:
VerifyOrReturn((mType & kFeatureMapWireless) != 0);
VerifyOrReturn(mFeatureFlags.Has(NetworkCommissioningFeature::kWiFiNetworkInterface) ||
mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface));
HandleCommand<Commands::ReorderNetwork::DecodableType>(
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleReorderNetwork(ctx, req); });
return;
Expand Down Expand Up @@ -174,14 +178,14 @@ CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValu
});

case Attributes::ScanMaxTimeSeconds::Id:
if (mType & kFeatureMapWireless)
if (mpWirelessDriver != nullptr)
{
return aEncoder.Encode(mpWirelessDriver->GetScanNetworkTimeoutSeconds());
}
return CHIP_NO_ERROR;

case Attributes::ConnectMaxTimeSeconds::Id:
if (mType & kFeatureMapWireless)
if (mpWirelessDriver != nullptr)
{
return aEncoder.Encode(mpWirelessDriver->GetConnectNetworkTimeoutSeconds());
}
Expand All @@ -201,7 +205,7 @@ CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValu
return aEncoder.Encode(Attributes::LastConnectErrorValue::TypeInfo::Type(0));

case Attributes::FeatureMap::Id:
return aEncoder.Encode(mType);
return aEncoder.Encode(mFeatureFlags);

default:
return CHIP_NO_ERROR;
Expand All @@ -223,19 +227,20 @@ CHIP_ERROR Instance::Write(const ConcreteDataAttributePath & aPath, AttributeVal

void Instance::HandleScanNetworks(HandlerContext & ctx, const Commands::ScanNetworks::DecodableType & req)
{
switch (mType)

if (mFeatureFlags.Has(NetworkCommissioningFeature::kWiFiNetworkInterface))
{
case kFeatureMapWiFi:
mAsyncCommandHandle = app::CommandHandler::Handle(&ctx.mCommandHandler);
mpDriver.Get<WiFiDriver *>()->ScanNetworks(req.ssid, this);
return;
case kFeatureMapThread:
}
else if (mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface))
{
mAsyncCommandHandle = app::CommandHandler::Handle(&ctx.mCommandHandler);
mpDriver.Get<ThreadDriver *>()->ScanNetworks(this);
return;
default:
}
else
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::UnsupportedCommand);
return;
}
}

Expand Down Expand Up @@ -414,7 +419,8 @@ void Instance::_OnCommissioningComplete(const DeviceLayer::ChipDeviceEvent * eve

void Instance::OnCommissioningComplete(CHIP_ERROR err)
{
VerifyOrReturn((mType & kFeatureMapWireless) != 0);
VerifyOrReturn(mpWirelessDriver != nullptr);

if (err == CHIP_NO_ERROR)
{
ChipLogDetail(Zcl, "Commissioning complete, notify platform driver to persist network credentails.");
Expand Down
20 changes: 7 additions & 13 deletions src/app/clusters/network-commissioning/network-commissioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,10 @@ class Instance : public CommandHandlerInterface,
DeviceLayer::NetworkCommissioning::ThreadScanResponseIterator * networks) override;

private:
static constexpr uint32_t kFeatureMapWiFi = 0b0000'0001;
static constexpr uint32_t kFeatureMapThread = 0b0000'0010;
static constexpr uint32_t kFeatureMapEthernet = 0b0000'0100;

static constexpr uint32_t kFeatureMapWireless = 0b0000'0011;

static void _OnCommissioningComplete(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
void OnCommissioningComplete(CHIP_ERROR err);

const uint32_t mType;
const BitFlags<NetworkCommissioningFeature> mFeatureFlags;

DeviceLayer::NetworkCommissioning::Internal::WirelessDriver * const mpWirelessDriver;
DeviceLayer::NetworkCommissioning::Internal::BaseDriver * const mpBaseDriver;
Expand All @@ -97,24 +91,24 @@ class Instance : public CommandHandlerInterface,
public:
Instance(EndpointId aEndpointId, DeviceLayer::NetworkCommissioning::WiFiDriver * apDelegate) :
CommandHandlerInterface(Optional<EndpointId>(aEndpointId), Id),
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id), mType(kFeatureMapWiFi), mpWirelessDriver(apDelegate),
mpBaseDriver(apDelegate)
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id),
mFeatureFlags(NetworkCommissioningFeature::kWiFiNetworkInterface), mpWirelessDriver(apDelegate), mpBaseDriver(apDelegate)
{
mpDriver.Set<DeviceLayer::NetworkCommissioning::WiFiDriver *>(apDelegate);
}

Instance(EndpointId aEndpointId, DeviceLayer::NetworkCommissioning::ThreadDriver * apDelegate) :
CommandHandlerInterface(Optional<EndpointId>(aEndpointId), Id),
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id), mType(kFeatureMapThread), mpWirelessDriver(apDelegate),
mpBaseDriver(apDelegate)
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id),
mFeatureFlags(NetworkCommissioningFeature::kThreadNetworkInterface), mpWirelessDriver(apDelegate), mpBaseDriver(apDelegate)
{
mpDriver.Set<DeviceLayer::NetworkCommissioning::ThreadDriver *>(apDelegate);
}

Instance(EndpointId aEndpointId, DeviceLayer::NetworkCommissioning::EthernetDriver * apDelegate) :
CommandHandlerInterface(Optional<EndpointId>(aEndpointId), Id),
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id), mType(kFeatureMapEthernet), mpWirelessDriver(nullptr),
mpBaseDriver(apDelegate)
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), Id),
mFeatureFlags(NetworkCommissioningFeature::kEthernetNetworkInterface), mpWirelessDriver(nullptr), mpBaseDriver(apDelegate)
{}

virtual ~Instance() = default;
Expand Down

0 comments on commit 1162867

Please sign in to comment.