Skip to content

Commit ff12ada

Browse files
committed
Rename RPC Event to RPC Actions
1 parent aa2d923 commit ff12ada

File tree

15 files changed

+353
-189
lines changed

15 files changed

+353
-189
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "chef-rpc-actions-worker.h"
20+
#include <app-common/zap-generated/attributes/Accessors.h>
21+
#include <app-common/zap-generated/callback.h>
22+
#include <app/data-model/Nullable.h>
23+
#include <app/util/config.h>
24+
#include <lib/core/DataModelTypes.h>
25+
#include <platform/CHIPDeviceLayer.h>
26+
#include <map>
27+
28+
//#include <Actions.h>
29+
30+
using chip::app::DataModel::Nullable;
31+
32+
using namespace chip;
33+
using namespace chip::app;
34+
using namespace chip::app::Clusters;
35+
using namespace chip::rpc;
36+
37+
static std::map<ClusterId, ActionsDelegate *> gActionsDelegateMap {};
38+
39+
static void RpcActionsTaskCallback(System::Layer * systemLayer, void * data)
40+
{
41+
//printf("\033[41m %s , %d, endpointId=%d, clusterId=%d \033[0m \n", __func__, __LINE__, queue->endpointId, queue->clusterId);
42+
43+
// struct ActionsDelegate * delegate = RpcFindActionsDelegate(queue->clusterId);
44+
// if ( nullptr == delegate ) {
45+
// TBD: Error cluster not registered
46+
// return;
47+
// }
48+
49+
// TBD: insert the queue t ActionHandler's queue
50+
// delete queue;
51+
}
52+
53+
bool ChefRpcActionsWorker::publishAction(chip::rpc::ActionTask task)
54+
{
55+
bool kickTimer = queue.size() == 0;
56+
57+
queue.push(task);
58+
59+
if (kickTimer) {
60+
(void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(10), RpcActionsTaskCallback, this);
61+
}
62+
return true;
63+
}
64+
65+
struct ActionsDelegate * RpcFindActionsDelegate(ClusterId clusterId)
66+
{
67+
if (gActionsDelegateMap.find(clusterId) != gActionsDelegateMap.end()) {
68+
return gActionsDelegateMap[clusterId];
69+
}
70+
71+
return nullptr;
72+
}
73+
74+
75+
void ActionsDelegate::RegisterRpcActionsDelegate(ClusterId clusterId, ActionsDelegate * delegate)
76+
{
77+
if ( nullptr == RpcFindActionsDelegate(clusterId) ) {
78+
gActionsDelegateMap[clusterId] = delegate;
79+
return;
80+
}
81+
82+
// TBD: print already registered
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <app/clusters/mode-base-server/mode-base-server.h>
22+
#include <app/util/config.h>
23+
#include <cstring>
24+
#include <utility>
25+
26+
#include "Rpc.h"
27+
28+
namespace chip {
29+
namespace app {
30+
31+
class ActionsDelegate
32+
{
33+
34+
public:
35+
ActionsDelegate(EndpointId endpoint, ClusterId cluster): mEndpointId(endpoint), mClusterId(cluster) { };
36+
37+
virtual ~ActionsDelegate() = default;
38+
39+
virtual void AttributeWriteHandler(chip::AttributeId attributeId, std::vector<uint32_t>args) {};
40+
virtual void CommandHandler(chip::CommandId commandId, std::vector<uint32_t>args) {};
41+
virtual void EventHandler(chip::EventId eventId, std::vector<uint32_t>args) {};
42+
43+
static void RegisterRpcActionsDelegate(ClusterId clusterId, ActionsDelegate * delegate);
44+
45+
protected:
46+
EndpointId mEndpointId;
47+
ClusterId mClusterId;
48+
};
49+
50+
} // namespace app
51+
} // namespace chip
52+
53+
class ChefRpcActionsWorker: public chip::rpc::ActionsSubscriber
54+
{
55+
public:
56+
ChefRpcActionsWorker() = default;
57+
~ChefRpcActionsWorker() override {};
58+
bool publishAction(chip::rpc::ActionTask task) override;
59+
60+
private:
61+
std::queue<chip::rpc::ActionTask> queue;
62+
63+
};

examples/chef/common/clusters/switch/SwitchManager.cpp

+29-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <platform/PlatformManager.h>
2424
#include "SwitchEventHandler.h"
2525

26-
#include "Rpc.h"
26+
#include "chef-rpc-actions-worker.h"
2727

2828
using namespace chip;
2929
using namespace chip::app;
@@ -32,36 +32,46 @@ using namespace chip::app::Clusters::Switch;
3232
using namespace chip::DeviceLayer;
3333

3434
using namespace chip::rpc;
35+
using namespace chip::app;
36+
37+
class SwitchActionsDelegate: public chip::app::ActionsDelegate
38+
{
39+
public:
40+
SwitchActionsDelegate(EndpointId endpointId, ClusterId clusterId, SwitchEventHandler *eventHandler): ActionsDelegate(endpointId, clusterId), mEventHandler(eventHandler){};
41+
~SwitchActionsDelegate() override {};
42+
43+
void AttributeWriteHandler(chip::AttributeId attributeId, std::vector<uint32_t>args) override;
44+
void EventHandler(chip::EventId eventId, std::vector<uint32_t>args) override;
3545

36-
static std::map<int, SwitchEventHandler *> gSwitchEventHandlers{};
3746

38-
SwitchEventHandler * GetSwitchEventHandler(EndpointId endpointId)
47+
private:
48+
SwitchEventHandler *mEventHandler;
49+
};
50+
51+
void SwitchActionsDelegate::AttributeWriteHandler(chip::AttributeId attributeId, std::vector<uint32_t>args)
3952
{
40-
if (gSwitchEventHandlers.find(endpointId) == gSwitchEventHandlers.end()) {
41-
return nullptr;
42-
}
4353

44-
return gSwitchEventHandlers[endpointId];
4554
}
4655

47-
void SwitchManagerEventHandler(intptr_t ctx, struct EventsRequest * data)
56+
void SwitchActionsDelegate::EventHandler(chip::EventId eventId, std::vector<uint32_t>args)
4857
{
49-
SwitchEventHandler * handler = GetSwitchEventHandler(data->endpointId);
50-
51-
if (nullptr == handler) {
52-
// TODO: Error
53-
return;
58+
switch (eventId) {
59+
case Events::SwitchLatched::Id:
60+
{
61+
uint8_t newPosition = static_cast<uint8_t>(args[0]);
62+
mEventHandler->OnSwitchLatched(newPosition);
63+
}
64+
break;
65+
default:
66+
break;
5467
}
55-
// Parse data->events
56-
// According to data->events, to dispatch events
57-
}
68+
};
5869

59-
void emberAfSwitchClusterInitCallback(EndpointId endpoint)
70+
void emberAfSwitchClusterInitCallback(EndpointId endpointId)
6071
{
6172
ChipLogProgress(Zcl, "Chef: emberAfSwitchClusterInitCallback");
62-
gSwitchEventHandlers[endpoint] = new SwitchEventHandler(endpoint);
6373
printf("\033[44m %s, %d, Switch::ID=%u \033[0m \n", __func__, __LINE__, Switch::Id);
64-
RpcRegisterAppEventsHandler( Switch::Id, SwitchManagerEventHandler, reinterpret_cast<intptr_t>(&gSwitchEventHandlers));
74+
ActionsDelegate::RegisterRpcActionsDelegate(Clusters::Switch::Id, new SwitchActionsDelegate(endpointId, Clusters::Switch::Id, new SwitchEventHandler(endpointId)));
6575
}
6676

6777

examples/chef/common/stubs.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "chef-concentration-measurement.h"
2020
#endif
2121

22+
//#include <Actions.h>
23+
2224
using chip::app::DataModel::Nullable;
2325

2426
using namespace chip;

examples/chef/linux/BUILD.gn

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ executable("${sample_name}") {
4747
"${project_dir}/common/chef-laundry-washer-mode.cpp",
4848
"${project_dir}/common/chef-operational-state-delegate-impl.cpp",
4949
"${project_dir}/common/chef-resource-monitoring-delegates.cpp",
50+
"${project_dir}/common/chef-rpc-actions-worker.cpp",
5051
"${project_dir}/common/chef-rvc-mode-delegate.cpp",
5152
"${project_dir}/common/chef-rvc-operational-state-delegate.cpp",
5253
"${project_dir}/common/clusters/audio-output/AudioOutputManager.cpp",
@@ -82,12 +83,12 @@ executable("${sample_name}") {
8283
if (chip_enable_pw_rpc) {
8384
defines = [
8485
"PW_RPC_ENABLED",
86+
"PW_RPC_ACTIONS_SERVICE=1",
8587
"PW_RPC_ATTRIBUTE_SERVICE=1",
8688
"PW_RPC_BOOLEAN_STATE_SERVICE=1",
8789
"PW_RPC_BUTTON_SERVICE=1",
8890
"PW_RPC_DESCRIPTOR_SERVICE=1",
8991
"PW_RPC_DEVICE_SERVICE=1",
90-
"PW_RPC_EVENT_SERVICE=1",
9192
"PW_RPC_LIGHTING_SERVICE=1",
9293
"PW_RPC_TRACING_SERVICE=1",
9394
]
@@ -110,12 +111,12 @@ executable("${sample_name}") {
110111
"$dir_pw_trace_tokenized",
111112
"$dir_pw_trace_tokenized:trace_rpc_service",
112113
"${chip_root}/config/linux/lib/pw_rpc:pw_rpc",
114+
"${chip_root}/examples/common/pigweed:actions_service.nanopb_rpc",
113115
"${chip_root}/examples/common/pigweed:attributes_service.nanopb_rpc",
114116
"${chip_root}/examples/common/pigweed:boolean_state_service.nanopb_rpc",
115117
"${chip_root}/examples/common/pigweed:button_service.nanopb_rpc",
116118
"${chip_root}/examples/common/pigweed:descriptor_service.nanopb_rpc",
117119
"${chip_root}/examples/common/pigweed:device_service.nanopb_rpc",
118-
"${chip_root}/examples/common/pigweed:event_service.nanopb_rpc",
119120
"${chip_root}/examples/common/pigweed:lighting_service.nanopb_rpc",
120121
"${chip_root}/examples/common/pigweed:rpc_services",
121122
]

examples/common/pigweed/BUILD.gn

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ pw_proto_library("echo_service") {
3535
prefix = "echo_service"
3636
}
3737

38+
pw_proto_library("actions_service") {
39+
sources = [ "protos/actions_service.proto" ]
40+
inputs = [ "protos/actions_service.options" ]
41+
deps = [ "$dir_pw_protobuf:common_protos" ]
42+
strip_prefix = "protos"
43+
prefix = "actions_service"
44+
}
45+
3846
pw_proto_library("attributes_service") {
3947
sources = [ "protos/attributes_service.proto" ]
4048
inputs = [ "protos/attributes_service.options" ]
@@ -72,14 +80,6 @@ pw_proto_library("button_service") {
7280
prefix = "button_service"
7381
}
7482

75-
pw_proto_library("event_service") {
76-
sources = [ "protos/event_service.proto" ]
77-
inputs = [ "protos/event_service.options" ]
78-
deps = [ "$dir_pw_protobuf:common_protos" ]
79-
strip_prefix = "protos"
80-
prefix = "event_service"
81-
}
82-
8383
pw_proto_library("lighting_service") {
8484
sources = [ "protos/lighting_service.proto" ]
8585
deps = [ "$dir_pw_protobuf:common_protos" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
syntax = "proto3";
2+
3+
package chip.rpc;
4+
5+
import 'pw_protobuf_protos/common.proto';
6+
7+
enum ActionType {
8+
WRITE_ATTRIBUTE = 0x00; // Write an cluster Attribute
9+
RUN_COMMAND = 0x01; // Run a cluster Command
10+
EMIT_EVENT = 0x02; // Emit a cluster Events
11+
}
12+
13+
message Action {
14+
ActionType type = 1; // ActionType above
15+
uint32 delayMs = 2; // Delay and run action after xx ms
16+
uint32 actionId = 3; // Align with Cluster Attribute/Event/Command ID
17+
optional uint32 arg1 = 4; // 1st attribute
18+
optional uint32 arg2 = 5; // 2nd attribute
19+
optional uint32 arg3 = 6; // 3rd attribute
20+
}
21+
22+
message ActionsWrite {
23+
uint32 endpoint_id = 1;
24+
uint32 cluster_id = 2;
25+
repeated Action actions = 3; // Actions including Attribute Write / Event / Command
26+
}
27+
28+
service Actions {
29+
rpc Set(ActionsWrite) returns (pw.protobuf.Empty){}
30+
}

examples/common/pigweed/protos/event_service.proto

-32
This file was deleted.

examples/common/pigweed/rpc_console/py/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ pw_python_package("chip_rpc") {
3939
"$dir_pw_rpc/py",
4040
"$dir_pw_system/py",
4141
"$dir_pw_tokenizer/py",
42+
"${chip_root}/examples/common/pigweed:actions_service.python",
4243
"${chip_root}/examples/common/pigweed:attributes_service.python",
4344
"${chip_root}/examples/common/pigweed:boolean_state_service.python",
4445
"${chip_root}/examples/common/pigweed:button_service.python",
4546
"${chip_root}/examples/common/pigweed:descriptor_service.python",
4647
"${chip_root}/examples/common/pigweed:device_service.python",
4748
"${chip_root}/examples/common/pigweed:echo_service.python",
48-
"${chip_root}/examples/common/pigweed:event_service.python",
4949
"${chip_root}/examples/common/pigweed:lighting_service.python",
5050
"${chip_root}/examples/common/pigweed:locking_service.python",
5151
"${chip_root}/examples/common/pigweed:ot_cli_service.python",

0 commit comments

Comments
 (0)