• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 GetAllDeviceId(std::vector<OHOS::DistributedHardware::DmDeviceInfo> &deviceList);
59     bool InitDeviceRdbStore(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore);
60     void NotifyDeviceChange();
61     void NotifyRemoteFileChange();
62     bool UpdateDeviceSyncStatus(const std::string &networkId, int32_t syncStatus);
63     bool GetDevicieSyncStatus(const std::string &networkId, int32_t &syncStatus);
64     std::string GetNetworkIdBySelfId(const std::string &selfId);
65     std::string GetUdidByNetworkId(std::string &networkId);
66     void OnSyncCompleted(const std::string &devId, const DistributedKv::Status staus);
67     void OnGetDevSecLevel(const std::string &udid, const int32_t level);
68     void GetDeviceInfoMap(std::unordered_map<std::string, OHOS::Media::MediaLibraryDeviceInfo> &outDeviceMap);
69     bool QueryAgingDeviceInfos(std::vector<MediaLibraryDeviceInfo> &outDeviceInfos);
70     bool QueryAllDeviceUdid(vector<string> &deviceUdids);
71     bool DeleteDeviceInfo(const std::string &udid);
72     MediaLibraryDevice();
73 
74     void GetMediaLibraryDeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &dmInfo,
75                                    OHOS::Media::MediaLibraryDeviceInfo& mlInfo);
76     bool QueryDeviceTable();
77     void ClearAllDevices();
78     bool IsHasDevice(const std::string &deviceUdid);
79     void RegisterToDM();
80     void UnRegisterFromDM();
81     void DevOnlineProcess(const DistributedHardware::DmDeviceInfo &devInfo);
82     void TryToGetTargetDevMLInfos(const std::string &udid, const std::string &networkId);
83 private:
84     static constexpr int SHORT_UDID_LEN = 8;
85     static constexpr int RANDOM_NUM = 999;
86 
87     static std::shared_ptr<MediaLibraryDevice> mlDMInstance_;
88     std::shared_ptr<AppExecFwk::EventHandler> deviceHandler_;
89     std::mutex devMtx_;
90     std::unordered_map<std::string, OHOS::Media::MediaLibraryDeviceInfo> deviceInfoMap_;
91     std::map<std::string, std::set<int>> excludeMap_;
92     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
93     std::shared_ptr<DevicesInfoInteract> devsInfoInter_;
94     std::string bundleName_;
95     std::mutex cvMtx_;
96     std::condition_variable kvSyncDoneCv_;
97     std::string localUdid_;
98     int32_t localDevLev_ {DEFAULT_DEV_SECURITY_LEVEL};
99     std::atomic<bool> localSecLevelGot_ {false};
100     std::mutex gotSecLevelMtx_;
101     std::condition_variable localSecLevelDoneCv_;
102     volatile bool isStart = false;
103 };
104 } // namespace Media
105 } // namespace OHOS
106 #endif // OHOS_MEDIALIBRARY_DEVICE_H
107