• 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 "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_sa_process_state.h"
30 #include "dscreen_util.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 REGISTER_SYSTEM_ABILITY_BY_ID(DScreenSourceService, DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, true);
35 
DScreenSourceService(int32_t saId,bool runOnCreate)36 DScreenSourceService::DScreenSourceService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
37 {
38     DHLOGI("dscreen source service create.");
39 }
40 
~DScreenSourceService()41 DScreenSourceService::~DScreenSourceService()
42 {
43     DHLOGI("~DScreenSourceService.");
44 }
45 
OnStart()46 void DScreenSourceService::OnStart()
47 {
48     DHLOGI("dscreen source service start.");
49     Init();
50 }
51 
OnStop()52 void DScreenSourceService::OnStop()
53 {
54     DHLOGI("dscreen source service stop.");
55     int32_t ret = DScreenManager::GetInstance().UnInit();
56     if (ret != DH_SUCCESS) {
57         DHLOGE("UnInit DScreenManager failed. err: %d", ret);
58     }
59     registerToService_ = false;
60 }
61 
Init()62 bool DScreenSourceService::Init()
63 {
64     DHLOGI("dscreen source service start init.");
65     if (!registerToService_) {
66         bool ret = Publish(this);
67         if (!ret) {
68             DHLOGE("dscreen source publish service failed.");
69             return false;
70         }
71         registerToService_ = true;
72     }
73     DHLOGI("dscreen init success.");
74     return true;
75 }
76 
InitSource(const std::string & params,const sptr<IDScreenSourceCallback> & callback)77 int32_t DScreenSourceService::InitSource(const std::string &params, const sptr<IDScreenSourceCallback> &callback)
78 {
79     DHLOGI("InitSource");
80     int32_t ret = DScreenManager::GetInstance().Init();
81     if (ret != DH_SUCCESS) {
82         DHLOGE("Init DScreenManager failed. err: %d", ret);
83         return ret;
84     }
85 
86     DScreenManager::GetInstance().RegisterDScreenCallback(callback);
87     return DH_SUCCESS;
88 }
89 
ReleaseSource()90 int32_t DScreenSourceService::ReleaseSource()
91 {
92     DHLOGI("ReleaseSource");
93     int32_t ret = DScreenManager::GetInstance().UnInit();
94     if (ret != DH_SUCCESS) {
95         DHLOGE("UnInit DScreenManager failed. err: %d", ret);
96         return ret;
97     }
98     DHLOGI("exit source sa process");
99     SetSourceProcessExit();
100     return DH_SUCCESS;
101 }
102 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)103 int32_t DScreenSourceService::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
104     const EnableParam &param, const std::string &reqId)
105 {
106     std::string attrs = param.attrs;
107     int ret = DScreenManager::GetInstance().EnableDistributedScreen(devId, dhId, attrs, reqId);
108     if (ret != DH_SUCCESS) {
109         DHLOGE("enable distributedScreen failed. devId: %s, dhId: %s, reqId: %s, attrs: %s",
110             GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str(), attrs.c_str());
111         ReportRegisterFail(DSCREEN_REGISTER_FAIL, ret, GetAnonyString(devId).c_str(),
112             GetAnonyString(dhId).c_str(), "enable distributedScreen failed.");
113         return ERR_DH_SCREEN_SA_ENABLE_FAILED;
114     }
115     return DH_SUCCESS;
116 }
117 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId)118 int32_t DScreenSourceService::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
119     const std::string &reqId)
120 {
121     int ret = DScreenManager::GetInstance().DisableDistributedScreen(devId, dhId, reqId);
122     if (ret != DH_SUCCESS) {
123         DHLOGE("disable distributedScreen failed. devId: %s, dhId: %s, reqId: %s",
124             GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str());
125         ReportUnRegisterFail(DSCREEN_REGISTER_FAIL, ret, GetAnonyString(devId).c_str(),
126             GetAnonyString(dhId).c_str(), "disable distributedScreen failed.");
127         return ERR_DH_SCREEN_SA_DISABLE_FAILED;
128     }
129     return DH_SUCCESS;
130 }
131 
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)132 int32_t DScreenSourceService::ConfigDistributedHardware(const std::string &devId, const std::string &dhId,
133     const std::string &key, const std::string &value)
134 {
135     (void)devId;
136     (void)dhId;
137     (void)key;
138     (void)value;
139     return DH_SUCCESS;
140 }
141 
DScreenNotify(const std::string & devId,const int32_t eventCode,const std::string & eventContent)142 void DScreenSourceService::DScreenNotify(const std::string &devId, const int32_t eventCode,
143     const std::string &eventContent)
144 {
145     DHLOGI("DScreenNotify, devId: %s, eventCode: %d", GetAnonyString(devId).c_str(), eventCode);
146     DScreenManager::GetInstance().HandleDScreenNotify(devId, eventCode, eventContent);
147 }
148 
Dump(int32_t fd,const std::vector<std::u16string> & args)149 int32_t DScreenSourceService::Dump(int32_t fd, const std::vector<std::u16string>& args)
150 {
151     DHLOGI("DScreenSourceService  Dump.");
152     (void)args;
153     std::string result;
154     DScreenManager::GetInstance().GetScreenDumpInfo(result);
155     int ret = dprintf(fd, "%s\n", result.c_str());
156     if (ret < 0) {
157         DHLOGE("dprintf error");
158         return ERR_DH_SCREEN_SA_HIDUMPER_ERROR;
159     }
160 
161     return ERR_OK;
162 }
163 } // namespace DistributedHardware
164 } // namespace OHOS