• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_enable.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 ENABLE_TIMEOUT_MS = 1000;
28 }
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "ComponentEnable"
31 
ComponentEnable()32 ComponentEnable::ComponentEnable() : status_(std::numeric_limits<int32_t>::max()) {}
33 
~ComponentEnable()34 ComponentEnable::~ComponentEnable() {}
35 
Enable(const std::string & networkId,const std::string & dhId,const EnableParam & param,IDistributedHardwareSource * handler)36 int32_t ComponentEnable::Enable(const std::string &networkId, const std::string &dhId, const EnableParam &param,
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->RegisterDistributedHardware(networkId, dhId, param, shared_from_this());
51     if (ret != DH_FWK_SUCCESS) {
52         DHLOGE("RegisterDistributedHardware failed, networkId = %{public}s dhId = %{public}s.",
53             GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
54         HiSysEventWriteCompMgrFailedMsg(DHFWK_DH_REGISTER_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
55             GetAnonyString(dhId), ret, "dhfwk register distributed hardware failed.");
56         return ERR_DH_FWK_COMPONENT_REGISTER_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(ENABLE_TIMEOUT_MS),
62         [this]() { return status_ != std::numeric_limits<int32_t>::max(); });
63     if (!waitStatus) {
64         DHLOGE("enable timeout, networkId = %{public}s dhId = %{public}s", GetAnonyString(networkId).c_str(),
65             GetAnonyString(dhId).c_str());
66         HiSysEventWriteCompMgrFailedMsg(DHFWK_DH_REGISTER_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
67             GetAnonyString(dhId), ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT,
68             "dhfwk distributed hardware enable timeout.");
69         return ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT;
70     }
71     return (status_ == DH_FWK_SUCCESS) ? DH_FWK_SUCCESS : ERR_DH_FWK_COMPONENT_ENABLE_FAILED;
72 }
73 
OnRegisterResult(const std::string & networkId,const std::string & dhId,int32_t status,const std::string & data)74 int32_t ComponentEnable::OnRegisterResult(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("enable success, networkId = %{public}s, dhId = %{public}s.", GetAnonyString(networkId).c_str(),
82             GetAnonyString(dhId).c_str());
83     } else {
84         DHLOGE("enable 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