Skip to content

Commit

Permalink
Cross platform built on ESP32/nRFConnect done
Browse files Browse the repository at this point in the history
And also enable the ApplicationInit
  • Loading branch information
erwinpan1 committed May 1, 2024
1 parent a66c941 commit 5ace910
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 19 deletions.
13 changes: 6 additions & 7 deletions examples/chef/common/chef-rpc-actions-worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,28 @@ void ChefRpcActionsWorker::ProcessActionQueue()
ActionTask task = queue.front();
queue.pop();

printf("\033[41m %s , %d, endpointId=%d, clusterId=%d \033[0m \n", __func__, __LINE__, task.endpointId, task.clusterId);

ActionsDelegate * delegate = RpcFindActionsDelegate(task.clusterId);
if ( nullptr == delegate ) {
printf("\033[41m %s , %d, Cannot run action due to not finding delegate: endpointId=%d, clusterId=%d \033[0m \n", __func__, __LINE__, task.endpointId, task.clusterId);
ChipLogError(NotSpecified, "Cannot run action due to not finding delegate: endpointId=%d, clusterId=%04lx, attributeId=%04lx \033[0m \n", task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId));
} else {
ActionType type = static_cast<ActionType>(task.type);

switch (type) {
case ActionType::WRITE_ATTRIBUTE:
{
printf("\033[41m %s , %d, Writing Attribute: %d, args size=%lu \033[0m \n", __func__, __LINE__, task.actionId, task.args.size());
ChipLogProgress(NotSpecified, "Writing Attribute: endpointId=%d, clusterId=%04lx, attributeId=%04lx, args.size=%lu", task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), static_cast<unsigned long>(task.args.size()));
delegate->AttributeWriteHandler(task.endpointId, static_cast<chip::AttributeId>(task.actionId), task.args);
}
break;
case ActionType::RUN_COMMAND:
{
printf("\033[41m %s , %d, Running Command: %d, args size=%lu \033[0m \n", __func__, __LINE__, task.actionId, task.args.size());
ChipLogProgress(NotSpecified, "Running Command: endpointId=%d, clusterId=%04lx, commandId=%04lx, args.size=%lu",task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), static_cast<unsigned long>(task.args.size()));
delegate->CommandHandler(task.endpointId, static_cast<chip::CommandId>(task.actionId), task.args);
}
break;
case ActionType::EMIT_EVENT:
{
printf("\033[41m %s , %d, Emitting Event: %d, args size=%lu \033[0m \n", __func__, __LINE__, task.actionId, task.args.size());
ChipLogProgress(NotSpecified, "Emitting Event: endpointId=%d, clusterId=%04lx, eventIdId=%04lx, args.size=%lu",task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), static_cast<unsigned long>(task.args.size()));
delegate->EventHandler(task.endpointId, static_cast<chip::EventId>(task.actionId), task.args);
}
break;
Expand All @@ -119,7 +117,8 @@ printf("\033[41m %s , %d, Emitting Event: %d, args size=%lu \033[0m \n", __func_

// Run next action
task = queue.front();
printf("\033[44m %s , %d, next action: endpointId=%d, clusterId=%d, delayMs=%d \033[0m \n", __func__, __LINE__, task.endpointId, task.clusterId, task.delayMs);
printf("\033[44m %s, %d, start new timer \033[0m \n", __func__, __LINE__);
ChipLogProgress(NotSpecified, "StartTimer to run next action: endpointId=%d, clusterId=%04lx, eventIdId=%04lx, task.delyMs=%lu",task.endpointId, static_cast<unsigned long>(task.clusterId), static_cast<unsigned long>(task.actionId), static_cast<unsigned long>(task.delayMs));
(void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(task.delayMs), RpcActionsTaskCallback, this);
}

Expand Down
2 changes: 2 additions & 0 deletions examples/chef/common/chef-rpc-actions-worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <app/clusters/mode-base-server/mode-base-server.h>
#include <app/util/config.h>
#include <cstring>
#include <vector>
#include <utility>
#include <queue>

#include "Rpc.h"

Expand Down
12 changes: 12 additions & 0 deletions examples/chef/common/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,15 @@ void emberAfWakeOnLanClusterInitCallback(EndpointId endpoint)
WakeOnLan::SetDefaultDelegate(endpoint, &wakeOnLanManager);
}
#endif

void ApplicationInit()
{
ChipLogProgress(NotSpecified, "Chef Application Init !!!")
printf("\033[41m %s, %d \033[0m\n", __func__, __LINE__);
}

void ApplicationShutdown()
{
ChipLogProgress(NotSpecified, "Chef Application Down !!!")
printf("\033[41m %s, %d \033[0m\n", __func__, __LINE__);
}
6 changes: 6 additions & 0 deletions examples/chef/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ void printQRCode()
app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance()));

extern void ApplicationInit();

void InitServer(intptr_t)
{
// Start IM server
Expand All @@ -172,6 +174,10 @@ void InitServer(intptr_t)
// Register a function to receive events from the CHIP device layer. Note that calls to
// this function will happen on the CHIP event loop thread, not the app_main thread.
PlatformMgr().AddEventHandler(DeviceEventCallback, reinterpret_cast<intptr_t>(nullptr));

// Application code should always be initialised after the initialisation of
// server.
ApplicationInit();
}

