Skip to content

Commit

Permalink
[Fabric-Sync] Allow RPC ports customization in example apps (#35194)
Browse files Browse the repository at this point in the history
* [Fabric-Sync] Allow RPC ports customization in example apps

* Restyled by clang-format

* Rename SetRpcClientPort to SetRpcRemoteServerPort

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
arkq and restyled-commits authored Aug 29, 2024
1 parent c14a370 commit d256926
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#if defined(PW_RPC_ENABLED)
#include <rpc/RpcClient.h>
#include <rpc/RpcServer.h>
#endif

using namespace chip;
Expand Down Expand Up @@ -116,7 +117,7 @@ void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category,
#if defined(PW_RPC_ENABLED)
void AttemptRpcClientConnect(System::Layer * systemLayer, void * appState)
{
if (InitRpcClient(kFabricBridgeServerPort) == CHIP_NO_ERROR)
if (StartRpcClient() == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Connected to Fabric-Bridge");
}
Expand Down Expand Up @@ -196,6 +197,9 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
}

#if defined(PW_RPC_ENABLED)
SetRpcRemoteServerPort(mFabricBridgeServerPort.Value());
InitRpcServer(mLocalServerPort.Value());
ChipLogProgress(NotSpecified, "PW_RPC initialized.");
DeviceLayer::PlatformMgr().ScheduleWork(ExecuteDeferredConnect, 0);
#endif

Expand Down
16 changes: 15 additions & 1 deletion examples/fabric-admin/commands/interactive/InteractiveCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

#include <string>

constexpr uint16_t kFabricBridgeServerPort = 33002;
constexpr uint16_t kFabricLocalServerPort = 33001;

class Commands;

class InteractiveCommand : public CHIPCommand
Expand Down Expand Up @@ -55,14 +58,25 @@ class InteractiveStartCommand : public InteractiveCommand
InteractiveStartCommand(Commands * commandsHandler, CredentialIssuerCommands * credsIssuerConfig) :
InteractiveCommand("start", commandsHandler, "Start an interactive shell that can then run other commands.",
credsIssuerConfig)
{}
{
#if defined(PW_RPC_ENABLED)
AddArgument("fabric-bridge-server-port", 0, UINT16_MAX, &mFabricBridgeServerPort,
"The fabric-bridge RPC port number to connect to.");
AddArgument("local-server-port", 0, UINT16_MAX, &mLocalServerPort, "The port number for local RPC server.");
#endif
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

private:
char * GetCommand(char * command);
std::string GetHistoryFilePath() const;

#if defined(PW_RPC_ENABLED)
chip::Optional<uint16_t> mFabricBridgeServerPort{ kFabricBridgeServerPort };
chip::Optional<uint16_t> mLocalServerPort{ kFabricLocalServerPort };
#endif
};

void PushCommand(const std::string & command);
9 changes: 0 additions & 9 deletions examples/fabric-admin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,10 @@
#include <string>
#include <vector>

#if defined(PW_RPC_ENABLED)
#include <rpc/RpcServer.h>
#endif

using namespace chip;

void ApplicationInit()
{
#if defined(PW_RPC_ENABLED)
InitRpcServer(kFabricAdminServerPort);
ChipLogProgress(NotSpecified, "PW_RPC initialized.");
#endif

DeviceMgr().Init();
}

Expand Down
8 changes: 6 additions & 2 deletions examples/fabric-admin/rpc/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ void RpcCompletedWithEmptyResponse(const pw_protobuf_Empty & response, pw::Statu

} // namespace

CHIP_ERROR InitRpcClient(uint16_t rpcServerPort)
void SetRpcRemoteServerPort(uint16_t port)
{
rpc::client::SetRpcServerPort(port);
}

CHIP_ERROR StartRpcClient()
{
rpc::client::SetRpcServerPort(rpcServerPort);
return rpc::client::StartPacketProcessing();
}

Expand Down
16 changes: 9 additions & 7 deletions examples/fabric-admin/rpc/RpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@

#include "fabric_bridge_service/fabric_bridge_service.rpc.pb.h"

constexpr uint16_t kFabricBridgeServerPort = 33002;

/**
* @brief Initializes the RPC client with the specified server port.
* @brief Sets the RPC server port to which the RPC client will connect.
*
* This function sets the RPC server port and starts packet processing for the RPC client.
* @param port The port number.
*/
void SetRpcRemoteServerPort(uint16_t port);

/**
* @brief Starts packet processing for the RPC client.
*
* @param rpcServerPort The port number on which the RPC server is running.
* @return CHIP_NO_ERROR on successful initialization, or an appropriate CHIP_ERROR on failure.
* @return CHIP_NO_ERROR on successful start, or an appropriate CHIP_ERROR on failure.
*/
CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
CHIP_ERROR StartRpcClient();

/**
* @brief Adds a synchronized device to the RPC client.
Expand Down
2 changes: 0 additions & 2 deletions examples/fabric-admin/rpc/RpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@

#pragma once

constexpr uint16_t kFabricAdminServerPort = 33001;

void InitRpcServer(uint16_t rpcServerPort);
8 changes: 6 additions & 2 deletions examples/fabric-bridge-app/linux/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ void RpcCompletedWithEmptyResponse(const pw_protobuf_Empty & response, pw::Statu

} // namespace

CHIP_ERROR InitRpcClient(uint16_t rpcServerPort)
void SetRpcRemoteServerPort(uint16_t port)
{
rpc::client::SetRpcServerPort(port);
}

CHIP_ERROR StartRpcClient()
{
rpc::client::SetRpcServerPort(rpcServerPort);
return rpc::client::StartPacketProcessing();
}

Expand Down
12 changes: 8 additions & 4 deletions examples/fabric-bridge-app/linux/include/RpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
#include <controller/CommissioningWindowParams.h>
#include <platform/CHIPDeviceLayer.h>

constexpr uint16_t kFabricAdminServerPort = 33001;
/**
* Sets the RPC server port to which the RPC client will connect.
*
* @param port The port number.
*/
void SetRpcRemoteServerPort(uint16_t port);

/**
* Initializes the RPC client by setting the server port and starting packet processing.
* Starts packet processing for the RPC client.
*
* @param rpcServerPort The port number of the RPC server.
* @return CHIP_ERROR An error code indicating the success or failure of the initialization process.
* - CHIP_NO_ERROR: Initialization was successful.
* - Other error codes indicating specific failure reasons.
*/
CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
CHIP_ERROR StartRpcClient();

/**
* Opens a commissioning window for a specified node using setup PIN (passcode).
Expand Down
2 changes: 0 additions & 2 deletions examples/fabric-bridge-app/linux/include/RpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@

#pragma once

constexpr uint16_t kFabricBridgeServerPort = 33002;

void InitRpcServer(uint16_t rpcServerPort);
55 changes: 49 additions & 6 deletions examples/fabric-bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* limitations under the License.
*/

#include <cstdlib>
#include <sys/ioctl.h>
#include <thread>

#include <AppMain.h>

#include "BridgedAdministratorCommissioning.h"
Expand All @@ -28,15 +32,13 @@
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/clusters/ecosystem-information-server/ecosystem-information-server.h>
#include <lib/support/CHIPArgParser.hpp>

#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
#include "RpcClient.h"
#include "RpcServer.h"
#endif

#include <sys/ioctl.h>
#include <thread>

// This is declared here and not in a header because zap/embr assumes all clusters
// are defined in a static endpoint in the .zap file. From there, the codegen will
// automatically use PluginApplicationCallbacksHeader.jinja to declare and call
Expand All @@ -59,8 +61,48 @@ constexpr uint16_t kPollIntervalMs = 100;
constexpr uint16_t kRetryIntervalS = 3;
#endif

uint16_t gFabricAdminServerPort = 33001;
uint16_t gLocalServerPort = 33002;

BridgedDeviceBasicInformationImpl gBridgedDeviceBasicInformationAttributes;

constexpr uint16_t kOptionFabricAdminServerPortNumber = 0xFF01;
constexpr uint16_t kOptionLocalServerPortNumber = 0xFF02;

ArgParser::OptionDef sProgramCustomOptionDefs[] = {
{ "fabric-admin-server-port", ArgParser::kArgumentRequired, kOptionFabricAdminServerPortNumber },
{ "local-server-port", ArgParser::kArgumentRequired, kOptionLocalServerPortNumber },
{},
};

const char sProgramCustomOptionHelp[] = " --fabric-admin-server-port <port>\n"
" The fabric-admin RPC port number to connect to (default: 33001).\n"
" --local-server-port <port>\n"
" The port number for local RPC server (default: 33002).\n"
"\n";

bool HandleCustomOption(const char * aProgram, ArgParser::OptionSet * aOptions, int aIdentifier, const char * aName,
const char * aValue)
{
switch (aIdentifier)
{
case kOptionFabricAdminServerPortNumber:
gFabricAdminServerPort = atoi(aValue);
break;
case kOptionLocalServerPortNumber:
gLocalServerPort = atoi(aValue);
break;
default:
ArgParser::PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
return false;
}

return true;
}

ArgParser::OptionSet sProgramCustomOptions = { HandleCustomOption, sProgramCustomOptionDefs, "GENERAL OPTIONS",
sProgramCustomOptionHelp };

bool KeyboardHit()
{
int bytesWaiting;
Expand Down Expand Up @@ -105,7 +147,7 @@ void BridgePollingThread()
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
void AttemptRpcClientConnect(System::Layer * systemLayer, void * appState)
{
if (InitRpcClient(kFabricAdminServerPort) == CHIP_NO_ERROR)
if (StartRpcClient() == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Connected to Fabric-Admin");
}
Expand Down Expand Up @@ -258,7 +300,8 @@ void ApplicationInit()
AttributeAccessInterfaceRegistry::Instance().Register(&gBridgedDeviceBasicInformationAttributes);

#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
InitRpcServer(kFabricBridgeServerPort);
SetRpcRemoteServerPort(gFabricAdminServerPort);
InitRpcServer(gLocalServerPort);
AttemptRpcClientConnect(&DeviceLayer::SystemLayer(), nullptr);
#endif

Expand All @@ -285,7 +328,7 @@ void ApplicationShutdown()

int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
if (ChipLinuxAppInit(argc, argv, &sProgramCustomOptions) != 0)
{
return -1;
}
Expand Down

0 comments on commit d256926

Please sign in to comment.