blob: 819e410bf133a1076190ab30d57d2828a6b560cf [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>
Mathias Agopian3cf61352010-02-09 17:46:37 -080034
35namespace android {
36
Igor Murashkinbef3f232013-05-30 17:47:38 -070037namespace {
38
39enum {
40 EX_SECURITY = -1,
41 EX_BAD_PARCELABLE = -2,
42 EX_ILLEGAL_ARGUMENT = -3,
43 EX_NULL_POINTER = -4,
44 EX_ILLEGAL_STATE = -5,
45 EX_HAS_REPLY_HEADER = -128, // special; see below
46};
47
48static bool readExceptionCode(Parcel& reply) {
49 int32_t exceptionCode = reply.readExceptionCode();
50
51 if (exceptionCode != 0) {
52 const char* errorMsg;
53 switch(exceptionCode) {
54 case EX_SECURITY:
55 errorMsg = "Security";
56 break;
57 case EX_BAD_PARCELABLE:
58 errorMsg = "BadParcelable";
59 break;
60 case EX_NULL_POINTER:
61 errorMsg = "NullPointer";
62 break;
63 case EX_ILLEGAL_STATE:
64 errorMsg = "IllegalState";
65 break;
66 // Binder should be handling this code inside Parcel::readException
67 // but lets have a to-string here anyway just in case.
68 case EX_HAS_REPLY_HEADER:
69 errorMsg = "HasReplyHeader";
70 break;
71 default:
72 errorMsg = "Unknown";
73 }
74
75 ALOGE("Binder transmission error %s (%d)", errorMsg, exceptionCode);
76 return true;
77 }
78
79 return false;
80}
81
82};
83
Mathias Agopian3cf61352010-02-09 17:46:37 -080084class BpCameraService: public BpInterface<ICameraService>
85{
86public:
87 BpCameraService(const sp<IBinder>& impl)
88 : BpInterface<ICameraService>(impl)
89 {
90 }
91
Chih-Chung Chang35a055b2010-05-06 16:36:58 +080092 // get number of cameras available
93 virtual int32_t getNumberOfCameras()
94 {
95 Parcel data, reply;
96 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
97 remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -070098
99 if (readExceptionCode(reply)) return 0;
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800100 return reply.readInt32();
101 }
102
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800103 // get information about a camera
104 virtual status_t getCameraInfo(int cameraId,
105 struct CameraInfo* cameraInfo) {
106 Parcel data, reply;
107 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
108 data.writeInt32(cameraId);
109 remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700110
111 if (readExceptionCode(reply)) return -EPROTO;
112 status_t result = reply.readInt32();
113 if (reply.readInt32() != 0) {
114 cameraInfo->facing = reply.readInt32();
115 cameraInfo->orientation = reply.readInt32();
116 }
117 return result;
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800118 }
119
Mathias Agopian3cf61352010-02-09 17:46:37 -0800120 // connect to camera service
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800121 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
122 const String16 &clientPackageName, int clientUid)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800123 {
124 Parcel data, reply;
125 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
126 data.writeStrongBinder(cameraClient->asBinder());
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800127 data.writeInt32(cameraId);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800128 data.writeString16(clientPackageName);
129 data.writeInt32(clientUid);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800130 remote()->transact(BnCameraService::CONNECT, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700131
132 if (readExceptionCode(reply)) return NULL;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800133 return interface_cast<ICamera>(reply.readStrongBinder());
134 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800135
136 // connect to camera service (pro client)
Igor Murashkinc073ba52013-02-26 14:32:34 -0800137 virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId,
138 const String16 &clientPackageName, int clientUid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800139 {
140 Parcel data, reply;
141 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
142 data.writeStrongBinder(cameraCb->asBinder());
143 data.writeInt32(cameraId);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800144 data.writeString16(clientPackageName);
145 data.writeInt32(clientUid);
Igor Murashkin634a5152013-02-20 17:15:11 -0800146 remote()->transact(BnCameraService::CONNECT_PRO, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700147
148 if (readExceptionCode(reply)) return NULL;
Igor Murashkin634a5152013-02-20 17:15:11 -0800149 return interface_cast<IProCameraUser>(reply.readStrongBinder());
150 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800151
152 virtual status_t addListener(const sp<ICameraServiceListener>& listener)
153 {
154 Parcel data, reply;
155 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
156 data.writeStrongBinder(listener->asBinder());
157 remote()->transact(BnCameraService::ADD_LISTENER, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700158
159 if (readExceptionCode(reply)) return -EPROTO;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800160 return reply.readInt32();
161 }
162
163 virtual status_t removeListener(const sp<ICameraServiceListener>& listener)
164 {
165 Parcel data, reply;
166 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
167 data.writeStrongBinder(listener->asBinder());
168 remote()->transact(BnCameraService::REMOVE_LISTENER, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700169
170 if (readExceptionCode(reply)) return -EPROTO;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800171 return reply.readInt32();
172 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800173};
174
175IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");
176
177// ----------------------------------------------------------------------
178
179status_t BnCameraService::onTransact(
180 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
181{
182 switch(code) {
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800183 case GET_NUMBER_OF_CAMERAS: {
184 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700185 reply->writeNoException();
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800186 reply->writeInt32(getNumberOfCameras());
187 return NO_ERROR;
188 } break;
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800189 case GET_CAMERA_INFO: {
190 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700191 CameraInfo cameraInfo = CameraInfo();
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800192 memset(&cameraInfo, 0, sizeof(cameraInfo));
193 status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700194 reply->writeNoException();
195 reply->writeInt32(result);
196
197 // Fake a parcelable object here
198 reply->writeInt32(1); // means the parcelable is included
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800199 reply->writeInt32(cameraInfo.facing);
200 reply->writeInt32(cameraInfo.orientation);
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800201 return NO_ERROR;
202 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800203 case CONNECT: {
204 CHECK_INTERFACE(ICameraService, data, reply);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800205 sp<ICameraClient> cameraClient =
206 interface_cast<ICameraClient>(data.readStrongBinder());
207 int32_t cameraId = data.readInt32();
208 const String16 clientName = data.readString16();
209 int32_t clientUid = data.readInt32();
210 sp<ICamera> camera = connect(cameraClient, cameraId,
211 clientName, clientUid);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700212 reply->writeNoException();
Mathias Agopian3cf61352010-02-09 17:46:37 -0800213 reply->writeStrongBinder(camera->asBinder());
214 return NO_ERROR;
215 } break;
Igor Murashkin634a5152013-02-20 17:15:11 -0800216 case CONNECT_PRO: {
217 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700218 sp<IProCameraCallbacks> cameraClient =
219 interface_cast<IProCameraCallbacks>(data.readStrongBinder());
Igor Murashkinc073ba52013-02-26 14:32:34 -0800220 int32_t cameraId = data.readInt32();
221 const String16 clientName = data.readString16();
222 int32_t clientUid = data.readInt32();
223 sp<IProCameraUser> camera = connect(cameraClient, cameraId,
224 clientName, clientUid);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700225 reply->writeNoException();
Igor Murashkin634a5152013-02-20 17:15:11 -0800226 reply->writeStrongBinder(camera->asBinder());
227 return NO_ERROR;
228 } break;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800229 case ADD_LISTENER: {
230 CHECK_INTERFACE(ICameraService, data, reply);
231 sp<ICameraServiceListener> listener =
232 interface_cast<ICameraServiceListener>(data.readStrongBinder());
Igor Murashkinbef3f232013-05-30 17:47:38 -0700233 reply->writeNoException();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800234 reply->writeInt32(addListener(listener));
235 return NO_ERROR;
236 } break;
237 case REMOVE_LISTENER: {
238 CHECK_INTERFACE(ICameraService, data, reply);
239 sp<ICameraServiceListener> listener =
240 interface_cast<ICameraServiceListener>(data.readStrongBinder());
Igor Murashkinbef3f232013-05-30 17:47:38 -0700241 reply->writeNoException();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800242 reply->writeInt32(removeListener(listener));
243 return NO_ERROR;
244 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800245 default:
246 return BBinder::onTransact(code, data, reply, flags);
247 }
248}
249
250// ----------------------------------------------------------------------------
251
252}; // namespace android