• 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_camera_sink_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 "anonymous_string.h"
26 #include "dcamera_handler.h"
27 #include "dcamera_hisysevent_adapter.h"
28 #include "dcamera_sink_service_ipc.h"
29 #include "distributed_camera_errno.h"
30 #include "distributed_hardware_log.h"
31 #include "dcamera_sa_process_state.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 REGISTER_SYSTEM_ABILITY_BY_ID(DistributedCameraSinkService, DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true);
36 
37 static CameraDumpInfo g_camDump;
38 DistributedCameraSinkService* DistributedCameraSinkService::dcSinkService;
DistributedCameraSinkService(int32_t saId,bool runOnCreate)39 DistributedCameraSinkService::DistributedCameraSinkService(int32_t saId, bool runOnCreate)
40     : SystemAbility(saId, runOnCreate)
41 {
42     dcSinkService = this;
43 }
44 
OnStart()45 void DistributedCameraSinkService::OnStart()
46 {
47     DHLOGI("DistributedCameraSinkService::OnStart");
48     if (state_ == DCameraServiceState::DCAMERA_SRV_STATE_RUNNING) {
49         DHLOGI("DistributedCameraSinkService has already started.");
50         return;
51     }
52 
53     if (!Init()) {
54         DHLOGE("DistributedCameraSinkService init failed");
55         return;
56     }
57     state_ = DCameraServiceState::DCAMERA_SRV_STATE_RUNNING;
58     DHLOGI("DCameraServiceState OnStart service success.");
59 }
60 
Init()61 bool DistributedCameraSinkService::Init()
62 {
63     DHLOGI("DistributedCameraSinkService start init");
64     DCameraSinkServiceIpc::GetInstance().Init();
65     if (!registerToService_) {
66         bool ret = Publish(this);
67         if (!ret) {
68             DHLOGE("DistributedCameraSinkService Publish service failed");
69             return false;
70         }
71         registerToService_ = true;
72     }
73     DHLOGI("DistributedCameraSinkService init success");
74     return true;
75 }
76 
OnStop()77 void DistributedCameraSinkService::OnStop()
78 {
79     DHLOGI("DistributedCameraSinkService OnStop service");
80     state_ = DCameraServiceState::DCAMERA_SRV_STATE_NOT_START;
81     registerToService_ = false;
82     DCameraSinkServiceIpc::GetInstance().UnInit();
83 }
84 
InitSink(const std::string & params)85 int32_t DistributedCameraSinkService::InitSink(const std::string& params)
86 {
87     DHLOGI("DistributedCameraSinkService::InitSink");
88     sinkVer_ = params;
89     g_camDump.version = sinkVer_;
90     int32_t ret = DCameraHandler::GetInstance().Initialize();
91     if (ret != DCAMERA_OK) {
92         DHLOGE("DistributedCameraSinkService::InitSink handler initialize failed, ret: %d", ret);
93         return ret;
94     }
95 
96     std::vector<std::string> cameras = DCameraHandler::GetInstance().GetCameras();
97     if (cameras.empty()) {
98         DHLOGE("DistributedCameraSinkService::InitSink no camera device");
99         return DCAMERA_BAD_VALUE;
100     }
101     g_camDump.camNumber = static_cast<int32_t>(cameras.size());
102     for (auto& dhId : cameras) {
103         std::shared_ptr<DCameraSinkDev> sinkDevice = std::make_shared<DCameraSinkDev>(dhId);
104         ret = sinkDevice->Init();
105         if (ret != DCAMERA_OK) {
106             DHLOGE("DistributedCameraSinkService::InitSink sink device init failed, ret: %d", ret);
107             return ret;
108         }
109         camerasMap_.emplace(dhId, sinkDevice);
110     }
111     DHLOGI("DistributedCameraSinkService::InitSink success");
112     return DCAMERA_OK;
113 }
114 
ReleaseSink()115 int32_t DistributedCameraSinkService::ReleaseSink()
116 {
117     DHLOGI("DistributedCameraSinkService::ReleaseSink");
118     for (auto iter = camerasMap_.begin(); iter != camerasMap_.end(); iter++) {
119         std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
120         int32_t ret = sinkDevice->UnInit();
121         if (ret != DCAMERA_OK) {
122             DHLOGE("DistributedCameraSinkService::ReleaseSink release sink device failed, ret: %d", ret);
123         }
124     }
125     camerasMap_.clear();
126     DHLOGI("check sink sa state.");
127     SetSinkProcessExit();
128     return DCAMERA_OK;
129 }
130 
SubscribeLocalHardware(const std::string & dhId,const std::string & parameters)131 int32_t DistributedCameraSinkService::SubscribeLocalHardware(const std::string& dhId, const std::string& parameters)
132 {
133     DHLOGI("DistributedCameraSinkService::SubscribeLocalHardware dhId: %s", GetAnonyString(dhId).c_str());
134     auto iter = camerasMap_.find(dhId);
135     if (iter == camerasMap_.end()) {
136         DHLOGE("DistributedCameraSinkService::SubscribeLocalHardware device not found");
137         return DCAMERA_NOT_FOUND;
138     }
139 
140     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
141     int32_t ret = sinkDevice->SubscribeLocalHardware(parameters);
142     if (ret != DCAMERA_OK) {
143         DHLOGE("DistributedCameraSinkService::SubscribeLocalHardware failed, ret: %d", ret);
144         return ret;
145     }
146     DHLOGI("DistributedCameraSinkService::SubscribeLocalHardware success");
147     return DCAMERA_OK;
148 }
149 
UnsubscribeLocalHardware(const std::string & dhId)150 int32_t DistributedCameraSinkService::UnsubscribeLocalHardware(const std::string& dhId)
151 {
152     DHLOGI("DistributedCameraSinkService::UnsubscribeLocalHardware dhId: %s", GetAnonyString(dhId).c_str());
153     auto iter = camerasMap_.find(dhId);
154     if (iter == camerasMap_.end()) {
155         DHLOGE("DistributedCameraSinkService::UnsubscribeLocalHardware device not found");
156         return DCAMERA_NOT_FOUND;
157     }
158 
159     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
160     int32_t ret = sinkDevice->UnsubscribeLocalHardware();
161     if (ret != DCAMERA_OK) {
162         DHLOGE("DistributedCameraSinkService::UnsubscribeLocalHardware failed, ret: %d", ret);
163         return ret;
164     }
165     DHLOGI("DistributedCameraSinkService::UnsubscribeLocalHardware success");
166     return DCAMERA_OK;
167 }
168 
StopCapture(const std::string & dhId)169 int32_t DistributedCameraSinkService::StopCapture(const std::string& dhId)
170 {
171     DHLOGI("DistributedCameraSinkService::StopCapture dhId: %s", GetAnonyString(dhId).c_str());
172     auto iter = camerasMap_.find(dhId);
173     if (iter == camerasMap_.end()) {
174         DHLOGE("DistributedCameraSinkService::StopCapture device not found");
175         return DCAMERA_NOT_FOUND;
176     }
177 
178     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
179     int32_t ret = sinkDevice->StopCapture();
180     if (ret != DCAMERA_OK) {
181         DHLOGE("DistributedCameraSinkService::StopCapture failed, ret: %d", ret);
182         return ret;
183     }
184     DHLOGI("DistributedCameraSinkService::StopCapture success");
185     return DCAMERA_OK;
186 }
187 
ChannelNeg(const std::string & dhId,std::string & channelInfo)188 int32_t DistributedCameraSinkService::ChannelNeg(const std::string& dhId, std::string& channelInfo)
189 {
190     DHLOGI("DistributedCameraSinkService::ChannelNeg dhId: %s", GetAnonyString(dhId).c_str());
191     auto iter = camerasMap_.find(dhId);
192     if (iter == camerasMap_.end()) {
193         DHLOGE("DistributedCameraSinkService::ChannelNeg device not found");
194         return DCAMERA_NOT_FOUND;
195     }
196 
197     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
198     int32_t ret = sinkDevice->ChannelNeg(channelInfo);
199     if (ret != DCAMERA_OK) {
200         DHLOGE("DistributedCameraSinkService::ChannelNeg failed, ret: %d", ret);
201         return ret;
202     }
203     DHLOGI("DistributedCameraSinkService::ChannelNeg success");
204     return DCAMERA_OK;
205 }
206 
GetCameraInfo(const std::string & dhId,std::string & cameraInfo)207 int32_t DistributedCameraSinkService::GetCameraInfo(const std::string& dhId, std::string& cameraInfo)
208 {
209     DHLOGI("DistributedCameraSinkService::GetCameraInfo dhId: %s", GetAnonyString(dhId).c_str());
210     auto iter = camerasMap_.find(dhId);
211     if (iter == camerasMap_.end()) {
212         DHLOGE("DistributedCameraSinkService::GetCameraInfo device not found");
213         return DCAMERA_NOT_FOUND;
214     }
215 
216     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
217     int32_t ret = sinkDevice->GetCameraInfo(cameraInfo);
218     if (ret != DCAMERA_OK) {
219         DHLOGE("DistributedCameraSinkService::GetCameraInfo failed, ret: %d", ret);
220         return ret;
221     }
222     DHLOGI("DistributedCameraSinkService::GetCameraInfo success");
223     return DCAMERA_OK;
224 }
225 
OpenChannel(const std::string & dhId,std::string & openInfo)226 int32_t DistributedCameraSinkService::OpenChannel(const std::string& dhId, std::string& openInfo)
227 {
228     DHLOGI("DistributedCameraSinkService::OpenChannel dhId: %s", GetAnonyString(dhId).c_str());
229     auto iter = camerasMap_.find(dhId);
230     if (iter == camerasMap_.end()) {
231         DHLOGE("DistributedCameraSinkService::OpenChannel device not found");
232         return DCAMERA_NOT_FOUND;
233     }
234 
235     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
236     int32_t ret = sinkDevice->OpenChannel(openInfo);
237     if (ret != DCAMERA_OK) {
238         DHLOGE("DistributedCameraSinkService::OpenChannel failed, ret: %d", ret);
239         ReportDcamerOptFail(DCAMERA_OPT_FAIL, DCAMERA_SINK_OPEN_CAM_ERROR,
240             CreateMsg("sink service open channel failed, dhId: %s", GetAnonyString(dhId).c_str()));
241         return ret;
242     }
243     DHLOGI("DistributedCameraSinkService::OpenChannel success");
244     return DCAMERA_OK;
245 }
246 
CloseChannel(const std::string & dhId)247 int32_t DistributedCameraSinkService::CloseChannel(const std::string& dhId)
248 {
249     DHLOGI("DistributedCameraSinkService::CloseChannel dhId: %s", GetAnonyString(dhId).c_str());
250     auto iter = camerasMap_.find(dhId);
251     if (iter == camerasMap_.end()) {
252         DHLOGE("DistributedCameraSinkService::CloseChannel device not found");
253         return DCAMERA_NOT_FOUND;
254     }
255 
256     std::shared_ptr<DCameraSinkDev> sinkDevice = iter->second;
257     int32_t ret = sinkDevice->CloseChannel();
258     if (ret != DCAMERA_OK) {
259         DHLOGE("DistributedCameraSinkService::CloseChannel failed, ret: %d", ret);
260         return ret;
261     }
262     DHLOGI("DistributedCameraSinkService::CloseChannel success");
263     return DCAMERA_OK;
264 }
265 
Dump(int32_t fd,const std::vector<std::u16string> & args)266 int DistributedCameraSinkService::Dump(int32_t fd, const std::vector<std::u16string>& args)
267 {
268     DHLOGI("DistributedCameraSinkService Dump.");
269     if (args.size() > DUMP_MAX_SIZE) {
270         DHLOGE("DistributedCameraSinkService Dump input is invalid");
271         return DCAMERA_BAD_VALUE;
272     }
273     std::string result;
274     std::vector<std::string> argsStr;
275     for (auto item : args) {
276         argsStr.emplace_back(Str16ToStr8(item));
277     }
278 
279     if (!DcameraSinkHidumper::GetInstance().Dump(argsStr, result)) {
280         DHLOGE("Hidump error");
281         return DCAMERA_BAD_VALUE;
282     }
283 
284     int ret = dprintf(fd, "%s\n", result.c_str());
285     if (ret < 0) {
286         DHLOGE("dprintf error");
287         return DCAMERA_BAD_VALUE;
288     }
289 
290     return DCAMERA_OK;
291 }
292 
GetCamIds()293 void DistributedCameraSinkService::GetCamIds()
294 {
295     std::vector<std::string> camIds;
296     for (auto it = camerasMap_.begin(); it != camerasMap_.end(); it++) {
297         camIds.push_back(it->second->GetDhid());
298     }
299     g_camDump.camIds = camIds;
300 }
301 
GetCamDumpInfo(CameraDumpInfo & camDump)302 void DistributedCameraSinkService::GetCamDumpInfo(CameraDumpInfo& camDump)
303 {
304     dcSinkService->GetCamIds();
305     camDump = g_camDump;
306 }
307 } // namespace DistributedHardware
308 } // namespace OHOS