SensorDevice.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  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. #include <inttypes.h>
  17. #include <math.h>
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Atomic.h>
  21. #include <utils/Errors.h>
  22. #include <utils/Singleton.h>
  23. #include <binder/BinderService.h>
  24. #include <binder/Parcel.h>
  25. #include <binder/IServiceManager.h>
  26. #include <hardware/sensors.h>
  27. #include "SensorDevice.h"
  28. #include "SensorService.h"
  29. namespace android {
  30. // ---------------------------------------------------------------------------
  31. ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
  32. SensorDevice::SensorDevice()
  33. : mSensorDevice(0),
  34. mSensorModule(0)
  35. {
  36. status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
  37. (hw_module_t const**)&mSensorModule);
  38. ALOGE_IF(err, "couldn't load %s module (%s)",
  39. SENSORS_HARDWARE_MODULE_ID, strerror(-err));
  40. if (mSensorModule) {
  41. err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
  42. ALOGE_IF(err, "couldn't open device for module %s (%s)",
  43. SENSORS_HARDWARE_MODULE_ID, strerror(-err));
  44. if (mSensorDevice) {
  45. if (mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_1 ||
  46. mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_2) {
  47. ALOGE(">>>> WARNING <<< Upgrade sensor HAL to version 1_3");
  48. }
  49. sensor_t const* list;
  50. ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
  51. mActivationCount.setCapacity(count);
  52. Info model;
  53. for (size_t i=0 ; i<size_t(count) ; i++) {
  54. mActivationCount.add(list[i].handle, model);
  55. mSensorDevice->activate(
  56. reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
  57. list[i].handle, 0);
  58. }
  59. }
  60. }
  61. }
  62. void SensorDevice::dump(String8& result)
  63. {
  64. if (!mSensorModule) return;
  65. sensor_t const* list;
  66. ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
  67. result.appendFormat("halVersion 0x%08x\n", getHalDeviceVersion());
  68. result.appendFormat("%d h/w sensors:\n", int(count));
  69. Mutex::Autolock _l(mLock);
  70. for (size_t i=0 ; i<size_t(count) ; i++) {
  71. const Info& info = mActivationCount.valueFor(list[i].handle);
  72. if (info.batchParams.isEmpty()) continue;
  73. result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle,
  74. info.batchParams.size());
  75. for (size_t j = 0; j < info.batchParams.size(); j++) {
  76. const BatchParams& params = info.batchParams.valueAt(j);
  77. result.appendFormat("%4.1f%s", params.batchDelay / 1e6f,
  78. j < info.batchParams.size() - 1 ? ", " : "");
  79. }
  80. result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f);
  81. result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle,
  82. info.batchParams.size());
  83. for (size_t j = 0; j < info.batchParams.size(); j++) {
  84. BatchParams params = info.batchParams.valueAt(j);
  85. result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f,
  86. j < info.batchParams.size() - 1 ? ", " : "");
  87. }
  88. result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f);
  89. }
  90. }
  91. ssize_t SensorDevice::getSensorList(sensor_t const** list) {
  92. if (!mSensorModule) return NO_INIT;
  93. ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
  94. return count;
  95. }
  96. status_t SensorDevice::initCheck() const {
  97. return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
  98. }
  99. ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
  100. if (!mSensorDevice) return NO_INIT;
  101. ssize_t c;
  102. do {
  103. c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice),
  104. buffer, count);
  105. } while (c == -EINTR);
  106. return c;
  107. }
  108. void SensorDevice::autoDisable(void *ident, int handle) {
  109. Info& info( mActivationCount.editValueFor(handle) );
  110. Mutex::Autolock _l(mLock);
  111. info.removeBatchParamsForIdent(ident);
  112. }
  113. status_t SensorDevice::activate(void* ident, int handle, int enabled)
  114. {
  115. if (!mSensorDevice) return NO_INIT;
  116. status_t err(NO_ERROR);
  117. bool actuateHardware = false;
  118. Mutex::Autolock _l(mLock);
  119. Info& info( mActivationCount.editValueFor(handle) );
  120. ALOGD_IF(DEBUG_CONNECTIONS,
  121. "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
  122. ident, handle, enabled, info.batchParams.size());
  123. if (enabled) {
  124. ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
  125. if (isClientDisabledLocked(ident)) {
  126. return INVALID_OPERATION;
  127. }
  128. if (info.batchParams.indexOfKey(ident) >= 0) {
  129. if (info.numActiveClients() == 1) {
  130. // This is the first connection, we need to activate the underlying h/w sensor.
  131. actuateHardware = true;
  132. }
  133. } else {
  134. // Log error. Every activate call should be preceded by a batch() call.
  135. ALOGE("\t >>>ERROR: activate called without batch");
  136. }
  137. } else {
  138. ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
  139. if (info.removeBatchParamsForIdent(ident) >= 0) {
  140. if (info.numActiveClients() == 0) {
  141. // This is the last connection, we need to de-activate the underlying h/w sensor.
  142. actuateHardware = true;
  143. } else {
  144. const int halVersion = getHalDeviceVersion();
  145. if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
  146. // Call batch for this sensor with the previously calculated best effort
  147. // batch_rate and timeout. One of the apps has unregistered for sensor
  148. // events, and the best effort batch parameters might have changed.
  149. ALOGD_IF(DEBUG_CONNECTIONS,
  150. "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle,
  151. info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
  152. info.bestBatchParams.batchTimeout);
  153. mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags,
  154. info.bestBatchParams.batchDelay,
  155. info.bestBatchParams.batchTimeout);
  156. }
  157. }
  158. } else {
  159. // sensor wasn't enabled for this ident
  160. }
  161. if (isClientDisabledLocked(ident)) {
  162. return NO_ERROR;
  163. }
  164. }
  165. if (actuateHardware) {
  166. ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
  167. enabled);
  168. err = mSensorDevice->activate(
  169. reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled);
  170. ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
  171. strerror(-err));
  172. if (err != NO_ERROR && enabled) {
  173. // Failure when enabling the sensor. Clean up on failure.
  174. info.removeBatchParamsForIdent(ident);
  175. }
  176. }
  177. // On older devices which do not support batch, call setDelay().
  178. if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.numActiveClients() > 0) {
  179. ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle,
  180. info.bestBatchParams.batchDelay);
  181. mSensorDevice->setDelay(
  182. reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
  183. handle, info.bestBatchParams.batchDelay);
  184. }
  185. return err;
  186. }
  187. status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
  188. int64_t maxBatchReportLatencyNs) {
  189. if (!mSensorDevice) return NO_INIT;
  190. if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
  191. samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
  192. }
  193. const int halVersion = getHalDeviceVersion();
  194. if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) {
  195. // Batch is not supported on older devices return invalid operation.
  196. return INVALID_OPERATION;
  197. }
  198. ALOGD_IF(DEBUG_CONNECTIONS,
  199. "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
  200. ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
  201. Mutex::Autolock _l(mLock);
  202. Info& info(mActivationCount.editValueFor(handle));
  203. if (info.batchParams.indexOfKey(ident) < 0) {
  204. BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs);
  205. info.batchParams.add(ident, params);
  206. } else {
  207. // A batch has already been called with this ident. Update the batch parameters.
  208. info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
  209. }
  210. BatchParams prevBestBatchParams = info.bestBatchParams;
  211. // Find the minimum of all timeouts and batch_rates for this sensor.
  212. info.selectBatchParams();
  213. ALOGD_IF(DEBUG_CONNECTIONS,
  214. "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
  215. " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
  216. prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay,
  217. prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout);
  218. status_t err(NO_ERROR);
  219. // If the min period or min timeout has changed since the last batch call, call batch.
  220. if (prevBestBatchParams != info.bestBatchParams) {
  221. if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
  222. ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle,
  223. info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
  224. info.bestBatchParams.batchTimeout);
  225. err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags,
  226. info.bestBatchParams.batchDelay,
  227. info.bestBatchParams.batchTimeout);
  228. } else {
  229. // For older devices which do not support batch, call setDelay() after activate() is
  230. // called. Some older devices may not support calling setDelay before activate(), so
  231. // call setDelay in SensorDevice::activate() method.
  232. }
  233. if (err != NO_ERROR) {
  234. ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s",
  235. mSensorDevice, handle,
  236. info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
  237. info.bestBatchParams.batchTimeout, strerror(-err));
  238. info.removeBatchParamsForIdent(ident);
  239. }
  240. }
  241. return err;
  242. }
  243. status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs)
  244. {
  245. if (!mSensorDevice) return NO_INIT;
  246. if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
  247. samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
  248. }
  249. Mutex::Autolock _l(mLock);
  250. if (isClientDisabledLocked(ident)) return INVALID_OPERATION;
  251. Info& info( mActivationCount.editValueFor(handle) );
  252. // If the underlying sensor is NOT in continuous mode, setDelay() should return an error.
  253. // Calling setDelay() in batch mode is an invalid operation.
  254. if (info.bestBatchParams.batchTimeout != 0) {
  255. return INVALID_OPERATION;
  256. }
  257. ssize_t index = info.batchParams.indexOfKey(ident);
  258. if (index < 0) {
  259. return BAD_INDEX;
  260. }
  261. BatchParams& params = info.batchParams.editValueAt(index);
  262. params.batchDelay = samplingPeriodNs;
  263. info.selectBatchParams();
  264. return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
  265. handle, info.bestBatchParams.batchDelay);
  266. }
  267. int SensorDevice::getHalDeviceVersion() const {
  268. if (!mSensorDevice) return -1;
  269. return mSensorDevice->common.version;
  270. }
  271. status_t SensorDevice::flush(void* ident, int handle) {
  272. if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) {
  273. return INVALID_OPERATION;
  274. }
  275. if (isClientDisabled(ident)) return INVALID_OPERATION;
  276. ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
  277. return mSensorDevice->flush(mSensorDevice, handle);
  278. }
  279. bool SensorDevice::isClientDisabled(void* ident) {
  280. Mutex::Autolock _l(mLock);
  281. return isClientDisabledLocked(ident);
  282. }
  283. bool SensorDevice::isClientDisabledLocked(void* ident) {
  284. return mDisabledClients.indexOf(ident) >= 0;
  285. }
  286. void SensorDevice::enableAllSensors() {
  287. Mutex::Autolock _l(mLock);
  288. mDisabledClients.clear();
  289. const int halVersion = getHalDeviceVersion();
  290. for (size_t i = 0; i< mActivationCount.size(); ++i) {
  291. Info& info = mActivationCount.editValueAt(i);
  292. if (info.batchParams.isEmpty()) continue;
  293. info.selectBatchParams();
  294. const int sensor_handle = mActivationCount.keyAt(i);
  295. ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ",
  296. sensor_handle);
  297. status_t err(NO_ERROR);
  298. if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) {
  299. err = mSensorDevice->batch(mSensorDevice, sensor_handle,
  300. info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
  301. info.bestBatchParams.batchTimeout);
  302. ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err));
  303. }
  304. if (err == NO_ERROR) {
  305. err = mSensorDevice->activate(
  306. reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
  307. sensor_handle, 1);
  308. ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err));
  309. }
  310. if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0) {
  311. err = mSensorDevice->setDelay(
  312. reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
  313. sensor_handle, info.bestBatchParams.batchDelay);
  314. ALOGE_IF(err, "Error calling setDelay sensor %d (%s)", sensor_handle, strerror(-err));
  315. }
  316. }
  317. }
  318. void SensorDevice::disableAllSensors() {
  319. Mutex::Autolock _l(mLock);
  320. for (size_t i = 0; i< mActivationCount.size(); ++i) {
  321. const Info& info = mActivationCount.valueAt(i);
  322. // Check if this sensor has been activated previously and disable it.
  323. if (info.batchParams.size() > 0) {
  324. const int sensor_handle = mActivationCount.keyAt(i);
  325. ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
  326. sensor_handle);
  327. mSensorDevice->activate(
  328. reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice),
  329. sensor_handle, 0);
  330. // Add all the connections that were registered for this sensor to the disabled
  331. // clients list.
  332. for (size_t j = 0; j < info.batchParams.size(); ++j) {
  333. mDisabledClients.add(info.batchParams.keyAt(j));
  334. }
  335. }
  336. }
  337. }
  338. status_t SensorDevice::injectSensorData(const sensors_event_t *injected_sensor_event) {
  339. ALOGD_IF(DEBUG_CONNECTIONS,
  340. "sensor_event handle=%d ts=%lld data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
  341. injected_sensor_event->sensor,
  342. injected_sensor_event->timestamp, injected_sensor_event->data[0],
  343. injected_sensor_event->data[1], injected_sensor_event->data[2],
  344. injected_sensor_event->data[3], injected_sensor_event->data[4],
  345. injected_sensor_event->data[5]);
  346. if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
  347. return INVALID_OPERATION;
  348. }
  349. return mSensorDevice->inject_sensor_data(mSensorDevice, injected_sensor_event);
  350. }
  351. status_t SensorDevice::setMode(uint32_t mode) {
  352. if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
  353. return INVALID_OPERATION;
  354. }
  355. return mSensorModule->set_operation_mode(mode);
  356. }
  357. // ---------------------------------------------------------------------------
  358. int SensorDevice::Info::numActiveClients() {
  359. SensorDevice& device(SensorDevice::getInstance());
  360. int num = 0;
  361. for (size_t i = 0; i < batchParams.size(); ++i) {
  362. if (!device.isClientDisabledLocked(batchParams.keyAt(i))) {
  363. ++num;
  364. }
  365. }
  366. return num;
  367. }
  368. status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags,
  369. int64_t samplingPeriodNs,
  370. int64_t maxBatchReportLatencyNs) {
  371. ssize_t index = batchParams.indexOfKey(ident);
  372. if (index < 0) {
  373. ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)",
  374. ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
  375. return BAD_INDEX;
  376. }
  377. BatchParams& params = batchParams.editValueAt(index);
  378. params.flags = flags;
  379. params.batchDelay = samplingPeriodNs;
  380. params.batchTimeout = maxBatchReportLatencyNs;
  381. return NO_ERROR;
  382. }
  383. void SensorDevice::Info::selectBatchParams() {
  384. BatchParams bestParams(0, -1, -1);
  385. SensorDevice& device(SensorDevice::getInstance());
  386. for (size_t i = 0; i < batchParams.size(); ++i) {
  387. if (device.isClientDisabledLocked(batchParams.keyAt(i))) continue;
  388. BatchParams params = batchParams.valueAt(i);
  389. if (bestParams.batchDelay == -1 || params.batchDelay < bestParams.batchDelay) {
  390. bestParams.batchDelay = params.batchDelay;
  391. }
  392. if (bestParams.batchTimeout == -1 || params.batchTimeout < bestParams.batchTimeout) {
  393. bestParams.batchTimeout = params.batchTimeout;
  394. }
  395. }
  396. bestBatchParams = bestParams;
  397. }
  398. ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
  399. ssize_t idx = batchParams.removeItem(ident);
  400. if (idx >= 0) {
  401. selectBatchParams();
  402. }
  403. return idx;
  404. }
  405. // ---------------------------------------------------------------------------
  406. }; // namespace android