| 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 |  | 
 | 18 | #include <stdint.h> | 
 | 19 | #include <sys/types.h> | 
 | 20 |  | 
 | 21 | #include <binder/Parcel.h> | 
 | 22 | #include <binder/IPCThreadState.h> | 
 | 23 | #include <binder/IServiceManager.h> | 
 | 24 |  | 
 | 25 | #include <camera/ICameraService.h> | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame^] | 26 | #include <camera/ICameraServiceListener.h> | 
| Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 27 | #include <camera/IProCameraUser.h> | 
 | 28 | #include <camera/IProCameraCallbacks.h> | 
 | 29 | #include <camera/ICamera.h> | 
 | 30 | #include <camera/ICameraClient.h> | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 31 |  | 
 | 32 | namespace android { | 
 | 33 |  | 
 | 34 | class BpCameraService: public BpInterface<ICameraService> | 
 | 35 | { | 
 | 36 | public: | 
 | 37 |     BpCameraService(const sp<IBinder>& impl) | 
 | 38 |         : BpInterface<ICameraService>(impl) | 
 | 39 |     { | 
 | 40 |     } | 
 | 41 |  | 
| Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 42 |     // get number of cameras available | 
 | 43 |     virtual int32_t getNumberOfCameras() | 
 | 44 |     { | 
 | 45 |         Parcel data, reply; | 
 | 46 |         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); | 
 | 47 |         remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply); | 
 | 48 |         return reply.readInt32(); | 
 | 49 |     } | 
 | 50 |  | 
| Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 51 |     // get information about a camera | 
 | 52 |     virtual status_t getCameraInfo(int cameraId, | 
 | 53 |                                    struct CameraInfo* cameraInfo) { | 
 | 54 |         Parcel data, reply; | 
 | 55 |         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); | 
 | 56 |         data.writeInt32(cameraId); | 
 | 57 |         remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply); | 
 | 58 |         cameraInfo->facing = reply.readInt32(); | 
 | 59 |         cameraInfo->orientation = reply.readInt32(); | 
 | 60 |         return reply.readInt32(); | 
 | 61 |     } | 
 | 62 |  | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 63 |     // connect to camera service | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 64 |     virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId, | 
 | 65 |                                 const String16 &clientPackageName, int clientUid) | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 66 |     { | 
 | 67 |         Parcel data, reply; | 
 | 68 |         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); | 
 | 69 |         data.writeStrongBinder(cameraClient->asBinder()); | 
| Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 70 |         data.writeInt32(cameraId); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 71 |         data.writeString16(clientPackageName); | 
 | 72 |         data.writeInt32(clientUid); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 73 |         remote()->transact(BnCameraService::CONNECT, data, &reply); | 
 | 74 |         return interface_cast<ICamera>(reply.readStrongBinder()); | 
 | 75 |     } | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 76 |  | 
 | 77 |     // connect to camera service (pro client) | 
| Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 78 |     virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId, | 
 | 79 |                                        const String16 &clientPackageName, int clientUid) | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 80 |     { | 
 | 81 |         Parcel data, reply; | 
 | 82 |         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); | 
 | 83 |         data.writeStrongBinder(cameraCb->asBinder()); | 
 | 84 |         data.writeInt32(cameraId); | 
| Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 85 |         data.writeString16(clientPackageName); | 
 | 86 |         data.writeInt32(clientUid); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 87 |         remote()->transact(BnCameraService::CONNECT_PRO, data, &reply); | 
 | 88 |         return interface_cast<IProCameraUser>(reply.readStrongBinder()); | 
 | 89 |     } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame^] | 90 |  | 
 | 91 |     virtual status_t addListener(const sp<ICameraServiceListener>& listener) | 
 | 92 |     { | 
 | 93 |         Parcel data, reply; | 
 | 94 |         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); | 
 | 95 |         data.writeStrongBinder(listener->asBinder()); | 
 | 96 |         remote()->transact(BnCameraService::ADD_LISTENER, data, &reply); | 
 | 97 |         return reply.readInt32(); | 
 | 98 |     } | 
 | 99 |  | 
 | 100 |     virtual status_t removeListener(const sp<ICameraServiceListener>& listener) | 
 | 101 |     { | 
 | 102 |         Parcel data, reply; | 
 | 103 |         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor()); | 
 | 104 |         data.writeStrongBinder(listener->asBinder()); | 
 | 105 |         remote()->transact(BnCameraService::REMOVE_LISTENER, data, &reply); | 
 | 106 |         return reply.readInt32(); | 
 | 107 |     } | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 108 | }; | 
 | 109 |  | 
 | 110 | IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService"); | 
 | 111 |  | 
 | 112 | // ---------------------------------------------------------------------- | 
 | 113 |  | 
 | 114 | status_t BnCameraService::onTransact( | 
 | 115 |     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
 | 116 | { | 
 | 117 |     switch(code) { | 
| Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 118 |         case GET_NUMBER_OF_CAMERAS: { | 
 | 119 |             CHECK_INTERFACE(ICameraService, data, reply); | 
 | 120 |             reply->writeInt32(getNumberOfCameras()); | 
 | 121 |             return NO_ERROR; | 
 | 122 |         } break; | 
| Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 123 |         case GET_CAMERA_INFO: { | 
 | 124 |             CHECK_INTERFACE(ICameraService, data, reply); | 
 | 125 |             CameraInfo cameraInfo; | 
 | 126 |             memset(&cameraInfo, 0, sizeof(cameraInfo)); | 
 | 127 |             status_t result = getCameraInfo(data.readInt32(), &cameraInfo); | 
 | 128 |             reply->writeInt32(cameraInfo.facing); | 
 | 129 |             reply->writeInt32(cameraInfo.orientation); | 
 | 130 |             reply->writeInt32(result); | 
 | 131 |             return NO_ERROR; | 
 | 132 |         } break; | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 133 |         case CONNECT: { | 
 | 134 |             CHECK_INTERFACE(ICameraService, data, reply); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 135 |             sp<ICameraClient> cameraClient = | 
 | 136 |                     interface_cast<ICameraClient>(data.readStrongBinder()); | 
 | 137 |             int32_t cameraId = data.readInt32(); | 
 | 138 |             const String16 clientName = data.readString16(); | 
 | 139 |             int32_t clientUid = data.readInt32(); | 
 | 140 |             sp<ICamera> camera = connect(cameraClient, cameraId, | 
 | 141 |                     clientName, clientUid); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 142 |             reply->writeStrongBinder(camera->asBinder()); | 
 | 143 |             return NO_ERROR; | 
 | 144 |         } break; | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 145 |         case CONNECT_PRO: { | 
 | 146 |             CHECK_INTERFACE(ICameraService, data, reply); | 
 | 147 |             sp<IProCameraCallbacks> cameraClient = interface_cast<IProCameraCallbacks>(data.readStrongBinder()); | 
| Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 148 |             int32_t cameraId = data.readInt32(); | 
 | 149 |             const String16 clientName = data.readString16(); | 
 | 150 |             int32_t clientUid = data.readInt32(); | 
 | 151 |             sp<IProCameraUser> camera = connect(cameraClient, cameraId, | 
 | 152 |                                                 clientName, clientUid); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 153 |             reply->writeStrongBinder(camera->asBinder()); | 
 | 154 |             return NO_ERROR; | 
 | 155 |         } break; | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame^] | 156 |         case ADD_LISTENER: { | 
 | 157 |             CHECK_INTERFACE(ICameraService, data, reply); | 
 | 158 |             sp<ICameraServiceListener> listener = | 
 | 159 |                 interface_cast<ICameraServiceListener>(data.readStrongBinder()); | 
 | 160 |             reply->writeInt32(addListener(listener)); | 
 | 161 |             return NO_ERROR; | 
 | 162 |         } break; | 
 | 163 |         case REMOVE_LISTENER: { | 
 | 164 |             CHECK_INTERFACE(ICameraService, data, reply); | 
 | 165 |             sp<ICameraServiceListener> listener = | 
 | 166 |                 interface_cast<ICameraServiceListener>(data.readStrongBinder()); | 
 | 167 |             reply->writeInt32(removeListener(listener)); | 
 | 168 |             return NO_ERROR; | 
 | 169 |         } break; | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 170 |         default: | 
 | 171 |             return BBinder::onTransact(code, data, reply, flags); | 
 | 172 |     } | 
 | 173 | } | 
 | 174 |  | 
 | 175 | // ---------------------------------------------------------------------------- | 
 | 176 |  | 
 | 177 | }; // namespace android |