• 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 
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);
94     void OnDeviceOffline(std::string deviceId);
95     void HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo);
96 
97 private:
98     void StartEventThread();
99     void StopEventThread();
100     void ThreadLoop();
101     int32_t AddTask(const std::shared_ptr<NotifyEvent> &task);
102     void RunTask(const std::shared_ptr<NotifyEvent> &task);
103     DmAuthForm GetAuthForm(const std::string &networkId);
104 
105 private:
106     std::mutex timerMapMutex_;
107     std::mutex remoteDeviceInfosMutex_;
108     std::shared_ptr<SoftbusConnector> softbusConnector_;
109     std::shared_ptr<IDeviceManagerServiceListener> listener_;
110     std::map<std::string, DmDeviceInfo> remoteDeviceInfos_;
111     std::map<std::string, DmDeviceInfo> stateDeviceInfos_;
112     std::map<std::string, StateTimerInfo> stateTimerInfoMap_;
113     std::shared_ptr<DmTimer> timer_;
114     std::shared_ptr<HiChainConnector> hiChainConnector_;
115     std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_;
116     std::string decisionSoName_;
117     NotifyTask eventTask_;
118 };
119 } // namespace DistributedHardware
120 } // namespace OHOS
121 #endif // OHOS_DM_DEVICE_STATE_MANAGER_H
122