Skip to content

Commit a104eb4

Browse files
committed
RPC can call delegate to SwitchManager/EventHandler
1 parent de798bb commit a104eb4

File tree

8 files changed

+251
-130
lines changed

8 files changed

+251
-130
lines changed

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

-43
This file was deleted.

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

+10-72
Original file line numberDiff line numberDiff line change
@@ -22,89 +22,45 @@
2222
#include <app/util/att-storage.h>
2323
#include <platform/PlatformManager.h>
2424

25+
#include "SwitchEventHandler.h"
26+
2527
using namespace chip;
2628
using namespace chip::app;
2729
using namespace chip::app::Clusters;
2830
using namespace chip::app::Clusters::Switch;
2931
using namespace chip::DeviceLayer;
3032

31-
class SwitchEventHandler
32-
{
33-
public:
34-
SwitchEventHandler(EndpointId endpoint):mEndpointId(endpoint){};
35-
36-
/**
37-
* Should be called when the latching switch is moved to a new position.
38-
*/
39-
void OnSwitchLatchedHandler(uint8_t newPosition);
40-
41-
/**
42-
* Should be called when the momentary switch starts to be pressed.
43-
*/
44-
void OnSwitchInitialPressedHandler(uint8_t newPosition);
45-
46-
/**
47-
* Should be called when the momentary switch has been pressed for a "long" time.
48-
*/
49-
void OnSwitchLongPressedHandler(uint8_t newPosition);
50-
51-
/**
52-
* Should be called when the momentary switch has been released.
53-
*/
54-
void OnSwitchShortReleasedHandler(uint8_t previousPosition);
55-
56-
/**
57-
* Should be called when the momentary switch has been released after having been pressed for a long time.
58-
*/
59-
void OnSwitchLongReleasedHandler(uint8_t previousPosition);
60-
61-
/**
62-
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
63-
* sequence, during that sequence.
64-
*/
65-
void OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count);
66-
67-
/**
68-
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
69-
* sequence, after it has been detected that the sequence has ended.
70-
*/
71-
void OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count);
72-
73-
private:
74-
EndpointId mEndpointId;
75-
};
76-
77-
78-
void SwitchEventHandler::OnSwitchLatchedHandler(uint8_t newPosition)
33+
34+
void SwitchEventHandler::OnSwitchLatched(uint8_t newPosition)
7935
{
8036
ChipLogDetail(NotSpecified, "The latching switch is moved to a new position:%d", newPosition);
8137

8238
Clusters::SwitchServer::Instance().OnSwitchLatch(mEndpointId, newPosition);
8339
}
8440

85-
void SwitchEventHandler::OnSwitchInitialPressedHandler(uint8_t newPosition)
41+
void SwitchEventHandler::OnSwitchInitialPressed(uint8_t newPosition)
8642
{
8743
ChipLogDetail(NotSpecified, "The new position when the momentary switch starts to be pressed:%d", newPosition);
8844

8945
Clusters::SwitchServer::Instance().OnInitialPress(mEndpointId, newPosition);
9046
}
9147

92-
void SwitchEventHandler::OnSwitchLongPressedHandler(uint8_t newPosition)
48+
void SwitchEventHandler::OnSwitchLongPressed(uint8_t newPosition)
9349
{
9450
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed for a long time:%d", newPosition);
9551

9652
Clusters::SwitchServer::Instance().OnLongPress(mEndpointId, newPosition);
9753
}
9854

99-
void SwitchEventHandler::OnSwitchShortReleasedHandler(uint8_t previousPosition)
55+
void SwitchEventHandler::OnSwitchShortReleased(uint8_t previousPosition)
10056
{
10157
ChipLogDetail(NotSpecified, "The the previous value of the CurrentPosition when the momentary switch has been released:%d",
10258
previousPosition);
10359

10460
Clusters::SwitchServer::Instance().OnShortRelease(mEndpointId, previousPosition);
10561
}
10662

107-
void SwitchEventHandler::OnSwitchLongReleasedHandler(uint8_t previousPosition)
63+
void SwitchEventHandler::OnSwitchLongReleased(uint8_t previousPosition)
10864
{
10965
ChipLogDetail(NotSpecified,
11066
"The the previous value of the CurrentPosition when the momentary switch has been released after having been "
@@ -114,7 +70,7 @@ void SwitchEventHandler::OnSwitchLongReleasedHandler(uint8_t previousPosition)
11470
Clusters::SwitchServer::Instance().OnLongRelease(mEndpointId, previousPosition);
11571
}
11672

117-
void SwitchEventHandler::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count)
73+
void SwitchEventHandler::OnSwitchMultiPressOngoing(uint8_t newPosition, uint8_t count)
11874
{
11975
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed in a multi-press sequence:%d",
12076
newPosition);
@@ -123,7 +79,7 @@ void SwitchEventHandler::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, u
12379
Clusters::SwitchServer::Instance().OnMultiPressOngoing(mEndpointId, newPosition, count);
12480
}
12581

