1 /* 2 * Copyright (c) 2022 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_DEVICE_STATE_MANAGER_H 17 #define OHOS_DM_DEVICE_STATE_MANAGER_H 18 19 #include <condition_variable> 20 #include <memory> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 25 #if defined(__LITEOS_M__) 26 #include "dm_mutex.h" 27 #else 28 #include <mutex> 29 #endif 30 #include "idevice_manager_service_listener.h" 31 #include "dm_adapter_manager.h" 32 #include "softbus_connector.h" 33 #include "dm_timer.h" 34 #include "hichain_connector.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 #define OFFLINE_TIMEOUT 300 39 struct StateTimerInfo { 40 std::string timerName; 41 std::string networkId; 42 bool isStart; 43 }; 44 45 class NotifyEvent { 46 public: NotifyEvent(int32_t eventId,const std::string & deviceId)47 NotifyEvent(int32_t eventId, const std::string &deviceId) : eventId_(eventId), deviceId_(deviceId) {}; ~NotifyEvent()48 ~NotifyEvent() {}; 49 GetEventId()50 int32_t GetEventId() const 51 { 52 return eventId_; 53 }; GetDeviceId()54 std::string GetDeviceId() const 55 { 56 return deviceId_; 57 }; 58 private: 59 int32_t eventId_; 60 std::string deviceId_; 61 }; 62 63 typedef struct NotifyTask { 64 std::thread queueThread_; 65 std::condition_variable queueCond_; 66 std::condition_variable queueFullCond_; 67 std::mutex queueMtx_; 68 std::queue<std::shared_ptr<NotifyEvent>> queue_; 69 bool threadRunning_ = false; 70 } NotifyTask; 71 72 class DmDeviceStateManager final : public ISoftbusStateCallback, 73 public std::enable_shared_from_this<DmDeviceStateManager> { 74 public: 75 DmDeviceStateManager(std::shared_ptr<SoftbusConnector> softbusConnector, 76 std::shared_ptr<IDeviceManagerServiceListener> listener, 77 std::shared_ptr<HiChainConnector> hiChainConnector); 78 ~DmDeviceStateManager(); 79 80 /** 81 * @tc.name: DmDeviceStateManager::SaveOnlineDeviceInfo 82 * @tc.desc: Save Online DeviceInfo of the Dm Device State Manager 83 * @tc.type: FUNC 84 */ 85 void SaveOnlineDeviceInfo(const std::string &pkgName, const DmDeviceInfo &info); 86 87 /** 88 * @tc.name: DmDeviceStateManager::DeleteOfflineDeviceInfo 89 * @tc.desc: Delete Offline DeviceInfo of the Dm Device State Manager 90 * @tc.type: FUNC 91 */ 92 void DeleteOfflineDeviceInfo(const std::string &pkgName, const DmDeviceInfo &info); 93 94 /** 95 * @tc.name: DmDeviceStateManager::PostDeviceOnline 96 * @tc.desc: Post Device Online of the Dm Device State Manager 97 * @tc.type: FUNC 98 */ 99 void PostDeviceOnline(const std::string &pkgName, DmDeviceInfo &info); 100 101 /** 102 * @tc.name: DmDeviceStateManager::PostDeviceOffline 103 * @tc.desc: Post Device Offline of the Dm Device State Manager 104 * @tc.type: FUNC 105 */ 106 void PostDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); 107 108 /** 109 * @tc.name: DmDeviceStateManager::PostDeviceChanged 110 * @tc.desc: Post Device Offline of the Dm Device State Manager 111 * @tc.type: FUNC 112 */ 113 void PostDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info); 114 /** 115 * @tc.name: DmDeviceStateManager::OnDeviceOnline 116 * @tc.desc: OnDevice Online of the Dm Device State Manager 117 * @tc.type: FUNC 118 */ 119 void OnDeviceOnline(const std::string &pkgName, DmDeviceInfo &info); 120 121 /** 122 * @tc.name: DmDeviceStateManager::OnDeviceOffline 123 * @tc.desc: OnDevice Offline of the Dm Device State Manager 124 * @tc.type: FUNC 125 */ 126 void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); 127 128 /** 129 * @tc.name: DmDeviceStateManager::OnDeviceChanged 130 * @tc.desc: OnDevice Changed of the Dm Device State Manager 131 * @tc.type: FUNC 132 */ 133 void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info); 134 135 /** 136 * @tc.name: DmDeviceStateManager::OnDeviceReady 137 * @tc.desc: OnDevice Ready of the Dm Device State Manager 138 * @tc.type: FUNC 139 */ 140 void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info); 141 142 /** 143 * @tc.name: DmDeviceStateManager::OnDbReady 144 * @tc.desc: OnDb Ready of the Dm Device State Manager 145 * @tc.type: FUNC 146 */ 147 void OnDbReady(const std::string &pkgName, const std::string &deviceId); 148 149 /** 150 * @tc.name: DmDeviceStateManager::RegisterSoftbusStateCallback 151 * @tc.desc: Register Softbus State Callback of the Dm Device State Manager 152 * @tc.type: FUNC 153 */ 154 int32_t RegisterSoftbusStateCallback(); 155 156 /** 157 * @tc.name: DmDeviceStateManager::RegisterOffLineTimer 158 * @tc.desc: Register OffLine Timerof the Dm Device State Manager 159 * @tc.type: FUNC 160 */ 161 void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo); 162 163 /** 164 * @tc.name: DmDeviceStateManager::StartOffLineTimer 165 * @tc.desc: Start OffLine Timer of the Dm Device State Manager 166 * @tc.type: FUNC 167 */ 168 void StartOffLineTimer(const DmDeviceInfo &deviceInfo); 169 170 /** 171 * @tc.name: DmDeviceStateManager::DeleteTimeOutGroup 172 * @tc.desc: Delete TimeOut Group of the Dm Device State Manager 173 * @tc.type: FUNC 174 */ 175 void DeleteTimeOutGroup(std::string name); 176 177 /** 178 * @tc.name: DmDeviceStateManager::RegisterDevStateCallback 179 * @tc.desc: Register DevState Callback of the Dm Device State Manager 180 * @tc.type: FUNC 181 */ 182 void RegisterDevStateCallback(const std::string &pkgName, const std::string &extra); 183 184 /** 185 * @tc.name: DmDeviceStateManager::UnRegisterDevStateCallback 186 * @tc.desc: UnRegister DevState Callback of the Dm Device State Manager 187 * @tc.type: FUNC 188 */ 189 void UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra); 190 191 /** 192 * @tc.name: DmDeviceStateManager::ProcNotifyEvent 193 * @tc.desc: Proc Notify Event of the Dm Device State Manager 194 * @tc.type: FUNC 195 */ 196 int32_t ProcNotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &deviceId); 197 198 void ChangeDeviceInfo(const std::string &pkgName, const DmDeviceInfo &info); 199 private: 200 void StartEventThread(); 201 void StopEventThread(); 202 void ThreadLoop(); 203 int32_t AddTask(const std::shared_ptr<NotifyEvent> &task); 204 void RunTask(const std::shared_ptr<NotifyEvent> &task); 205 DmAuthForm GetAuthForm(const std::string &networkId); 206 private: 207 #if !defined(__LITEOS_M__) 208 std::mutex timerMapMutex_; 209 std::mutex remoteDeviceInfosMutex_; 210 std::mutex decisionInfosMutex_; 211 #endif 212 std::shared_ptr<SoftbusConnector> softbusConnector_; 213 std::shared_ptr<IDeviceManagerServiceListener> listener_; 214 std::map<std::string, DmDeviceInfo> remoteDeviceInfos_; 215 std::map<std::string, std::string> decisionInfos_; 216 std::map<std::string, StateTimerInfo> stateTimerInfoMap_; 217 std::shared_ptr<DmTimer> timer_; 218 std::shared_ptr<HiChainConnector> hiChainConnector_; 219 std::string decisionSoName_; 220 NotifyTask eventTask_; 221 }; 222 } // namespace DistributedHardware 223 } // namespace OHOS 224 #endif // OHOS_DM_DEVICE_STATE_MANAGER_H 225