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