Skip to content

Commit 69cb8cb

Browse files
committed
Refer to the latest lock-app codes
1 parent d39a7e5 commit 69cb8cb

File tree

3 files changed

+99
-100
lines changed

3 files changed

+99
-100
lines changed

examples/chef/common/clusters/door-lock/chef-doorlock-stubs.cpp

+40-72
Original file line numberDiff line numberDiff line change
@@ -50,66 +50,18 @@ bool emberAfPluginDoorLockOnDoorUnlockCommand(chip::EndpointId endpointId, const
5050
return LockManager::Instance().Unlock(endpointId, fabricIdx, nodeId, pinCode, err, OperationSourceEnum::kRemote);
5151
}
5252

53-
//UnlockWithTimeout
54-
55-
// =============================================================================
56-
// Users and credentials access callbacks
57-
// =============================================================================
58-
59-
// SetWeekDaySchedule
60-
DlStatus emberAfPluginDoorLockSetSchedule(chip::EndpointId endpointId, uint8_t weekdayIndex, uint16_t userIndex,
61-
DlScheduleStatus status, DaysMaskMap daysMask, uint8_t startHour, uint8_t startMinute,
62-
uint8_t endHour, uint8_t endMinute)
63-
{
64-
return LockManager::Instance().SetSchedule(endpointId, weekdayIndex, userIndex, status, daysMask, startHour, startMinute,
65-
endHour, endMinute);
66-
}
67-
68-
// GetWeekDaySchedule
69-
DlStatus emberAfPluginDoorLockGetSchedule(chip::EndpointId endpointId, uint8_t weekdayIndex, uint16_t userIndex,
70-
EmberAfPluginDoorLockWeekDaySchedule & schedule)
71-
{
72-
return LockManager::Instance().GetSchedule(endpointId, weekdayIndex, userIndex, schedule);
73-
}
74-
75-
// emberAfDoorLockClusterClearWeekDayScheduleCallback was handled in src/app/clusters/door-lock-server/door-lock-server.cpp
76-
// And finally call into emberAfPluginDoorLockSetSchedule
77-
78-
// SetYearDaySchedule
79-
DlStatus emberAfPluginDoorLockSetSchedule(chip::EndpointId endpointId, uint8_t yearDayIndex, uint16_t userIndex,
80-
DlScheduleStatus status, uint32_t localStartTime, uint32_t localEndTime)
81-
{
82-
return LockManager::Instance().SetSchedule(endpointId, yearDayIndex, userIndex, status, localStartTime, localEndTime);
83-
}
84-
85-
// GetYearDaySchedule
86-
DlStatus emberAfPluginDoorLockGetSchedule(chip::EndpointId endpointId, uint8_t yearDayIndex, uint16_t userIndex,
87-
EmberAfPluginDoorLockYearDaySchedule & schedule)
88-
{
89-
return LockManager::Instance().GetSchedule(endpointId, yearDayIndex, userIndex, schedule);
90-
}
91-
92-
// emberAfDoorLockClusterClearYearDayScheduleCallback was handled in src/app/clusters/door-lock-server/door-lock-server.cpp
93-
// And finally call into emberAfPluginDoorLockSetSchedule
94-
95-
// SetHolidaySchedule
96-
DlStatus emberAfPluginDoorLockSetSchedule(chip::EndpointId endpointId, uint8_t holidayIndex, DlScheduleStatus status,
97-
uint32_t localStartTime, uint32_t localEndTime, OperatingModeEnum operatingMode)
53+
bool emberAfPluginDoorLockOnDoorUnboltCommand(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx,
54+
const Nullable<chip::NodeId> & nodeId, const Optional<ByteSpan> & pinCode,
55+
OperationErrorEnum & err)
9856
{
99-
return LockManager::Instance().SetSchedule(endpointId, holidayIndex, status, localStartTime, localEndTime, operatingMode);
57+
return LockManager::Instance().Unbolt(endpointId, fabricIdx, nodeId, pinCode, err, OperationSourceEnum::kRemote);
10058
}
10159

