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 16 #ifndef OHOS_MEDIALIBRARY_DEVICE_H 17 #define OHOS_MEDIALIBRARY_DEVICE_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 24 #include "device_manager.h" 25 #include "device_manager_callback.h" 26 #include "devices_info_interact.h" 27 #include "distributed_kv_data_manager.h" 28 #include "event_handler.h" 29 #include "medialibrary_db_const.h" 30 #include "medialibrary_device_info.h" 31 #include "medialibrary_device_operations.h" 32 #include "rdb_errno.h" 33 #include "rdb_helper.h" 34 #include "single_kvstore.h" 35 36 namespace OHOS { 37 namespace Media { 38 using namespace OHOS::NativeRdb; 39 static constexpr int DEFAULT_DEV_SECURITY_LEVEL = 1; 40 class MediaLibraryDevice : public DistributedHardware::DeviceStateCallback, 41 public DistributedHardware::DmInitCallback, 42 public std::enable_shared_from_this<MediaLibraryDevice> { 43 public: 44 virtual ~MediaLibraryDevice(); 45 MediaLibraryDevice(const MediaLibraryDevice&) = delete; 46 MediaLibraryDevice(MediaLibraryDevice&&) = delete; 47 MediaLibraryDevice& operator=(const MediaLibraryDevice&) = delete; 48 MediaLibraryDevice& operator=(MediaLibraryDevice&&) = delete; 49 static std::shared_ptr<MediaLibraryDevice> GetInstance(); 50 void Start(); 51 void Stop(); 52 void OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; 53 void OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; 54 void OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; 55 void OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo) override; 56 void OnRemoteDied() override; 57 58 void GetAllNetworkId(std::vector<OHOS::DistributedHardware::DmDeviceInfo> &deviceList); 59 bool InitDeviceRdbStore(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore); 60 bool InitDeviceKvStore(const std::shared_ptr<DistributedKv::SingleKvStore> &kvStore); 61 void NotifyDeviceChange(); 62 void NotifyRemoteFileChange(); 63 bool UpdateDeviceSyncStatus(const std::string &networkId, const std::string &tableName, int32_t syncStatus); 64 bool GetDeviceSyncStatus(const std::string &networkId, const std::string &tableName, int32_t &syncStatus); 65 bool GetDeviceIdByNetworkId(const std::string &networkId, std::string &deviceId); 66 std::string GetNetworkIdBySelfId(const std::string &selfId); 67 std::string GetUdidByNetworkId(std::string &networkId); 68 void OnSyncCompleted(const std::string &devId, const DistributedKv::Status staus); 69 void OnGetDevSecLevel(const std::string &udid, const int32_t level); 70 void GetDeviceInfoMap(std::unordered_map<std::string, OHOS::Media::MediaLibraryDeviceInfo> &outDeviceMap); 71 bool QueryAgingDeviceInfos(std::vector<MediaLibraryDeviceInfo> &outDeviceInfos); 72 bool QueryAllDeviceUdid(vector<string> &deviceUdids); 73 bool DeleteDeviceInfo(const std::string &udid); 74 MediaLibraryDevice(); 75 76 void GetMediaLibraryDeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &dmInfo, 77 OHOS::Media::MediaLibraryDeviceInfo& mlInfo); 78 bool QueryDeviceTable(); 79 void ClearAllDevices(); 80 bool IsHasDevice(const std::string &deviceUdid); 81 void RegisterToDM(); 82 void UnRegisterFromDM(); 83 void DevOnlineProcess(const DistributedHardware::DmDeviceInfo &devInfo); 84 void TryToGetTargetDevMLInfos(const std::string &udid, const std::string &networkId); 85 bool IsHasActiveDevice(); 86 87 private: 88 static constexpr int SHORT_UDID_LEN = 8; 89 static constexpr int RANDOM_NUM = 999; 90 91 static std::shared_ptr<MediaLibraryDevice> mlDMInstance_; 92 std::shared_ptr<AppExecFwk::EventHandler> deviceHandler_; 93 std::mutex devMtx_; 94 std::unordered_map<std::string, OHOS::Media::MediaLibraryDeviceInfo> deviceInfoMap_; 95 std::map<std::string, std::set<int>> excludeMap_; 96 std::shared_ptr<MediaLibraryRdbStore> rdbStore_; 97 std::shared_ptr<DistributedKv::SingleKvStore> kvStore_; 98 std::shared_ptr<DevicesInfoInteract> devsInfoInter_; 99 std::string bundleName_; 100 std::mutex cvMtx_; 101 std::condition_variable kvSyncDoneCv_; 102 std::string localUdid_; 103 int32_t localDevLev_ {DEFAULT_DEV_SECURITY_LEVEL}; 104 std::atomic<bool> localSecLevelGot_ {false}; 105 std::mutex gotSecLevelMtx_; 106 std::condition_variable localSecLevelDoneCv_; 107 volatile bool isStart = false; 108 }; 109 } // namespace Media 110 } // namespace OHOS 111 #endif // OHOS_MEDIALIBRARY_DEVICE_H 112