• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_proxy.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <message_parcel.h>
20 #include "utils_data_stub.h"
21 #include "metadata_utils.h"
22 #include "icamera_device.h"
23 #include "icamera_host_callback.h"
24 #include "icamera_device_callback.h"
25 
26 namespace OHOS::Camera {
Get(const char * serviceName)27 sptr<ICameraHost> ICameraHost::Get(const char *serviceName)
28 {
29     do {
30         using namespace OHOS::HDI::ServiceManager::V1_0;
31         auto servMgr = IServiceManager::Get();
32         if (servMgr == nullptr) {
33             HDF_LOGE("%s: IServiceManager failed!", __func__);
34             break;
35         }
36 
37         auto remote = servMgr->GetService(serviceName);
38         if (remote != nullptr) {
39             sptr<CameraHostProxy> hostSptr = iface_cast<CameraHostProxy>(remote);
40             return hostSptr;
41         }
42         HDF_LOGE("%s: GetService failed! serviceName = %s", __func__, serviceName);
43     } while (false);
44 
45     HDF_LOGE("%s: get %s failed!", __func__, serviceName);
46     return nullptr;
47 }
48 
SetCallback(const OHOS::sptr<ICameraHostCallback> & callback)49 CamRetCode CameraHostProxy::SetCallback(const OHOS::sptr<ICameraHostCallback> &callback)
50 {
51     MessageParcel data;
52     MessageParcel reply;
53     MessageOption option;
54 
55     if (!data.WriteInterfaceToken(CameraHostProxy::GetDescriptor())) {
56         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
57         return INVALID_ARGUMENT;
58     }
59 
60     bool callbackFlag = (callback != nullptr);
61     if (!data.WriteBool(callbackFlag)) {
62         HDF_LOGE("%s: set callback flag failed!", __func__);
63         return INVALID_ARGUMENT;
64     }
65 
66     if (callbackFlag && !data.WriteRemoteObject(callback->AsObject())) {
67         HDF_LOGE("%s: set callback write remote obj failed!", __func__);
68         return INVALID_ARGUMENT;
69     }
70 
71     int32_t ret = Remote()->SendRequest(CMD_CAMERA_HOST_SET_CALLBACK, data, reply, option);
72     if (ret != HDF_SUCCESS) {
73         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
74         return INVALID_ARGUMENT;
75     }
76 
77     return static_cast<CamRetCode>(reply.ReadInt32());
78 }
79 
GetCameraIds(std::vector<std::string> & cameraIds)80 CamRetCode CameraHostProxy::GetCameraIds(std::vector<std::string> &cameraIds)
81 {
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option;
85 
86     if (!data.WriteInterfaceToken(CameraHostProxy::GetDescriptor())) {
87         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
88         return INVALID_ARGUMENT;
89     }
90 
91     int32_t ret = Remote()->SendRequest(CMD_CAMERA_HOST_GET_CAMERAID, data, reply, option);
92     if (ret != HDF_SUCCESS) {
93         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
94         return INVALID_ARGUMENT;
95     }
96 
97     CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
98 
99     if (!reply.ReadStringVector(&cameraIds)) {
100         HDF_LOGE("%{public}s: read cameraids failed.", __func__);
101         return INVALID_ARGUMENT;
102     }
103 
104     return retCode;
105 }
106 
GetCameraAbility(const std::string & cameraId,std::shared_ptr<CameraAbility> & ability)107 CamRetCode CameraHostProxy::GetCameraAbility(const std::string &cameraId,
108     std::shared_ptr<CameraAbility> &ability)
109 {
110     MessageParcel data;
111     MessageParcel reply;
112     MessageOption option;
113 
114     if (!data.WriteInterfaceToken(CameraHostProxy::GetDescriptor())) {
115         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
116         return INVALID_ARGUMENT;
117     }
118 
119     if (!data.WriteString(cameraId)) {
120         HDF_LOGE("%{public}s: write cameraId failed!", __func__);
121         return INVALID_ARGUMENT;
122     }
123 
124     int32_t ret = Remote()->SendRequest(
125         CMD_CAMERA_HOST_GET_CAMERA_ABILITY, data, reply, option);
126     if (ret != HDF_SUCCESS) {
127         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
128         return INVALID_ARGUMENT;
129     }
130 
131     CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
132     MetadataUtils::DecodeCameraMetadata(reply, ability);
133 
134     return retCode;
135 }
136 
OpenCamera(const std::string & cameraId,const OHOS::sptr<ICameraDeviceCallback> & callback,OHOS::sptr<ICameraDevice> & pDevice)137 CamRetCode CameraHostProxy::OpenCamera(const std::string &cameraId,
138     const OHOS::sptr<ICameraDeviceCallback> &callback,
139     OHOS::sptr<ICameraDevice> &pDevice)
140 {
141     MessageParcel data;
142     MessageParcel reply;
143     MessageOption option;
144 
145     if (!data.WriteInterfaceToken(CameraHostProxy::GetDescriptor())) {
146         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
147         return INVALID_ARGUMENT;
148     }
149 
150     if (!data.WriteString(cameraId)) {
151         HDF_LOGE("%{public}s: write cameraId failed!", __func__);
152         return INVALID_ARGUMENT;
153     }
154 
155     bool callbackFlag = (callback != nullptr);
156     if (!data.WriteBool(callbackFlag)) {
157         HDF_LOGE("%s: write camera callback flag failed", __func__);
158         return INVALID_ARGUMENT;
159     }
160 
161     if (callbackFlag && !data.WriteRemoteObject(callback->AsObject())) {
162         HDF_LOGE("%s: write camera device callback failed", __func__);
163         return INVALID_ARGUMENT;
164     }
165 
166     int32_t ret = Remote()->SendRequest(CMD_CAMERA_HOST_OPEN_CAMERA, data, reply, option);
167     if (ret != HDF_SUCCESS) {
168         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
169         return INVALID_ARGUMENT;
170     }
171 
172     CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
173     bool flag = reply.ReadBool();
174     if (flag) {
175         sptr<IRemoteObject> remoteCameraDevice = reply.ReadRemoteObject();
176         if (remoteCameraDevice == nullptr) {
177             HDF_LOGE("%{public}s: CameraHostProxy remoteCameraDevice is null", __func__);
178         }
179         pDevice = OHOS::iface_cast<ICameraDevice>(remoteCameraDevice);
180     }
181 
182     return retCode;
183 }
184 
SetFlashlight(const std::string & cameraId,bool & isEnable)185 CamRetCode CameraHostProxy::SetFlashlight(const std::string &cameraId, bool &isEnable)
186 {
187     MessageParcel data;
188     MessageParcel reply;
189     MessageOption option;
190 
191     if (!data.WriteInterfaceToken(CameraHostProxy::GetDescriptor())) {
192         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
193         return INVALID_ARGUMENT;
194     }
195 
196     if (!data.WriteString(cameraId)) {
197         HDF_LOGE("%{public}s: write cameraId failed!", __func__);
198         return INVALID_ARGUMENT;
199     }
200 
201     if (!data.WriteBool(isEnable)) {
202         HDF_LOGE("%{public}s: write isEnable failed!", __func__);
203         return INVALID_ARGUMENT;
204     }
205 
206     int32_t ret = Remote()->SendRequest(CMD_CAMERA_HOST_SET_FLASH_LIGHT, data, reply, option);
207     if (ret != HDF_SUCCESS) {
208         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
209         return INVALID_ARGUMENT;
210     }
211 
212     return static_cast<CamRetCode>(reply.ReadInt32());
213 }
214 }