1 /*
2 * Copyright (c) 2022-2023 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 "dscreen_source_service.h"
17
18 #include "if_system_ability_manager.h"
19 #include "ipc_skeleton.h"
20 #include "ipc_types.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24
25 #include "dscreen_constants.h"
26 #include "dscreen_errcode.h"
27 #include "dscreen_hisysevent.h"
28 #include "dscreen_log.h"
29 #include "dscreen_util.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 REGISTER_SYSTEM_ABILITY_BY_ID(DScreenSourceService, DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, true);
34
DScreenSourceService(int32_t saId,bool runOnCreate)35 DScreenSourceService::DScreenSourceService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
36 {
37 DHLOGI("dscreen source service create.");
38 }
39
OnStart()40 void DScreenSourceService::OnStart()
41 {
42 DHLOGI("dscreen source service start.");
43 Init();
44 }
45
OnStop()46 void DScreenSourceService::OnStop()
47 {
48 DHLOGI("dscreen source service stop.");
49 int32_t ret = V2_0::DScreenManager::GetInstance().Release();
50 if (ret != DH_SUCCESS) {
51 DHLOGE("Release V2_0::DScreenManager failed. err: %" PRId32, ret);
52 }
53 ret = V1_0::DScreenManager::GetInstance().UnInit();
54 if (ret != DH_SUCCESS) {
55 DHLOGE("UnInit V1_0::DScreenManager failed. err: %" PRId32, ret);
56 }
57 registerToService_ = false;
58 }
59
Init()60 bool DScreenSourceService::Init()
61 {
62 DHLOGI("Init dscreen source service.");
63 if (!registerToService_) {
64 bool ret = Publish(this);
65 if (!ret) {
66 DHLOGE("dscreen source publish service failed.");
67 return false;
68 }
69 registerToService_ = true;
70 }
71 DHLOGI("dscreen source service init success.");
72 return true;
73 }
74
InitSource(const std::string & params,const sptr<IDScreenSourceCallback> & callback)75 int32_t DScreenSourceService::InitSource(const std::string ¶ms, const sptr<IDScreenSourceCallback> &callback)
76 {
77 DHLOGI("Init source dscreen manager, params: %s", params.c_str());
78 if (callback == nullptr) {
79 DHLOGE("DScreenSourceService::InitSource, callback is nullptr.");
80 return ERR_DH_SCREEN_SA_INIT_SOURCE_FAIL;
81 }
82
83 if (IsSupportAVTransEngine(params)) {
84 int32_t ret = V2_0::DScreenManager::GetInstance().Initialize();
85 if (ret != DH_SUCCESS) {
86 DHLOGE("Initialize V2_0::DScreenManager failed. err: %" PRId32, ret);
87 return ret;
88 }
89 V2_0::DScreenManager::GetInstance().RegisterDScreenCallback(callback);
90 } else {
91 int32_t ret = V1_0::DScreenManager::GetInstance().Init();
92 if (ret != DH_SUCCESS) {
93 DHLOGE("Init V1_0::DScreenManager failed. err: %" PRId32, ret);
94 return ret;
95 }
96 V1_0::DScreenManager::GetInstance().RegisterDScreenCallback(callback);
97 }
98 return DH_SUCCESS;
99 }
100
ReleaseSource()101 int32_t DScreenSourceService::ReleaseSource()
102 {
103 DHLOGI("Release source dscreen manager.");
104 int32_t ret = V1_0::DScreenManager::GetInstance().UnInit();
105 if (ret != DH_SUCCESS) {
106 DHLOGE("UnInit V1_0::DScreenManager failed. err: %" PRId32, ret);
107 }
108 ret = V2_0::DScreenManager::GetInstance().Release();
109 if (ret != DH_SUCCESS) {
110 DHLOGE("Release V2_0::DScreenManager failed. err: %" PRId32, ret);
111 }
112 DHLOGI("exit source sa process");
113 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
114 if (systemAbilityMgr == nullptr) {
115 DHLOGE("systemAbilityMgr is null");
116 return DSCREEN_INIT_ERR;
117 }
118 ret = systemAbilityMgr->UnloadSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID);
119 if (ret != DH_SUCCESS) {
120 DHLOGE("source systemAbilityMgr UnLoadSystemAbility failed, ret: %" PRId32, ret);
121 return DSCREEN_BAD_VALUE;
122 }
123 DHLOGI("source systemAbilityMgr UnLoadSystemAbility success");
124 return DH_SUCCESS;
125 }
126
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)127 int32_t DScreenSourceService::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
128 const EnableParam ¶m, const std::string &reqId)
129 {
130 DHLOGI("Register source distributed screen, peer dscreen version: %s", param.version.c_str());
131 int32_t ret = DH_SUCCESS;
132 if (IsSupportAVTransEngine(param.version)) {
133 ret = V2_0::DScreenManager::GetInstance().EnableDistributedScreen(devId, dhId, param, reqId);
134 } else {
135 ret = V1_0::DScreenManager::GetInstance().EnableDistributedScreen(devId, dhId, param, reqId);
136 }
137 if (ret != DH_SUCCESS) {
138 DHLOGE("enable distributedScreen failed. devId: %s, dhId: %s, reqId: %s, param attrs: %s",
139 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str(), param.attrs.c_str());
140 ReportRegisterFail(DSCREEN_REGISTER_FAIL, ret, GetAnonyString(devId).c_str(),
141 GetAnonyString(dhId).c_str(), "enable distributedScreen failed.");
142 return ERR_DH_SCREEN_SA_ENABLE_FAILED;
143 }
144 return DH_SUCCESS;
145 }
146
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId)147 int32_t DScreenSourceService::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
148 const std::string &reqId)
149 {
150 int ret = V1_0::DScreenManager::GetInstance().DisableDistributedScreen(devId, dhId, reqId);
151 if (ret != DH_SUCCESS) {
152 DHLOGE("V1_0::DScreenManager disable distributedScreen failed. devId: %s, dhId: %s, reqId: %s",
153 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str());
154 ReportUnRegisterFail(DSCREEN_REGISTER_FAIL, ret, GetAnonyString(devId).c_str(),
155 GetAnonyString(dhId).c_str(), "disable distributedScreen failed.");
156 return ERR_DH_SCREEN_SA_DISABLE_FAILED;
157 }
158
159 ret = V2_0::DScreenManager::GetInstance().DisableDistributedScreen(devId, dhId, reqId);
160 if (ret != DH_SUCCESS) {
161 DHLOGE("V2_0::DScreenManager disable distributedScreen failed. devId: %s, dhId: %s, reqId: %s",
162 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str());
163 ReportUnRegisterFail(DSCREEN_REGISTER_FAIL, ret, GetAnonyString(devId).c_str(),
164 GetAnonyString(dhId).c_str(), "disable distributedScreen failed.");
165 return ERR_DH_SCREEN_SA_DISABLE_FAILED;
166 }
167 return DH_SUCCESS;
168 }
169
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)170 int32_t DScreenSourceService::ConfigDistributedHardware(const std::string &devId, const std::string &dhId,
171 const std::string &key, const std::string &value)
172 {
173 (void)devId;
174 (void)dhId;
175 (void)key;
176 (void)value;
177 return DH_SUCCESS;
178 }
179
DScreenNotify(const std::string & devId,const int32_t eventCode,const std::string & eventContent)180 void DScreenSourceService::DScreenNotify(const std::string &devId, const int32_t eventCode,
181 const std::string &eventContent)
182 {
183 DHLOGI("DScreenNotify, devId: %s, eventCode: %" PRId32, GetAnonyString(devId).c_str(), eventCode);
184 V1_0::DScreenManager::GetInstance().HandleDScreenNotify(devId, eventCode, eventContent);
185 }
186
Dump(int32_t fd,const std::vector<std::u16string> & args)187 int32_t DScreenSourceService::Dump(int32_t fd, const std::vector<std::u16string>& args)
188 {
189 DHLOGI("DScreenSourceService Dump.");
190 (void)args;
191 std::string result_v1;
192 V1_0::DScreenManager::GetInstance().GetScreenDumpInfo(result_v1);
193 std::string result_v2;
194 V2_0::DScreenManager::GetInstance().GetScreenDumpInfo(result_v2);
195 int ret = dprintf(fd, "%s\n", (result_v1 + result_v2).c_str());
196 if (ret < 0) {
197 DHLOGE("dprintf error");
198 return ERR_DH_SCREEN_SA_HIDUMPER_ERROR;
199 }
200
201 return ERR_OK;
202 }
203 } // namespace DistributedHardware
204 } // namespace OHOS