1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <sys/types.h>
#include <utils/SortedVector.h>
#include <utils/KeyedVector.h>
#include <utils/threads.h>
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Singleton.h>
#include <binder/BinderService.h>
#include <binder/IServiceManager.h>
#include <gui/ISensorServer.h>
#include <gui/ISensorEventConnection.h>
#include <hardware/sensors.h>
#include "SensorService.h"
namespace android {
// ---------------------------------------------------------------------------
/*
* TODO:
* - handle per-connection event rate
* - filter events per connection
* - make sure to keep the last value of each event type so we can quickly
* send something to application when they enable a sensor that is already
* active (the issue here is that it can take time before a value is
* produced by the h/w if the rate is low or if it's a one-shot sensor).
* - send sensor info to battery service
*/
// ---------------------------------------------------------------------------
class BatteryService : public Singleton<BatteryService> {
friend class Singleton<BatteryService>;
sp<IBinder> mBatteryStatService;
BatteryService() {
const String16 name("batteryinfo");
//getService(name, &mBatteryStatService);
}
public:
void enableSensor(int handle) {
if (mBatteryStatService != 0) {
int uid = IPCThreadState::self()->getCallingUid();
//mBatteryStatService->noteStartSensor(uid, handle);
}
}
void disableSensor(int handle) {
if (mBatteryStatService != 0) {
int uid = IPCThreadState::self()->getCallingUid();
//mBatteryStatService->noteStopSensor(uid, handle);
}
}
};
ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
// ---------------------------------------------------------------------------
SensorService::SensorService()
: Thread(false),
mSensorDevice(0),
mSensorModule(0),
mDump("android.permission.DUMP"),
mInitCheck(NO_INIT)
{
}
void SensorService::onFirstRef()
{
LOGD("nuSensorService starting...");
status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
(hw_module_t const**)&mSensorModule);
LOGE_IF(err, "couldn't load %s module (%s)",
SENSORS_HARDWARE_MODULE_ID, strerror(-err));
if (mSensorModule) {
err = sensors_open(&mSensorModule->common, &mSensorDevice);
LOGE_IF(err, "couldn't open device for module %s (%s)",
SENSORS_HARDWARE_MODULE_ID, strerror(-err));
struct sensor_t const* list;
int count = mSensorModule->get_sensors_list(mSensorModule, &list);
for (int i=0 ; i<count ; i++) {
Sensor sensor(list + i);
LOGI("%s", sensor.getName().string());
mSensorList.add(sensor);
if (mSensorDevice) {
mSensorDevice->activate(mSensorDevice, sensor.getHandle(), 0);
}
}
if (mSensorDevice) {
run("SensorService", PRIORITY_URGENT_DISPLAY);
mInitCheck = NO_ERROR;
}
}
}
SensorService::~SensorService()
{
}
status_t SensorService::dump(int fd, const Vector<String16>& args)
{
const size_t SIZE = 1024;
char buffer[SIZE];
String8 result;
if (!mDump.checkCalling()) {
snprintf(buffer, SIZE, "Permission Denial: "
"can't dump SurfaceFlinger from pid=%d, uid=%d\n",
IPCThreadState::self()->getCallingPid(),
IPCThreadState::self()->getCallingUid());
result.append(buffer);
} else {
Mutex::Autolock _l(mLock);
snprintf(buffer, SIZE, "%d connections / %d active\n",
mConnections.size(), mActiveConnections.size());
result.append(buffer);
snprintf(buffer, SIZE, "Active sensors:\n");
result.append(buffer);
for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
int handle = mActiveSensors.keyAt(i);
snprintf(buffer, SIZE, "%s (handle=%d, connections=%d)\n",
getSensorName(handle).string(),
handle,
mActiveSensors.valueAt(i)->getNumConnections());
result.append(buffer);
}
}
write(fd, result.string(), result.size());
return NO_ERROR;
}
bool SensorService::threadLoop()
{
LOGD("nuSensorService thread starting...");
sensors_event_t buffer[16];
struct sensors_poll_device_t* device = mSensorDevice;
ssize_t count;
do {
count = device->poll(device, buffer, sizeof(buffer)/sizeof(*buffer));
if (count<0) {
LOGE("sensor poll failed (%s)", strerror(-count));
break;
}
const SortedVector< wp<SensorEventConnection> > activeConnections(
getActiveConnections());
size_t numConnections = activeConnections.size();
if (numConnections) {
for (size_t i=0 ; i<numConnections ; i++) {
sp<SensorEventConnection> connection(activeConnections[i].promote());
if (connection != 0) {
connection->sendEvents(buffer, count);
}
}
}
} while (count >= 0 || Thread::exitPending());
LOGW("Exiting SensorService::threadLoop!");
return false;
}
SortedVector< wp<SensorService::SensorEventConnection> >
SensorService::getActiveConnections() const
{
Mutex::Autolock _l(mLock);
return mActiveConnections;
}
String8 SensorService::getSensorName(int handle) const {
size_t count = mSensorList.size();
for (size_t i=0 ; i<count ; i++) {
const Sensor& sensor(mSensorList[i]);
if (sensor.getHandle() == handle) {
return sensor.getName();
}
}
String8 result("unknown");
return result;
}
Vector<Sensor> SensorService::getSensorList()
{
return mSensorList;
}
sp<ISensorEventConnection> SensorService::createSensorEventConnection()
{
sp<SensorEventConnection> result(new SensorEventConnection(this));
Mutex::Autolock _l(mLock);
mConnections.add(result);
return result;
}
void SensorService::cleanupConnection(const wp<SensorEventConnection>& connection)
{
Mutex::Autolock _l(mLock);
ssize_t index = mConnections.indexOf(connection);
if (index >= 0) {
size_t size = mActiveSensors.size();
for (size_t i=0 ; i<size ; ) {
SensorRecord* rec = mActiveSensors.valueAt(i);
if (rec && rec->removeConnection(connection)) {
mSensorDevice->activate(mSensorDevice, mActiveSensors.keyAt(i), 0);
mActiveSensors.removeItemsAt(i, 1);
delete rec;
size--;
} else {
i++;
}
}
mActiveConnections.remove(connection);
mConnections.removeItemsAt(index, 1);
}
}
status_t SensorService::enable(const sp<SensorEventConnection>& connection,
int handle)
{
if (mInitCheck != NO_ERROR)
return mInitCheck;
status_t err = NO_ERROR;
Mutex::Autolock _l(mLock);
SensorRecord* rec = mActiveSensors.valueFor(handle);
if (rec == 0) {
rec = new SensorRecord(connection);
mActiveSensors.add(handle, rec);
err = mSensorDevice->activate(mSensorDevice, handle, 1);
LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err));
if (err == 0) {
BatteryService::getInstance().enableSensor(handle);
}
} else {
err = rec->addConnection(connection);
}
if (err == NO_ERROR) {
// connection now active
connection->addSensor(handle);
if (mActiveConnections.indexOf(connection) < 0) {
mActiveConnections.add(connection);
}
}
return err;
}
status_t SensorService::disable(const sp<SensorEventConnection>& connection,
int handle)
{
if (mInitCheck != NO_ERROR)
return mInitCheck;
status_t err = NO_ERROR;
Mutex::Autolock _l(mLock);
SensorRecord* rec = mActiveSensors.valueFor(handle);
LOGW("sensor (handle=%d) is not enabled", handle);
if (rec) {
// see if this connection becomes inactive
connection->removeSensor(handle);
if (connection->hasAnySensor() == false) {
mActiveConnections.remove(connection);
}
// see if this sensor becomes inactive
if (rec->removeConnection(connection)) {
mActiveSensors.removeItem(handle);
delete rec;
err = mSensorDevice->activate(mSensorDevice, handle, 0);
if (err == 0) {
BatteryService::getInstance().disableSensor(handle);
}
}
}
return err;
}
status_t SensorService::setRate(const sp<SensorEventConnection>& connection,
int handle, nsecs_t ns)
{
if (mInitCheck != NO_ERROR)
return mInitCheck;
status_t err = NO_ERROR;
Mutex::Autolock _l(mLock);
err = mSensorDevice->setDelay(mSensorDevice, handle, ns);
// TODO: handle rate per connection
return err;
}
// ---------------------------------------------------------------------------
SensorService::SensorRecord::SensorRecord(
const sp<SensorEventConnection>& connection)
{
mConnections.add(connection);
}
status_t SensorService::SensorRecord::addConnection(
const sp<SensorEventConnection>& connection)
{
if (mConnections.indexOf(connection) < 0) {
mConnections.add(connection);
}
return NO_ERROR;
}
bool SensorService::SensorRecord::removeConnection(
const wp<SensorEventConnection>& connection)
{
ssize_t index = mConnections.indexOf(connection);
if (index >= 0) {
mConnections.removeItemsAt(index, 1);
}
return mConnections.size() ? false : true;
}
// ---------------------------------------------------------------------------
SensorService::SensorEventConnection::SensorEventConnection(
const sp<SensorService>& service)
: mService(service), mChannel(new SensorChannel())
{
}
SensorService::SensorEventConnection::~SensorEventConnection()
{
mService->cleanupConnection(this);
}
void SensorService::SensorEventConnection::onFirstRef()
{
}
void SensorService::SensorEventConnection::addSensor(int32_t handle) {
if (mSensorList.indexOf(handle) <= 0) {
mSensorList.add(handle);
}
}
void SensorService::SensorEventConnection::removeSensor(int32_t handle) {
mSensorList.remove(handle);
}
bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
return mSensorList.indexOf(handle) >= 0;
}
bool SensorService::SensorEventConnection::hasAnySensor() const {
return mSensorList.size() ? true : false;
}
status_t SensorService::SensorEventConnection::sendEvents(
sensors_event_t const* buffer, size_t count)
{
// TODO: we should only send the events for the sensors this connection
// is registered for.
ssize_t size = mChannel->write(buffer, count*sizeof(sensors_event_t));
if (size == -EAGAIN) {
// the destination doesn't accept events anymore, it's probably
// full. For now, we just drop the events on the floor.
LOGW("dropping %d events on the floor", count);
return size;
}
LOGE_IF(size<0, "dropping %d events on the floor (%s)",
count, strerror(-size));
return size < 0 ? size : NO_ERROR;
}
sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
{
return mChannel;
}
status_t SensorService::SensorEventConnection::enableDisable(
int handle, bool enabled)
{
status_t err;
if (enabled) {
err = mService->enable(this, handle);
} else {
err = mService->disable(this, handle);
}
return err;
}
status_t SensorService::SensorEventConnection::setEventRate(
int handle, nsecs_t ns)
{
return mService->setRate(this, handle, ns);
}
// ---------------------------------------------------------------------------
}; // namespace android
|