blob: 3debe224de41b578f32dc97f85db5a26a82d1683 [file] [log] [blame]
Mathias Agopian3cf61352010-02-09 17:46:37 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Igor Murashkinbef3f232013-05-30 17:47:38 -070018#define LOG_TAG "BpCameraService"
19#include <utils/Log.h>
20
Mathias Agopian3cf61352010-02-09 17:46:37 -080021#include <stdint.h>
22#include <sys/types.h>
23
24#include <binder/Parcel.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27
28#include <camera/ICameraService.h>
Igor Murashkinbfc99152013-02-27 12:55:20 -080029#include <camera/ICameraServiceListener.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080030#include <camera/IProCameraUser.h>
31#include <camera/IProCameraCallbacks.h>
32#include <camera/ICamera.h>
33#include <camera/ICameraClient.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070034#include <camera/camera2/ICameraDeviceUser.h>
35#include <camera/camera2/ICameraDeviceCallbacks.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080036
37namespace android {
38
Igor Murashkinbef3f232013-05-30 17:47:38 -070039namespace {
40
41enum {
42 EX_SECURITY = -1,
43 EX_BAD_PARCELABLE = -2,
44 EX_ILLEGAL_ARGUMENT = -3,
45 EX_NULL_POINTER = -4,
46 EX_ILLEGAL_STATE = -5,
47 EX_HAS_REPLY_HEADER = -128, // special; see below
48};
49
50static bool readExceptionCode(Parcel& reply) {
51 int32_t exceptionCode = reply.readExceptionCode();
52
53 if (exceptionCode != 0) {
54 const char* errorMsg;
55 switch(exceptionCode) {
56 case EX_SECURITY:
57 errorMsg = "Security";
58 break;
59 case EX_BAD_PARCELABLE:
60 errorMsg = "BadParcelable";
61 break;
62 case EX_NULL_POINTER:
63 errorMsg = "NullPointer";
64 break;
65 case EX_ILLEGAL_STATE:
66 errorMsg = "IllegalState";
67 break;
68 // Binder should be handling this code inside Parcel::readException
69 // but lets have a to-string here anyway just in case.
70 case EX_HAS_REPLY_HEADER:
71 errorMsg = "HasReplyHeader";
72 break;
73 default:
74 errorMsg = "Unknown";
75 }
76
77 ALOGE("Binder transmission error %s (%d)", errorMsg, exceptionCode);
78 return true;
79 }
80
81 return false;
82}
83
84};
85
Mathias Agopian3cf61352010-02-09 17:46:37 -080086class BpCameraService: public BpInterface<ICameraService>
87{
88public:
89 BpCameraService(const sp<IBinder>& impl)
90 : BpInterface<ICameraService>(impl)
91 {
92 }
93
Chih-Chung Chang35a055b2010-05-06 16:36:58 +080094 // get number of cameras available
95 virtual int32_t getNumberOfCameras()
96 {
97 Parcel data, reply;
98 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
99 remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700100
101 if (readExceptionCode(reply)) return 0;
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800102 return reply.readInt32();
103 }
104
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800105 // get information about a camera
106 virtual status_t getCameraInfo(int cameraId,
107 struct CameraInfo* cameraInfo) {
108 Parcel data, reply;
109 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
110 data.writeInt32(cameraId);
111 remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700112
113 if (readExceptionCode(reply)) return -EPROTO;
114 status_t result = reply.readInt32();
115 if (reply.readInt32() != 0) {
116 cameraInfo->facing = reply.readInt32();
117 cameraInfo->orientation = reply.readInt32();
118 }
119 return result;
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800120 }
121
Igor Murashkine7ee7632013-06-11 18:10:18 -0700122 // connect to camera service (android.hardware.Camera)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700123 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
124 const String16 &clientPackageName, int clientUid,
125 /*out*/
126 sp<ICamera>& device)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800127 {
128 Parcel data, reply;
129 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
130 data.writeStrongBinder(cameraClient->asBinder());
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800131 data.writeInt32(cameraId);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800132 data.writeString16(clientPackageName);
133 data.writeInt32(clientUid);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800134 remote()->transact(BnCameraService::CONNECT, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700135
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700136 if (readExceptionCode(reply)) return -EPROTO;
137 status_t status = reply.readInt32();
138 if (reply.readInt32() != 0) {
139 device = interface_cast<ICamera>(reply.readStrongBinder());
140 }
141 return status;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800142 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800143
144 // connect to camera service (pro client)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700145 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb, int cameraId,
146 const String16 &clientPackageName, int clientUid,
147 /*out*/
148 sp<IProCameraUser>& device)
Igor Murashkin634a5152013-02-20 17:15:11 -0800149 {
150 Parcel data, reply;
151 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
152 data.writeStrongBinder(cameraCb->asBinder());
153 data.writeInt32(cameraId);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800154 data.writeString16(clientPackageName);
155 data.writeInt32(clientUid);
Igor Murashkin634a5152013-02-20 17:15:11 -0800156 remote()->transact(BnCameraService::CONNECT_PRO, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700157
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700158 if (readExceptionCode(reply)) return -EPROTO;
159 status_t status = reply.readInt32();
160 if (reply.readInt32() != 0) {
161 device = interface_cast<IProCameraUser>(reply.readStrongBinder());
162 }
163 return status;
Igor Murashkin634a5152013-02-20 17:15:11 -0800164 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800165
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700166 // connect to camera service (android.hardware.camera2.CameraDevice)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700167 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700168 const sp<ICameraDeviceCallbacks>& cameraCb,
169 int cameraId,
170 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700171 int clientUid,
172 /*out*/
173 sp<ICameraDeviceUser>& device)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700174 {
175 Parcel data, reply;
176 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
177 data.writeStrongBinder(cameraCb->asBinder());
178 data.writeInt32(cameraId);
179 data.writeString16(clientPackageName);
180 data.writeInt32(clientUid);
181 remote()->transact(BnCameraService::CONNECT_DEVICE, data, &reply);
182
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700183 if (readExceptionCode(reply)) return -EPROTO;
184 status_t status = reply.readInt32();
185 if (reply.readInt32() != 0) {
186 device = interface_cast<ICameraDeviceUser>(reply.readStrongBinder());
187 }
188 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700189 }
190
Igor Murashkinbfc99152013-02-27 12:55:20 -0800191 virtual status_t addListener(const sp<ICameraServiceListener>& listener)
192 {
193 Parcel data, reply;
194 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
195 data.writeStrongBinder(listener->asBinder());
196 remote()->transact(BnCameraService::ADD_LISTENER, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700197
198 if (readExceptionCode(reply)) return -EPROTO;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800199 return reply.readInt32();
200 }
201
202 virtual status_t removeListener(const sp<ICameraServiceListener>& listener)
203 {
204 Parcel data, reply;
205 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
206 data.writeStrongBinder(listener->asBinder());
207 remote()->transact(BnCameraService::REMOVE_LISTENER, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700208
209 if (readExceptionCode(reply)) return -EPROTO;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800210 return reply.readInt32();
211 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800212};
213
214IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");
215
216// ----------------------------------------------------------------------
217
218status_t BnCameraService::onTransact(
219 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
220{
221 switch(code) {
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800222 case GET_NUMBER_OF_CAMERAS: {
223 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700224 reply->writeNoException();
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800225 reply->writeInt32(getNumberOfCameras());
226 return NO_ERROR;
227 } break;
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800228 case GET_CAMERA_INFO: {
229 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700230 CameraInfo cameraInfo = CameraInfo();
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800231 memset(&cameraInfo, 0, sizeof(cameraInfo));
232 status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700233 reply->writeNoException();
234 reply->writeInt32(result);
235
236 // Fake a parcelable object here
237 reply->writeInt32(1); // means the parcelable is included
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800238 reply->writeInt32(cameraInfo.facing);
239 reply->writeInt32(cameraInfo.orientation);
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800240 return NO_ERROR;
241 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800242 case CONNECT: {
243 CHECK_INTERFACE(ICameraService, data, reply);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800244 sp<ICameraClient> cameraClient =
245 interface_cast<ICameraClient>(data.readStrongBinder());
246 int32_t cameraId = data.readInt32();
247 const String16 clientName = data.readString16();
248 int32_t clientUid = data.readInt32();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700249 sp<ICamera> camera;
250 status_t status = connect(cameraClient, cameraId,
251 clientName, clientUid, /*out*/ camera);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700252 reply->writeNoException();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700253 reply->writeInt32(status);
254 if (camera != NULL) {
255 reply->writeInt32(1);
256 reply->writeStrongBinder(camera->asBinder());
257 } else {
258 reply->writeInt32(0);
259 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800260 return NO_ERROR;
261 } break;
Igor Murashkin634a5152013-02-20 17:15:11 -0800262 case CONNECT_PRO: {
263 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700264 sp<IProCameraCallbacks> cameraClient =
265 interface_cast<IProCameraCallbacks>(data.readStrongBinder());
Igor Murashkinc073ba52013-02-26 14:32:34 -0800266 int32_t cameraId = data.readInt32();
267 const String16 clientName = data.readString16();
268 int32_t clientUid = data.readInt32();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700269 sp<IProCameraUser> camera;
270 status_t status = connectPro(cameraClient, cameraId,
271 clientName, clientUid, /*out*/ camera);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700272 reply->writeNoException();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700273 reply->writeInt32(status);
274 if (camera != NULL) {
275 reply->writeInt32(1);
276 reply->writeStrongBinder(camera->asBinder());
277 } else {
278 reply->writeInt32(0);
279 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800280 return NO_ERROR;
281 } break;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700282 case CONNECT_DEVICE: {
283 CHECK_INTERFACE(ICameraService, data, reply);
284 sp<ICameraDeviceCallbacks> cameraClient =
285 interface_cast<ICameraDeviceCallbacks>(data.readStrongBinder());
286 int32_t cameraId = data.readInt32();
287 const String16 clientName = data.readString16();
288 int32_t clientUid = data.readInt32();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700289 sp<ICameraDeviceUser> camera;
290 status_t status = connectDevice(cameraClient, cameraId,
291 clientName, clientUid, /*out*/ camera);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700292 reply->writeNoException();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700293 reply->writeInt32(status);
294 if (camera != NULL) {
295 reply->writeInt32(1);
296 reply->writeStrongBinder(camera->asBinder());
297 } else {
298 reply->writeInt32(0);
299 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700300 return NO_ERROR;
301 } break;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800302 case ADD_LISTENER: {
303 CHECK_INTERFACE(ICameraService, data, reply);
304 sp<ICameraServiceListener> listener =
305 interface_cast<ICameraServiceListener>(data.readStrongBinder());
Igor Murashkinbef3f232013-05-30 17:47:38 -0700306 reply->writeNoException();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800307 reply->writeInt32(addListener(listener));
308 return NO_ERROR;
309 } break;
310 case REMOVE_LISTENER: {
311 CHECK_INTERFACE(ICameraService, data, reply);
312 sp<ICameraServiceListener> listener =
313 interface_cast<ICameraServiceListener>(data.readStrongBinder());
Igor Murashkinbef3f232013-05-30 17:47:38 -0700314 reply->writeNoException();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800315 reply->writeInt32(removeListener(listener));
316 return NO_ERROR;
317 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800318 default:
319 return BBinder::onTransact(code, data, reply, flags);
320 }
321}
322
323// ----------------------------------------------------------------------------
324
325}; // namespace android