• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "plugin_listener_impl.h"
17 
18 #include "anonymous_string.h"
19 #include "capability_info.h"
20 #include "capability_info_manager.h"
21 #include "constants.h"
22 #include "dh_context.h"
23 #include "dh_utils_tool.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "publisher.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "PluginListenerImpl"
32 
PluginHardware(const std::string & dhId,const std::string & attrs,const std::string & subtype)33 void PluginListenerImpl::PluginHardware(const std::string &dhId, const std::string &attrs, const std::string &subtype)
34 {
35     if (!IsIdLengthValid(dhId) || !IsMessageLengthValid(attrs)) {
36         return;
37     }
38     DHLOGI("plugin start, dhId: %{public}s", GetAnonyString(dhId).c_str());
39     std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
40     std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
41     std::string devName = DHContext::GetInstance().GetDeviceInfo().deviceName;
42     uint16_t devType = DHContext::GetInstance().GetDeviceInfo().deviceType;
43     std::shared_ptr<CapabilityInfo> dhCapabilityInfo =
44         std::make_shared<CapabilityInfo>(dhId, deviceId, devName, devType, dhType_, attrs, subtype);
45     capabilityInfos.push_back(dhCapabilityInfo);
46 
47     CapabilityInfoManager::GetInstance()->AddCapability(capabilityInfos);
48     Publisher::GetInstance().PublishMessage(DHTopic::TOPIC_PHY_DEV_PLUGIN, dhId);
49     DHLOGI("plugin end, dhId: %{public}s", GetAnonyString(dhId).c_str());
50 }
51 
UnPluginHardware(const std::string & dhId)52 void PluginListenerImpl::UnPluginHardware(const std::string &dhId)
53 {
54     if (!IsIdLengthValid(dhId)) {
55         return;
56     }
57     DHLOGI("unplugin start, dhId: %{public}s", GetAnonyString(dhId).c_str());
58     std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
59     std::shared_ptr<CapabilityInfo> capability = nullptr;
60     auto ret = CapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capability);
61     if ((ret != DH_FWK_SUCCESS) || (capability == nullptr)) {
62         DHLOGE("GetCapability failed, deviceId =%{public}s, dhId = %{public}s, errCode = %{public}d",
63             GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str(), ret);
64         return;
65     }
66     CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capability->GetKey());
67     DHLOGI("unplugin end, dhId: %{public}s", GetAnonyString(dhId).c_str());
68 }
69 } // namespace DistributedHardware
70 } // namespace OHOS
71