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