Skip to content

Commit 7e13df1

Browse files
committed
Chef RPC Actions Draft done.
TBD: Debug Optimize Error code Confirm timer is ms
1 parent c952a9a commit 7e13df1

File tree

8 files changed

+172
-98
lines changed

8 files changed

+172
-98
lines changed

examples/chef/common/chef-rpc-actions-worker.cpp

+46-36
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,10 @@ ActionsDelegate * RpcFindActionsDelegate(ClusterId clusterId)
4747

4848
static void RpcActionsTaskCallback(System::Layer * systemLayer, void * data)
4949
{
50-
ActionTask task = ChefRpcActionsWorker::Instance().PopActionQueue();
50+
ChefRpcActionsWorker * worker = (ChefRpcActionsWorker *)data;
51+
printf("\033[44m %s , %d \033[0m \n", __func__, __LINE__);
5152

52-
printf("\033[41m %s , %d, endpointId=%d, clusterId=%d \033[0m \n", __func__, __LINE__, task.endpointId, task.clusterId);
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-
}
81-
// TBD: insert the queue t ActionHandler's queue
82-
// delete queue;
53+
worker->ProcessActionQueue();
8354
}
8455

8556
bool ChefRpcActionsCallback(EndpointId endpointId, ClusterId clusterId, uint8_t type, uint32_t delayMs, uint32_t actionId, std::vector<uint32_t> args)
@@ -98,18 +69,57 @@ bool ChefRpcActionsWorker::EnqueueAction(ActionTask task)
9869
kickTimer = true; // kick timer when the first task is adding to the queue
9970
}
10071
if (kickTimer) {
101-
(void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(10), RpcActionsTaskCallback, this);
72+
(void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(task.delayMs), RpcActionsTaskCallback, this);
10273
}
10374
return true;
10475
}
10576

106-
ActionTask ChefRpcActionsWorker::PopActionQueue()
77+
void ChefRpcActionsWorker::ProcessActionQueue()
10778
{
108-
// assert !queue.empty()
79+
// delete queue;
10980
ActionTask task = queue.front();
11081
queue.pop();
11182

112-
return task;
83+
printf("\033[41m %s , %d, endpointId=%d, clusterId=%d \033[0m \n", __func__, __LINE__, task.endpointId, task.clusterId);
84+
85+
ActionsDelegate * delegate = RpcFindActionsDelegate(task.clusterId);
86+
if ( nullptr == delegate ) {
87+
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);
88+
} else {
89+
ActionType type = static_cast<ActionType>(task.type);
90+
91+
switch (type) {
92+
case ActionType::WRITE_ATTRIBUTE:
93+
{
94+
printf("\033[41m %s , %d, Writing Attribute: %d, args size=%lu \033[0m \n", __func__, __LINE__, task.actionId, task.args.size());
95+
delegate->AttributeWriteHandler(task.endpointId, static_cast<chip::AttributeId>(task.actionId), task.args);
96+
}
97+
break;
98+
case ActionType::RUN_COMMAND:
99+
{
100+
printf("\033[41m %s , %d, Running Command: %d, args size=%lu \033[0m \n", __func__, __LINE__, task.actionId, task.args.size());
101+
delegate->CommandHandler(task.endpointId, static_cast<chip::CommandId>(task.actionId), task.args);
102+
}
103+
break;
104+
case ActionType::EMIT_EVENT:
105+
{
106+
printf("\033[41m %s , %d, Emitting Event: %d, args size=%lu \033[0m \n", __func__, __LINE__, task.actionId, task.args.size());
107+
delegate->EventHandler(task.endpointId, static_cast<chip::EventId>(task.actionId), task.args);
108+
}
109+
break;
110+
default:
111+
break;
112+
}
113+
}
114+
115+
if (queue.empty()) {
116+
// Return due to no extra queue item to run.
117+
return;
118+
}
119+
120+
// Run next task
121+
task = queue.front();
122+
(void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(task.delayMs), RpcActionsTaskCallback, this);
113123
}
114124

115125
void ChefRpcActionsWorker::RegisterRpcActionsDelegate(ClusterId clusterId, ActionsDelegate * delegate)

