2 * Copyright (C) 2015 Intel Corporation.
4 * This file represents the entry point for the activity recognition HAL module.
9 #include <sys/eventfd.h>
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
16 #include <hardware/sensors.h>
19 #include "activity_event_utils.h"
21 #define MODULE_VERSION 1
24 #define MODULE_NAME "Activity recognition HAL"
25 #define MODULE_AUTHOR "Intel"
27 #define CONTROL_FD (-1)
31 * This table maps syfs entries in scan_elements directories to sensor types,
32 * and will also be used to determine other sysfs names as well as the iio
33 * device number associated to a specific sensor.
35 sensor_catalog_entry_t sensor_catalog[] = {
38 .type = SENSOR_TYPE_SIGNIFICANT_MOTION,
43 DECLARE_VOID_CHANNEL("still")
46 { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "rising") },
47 { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "falling") },
51 DECLARE_VOID_CHANNEL("walking")
54 { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "rising") },
55 { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "falling") },
59 DECLARE_VOID_CHANNEL("running")
62 { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "rising") },
63 { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "falling") },
70 unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
72 /* All possible activities - see activity_recognition.h */
73 static char const* sysfs_activity_names[MAX_ACTIVITIES] = {
82 /* Internal HAL info */
83 static struct activity_event_info supported_activities[MAX_ACTIVITIES + 1];
84 /* Android framework level description (activities' name). The element on the
85 * first position (0) is reserved for the FLUSH COMPLETE event, thus we have
86 * (MAX_ACTIVITIES + 1) possible activities.
88 static char const* supported_activity_names[MAX_ACTIVITIES + 1];
89 /* Supported activities count */
90 static unsigned int count;
92 static int poll_fd, control_fd, exit_fd;
93 static pthread_t control_thread;
95 static activity_recognition_callback_procs_t activity_dev_callback;
96 static pthread_mutex_t callback_mutex;
98 static int instances_count;
100 static void register_activity_callback(const struct activity_recognition_device *dev __attribute((unused)),
101 const activity_recognition_callback_procs_t* callback)
103 pthread_mutex_lock(&callback_mutex);
104 activity_dev_callback.activity_callback = callback->activity_callback;
105 pthread_mutex_unlock(&callback_mutex);
108 static bool check_activity_event(uint32_t activity_handle, uint32_t event_type)
110 if (activity_handle > count)
113 /* Also return false if the handle is 0 - this is reserved for flush
114 * event, that is currently not supported. */
115 if (activity_handle == 0)
118 switch (event_type) {
119 case ACTIVITY_EVENT_ENTER:
120 case ACTIVITY_EVENT_EXIT:
122 case ACTIVITY_EVENT_FLUSH_COMPLETE:
123 /* Not supported yet */
129 static int set_activity_event_state(const struct activity_recognition_device *dev __attribute((unused)),
130 uint32_t activity_handle, uint32_t event_type,
133 uint64_t control_code;
136 /* Check received index boundaries */
137 if (!check_activity_event(activity_handle, event_type)) {
138 ALOGE("Received invalid <activity %d, event %d> %s request\n",
139 activity_handle, event_type, action);
143 control_code = get_control_code((uint8_t) 1,
144 (uint8_t) activity_handle,
145 (uint8_t) event_type);
147 ret = write(control_fd, &control_code, sizeof(control_code));
149 ALOGE("Error writing to control fd to %s activity event\n", action);
153 ALOGI("Sent %s <%s, %i> request\n", action, supported_activity_names[activity_handle], event_type);
158 static int enable_activity_event(const struct activity_recognition_device *dev,
159 uint32_t activity_handle, uint32_t event_type,
160 int64_t max_batch_report_latency_ns __attribute((unused)))
162 return set_activity_event_state(dev, activity_handle, event_type, "enable");
165 static int disable_activity_event(const struct activity_recognition_device *dev,
166 uint32_t activity_handle, uint32_t event_type)
168 return set_activity_event_state(dev, activity_handle, event_type, "disable");
172 * For now, just report that the function call has been made, since we yet do
173 * not have a batch FIFO device.
175 static int flush(const struct activity_recognition_device *dev __attribute((unused)))
177 ALOGV("Flushing...\n");
182 static void process_disabling_activity_ev(uint8_t activity, uint8_t event);
184 static int close_device(struct hw_device_t *device __attribute((unused)))
186 int j, ret, exit_ping;
189 if (!instances_count)
197 /* Send exit request to the worker thread and close resources. We can
198 * write anything to the exit fd, we just need the event.
201 write(exit_fd, &exit_ping, sizeof(exit_ping));
203 /* Wait for worker thread to finish in order to release shared
206 pthread_join(control_thread, NULL);
208 /* Close exit fd after sending the canceling request. */
209 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
211 ALOGE("Error deleting exit fd from polling pool\n");
214 /* Clean control data. */
215 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
217 ALOGE("Error deleting control fd from polling pool\n");
220 /* Disable all monitored <activity, event> pairs. This step should be
221 * the last one, after worker thread has ended in order to avoid
222 * supported_activities data corruption and avoid using another lock.
224 for (i = 1; i <= count; i++)
225 for (j = 0; j < MAX_EVENTS_PER_ACTIVITY; j++)
226 if (supported_activities[i].monitored[j])
227 process_disabling_activity_ev(
229 supported_activities[i].event[j]->event_type);
233 pthread_mutex_destroy(&callback_mutex);
235 ALOGI("Successfully closed device\n");
241 * Finds the event given by type in the sensor's channel structure and retrieves
243 * Equivalence (activity HAL naming - sensor HAL naming):
244 * ACTIVITY_EVENT_ENTER - "rising"
245 * ACTIVITY_EVENT_EXIT - "falling"
247 static int get_ev_index(int ev_type, channel_descriptor_t *chann)
253 case ACTIVITY_EVENT_ENTER:
256 case ACTIVITY_EVENT_EXIT:
264 for (i = 0; i < chann->num_events; i++) {
265 if (strcmp(ev_dir, chann->event[i].dir) == 0)
272 static int set_event_enabling(int dev_num, const char *en_path, int value)
277 ret = snprintf(path, sizeof(EVENTS_PATH) + sizeof(en_path), EVENTS_PATH "%s", dev_num, en_path);
281 return sysfs_write_int(path, value);
285 static void process_enabling_activity_ev(uint8_t activity, uint8_t event)
287 struct activity_event_info *activ = supported_activities + activity;
288 channel_descriptor_t *chann;
289 struct activity_event *ev;
291 struct epoll_event ev_data;
292 int dev_fd, ev_index, ret;
294 bool open_now = false;
296 /* Allocate event structure and populate it */
297 ev = malloc(sizeof(*ev));
299 ALOGE("Error allocating activity event for enabling\n");
303 ev->event_type = (uint32_t) event;
304 ev->activity = (uint32_t) activity;
307 /* The event fd is one per device, so we need to check if we have not
308 * retrieved it already when monitoring another <activity, event> pair.
309 * If it has not been retrieved, get it and update all other activities
310 * associated with the same device.
312 if (activ->event_fd == -1) {
313 ret = snprintf(path, sizeof(DEV_FILE_PATH), DEV_FILE_PATH, activ->dev_num);
317 dev_fd = open(path, O_RDONLY | O_NONBLOCK);
321 ret = ioctl(dev_fd, IIO_GET_EVENT_FD_IOCTL, &activ->event_fd);
328 ev_data.events = EPOLLIN;
329 ev_data.data.fd = activ->event_fd;
330 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, activ->event_fd, &ev_data);
334 /* Update all other activities generated by this device */
335 for (i = 1; i <= count; i++)
336 if (supported_activities[i].dev_num == activ->dev_num)
337 supported_activities[i].event_fd = activ->event_fd;
340 /* Activate the event */
341 chann = sensor_catalog[activ->sensor_catalog_index].channel + activ->channel_index;
342 ev_index = get_ev_index((int)event, chann);
344 ALOGE("Invalid event index: %d\n", ev_index);
347 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 1);
351 /* Internally mark that the <activity, event> pair is being monitored.
352 * We keep the same event index in our activity structure as is in the
353 * channel descriptor structure.
355 activ->event[ev_index] = ev;
356 activ->monitored[ev_index] = true;
357 activ->event_count++;
363 close(activ->event_fd);
364 for (i = 1; i <= count; i++)
365 if (supported_activities[i].dev_num == activ->dev_num)
366 supported_activities[i].event_fd = -1;
372 static bool device_not_monitored(int dev_num)
376 for (i = 1; i <= count; i++)
377 if (supported_activities[i].dev_num == dev_num &&
378 supported_activities[i].event_count > 0) {
385 static void process_disabling_activity_ev(uint8_t activity, uint8_t event)
387 struct activity_event_info *activ = supported_activities + activity;
388 channel_descriptor_t *chann;
392 /* Deactivate the event. */
393 chann = sensor_catalog[activ->sensor_catalog_index].channel + activ->channel_index;
394 ev_index = get_ev_index((int)event, chann);
396 ALOGE("Invalid event index: %d\n", ev_index);
398 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 0);
400 ALOGE("Could not deactivate event - writing error\n");
403 /* Mark that the <activity, event> pair is not monitored any longer. */
404 activ->monitored[ev_index] = false;
405 activ->event_count--;
407 /* Close the event fd if this is the last pair monitored for the given
408 * device and remove it from the polling pool.
410 if (device_not_monitored(activ->dev_num)) {
411 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, activ->event_fd, NULL);
413 ALOGE("Error removing event fd from polling pool\n");
417 close(activ->event_fd);
418 for (i = 1; i <= count; i++)
419 if (supported_activities[i].dev_num == activ->dev_num)
420 supported_activities[i].event_fd = -1;
423 /* Free resources. */
424 free(activ->event[ev_index]);
425 activ->event[ev_index] = NULL;
428 static void process_control_event(void)
430 struct control_event_data control_data;
431 uint64_t control_code;
434 /* Read control data from the control fd and interpret it */
435 ret = read(control_fd, &control_code, sizeof(control_code));
437 ALOGW("Error reading from control fd\n");
441 get_control_data(control_code, &control_data);
443 if (control_data.enable)
444 process_enabling_activity_ev(control_data.activity, control_data.event);
446 process_disabling_activity_ev(control_data.activity, control_data.event);
449 static int get_activity_index(int modifier)
453 /* Start from 1 since 0 is reserved for FLUSH_COMPLETE event. */
454 for (i = 1; i <= count; i++)
455 if (supported_activities[i].modifier == modifier)
461 static void process_activity_event(int fd, struct activity_event events[], int *count)
463 struct iio_event_data event;
464 int ret, chann_type, ev_type, ev_dir, ev_modifier, index, activity_index;
466 /* Retrieve event. */
467 ret = read(fd, &event, sizeof(event));
469 ALOGE("Error reading event\n");
473 /* Extract fields we are interested in and check the generated event. */
474 chann_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event.id);
475 if (chann_type != IIO_ACTIVITY) {
476 ALOGW("Event came from other than an activity channel\n");
480 ev_modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event.id);
481 switch (ev_modifier) {
483 case IIO_MOD_WALKING:
484 case IIO_MOD_RUNNING:
485 activity_index = get_activity_index(ev_modifier);
486 if (activity_index >= 0)
489 ALOGW("Incompatible modifier - none of the supported activities is present\n");
493 ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event.id);
494 if (ev_type != IIO_EV_TYPE_THRESH) {
495 ALOGW("Event type is not threshold\n");
499 ev_dir = IIO_EVENT_CODE_EXTRACT_DIR(event.id);
501 case IIO_EV_DIR_RISING:
502 ev_dir = ACTIVITY_EVENT_ENTER;
504 case IIO_EV_DIR_FALLING:
505 ev_dir = ACTIVITY_EVENT_EXIT;
508 ALOGW("Incompatible event direction - only RISING and FALLING supported\n");
512 /* Add the activity event to the array for further processing. */
514 events[index].event_type = ev_dir;
515 events[index].activity = activity_index;
516 events[index].timestamp = event.timestamp;
522 static void* events_routine(void *arg __attribute((unused)))
524 struct epoll_event events[MAX_ACTIVITIES + 2];
525 struct activity_event data_events[MAX_ACTIVITIES];
526 int no_events, i, no_activity_events;
529 ALOGV("Waiting for sensor events ...\n");
531 no_activity_events = 0;
532 no_events = epoll_wait(poll_fd, events, MAX_ACTIVITIES + 2, -1);
533 if (no_events == -1) {
534 ALOGE("epoll_wait error %s\n", strerror(errno));
538 for (i = 0; i < no_events; i++)
539 if (events[i].events == EPOLLIN) {
540 int data = events[i].data.fd;
543 process_activity_event(data,
545 &no_activity_events);
548 process_control_event();
553 ALOGW("Invalid event user data: %d \n", events[i].data.fd);
557 ALOGW("Epoll events %i not expected\n", events[i].events);
559 /* Call the callback function for the retrieved events (if it
562 pthread_mutex_lock(&callback_mutex);
563 if (activity_dev_callback.activity_callback) {
564 activity_dev_callback.activity_callback(
565 &activity_dev_callback,
569 pthread_mutex_unlock(&callback_mutex);
573 static int set_up_control_data(void)
575 struct epoll_event control_ev, exit_ev;
578 ret = pthread_mutex_init(&callback_mutex, NULL);
582 /* Maximum fds is maximum activities + 1 control fd + 1 exit fd */
583 poll_fd = epoll_create(MAX_ACTIVITIES + 2);
587 goto poll_control_err;
589 control_fd = eventfd(0, 0);
590 if (control_fd == -1) {
592 goto poll_control_err;
595 control_ev.events = EPOLLIN;
596 /* Set data field to event file descriptor */
597 control_ev.data.fd = CONTROL_FD;
598 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, control_fd, &control_ev);
600 goto control_data_err;
602 exit_fd = eventfd(0, 0);
604 ALOGE("Error allocating exit fd\n");
605 goto exit_control_err;
607 exit_ev.events = EPOLLIN;
608 exit_ev.data.fd = EXIT_FD;
609 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, exit_fd, &exit_ev);
611 ALOGE("Error adding exit fd to the polling pool\n");
615 /* Start worker thread to wait on all event sources */
616 ret = pthread_create(&control_thread, NULL, events_routine, NULL);
623 epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
627 epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
636 /* Returns the IIO_MOD_* equivalent to the given name. */
637 static int get_modifier_as_int(const char* mod)
639 if (strncmp(mod, "still", sizeof("still")) == 0)
640 return IIO_MOD_STILL;
642 if (strncmp(mod, "walking", sizeof("walking")) == 0)
643 return IIO_MOD_WALKING;
645 if (strncmp(mod, "running", sizeof("running")) == 0)
646 return IIO_MOD_RUNNING;
651 static void add_activity(int sensor_catalog_index, int channel_index,
652 int dev_num, const char *name)
654 int index, i, modifier;
656 if (count == MAX_ACTIVITIES) {
657 ALOGE("Trying to add more than supported activities!\n");
661 modifier = get_modifier_as_int(name);
663 ALOGE("Invalid channel name as modifier: %s\n", name);
669 for (i = 0; i < MAX_EVENTS_PER_ACTIVITY; i++) {
670 supported_activities[index].event[i] = NULL;
671 supported_activities[index].monitored[i] = false;
673 supported_activities[index].modifier = modifier;
674 supported_activities[index].event_count = 0;
675 supported_activities[index].sensor_catalog_index = sensor_catalog_index;
676 supported_activities[index].channel_index = channel_index;
677 supported_activities[index].dev_num = dev_num;
678 supported_activities[index].event_fd = -1;
680 supported_activity_names[index] = name;
683 static bool is_activity_valid(const char *activity_name)
687 /* Look if this activity has not been already added */
688 for (i = 1; i <= count; i++)
689 if (strcmp(supported_activity_names[i], activity_name) == 0)
692 /* Check that the found activity is recognized by this API */
693 for (i = 0; i < MAX_ACTIVITIES; i++)
694 if (strcmp(sysfs_activity_names[i], activity_name) == 0)
700 /* Get all possible activities provided by the IIO sensors */
701 static void discover_activity_events(void)
703 channel_descriptor_t *chann;
704 int i, num_channels, dev_num;
706 char event_sensors[catalog_size];
708 /* Discover event sensors */
709 for (dev_num = 0; dev_num < MAX_DEVICES; dev_num++) {
710 discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
711 for (index = 0; index < catalog_size; index++) {
712 if (!event_sensors[index])
715 num_channels = sensor_catalog[index].num_channels;
716 for (i = 0; i < num_channels; i++) {
717 chann = sensor_catalog[index].channel + i;
719 if (is_activity_valid(chann->name))
720 add_activity(index, i, dev_num, chann->name);
725 ALOGI("Discovered %d activities\n", count);
728 static int open_module(const struct hw_module_t *module, const char *id,
729 struct hw_device_t **device)
731 static struct activity_recognition_device activity_dev;
734 if (strncmp(id, ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, sizeof(ACTIVITY_RECOGNITION_HARDWARE_INTERFACE)) != 0)
737 activity_dev.common.tag = HARDWARE_DEVICE_TAG;
738 activity_dev.common.version = ACTIVITY_RECOGNITION_API_VERSION_0_1;
739 activity_dev.common.module = (struct hw_module_t *) module;
740 activity_dev.common.close = close_device;
742 activity_dev.register_activity_callback = register_activity_callback;
743 activity_dev.enable_activity_event = enable_activity_event;
744 activity_dev.disable_activity_event = disable_activity_event;
745 activity_dev.flush = flush;
747 *device = &activity_dev.common;
749 if (instances_count == 0) {
750 discover_activity_events();
751 ret = set_up_control_data();
753 ALOGI("Initialized activity recognition HAL (exit code %i)\n", ret);
761 static struct hw_module_methods_t module_methods = {
766 static int get_supported_activities_list(struct activity_recognition_module *module __attribute((unused)),
767 char const* const* *activity_list)
769 *activity_list = supported_activity_names + 1;
774 /* Module descriptor visible to the Android framework. */
775 struct activity_recognition_module __attribute__ ((visibility ("default")))
776 HAL_MODULE_INFO_SYM = {
778 .tag = HARDWARE_MODULE_TAG,
779 .module_api_version = MODULE_VERSION,
780 .hal_api_version = HAL_VERSION,
781 .id = ACTIVITY_RECOGNITION_HARDWARE_MODULE_ID,
783 .author = MODULE_AUTHOR,
784 .methods = &module_methods,
786 .get_supported_activities_list = get_supported_activities_list