1 /* 2 * Copyright (c) 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_DEVICE_PROFILE_MANAGER_H 17 #define OHOS_DP_DEVICE_PROFILE_MANAGER_H 18 19 #include <atomic> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <vector> 24 25 #include "dm_device_info.h" 26 #include "iremote_object.h" 27 28 #include "characteristic_profile.h" 29 #include "device_profile.h" 30 #include "dp_sync_options.h" 31 #include "i_dp_sync_adapter.h" 32 #include "i_sync_completed_callback.h" 33 #include "ikv_adapter.h" 34 #include "kv_data_change_listener.h" 35 #include "kv_store_death_recipient.h" 36 #include "kv_sync_completed_listener.h" 37 #include "service_profile.h" 38 #include "single_instance.h" 39 40 namespace OHOS { 41 namespace DistributedDeviceProfile { 42 class DeviceProfileManager { 43 DECLARE_SINGLE_INSTANCE(DeviceProfileManager); 44 45 public: 46 int32_t Init(); 47 int32_t UnInit(); 48 int32_t ReInit(); 49 int32_t PutDeviceProfile(const DeviceProfile& deviceProfile); 50 int32_t PutServiceProfile(const ServiceProfile& serviceProfile); 51 int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles); 52 int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile); 53 int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& charProfiles); 54 int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); 55 int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, 56 ServiceProfile& serviceProfile); 57 int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 58 const std::string& characteristicKey, CharacteristicProfile& charProfile); 59 int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName); 60 int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 61 const std::string& characteristicKey); 62 int32_t GetAllDeviceProfile(std::vector<DeviceProfile>& deviceProfiles); 63 int32_t GetAllServiceProfile(std::vector<ServiceProfile>& serviceProfiles); 64 int32_t GetAllCharacteristicProfile(std::vector<CharacteristicProfile>& charProfiles); 65 int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, 66 sptr<IRemoteObject> syncCompletedCallback); 67 std::vector<DistributedKv::Entry> GetEntriesByKeys(const std::vector<std::string>& keys); 68 int32_t SavePutTempCache(std::map<std::string, std::string>& entries); 69 bool IsFirstInitDB(); 70 void ResetFirst(); 71 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo); 72 73 private: 74 bool LoadDpSyncAdapter(); 75 void UnloadDpSyncAdapter(); 76 int32_t RunloadedFunction(const std::string& deviceId, sptr<IRemoteObject> syncCompletedCallback); 77 int32_t SyncWithNotOHBasedDevice(const std::vector<std::string>& notOHBasedDevices, 78 const std::string& callerDescriptor, sptr<IRemoteObject> syncCompletedCallback); 79 void SyncWithNotOHBasedDeviceFailed(const std::vector<std::string>& notOHBasedDevices, 80 sptr<IRemoteObject> syncCompletedCallback); 81 void AddToPutTempCache(const std::map<std::string, std::string>& values); 82 void FixDataOnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo); 83 int32_t DeleteBatchByKeys(const std::vector<std::string>& delKeys); 84 int32_t GetProfilesByOwner(const std::string& uuid, std::map<std::string, std::string>& entries); 85 int32_t GetProfilesByKeyPrefix(const std::string& udid, std::map<std::string, std::string>& entries); 86 // Clean data that does not belong to the local. 87 void FixLocalData(const std::string& localUdid, const std::map<std::string, std::string>& localDataByOwner); 88 // Clean ohbase data when the peer is non-ohbase 89 void FixRemoteDataWhenPeerIsNonOH(const std::string& remoteUdid); 90 // Clean non-ohbase data when the peer is ohbase 91 void FixRemoteDataWhenPeerIsOHBase(const std::string& remoteUdid, 92 const std::map<std::string, std::string>& localDataByOwner); 93 void NotifyNotOHBaseOnline(const DistributedHardware::DmDeviceInfo deviceInfo); 94 void E2ESyncDynamicProfile(const DistributedHardware::DmDeviceInfo deviceInfo); 95 bool isAdapterSoLoaded_ = false; 96 std::mutex isAdapterLoadLock_; 97 std::mutex dynamicStoreMutex_; 98 std::shared_ptr<IKVAdapter> deviceProfileStore_ = nullptr; 99 std::shared_ptr<IDPSyncAdapter> dpSyncAdapter_; 100 std::atomic<bool> isFirst_{true}; 101 std::mutex putTempCacheMutex_; 102 std::map<std::string, std::string> putTempCache_; 103 }; 104 } // namespace DeviceProfile 105 } // namespace OHOS 106 #endif // OHOS_DP_DEVICE_PROFILE_MANAGER_H 107