blob: c9ab9922482e368ca6e459e1b517f9d9a3a4ca28 [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18#include <sys/types.h>
19
20#include <utils/SortedVector.h>
21#include <utils/KeyedVector.h>
22#include <utils/threads.h>
23#include <utils/Atomic.h>
24#include <utils/Errors.h>
25#include <utils/RefBase.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070026#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070027#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070028
29#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070030#include <binder/IServiceManager.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070031
32#include <gui/ISensorServer.h>
33#include <gui/ISensorEventConnection.h>
34
35#include <hardware/sensors.h>
36
37#include "SensorService.h"
38
39namespace android {
40// ---------------------------------------------------------------------------
41
Mathias Agopian451beee2010-07-19 15:03:55 -070042class BatteryService : public Singleton<BatteryService> {
Mathias Agopianc4a930d2010-07-22 22:19:43 -070043 static const int TRANSACTION_noteStartSensor = IBinder::FIRST_CALL_TRANSACTION + 3;
44 static const int TRANSACTION_noteStopSensor = IBinder::FIRST_CALL_TRANSACTION + 4;
45 static const String16 DESCRIPTOR;
46
Mathias Agopian451beee2010-07-19 15:03:55 -070047 friend class Singleton<BatteryService>;
48 sp<IBinder> mBatteryStatService;
Mathias Agopianc4a930d2010-07-22 22:19:43 -070049
Mathias Agopian451beee2010-07-19 15:03:55 -070050 BatteryService() {
Mathias Agopianc4a930d2010-07-22 22:19:43 -070051 const sp<IServiceManager> sm(defaultServiceManager());
52 if (sm != NULL) {
53 const String16 name("batteryinfo");
54 mBatteryStatService = sm->getService(name);
55 }
Mathias Agopian451beee2010-07-19 15:03:55 -070056 }
Mathias Agopianc4a930d2010-07-22 22:19:43 -070057
58 status_t noteStartSensor(int uid, int handle) {
59 Parcel data, reply;
60 data.writeInterfaceToken(DESCRIPTOR);
61 data.writeInt32(uid);
62 data.writeInt32(handle);
63 status_t err = mBatteryStatService->transact(
64 TRANSACTION_noteStartSensor, data, &reply, 0);
65 err = reply.readExceptionCode();
66 return err;
67 }
68
69 status_t noteStopSensor(int uid, int handle) {
70 Parcel data, reply;
71 data.writeInterfaceToken(DESCRIPTOR);
72 data.writeInt32(uid);
73 data.writeInt32(handle);
74 status_t err = mBatteryStatService->transact(
75 TRANSACTION_noteStopSensor, data, &reply, 0);
76 err = reply.readExceptionCode();
77 return err;
78 }
79
Mathias Agopian451beee2010-07-19 15:03:55 -070080public:
81 void enableSensor(int handle) {
82 if (mBatteryStatService != 0) {
83 int uid = IPCThreadState::self()->getCallingUid();
Mathias Agopianc4a930d2010-07-22 22:19:43 -070084 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
85 noteStartSensor(uid, handle);
86 IPCThreadState::self()->restoreCallingIdentity(identity);
Mathias Agopian451beee2010-07-19 15:03:55 -070087 }
88 }
89 void disableSensor(int handle) {
90 if (mBatteryStatService != 0) {
91 int uid = IPCThreadState::self()->getCallingUid();
Mathias Agopianc4a930d2010-07-22 22:19:43 -070092 int64_t identity = IPCThreadState::self()->clearCallingIdentity();
93 noteStopSensor(uid, handle);
94 IPCThreadState::self()->restoreCallingIdentity(identity);
Mathias Agopian451beee2010-07-19 15:03:55 -070095 }
96 }
97};
98
Mathias Agopianc4a930d2010-07-22 22:19:43 -070099const String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats");
100
Mathias Agopian451beee2010-07-19 15:03:55 -0700101ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
102
103// ---------------------------------------------------------------------------
104
Mathias Agopian1cd70002010-07-21 15:59:50 -0700105// 100 events/s max
106static const nsecs_t MINIMUM_EVENT_PERIOD = ms2ns(10);
107
Mathias Agopianfc328812010-07-14 23:41:37 -0700108SensorService::SensorService()
109 : Thread(false),
Mathias Agopian50df2952010-07-19 19:09:10 -0700110 mSensorDevice(0),
111 mSensorModule(0),
112 mDump("android.permission.DUMP"),
113 mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -0700114{
115}
116
117void SensorService::onFirstRef()
118{
Mathias Agopian50df2952010-07-19 19:09:10 -0700119 LOGD("nuSensorService starting...");
120
Mathias Agopianfc328812010-07-14 23:41:37 -0700121 status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
122 (hw_module_t const**)&mSensorModule);
123
124 LOGE_IF(err, "couldn't load %s module (%s)",
125 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
126
Mathias Agopian50df2952010-07-19 19:09:10 -0700127 if (mSensorModule) {
128 err = sensors_open(&mSensorModule->common, &mSensorDevice);
Mathias Agopianfc328812010-07-14 23:41:37 -0700129
Mathias Agopian50df2952010-07-19 19:09:10 -0700130 LOGE_IF(err, "couldn't open device for module %s (%s)",
131 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
Mathias Agopianfc328812010-07-14 23:41:37 -0700132
Mathias Agopian3560fb22010-07-22 21:24:39 -0700133 sensors_event_t event;
134 memset(&event, 0, sizeof(event));
135
Mathias Agopian50df2952010-07-19 19:09:10 -0700136 struct sensor_t const* list;
137 int count = mSensorModule->get_sensors_list(mSensorModule, &list);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700138 mLastEventSeen.setCapacity(count);
Mathias Agopian50df2952010-07-19 19:09:10 -0700139 for (int i=0 ; i<count ; i++) {
140 Sensor sensor(list + i);
141 LOGI("%s", sensor.getName().string());
142 mSensorList.add(sensor);
143 if (mSensorDevice) {
144 mSensorDevice->activate(mSensorDevice, sensor.getHandle(), 0);
145 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700146 mLastEventSeen.add(sensor.getHandle(), event);
Mathias Agopian50df2952010-07-19 19:09:10 -0700147 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700148
Mathias Agopian50df2952010-07-19 19:09:10 -0700149 if (mSensorDevice) {
150 run("SensorService", PRIORITY_URGENT_DISPLAY);
151 mInitCheck = NO_ERROR;
152 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700153 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700154}
155
156SensorService::~SensorService()
157{
158}
159
160status_t SensorService::dump(int fd, const Vector<String16>& args)
161{
162 const size_t SIZE = 1024;
163 char buffer[SIZE];
164 String8 result;
165 if (!mDump.checkCalling()) {
166 snprintf(buffer, SIZE, "Permission Denial: "
167 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
168 IPCThreadState::self()->getCallingPid(),
169 IPCThreadState::self()->getCallingUid());
170 result.append(buffer);
171 } else {
172 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700173 snprintf(buffer, SIZE, "Sensor List:\n");
174 result.append(buffer);
175 for (size_t i=0 ; i<mSensorList.size() ; i++) {
176 const Sensor& s(mSensorList[i]);
177 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian24d72352010-11-05 19:12:58 -0700178 snprintf(buffer, SIZE, "%s (vendor=%s, handle=%d, maxRate=%.2fHz, last=<%5.1f,%5.1f,%5.1f>)\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700179 s.getName().string(),
180 s.getVendor().string(),
181 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700182 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700183 e.data[0], e.data[1], e.data[2]);
184 result.append(buffer);
185 }
186
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700187 snprintf(buffer, SIZE, "%d active connections\n",
188 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700189 result.append(buffer);
190 snprintf(buffer, SIZE, "Active sensors:\n");
191 result.append(buffer);
192 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700193 int handle = mActiveSensors.keyAt(i);
194 snprintf(buffer, SIZE, "%s (handle=%d, connections=%d)\n",
195 getSensorName(handle).string(),
196 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700197 mActiveSensors.valueAt(i)->getNumConnections());
198 result.append(buffer);
199 }
200 }
201 write(fd, result.string(), result.size());
202 return NO_ERROR;
203}
204
205bool SensorService::threadLoop()
206{
207 LOGD("nuSensorService thread starting...");
208
209 sensors_event_t buffer[16];
Mathias Agopiancf510012010-07-22 16:18:10 -0700210 sensors_event_t scratch[16];
Mathias Agopianfc328812010-07-14 23:41:37 -0700211 struct sensors_poll_device_t* device = mSensorDevice;
212 ssize_t count;
213
214 do {
215 count = device->poll(device, buffer, sizeof(buffer)/sizeof(*buffer));
216 if (count<0) {
217 LOGE("sensor poll failed (%s)", strerror(-count));
218 break;
219 }
220
221 const SortedVector< wp<SensorEventConnection> > activeConnections(
222 getActiveConnections());
223
224 size_t numConnections = activeConnections.size();
225 if (numConnections) {
Mathias Agopiancf510012010-07-22 16:18:10 -0700226 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700227
228 // record the last event for each sensor
229 int32_t prev = buffer[0].sensor;
230 for (ssize_t i=1 ; i<count ; i++) {
231 // record the last event of each sensor type in this buffer
232 int32_t curr = buffer[i].sensor;
233 if (curr != prev) {
234 mLastEventSeen.editValueFor(prev) = buffer[i-1];
235 prev = curr;
236 }
237 }
238 mLastEventSeen.editValueFor(prev) = buffer[count-1];
239
Mathias Agopianfc328812010-07-14 23:41:37 -0700240 for (size_t i=0 ; i<numConnections ; i++) {
241 sp<SensorEventConnection> connection(activeConnections[i].promote());
242 if (connection != 0) {
Mathias Agopiancf510012010-07-22 16:18:10 -0700243 connection->sendEvents(buffer, count, scratch);
Mathias Agopianfc328812010-07-14 23:41:37 -0700244 }
245 }
246 }
247
248 } while (count >= 0 || Thread::exitPending());
249
250 LOGW("Exiting SensorService::threadLoop!");
251 return false;
252}
253
254SortedVector< wp<SensorService::SensorEventConnection> >
255SensorService::getActiveConnections() const
256{
257 Mutex::Autolock _l(mLock);
258 return mActiveConnections;
259}
260
Mathias Agopian5d270722010-07-19 15:20:39 -0700261String8 SensorService::getSensorName(int handle) const {
262 size_t count = mSensorList.size();
263 for (size_t i=0 ; i<count ; i++) {
264 const Sensor& sensor(mSensorList[i]);
265 if (sensor.getHandle() == handle) {
266 return sensor.getName();
267 }
268 }
269 String8 result("unknown");
270 return result;
271}
272
Mathias Agopianfc328812010-07-14 23:41:37 -0700273Vector<Sensor> SensorService::getSensorList()
274{
275 return mSensorList;
276}
277
278sp<ISensorEventConnection> SensorService::createSensorEventConnection()
279{
280 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700281 return result;
282}
283
284void SensorService::cleanupConnection(const wp<SensorEventConnection>& connection)
285{
286 Mutex::Autolock _l(mLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700287 size_t size = mActiveSensors.size();
288 for (size_t i=0 ; i<size ; ) {
289 SensorRecord* rec = mActiveSensors.valueAt(i);
290 if (rec && rec->removeConnection(connection)) {
291 mSensorDevice->activate(mSensorDevice, mActiveSensors.keyAt(i), 0);
292 mActiveSensors.removeItemsAt(i, 1);
293 delete rec;
294 size--;
295 } else {
296 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700297 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700298 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700299 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700300}
301
302status_t SensorService::enable(const sp<SensorEventConnection>& connection,
303 int handle)
304{
Mathias Agopian50df2952010-07-19 19:09:10 -0700305 if (mInitCheck != NO_ERROR)
306 return mInitCheck;
307
Mathias Agopianfc328812010-07-14 23:41:37 -0700308 status_t err = NO_ERROR;
309 Mutex::Autolock _l(mLock);
310 SensorRecord* rec = mActiveSensors.valueFor(handle);
311 if (rec == 0) {
312 rec = new SensorRecord(connection);
313 mActiveSensors.add(handle, rec);
314 err = mSensorDevice->activate(mSensorDevice, handle, 1);
315 LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err));
Mathias Agopian451beee2010-07-19 15:03:55 -0700316 if (err == 0) {
317 BatteryService::getInstance().enableSensor(handle);
318 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700319 } else {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700320 if (rec->addConnection(connection)) {
321 // this sensor is already activated, but we are adding a
322 // connection that uses it. Immediately send down the last
323 // known value of the requested sensor.
324 sensors_event_t scratch;
325 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
326 if (event.version == sizeof(sensors_event_t)) {
327 connection->sendEvents(&event, 1);
328 }
329 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700330 }
331 if (err == NO_ERROR) {
332 // connection now active
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700333 if (connection->addSensor(handle)) {
334 // the sensor was added (which means it wasn't already there)
335 // so, see if this connection becomes active
336 if (mActiveConnections.indexOf(connection) < 0) {
337 mActiveConnections.add(connection);
338 }
339 // this could change the sensor event delivery speed
340 recomputeEventsPeriodLocked(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700341 }
342 }
343 return err;
344}
345
346status_t SensorService::disable(const sp<SensorEventConnection>& connection,
347 int handle)
348{
Mathias Agopian50df2952010-07-19 19:09:10 -0700349 if (mInitCheck != NO_ERROR)
350 return mInitCheck;
351
Mathias Agopianfc328812010-07-14 23:41:37 -0700352 status_t err = NO_ERROR;
353 Mutex::Autolock _l(mLock);
354 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700355 if (rec) {
356 // see if this connection becomes inactive
357 connection->removeSensor(handle);
358 if (connection->hasAnySensor() == false) {
359 mActiveConnections.remove(connection);
360 }
361 // see if this sensor becomes inactive
362 if (rec->removeConnection(connection)) {
363 mActiveSensors.removeItem(handle);
364 delete rec;
365 err = mSensorDevice->activate(mSensorDevice, handle, 0);
Mathias Agopian451beee2010-07-19 15:03:55 -0700366 if (err == 0) {
367 BatteryService::getInstance().disableSensor(handle);
368 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700369 }
370 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700371 if (err == NO_ERROR) {
372 recomputeEventsPeriodLocked(handle);
373 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700374 return err;
375}
376
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700377status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700378 int handle, nsecs_t ns)
379{
Mathias Agopian50df2952010-07-19 19:09:10 -0700380 if (mInitCheck != NO_ERROR)
381 return mInitCheck;
382
Mathias Agopian1cd70002010-07-21 15:59:50 -0700383 if (ns < 0)
384 return BAD_VALUE;
385
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700386 if (ns < MINIMUM_EVENTS_PERIOD)
387 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700388
Mathias Agopianfc328812010-07-14 23:41:37 -0700389 Mutex::Autolock _l(mLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700390 status_t err = connection->setEventRateLocked(handle, ns);
391 if (err == NO_ERROR) {
392 recomputeEventsPeriodLocked(handle);
393 }
394 return err;
395}
Mathias Agopianfc328812010-07-14 23:41:37 -0700396
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700397status_t SensorService::recomputeEventsPeriodLocked(int32_t handle)
398{
399 status_t err = NO_ERROR;
400 nsecs_t wanted = ms2ns(1000);
401 size_t count = mActiveConnections.size();
402 for (size_t i=0 ; i<count ; i++) {
403 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
404 if (connection != NULL) {
405 nsecs_t ns = connection->getEventRateForSensor(handle);
406 if (ns) {
407 wanted = wanted < ns ? wanted : ns;
408 }
409 }
410 }
411 err = mSensorDevice->setDelay(mSensorDevice, handle, wanted);
Mathias Agopianfc328812010-07-14 23:41:37 -0700412 return err;
413}
414
415// ---------------------------------------------------------------------------
416
417SensorService::SensorRecord::SensorRecord(
418 const sp<SensorEventConnection>& connection)
419{
420 mConnections.add(connection);
421}
422
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700423bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700424 const sp<SensorEventConnection>& connection)
425{
426 if (mConnections.indexOf(connection) < 0) {
427 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700428 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700429 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700430 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700431}
432
433bool SensorService::SensorRecord::removeConnection(
434 const wp<SensorEventConnection>& connection)
435{
436 ssize_t index = mConnections.indexOf(connection);
437 if (index >= 0) {
438 mConnections.removeItemsAt(index, 1);
439 }
440 return mConnections.size() ? false : true;
441}
442
443// ---------------------------------------------------------------------------
444
445SensorService::SensorEventConnection::SensorEventConnection(
446 const sp<SensorService>& service)
447 : mService(service), mChannel(new SensorChannel())
448{
449}
450
451SensorService::SensorEventConnection::~SensorEventConnection()
452{
453 mService->cleanupConnection(this);
454}
455
456void SensorService::SensorEventConnection::onFirstRef()
457{
458}
459
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700460bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
461 if (mSensorInfo.indexOfKey(handle) <= 0) {
462 SensorInfo info;
463 mSensorInfo.add(handle, info);
464 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700465 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700466 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700467}
468
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700469bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
470 if (mSensorInfo.removeItem(handle) >= 0) {
471 return true;
472 }
473 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700474}
475
476bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700477 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700478}
479
480bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700481 return mSensorInfo.size() ? true : false;
482}
483
484status_t SensorService::SensorEventConnection::setEventRateLocked(
485 int handle, nsecs_t ns)
486{
487 ssize_t index = mSensorInfo.indexOfKey(handle);
488 if (index >= 0) {
489 SensorInfo& info = mSensorInfo.editValueFor(handle);
490 info.ns = ns;
491 return NO_ERROR;
492 }
493 return status_t(index);
Mathias Agopianfc328812010-07-14 23:41:37 -0700494}
495
496status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700497 sensors_event_t const* buffer, size_t numEvents,
498 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700499{
Mathias Agopiancf510012010-07-22 16:18:10 -0700500 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700501 size_t count = 0;
502 if (scratch) {
503 size_t i=0;
504 while (i<numEvents) {
505 const int32_t curr = buffer[i].sensor;
506 if (mSensorInfo.indexOfKey(curr) >= 0) {
507 do {
508 scratch[count++] = buffer[i++];
509 } while ((i<numEvents) && (buffer[i].sensor == curr));
510 } else {
511 i++;
512 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700513 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700514 } else {
515 scratch = const_cast<sensors_event_t *>(buffer);
516 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700517 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700518
Mathias Agopian3560fb22010-07-22 21:24:39 -0700519 if (count == 0)
520 return 0;
521
Mathias Agopiancf510012010-07-22 16:18:10 -0700522 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700523 if (size == -EAGAIN) {
524 // the destination doesn't accept events anymore, it's probably
525 // full. For now, we just drop the events on the floor.
526 LOGW("dropping %d events on the floor", count);
527 return size;
528 }
529
530 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
531 count, strerror(-size));
532
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700533 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700534}
535
536sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
537{
538 return mChannel;
539}
540
541status_t SensorService::SensorEventConnection::enableDisable(
542 int handle, bool enabled)
543{
544 status_t err;
545 if (enabled) {
546 err = mService->enable(this, handle);
547 } else {
548 err = mService->disable(this, handle);
549 }
550 return err;
551}
552
553status_t SensorService::SensorEventConnection::setEventRate(
554 int handle, nsecs_t ns)
555{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700556 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700557}
558
559// ---------------------------------------------------------------------------
560}; // namespace android
561