forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoccupancy-sensor-server.cpp
261 lines (207 loc) · 8.51 KB
/
occupancy-sensor-server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
*
* Copyright (c) 2020-2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "occupancy-sensor-server.h"
#include "occupancy-hal.h"
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/EventLogging.h>
#include <app/data-model/Encode.h>
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
#include <lib/core/CHIPError.h>
using chip::Protocols::InteractionModel::Status;
namespace chip {
namespace app {
namespace Clusters {
namespace OccupancySensing {
namespace {
Structs::HoldTimeLimitsStruct::Type
sHoldTimeLimitsStructs[MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT];
uint16_t sHoldTime[MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT];
} // namespace
CHIP_ERROR Instance::Init()
{
VerifyOrReturnError(registerAttributeAccessOverride(this), CHIP_ERROR_INCORRECT_STATE);
return CHIP_NO_ERROR;
}
void Instance::Shutdown()
{
unregisterAttributeAccessOverride(this);
}
CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
VerifyOrDie(aPath.mClusterId == app::Clusters::OccupancySensing::Id);
switch (aPath.mAttributeId)
{
case Attributes::FeatureMap::Id:
ReturnErrorOnFailure(aEncoder.Encode(mFeature));
break;
case Attributes::HoldTime::Id: {
uint16_t * holdTime = GetHoldTimeForEndpoint(aPath.mEndpointId);
if (holdTime == nullptr)
{
return CHIP_ERROR_NOT_FOUND;
}
return aEncoder.Encode(*holdTime);
}
case Attributes::HoldTimeLimits::Id: {
Structs::HoldTimeLimitsStruct::Type * holdTimeLimitsStruct = GetHoldTimeLimitsForEndpoint(aPath.mEndpointId);
if (holdTimeLimitsStruct == nullptr)
{
return CHIP_ERROR_NOT_FOUND;
}
return aEncoder.Encode(*holdTimeLimitsStruct);
}
default:
return CHIP_NO_ERROR;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR Instance::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
{
VerifyOrDie(aPath.mClusterId == app::Clusters::OccupancySensing::Id);
switch (aPath.mAttributeId)
{
case Attributes::HoldTime::Id: {
uint16_t newHoldTime;
ReturnErrorOnFailure(aDecoder.Decode(newHoldTime));
Structs::HoldTimeLimitsStruct::Type * currHoldTimeLimits = GetHoldTimeLimitsForEndpoint(aPath.mEndpointId);
VerifyOrReturnError(currHoldTimeLimits != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(newHoldTime >= currHoldTimeLimits->holdTimeMin, CHIP_IM_GLOBAL_STATUS(ConstraintError));
VerifyOrReturnError(newHoldTime <= currHoldTimeLimits->holdTimeMax, CHIP_IM_GLOBAL_STATUS(ConstraintError));
return SetHoldTime(aPath.mEndpointId, newHoldTime);
}
default: {
break;
}
}
return CHIP_NO_ERROR;
}
bool Instance::HasFeature(Feature aFeature) const
{
return mFeature.Has(aFeature);
}
Structs::HoldTimeLimitsStruct::Type * GetHoldTimeLimitsForEndpoint(EndpointId endpoint)
{
auto index = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::OccupancySensing::Id,
MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT);
if (index == kEmberInvalidEndpointIndex)
{
return nullptr;
}
if (index >= ArraySize(sHoldTimeLimitsStructs))
{
ChipLogError(NotSpecified, "Internal error: invalid/unexpected hold time limits index.");
return nullptr;
}
return &sHoldTimeLimitsStructs[index];
}
CHIP_ERROR SetHoldTimeLimits(EndpointId endpointId, const Structs::HoldTimeLimitsStruct::Type & holdTimeLimits)
{
VerifyOrReturnError(kInvalidEndpointId != endpointId, CHIP_ERROR_INVALID_ARGUMENT);
Structs::HoldTimeLimitsStruct::Type * holdTimeLimitsForEndpoint = GetHoldTimeLimitsForEndpoint(endpointId);
VerifyOrReturnError(holdTimeLimitsForEndpoint != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
holdTimeLimitsForEndpoint->holdTimeMin = holdTimeLimits.holdTimeMin;
holdTimeLimitsForEndpoint->holdTimeMax = holdTimeLimits.holdTimeMax;
holdTimeLimitsForEndpoint->holdTimeDefault = holdTimeLimits.holdTimeDefault;
MatterReportingAttributeChangeCallback(endpointId, OccupancySensing::Id, Attributes::HoldTimeLimits::Id);
return CHIP_NO_ERROR;
}
uint16_t * GetHoldTimeForEndpoint(EndpointId endpoint)
{
auto index = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::OccupancySensing::Id,
MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT);
if (index == kEmberInvalidEndpointIndex)
{
return nullptr;
}
if (index >= ArraySize(sHoldTimeLimitsStructs))
{
ChipLogError(NotSpecified, "Internal error: invalid/unexpected hold time index.");
return nullptr;
}
return &sHoldTime[index];
}
CHIP_ERROR SetHoldTime(EndpointId endpointId, const uint16_t & holdTime)
{
VerifyOrReturnError(kInvalidEndpointId != endpointId, CHIP_ERROR_INVALID_ARGUMENT);
uint16_t * holdTimeForEndpoint = GetHoldTimeForEndpoint(endpointId);
VerifyOrReturnError(holdTimeForEndpoint != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
*holdTimeForEndpoint = holdTime;
MatterReportingAttributeChangeCallback(endpointId, OccupancySensing::Id, Attributes::HoldTime::Id);
return CHIP_NO_ERROR;
}
} // namespace OccupancySensing
} // namespace Clusters
} // namespace app
} // namespace chip
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OccupancySensing;
//******************************************************************************
// Plugin init function
//******************************************************************************
void emberAfOccupancySensingClusterServerInitCallback(EndpointId endpoint)
{
auto deviceType = halOccupancyGetSensorType(endpoint);
chip::BitMask<OccupancySensorTypeBitmap> deviceTypeBitmap = 0;
switch (deviceType)
{
case HAL_OCCUPANCY_SENSOR_TYPE_PIR:
deviceTypeBitmap.Set(OccupancySensorTypeBitmap::kPir);
Attributes::OccupancySensorType::Set(endpoint, OccupancySensorTypeEnum::kPir);
break;
case HAL_OCCUPANCY_SENSOR_TYPE_ULTRASONIC:
deviceTypeBitmap.Set(OccupancySensorTypeBitmap::kUltrasonic);
Attributes::OccupancySensorType::Set(endpoint, OccupancySensorTypeEnum::kUltrasonic);
break;
case HAL_OCCUPANCY_SENSOR_TYPE_PIR_AND_ULTRASONIC:
deviceTypeBitmap.Set(OccupancySensorTypeBitmap::kPir);
deviceTypeBitmap.Set(OccupancySensorTypeBitmap::kUltrasonic);
Attributes::OccupancySensorType::Set(endpoint, OccupancySensorTypeEnum::kPIRAndUltrasonic);
break;
case HAL_OCCUPANCY_SENSOR_TYPE_PHYSICAL:
deviceTypeBitmap.Set(OccupancySensorTypeBitmap::kPhysicalContact);
Attributes::OccupancySensorType::Set(endpoint, OccupancySensorTypeEnum::kPhysicalContact);
break;
default:
break;
}
Attributes::OccupancySensorTypeBitmap::Set(endpoint, deviceTypeBitmap);
}
//******************************************************************************
// Notification callback from the HAL plugin
//******************************************************************************
void halOccupancyStateChangedCallback(EndpointId endpoint, HalOccupancyState occupancyState)
{
chip::BitMask<OccupancyBitmap> mappedOccupancyState;
if (occupancyState & HAL_OCCUPANCY_STATE_OCCUPIED)
{
mappedOccupancyState.Set(OccupancyBitmap::kOccupied);
ChipLogProgress(Zcl, "Occupancy detected");
}
else
{
ChipLogProgress(Zcl, "Occupancy no longer detected");
}
Attributes::Occupancy::Set(endpoint, occupancyState);
}
HalOccupancySensorType __attribute__((weak)) halOccupancyGetSensorType(EndpointId endpoint)
{
return HAL_OCCUPANCY_SENSOR_TYPE_PIR;
}
void MatterOccupancySensingPluginServerInitCallback() {}