Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed May 31, 2024
1 parent 9631848 commit 71fb5f3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 42 deletions.
41 changes: 36 additions & 5 deletions examples/fabric-admin/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@
#include <string>
#include <vector>

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

using namespace chip;

namespace {

constexpr char kInteractiveModePrompt[] = ">>> ";
constexpr char kInteractiveModeHistoryFileName[] = "chip_tool_history";
constexpr char kInteractiveModeStopCommand[] = "quit()";

namespace {
constexpr uint16_t kRetryIntervalS = 5;

// File pointer for the log file
FILE * sLogFile = nullptr;
Expand Down Expand Up @@ -67,7 +74,7 @@ void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category,
return;
}

uint64_t timeMs = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
uint64_t timeMs = System::SystemClock().GetMonotonicMilliseconds64().count();
uint64_t seconds = timeMs / 1000;
uint64_t milliseconds = timeMs % 1000;

Expand All @@ -82,6 +89,26 @@ void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category,
funlockfile(sLogFile);
}

#if defined(PW_RPC_ENABLED)
void AttemptRpcClientConnect(System::Layer * systemLayer, void * appState)
{
if (InitRpcClient(kFabricBridgeServerPort) == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Connected to Fabric-Bridge");
}
else
{
ChipLogError(NotSpecified, "Failed to connect to Fabric-Bridge, retry in %d seconds....", kRetryIntervalS);
systemLayer->StartTimer(System::Clock::Seconds16(kRetryIntervalS), AttemptRpcClientConnect, nullptr);
}
}

void ExecuteDeferredConnect(intptr_t ignored)
{
AttemptRpcClientConnect(&DeviceLayer::SystemLayer(), nullptr);
}
#endif

} // namespace

char * InteractiveStartCommand::GetCommand(char * command)
Expand Down Expand Up @@ -134,9 +161,13 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
OpenLogFile(mLogFilePath.Value());

// Redirect logs to the custom logging callback
chip::Logging::SetLogRedirectCallback(LoggingCallback);
Logging::SetLogRedirectCallback(LoggingCallback);
}

#if defined(PW_RPC_ENABLED)
DeviceLayer::PlatformMgr().ScheduleWork(ExecuteDeferredConnect, 0);
#endif

char * command = nullptr;
int status;
while (true)
Expand Down Expand Up @@ -167,7 +198,7 @@ bool InteractiveCommand::ParseCommand(char * command, int * status)
// If scheduling the cleanup fails, there is not much we can do.
// But if something went wrong while the application is leaving it could be because things have
// not been cleaned up properly, so it is still useful to log the failure.
LogErrorOnFailure(chip::DeviceLayer::PlatformMgr().ScheduleWork(ExecuteDeferredCleanups, 0));
LogErrorOnFailure(DeviceLayer::PlatformMgr().ScheduleWork(ExecuteDeferredCleanups, 0));
return false;
}

Expand Down
16 changes: 1 addition & 15 deletions examples/fabric-admin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,19 @@

#include <iostream>
#include <string>
#include <thread>
#include <vector>

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

#define RETRY_INTERVAL_S (3)
using namespace chip;

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

while (true)
{
if (InitRpcClient(kFabricBridgeServerPort) == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Connected to Fabric-Bridge");
break;
}

ChipLogError(NotSpecified, "Failed to connect to Fabric-Bridge, retry in %d seconds....", RETRY_INTERVAL_S);
std::this_thread::sleep_for(std::chrono::seconds(RETRY_INTERVAL_S));
}
#endif
}

Expand Down
3 changes: 0 additions & 3 deletions examples/fabric-admin/rpc/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,9 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort)
{
if (rpcSocketStream.Connect(rpcServerAddress, rpcServerPort) != PW_STATUS_OK)
{
ChipLogError(NotSpecified, "Failed to connect the Fabric-Admin");
return CHIP_ERROR_NOT_CONNECTED;
}

ChipLogProgress(NotSpecified, "Connectted to the Fabric-Admin\n");

// Start a thread to process incoming packets
std::thread packet_processor(ProcessPackets);
packet_processor.detach();
Expand Down
2 changes: 1 addition & 1 deletion examples/fabric-admin/rpc/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FabricAdmin final : public chip::rpc::FabricAdmin
pw::Status OpenCommissioningWindow(const chip_rpc_DeviceInfo & request, chip_rpc_OperationStatus & response) override
{
chip::NodeId nodeId = request.node_id;
printf("Received OpenCommissioningWindow request: 0x%llx\n", nodeId);
printf("Received OpenCommissioningWindow request: 0x%lx\n", nodeId);
response.success = false;

return pw::OkStatus();
Expand Down
3 changes: 0 additions & 3 deletions examples/fabric-bridge-app/linux/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,9 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort)
{
if (rpcSocketStream.Connect(rpcServerAddress, rpcServerPort) != PW_STATUS_OK)
{
ChipLogError(NotSpecified, "Failed to connect the Fabric-Admin");
return CHIP_ERROR_NOT_CONNECTED;
}

ChipLogProgress(NotSpecified, "Connectted to the Fabric-Admin\n");

// Start a thread to process incoming packets
std::thread packet_processor(ProcessPackets);
packet_processor.detach();
Expand Down
35 changes: 20 additions & 15 deletions examples/fabric-bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ using namespace chip::Transport;
using namespace chip::DeviceLayer;
using namespace chip::app::Clusters;

#define POLL_INTERVAL_MS (100)
#define RETRY_INTERVAL_S (3)

namespace {

constexpr uint16_t kPollIntervalMs = 100;
constexpr uint16_t kRetryIntervalS = 3;

EndpointId gCurrentEndpointId;
EndpointId gFirstDynamicEndpointId;
Device * gDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];
Expand Down Expand Up @@ -103,9 +103,24 @@ void BridgePollingThread()
}

// Sleep to avoid tight loop reading commands
usleep(POLL_INTERVAL_MS * 1000);
usleep(kPollIntervalMs * 1000);
}
}

#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE)
void AttemptRpcClientConnect(System::Layer * systemLayer, void * appState)
{
if (InitRpcClient(kFabricAdminServerPort) == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Connected to Fabric-Admin");
}
else
{
ChipLogError(NotSpecified, "Failed to connect to Fabric-Admin, retry in %d seconds....", kRetryIntervalS);
systemLayer->StartTimer(System::Clock::Seconds16(kRetryIntervalS), AttemptRpcClientConnect, nullptr);
}
}
#endif

} // namespace

Expand Down Expand Up @@ -302,17 +317,7 @@ void ApplicationInit()
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE)
InitRpcServer(kFabricBridgeServerPort);

while (true)
{
if (InitRpcClient(kFabricAdminServerPort) == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Connected to Fabric-Admin");
break;
}

ChipLogError(NotSpecified, "Failed to connect to Fabric-Admin, retry in %d seconds....", RETRY_INTERVAL_S);
std::this_thread::sleep_for(std::chrono::seconds(RETRY_INTERVAL_S));
}
AttemptRpcClientConnect(&DeviceLayer::SystemLayer(), nullptr);
#endif

// Start a thread for bridge polling
Expand Down

0 comments on commit 71fb5f3

Please sign in to comment.