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