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_DP_PROFILE_CACHE_H 17 #define OHOS_DP_PROFILE_CACHE_H 18 19 #include <mutex> 20 #include <unordered_map> 21 #include <unordered_set> 22 #include <vector> 23 24 #include "iremote_object.h" 25 #include "single_instance.h" 26 27 #include "characteristic_profile.h" 28 #include "device_profile.h" 29 #include "distributed_device_profile_constants.h" 30 #include "distributed_device_profile_log.h" 31 #include "dp_subscribe_info.h" 32 #include "profile_utils.h" 33 #include "service_profile.h" 34 #include "trusted_device_info.h" 35 36 namespace OHOS { 37 namespace DistributedDeviceProfile { 38 class ProfileCache { 39 DECLARE_SINGLE_INSTANCE(ProfileCache); 40 41 public: 42 int32_t Init(); 43 int32_t UnInit(); 44 // local dynamic DeviceProfile 45 int32_t AddDeviceProfile(const DeviceProfile& deviceProfile); 46 // local dynamic ServiceProfile 47 int32_t AddServiceProfile(const ServiceProfile& serviceProfile); 48 // all static meta CharProfile 、 all switch CharProfile 、 local dynamic CharProfile 49 int32_t AddCharProfile(const CharacteristicProfile& charProfile); 50 // all static CharProfile 51 int32_t AddStaticCharProfile(const CharacteristicProfile& charProfile); 52 int32_t AddStaticCharProfileBatch(const std::unordered_map<std::string, CharacteristicProfile>& charProfiles); 53 uint32_t GetSwitch(); 54 int32_t GetNetWorkIdByUdid(const std::string& udid, std::string& networkId); 55 int32_t GetUdidByNetWorkId(const std::string& networkId, std::string& udid); 56 int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); 57 int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, 58 ServiceProfile& serviceProfile); 59 int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 60 const std::string& charKey, CharacteristicProfile& charProfile); 61 int32_t GetStaticCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 62 const std::string& charKey, CharacteristicProfile& charProfile); 63 int32_t DeleteDeviceProfile(const std::string& deviceId); 64 int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName); 65 int32_t DeleteCharProfile(const std::string& deviceId, const std::string& serviceName, const std::string& charKey); 66 bool IsDeviceProfileExist(const DeviceProfile& deviceProfile); 67 bool IsServiceProfileExist(const ServiceProfile& serviceProfile); 68 bool IsCharProfileExist(const CharacteristicProfile& charProfile); 69 int32_t RefreshProfileCache(); 70 int32_t AddSyncListener(const std::string& caller, sptr<IRemoteObject> syncListener); 71 int32_t GetSyncListeners(std::map<std::string, sptr<IRemoteObject>>& syncListeners); 72 int32_t RemoveSyncListeners(std::map<std::string, sptr<IRemoteObject>> syncListeners); 73 int32_t RemoveSyncListener(const std::string& caller); 74 int32_t RemoveSyncListener(sptr<IRemoteObject> syncListener); 75 int32_t SetSwitchByProfileBatch(const std::vector<CharacteristicProfile>& charProfiles, 76 const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, uint32_t& outSwitch); 77 int32_t SetSwitchByProfile(const CharacteristicProfile& charProfile, 78 const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, uint32_t& outSwitch); 79 bool IsSwitchValid(const CharacteristicProfile& charProfile, 80 const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, const std::string& operate); 81 int32_t SetSwitchProfile(CharacteristicProfile& charProfile, uint32_t switchValue); 82 void OnNodeOnline(const TrustedDeviceInfo& trustedDeviceInfo); 83 void OnNodeOffline(const std::string& peerNetworkId); 84 void SetCurSwitch(uint32_t newSwitch); 85 int32_t GetServiceNameByPos(int32_t pos, const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, 86 std::string& serviceName); 87 int32_t GetSwitchProfilesByServiceName(const std::string& charProfileKey, CharacteristicProfile& switchProfile); 88 bool IsCharProfileKeyExist(const std::string& charKey); 89 std::string GetLocalUdid(); 90 std::string GetLocalNetworkId(); 91 std::string GetLocalUuid(); 92 std::string GetLocalAccountId(); 93 int32_t AddAllTrustedDevices(const std::vector<TrustedDeviceInfo>& deviceInfos); 94 bool FilterAndGroupOnlineDevices(const std::vector<std::string>& deviceList, 95 std::vector<std::string>& ohBasedDevices, std::vector<std::string>& notOHBasedDevices); 96 bool IsDeviceOnline(); 97 98 private: 99 int32_t RefreshCharProfileCache(const std::vector<CharacteristicProfile>& characteristicProfiles); 100 int32_t RefreshStaticProfileCache(const std::unordered_map<std::string, CharacteristicProfile>& staticProfiles); 101 102 private: 103 std::string localNetworkId_; 104 std::mutex switchMutex_; 105 uint32_t curLocalSwitch_ = 0x0000; 106 std::mutex onlineDeviceLock_; 107 std::unordered_map<std::string, TrustedDeviceInfo> onlineDevMap_; 108 std::mutex deviceProfileMutex_; 109 // The key is profileKey, the value is DeviceProfile 110 std::unordered_map<std::string, DeviceProfile> deviceProfileMap_; 111 std::mutex serviceProfileMutex_; 112 // The key is profileKey, the value is ServiceProfile 113 std::unordered_map<std::string, ServiceProfile> serviceProfileMap_; 114 std::mutex charProfileMutex_; 115 // The key is profileKey, the value is CharacteristicProfile 116 std::unordered_map<std::string, CharacteristicProfile> charProfileMap_; 117 std::mutex staticCharProfileMutex_; 118 // The key is profileKey, the value is CharacteristicProfile 119 std::unordered_map<std::string, CharacteristicProfile> staticCharProfileMap_; 120 std::mutex syncListenerMutex_; 121 // The key is procName, the value is syncCallback 122 std::map<std::string, sptr<IRemoteObject>> syncListenerMap_; 123 sptr<IRemoteObject::DeathRecipient> syncListenerDeathRecipient_ = nullptr; 124 std::mutex localUuidMtx_; 125 std::string localUuid_; 126 }; 127 } // namespace DeviceProfile 128 } // namespace OHOS 129 #endif // OHOS_DP_PROFILE_CACHE_H 130