• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
26 #include <camera/ICameraServiceListener.h>
27 #include <camera/IProCameraUser.h>
28 #include <camera/IProCameraCallbacks.h>
29 #include <camera/ICamera.h>
30 #include <camera/ICameraClient.h>
31 
32 namespace android {
33 
34 class BpCameraService: public BpInterface<ICameraService>
35 {
36 public:
BpCameraService(const sp<IBinder> & impl)37     BpCameraService(const sp<IBinder>& impl)
38         : BpInterface<ICameraService>(impl)
39     {
40     }
41 
42     // get number of cameras available
getNumberOfCameras()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 
51     // get information about a camera
getCameraInfo(int cameraId,struct CameraInfo * cameraInfo)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 
63     // connect to camera service
connect(const sp<ICameraClient> & cameraClient,int cameraId,const String16 & clientPackageName,int clientUid)64     virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
65                                 const String16 &clientPackageName, int clientUid)
66     {
67         Parcel data, reply;
68         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
69         data.writeStrongBinder(cameraClient->asBinder());
70         data.writeInt32(cameraId);
71         data.writeString16(clientPackageName);
72         data.writeInt32(clientUid);
73         remote()->transact(BnCameraService::CONNECT, data, &reply);
74         return interface_cast<ICamera>(reply.readStrongBinder());
75     }
76 
77     // connect to camera service (pro client)
connect(const sp<IProCameraCallbacks> & cameraCb,int cameraId,const String16 & clientPackageName,int clientUid)78     virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId,
79                                        const String16 &clientPackageName, int clientUid)
80     {
81         Parcel data, reply;
82         data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
83         data.writeStrongBinder(cameraCb->asBinder());
84         data.writeInt32(cameraId);
85         data.writeString16(clientPackageName);
86         data.writeInt32(clientUid);
87         remote()->transact(BnCameraService::CONNECT_PRO, data, &reply);
88         return interface_cast<IProCameraUser>(reply.readStrongBinder());
89     }
90 
addListener(const sp<ICameraServiceListener> & listener)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 
removeListener(const sp<ICameraServiceListener> & listener)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     }
108 };
109 
110 IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");
111 
112 // ----------------------------------------------------------------------
113 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)114 status_t BnCameraService::onTransact(
115     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
116 {
117     switch(code) {
118         case GET_NUMBER_OF_CAMERAS: {
119             CHECK_INTERFACE(ICameraService, data, reply);
120             reply->writeInt32(getNumberOfCameras());
121             return NO_ERROR;
122         } break;
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;
133         case CONNECT: {
134             CHECK_INTERFACE(ICameraService, data, reply);
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);
142             reply->writeStrongBinder(camera->asBinder());
143             return NO_ERROR;
144         } break;
145         case CONNECT_PRO: {
146             CHECK_INTERFACE(ICameraService, data, reply);
147             sp<IProCameraCallbacks> cameraClient = interface_cast<IProCameraCallbacks>(data.readStrongBinder());
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);
153             reply->writeStrongBinder(camera->asBinder());
154             return NO_ERROR;
155         } break;
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;
170         default:
171             return BBinder::onTransact(code, data, reply, flags);
172     }
173 }
174 
175 // ----------------------------------------------------------------------------
176 
177 }; // namespace android
178