126-
void SwitchEventHandler::OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count)
82+
void SwitchEventHandler::OnSwitchMultiPressComplete(uint8_t previousPosition, uint8_t count)
12783
{
12884
ChipLogDetail(NotSpecified, "The previous position when the momentary switch has been pressed in a multi-press sequence:%d",
12985
previousPosition);
@@ -132,22 +88,4 @@ void SwitchEventHandler::OnSwitchMultiPressCompleteHandler(uint8_t previousPosit
13288
Clusters::SwitchServer::Instance().OnMultiPressComplete(mEndpointId, previousPosition, count);
13389
}
13490

135-
static std::map<int, SwitchEventHandler *> gSwitchEventHandlers{};
136-
137-
SwitchEventHandler * GetSwitchEventHandler(EndpointId endpointId)
138-
{
139-
if (gSwitchEventHandlers.find(endpointId) == gSwitchEventHandlers.end()) {
140-
return nullptr;
141-
}
142-
143-
return gSwitchEventHandlers[endpointId];
144-
}
145-
146-
void emberAfSwitchClusterInitCallback(EndpointId endpoint)
147-
{
148-
ChipLogProgress(Zcl, "Chef: emberAfSwitchClusterInitCallback");
149-
gSwitchEventHandlers[endpoint] = new SwitchEventHandler(endpoint);
150-
printf("\033[44m %s, %d, Switch::ID=%u \033[0m \n", __func__, __LINE__, Switch::Id);
151-
}
152-
15391

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 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-common/zap-generated/cluster-objects.h>
22+
#include <app/clusters/switch-server/switch-server.h>
23+
24+
#include <protocols/interaction_model/StatusCode.h>
25+
26+
namespace chip {
27+
namespace app {
28+
namespace Clusters {
29+
30+
namespace Switch {
31+
32+
class SwitchEventHandler
33+
{
34+
public:
35+
SwitchEventHandler(EndpointId endpoint):mEndpointId(endpoint){};
36+
37+
/**
38+
* Should be called when the latching switch is moved to a new position.
39+
*/
40+
void OnSwitchLatched(uint8_t newPosition);
41+
42+
/**
43+
* Should be called when the momentary switch starts to be pressed.
44+
*/
45+
void OnSwitchInitialPressed(uint8_t newPosition);
46+
47+
/**
48+
* Should be called when the momentary switch has been pressed for a "long" time.
49+
*/
50+
void OnSwitchLongPressed(uint8_t newPosition);
51+
52+
/**
53+
* Should be called when the momentary switch has been released.
54+
*/
55+
void OnSwitchShortReleased(uint8_t previousPosition);
56+
57+
/**
58+
* Should be called when the momentary switch has been released after having been pressed for a long time.
59+
*/
60+
void OnSwitchLongReleased(uint8_t previousPosition);
61+
62+
/**
63+
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
64+
* sequence, during that sequence.
65+
*/
66+
void OnSwitchMultiPressOngoing(uint8_t newPosition, uint8_t count);
67+
68+
/**
69+
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
70+
* sequence, after it has been detected that the sequence has ended.
71+
*/
72+
void OnSwitchMultiPressComplete(uint8_t previousPosition, uint8_t count);
73+
74+
private:
75+
EndpointId mEndpointId;
76+
};
77+
78+
79+
} // namespace Switch
80+
} // namespace Clusters
81+
} // namespace app
82+
} // namespace chip
83+
84+
85+
86+
//class AllClustersCommandDelegate : public NamedPipeCommandDelegate
87+
//{
88+
//public:
89+
// void OnEventCommandReceived(const char * json) override;
90+
//};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 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 <app-common/zap-generated/attributes/Accessors.h>
20+
#include <app/clusters/switch-server/switch-server.h>
21+
#include <app/server/Server.h>
22+
#include <app/util/att-storage.h>
23+
#include <platform/PlatformManager.h>
24+
#include "SwitchEventHandler.h"
25+
26+
#include "Rpc.h"
27+
28+
using namespace chip;
29+
using namespace chip::app;
30+
using namespace chip::app::Clusters;
31+
using namespace chip::app::Clusters::Switch;
32+
using namespace chip::DeviceLayer;
33+
34+
using namespace chip::rpc;
35+
36+
static std::map<int, SwitchEventHandler *> gSwitchEventHandlers{};
37+
38+
SwitchEventHandler * GetSwitchEventHandler(EndpointId endpointId)
39+
{
40+
if (gSwitchEventHandlers.find(endpointId) == gSwitchEventHandlers.end()) {
41+
return nullptr;
42+
}
43+
44+
return gSwitchEventHandlers[endpointId];
45+
}
46+
47+
void SwitchManagerEventHandler(intptr_t ctx, struct EventsRequest * data)
48+
{
49+
SwitchEventHandler * handler = GetSwitchEventHandler(data->endpointId);
50+
51+
if (nullptr == handler) {
52+
// TODO: Error
53+
return;
54+
}
55+
// Parse data->events
56+
// According to data->events, to dispatch events
57+
}
58+
59+
void emberAfSwitchClusterInitCallback(EndpointId endpoint)
60+
{
61+
ChipLogProgress(Zcl, "Chef: emberAfSwitchClusterInitCallback");
62+
gSwitchEventHandlers[endpoint] = new SwitchEventHandler(endpoint);
63+
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));
65+
}
66+
67+

