|
20 | 20 | #include "BoltLockManager.h"
|
21 | 21 |
|
22 | 22 | #include "AppConfig.h"
|
| 23 | +#include "AppEvent.h" |
23 | 24 | #include "AppTask.h"
|
24 | 25 |
|
25 |
| -#include <logging/log.h> |
26 |
| -#include <zephyr.h> |
27 |
| - |
28 |
| -LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL); |
29 |
| - |
30 |
| -static k_timer sLockTimer; |
31 |
| - |
32 | 26 | BoltLockManager BoltLockManager::sLock;
|
33 | 27 |
|
34 |
| -void BoltLockManager::Init() |
| 28 | +void BoltLockManager::Init(StateChangeCallback callback) |
35 | 29 | {
|
36 |
| - k_timer_init(&sLockTimer, &BoltLockManager::TimerEventHandler, nullptr); |
37 |
| - k_timer_user_data_set(&sLockTimer, this); |
| 30 | + mStateChangeCallback = callback; |
38 | 31 |
|
39 |
| - mState = kState_LockingCompleted; |
40 |
| - mAutoLockTimerArmed = false; |
41 |
| - mAutoRelock = false; |
42 |
| - mAutoLockDuration = 0; |
43 |
| -} |
44 |
| - |
45 |
| -void BoltLockManager::SetCallbacks(Callback_fn_initiated aActionInitiated_CB, Callback_fn_completed aActionCompleted_CB) |
46 |
| -{ |
47 |
| - mActionInitiated_CB = aActionInitiated_CB; |
48 |
| - mActionCompleted_CB = aActionCompleted_CB; |
| 32 | + k_timer_init(&mActuatorTimer, &BoltLockManager::ActuatorTimerEventHandler, nullptr); |
| 33 | + k_timer_user_data_set(&mActuatorTimer, this); |
49 | 34 | }
|
50 | 35 |
|
51 |
| -bool BoltLockManager::IsActionInProgress() |
| 36 | +void BoltLockManager::Lock(OperationSource source) |
52 | 37 | {
|
53 |
| - return (mState == kState_LockingInitiated || mState == kState_UnlockingInitiated) ? true : false; |
54 |
| -} |
| 38 | + VerifyOrReturn(mState != State::kLockingCompleted); |
| 39 | + SetState(State::kLockingInitiated, source); |
55 | 40 |
|
56 |
| -bool BoltLockManager::IsUnlocked() |
57 |
| -{ |
58 |
| - return (mState == kState_UnlockingCompleted) ? true : false; |
| 41 | + mActuatorOperationSource = source; |
| 42 | + k_timer_start(&mActuatorTimer, K_MSEC(kActuatorMovementTimeMs), K_NO_WAIT); |
59 | 43 | }
|
60 | 44 |
|
61 |
| -void BoltLockManager::EnableAutoRelock(bool aOn) |
| 45 | +void BoltLockManager::Unlock(OperationSource source) |
62 | 46 | {
|
63 |
| - mAutoRelock = aOn; |
64 |
| -} |
| 47 | + VerifyOrReturn(mState != State::kUnlockingCompleted); |
| 48 | + SetState(State::kUnlockingInitiated, source); |
65 | 49 |
|
66 |
| -void BoltLockManager::SetAutoLockDuration(uint32_t aDurationInSecs) |
67 |
| -{ |
68 |
| - mAutoLockDuration = aDurationInSecs; |
| 50 | + mActuatorOperationSource = source; |
| 51 | + k_timer_start(&mActuatorTimer, K_MSEC(kActuatorMovementTimeMs), K_NO_WAIT); |
69 | 52 | }
|
70 | 53 |
|
71 |
| -bool BoltLockManager::InitiateAction(int32_t aActor, Action_t aAction) |
| 54 | +void BoltLockManager::ActuatorTimerEventHandler(k_timer * timer) |
72 | 55 | {
|
73 |
| - bool action_initiated = false; |
74 |
| - State_t new_state; |
75 |
| - |
76 |
| - // Initiate Lock/Unlock Action only when the previous one is complete. |
77 |
| - if (mState == kState_LockingCompleted && aAction == UNLOCK_ACTION) |
78 |
| - { |
79 |
| - action_initiated = true; |
80 |
| - mCurrentActor = aActor; |
81 |
| - new_state = kState_UnlockingInitiated; |
82 |
| - } |
83 |
| - else if (mState == kState_UnlockingCompleted && aAction == LOCK_ACTION) |
84 |
| - { |
85 |
| - action_initiated = true; |
86 |
| - mCurrentActor = aActor; |
87 |
| - new_state = kState_LockingInitiated; |
88 |
| - } |
89 |
| - |
90 |
| - if (action_initiated) |
91 |
| - { |
92 |
| - if (mAutoLockTimerArmed && new_state == kState_LockingInitiated) |
93 |
| - { |
94 |
| - // If auto lock timer has been armed and someone initiates locking, |
95 |
| - // cancel the timer and continue as normal. |
96 |
| - mAutoLockTimerArmed = false; |
97 |
| - |
98 |
| - CancelTimer(); |
99 |
| - } |
100 |
| - |
101 |
| - StartTimer(ACTUATOR_MOVEMENT_PERIOS_MS); |
102 |
| - |
103 |
| - // Since the timer started successfully, update the state and trigger callback |
104 |
| - mState = new_state; |
105 |
| - |
106 |
| - if (mActionInitiated_CB) |
107 |
| - { |
108 |
| - mActionInitiated_CB(aAction, aActor); |
109 |
| - } |
110 |
| - } |
111 |
| - |
112 |
| - return action_initiated; |
113 |
| -} |
| 56 | + // The timer event handler is called in the context of the system clock ISR. |
| 57 | + // Post an event to the application task queue to process the event in the |
| 58 | + // context of the application thread. |
114 | 59 |
|
115 |
| -void BoltLockManager::StartTimer(uint32_t aTimeoutMs) |
116 |
| -{ |
117 |
| - k_timer_start(&sLockTimer, K_MSEC(aTimeoutMs), K_NO_WAIT); |
118 |
| -} |
119 |
| - |
120 |
| -void BoltLockManager::CancelTimer(void) |
121 |
| -{ |
122 |
| - k_timer_stop(&sLockTimer); |
123 |
| -} |
124 |
| - |
125 |
| -void BoltLockManager::TimerEventHandler(k_timer * timer) |
126 |
| -{ |
127 |
| - BoltLockManager * lock = static_cast<BoltLockManager *>(k_timer_user_data_get(timer)); |
128 |
| - |
129 |
| - // The timer event handler will be called in the context of the timer task |
130 |
| - // once sLockTimer expires. Post an event to apptask queue with the actual handler |
131 |
| - // so that the event can be handled in the context of the apptask. |
132 | 60 | AppEvent event;
|
133 | 61 | event.Type = AppEvent::kEventType_Timer;
|
134 |
| - event.TimerEvent.Context = lock; |
135 |
| - event.Handler = lock->mAutoLockTimerArmed ? AutoReLockTimerEventHandler : ActuatorMovementTimerEventHandler; |
| 62 | + event.TimerEvent.Context = static_cast<BoltLockManager *>(k_timer_user_data_get(timer)); |
| 63 | + event.Handler = BoltLockManager::ActuatorAppEventHandler; |
136 | 64 | GetAppTask().PostEvent(&event);
|
137 | 65 | }
|
138 | 66 |
|
139 |
| -void BoltLockManager::AutoReLockTimerEventHandler(AppEvent * aEvent) |
| 67 | +void BoltLockManager::ActuatorAppEventHandler(AppEvent * aEvent) |
140 | 68 | {
|
141 | 69 | BoltLockManager * lock = static_cast<BoltLockManager *>(aEvent->TimerEvent.Context);
|
142 |
| - int32_t actor = 0; |
143 |
| - |
144 |
| - // Make sure auto lock timer is still armed. |
145 |
| - if (!lock->mAutoLockTimerArmed) |
146 |
| - return; |
147 | 70 |
|
148 |
| - lock->mAutoLockTimerArmed = false; |
149 |
| - |
150 |
| - LOG_INF("Auto Re-Lock has been triggered!"); |
151 |
| - |
152 |
| - lock->InitiateAction(actor, LOCK_ACTION); |
| 71 | + switch (lock->mState) |
| 72 | + { |
| 73 | + case State::kLockingInitiated: |
| 74 | + lock->SetState(State::kLockingCompleted, lock->mActuatorOperationSource); |
| 75 | + break; |
| 76 | + case State::kUnlockingInitiated: |
| 77 | + lock->SetState(State::kUnlockingCompleted, lock->mActuatorOperationSource); |
| 78 | + break; |
| 79 | + default: |
| 80 | + break; |
| 81 | + } |
153 | 82 | }
|
154 | 83 |
|
155 |
| -void BoltLockManager::ActuatorMovementTimerEventHandler(AppEvent * aEvent) |
| 84 | +void BoltLockManager::SetState(State state, OperationSource source) |
156 | 85 | {
|
157 |
| - Action_t actionCompleted = INVALID_ACTION; |
158 |
| - |
159 |
| - BoltLockManager * lock = static_cast<BoltLockManager *>(aEvent->TimerEvent.Context); |
| 86 | + mState = state; |
160 | 87 |
|
161 |
| - if (lock->mState == kState_LockingInitiated) |
| 88 | + if (mStateChangeCallback != nullptr) |
162 | 89 | {
|
163 |
| - lock->mState = kState_LockingCompleted; |
164 |
| - actionCompleted = LOCK_ACTION; |
165 |
| - } |
166 |
| - else if (lock->mState == kState_UnlockingInitiated) |
167 |
| - { |
168 |
| - lock->mState = kState_UnlockingCompleted; |
169 |
| - actionCompleted = UNLOCK_ACTION; |
170 |
| - } |
171 |
| - |
172 |
| - if (actionCompleted != INVALID_ACTION) |
173 |
| - { |
174 |
| - if (lock->mActionCompleted_CB) |
175 |
| - { |
176 |
| - lock->mActionCompleted_CB(actionCompleted, lock->mCurrentActor); |
177 |
| - } |
178 |
| - |
179 |
| - if (lock->mAutoRelock && actionCompleted == UNLOCK_ACTION) |
180 |
| - { |
181 |
| - // Start the timer for auto relock |
182 |
| - lock->StartTimer(lock->mAutoLockDuration * 1000); |
183 |
| - |
184 |
| - lock->mAutoLockTimerArmed = true; |
185 |
| - |
186 |
| - LOG_INF("Auto Re-lock enabled. Will be triggered in %u seconds", lock->mAutoLockDuration); |
187 |
| - } |
| 90 | + mStateChangeCallback(state, source); |
188 | 91 | }
|
189 | 92 | }
|
0 commit comments