examples/chef/common/chef-rpc-actions-worker.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@ namespace app {
3030

3131
class ActionsDelegate
3232
{
33-
3433
public:
35-
ActionsDelegate(EndpointId endpoint, ClusterId cluster): mEndpointId(endpoint), mClusterId(cluster) { };
34+
ActionsDelegate(ClusterId clusterId): mClusterId(clusterId) { };
3635

3736
virtual ~ActionsDelegate() = default;
3837

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) {};
38+
virtual void AttributeWriteHandler(chip::EndpointId endpointId, chip::AttributeId attributeId, std::vector<uint32_t>args) {};
39+
virtual void CommandHandler(chip::EndpointId endpointId, chip::CommandId commandId, std::vector<uint32_t>args) {};
40+
virtual void EventHandler(chip::EndpointId endpointId, chip::EventId eventId, std::vector<uint32_t>args) {};
4241

4342
protected:
44-
EndpointId mEndpointId;
4543
ClusterId mClusterId;
4644
};
4745

@@ -65,7 +63,7 @@ class ChefRpcActionsWorker
6563
ChefRpcActionsWorker();
6664

6765
bool EnqueueAction(ActionTask task);
68-
ActionTask PopActionQueue();
66+
void ProcessActionQueue();
6967
void RegisterRpcActionsDelegate(ClusterId clusterId, ActionsDelegate * delegate);
7068

7169
private:

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,61 @@ using namespace chip::app::Clusters::Switch;
3131
using namespace chip::DeviceLayer;
3232

3333

34-
void SwitchEventHandler::OnSwitchLatched(uint8_t newPosition)
34+
void SwitchEventHandler::OnSwitchLatched(EndpointId endpointId, uint8_t newPosition)
3535
{
3636
ChipLogDetail(NotSpecified, "The latching switch is moved to a new position:%d", newPosition);
3737

38-
Clusters::SwitchServer::Instance().OnSwitchLatch(mEndpointId, newPosition);
38+
Clusters::SwitchServer::Instance().OnSwitchLatch(endpointId, newPosition);
3939
}
4040

41-
void SwitchEventHandler::OnSwitchInitialPressed(uint8_t newPosition)
41+
void SwitchEventHandler::OnInitialPress(EndpointId endpointId, uint8_t newPosition)
4242
{
4343
ChipLogDetail(NotSpecified, "The new position when the momentary switch starts to be pressed:%d", newPosition);
4444

45-
Clusters::SwitchServer::Instance().OnInitialPress(mEndpointId, newPosition);
45+
Clusters::SwitchServer::Instance().OnInitialPress(endpointId, newPosition);
4646
}
4747

48-
void SwitchEventHandler::OnSwitchLongPressed(uint8_t newPosition)
48+
void SwitchEventHandler::OnLongPress(EndpointId endpointId, uint8_t newPosition)
4949
{
5050
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed for a long time:%d", newPosition);
5151

52-
Clusters::SwitchServer::Instance().OnLongPress(mEndpointId, newPosition);
52+
Clusters::SwitchServer::Instance().OnLongPress(endpointId, newPosition);
5353
}
5454

55-
void SwitchEventHandler::OnSwitchShortReleased(uint8_t previousPosition)
55+
void SwitchEventHandler::OnShortRelease(EndpointId endpointId, uint8_t previousPosition)
5656
{
5757
ChipLogDetail(NotSpecified, "The the previous value of the CurrentPosition when the momentary switch has been released:%d",
5858
previousPosition);
5959

60-
Clusters::SwitchServer::Instance().OnShortRelease(mEndpointId, previousPosition);
60+
Clusters::SwitchServer::Instance().OnShortRelease(endpointId, previousPosition);
6161
}
6262

63-
void SwitchEventHandler::OnSwitchLongReleased(uint8_t previousPosition)
63+
void SwitchEventHandler::OnLongRelease(EndpointId endpointId, uint8_t previousPosition)
6464
{
6565
ChipLogDetail(NotSpecified,
6666
"The the previous value of the CurrentPosition when the momentary switch has been released after having been "
6767
"pressed for a long time:%d",
6868
previousPosition);
6969

70-
Clusters::SwitchServer::Instance().OnLongRelease(mEndpointId, previousPosition);
70+
Clusters::SwitchServer::Instance().OnLongRelease(endpointId, previousPosition);
7171
}
7272