102-
// GetHolidaySchedule
103-
DlStatus emberAfPluginDoorLockGetSchedule(chip::EndpointId endpointId, uint8_t holidayIndex,
104-
EmberAfPluginDoorLockHolidaySchedule & schedule)
60+
bool emberAfPluginDoorLockGetUser(chip::EndpointId endpointId, uint16_t userIndex, EmberAfPluginDoorLockUserInfo & user)
10561
{
106-
return LockManager::Instance().GetSchedule(endpointId, holidayIndex, schedule);
62+
return LockManager::Instance().GetUser(endpointId, userIndex, user);
10763
}
10864

109-
// emberAfDoorLockClusterClearHolidayScheduleCallback was handled in src/app/clusters/door-lock-server/door-lock-server.cpp
110-
// And finally call into emberAfPluginDoorLockSetSchedule
111-
112-
// SetUser
11365
bool emberAfPluginDoorLockSetUser(chip::EndpointId endpointId, uint16_t userIndex, chip::FabricIndex creator,
11466
chip::FabricIndex modifier, const chip::CharSpan & userName, uint32_t uniqueId,
11567
UserStatusEnum userStatus, UserTypeEnum usertype, CredentialRuleEnum credentialRule,
@@ -120,16 +72,12 @@ bool emberAfPluginDoorLockSetUser(chip::EndpointId endpointId, uint16_t userInde
12072
credentialRule, credentials, totalCredentials);
12173
}
12274

123-
// GetUser
124-
bool emberAfPluginDoorLockGetUser(chip::EndpointId endpointId, uint16_t userIndex, EmberAfPluginDoorLockUserInfo & user)
75+
bool emberAfPluginDoorLockGetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, CredentialTypeEnum credentialType,
76+
EmberAfPluginDoorLockCredentialInfo & credential)
12577
{
126-
return LockManager::Instance().GetUser(endpointId, userIndex, user);
78+
return LockManager::Instance().GetCredential(endpointId, credentialIndex, credentialType, credential);
12779
}
12880

129-
// emberAfDoorLockClusterClearUserCallback was handled in src/app/clusters/door-lock-server/door-lock-server.cpp
130-
// And finally call into emberAfPluginDoorLockSetCredential and emberAfPluginDoorLockSetUser
131-
132-
// SetCredential
13381
bool emberAfPluginDoorLockSetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, chip::FabricIndex creator,
13482
chip::FabricIndex modifier, DlCredentialStatus credentialStatus,
13583
CredentialTypeEnum credentialType, const chip::ByteSpan & credentialData)
@@ -138,22 +86,42 @@ bool emberAfPluginDoorLockSetCredential(chip::EndpointId endpointId, uint16_t cr
13886
credentialData);
13987
}
14088

141-
// GetCredential
142-
bool emberAfPluginDoorLockGetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, CredentialTypeEnum credentialType,
143-
EmberAfPluginDoorLockCredentialInfo & credential)
89+
DlStatus emberAfPluginDoorLockGetSchedule(chip::EndpointId endpointId, uint8_t weekdayIndex, uint16_t userIndex,
90+
EmberAfPluginDoorLockWeekDaySchedule & schedule)
14491
{
145-
return LockManager::Instance().GetCredential(endpointId, credentialIndex, credentialType, credential);
92+
return LockManager::Instance().GetSchedule(endpointId, weekdayIndex, userIndex, schedule);
14693
}
14794

148-
// emberAfDoorLockClusterClearCredentialCallback was handled in src/app/clusters/door-lock-server/door-lock-server.cpp
149-
// And finally call into emberAfPluginDoorLockSetCredential and emberAfPluginDoorLockSetUser
95+
DlStatus emberAfPluginDoorLockGetSchedule(chip::EndpointId endpointId, uint8_t holidayIndex,
96+
EmberAfPluginDoorLockHolidaySchedule & schedule)
97+
{
98+
return LockManager::Instance().GetSchedule(endpointId, holidayIndex, schedule);
99+
}
150100

