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