1 /*
2 * Copyright (c) 2021 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 "camera_host_service_stub.h"
17 #include <hdf_log.h>
18 #include <hdf_base.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "utils_data_stub.h"
21 #include "icamera_device.h"
22 #include "icamera_host_callback.h"
23
24 namespace OHOS::Camera {
CameraHostStub()25 CameraHostStub::CameraHostStub()
26 {
27 }
28
Init()29 RetCode CameraHostStub::Init()
30 {
31 cameraHost_ = CameraHost::CreateCameraHost();
32 if (cameraHost_ == nullptr) {
33 HDF_LOGE("%s: camera host service start failed", __func__);
34 return RC_ERROR;
35 }
36 return RC_OK;
37 }
38
CameraHostStubSetCallback(MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t CameraHostStub::CameraHostStubSetCallback(
40 MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42 bool flag = data.ReadBool();
43 sptr<ICameraHostCallback> hostCallback = nullptr;
44 if (flag) {
45 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
46 hostCallback = OHOS::iface_cast<ICameraHostCallback>(remoteObj);
47 }
48 CamRetCode ret = cameraHost_->SetCallback(hostCallback);
49 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
50 HDF_LOGE("%s: write retcode failed", __func__);
51 return HDF_FAILURE;
52 }
53
54 return HDF_SUCCESS;
55 }
56
CameraHostStubGetCameraIds(MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int32_t CameraHostStub::CameraHostStubGetCameraIds(
58 MessageParcel &data, MessageParcel &reply, MessageOption &option)
59 {
60 if (cameraHost_ == nullptr) {
61 return HDF_FAILURE;
62 }
63
64 std::vector<std::string> cameraIds;
65 CamRetCode ret = cameraHost_->GetCameraIds(cameraIds);
66 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
67 HDF_LOGE("%s: write retcode failed", __func__);
68 return HDF_FAILURE;
69 }
70
71 if (!reply.WriteStringVector(cameraIds)) {
72 HDF_LOGE("%s: write cameraIds failed", __func__);
73 return HDF_FAILURE;
74 }
75
76 return HDF_SUCCESS;
77 }
78
CameraHostStubGetCameraAbility(MessageParcel & data,MessageParcel & reply,MessageOption & option)79 int32_t CameraHostStub::CameraHostStubGetCameraAbility(
80 MessageParcel &data, MessageParcel &reply, MessageOption &option)
81 {
82 const std::string cameraId = data.ReadString();
83 if (cameraId.empty()) {
84 HDF_LOGE("%s: read input param is empty", __func__);
85 return HDF_ERR_INVALID_PARAM;
86 }
87
88 std::shared_ptr<CameraAbility> ability = nullptr;
89 CamRetCode ret = cameraHost_->GetCameraAbility(cameraId, ability);
90 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
91 HDF_LOGE("%s: write retcode failed", __func__);
92 return HDF_FAILURE;
93 }
94
95 bool bRet = UtilsDataStub::EncodeCameraMetadata(ability, reply);
96 if (!bRet) {
97 HDF_LOGE("%s: write ability failed", __func__);
98 return HDF_FAILURE;
99 }
100
101 return HDF_SUCCESS;
102 }
103
CameraHostStubOpenCamera(MessageParcel & data,MessageParcel & reply,MessageOption & option)104 int32_t CameraHostStub::CameraHostStubOpenCamera(
105 MessageParcel &data, MessageParcel &reply, MessageOption &option)
106 {
107 const std::string cameraId = data.ReadString();
108 if (cameraId.empty()) {
109 HDF_LOGE("%s: read input param is empty", __func__);
110 return HDF_ERR_INVALID_PARAM;
111 }
112
113 bool flag = data.ReadBool();
114 OHOS::sptr<ICameraDeviceCallback> deviceCallback = nullptr;
115 if (flag) {
116 OHOS::sptr<IRemoteObject> remoteCallback = data.ReadRemoteObject();
117 deviceCallback = OHOS::iface_cast<ICameraDeviceCallback>(remoteCallback);
118 }
119
120 OHOS::sptr<ICameraDevice> cameraDevice = nullptr;
121 CamRetCode ret = cameraHost_->OpenCamera(cameraId, deviceCallback, cameraDevice);
122 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
123 HDF_LOGE("%s: get stream operator failed", __func__);
124 return HDF_FAILURE;
125 }
126
127 bool deviceFlag = (cameraDevice != nullptr);
128 if (!reply.WriteBool(deviceFlag)) {
129 HDF_LOGE("%s: write camera device flag failed", __func__);
130 return HDF_FAILURE;
131 }
132
133 if (deviceFlag && !reply.WriteRemoteObject(cameraDevice->AsObject())) {
134 HDF_LOGE("%s: write camera device failed", __func__);
135 return HDF_FAILURE;
136 }
137
138 return HDF_SUCCESS;
139 }
140
CameraHostStubSetFlashlight(MessageParcel & data,MessageParcel & reply,MessageOption & option)141 int32_t CameraHostStub::CameraHostStubSetFlashlight(
142 MessageParcel &data, MessageParcel &reply, MessageOption &option)
143 {
144 if (cameraHost_ == nullptr) {
145 HDF_LOGE("%s: camera host is null", __func__);
146 return HDF_FAILURE;
147 }
148
149 std::string cameraId = data.ReadString();
150 bool isEnable = data.ReadBool();
151 CamRetCode ret = cameraHost_->SetFlashlight(cameraId, isEnable);
152 if (!reply.WriteInt32(static_cast<int32_t>(ret))) {
153 HDF_LOGE("%s: write retcode failed", __func__);
154 return HDF_FAILURE;
155 }
156
157 return HDF_SUCCESS;
158 }
159
CameraHostServiceStubOnRemoteRequest(int cmdId,MessageParcel & data,MessageParcel & reply,MessageOption & option)160 int32_t CameraHostStub::CameraHostServiceStubOnRemoteRequest(int cmdId, MessageParcel &data,
161 MessageParcel &reply, MessageOption &option)
162 {
163 switch(cmdId) {
164 case CMD_CAMERA_HOST_SET_CALLBACK: {
165 return CameraHostStubSetCallback(data, reply, option);
166 }
167 case CMD_CAMERA_HOST_GET_CAMERAID: {
168 return CameraHostStubGetCameraIds(data, reply, option);
169 }
170 case CMD_CAMERA_HOST_GET_CAMERA_ABILITY: {
171 return CameraHostStubGetCameraAbility(data, reply, option);
172 }
173 case CMD_CAMERA_HOST_OPEN_CAMERA: {
174 return CameraHostStubOpenCamera(data, reply, option);
175 }
176 case CMD_CAMERA_HOST_SET_FLASH_LIGHT: {
177 return CameraHostStubSetFlashlight(data, reply, option);
178 }
179 default: {
180 HDF_LOGE("%s: not support cmd %d", __func__, cmdId);
181 return HDF_ERR_INVALID_PARAM;
182 }
183 }
184 return HDF_SUCCESS;
185 }
186 }
187
CameraHostStubInstance()188 void *CameraHostStubInstance()
189 {
190 OHOS::Camera::CameraHostStub *stub =
191 new (std::nothrow) OHOS::Camera::CameraHostStub();
192 if (stub == nullptr) {
193 HDF_LOGE("%s: camera host stub create failed.", __func__);
194 return nullptr;
195 }
196
197 OHOS::Camera::RetCode ret = stub->Init();
198 if (ret != OHOS::Camera::RC_OK) {
199 delete stub;
200 stub = nullptr;
201 return nullptr;
202 }
203
204 return reinterpret_cast<void*>(stub);
205 }
206
DestroyCameraHostStub(void * stubObj)207 void DestroyCameraHostStub(void *stubObj)
208 {
209 delete reinterpret_cast<OHOS::Camera::CameraHostStub *>(stubObj);
210 stubObj = nullptr;
211 }
212
CameraHostServiceOnRemoteRequest(void * stub,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)213 int32_t CameraHostServiceOnRemoteRequest(void *stub, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
214 {
215 if (stub == nullptr) {
216 HDF_LOGE("%s:stub is null", __func__);
217 return HDF_FAILURE;
218 }
219
220 OHOS::Camera::CameraHostStub *cameraHostStub =
221 reinterpret_cast<OHOS::Camera::CameraHostStub *>(stub);
222 OHOS::MessageParcel *dataParcel = nullptr;
223 OHOS::MessageParcel *replyParcel = nullptr;
224
225 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
226 HDF_LOGE("%s:invalid reply sbuf object to dispatch", __func__);
227 return HDF_ERR_INVALID_PARAM;
228 }
229
230 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
231 HDF_LOGE("%s:invalid data sbuf object to dispatch", __func__);
232 return HDF_ERR_INVALID_PARAM;
233 }
234
235 OHOS::MessageOption option;
236 return cameraHostStub->CameraHostServiceStubOnRemoteRequest(cmdId, *dataParcel, *replyParcel, option);
237 }