151-
// UnboltDoor
152-
bool emberAfPluginDoorLockOnDoorUnboltCommand(chip::EndpointId endpointId, const Nullable<chip::FabricIndex> & fabricIdx,
153-
const Nullable<chip::NodeId> & nodeId, const Optional<ByteSpan> & pinCode,
154-
OperationErrorEnum & err)
101+
DlStatus emberAfPluginDoorLockSetSchedule(chip::EndpointId endpointId, uint8_t weekdayIndex, uint16_t userIndex,
102+
DlScheduleStatus status, DaysMaskMap daysMask, uint8_t startHour, uint8_t startMinute,
103+
uint8_t endHour, uint8_t endMinute)
155104
{
156-
return LockManager::Instance().Unbolt(endpointId, fabricIdx, nodeId, pinCode, err, OperationSourceEnum::kRemote);
105+
return LockManager::Instance().SetSchedule(endpointId, weekdayIndex, userIndex, status, daysMask, startHour, startMinute,
106+
endHour, endMinute);
107+
}
108+
109+
DlStatus emberAfPluginDoorLockSetSchedule(chip::EndpointId endpointId, uint8_t yearDayIndex, uint16_t userIndex,
110+
DlScheduleStatus status, uint32_t localStartTime, uint32_t localEndTime)
111+
{
112+
return LockManager::Instance().SetSchedule(endpointId, yearDayIndex, userIndex, status, localStartTime, localEndTime);
113+
}
114+
115+
DlStatus emberAfPluginDoorLockGetSchedule(chip::EndpointId endpointId, uint8_t yearDayIndex, uint16_t userIndex,
116+
EmberAfPluginDoorLockYearDaySchedule & schedule)
117+
{
118+
return LockManager::Instance().GetSchedule(endpointId, yearDayIndex, userIndex, schedule);
119+
}
120+
121+
DlStatus emberAfPluginDoorLockSetSchedule(chip::EndpointId endpointId, uint8_t holidayIndex, DlScheduleStatus status,
122+
uint32_t localStartTime, uint32_t localEndTime, OperatingModeEnum operatingMode)
123+
{
124+
return LockManager::Instance().SetSchedule(endpointId, holidayIndex, status, localStartTime, localEndTime, operatingMode);
157125
}
158126

159127
void emberAfDoorLockClusterInitCallback(EndpointId endpoint)

examples/chef/common/clusters/door-lock/chef-lock-endpoint.cpp

+49-26
Original file line numberDiff line numberDiff line change
@@ -509,19 +509,20 @@ printf("\n \033[41m %s, %d \033[0m \n", __func__, __LINE__);
509509
auto userIndex = static_cast<uint8_t>(user - mLockUsers.begin());
510510

