• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "distributed_hardware_fwk_kit.h"
17 
18 #include <cinttypes>
19 
20 #include "anonymous_string.h"
21 #include "constants.h"
22 #include "dhfwk_sa_manager.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "idistributed_hardware.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
DistributedHardwareFwkKit()29 DistributedHardwareFwkKit::DistributedHardwareFwkKit() : listenerMap_({}), isDHFWKOnLine_(false)
30 {
31     DHLOGI("Ctor DistributedHardwareFwkKit");
32     DHFWKSAManager::GetInstance().RegisterSAStateCallback(
33         std::bind(&DistributedHardwareFwkKit::OnDHFWKOnLine, this, std::placeholders::_1));
34     DHFWKSAManager::GetInstance().RegisterAbilityListener();
35 }
36 
~DistributedHardwareFwkKit()37 DistributedHardwareFwkKit::~DistributedHardwareFwkKit()
38 {
39     DHLOGI("Dtor DistributedHardwareFwkKit");
40 }
41 
RegisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)42 int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
43 {
44     DHLOGI("Register publisher listener, topic: %" PRIu32 ", is DHFWK online: %s",
45         (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
46     if (!IsDHTopicValid(topic)) {
47         DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic);
48         return ERR_DH_FWK_PARA_INVALID;
49     }
50 
51     int32_t ret = DH_FWK_SUCCESS;
52     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
53         ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener);
54         DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret);
55     } else {
56         DHLOGI("DHFWK not online, or get proxy failed, save listener temporary");
57         std::lock_guard<std::mutex> lock(listenerMutex_);
58         if (listenerMap_.size() >= MAX_TOPIC_SIZE || listenerMap_[topic].size() >= MAX_LISTENER_SIZE) {
59             DHLOGE("listeners are over size!");
60             return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE;
61         }
62         listenerMap_[topic].insert(listener);
63     }
64 
65     return ret;
66 }
67 
UnregisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)68 int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
69 {
70     DHLOGI("Unregister publisher listener, topic: %" PRIu32 ", is DHFWK online: %s",
71         (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
72     if (!IsDHTopicValid(topic)) {
73         DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic);
74         return ERR_DH_FWK_PARA_INVALID;
75     }
76 
77     int32_t ret = DH_FWK_SUCCESS;
78     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
79         ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener);
80         DHLOGI("Unregister publisher listener to DHFWK, ret: %" PRId32, ret);
81     }
82     std::lock_guard<std::mutex> lock(listenerMutex_);
83     listenerMap_[topic].erase(listener);
84 
85     return ret;
86 }
87 
PublishMessage(const DHTopic topic,const std::string & message)88 int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message)
89 {
90     DHLOGI("Publish message, topic: %" PRIu32, (uint32_t)topic);
91     if (!IsDHTopicValid(topic)) {
92         DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic);
93         return ERR_DH_FWK_PARA_INVALID;
94     }
95     if (message.empty() || message.size() > MAX_MESSAGE_LEN) {
96         DHLOGE("Message size is invalid!");
97         return ERR_DH_FWK_PARA_INVALID;
98     }
99 
100     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
101         DHLOGI("DHFWK not online, can not publish message");
102         return ERR_DH_FWK_PUBLISH_MSG_FAILED;
103     }
104 
105     int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message);
106     DHLOGI("Publish message to DHFWK, ret: %" PRId32, ret);
107 
108     return ret;
109 }
110 
IsDHTopicValid(DHTopic topic)111 bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic)
112 {
113     return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX;
114 }
115 
OnDHFWKOnLine(bool isOnLine)116 void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine)
117 {
118     DHLOGI("Receive DHFWK online callback, %s", (isOnLine ? "true" : "false"));
119     isDHFWKOnLine_ = isOnLine;
120 
121     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
122         return;
123     }
124 
125     DHLOGI("DHFWK online, register saved listener to it");
126     std::unordered_map<DHTopic, std::set<sptr<IPublisherListener>>> regSuccListeners;
127     std::lock_guard<std::mutex> lock(listenerMutex_);
128     for (const auto &entry : listenerMap_) {
129         for (const auto &listener : entry.second) {
130             int32_t ret =
131                 DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener);
132             if (ret == DH_FWK_SUCCESS) {
133                 regSuccListeners[entry.first].insert(listener);
134             }
135         }
136     }
137 
138     for (const auto &succ : regSuccListeners) {
139         for (const auto &listener : succ.second) {
140             listenerMap_[succ.first].erase(listener);
141         }
142     }
143 }
144 
IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)145 bool DistributedHardwareFwkKit::IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)
146 {
147     return spec > QueryLocalSysSpecType::MIN && spec < QueryLocalSysSpecType::MAX;
148 }
149 
QueryLocalSysSpec(enum QueryLocalSysSpecType spec)150 std::string DistributedHardwareFwkKit::QueryLocalSysSpec(enum QueryLocalSysSpecType spec)
151 {
152     DHLOGI("Query Local Sys Spec, %d", (uint32_t)spec);
153     if (!IsQueryLocalSysSpecTypeValid(spec)) {
154         DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)spec);
155         return "";
156     }
157 
158     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
159         DHLOGI("DHFWK not online, can not publish message");
160         return "";
161     }
162 
163     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->QueryLocalSysSpec(spec);
164 }
165 
InitializeAVCenter(const TransRole & transRole,int32_t & engineId)166 int32_t DistributedHardwareFwkKit::InitializeAVCenter(const TransRole &transRole, int32_t &engineId)
167 {
168     DHLOGI("Initialize av control center, transRole: %" PRIu32, (uint32_t)transRole);
169 
170     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
171         DHLOGI("DHFWK not online or get proxy failed, can not initializeA av control center");
172         return ERR_DH_FWK_POINTER_IS_NULL;
173     }
174 
175     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->InitializeAVCenter(transRole, engineId);
176 }
177 
ReleaseAVCenter(int32_t engineId)178 int32_t DistributedHardwareFwkKit::ReleaseAVCenter(int32_t engineId)
179 {
180     DHLOGI("Release av control center, engineId: %" PRId32, engineId);
181 
182     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
183         DHLOGI("DHFWK not online or get proxy failed, can not release av control center");
184         return ERR_DH_FWK_POINTER_IS_NULL;
185     }
186 
187     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ReleaseAVCenter(engineId);
188 }
189 
CreateControlChannel(int32_t engineId,const std::string & peerDevId)190 int32_t DistributedHardwareFwkKit::CreateControlChannel(int32_t engineId, const std::string &peerDevId)
191 {
192     DHLOGI("Create av control center channel, engineId: %" PRId32 ", peerDevId=%s.", engineId,
193         GetAnonyString(peerDevId).c_str());
194 
195     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
196         DHLOGI("DHFWK not online or get proxy failed, can not create av control center channel");
197         return ERR_DH_FWK_POINTER_IS_NULL;
198     }
199 
200     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->CreateControlChannel(engineId, peerDevId);
201 }
202 
NotifyAVCenter(int32_t engineId,const AVTransEvent & event)203 int32_t DistributedHardwareFwkKit::NotifyAVCenter(int32_t engineId, const AVTransEvent &event)
204 {
205     DHLOGI("Notify av control center, engineId: %" PRId32 ", event type=%" PRId32, engineId, event.type);
206 
207     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
208         DHLOGI("DHFWK not online or get proxy failed, can not notity av control center event.");
209         return ERR_DH_FWK_POINTER_IS_NULL;
210     }
211 
212     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->NotifyAVCenter(engineId, event);
213 }
214 
RegisterCtlCenterCallback(int32_t engineId,const sptr<IAVTransControlCenterCallback> & callback)215 int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId,
216     const sptr<IAVTransControlCenterCallback> &callback)
217 {
218     DHLOGI("Register av control center callback. engineId: %" PRId32, engineId);
219 
220     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
221         DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
222         return ERR_DH_FWK_POINTER_IS_NULL;
223     }
224 
225     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback);
226 }
227 } // DistributedHardware
228 } // OHOS