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