1 /* 2 * Copyright (c) 2021 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 void Start() override; 61 void Stop() override; 62 void JoinGroup(std::weak_ptr<MountPoint> mp); 63 void QuitGroup(std::weak_ptr<MountPoint> mp); 64 65 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 66 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 67 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)68 void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} 69 70 void OfflineAllDevice(); 71 void ReconnectOnlineDevices(); 72 void OnRemoteDied() override; 73 74 DeviceInfo &GetLocalDeviceInfo(); 75 std::vector<DeviceInfo> GetRemoteDevicesInfo(); 76 77 private: 78 void StartInstance() override; 79 void StopInstance() override; 80 void InitLocalNodeInfo(); 81 82 void RegisterToExternalDm(); 83 void UnregisterFromExternalDm(); 84 85 void QueryRelatedGroups(const std::string &udid, const std::string &networkId); 86 bool CheckIsAccountless(const GroupInfo &group); 87 std::shared_ptr<NetworkAgentTemplate> FindNetworkBaseTrustRelation(bool isAccountless); 88 // We use a mutex instead of a shared_mutex to serialize online/offline procedures 89 std::mutex mpToNetworksMutex_; 90 std::map<uintptr_t, std::shared_ptr<NetworkAgentTemplate>> mpToNetworks_; 91 DeviceInfo localDeviceInfo_; 92 93 // cid-->same_account/accoutless's network 94 std::unordered_map<std::string, std::shared_ptr<NetworkAgentTemplate>> cidNetTypeRecord_; 95 }; 96 } // namespace DistributedFile 97 } // namespace Storage 98 } // namespace OHOS 99 #endif // DEVICE_MANAGER_AGENT_H