1 /*
2 * Copyright (c) 2021 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 #include "device_profile_adapter.h"
17
18 #include "dm_constants.h"
19 #include "dm_device_state_manager.h"
20 #include "dm_log.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
DeviceProfileAdapter()24 DeviceProfileAdapter::DeviceProfileAdapter()
25 {
26 LOGI("DeviceProfileAdapter construct");
27 }
28
~DeviceProfileAdapter()29 DeviceProfileAdapter::~DeviceProfileAdapter()
30 {
31 LOGI("DeviceProfileAdapter Destructor");
32 }
33
RegisterProfileListener(const std::string & pkgName,const std::string & deviceId,std::shared_ptr<DmDeviceStateManager> callback)34 int32_t DeviceProfileAdapter::RegisterProfileListener(const std::string &pkgName, const std::string &deviceId,
35 std::shared_ptr<DmDeviceStateManager> callback)
36 {
37 if (pkgName.empty() || deviceId.empty() || callback == nullptr) {
38 LOGE("Not a reasonable function argument");
39 return ERR_DM_INPUT_PARA_INVALID;
40 }
41 LOGI("register profile listener with pkgName: %s", pkgName.c_str());
42 std::lock_guard<std::mutex> mutexLock(deviceProfileAdapterMutex_);
43 deviceStateManager_ = callback;
44 if (profileConnector_ == nullptr) {
45 profileConnector_ = std::make_shared<ProfileConnector>();
46 }
47 if (profileConnector_ != nullptr) {
48 profileConnector_->RegisterProfileCallback(pkgName, deviceId, this);
49 }
50 return DM_OK;
51 }
52
UnRegisterProfileListener(const std::string & pkgName)53 int32_t DeviceProfileAdapter::UnRegisterProfileListener(const std::string &pkgName)
54 {
55 if (pkgName.empty()) {
56 LOGE("not a reasonable function argument");
57 return ERR_DM_INPUT_PARA_INVALID;
58 }
59 LOGI("unregister profile listener with pkgName: %s", pkgName.c_str());
60 std::lock_guard<std::mutex> mutexLock(deviceProfileAdapterMutex_);
61 deviceStateManager_ = nullptr;
62 if (profileConnector_ != nullptr) {
63 profileConnector_->UnRegisterProfileCallback(pkgName);
64 }
65 return DM_OK;
66 }
67
OnProfileChanged(const std::string & pkgName,const std::string & deviceId)68 void DeviceProfileAdapter::OnProfileChanged(const std::string &pkgName, const std::string &deviceId)
69 {
70 LOGI("on profile changed with pkgName: %s", pkgName.c_str());
71 std::lock_guard<std::mutex> mutexLock(deviceProfileAdapterMutex_);
72 if (deviceStateManager_ == nullptr) {
73 LOGE("deviceStateManager_ is nullptr");
74 return;
75 }
76 deviceStateManager_->OnProfileReady(pkgName, deviceId);
77 }
78
OnProfileComplete(const std::string & pkgName,const std::string & deviceId)79 void DeviceProfileAdapter::OnProfileComplete(const std::string &pkgName, const std::string &deviceId)
80 {
81 LOGI("on profile complete with pkgName: %s", pkgName.c_str());
82 std::lock_guard<std::mutex> mutexLock(deviceProfileAdapterMutex_);
83 if (deviceStateManager_ == nullptr) {
84 LOGE("deviceStateManager_ is nullptr");
85 return;
86 }
87 deviceStateManager_->OnProfileReady(pkgName, deviceId);
88 }
89
CreateDeviceProfileObject(void)90 extern "C" IProfileAdapter *CreateDeviceProfileObject(void)
91 {
92 return new DeviceProfileAdapter;
93 }
94 } // namespace DistributedHardware
95 } // namespace OHOS
96