sensors_dummy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2009 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. /* this implements a sensors hardware library for the Android emulator.
  17. * the following code should be built as a shared library that will be
  18. * placed into /system/lib/hw/sensors.goldfish.so
  19. *
  20. * it will be loaded by the code in hardware/libhardware/hardware.c
  21. * which is itself called from com_android_server_SensorService.cpp
  22. */
  23. #define SENSORS_SERVICE_NAME "sensors"
  24. #define LOG_TAG "Dummy_Sensors"
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <log/log.h>
  30. #include <cutils/sockets.h>
  31. #include <hardware/sensors.h>
  32. #include <pthread.h>
  33. #if 0
  34. #define D(...) ALOGD(__VA_ARGS__)
  35. #else
  36. #define D(...) ((void)0)
  37. #endif
  38. #define E(...) ALOGE(__VA_ARGS__)
  39. /** SENSOR IDS AND NAMES
  40. **/
  41. #define MAX_NUM_SENSORS 8
  42. #define SUPPORTED_SENSORS ((1<<MAX_NUM_SENSORS)-1)
  43. #define ID_BASE SENSORS_HANDLE_BASE
  44. #define ID_ACCELERATION (ID_BASE+0)
  45. #define ID_MAGNETIC_FIELD (ID_BASE+1)
  46. #define ID_ORIENTATION (ID_BASE+2)
  47. #define ID_TEMPERATURE (ID_BASE+3)
  48. #define ID_PROXIMITY (ID_BASE+4)
  49. #define ID_LIGHT (ID_BASE+5)
  50. #define ID_PRESSURE (ID_BASE+6)
  51. #define ID_HUMIDITY (ID_BASE+7)
  52. #define SENSORS_ACCELERATION (1 << ID_ACCELERATION)
  53. #define SENSORS_MAGNETIC_FIELD (1 << ID_MAGNETIC_FIELD)
  54. #define SENSORS_ORIENTATION (1 << ID_ORIENTATION)
  55. #define SENSORS_TEMPERATURE (1 << ID_TEMPERATURE)
  56. #define SENSORS_PROXIMITY (1 << ID_PROXIMITY)
  57. #define SENSORS_LIGHT (1 << ID_LIGHT)
  58. #define SENSORS_PRESSURE (1 << ID_PRESSURE)
  59. #define SENSORS_HUMIDITY (1 << ID_HUMIDITY)
  60. #define ID_CHECK(x) ((unsigned)((x) - ID_BASE) < MAX_NUM_SENSORS)
  61. #define SENSORS_LIST \
  62. SENSOR_(ACCELERATION,"acceleration") \
  63. SENSOR_(MAGNETIC_FIELD,"magnetic-field") \
  64. SENSOR_(ORIENTATION,"orientation") \
  65. SENSOR_(TEMPERATURE,"temperature") \
  66. SENSOR_(PROXIMITY,"proximity") \
  67. SENSOR_(LIGHT, "light") \
  68. SENSOR_(PRESSURE, "pressure") \
  69. SENSOR_(HUMIDITY, "humidity")
  70. static const struct {
  71. const char* name;
  72. int id; } _sensorIds[MAX_NUM_SENSORS] =
  73. {
  74. #define SENSOR_(x,y) { y, ID_##x },
  75. SENSORS_LIST
  76. #undef SENSOR_
  77. };
  78. static const char*
  79. _sensorIdToName( int id )
  80. {
  81. int nn;
  82. for (nn = 0; nn < MAX_NUM_SENSORS; nn++)
  83. if (id == _sensorIds[nn].id)
  84. return _sensorIds[nn].name;
  85. return "<UNKNOWN>";
  86. }
  87. static int
  88. _sensorIdFromName( const char* name )
  89. {
  90. int nn;
  91. if (name == NULL)
  92. return -1;
  93. for (nn = 0; nn < MAX_NUM_SENSORS; nn++)
  94. if (!strcmp(name, _sensorIds[nn].name))
  95. return _sensorIds[nn].id;
  96. return -1;
  97. }
  98. /* return the current time in nanoseconds */
  99. static int64_t now_ns(void) {
  100. struct timespec ts;
  101. clock_gettime(CLOCK_MONOTONIC, &ts);
  102. return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
  103. }
  104. /** SENSORS POLL DEVICE
  105. **
  106. ** This one is used to read sensor data from the hardware.
  107. ** We implement this by simply reading the data from the
  108. ** emulator through the QEMUD channel.
  109. **/
  110. typedef struct SensorDevice {
  111. struct sensors_poll_device_1 device;
  112. sensors_event_t sensors[MAX_NUM_SENSORS];
  113. uint32_t pendingSensors;
  114. int64_t timeStart;
  115. int64_t timeOffset;
  116. uint32_t active_sensors;
  117. int fd;
  118. pthread_mutex_t lock;
  119. } SensorDevice;
  120. /* Grab the file descriptor to the emulator's sensors service pipe.
  121. * This function returns a file descriptor on success, or -errno on
  122. * failure, and assumes the SensorDevice instance's lock is held.
  123. *
  124. * This is needed because set_delay(), poll() and activate() can be called
  125. * from different threads, and poll() is blocking.
  126. *
  127. * 1) On a first thread, de-activate() all sensors first, then call poll(),
  128. * which results in the thread blocking.
  129. *
  130. * 2) On a second thread, slightly later, call set_delay() then activate()
  131. * to enable the acceleration sensor.
  132. *
  133. * The system expects this to unblock the first thread which will receive
  134. * new sensor events after the activate() call in 2).
  135. *
  136. * This cannot work if both threads don't use the same connection.
  137. *
  138. * TODO(digit): This protocol is brittle, implement another control channel
  139. * for set_delay()/activate()/batch() when supporting HAL 1.3
  140. */
  141. static int sensor_device_get_fd_locked(SensorDevice* dev) {
  142. /* Create connection to service on first call */
  143. if (dev->fd < 0) {
  144. int ret = -errno;
  145. E("%s: Could not open connection to service: %s", __FUNCTION__,
  146. strerror(-ret));
  147. return ret;
  148. }
  149. return dev->fd;
  150. }
  151. /* Pick up one pending sensor event. On success, this returns the sensor
  152. * id, and sets |*event| accordingly. On failure, i.e. if there are no
  153. * pending events, return -EINVAL.
  154. *
  155. * Note: The device's lock must be acquired.
  156. */
  157. static int sensor_device_pick_pending_event_locked(SensorDevice* d,
  158. sensors_event_t* event)
  159. {
  160. uint32_t mask = SUPPORTED_SENSORS & d->pendingSensors;
  161. if (mask) {
  162. uint32_t i = 31 - __builtin_clz(mask);
  163. pthread_mutex_lock(&d->lock);
  164. d->pendingSensors &= ~(1U << i);
  165. *event = d->sensors[i];
  166. event->sensor = i;
  167. event->version = sizeof(*event);
  168. pthread_mutex_unlock(&d->lock);
  169. D("%s: %d [%f, %f, %f]", __FUNCTION__,
  170. i,
  171. event->data[0],
  172. event->data[1],
  173. event->data[2]);
  174. return i;
  175. }
  176. E("No sensor to return!!! pendingSensors=0x%08x", d->pendingSensors);
  177. // we may end-up in a busy loop, slow things down, just in case.
  178. usleep(1000);
  179. return -EINVAL;
  180. }
  181. static int sensor_device_close(struct hw_device_t* dev0)
  182. {
  183. SensorDevice* dev = (void*)dev0;
  184. // Assume that there are no other threads blocked on poll()
  185. if (dev->fd >= 0) {
  186. close(dev->fd);
  187. dev->fd = -1;
  188. }
  189. pthread_mutex_destroy(&dev->lock);
  190. free(dev);
  191. return 0;
  192. }
  193. /* Return an array of sensor data. This function blocks until there is sensor
  194. * related events to report. On success, it will write the events into the
  195. * |data| array, which contains |count| items. The function returns the number
  196. * of events written into the array, which shall never be greater than |count|.
  197. * On error, return -errno code.
  198. *
  199. * Note that according to the sensor HAL [1], it shall never return 0!
  200. *
  201. * [1] http://source.android.com/devices/sensors/hal-interface.html
  202. */
  203. static int sensor_device_poll(struct sensors_poll_device_t *dev0,
  204. sensors_event_t* data, int count)
  205. {
  206. return -EIO;
  207. }
  208. static int sensor_device_activate(struct sensors_poll_device_t *dev0,
  209. int handle,
  210. int enabled)
  211. {
  212. SensorDevice* dev = (void*)dev0;
  213. D("%s: handle=%s (%d) enabled=%d", __FUNCTION__,
  214. _sensorIdToName(handle), handle, enabled);
  215. /* Sanity check */
  216. if (!ID_CHECK(handle)) {
  217. E("%s: bad handle ID", __FUNCTION__);
  218. return -EINVAL;
  219. }
  220. /* Exit early if sensor is already enabled/disabled. */
  221. uint32_t mask = (1U << handle);
  222. uint32_t sensors = enabled ? mask : 0;
  223. pthread_mutex_lock(&dev->lock);
  224. uint32_t active = dev->active_sensors;
  225. uint32_t new_sensors = (active & ~mask) | (sensors & mask);
  226. uint32_t changed = active ^ new_sensors;
  227. if (changed)
  228. dev->active_sensors = new_sensors;
  229. pthread_mutex_unlock(&dev->lock);
  230. return 0;
  231. }
  232. static int sensor_device_default_flush(
  233. struct sensors_poll_device_1* dev0,
  234. int handle) {
  235. SensorDevice* dev = (void*)dev0;
  236. D("%s: handle=%s (%d)", __FUNCTION__,
  237. _sensorIdToName(handle), handle);
  238. /* Sanity check */
  239. if (!ID_CHECK(handle)) {
  240. E("%s: bad handle ID", __FUNCTION__);
  241. return -EINVAL;
  242. }
  243. pthread_mutex_lock(&dev->lock);
  244. dev->sensors[handle].version = META_DATA_VERSION;
  245. dev->sensors[handle].type = SENSOR_TYPE_META_DATA;
  246. dev->sensors[handle].sensor = 0;
  247. dev->sensors[handle].timestamp = 0;
  248. dev->sensors[handle].meta_data.what = META_DATA_FLUSH_COMPLETE;
  249. dev->pendingSensors |= (1U << handle);
  250. pthread_mutex_unlock(&dev->lock);
  251. return 0;
  252. }
  253. static int sensor_device_set_delay(struct sensors_poll_device_t *dev0,
  254. int handle __unused,
  255. int64_t ns)
  256. {
  257. return 0;
  258. }
  259. static int sensor_device_default_batch(
  260. struct sensors_poll_device_1* dev,
  261. int sensor_handle,
  262. int flags,
  263. int64_t sampling_period_ns,
  264. int64_t max_report_latency_ns) {
  265. return sensor_device_set_delay(dev, sensor_handle, sampling_period_ns);
  266. }
  267. /** MODULE REGISTRATION SUPPORT
  268. **
  269. ** This is required so that hardware/libhardware/hardware.c
  270. ** will dlopen() this library appropriately.
  271. **/
  272. /*
  273. * the following is the list of all supported sensors.
  274. * this table is used to build sSensorList declared below
  275. * according to which hardware sensors are reported as
  276. * available from the emulator (see get_sensors_list below)
  277. *
  278. * note: numerical values for maxRange/resolution/power for
  279. * all sensors but light, pressure and humidity were
  280. * taken from the reference AK8976A implementation
  281. */
  282. static const struct sensor_t sSensorListInit[] = {
  283. { .name = "Accelerometer",
  284. .vendor = "The Android Open Source Project",
  285. .version = 1,
  286. .handle = ID_ACCELERATION,
  287. .type = SENSOR_TYPE_ACCELEROMETER,
  288. .maxRange = 2.8f,
  289. .resolution = 1.0f/4032.0f,
  290. .power = 3.0f,
  291. .minDelay = 10000,
  292. .maxDelay = 60 * 1000 * 1000,
  293. .fifoReservedEventCount = 0,
  294. .fifoMaxEventCount = 0,
  295. .stringType = 0,
  296. .requiredPermission = 0,
  297. .flags = SENSOR_FLAG_CONTINUOUS_MODE,
  298. .reserved = {}
  299. },
  300. };
  301. static struct sensor_t sSensorList[1];
  302. static int sensors__get_sensors_list(struct sensors_module_t* module __unused,
  303. struct sensor_t const** list)
  304. {
  305. *list = sSensorList;
  306. return 0;
  307. }
  308. static int
  309. open_sensors(const struct hw_module_t* module,
  310. const char* name,
  311. struct hw_device_t* *device)
  312. {
  313. int status = -EINVAL;
  314. D("%s: name=%s", __FUNCTION__, name);
  315. if (!strcmp(name, SENSORS_HARDWARE_POLL)) {
  316. SensorDevice *dev = malloc(sizeof(*dev));
  317. memset(dev, 0, sizeof(*dev));
  318. dev->device.common.tag = HARDWARE_DEVICE_TAG;
  319. dev->device.common.version = SENSORS_DEVICE_API_VERSION_1_3;
  320. dev->device.common.module = (struct hw_module_t*) module;
  321. dev->device.common.close = sensor_device_close;
  322. dev->device.poll = sensor_device_poll;
  323. dev->device.activate = sensor_device_activate;
  324. dev->device.setDelay = sensor_device_set_delay;
  325. // Version 1.3-specific functions
  326. dev->device.batch = sensor_device_default_batch;
  327. dev->device.flush = sensor_device_default_flush;
  328. dev->fd = -1;
  329. pthread_mutex_init(&dev->lock, NULL);
  330. *device = &dev->device.common;
  331. status = 0;
  332. }
  333. return status;
  334. }
  335. static struct hw_module_methods_t sensors_module_methods = {
  336. .open = open_sensors
  337. };
  338. struct sensors_module_t HAL_MODULE_INFO_SYM = {
  339. .common = {
  340. .tag = HARDWARE_MODULE_TAG,
  341. .version_major = 1,
  342. .version_minor = 0,
  343. .id = SENSORS_HARDWARE_MODULE_ID,
  344. .name = "Dummy SENSORS Module",
  345. .author = "The Android Open Source Project",
  346. .methods = &sensors_module_methods,
  347. },
  348. .get_sensors_list = sensors__get_sensors_list
  349. };