1 /*
2  * Copyright (c) 2021-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 #ifndef DEVICE_MANAGER_AGENT_H
16 #define DEVICE_MANAGER_AGENT_H
17 
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 #include "device_auth.h"
25 #include "device_info.h"
26 #include "device_manager.h"
27 #include "dfsu_actor.h"
28 #include "dfsu_singleton.h"
29 #include "dfsu_startable.h"
30 #include "i_file_dfs_listener.h"
31 #include "mountpoint/mount_point.h"
32 #include "network/network_agent_template.h"
33 #include "nlohmann/json.hpp"
34 #include "storage_manager_proxy.h"
35 #include "utils_directory.h"
36 
37 namespace OHOS {
38 namespace Storage {
39 namespace DistributedFile {
40 const int32_t ON_STATUS_OFFLINE = 13900046;
41 struct GroupInfo {
42     std::string groupName;
43     std::string groupId;
44     std::string groupOwner;
45     int32_t groupType;
GroupInfoGroupInfo46     GroupInfo() : groupType(0) {}
GroupInfoGroupInfo47     GroupInfo(std::string name, std::string id, std::string owner, int32_t type)
48         : groupName(name), groupId(id), groupOwner(owner), groupType(type)
49     {
50     }
51 };
52 
53 void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo);
54 
55 class DeviceManagerAgent final : public DistributedHardware::DmInitCallback,
56                                  public DistributedHardware::DeviceStateCallback,
57                                  public std::enable_shared_from_this<DeviceManagerAgent>,
58                                  public DfsuStartable,
59                                  public DfsuActor<DeviceManagerAgent>,
60                                  public Utils::DfsuSingleton<DeviceManagerAgent> {
61     DECLARE_SINGLETON(DeviceManagerAgent);
62 
63 public:
64     enum P2PErrCode:int32_t {
65         P2P_SUCCESS,
66         P2P_FAILED,
67     };
68     void Start() override;
69     void Stop() override;
70     void JoinGroup(std::weak_ptr<MountPoint> mp);
71     void QuitGroup(std::weak_ptr<MountPoint> mp);
72 
73     void InitDeviceInfos();
74     int32_t IsSupportedDevice(const DistributedHardware::DmDeviceInfo &deviceInfo);
75 
76     void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
77     void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
78     void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)79     void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override {}
80 
81     void ClearCount(const DistributedHardware::DmDeviceInfo &deviceInfo);
82     int32_t OnDeviceP2POnline(const DistributedHardware::DmDeviceInfo &deviceInfo);
83     int32_t OnDeviceP2POffline(const DistributedHardware::DmDeviceInfo &deviceInfo);
84     int32_t AddRemoteReverseObj(uint32_t callingTokenId, sptr<IFileDfsListener> remoteReverseObj);
85     int32_t RemoveRemoteReverseObj(bool clear, uint32_t callingTokenId);
86     void NotifyRemoteReverseObj(const std::string &networkId, int32_t status);
87     int32_t FindListenerByObject(const wptr<IRemoteObject> &remote, uint32_t &tokenId,
88         sptr<IFileDfsListener>& listener);
89     std::string GetDeviceIdByNetworkId(const std::string &networkId);
90     int32_t MountDfsDocs(const std::string &networkId, const std::string &deviceId);
91     int32_t UMountDfsDocs(const std::string &networkId, const std::string &deviceId, bool needClear);
92     void AddNetworkId(uint32_t tokenId, const std::string &networkId);
93     void RemoveNetworkId(uint32_t tokenId);
94     void RemoveNetworkIdByOne(uint32_t tokenId, const std::string &networkId);
95     void RemoveNetworkIdForAllToken(const std::string &networkId);
96     void ClearNetworkId();
97     void IncreaseMountDfsCount(const std::string &deviceId);
98     void RemoveMountDfsCount(const std::string &devcieId);
99     std::unordered_set<std::string> GetNetworkIds(uint32_t tokenId);
100 
101     void OfflineAllDevice();
102     void ReconnectOnlineDevices();
103     void OnRemoteDied() override;
104 
105     DeviceInfo &GetLocalDeviceInfo();
106     std::vector<DeviceInfo> GetRemoteDevicesInfo();
107     std::mutex appCallConnectMutex_;
108     std::unordered_map<uint32_t, sptr<IFileDfsListener>> appCallConnect_;
109 
110 private:
111     void StartInstance() override;
112     void StopInstance() override;
113     void InitLocalNodeInfo();
114 
115     void RegisterToExternalDm();
116     void UnregisterFromExternalDm();
117 
118     int32_t GetNetworkType(const std::string &cid);
119     bool IsWifiNetworkType(int32_t networkType);
120 
121     void QueryRelatedGroups(const std::string &udid, const std::string &networkId);
122     bool CheckIsAccountless(const GroupInfo &group);
123     std::shared_ptr<NetworkAgentTemplate> FindNetworkBaseTrustRelation(bool isAccountless);
124     // We use a mutex instead of a shared_mutex to serialize online/offline procedures
125     std::mutex mpToNetworksMutex_;
126     std::map<uintptr_t, std::shared_ptr<NetworkAgentTemplate>> mpToNetworks_;
127     DeviceInfo localDeviceInfo_;
128 
129     // cid-->same_account/accoutless's network
130     std::unordered_map<std::string, std::shared_ptr<NetworkAgentTemplate>> cidNetTypeRecord_;
131     std::unordered_map<std::string, int32_t> cidNetworkType_;
132     bool MountDfsCountOnly(const std::string &deviceId);
133     bool UMountDfsCountOnly(const std::string &deviceId, bool needClear);
134     int32_t GetCurrentUserId();
135     void GetStorageManager();
136     sptr<StorageManager::IStorageManager> storageMgrProxy_;
137     std::mutex mountDfsCountMutex_;
138     std::unordered_map<std::string, int32_t> mountDfsCount_;
139     std::mutex networkIdMapMutex_;
140     std::unordered_map<uint32_t, std::unordered_set<std::string>> networkIdMap_;
141 };
142 } // namespace DistributedFile
143 } // namespace Storage
144 } // namespace OHOS
145 #endif // DEVICE_MANAGER_AGENT_H
146