1 /*
2  * Copyright (c) 2022-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 "distributed_hardware_fwk_kit.h"
17 
18 #include <cinttypes>
19 
20 #include "anonymous_string.h"
21 #include "constants.h"
22 #include "dh_utils_tool.h"
23 #include "dhfwk_sa_manager.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "dh_utils_hisysevent.h"
27 #include "idistributed_hardware.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
DistributedHardwareFwkKit()31 DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false)
32 {
33     DHLOGI("Ctor DistributedHardwareFwkKit");
34     DHFWKSAManager::GetInstance().RegisterSAStateCallback([this](bool isOnLine) { this->OnDHFWKOnLine(isOnLine); });
35     DHFWKSAManager::GetInstance().RegisterAbilityListener();
36 }
37 
~DistributedHardwareFwkKit()38 DistributedHardwareFwkKit::~DistributedHardwareFwkKit()
39 {
40     DHLOGI("Dtor DistributedHardwareFwkKit");
41 }
42 
RegisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)43 int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
44 {
45     DHLOGI("Register publisher listener, topic: %{public}" PRIu32 ", is DHFWK online: %{public}s",
46         (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
47     if (!IsDHTopicValid(topic)) {
48         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
49         return ERR_DH_FWK_PARA_INVALID;
50     }
51 
52     int32_t ret = DH_FWK_SUCCESS;
53     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
54         ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener);
55         DHLOGI("Register publisher listener to DHFWK, ret: %{public}" PRId32, ret);
56         if (ret == DH_FWK_SUCCESS) {
57             return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener);
58         }
59     } else {
60         DHLOGI("DHFWK not online, or get proxy failed, save listener temporary");
61         return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener);
62     }
63 
64     return ret;
65 }
66 
UnregisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)67 int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
68 {
69     DHLOGI("Unregister publisher listener, topic: %{public}" PRIu32 ", is DHFWK online: %{public}s",
70         (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
71     if (!IsDHTopicValid(topic)) {
72         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
73         return ERR_DH_FWK_PARA_INVALID;
74     }
75 
76     int32_t ret = DH_FWK_SUCCESS;
77     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
78         ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener);
79         DHLOGI("Unregister publisher listener to DHFWK, ret: %{public}" PRId32, ret);
80     }
81 
82     DHFWKSAManager::GetInstance().RemovePublisherListenerFromCache(topic, listener);
83     return ret;
84 }
85 
PublishMessage(const DHTopic topic,const std::string & message)86 int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message)
87 {
88     DHLOGI("Publish message, topic: %{public}" PRIu32, (uint32_t)topic);
89     if (!IsDHTopicValid(topic)) {
90         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
91         return ERR_DH_FWK_PARA_INVALID;
92     }
93     if (!IsMessageLengthValid(message)) {
94         return ERR_DH_FWK_PARA_INVALID;
95     }
96 
97     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
98         DHLOGI("DHFWK not online, can not publish message");
99         return ERR_DH_FWK_PUBLISH_MSG_FAILED;
100     }
101 
102     int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message);
103     DHLOGI("Publish message to DHFWK, ret: %{public}" PRId32, ret);
104 
105     return ret;
106 }
107 
IsDHTopicValid(DHTopic topic)108 bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic)
109 {
110     return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX;
111 }
112 
OnDHFWKOnLine(bool isOnLine)113 void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine)
114 {
115     DHLOGI("Receive DHFWK online callback, %{public}s", (isOnLine ? "true" : "false"));
116     isDHFWKOnLine_ = isOnLine;
117 }
118 
IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)119 bool DistributedHardwareFwkKit::IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)
120 {
121     return spec > QueryLocalSysSpecType::MIN && spec < QueryLocalSysSpecType::MAX;
122 }
123 
QueryLocalSysSpec(enum QueryLocalSysSpecType spec)124 std::string DistributedHardwareFwkKit::QueryLocalSysSpec(enum QueryLocalSysSpecType spec)
125 {
126     DHLOGI("Query Local Sys Spec, %{public}d", (uint32_t)spec);
127     if (!IsQueryLocalSysSpecTypeValid(spec)) {
128         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)spec);
129         return "";
130     }
131 
132     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
133         DHLOGI("DHFWK not online, can not publish message");
134         return "";
135     }
136 
137     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->QueryLocalSysSpec(spec);
138 }
139 
InitializeAVCenter(const TransRole & transRole,int32_t & engineId)140 int32_t DistributedHardwareFwkKit::InitializeAVCenter(const TransRole &transRole, int32_t &engineId)
141 {
142     DHLOGI("Initialize av control center, transRole: %{public}" PRIu32, (uint32_t)transRole);
143 
144     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
145         DHLOGI("DHFWK not online or get proxy failed, can not initializeA av control center");
146         return ERR_DH_FWK_POINTER_IS_NULL;
147     }
148 
149     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->InitializeAVCenter(transRole, engineId);
150 }
151 
ReleaseAVCenter(int32_t engineId)152 int32_t DistributedHardwareFwkKit::ReleaseAVCenter(int32_t engineId)
153 {
154     DHLOGI("Release av control center, engineId: %{public}" PRId32, engineId);
155 
156     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
157         DHLOGI("DHFWK not online or get proxy failed, can not release av control center");
158         return ERR_DH_FWK_POINTER_IS_NULL;
159     }
160 
161     DHFWKSAManager::GetInstance().RemoveAVTransControlCenterCbFromCache(engineId);
162     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ReleaseAVCenter(engineId);
163 }
164 
CreateControlChannel(int32_t engineId,const std::string & peerDevId)165 int32_t DistributedHardwareFwkKit::CreateControlChannel(int32_t engineId, const std::string &peerDevId)
166 {
167     DHLOGI("Create av control center channel, engineId: %{public}" PRId32 ", peerDevId=%{public}s.", engineId,
168         GetAnonyString(peerDevId).c_str());
169 
170     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
171         DHLOGI("DHFWK not online or get proxy failed, can not create av control center channel");
172         return ERR_DH_FWK_POINTER_IS_NULL;
173     }
174 
175     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->CreateControlChannel(engineId, peerDevId);
176 }
177 
NotifyAVCenter(int32_t engineId,const AVTransEvent & event)178 int32_t DistributedHardwareFwkKit::NotifyAVCenter(int32_t engineId, const AVTransEvent &event)
179 {
180     DHLOGI("Notify av control center, engineId: %{public}" PRId32 ", event type=%{public}" PRId32, engineId,
181         event.type);
182 
183     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
184         DHLOGI("DHFWK not online or get proxy failed, can not notity av control center event.");
185         return ERR_DH_FWK_POINTER_IS_NULL;
186     }
187 
188     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->NotifyAVCenter(engineId, event);
189 }
190 
RegisterCtlCenterCallback(int32_t engineId,const sptr<IAVTransControlCenterCallback> callback)191 int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId,
192     const sptr<IAVTransControlCenterCallback> callback)
193 {
194     DHLOGI("Register av control center callback. engineId: %{public}" PRId32, engineId);
195 
196     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
197         DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
198         return ERR_DH_FWK_POINTER_IS_NULL;
199     }
200     DHFWKSAManager::GetInstance().AddAVTransControlCenterCbToCache(engineId, callback);
201     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback);
202 }
203 
PauseDistributedHardware(DHType dhType,const std::string & networkId)204 int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const std::string &networkId)
205 {
206     if (!IsIdLengthValid(networkId)) {
207         return ERR_DH_FWK_PARA_INVALID;
208     }
209     DHLOGI("Pause distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
210         GetAnonyString(networkId).c_str());
211 
212     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
213         DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
214         return ERR_DH_FWK_POINTER_IS_NULL;
215     }
216     HiSysEventWriteMsg(DHFWK_INIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
217         "user pause sink ui.");
218     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->PauseDistributedHardware(dhType, networkId);
219 }
220 
ResumeDistributedHardware(DHType dhType,const std::string & networkId)221 int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, const std::string &networkId)
222 {
223     if (!IsIdLengthValid(networkId)) {
224         return ERR_DH_FWK_PARA_INVALID;
225     }
226     DHLOGI("Resume distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
227         GetAnonyString(networkId).c_str());
228 
229     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
230         DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
231         return ERR_DH_FWK_POINTER_IS_NULL;
232     }
233     HiSysEventWriteMsg(DHFWK_INIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
234         "user resume sink ui.");
235     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ResumeDistributedHardware(dhType, networkId);
236 }
237 
StopDistributedHardware(DHType dhType,const std::string & networkId)238 int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const std::string &networkId)
239 {
240     if (!IsIdLengthValid(networkId)) {
241         return ERR_DH_FWK_PARA_INVALID;
242     }
243     DHLOGI("Stop distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
244         GetAnonyString(networkId).c_str());
245 
246     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
247         DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
248         return ERR_DH_FWK_POINTER_IS_NULL;
249     }
250     HiSysEventWriteMsg(DHFWK_EXIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
251         "user stop sink ui.");
252     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId);
253 }
254 } // DistributedHardware
255 } // OHOS