• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 <vector>
23 
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 "mountpoint/mount_point.h"
31 #include "network/network_agent_template.h"
32 #include "nlohmann/json.hpp"
33 
34 namespace OHOS {
35 namespace Storage {
36 namespace DistributedFile {
37 struct GroupInfo {
38     std::string groupName;
39     std::string groupId;
40     std::string groupOwner;
41     int32_t groupType;
GroupInfoGroupInfo42     GroupInfo() : groupType(0) {}
GroupInfoGroupInfo43     GroupInfo(std::string name, std::string id, std::string owner, int32_t type)
44         : groupName(name), groupId(id), groupOwner(owner), groupType(type)
45     {
46     }
47 };
48 
49 void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo);
50 
51 class DeviceManagerAgent final : public DistributedHardware::DmInitCallback,
52                                  public DistributedHardware::DeviceStateCallback,
53                                  public std::enable_shared_from_this<DeviceManagerAgent>,
54                                  public DfsuStartable,
55                                  public DfsuActor<DeviceManagerAgent>,
56                                  public Utils::DfsuSingleton<DeviceManagerAgent> {
57     DECLARE_SINGLETON(DeviceManagerAgent);
58 
59 public:
60     enum P2PErrCode:int32_t {
61         P2P_SUCCESS,
62         P2P_FAILED,
63     };
64     void Start() override;
65     void Stop() override;
66     void JoinGroup(std::weak_ptr<MountPoint> mp);
67     void QuitGroup(std::weak_ptr<MountPoint> mp);
68 
69     void InitDeviceInfos();
70 
71     void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
72     void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
73     void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override;
OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)74     void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override {}
75 
76     int32_t OnDeviceP2POnline(const DistributedHardware::DmDeviceInfo &deviceInfo);
77     int32_t OnDeviceP2POffline(const DistributedHardware::DmDeviceInfo &deviceInfo);
78 
79     void OfflineAllDevice();
80     void ReconnectOnlineDevices();
81     void OnRemoteDied() override;
82 
83     DeviceInfo &GetLocalDeviceInfo();
84     std::vector<DeviceInfo> GetRemoteDevicesInfo();
85 
86 private:
87     void StartInstance() override;
88     void StopInstance() override;
89     void InitLocalNodeInfo();
90 
91     void RegisterToExternalDm();
92     void UnregisterFromExternalDm();
93 
94     int32_t GetNetworkType(const std::string &cid);
95     bool IsWifiNetworkType(int32_t networkType);
96 
97     void QueryRelatedGroups(const std::string &udid, const std::string &networkId);
98     bool CheckIsAccountless(const GroupInfo &group);
99     std::shared_ptr<NetworkAgentTemplate> FindNetworkBaseTrustRelation(bool isAccountless);
100     // We use a mutex instead of a shared_mutex to serialize online/offline procedures
101     std::mutex mpToNetworksMutex_;
102     std::map<uintptr_t, std::shared_ptr<NetworkAgentTemplate>> mpToNetworks_;
103     DeviceInfo localDeviceInfo_;
104 
105     // cid-->same_account/accoutless's network
106     std::unordered_map<std::string, std::shared_ptr<NetworkAgentTemplate>> cidNetTypeRecord_;
107     std::unordered_map<std::string, int32_t> cidNetworkType_;
108 };
109 } // namespace DistributedFile
110 } // namespace Storage
111 } // namespace OHOS
112 #endif // DEVICE_MANAGER_AGENT_H