511511
// Check if schedules affect the user
512-
bool ret = weekDayScheduleInAction(userIndex);
513-
printf("\033[44m %s, %d, ret=%d \033[0m \n", __func__, __LINE__, static_cast<int>(ret));
514-
if ( ((user->userType == UserTypeEnum::kScheduleRestrictedUser || user->userType == UserTypeEnum::kWeekDayScheduleUser) &&
515-
!weekDayScheduleInAction(userIndex)) ||
516-
((user->userType == UserTypeEnum::kScheduleRestrictedUser || user->userType == UserTypeEnum::kYearDayScheduleUser) &&
517-
!yearDayScheduleInAction(userIndex)) )
518-
{
519-
ChipLogDetail(Zcl,
520-
"Lock App: associated user is not allowed to operate the lock due to schedules"
521-
"[endpointId=%d,userIndex=%u]",
522-
mEndpointId, userIndex);
523-
err = OperationErrorEnum::kRestricted;
524-
return false;
512+
bool haveWeekDaySchedules = false;
513+
bool haveYearDaySchedules = false;
514+
if (weekDayScheduleForbidsAccess(userIndex, &haveWeekDaySchedules) ||
515+
yearDayScheduleForbidsAccess(userIndex, &haveYearDaySchedules) ||
516+
// Also disallow access for a user that's supposed to have _some_
517+
// schedule but doesn't have any
518+
(user->userType == UserTypeEnum::kScheduleRestrictedUser && !haveWeekDaySchedules && !haveYearDaySchedules))
519+
{
520+
ChipLogDetail(Zcl,
521+
"Lock App: associated user is not allowed to operate the lock due to schedules"
522+
"[endpointId=%d,userIndex=%u]",
523+
mEndpointId, userIndex);
524+
err = OperationErrorEnum::kRestricted;
525+
return false;
525526
}
526527
ChipLogProgress(
527528
Zcl,
@@ -582,12 +583,23 @@ void LockEndpoint::OnLockActionCompleteCallback(chip::System::Layer *, void * ca
582583
}
583584
}
584585

585-
bool LockEndpoint::weekDayScheduleInAction(uint16_t userIndex) const
586+
bool LockEndpoint::weekDayScheduleForbidsAccess(uint16_t userIndex, bool * haveSchedule) const
586587
{
588+
*haveSchedule = std::any_of(mWeekDaySchedules[userIndex].begin(), mWeekDaySchedules[userIndex].end(),
589+
[](const WeekDaysScheduleInfo & s) { return s.status == DlScheduleStatus::kOccupied; });
590+
587591
const auto & user = mLockUsers[userIndex];
588592
if (user.userType != UserTypeEnum::kScheduleRestrictedUser && user.userType != UserTypeEnum::kWeekDayScheduleUser)
589593
{
590-
return true;
594+
// Weekday schedules don't apply to this user.
595+
return false;
596+
}
597+
598+
if (user.userType == UserTypeEnum::kScheduleRestrictedUser && !*haveSchedule)
599+
{
600+
// It's valid to not have any schedules of a given type; on its own this
601+
// does not prevent access.
602+
return false;
591603
}
592604

593605
chip::System::Clock::Milliseconds64 cTMs;
@@ -596,7 +608,7 @@ bool LockEndpoint::weekDayScheduleInAction(uint16_t userIndex) const
596608
{
597609
ChipLogError(Zcl, "Lock App: unable to get current time to check user schedules [endpointId=%d,error=%d (%s)]", mEndpointId,
598610
chipError.AsInteger(), chipError.AsString());
599-
return false;
611+
return true;
600612
}
601613
time_t unixEpoch = std::chrono::duration_cast<chip::System::Clock::Seconds32>(cTMs).count();
602614

@@ -606,8 +618,9 @@ bool LockEndpoint::weekDayScheduleInAction(uint16_t userIndex) const
606618
auto currentTime =
607619
calendarTime.tm_hour * chip::kSecondsPerHour + calendarTime.tm_min * chip::kSecondsPerMinute + calendarTime.tm_sec;
608620

609-
// Second, check the week day schedules.
610-
return std::any_of(
621+
// Now check whether any schedule allows the current time. If it does,
622+
// access is not forbidden.
623+
return !std::any_of(
611624
mWeekDaySchedules[userIndex].begin(), mWeekDaySchedules[userIndex].end(),
612625
[currentTime, calendarTime](const WeekDaysScheduleInfo & s) {
613626
auto startTime = s.schedule.startHour * chip::kSecondsPerHour + s.schedule.startMinute * chip::kSecondsPerMinute;
@@ -620,12 +633,22 @@ bool LockEndpoint::weekDayScheduleInAction(uint16_t userIndex) const
620633
});
621634
}
622635

623-
bool LockEndpoint::yearDayScheduleInAction(uint16_t userIndex) const
636+
bool LockEndpoint::yearDayScheduleForbidsAccess(uint16_t userIndex, bool * haveSchedule) const
624637
{
638+
*haveSchedule = std::any_of(mYearDaySchedules[userIndex].begin(), mYearDaySchedules[userIndex].end(),
639+
[](const YearDayScheduleInfo & sch) { return sch.status == DlScheduleStatus::kOccupied; });
640+
625641
const auto & user = mLockUsers[userIndex];
626642
if (user.userType != UserTypeEnum::kScheduleRestrictedUser && user.userType != UserTypeEnum::kYearDayScheduleUser)
627643
{
628-
return true;
644+
return false;
645+
}
646+
647+
if (user.userType == UserTypeEnum::kScheduleRestrictedUser && !*haveSchedule)
648+
{
649+
// It's valid to not have any schedules of a given type; on its own this
650+
// does not prevent access.
651+
return false;
629652
}
630653

631654
chip::System::Clock::Milliseconds64 cTMs;
@@ -634,7 +657,7 @@ bool LockEndpoint::yearDayScheduleInAction(uint16_t userIndex) const
634657
{
635658
ChipLogError(Zcl, "Lock App: unable to get current time to check user schedules [endpointId=%d,error=%d (%s)]", mEndpointId,
636659
chipError.AsInteger(), chipError.AsString());
637-
return false;
660+
return true;
638661
}
639662
auto unixEpoch = std::chrono::duration_cast<chip::System::Clock::Seconds32>(cTMs).count();
640663
uint32_t chipEpoch = 0;
@@ -647,11 +670,11 @@ bool LockEndpoint::yearDayScheduleInAction(uint16_t userIndex) const
647670
return false;
648671
}
649672

650-
return std::any_of(mYearDaySchedules[userIndex].begin(), mYearDaySchedules[userIndex].end(),
651-
[chipEpoch](const YearDayScheduleInfo & sch) {
652-
return sch.status == DlScheduleStatus::kOccupied && sch.schedule.localStartTime <= chipEpoch &&
653-
chipEpoch <= sch.schedule.localEndTime;
654-
});
673+
return !std::any_of(mYearDaySchedules[userIndex].begin(), mYearDaySchedules[userIndex].end(),
674+
[chipEpoch](const YearDayScheduleInfo & sch) {
675+
return sch.status == DlScheduleStatus::kOccupied && sch.schedule.localStartTime <= chipEpoch &&
676+
chipEpoch <= sch.schedule.localEndTime;
677+
});
655678
}
656679

657680
const char * LockEndpoint::lockStateToString(DlLockState lockState) const

examples/chef/common/clusters/door-lock/chef-lock-endpoint.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,16 @@ class LockEndpoint
106106
OperationSourceEnum opSource = OperationSourceEnum::kUnspecified);
107107
const char * lockStateToString(DlLockState lockState) const;
108108

109-
bool weekDayScheduleInAction(uint16_t userIndex) const;
110-
bool yearDayScheduleInAction(uint16_t userIndex) const;
109+
// Returns true if week day schedules should apply to the user, there are
110+
// schedules defined for the user, and access is not currently allowed by
111+
// those schedules. The outparam indicates whether there were in fact any
112+
// year day schedules defined for the user.
113+
bool weekDayScheduleForbidsAccess(uint16_t userIndex, bool * haveSchedule) const;
114+
// Returns true if year day schedules should apply to the user, there are
115+
// schedules defined for the user, and access is not currently allowed by
116+
// those schedules. The outparam indicates whether there were in fact any
117+
// year day schedules defined for the user.
118+
bool yearDayScheduleForbidsAccess(uint16_t userIndex, bool * haveSchedule) const;
111119

112120
static void OnLockActionCompleteCallback(chip::System::Layer *, void * callbackContext);
113121

0 commit comments

Comments
 (0)