1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DM_AUTH_STATE_MACHINE_V2_H 17 #define OHOS_DM_AUTH_STATE_MACHINE_V2_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <optional> 25 #include <queue> 26 #include <set> 27 #include <thread> 28 #include <chrono> 29 30 #include "dm_auth_state.h" 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 35 // Define the state transition table type 36 using StateTransitionTable = std::map<DmAuthStateType, std::set<DmAuthStateType>>; 37 38 enum DmEventType { 39 ON_TRANSMIT = 0, 40 ON_SESSION_KEY_RETURNED, 41 ON_REQUEST, 42 ON_FINISH, 43 ON_ERROR, 44 ON_ULTRASONIC_PIN_CHANGED, 45 ON_ULTRASONIC_PIN_TIMEOUT, 46 47 ON_TIMEOUT, 48 ON_USER_OPERATION, 49 ON_FAIL, 50 ON_SCREEN_LOCKED, 51 ON_SESSION_OPENED, 52 }; 53 54 class DmAuthStateMachine { 55 public: 56 DmAuthStateMachine(std::shared_ptr<DmAuthContext> context); 57 ~DmAuthStateMachine(); 58 59 // Notify state transition, execute the corresponding action for the state, and handle exceptions 60 // only allowed to be called within OnDataReceived 61 int32_t TransitionTo(std::shared_ptr<DmAuthState> state); 62 63 // Wait for the expected event within the action, block until the expected event is completed or 64 // an exception occurs, returning the actual event that occurred (only allowed to be called within actions) 65 DmEventType WaitExpectEvent(DmEventType eventType); 66 67 // Notify the completion of an event, passing the event enumeration 68 // (only allowed to be called when the event is triggered). If it's an exception event, 69 // record it in the context's reason or reply. 70 void NotifyEventFinish(DmEventType eventType); 71 72 DmAuthStateType GetCurState(); 73 74 // Stop the thread 75 void Stop(); 76 77 private: 78 // Loop to wait for state transitions and execute actions 79 void Run(std::shared_ptr<DmAuthContext> context); 80 void InsertSrcTransTable(); 81 void InsertSinkTransTable(); 82 void InsertUltrasonicSrcTransTable(); 83 void InsertUltrasonicSinkTransTable(); 84 void InsertCredentialAuthSrcTransTable(); 85 void InsertCredentialAuthSinkTransTable(); 86 87 // Fetch the current state and execute it 88 std::optional<std::shared_ptr<DmAuthState>> FetchAndSetCurState(); 89 90 void SetCurState(DmAuthStateType state); 91 92 bool CheckStateTransitValid(DmAuthStateType nextState); 93 94 void NotifyEventWait(); 95 void NotifyStateWait(); 96 97 DmAuthStateType curState_; 98 DmAuthStateType preState_{DmAuthStateType::AUTH_IDLE_STATE}; // Previous push state 99 100 // State transition table for normal state transitions (all state transitions to the Finish state are valid) 101 StateTransitionTable stateTransitionTable_; 102 103 std::queue<DmEventType> eventQueue_; 104 105 // Set of exception events 106 std::set<DmEventType> exceptionEvent_; 107 108 // Atomic flag to control the state machine's running state 109 std::atomic<bool> running_; 110 111 // Queue for storing states 112 std::queue<std::shared_ptr<DmAuthState>> statesQueue_; 113 114 // Synchronization primitives 115 std::mutex stateMutex_; 116 std::condition_variable stateCv_; 117 std::mutex eventMutex_; 118 std::condition_variable eventCv_; 119 120 // Direction of authentication 121 DmAuthDirection direction_; 122 int32_t reason{DM_OK}; 123 124 // Thread for state machine execution 125 std::thread thread_; 126 }; 127 } // namespace DistributedHardware 128 } // namespace OHOS 129 #endif // OHOS_DM_AUTH_STATE_MACHINE_V2_H 130