1 /*
2  * Copyright (c) 2021-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 "component_disable.h"
17 
18 #include "anonymous_string.h"
19 #include "constants.h"
20 #include "dh_utils_hisysevent.h"
21 #include "dh_utils_tool.h"
22 #include "distributed_hardware_errno.h"
23 #include "distributed_hardware_log.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 #undef DH_LOG_TAG
28 #define DH_LOG_TAG "ComponentDisable"
29 
ComponentDisable()30 ComponentDisable::ComponentDisable() : status_(std::numeric_limits<int32_t>::max()) {}
31 
~ComponentDisable()32 ComponentDisable::~ComponentDisable() {}
33 
Disable(const std::string & networkId,const std::string & dhId,IDistributedHardwareSource * handler)34 int32_t ComponentDisable::Disable(const std::string &networkId, const std::string &dhId,
35     IDistributedHardwareSource *handler)
36 {
37     if (!IsIdLengthValid(networkId) || !IsIdLengthValid(dhId)) {
38         return ERR_DH_FWK_PARA_INVALID;
39     }
40     DHLOGD("networkId = %{public}s dhId = %{public}s.", GetAnonyString(networkId).c_str(),
41         GetAnonyString(dhId).c_str());
42     if (handler == nullptr) {
43         DHLOGE("handler is null, networkId = %{public}s dhId = %{public}s.", GetAnonyString(networkId).c_str(),
44             GetAnonyString(dhId).c_str());
45         return ERR_DH_FWK_PARA_INVALID;
46     }
47 
48     auto ret = handler->UnregisterDistributedHardware(networkId, dhId, shared_from_this());
49     if (ret != DH_FWK_SUCCESS) {
50         DHLOGE("UnregisterDistributedHardware failed, networkId = %{public}s dhId = %{public}s.",
51             GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
52         HiSysEventWriteCompMgrFailedMsg(DHFWK_DH_UNREGISTER_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
53             GetAnonyString(dhId), ret, "dhfwk unregister distributed hardware failed.");
54         return ERR_DH_FWK_COMPONENT_UNREGISTER_FAILED;
55     }
56 
57     // wait for callback until timeout
58     std::unique_lock<std::mutex> lock(mutex_);
59     auto waitStatus = conVar_.wait_for(lock, std::chrono::milliseconds(DISABLE_TIMEOUT_MS),
60         [this]() { return status_ != std::numeric_limits<int32_t>::max(); });
61     if (!waitStatus) {
62         DHLOGE("disable timeout, networkId = %{public}s dhId = %{public}s.", GetAnonyString(networkId).c_str(),
63             GetAnonyString(dhId).c_str());
64         HiSysEventWriteCompMgrFailedMsg(DHFWK_DH_UNREGISTER_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
65             GetAnonyString(dhId), ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT,
66             "dhfwk distributed hardware disable timeout.");
67         return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT;
68     }
69     return (status_ == DH_FWK_SUCCESS) ? DH_FWK_SUCCESS : ERR_DH_FWK_COMPONENT_DISABLE_FAILED;
70 }
71 
OnUnregisterResult(const std::string & networkId,const std::string & dhId,int32_t status,const std::string & data)72 int32_t ComponentDisable::OnUnregisterResult(const std::string &networkId, const std::string &dhId, int32_t status,
73     const std::string &data)
74 {
75     if (!IsIdLengthValid(networkId) || !IsIdLengthValid(dhId)) {
76         return ERR_DH_FWK_PARA_INVALID;
77     }
78     if (status == DH_FWK_SUCCESS) {
79         DHLOGI("disable success, networkId = %{public}s, dhId = %{public}s.", GetAnonyString(networkId).c_str(),
80             GetAnonyString(dhId).c_str());
81     } else {
82         DHLOGE("disable failed, networkId = %{public}s, dhId = %{public}s, status = %{public}d.",
83             GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), status);
84     }
85 
86     std::unique_lock<std::mutex> lock(mutex_);
87     status_ = status;
88     conVar_.notify_all();
89     return status_;
90 }
91 } // namespace DistributedHardware
92 } // namespace OHOS
93