Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 1 | /* |
| 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 18 | #define LOG_TAG "BpCameraService" |
| 19 | #include <utils/Log.h> |
| 20 | |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 21 | #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 Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 29 | #include <camera/ICameraServiceListener.h> |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 30 | #include <camera/IProCameraUser.h> |
| 31 | #include <camera/IProCameraCallbacks.h> |
| 32 | #include <camera/ICamera.h> |
| 33 | #include <camera/ICameraClient.h> |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 37 | namespace { |
| 38 | |
| 39 | enum { |
| 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 | |
| 48 | static 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 Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 84 | class BpCameraService: public BpInterface<ICameraService> |
| 85 | { |
| 86 | public: |
| 87 | BpCameraService(const sp<IBinder>& impl) |
| 88 | : BpInterface<ICameraService>(impl) |
| 89 | { |
| 90 | } |
| 91 | |
Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 92 | // 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 98 | |
| 99 | if (readExceptionCode(reply)) return 0; |
Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 100 | return reply.readInt32(); |
| 101 | } |
| 102 | |
Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 103 | // 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 110 | |
| 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 Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 120 | // connect to camera service |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 121 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId, |
| 122 | const String16 &clientPackageName, int clientUid) |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 123 | { |
| 124 | Parcel data, reply; |
| 125 | data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); |
| 126 | data.writeStrongBinder(cameraClient->asBinder()); |
Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 127 | data.writeInt32(cameraId); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 128 | data.writeString16(clientPackageName); |
| 129 | data.writeInt32(clientUid); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 130 | remote()->transact(BnCameraService::CONNECT, data, &reply); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 131 | |
| 132 | if (readExceptionCode(reply)) return NULL; |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 133 | return interface_cast<ICamera>(reply.readStrongBinder()); |
| 134 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 135 | |
| 136 | // connect to camera service (pro client) |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 137 | virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId, |
| 138 | const String16 &clientPackageName, int clientUid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 139 | { |
| 140 | Parcel data, reply; |
| 141 | data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); |
| 142 | data.writeStrongBinder(cameraCb->asBinder()); |
| 143 | data.writeInt32(cameraId); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 144 | data.writeString16(clientPackageName); |
| 145 | data.writeInt32(clientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 146 | remote()->transact(BnCameraService::CONNECT_PRO, data, &reply); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 147 | |
| 148 | if (readExceptionCode(reply)) return NULL; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 149 | return interface_cast<IProCameraUser>(reply.readStrongBinder()); |
| 150 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 151 | |
| 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 158 | |
| 159 | if (readExceptionCode(reply)) return -EPROTO; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 160 | 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 169 | |
| 170 | if (readExceptionCode(reply)) return -EPROTO; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 171 | return reply.readInt32(); |
| 172 | } |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService"); |
| 176 | |
| 177 | // ---------------------------------------------------------------------- |
| 178 | |
| 179 | status_t BnCameraService::onTransact( |
| 180 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 181 | { |
| 182 | switch(code) { |
Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 183 | case GET_NUMBER_OF_CAMERAS: { |
| 184 | CHECK_INTERFACE(ICameraService, data, reply); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 185 | reply->writeNoException(); |
Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 186 | reply->writeInt32(getNumberOfCameras()); |
| 187 | return NO_ERROR; |
| 188 | } break; |
Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 189 | case GET_CAMERA_INFO: { |
| 190 | CHECK_INTERFACE(ICameraService, data, reply); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 191 | CameraInfo cameraInfo = CameraInfo(); |
Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 192 | memset(&cameraInfo, 0, sizeof(cameraInfo)); |
| 193 | status_t result = getCameraInfo(data.readInt32(), &cameraInfo); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 194 | reply->writeNoException(); |
| 195 | reply->writeInt32(result); |
| 196 | |
| 197 | // Fake a parcelable object here |
| 198 | reply->writeInt32(1); // means the parcelable is included |
Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 199 | reply->writeInt32(cameraInfo.facing); |
| 200 | reply->writeInt32(cameraInfo.orientation); |
Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 201 | return NO_ERROR; |
| 202 | } break; |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 203 | case CONNECT: { |
| 204 | CHECK_INTERFACE(ICameraService, data, reply); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 205 | 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 212 | reply->writeNoException(); |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 213 | reply->writeStrongBinder(camera->asBinder()); |
| 214 | return NO_ERROR; |
| 215 | } break; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 216 | case CONNECT_PRO: { |
| 217 | CHECK_INTERFACE(ICameraService, data, reply); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 218 | sp<IProCameraCallbacks> cameraClient = |
| 219 | interface_cast<IProCameraCallbacks>(data.readStrongBinder()); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 220 | 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 225 | reply->writeNoException(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 226 | reply->writeStrongBinder(camera->asBinder()); |
| 227 | return NO_ERROR; |
| 228 | } break; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 229 | case ADD_LISTENER: { |
| 230 | CHECK_INTERFACE(ICameraService, data, reply); |
| 231 | sp<ICameraServiceListener> listener = |
| 232 | interface_cast<ICameraServiceListener>(data.readStrongBinder()); |
Igor Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 233 | reply->writeNoException(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 234 | 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 Murashkin | bef3f23 | 2013-05-30 17:47:38 -0700 | [diff] [blame^] | 241 | reply->writeNoException(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 242 | reply->writeInt32(removeListener(listener)); |
| 243 | return NO_ERROR; |
| 244 | } break; |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 245 | default: |
| 246 | return BBinder::onTransact(code, data, reply, flags); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // ---------------------------------------------------------------------------- |
| 251 | |
| 252 | }; // namespace android |