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 "hcamera_device_stub.h"
17 #include "camera_log.h"
18 #include "camera_util.h"
19 #include "metadata_utils.h"
20 #include "remote_request_code.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int HCameraDeviceStub::OnRemoteRequest(
25 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
26 {
27 DisableJeMalloc();
28 int errCode = -1;
29
30 if (data.ReadInterfaceToken() != GetDescriptor()) {
31 return errCode;
32 }
33 switch (code) {
34 case CAMERA_DEVICE_OPEN: {
35 errCode = Open();
36 break;
37 }
38 case CAMERA_DEVICE_CLOSE:
39 errCode = Close();
40 break;
41 case CAMERA_DEVICE_RELEASE:
42 errCode = Release();
43 break;
44 case CAMERA_DEVICE_SET_CALLBACK:
45 errCode = HCameraDeviceStub::HandleSetCallback(data);
46 break;
47 case CAMERA_DEVICE_UPDATE_SETTNGS:
48 errCode = HCameraDeviceStub::HandleUpdateSetting(data);
49 break;
50 case CAMERA_DEVICE_ENABLED_RESULT:
51 errCode = HCameraDeviceStub::HandleEnableResult(data);
52 break;
53 case CAMERA_DEVICE_GET_ENABLED_RESULT:
54 errCode = HCameraDeviceStub::HandleGetEnabledResults(reply);
55 break;
56 case CAMERA_DEVICE_DISABLED_RESULT:
57 errCode = HCameraDeviceStub::HandleDisableResult(data);
58 break;
59 default:
60 MEDIA_ERR_LOG("HCameraDeviceStub request code %{public}d not handled", code);
61 errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 break;
63 }
64
65 return errCode;
66 }
67
HandleSetCallback(MessageParcel & data)68 int HCameraDeviceStub::HandleSetCallback(MessageParcel &data)
69 {
70 auto remoteObject = data.ReadRemoteObject();
71 if (remoteObject == nullptr) {
72 MEDIA_ERR_LOG("HCameraDeviceStub HandleSetCallback CameraDeviceServiceCallback is null");
73 return IPC_STUB_INVALID_DATA_ERR;
74 }
75
76 auto callback = iface_cast<ICameraDeviceServiceCallback>(remoteObject);
77
78 return SetCallback(callback);
79 }
80
HandleUpdateSetting(MessageParcel & data)81 int HCameraDeviceStub::HandleUpdateSetting(MessageParcel &data)
82 {
83 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = nullptr;
84 OHOS::Camera::MetadataUtils::DecodeCameraMetadata(data, metadata);
85
86 return UpdateSetting(metadata);
87 }
88
HandleGetEnabledResults(MessageParcel & reply)89 int HCameraDeviceStub::HandleGetEnabledResults(MessageParcel &reply)
90 {
91 std::vector<int32_t> results;
92 int ret = GetEnabledResults(results);
93 if (ret != ERR_NONE) {
94 MEDIA_ERR_LOG("CameraDeviceStub::HandleGetEnabledResults GetEnabledResults failed : %{public}d", ret);
95 return ret;
96 }
97
98 if (!reply.WriteInt32Vector(results)) {
99 MEDIA_ERR_LOG("HCameraDeviceStub::HandleGetEnabledResults write results failed");
100 return IPC_STUB_WRITE_PARCEL_ERR;
101 }
102
103 return ret;
104 }
105
HandleEnableResult(MessageParcel & data)106 int HCameraDeviceStub::HandleEnableResult(MessageParcel &data)
107 {
108 std::vector<int32_t> results;
109 if (!data.ReadInt32Vector(&results)) {
110 MEDIA_ERR_LOG("CameraDeviceStub::HandleEnableResult read results failed");
111 return IPC_STUB_INVALID_DATA_ERR;
112 }
113
114 int ret = EnableResult(results);
115 if (ret != ERR_NONE) {
116 MEDIA_ERR_LOG("CameraDeviceStub::HandleEnableResult EnableResult failed : %{public}d", ret);
117 }
118
119 return ret;
120 }
121
HandleDisableResult(MessageParcel & data)122 int HCameraDeviceStub::HandleDisableResult(MessageParcel &data)
123 {
124 std::vector<int32_t> results;
125 if (!data.ReadInt32Vector(&results)) {
126 MEDIA_ERR_LOG("CameraDeviceStub::HandleDisableResult read results failed");
127 return IPC_STUB_INVALID_DATA_ERR;
128 }
129
130 int ret = DisableResult(results);
131 if (ret != ERR_NONE) {
132 MEDIA_ERR_LOG("CameraDeviceStub::HandleDisableResult DisableResult failed : %{public}d", ret);
133 }
134
135 return ret;
136 }
137 } // namespace CameraStandard
138 } // namespace OHOS
139