@@ -36,48 +36,100 @@ using namespace chip::rpc;
36
36
37
37
static std::map<ClusterId, ActionsDelegate *> gActionsDelegateMap {};
38
38
39
+ ActionsDelegate * RpcFindActionsDelegate (ClusterId clusterId)
40
+ {
41
+ if (gActionsDelegateMap .find (clusterId) != gActionsDelegateMap .end ()) {
42
+ return gActionsDelegateMap [clusterId];
43
+ }
44
+
45
+ return nullptr ;
46
+ }
47
+
39
48
static void RpcActionsTaskCallback (System::Layer * systemLayer, void * data)
40
49
{
41
- // printf("\033[41m %s , %d, endpointId=%d, clusterId=%d \033[0m \n", __func__, __LINE__, queue->endpointId, queue->clusterId );
50
+ ActionTask task = ChefRpcActionsWorker::Instance (). PopActionQueue ( );
42
51
43
- // struct ActionsDelegate * delegate = RpcFindActionsDelegate(queue->clusterId);
44
- // if ( nullptr == delegate ) {
45
- // TBD: Error cluster not registered
46
- // return;
47
- // }
52
+ printf (" \033 [41m %s , %d, endpointId=%d, clusterId=%d \033 [0m \n " , __func__, __LINE__, task.endpointId , task.clusterId );
48
53
54
+ ActionsDelegate * delegate = RpcFindActionsDelegate (task.clusterId );
55
+ if ( nullptr == delegate ) {
56
+ // TBD: Error cluster not registered
57
+ return ;
58
+ }
59
+
60
+ ActionType type = static_cast <ActionType>(task.type );
61
+
62
+ switch (type) {
63
+ case ActionType::WRITE_ATTRIBUTE:
64
+ {
65
+ delegate->AttributeWriteHandler (static_cast <chip::AttributeId>(task.actionId ), task.args );
66
+ }
67
+ break ;
68
+ case ActionType::RUN_COMMAND:
69
+ {
70
+ delegate->CommandHandler (static_cast <chip::CommandId>(task.actionId ), task.args );
71
+ }
72
+ break ;
73
+ case ActionType::EMIT_EVENT:
74
+ {
75
+ delegate->EventHandler (static_cast <chip::EventId>(task.actionId ), task.args );
76
+ }
77
+ break ;
78
+ default :
79
+ break ;
80
+ }
49
81
// TBD: insert the queue t ActionHandler's queue
50
82
// delete queue;
51
83
}
52
84
53
- bool ChefRpcActionsWorker::publishAction (chip::rpc::ActionTask task )
85
+ bool ChefRpcActionsCallback (EndpointId endpointId, ClusterId clusterId, uint8_t type, uint32_t delayMs, uint32_t actionId, std::vector< uint32_t > args )
54
86
{
55
- bool kickTimer = queue.size () == 0 ;
87
+ ActionTask task (endpointId, clusterId, static_cast <ActionType>(type), delayMs, actionId, args);
88
+ // TBD: Stack lock
89
+ return ChefRpcActionsWorker::Instance ().EnqueueAction (task);
90
+ }
56
91
57
- queue.push (task);
92
+ bool ChefRpcActionsWorker::EnqueueAction (ActionTask task)
93
+ {
94
+ bool kickTimer = false ;
58
95
96
+ if (queue.empty ()) {
97
+ queue.push (task);
98
+ kickTimer = true ; // kick timer when the first task is adding to the queue
99
+ }
59
100
if (kickTimer) {
60
101
(void ) DeviceLayer::SystemLayer ().StartTimer (System::Clock::Milliseconds32 (10 ), RpcActionsTaskCallback, this );
61
102
}
62
103
return true ;
63
104
}
64
105
65
- struct ActionsDelegate * RpcFindActionsDelegate (ClusterId clusterId )
106
+ ActionTask ChefRpcActionsWorker::PopActionQueue ( )
66
107
{
67
- if ( gActionsDelegateMap . find (clusterId) != gActionsDelegateMap . end ()) {
68
- return gActionsDelegateMap [clusterId] ;
69
- }
108
+ // assert !queue.empty()
109
+ ActionTask task = queue. front () ;
110
+ queue. pop ();
70
111
71
- return nullptr ;
112
+ return task ;
72
113
}
73
114
74
-
75
- void ActionsDelegate::RegisterRpcActionsDelegate (ClusterId clusterId, ActionsDelegate * delegate)
115
+ void ChefRpcActionsWorker::RegisterRpcActionsDelegate (ClusterId clusterId, ActionsDelegate * delegate)
76
116
{
77
117
if ( nullptr == RpcFindActionsDelegate (clusterId) ) {
78
118
gActionsDelegateMap [clusterId] = delegate;
79
119
return ;
80
120
}
81
-
82
121
// TBD: print already registered
83
122
}
123
+
124
+ ChefRpcActionsWorker::ChefRpcActionsWorker ()
125
+ {
126
+ chip::rpc::SubscribeActions (ChefRpcActionsCallback);
127
+ }
128
+
129
+ static ChefRpcActionsWorker instance;
130
+
131
+ ChefRpcActionsWorker & ChefRpcActionsWorker::Instance ()
132
+ {
133
+ return instance;
134
+ }
135
+
0 commit comments