• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 #include "hichain_auth_connector.h"
36 #include "multiple_user_connector.h"
37 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
38 #include "deviceprofile_connector.h"
39 #endif
40 
41 namespace OHOS {
42 namespace DistributedHardware {
43 #define OFFLINE_TIMEOUT 300
44 struct StateTimerInfo {
45     std::string timerName;
46     std::string networkId;
47     bool isStart;
48 };
49 
50 class NotifyEvent {
51 public:
NotifyEvent(int32_t eventId,const std::string & deviceId)52     NotifyEvent(int32_t eventId, const std::string &deviceId) : eventId_(eventId), deviceId_(deviceId) {};
~NotifyEvent()53     ~NotifyEvent() {};
54 
GetEventId()55     int32_t GetEventId() const
56     {
57         return eventId_;
58     };
GetDeviceId()59     std::string GetDeviceId() const
60     {
61         return deviceId_;
62     };
63 private:
64     int32_t eventId_;
65     std::string deviceId_;
66 };
67 
68 typedef struct NotifyTask {
69     std::thread queueThread_;
70     std::condition_variable queueCond_;
71     std::condition_variable queueFullCond_;
72     std::mutex queueMtx_;
73     std::queue<std::shared_ptr<NotifyEvent>> queue_;
74     bool threadRunning_ = false;
75 } NotifyTask;
76 
77 class DmDeviceStateManager final : public ISoftbusStateCallback,
78                                    public std::enable_shared_from_this<DmDeviceStateManager> {
79 public:
80     DmDeviceStateManager(std::shared_ptr<SoftbusConnector> softbusConnector,
81                          std::shared_ptr<IDeviceManagerServiceListener> listener,
82                          std::shared_ptr<HiChainConnector> hiChainConnector,
83                          std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector);
84     ~DmDeviceStateManager();
85 
86     int32_t ProcNotifyEvent(const int32_t eventId, const std::string &deviceId);
87     void SaveOnlineDeviceInfo(const DmDeviceInfo &info);
88     void DeleteOfflineDeviceInfo(const DmDeviceInfo &info);
89     void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo,
90         std::vector<ProcessInfo> &processInfoVec);
91     void OnDbReady(const std::string &pkgName, const std::string &uuid);
92     void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo);
93     void StartOffLineTimer(const DmDeviceInfo &deviceInfo);
94     void DeleteTimeOutGroup(std::string name);
95     void ChangeDeviceInfo(const DmDeviceInfo &info);
96     int32_t RegisterSoftbusStateCallback();
97     void OnDeviceOnline(std::string deviceId, int32_t authForm);
98     void OnDeviceOffline(std::string deviceId);
99     std::string GetUdidByNetWorkId(std::string networkId);
100     bool CheckIsOnline(const std::string &udid);
101     void DeleteOffLineTimer(std::string udidHash);
102     void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo, std::vector<ProcessInfo> &processInfos);
103 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
104     int32_t DeleteSkCredAndAcl(const std::vector<DmAclIdParam> &acls);
105 #endif
106 private:
107     void StartEventThread();
108     void StopEventThread();
109     void ThreadLoop();
110     int32_t AddTask(const std::shared_ptr<NotifyEvent> &task);
111     void RunTask(const std::shared_ptr<NotifyEvent> &task);
112     DmAuthForm GetAuthForm(const std::string &networkId);
113 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
114     int32_t DeleteGroupByDP(const std::string &deviceId);
115     void DeleteCredential(DmOfflineParam offlineParam, const std::string &deviceId);
116 #endif
117     void ProcessDeviceStateChange(const DmDeviceState devState, const DmDeviceInfo &devInfo,
118         std::vector<ProcessInfo> &processInfoVec);
119 private:
120     std::mutex timerMapMutex_;
121     std::mutex remoteDeviceInfosMutex_;
122     std::shared_ptr<SoftbusConnector> softbusConnector_;
123     std::shared_ptr<IDeviceManagerServiceListener> listener_;
124     std::map<std::string, DmDeviceInfo> remoteDeviceInfos_;
125     std::map<std::string, DmDeviceInfo> stateDeviceInfos_;
126     std::map<std::string, StateTimerInfo> stateTimerInfoMap_;
127     std::map<std::string, std::string> udidhash2udidMap_;
128     std::shared_ptr<DmTimer> timer_;
129     std::shared_ptr<HiChainConnector> hiChainConnector_;
130     std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_;
131     std::string decisionSoName_;
132     NotifyTask eventTask_;
133 };
134 } // namespace DistributedHardware
135 } // namespace OHOS
136 #endif // OHOS_DM_DEVICE_STATE_MANAGER_H
137