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