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, const 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::OnDeviceOnline 110 * @tc.desc: OnDevice Online of the Dm Device State Manager 111 * @tc.type: FUNC 112 */ 113 void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info); 114 115 /** 116 * @tc.name: DmDeviceStateManager::OnDeviceOffline 117 * @tc.desc: OnDevice Offline of the Dm Device State Manager 118 * @tc.type: FUNC 119 */ 120 void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); 121 122 /** 123 * @tc.name: DmDeviceStateManager::OnDeviceChanged 124 * @tc.desc: OnDevice Changed of the Dm Device State Manager 125 * @tc.type: FUNC 126 */ 127 void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info); 128 129 /** 130 * @tc.name: DmDeviceStateManager::OnDeviceReady 131 * @tc.desc: OnDevice Ready of the Dm Device State Manager 132 * @tc.type: FUNC 133 */ 134 void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info); 135 136 /** 137 * @tc.name: DmDeviceStateManager::OnDbReady 138 * @tc.desc: OnDb Ready of the Dm Device State Manager 139 * @tc.type: FUNC 140 */ 141 void OnDbReady(const std::string &pkgName, const std::string &deviceId); 142 143 /** 144 * @tc.name: DmDeviceStateManager::RegisterSoftbusStateCallback 145 * @tc.desc: Register Softbus State Callback of the Dm Device State Manager 146 * @tc.type: FUNC 147 */ 148 int32_t RegisterSoftbusStateCallback(); 149 150 /** 151 * @tc.name: DmDeviceStateManager::RegisterOffLineTimer 152 * @tc.desc: Register OffLine Timerof the Dm Device State Manager 153 * @tc.type: FUNC 154 */ 155 void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo); 156 157 /** 158 * @tc.name: DmDeviceStateManager::StartOffLineTimer 159 * @tc.desc: Start OffLine Timer of the Dm Device State Manager 160 * @tc.type: FUNC 161 */ 162 void StartOffLineTimer(const DmDeviceInfo &deviceInfo); 163 164 /** 165 * @tc.name: DmDeviceStateManager::DeleteTimeOutGroup 166 * @tc.desc: Delete TimeOut Group of the Dm Device State Manager 167 * @tc.type: FUNC 168 */ 169 void DeleteTimeOutGroup(std::string name); 170 171 /** 172 * @tc.name: DmDeviceStateManager::RegisterDevStateCallback 173 * @tc.desc: Register DevState Callback of the Dm Device State Manager 174 * @tc.type: FUNC 175 */ 176 void RegisterDevStateCallback(const std::string &pkgName, const std::string &extra); 177 178 /** 179 * @tc.name: DmDeviceStateManager::UnRegisterDevStateCallback 180 * @tc.desc: UnRegister DevState Callback of the Dm Device State Manager 181 * @tc.type: FUNC 182 */ 183 void UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra); 184 185 /** 186 * @tc.name: DmDeviceStateManager::ProcNotifyEvent 187 * @tc.desc: Proc Notify Event of the Dm Device State Manager 188 * @tc.type: FUNC 189 */ 190 int32_t ProcNotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &deviceId); 191 192 private: 193 void StartEventThread(); 194 void StopEventThread(); 195 void ThreadLoop(); 196 int32_t AddTask(const std::shared_ptr<NotifyEvent> &task); 197 void RunTask(const std::shared_ptr<NotifyEvent> &task); 198 private: 199 #if !defined(__LITEOS_M__) 200 std::mutex timerMapMutex_; 201 std::mutex remoteDeviceInfosMutex_; 202 #endif 203 std::shared_ptr<SoftbusConnector> softbusConnector_; 204 std::shared_ptr<IDeviceManagerServiceListener> listener_; 205 std::map<std::string, DmDeviceInfo> remoteDeviceInfos_; 206 std::map<std::string, std::string> decisionInfos_; 207 std::map<std::string, StateTimerInfo> stateTimerInfoMap_; 208 std::shared_ptr<DmTimer> timer_; 209 std::shared_ptr<HiChainConnector> hiChainConnector_; 210 std::string decisionSoName_; 211 NotifyTask eventTask_; 212 }; 213 } // namespace DistributedHardware 214 } // namespace OHOS 215 #endif // OHOS_DM_DEVICE_STATE_MANAGER_H 216