examples/chef/linux/BUILD.gn

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ executable("${sample_name}") {
5858
"${project_dir}/common/clusters/low-power/LowPowerManager.cpp",
5959
"${project_dir}/common/clusters/media-input/MediaInputManager.cpp",
6060
"${project_dir}/common/clusters/media-playback/MediaPlaybackManager.cpp",
61-
"${project_dir}/common/clusters/switch/SwitchDelegate.cpp",
61+
"${project_dir}/common/clusters/switch/SwitchEventHandler.cpp",
62+
"${project_dir}/common/clusters/switch/SwitchManager.cpp",
6263
"${project_dir}/common/clusters/target-navigator/TargetNavigatorManager.cpp",
6364
"${project_dir}/common/clusters/wake-on-lan/WakeOnLanManager.cpp",
6465
"${project_dir}/common/stubs.cpp",

examples/common/pigweed/rpc_services/Event.h

+10-14
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,10 @@ class Event final : public pw_rpc::nanopb::Event::Service<Event>
3333
public:
3434
::pw::Status Set( const ::chip_rpc_EventSetRequest& request, ::chip_rpc_EventSetResponse& response)
3535
{
36-
EndpointId endpointId = request.endpoint_id;
37-
uint8_t newPosition = 1 ; // to be parsed from request.event_payload
38-
39-
printf("\033[41m %s, %d, request.event_playload=%s \033[0m \n", __func__, __LINE__, request.event_payload);
40-
EventNumber eventNumber;
41-
{
42-
DeviceLayer::StackLock lock;
43-
44-
// Update attribute first, then emit SwitchLatched event only on success.
45-
RETURN_STATUS_IF_NOT_OK(app::Clusters::Switch::Attributes::CurrentPosition::Set(endpointId, newPosition));
36+
printf("\033[41m %s, %d, request.endpoint_id=%d, request.cluster_id=%d, request.event_playload=%s \033[0m \n", __func__, __LINE__, request.endpoint_id, request.cluster_id, request.event_payload);
4637

47-
chip::app::Clusters::Switch::Events::SwitchLatched::Type event{ newPosition };
48-
RETURN_STATUS_IF_NOT_OK(app::LogEvent(event, endpointId, eventNumber));
49-
}
38+
mEventsSubscriber(request.endpoint_id, request.cluster_id, request.event_payload);
5039

51-
response.event_number = static_cast<uint64_t>(eventNumber);
5240
return pw::OkStatus();
5341
}
5442

@@ -66,6 +54,14 @@ printf("\033[41m %s, %d, request.event_playload=%s \033[0m \n", __func__, __LINE
6654
response.event_id = endpointId;
6755
return pw::OkStatus();
6856
}
57+
58+
using EventsSubscriber = void (*)(EndpointId endpointId, ClusterId clusterId, std::string events);
59+
60+
void RegisterEventsSubscriber(EventsSubscriber subscriber) { mEventsSubscriber = subscriber; }
61+
62+
private:
63+
EventsSubscriber mEventsSubscriber;
64+
6965
};
7066

7167
} // namespace rpc

0 commit comments

Comments
 (0)