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