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