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 35 namespace OHOS { 36 namespace DistributedDeviceProfile { 37 class ProfileCache { 38 DECLARE_SINGLE_INSTANCE(ProfileCache); 39 40 public: 41 int32_t Init(); 42 int32_t UnInit(); 43 // local dynamic DeviceProfile 44 int32_t AddDeviceProfile(const DeviceProfile& deviceProfile); 45 // local dynamic ServiceProfile 46 int32_t AddServiceProfile(const ServiceProfile& serviceProfile); 47 // all static meta CharProfile 、 all switch CharProfile 、 local dynamic CharProfile 48 int32_t AddCharProfile(const CharacteristicProfile& charProfile); 49 // all static CharProfile 50 int32_t AddStaticCharProfile(const CharacteristicProfile& charProfile); 51 int32_t AddStaticCharProfileBatch(const std::unordered_map<std::string, CharacteristicProfile>& charProfiles); 52 uint32_t GetSwitch(); 53 int32_t GetNetWorkIdByUdid(const std::string& udid, std::string& networkId); 54 int32_t GetUdidByNetWorkId(const std::string& networkId, std::string& udid); 55 int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); 56 int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, 57 ServiceProfile& serviceProfile); 58 int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 59 const std::string& charKey, CharacteristicProfile& charProfile); 60 int32_t GetStaticCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 61 const std::string& charKey, CharacteristicProfile& charProfile); 62 int32_t DeleteDeviceProfile(const std::string& deviceId); 63 int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName); 64 int32_t DeleteCharProfile(const std::string& deviceId, const std::string& serviceName, const std::string& charKey); 65 bool IsDeviceProfileExist(const DeviceProfile& deviceProfile); 66 bool IsServiceProfileExist(const ServiceProfile& serviceProfile); 67 bool IsCharProfileExist(const CharacteristicProfile& charProfile); 68 int32_t RefreshProfileCache(); 69 int32_t AddSyncListener(const std::string& caller, sptr<IRemoteObject> syncListener); 70 int32_t GetSyncListeners(std::map<std::string, sptr<IRemoteObject>>& syncListeners); 71 int32_t RemoveSyncListeners(std::map<std::string, sptr<IRemoteObject>> syncListeners); 72 int32_t RemoveSyncListener(const std::string& caller); 73 int32_t RemoveSyncListener(sptr<IRemoteObject> syncListener); 74 int32_t SetSwitchByProfileBatch(const std::vector<CharacteristicProfile>& charProfiles, 75 const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, uint32_t& outSwitch); 76 int32_t SetSwitchByProfile(const CharacteristicProfile& charProfile, 77 const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, uint32_t& outSwitch); 78 bool IsSwitchValid(const CharacteristicProfile& charProfile, 79 const std::unordered_map<std::string, SwitchFlag>& switchServiceMap, const std::string& operate); 80 int32_t SetSwitchProfile(CharacteristicProfile& charProfile, uint32_t switchValue); 81 void OnNodeOnline(const std::string& peerNetworkId); 82 void OnNodeOffline(const std::string& peerNetworkId); 83 bool IsLocalOrOnlineDevice(const std::string& deviceId); 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 93 private: 94 int32_t RefreshCharProfileCache(const std::vector<CharacteristicProfile>& characteristicProfiles); 95 int32_t RefreshStaticProfileCache(const std::unordered_map<std::string, CharacteristicProfile>& staticProfiles); 96 97 private: 98 std::string localNetworkId_; 99 std::mutex switchMutex_; 100 uint32_t curLocalSwitch_ = 0x0000; 101 std::mutex onlineDeviceLock_; 102 std::unordered_map<std::string, std::string> onlineDevMap_; 103 std::mutex deviceProfileMutex_; 104 // The key is profileKey, the value is DeviceProfile 105 std::unordered_map<std::string, DeviceProfile> deviceProfileMap_; 106 std::mutex serviceProfileMutex_; 107 // The key is profileKey, the value is ServiceProfile 108 std::unordered_map<std::string, ServiceProfile> serviceProfileMap_; 109 std::mutex charProfileMutex_; 110 // The key is profileKey, the value is CharacteristicProfile 111 std::unordered_map<std::string, CharacteristicProfile> charProfileMap_; 112 std::mutex staticCharProfileMutex_; 113 // The key is profileKey, the value is CharacteristicProfile 114 std::unordered_map<std::string, CharacteristicProfile> staticCharProfileMap_; 115 std::mutex syncListenerMutex_; 116 // The key is procName, the value is syncCallback 117 std::map<std::string, sptr<IRemoteObject>> syncListenerMap_; 118 sptr<IRemoteObject::DeathRecipient> syncListenerDeathRecipient_ = nullptr; 119 std::mutex localUuidMtx_; 120 std::string localUuid_; 121 }; 122 } // namespace DeviceProfile 123 } // namespace OHOS 124 #endif // OHOS_DP_PROFILE_CACHE_H 125