forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockManager.h
313 lines (264 loc) · 11.9 KB
/
LockManager.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
*
* Copyright (c) 2019 Google LLC.
* All rights reserved.
*
* 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.
*/
#pragma once
#include <app/clusters/door-lock-server/door-lock-server.h>
#include <stdbool.h>
#include <stdint.h>
#include "AppEvent.h"
#include <cmsis_os2.h>
#include <lib/core/CHIPError.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
namespace EFR32DoorLock {
namespace ResourceRanges {
// Used to size arrays
static constexpr uint16_t kMaxUsers = 10;
static constexpr uint8_t kMaxCredentialsPerUser = 10;
static constexpr uint8_t kMaxWeekdaySchedulesPerUser = 10;
static constexpr uint8_t kMaxYeardaySchedulesPerUser = 10;
static constexpr uint8_t kMaxHolidaySchedules = 10;
static constexpr uint8_t kMaxCredentialSize = 20;
static constexpr uint8_t kNumCredentialTypes = 6;
} // namespace ResourceRanges
namespace LockInitParams {
struct LockParam
{
// Read from zap attributes
uint16_t numberOfUsers = 0;
uint8_t numberOfCredentialsPerUser = 0;
uint8_t numberOfWeekdaySchedulesPerUser = 0;
uint8_t numberOfYeardaySchedulesPerUser = 0;
uint8_t numberOfHolidaySchedules = 0;
};
class ParamBuilder
{
public:
ParamBuilder & SetNumberOfUsers(uint16_t numberOfUsers)
{
lockParam_.numberOfUsers = numberOfUsers;
return *this;
}
ParamBuilder & SetNumberOfCredentialsPerUser(uint8_t numberOfCredentialsPerUser)
{
lockParam_.numberOfCredentialsPerUser = numberOfCredentialsPerUser;
return *this;
}
ParamBuilder & SetNumberOfWeekdaySchedulesPerUser(uint8_t numberOfWeekdaySchedulesPerUser)
{
lockParam_.numberOfWeekdaySchedulesPerUser = numberOfWeekdaySchedulesPerUser;
return *this;
}
ParamBuilder & SetNumberOfYeardaySchedulesPerUser(uint8_t numberOfYeardaySchedulesPerUser)
{
lockParam_.numberOfYeardaySchedulesPerUser = numberOfYeardaySchedulesPerUser;
return *this;
}
ParamBuilder & SetNumberOfHolidaySchedules(uint8_t numberOfHolidaySchedules)
{
lockParam_.numberOfHolidaySchedules = numberOfHolidaySchedules;
return *this;
}
LockParam GetLockParam() { return lockParam_; }
private:
LockParam lockParam_;
};
} // namespace LockInitParams
namespace Storage {
using namespace EFR32DoorLock::ResourceRanges;
struct WeekDayScheduleInfo
{
DlScheduleStatus status;
EmberAfPluginDoorLockWeekDaySchedule schedule;
};
struct YearDayScheduleInfo
{
DlScheduleStatus status;
EmberAfPluginDoorLockYearDaySchedule schedule;
};
struct HolidayScheduleInfo
{
DlScheduleStatus status;
EmberAfPluginDoorLockHolidaySchedule schedule;
};
struct LockUserInfo
{
char userName[DOOR_LOCK_MAX_USER_NAME_SIZE];
size_t userNameSize;
uint32_t userUniqueId;
UserStatusEnum userStatus;
UserTypeEnum userType;
CredentialRuleEnum credentialRule;
chip::EndpointId endpointId;
chip::FabricIndex createdBy;
chip::FabricIndex lastModifiedBy;
uint16_t currentCredentialCount;
};
struct LockCredentialInfo
{
DlCredentialStatus status;
CredentialTypeEnum credentialType;
chip::FabricIndex createdBy;
chip::FabricIndex lastModifiedBy;
uint8_t credentialData[kMaxCredentialSize];
size_t credentialDataSize;
};
} // namespace Storage
} // namespace EFR32DoorLock
using namespace ::chip;
using namespace EFR32DoorLock::ResourceRanges;
using namespace EFR32DoorLock::Storage;
class LockManager
{
public:
enum Action_t
{
LOCK_ACTION = 0,
UNLOCK_ACTION,
UNLATCH_ACTION,
INVALID_ACTION
} Action;
enum State_t
{
kState_LockInitiated = 0,
kState_LockCompleted,
kState_UnlockInitiated,
kState_UnlatchInitiated,
kState_UnlockCompleted,
kState_UnlatchCompleted,
} State;
CHIP_ERROR Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state,
EFR32DoorLock::LockInitParams::LockParam lockParam, PersistentStorageDelegate * storage);
bool NextState();
bool IsActionInProgress();
bool InitiateAction(int32_t aActor, Action_t aAction);
typedef void (*Callback_fn_initiated)(Action_t, int32_t aActor);
typedef void (*Callback_fn_completed)(Action_t);
void SetCallbacks(Callback_fn_initiated aActionInitiated_CB, Callback_fn_completed aActionCompleted_CB);
bool Lock(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx, const Nullable<chip::NodeId> & nodeId,
const Optional<chip::ByteSpan> & pin, OperationErrorEnum & err);
bool Unlock(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx, const Nullable<chip::NodeId> & nodeId,
const Optional<chip::ByteSpan> & pin, OperationErrorEnum & err);
bool Unbolt(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx, const Nullable<chip::NodeId> & nodeId,
const Optional<chip::ByteSpan> & pin, OperationErrorEnum & err);
bool GetUser(chip::EndpointId endpointId, uint16_t userIndex, EmberAfPluginDoorLockUserInfo & user);
bool SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip::FabricIndex creator, chip::FabricIndex modifier,
const chip::CharSpan & userName, uint32_t uniqueId, UserStatusEnum userStatus, UserTypeEnum usertype,
CredentialRuleEnum credentialRule, const CredentialStruct * credentials, size_t totalCredentials);
bool GetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, CredentialTypeEnum credentialType,
EmberAfPluginDoorLockCredentialInfo & credential);
bool SetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, chip::FabricIndex creator, chip::FabricIndex modifier,
DlCredentialStatus credentialStatus, CredentialTypeEnum credentialType,
const chip::ByteSpan & credentialData);
DlStatus GetWeekdaySchedule(chip::EndpointId endpointId, uint8_t weekdayIndex, uint16_t userIndex,
EmberAfPluginDoorLockWeekDaySchedule & schedule);
DlStatus SetWeekdaySchedule(chip::EndpointId endpointId, uint8_t weekdayIndex, uint16_t userIndex, DlScheduleStatus status,
DaysMaskMap daysMask, uint8_t startHour, uint8_t startMinute, uint8_t endHour, uint8_t endMinute);
DlStatus GetYeardaySchedule(chip::EndpointId endpointId, uint8_t yearDayIndex, uint16_t userIndex,
EmberAfPluginDoorLockYearDaySchedule & schedule);
DlStatus SetYeardaySchedule(chip::EndpointId endpointId, uint8_t yearDayIndex, uint16_t userIndex, DlScheduleStatus status,
uint32_t localStartTime, uint32_t localEndTime);
DlStatus GetHolidaySchedule(chip::EndpointId endpointId, uint8_t holidayIndex, EmberAfPluginDoorLockHolidaySchedule & schedule);
DlStatus SetHolidaySchedule(chip::EndpointId endpointId, uint8_t holidayIndex, DlScheduleStatus status, uint32_t localStartTime,
uint32_t localEndTime, OperatingModeEnum operatingMode);
bool IsValidUserIndex(uint16_t userIndex);
bool IsValidCredentialIndex(uint16_t credentialIndex, CredentialTypeEnum type);
bool IsValidCredentialType(CredentialTypeEnum type);
bool IsValidWeekdayScheduleIndex(uint8_t scheduleIndex);
bool IsValidYeardayScheduleIndex(uint8_t scheduleIndex);
bool IsValidHolidayScheduleIndex(uint8_t scheduleIndex);
bool setLockState(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx,
const Nullable<chip::NodeId> & nodeId, DlLockState lockState, const Optional<chip::ByteSpan> & pin,
OperationErrorEnum & err);
const char * lockStateToString(DlLockState lockState) const;
void UnlockAfterUnlatch();
private:
struct UnlatchContext
{
static constexpr uint8_t kMaxPinLength = UINT8_MAX;
uint8_t mPinBuffer[kMaxPinLength];
uint8_t mPinLength;
chip::EndpointId mEndpointId;
Nullable<chip::FabricIndex> mFabricIdx;
Nullable<chip::NodeId> mNodeId;
OperationErrorEnum mErr;
void Update(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx,
const Nullable<chip::NodeId> & nodeId, const Optional<chip::ByteSpan> & pin, OperationErrorEnum & err)
{
mEndpointId = endpointId;
mFabricIdx = fabricIdx;
mNodeId = nodeId;
mErr = err;
if (pin.HasValue())
{
memcpy(mPinBuffer, pin.Value().data(), pin.Value().size());
mPinLength = static_cast<uint8_t>(pin.Value().size());
}
else
{
memset(mPinBuffer, 0, kMaxPinLength);
mPinLength = 0;
}
}
};
UnlatchContext mUnlatchContext;
chip::EndpointId mCurrentEndpointId;
friend LockManager & LockMgr();
State_t mState;
Callback_fn_initiated mActionInitiated_CB;
Callback_fn_completed mActionCompleted_CB;
void CancelTimer(void);
void StartTimer(uint32_t aTimeoutMs);
static void TimerEventHandler(void * timerCbArg);
static void AutoLockTimerEventHandler(AppEvent * aEvent);
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);
osTimerId_t mLockTimer;
EFR32DoorLock::LockInitParams::LockParam LockParams;
// Stores LockUserInfo corresponding to a user index
static StorageKeyName LockUserEndpoint(uint16_t userIndex, chip::EndpointId endpoint)
{
return StorageKeyName::Formatted("g/lu/%x/e/%x", userIndex, endpoint);
}
// Stores LockCredentialInfo corresponding to a credential index and type
static StorageKeyName LockCredentialEndpoint(uint16_t credentialIndex, CredentialTypeEnum credentialType,
chip::EndpointId endpoint)
{
return StorageKeyName::Formatted("g/lc/%x/t/%x/e/%x", credentialIndex, static_cast<uint16_t>(credentialType), endpoint);
}
// Stores all the credential indices that belong to a user
static StorageKeyName LockUserCredentialMap(uint16_t userIndex) { return StorageKeyName::Formatted("g/lu/%x/lc", userIndex); }
// Stores WeekDayScheduleInfo corresponding to a user and schedule index
static StorageKeyName LockUserWeekDayScheduleEndpoint(uint16_t userIndex, uint16_t scheduleIndex, chip::EndpointId endpoint)
{
return StorageKeyName::Formatted("g/lu/%x/lw/%x/e/%x", userIndex, scheduleIndex, endpoint);
}
// Stores YearDayScheduleInfo corresponding to a user and schedule index
static StorageKeyName LockUserYearDayScheduleEndpoint(uint16_t userIndex, uint16_t scheduleIndex, chip::EndpointId endpoint)
{
return StorageKeyName::Formatted("g/lu/%x/ly/%x/e/%x", userIndex, scheduleIndex, endpoint);
}
// Stores HolidayScheduleInfo corresponding to a schedule index
static StorageKeyName LockHolidayScheduleEndpoint(uint16_t scheduleIndex, chip::EndpointId endpoint)
{
return StorageKeyName::Formatted("g/lh/%x/e/%x", scheduleIndex, endpoint);
}
// Pointer to the PeristentStorage
PersistentStorageDelegate * mStorage = nullptr;
LockUserInfo mUserInStorage;
LockCredentialInfo mCredentialInStorage;
CredentialStruct mCredentials[kMaxCredentialsPerUser];
};
LockManager & LockMgr();