73-
void SwitchEventHandler::OnSwitchMultiPressOngoing(uint8_t newPosition, uint8_t count)
73+
void SwitchEventHandler::OnMultiPressOngoing(EndpointId endpointId, uint8_t newPosition, uint8_t count)
7474
{
7575
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed in a multi-press sequence:%d",
7676
newPosition);
7777
ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count);
7878

79-
Clusters::SwitchServer::Instance().OnMultiPressOngoing(mEndpointId, newPosition, count);
79+
Clusters::SwitchServer::Instance().OnMultiPressOngoing(endpointId, newPosition, count);
8080
}
8181

82-
void SwitchEventHandler::OnSwitchMultiPressComplete(uint8_t previousPosition, uint8_t count)
82+
void SwitchEventHandler::OnMultiPressComplete(EndpointId endpointId, uint8_t previousPosition, uint8_t count)
8383
{
8484
ChipLogDetail(NotSpecified, "The previous position when the momentary switch has been pressed in a multi-press sequence:%d",
8585
previousPosition);
8686
ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count);
8787

88-
Clusters::SwitchServer::Instance().OnMultiPressComplete(mEndpointId, previousPosition, count);
88+
Clusters::SwitchServer::Instance().OnMultiPressComplete(endpointId, previousPosition, count);
8989
}
9090

9191

examples/chef/common/clusters/switch/SwitchEventHandler.h

+8-17
Original file line numberDiff line numberDiff line change
@@ -32,59 +32,50 @@ namespace Switch {
3232
class SwitchEventHandler
3333
{
3434
public:
35-
SwitchEventHandler(EndpointId endpoint):mEndpointId(endpoint){};
35+
SwitchEventHandler() {};
3636

3737
/**
3838
* Should be called when the latching switch is moved to a new position.
3939
*/
40-
void OnSwitchLatched(uint8_t newPosition);
40+
void OnSwitchLatched(EndpointId endpointId, uint8_t newPosition);
4141

4242
/**
4343
* Should be called when the momentary switch starts to be pressed.
4444
*/
45-
void OnSwitchInitialPressed(uint8_t newPosition);
45+
void OnInitialPress(EndpointId endpointId, uint8_t newPosition);
4646

4747
/**
4848
* Should be called when the momentary switch has been pressed for a "long" time.
4949
*/
50-
void OnSwitchLongPressed(uint8_t newPosition);
50+
void OnLongPress(EndpointId endpointId, uint8_t newPosition);
5151

5252
/**
5353
* Should be called when the momentary switch has been released.
5454
*/
55-
void OnSwitchShortReleased(uint8_t previousPosition);
55+
void OnShortRelease(EndpointId endpointId, uint8_t previousPosition);
5656

5757
/**
5858
* Should be called when the momentary switch has been released after having been pressed for a long time.
5959
*/
60-
void OnSwitchLongReleased(uint8_t previousPosition);
60+
void OnLongRelease(EndpointId endpointId, uint8_t previousPosition);
6161

6262
/**
6363
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
6464
* sequence, during that sequence.
6565
*/
66-
void OnSwitchMultiPressOngoing(uint8_t newPosition, uint8_t count);
66+
void OnMultiPressOngoing(EndpointId endpointId, uint8_t newPosition, uint8_t count);
6767

6868
/**
6969
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
7070
* sequence, after it has been detected that the sequence has ended.
7171
*/
72-
void OnSwitchMultiPressComplete(uint8_t previousPosition, uint8_t count);
72+
void OnMultiPressComplete(EndpointId endpointId, uint8_t previousPosition, uint8_t count);
7373

7474
private:
75-
EndpointId mEndpointId;
7675
};
7776

7877

7978
} // namespace Switch
8079
} // namespace Clusters
8180
} // namespace app
8281
} // namespace chip
83-
84-
85-
86-
//class AllClustersCommandDelegate : public NamedPipeCommandDelegate
87-
//{
88-
//public:
89-
// void OnEventCommandReceived(const char * json) override;
90-
//};

0 commit comments

Comments
 (0)