extern "C" void app_main(void)
Expand Down
4 changes: 0 additions & 4 deletions examples/chef/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ using namespace chip::Shell;
using namespace chip::app;
using namespace chip::app::Clusters;

void ApplicationInit() {}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
Expand Down
9 changes: 9 additions & 0 deletions examples/chef/nrfconnect/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ chip::Crypto::PSAOperationalKeystore sPSAOperationalKeystore{};
#endif
} // namespace

extern void ApplicationInit();

void InitServer(intptr_t)
{
ApplicationInit();
}

int main()
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -168,6 +175,8 @@ int main()
cmd_app_server_init();
#endif

chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer);

#if CONFIG_CHIP_LIB_SHELL
Engine::Root().RunMainLoop();
#endif
Expand Down
15 changes: 15 additions & 0 deletions examples/platform/esp32/Rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ static TaskHandle_t sRpcTaskHandle;
StaticTask_t sRpcTaskBuffer;
StackType_t sRpcTaskStack[RPC_TASK_STACK_SIZE];

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
Actions actions_service;
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Attributes attributes_service;
#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Expand Down Expand Up @@ -321,6 +325,10 @@ Esp32WiFi wifi_service;

void RegisterServices(pw::rpc::Server & server)
{
#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
server.RegisterService(actions_service);
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
server.RegisterService(attributes_service);
#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Expand Down Expand Up @@ -365,6 +373,13 @@ void RegisterServices(pw::rpc::Server & server)

} // namespace

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
void SubscribeActions(RpcActionsSubscribeCallback subscriber)
{
actions_service.SubscribeActions(subscriber);
}
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

void RunRpcService(void *)
{
Start(RegisterServices, &logger_mutex);
Expand Down
11 changes: 11 additions & 0 deletions examples/platform/esp32/Rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
namespace chip {
namespace rpc {

enum class ActionType : uint8_t
{
WRITE_ATTRIBUTE = 0x00, // Write an cluster Attribute
RUN_COMMAND = 0x01, // Run a cluster Command
EMIT_EVENT = 0x02, // Emit a cluster Events
};

using RpcActionsSubscribeCallback = bool (*)(EndpointId endpointId, ClusterId clusterId, uint8_t type, uint32_t delayMs, uint32_t actionId, std::vector<uint32_t> args);

void SubscribeActions(RpcActionsSubscribeCallback subscriber);

void Init();

} // namespace rpc
Expand Down
16 changes: 8 additions & 8 deletions examples/platform/linux/Rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include <map>
#include "Rpc.h"

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
#include "pigweed/rpc_services/Actions.h"
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
#include "pigweed/rpc_services/Attributes.h"
#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Expand All @@ -42,10 +46,6 @@
#include "pigweed/rpc_services/Device.h"
#endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
#include "pigweed/rpc_services/Actions.h"
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
#include "pigweed/rpc_services/Lighting.h"
#endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
Expand Down Expand Up @@ -76,6 +76,10 @@ namespace chip {
namespace rpc {
namespace {

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
Actions actions_service;
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Attributes attributes_service;
#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Expand All @@ -92,10 +96,6 @@ Descriptor descriptor_service;
Device device_service;
#endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
Actions actions_service;
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
Lighting lighting_service;
#endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
Expand Down
19 changes: 19 additions & 0 deletions examples/platform/nrfconnect/Rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
#include "pigweed/rpc_services/Actions.h"
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
#include "pigweed/rpc_services/Attributes.h"
#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Expand Down Expand Up @@ -89,6 +93,10 @@ size_t pw_trace_GetTraceTimeTicksPerSecond()
namespace chip {
namespace rpc {

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
Actions actions_service;
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
namespace {

Expand Down Expand Up @@ -189,6 +197,10 @@ pw::trace::TraceService trace_service(pw::trace::GetTokenizedTracer());

void RegisterServices(pw::rpc::Server & server)
{
#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
server.RegisterService(actions_service);
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

#if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
server.RegisterService(attributes_service);
#endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
Expand Down Expand Up @@ -237,6 +249,13 @@ void RegisterServices(pw::rpc::Server & server)

} // namespace

#if defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE
void SubscribeActions(RpcActionsSubscribeCallback subscriber)
{
actions_service.SubscribeActions(subscriber);
}
#endif // defined(PW_RPC_ACTIONS_SERVICE) && PW_RPC_ACTIONS_SERVICE

void RunRpcService(void *, void *, void *)
{
Start(RegisterServices, &logger_mutex);
Expand Down
12 changes: 12 additions & 0 deletions examples/platform/nrfconnect/Rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
namespace chip {
namespace rpc {

enum class ActionType : uint8_t
{
WRITE_ATTRIBUTE = 0x00, // Write an cluster Attribute
RUN_COMMAND = 0x01, // Run a cluster Command
EMIT_EVENT = 0x02, // Emit a cluster Events
};

using RpcActionsSubscribeCallback = bool (*)(EndpointId endpointId, ClusterId clusterId, uint8_t type, uint32_t delayMs, uint32_t actionId, std::vector<uint32_t> args);

void SubscribeActions(RpcActionsSubscribeCallback subscriber);


class NrfButton;

void RunRpcService(void *, void *, void *);
Expand Down

0 comments on commit 5ace910

Please sign in to comment.