• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "distributed_hardware_service.h"
17 
18 #include <cinttypes>
19 
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "iservice_registry.h"
24 #include "nlohmann/json.hpp"
25 #include "string_ex.h"
26 #include "system_ability_definition.h"
27 
28 #include "access_manager.h"
29 #include "av_trans_control_center.h"
30 #include "capability_info_manager.h"
31 #include "dh_context.h"
32 #include "dh_utils_tool.h"
33 #include "dh_utils_hisysevent.h"
34 #include "distributed_hardware_fwk_kit_paras.h"
35 #include "distributed_hardware_errno.h"
36 #include "distributed_hardware_log.h"
37 #include "distributed_hardware_manager_factory.h"
38 #include "publisher.h"
39 
40 namespace OHOS {
41 namespace DistributedHardware {
42 REGISTER_SYSTEM_ABILITY_BY_ID(DistributedHardwareService, DISTRIBUTED_HARDWARE_SA_ID, true);
43 
DistributedHardwareService(int32_t saId,bool runOnCreate)44 DistributedHardwareService::DistributedHardwareService(int32_t saId, bool runOnCreate)
45     : SystemAbility(saId, runOnCreate)
46 {
47 }
48 
OnStart()49 void DistributedHardwareService::OnStart()
50 {
51     DHLOGI("DistributedHardwareService::OnStart start");
52     HiSysEventWriteMsg(DHFWK_INIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
53         "dhfwk sa start on demand.");
54 
55     if (state_ == ServiceRunningState::STATE_RUNNING) {
56         DHLOGI("DistributedHardwareService has already started.");
57         return;
58     }
59     if (!Init()) {
60         DHLOGE("failed to init DistributedHardwareService");
61         return;
62     }
63     state_ = ServiceRunningState::STATE_RUNNING;
64     DHLOGI("DistributedHardwareService::OnStart start service success.");
65 }
66 
Init()67 bool DistributedHardwareService::Init()
68 {
69     DHLOGI("DistributedHardwareService::Init ready to init.");
70     if (!registerToService_) {
71         bool ret = Publish(this);
72         if (!ret) {
73             DHLOGE("DistributedHardwareService::Init Publish failed!");
74             HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
75                 "dhfwk sa init publish failed.");
76             return false;
77         }
78         registerToService_ = true;
79     }
80     auto ret = AccessManager::GetInstance()->Init();
81     if (ret != DH_FWK_SUCCESS) {
82         DHLOGE("DistributedHardwareService::Init failed.");
83         HiSysEventWriteErrCodeMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
84             ret, "dhfwk sa AccessManager init fail.");
85         return false;
86     }
87     DHLOGI("DistributedHardwareService::Init init success.");
88     HiSysEventWriteMsg(DHFWK_INIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
89         "dhfwk sa init success.");
90     return true;
91 }
92 
OnStop()93 void DistributedHardwareService::OnStop()
94 {
95     DHLOGI("DistributedHardwareService::OnStop ready to stop service.");
96     state_ = ServiceRunningState::STATE_NOT_START;
97     registerToService_ = false;
98 }
99 
RegisterPublisherListener(const DHTopic topic,const sptr<IPublisherListener> & listener)100 int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic,
101     const sptr<IPublisherListener> &listener)
102 {
103     Publisher::GetInstance().RegisterListener(topic, listener);
104     return DH_FWK_SUCCESS;
105 }
106 
UnregisterPublisherListener(const DHTopic topic,const sptr<IPublisherListener> & listener)107 int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic,
108     const sptr<IPublisherListener> &listener)
109 {
110     Publisher::GetInstance().UnregisterListener(topic, listener);
111     return DH_FWK_SUCCESS;
112 }
113 
PublishMessage(const DHTopic topic,const std::string & msg)114 int32_t DistributedHardwareService::PublishMessage(const DHTopic topic, const std::string &msg)
115 {
116     Publisher::GetInstance().PublishMessage(topic, msg);
117     return DH_FWK_SUCCESS;
118 }
119 
QueryLocalSysSpec(const QueryLocalSysSpecType spec)120 std::string DistributedHardwareService::QueryLocalSysSpec(const QueryLocalSysSpecType spec)
121 {
122     DeviceInfo localDevInfo = DHContext::GetInstance().GetDeviceInfo();
123     std::vector<std::shared_ptr<CapabilityInfo>> resInfos;
124     CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(localDevInfo.deviceId, resInfos);
125     DHType targetDhType = DHType::UNKNOWN;
126     std::string targetKey = "";
127     switch (spec) {
128         case QueryLocalSysSpecType::HISTREAMER_AUDIO_ENCODER:
129             targetKey = KEY_HISTREAMER_AUDIO_ENCODER;
130             targetDhType = DHType::AUDIO;
131             break;
132         case QueryLocalSysSpecType::HISTREAMER_AUDIO_DECODER:
133             targetKey = KEY_HISTREAMER_AUDIO_DECODER;
134             targetDhType = DHType::AUDIO;
135             break;
136         case QueryLocalSysSpecType::HISTREAMER_VIDEO_ENCODER:
137             targetKey = KEY_HISTREAMER_VIDEO_ENCODER;
138             targetDhType = DHType::SCREEN;
139             break;
140         case QueryLocalSysSpecType::HISTREAMER_VIDEO_DECODER:
141             targetKey = KEY_HISTREAMER_VIDEO_DECODER;
142             targetDhType = DHType::SCREEN;
143             break;
144         default:
145             break;
146     }
147 
148     DHLOGE("QueryLocalSysSpec targetKey: %s, targetDhType: %" PRIu32, targetKey.c_str(), (uint32_t)targetDhType);
149     if (targetDhType == DHType::UNKNOWN) {
150         DHLOGE("Can not find matched dhtype");
151         return "";
152     }
153 
154     std::string attrs = "";
155     for (const auto &cap : resInfos) {
156         if (cap->GetDHType() != targetDhType) {
157             continue;
158         }
159         attrs = cap->GetDHAttrs();
160         break;
161     }
162     if (attrs.empty()) {
163         DHLOGE("Can not find dh attrs");
164         return "";
165     }
166 
167     return QueryDhSysSpec(targetKey, attrs);
168 }
169 
QueryDhSysSpec(const std::string & targetKey,std::string & attrs)170 std::string DistributedHardwareService::QueryDhSysSpec(const std::string &targetKey, std::string &attrs)
171 {
172     nlohmann::json attrJson = nlohmann::json::parse(attrs, nullptr, false);
173     if (attrJson.is_discarded()) {
174         DHLOGE("attrs json is invalid, attrs: %s", attrs.c_str());
175         return "";
176     }
177 
178     if (!IsString(attrJson, targetKey)) {
179         DHLOGE("Attrs Json not contains key: %s", targetKey.c_str());
180         return "";
181     }
182     return attrJson.at(targetKey).get<std::string>();
183 }
184 
InitializeAVCenter(const TransRole & transRole,int32_t & engineId)185 int32_t DistributedHardwareService::InitializeAVCenter(const TransRole &transRole, int32_t &engineId)
186 {
187     return AVTransControlCenter::GetInstance().InitializeAVCenter(transRole, engineId);
188 }
189 
ReleaseAVCenter(int32_t engineId)190 int32_t DistributedHardwareService::ReleaseAVCenter(int32_t engineId)
191 {
192     return AVTransControlCenter::GetInstance().ReleaseAVCenter(engineId);
193 }
194 
CreateControlChannel(int32_t engineId,const std::string & peerDevId)195 int32_t DistributedHardwareService::CreateControlChannel(int32_t engineId, const std::string &peerDevId)
196 {
197     return AVTransControlCenter::GetInstance().CreateControlChannel(engineId, peerDevId);
198 }
199 
NotifyAVCenter(int32_t engineId,const AVTransEvent & event)200 int32_t DistributedHardwareService::NotifyAVCenter(int32_t engineId, const AVTransEvent &event)
201 {
202     return AVTransControlCenter::GetInstance().NotifyAVCenter(engineId, event);
203 }
204 
RegisterCtlCenterCallback(int32_t engineId,const sptr<IAVTransControlCenterCallback> & callback)205 int32_t DistributedHardwareService::RegisterCtlCenterCallback(int32_t engineId,
206     const sptr<IAVTransControlCenterCallback> &callback)
207 {
208     return AVTransControlCenter::GetInstance().RegisterCtlCenterCallback(engineId, callback);
209 }
210 
Dump(int32_t fd,const std::vector<std::u16string> & args)211 int DistributedHardwareService::Dump(int32_t fd, const std::vector<std::u16string>& args)
212 {
213     DHLOGI("DistributedHardwareService  Dump.");
214 
215     std::vector<std::string> argsStr {};
216     for (auto item : args) {
217         argsStr.emplace_back(Str16ToStr8(item));
218     }
219 
220     std::string result("");
221     int ret = AccessManager::GetInstance()->Dump(argsStr, result);
222     if (ret != DH_FWK_SUCCESS) {
223         DHLOGE("Dump error, ret = %d", ret);
224     }
225 
226     if (dprintf(fd, "%s\n", result.c_str()) < 0) {
227         DHLOGE("Hidump dprintf error");
228         ret = ERR_DH_FWK_HIDUMP_DPRINTF_ERROR;
229     }
230 
231     return ret;
232 }
233 } // namespace DistributedHardware
234 } // namespace OHOS
235