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"
20 #include <linux/iio/events.h>
22 #define MODULE_VERSION 1
25 #define MODULE_NAME "Activity recognition HAL"
26 #define MODULE_AUTHOR "Intel"
28 #define CONTROL_FD (-1)
32 * This table maps syfs entries in scan_elements directories to sensor types,
33 * and will also be used to determine other sysfs names as well as the iio
34 * device number associated to a specific sensor.
36 sensor_catalog_entry_t sensor_catalog[] = {
39 .type = SENSOR_TYPE_SIGNIFICANT_MOTION,
44 DECLARE_VOID_CHANNEL("still")
47 { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "rising") },
48 { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "falling") },
52 DECLARE_VOID_CHANNEL("walking")
55 { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "rising") },
56 { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "falling") },
60 DECLARE_VOID_CHANNEL("running")
63 { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "rising") },
64 { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "falling") },
71 unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
73 /* All possible activities - see activity_recognition.h */
74 static char const* sysfs_activity_names[MAX_ACTIVITIES] = {
83 /* Internal HAL info */
84 static struct activity_event_info supported_activities[MAX_ACTIVITIES + 1];
85 /* Android framework level description (activities' name). The element on the
86 * first position (0) is reserved for the FLUSH COMPLETE event, thus we have
87 * (MAX_ACTIVITIES + 1) possible activities.
89 static char const* supported_activity_names[MAX_ACTIVITIES + 1];
90 /* Supported activities count */
91 static unsigned int count;
93 static int poll_fd, control_fd, exit_fd;
94 static pthread_t control_thread;
96 static activity_recognition_callback_procs_t activity_dev_callback;
97 static pthread_mutex_t callback_mutex;
99 static int instances_count;
101 static void register_activity_callback(const struct activity_recognition_device *dev __attribute((unused)),
102 const activity_recognition_callback_procs_t* callback)
104 pthread_mutex_lock(&callback_mutex);
105 activity_dev_callback.activity_callback = callback->activity_callback;
106 pthread_mutex_unlock(&callback_mutex);
109 static bool check_activity_event(uint32_t activity_handle, uint32_t event_type)
111 if (activity_handle > count)
114 /* Also return false if the handle is 0 - this is reserved for flush
115 * event, that is currently not supported. */
116 if (activity_handle == 0)
119 switch (event_type) {
120 case ACTIVITY_EVENT_ENTER:
121 case ACTIVITY_EVENT_EXIT:
123 case ACTIVITY_EVENT_FLUSH_COMPLETE:
124 /* Not supported yet */
130 static int set_activity_event_state(const struct activity_recognition_device *dev __attribute((unused)),
131 uint32_t activity_handle, uint32_t event_type,
134 uint64_t control_code;
137 /* Check received index boundaries */
138 if (!check_activity_event(activity_handle, event_type)) {
139 ALOGE("Received invalid <activity %d, event %d> %s request\n",
140 activity_handle, event_type, action);
144 control_code = get_control_code((uint8_t) 1,
145 (uint8_t) activity_handle,
146 (uint8_t) event_type);
148 ret = write(control_fd, &control_code, sizeof(control_code));
150 ALOGE("Error writing to control fd to %s activity event\n", action);
154 ALOGI("Sent %s <%s, %i> request\n", action, supported_activity_names[activity_handle], event_type);
159 static int enable_activity_event(const struct activity_recognition_device *dev,
160 uint32_t activity_handle, uint32_t event_type,
161 int64_t max_batch_report_latency_ns __attribute((unused)))
163 return set_activity_event_state(dev, activity_handle, event_type, "enable");
166 static int disable_activity_event(const struct activity_recognition_device *dev,
167 uint32_t activity_handle, uint32_t event_type)
169 return set_activity_event_state(dev, activity_handle, event_type, "disable");
173 * For now, just report that the function call has been made, since we yet do
174 * not have a batch FIFO device.
176 static int flush(const struct activity_recognition_device *dev __attribute((unused)))
178 ALOGV("Flushing...\n");
183 static void process_disabling_activity_ev(uint8_t activity, uint8_t event);
185 static int close_device(struct hw_device_t *device __attribute((unused)))
187 int j, ret, exit_ping;
190 if (!instances_count)
198 /* Send exit request to the worker thread and close resources. We can
199 * write anything to the exit fd, we just need the event.
202 write(exit_fd, &exit_ping, sizeof(exit_ping));
204 /* Wait for worker thread to finish in order to release shared
207 pthread_join(control_thread, NULL);
209 /* Close exit fd after sending the canceling request. */
210 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
212 ALOGE("Error deleting exit fd from polling pool\n");
215 /* Clean control data. */
216 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
218 ALOGE("Error deleting control fd from polling pool\n");
221 /* Disable all monitored <activity, event> pairs. This step should be
222 * the last one, after worker thread has ended in order to avoid
223 * supported_activities data corruption and avoid using another lock.
225 for (i = 1; i <= count; i++)
226 for (j = 0; j < MAX_EVENTS_PER_ACTIVITY; j++)
227 if (supported_activities[i].monitored[j])
228 process_disabling_activity_ev(
230 supported_activities[i].event[j]->event_type);
234 pthread_mutex_destroy(&callback_mutex);
236 ALOGI("Successfully closed device\n");
242 * Finds the event given by type in the sensor's channel structure and retrieves
244 * Equivalence (activity HAL naming - sensor HAL naming):
245 * ACTIVITY_EVENT_ENTER - "rising"
246 * ACTIVITY_EVENT_EXIT - "falling"
248 static int get_ev_index(int ev_type, channel_descriptor_t *chann)
254 case ACTIVITY_EVENT_ENTER:
257 case ACTIVITY_EVENT_EXIT:
265 for (i = 0; i < chann->num_events; i++) {
266 if (strcmp(ev_dir, chann->event[i].dir) == 0)
273 static int set_event_enabling(int dev_num, const char *en_path, int value)
278 ret = snprintf(path, sizeof(EVENTS_PATH) + sizeof(en_path), EVENTS_PATH "%s", dev_num, en_path);
282 return sysfs_write_int(path, value);
286 static void process_enabling_activity_ev(uint8_t activity, uint8_t event)
288 struct activity_event_info *activ = supported_activities + activity;
289 channel_descriptor_t *chann;
290 struct activity_event *ev;
292 struct epoll_event ev_data;
293 int dev_fd, ev_index, ret;
295 bool open_now = false;
297 /* Allocate event structure and populate it */
298 ev = malloc(sizeof(*ev));
300 ALOGE("Error allocating activity event for enabling\n");
304 ev->event_type = (uint32_t) event;
305 ev->activity = (uint32_t) activity;
308 /* The event fd is one per device, so we need to check if we have not
309 * retrieved it already when monitoring another <activity, event> pair.
310 * If it has not been retrieved, get it and update all other activities
311 * associated with the same device.
313 if (activ->event_fd == -1) {
314 ret = snprintf(path, sizeof(DEV_FILE_PATH), DEV_FILE_PATH, activ->dev_num);
318 dev_fd = open(path, O_RDONLY | O_NONBLOCK);
322 ret = ioctl(dev_fd, IIO_GET_EVENT_FD_IOCTL, &activ->event_fd);
329 ev_data.events = EPOLLIN;
330 ev_data.data.fd = activ->event_fd;
331 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, activ->event_fd, &ev_data);
335 /* Update all other activities generated by this device */
336 for (i = 1; i <= count; i++)
337 if (supported_activities[i].dev_num == activ->dev_num)
338 supported_activities[i].event_fd = activ->event_fd;
341 /* Activate the event */
342 chann = sensor_catalog[activ->sensor_catalog_index].channel + activ->channel_index;
343 ev_index = get_ev_index((int)event, chann);
345 ALOGE("Invalid event index: %d\n", ev_index);
348 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 1);
352 /* Internally mark that the <activity, event> pair is being monitored.
353 * We keep the same event index in our activity structure as is in the
354 * channel descriptor structure.
356 activ->event[ev_index] = ev;
357 activ->monitored[ev_index] = true;
358 activ->event_count++;
364 close(activ->event_fd);
365 for (i = 1; i <= count; i++)
366 if (supported_activities[i].dev_num == activ->dev_num)
367 supported_activities[i].event_fd = -1;
373 static bool device_not_monitored(int dev_num)
377 for (i = 1; i <= count; i++)
378 if (supported_activities[i].dev_num == dev_num &&
379 supported_activities[i].event_count > 0) {
386 static void process_disabling_activity_ev(uint8_t activity, uint8_t event)
388 struct activity_event_info *activ = supported_activities + activity;
389 channel_descriptor_t *chann;
393 /* Deactivate the event. */
394 chann = sensor_catalog[activ->sensor_catalog_index].channel + activ->channel_index;
395 ev_index = get_ev_index((int)event, chann);
397 ALOGE("Invalid event index: %d\n", ev_index);
399 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 0);
401 ALOGE("Could not deactivate event - writing error\n");
404 /* Mark that the <activity, event> pair is not monitored any longer. */
405 activ->monitored[ev_index] = false;
406 activ->event_count--;
408 /* Close the event fd if this is the last pair monitored for the given
409 * device and remove it from the polling pool.
411 if (device_not_monitored(activ->dev_num)) {
412 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, activ->event_fd, NULL);
414 ALOGE("Error removing event fd from polling pool\n");
418 close(activ->event_fd);
419 for (i = 1; i <= count; i++)
420 if (supported_activities[i].dev_num == activ->dev_num)
421 supported_activities[i].event_fd = -1;
424 /* Free resources. */
425 free(activ->event[ev_index]);
426 activ->event[ev_index] = NULL;
429 static void process_control_event(void)
431 struct control_event_data control_data;
432 uint64_t control_code;
435 /* Read control data from the control fd and interpret it */
436 ret = read(control_fd, &control_code, sizeof(control_code));
438 ALOGW("Error reading from control fd\n");
442 get_control_data(control_code, &control_data);
444 if (control_data.enable)
445 process_enabling_activity_ev(control_data.activity, control_data.event);
447 process_disabling_activity_ev(control_data.activity, control_data.event);
450 static int get_activity_index(int modifier)
454 /* Start from 1 since 0 is reserved for FLUSH_COMPLETE event. */
455 for (i = 1; i <= count; i++)
456 if (supported_activities[i].modifier == modifier)
462 static void process_activity_event(int fd, struct activity_event events[], int *count)
464 struct iio_event_data event;
465 int ret, chann_type, ev_type, ev_dir, ev_modifier, index, activity_index;
467 /* Retrieve event. */
468 ret = read(fd, &event, sizeof(event));
470 ALOGE("Error reading event\n");
474 /* Extract fields we are interested in and check the generated event. */
475 chann_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event.id);
476 if (chann_type != IIO_ACTIVITY) {
477 ALOGW("Event came from other than an activity channel\n");
481 ev_modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event.id);
482 switch (ev_modifier) {
484 case IIO_MOD_WALKING:
485 case IIO_MOD_RUNNING:
486 activity_index = get_activity_index(ev_modifier);
487 if (activity_index >= 0)
490 ALOGW("Incompatible modifier - none of the supported activities is present\n");
494 ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event.id);
495 if (ev_type != IIO_EV_TYPE_THRESH) {
496 ALOGW("Event type is not threshold\n");
500 ev_dir = IIO_EVENT_CODE_EXTRACT_DIR(event.id);
502 case IIO_EV_DIR_RISING:
503 ev_dir = ACTIVITY_EVENT_ENTER;
505 case IIO_EV_DIR_FALLING:
506 ev_dir = ACTIVITY_EVENT_EXIT;
509 ALOGW("Incompatible event direction - only RISING and FALLING supported\n");
513 /* Add the activity event to the array for further processing. */
515 events[index].event_type = ev_dir;
516 events[index].activity = activity_index;
517 events[index].timestamp = event.timestamp;
523 static void* events_routine(void *arg __attribute((unused)))
525 struct epoll_event events[MAX_ACTIVITIES + 2];
526 struct activity_event data_events[MAX_ACTIVITIES];
527 int no_events, i, no_activity_events;
530 ALOGV("Waiting for sensor events ...\n");
532 no_activity_events = 0;
533 no_events = epoll_wait(poll_fd, events, MAX_ACTIVITIES + 2, -1);
534 if (no_events == -1) {
535 ALOGE("epoll_wait error %s\n", strerror(errno));
539 for (i = 0; i < no_events; i++)
540 if (events[i].events == EPOLLIN) {
541 int data = events[i].data.fd;
544 process_activity_event(data,
546 &no_activity_events);
549 process_control_event();
554 ALOGW("Invalid event user data: %d \n", events[i].data.fd);
558 ALOGW("Epoll events %i not expected\n", events[i].events);
560 /* Call the callback function for the retrieved events (if it
563 pthread_mutex_lock(&callback_mutex);
564 if (activity_dev_callback.activity_callback) {
565 activity_dev_callback.activity_callback(
566 &activity_dev_callback,
570 pthread_mutex_unlock(&callback_mutex);
574 static int set_up_control_data(void)
576 struct epoll_event control_ev, exit_ev;
579 ret = pthread_mutex_init(&callback_mutex, NULL);
583 /* Maximum fds is maximum activities + 1 control fd + 1 exit fd */
584 poll_fd = epoll_create(MAX_ACTIVITIES + 2);
588 goto poll_control_err;
590 control_fd = eventfd(0, 0);
591 if (control_fd == -1) {
593 goto poll_control_err;
596 control_ev.events = EPOLLIN;
597 /* Set data field to event file descriptor */
598 control_ev.data.fd = CONTROL_FD;
599 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, control_fd, &control_ev);
601 goto control_data_err;
603 exit_fd = eventfd(0, 0);
605 ALOGE("Error allocating exit fd\n");
606 goto exit_control_err;
608 exit_ev.events = EPOLLIN;
609 exit_ev.data.fd = EXIT_FD;
610 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, exit_fd, &exit_ev);
612 ALOGE("Error adding exit fd to the polling pool\n");
616 /* Start worker thread to wait on all event sources */
617 ret = pthread_create(&control_thread, NULL, events_routine, NULL);
624 epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
628 epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
637 /* Returns the IIO_MOD_* equivalent to the given name. */
638 static int get_modifier_as_int(const char* mod)
640 if (strncmp(mod, "still", sizeof("still")) == 0)
641 return IIO_MOD_STILL;
643 if (strncmp(mod, "walking", sizeof("walking")) == 0)
644 return IIO_MOD_WALKING;
646 if (strncmp(mod, "running", sizeof("running")) == 0)
647 return IIO_MOD_RUNNING;
652 static void add_activity(int sensor_catalog_index, int channel_index,
653 int dev_num, const char *name)
655 int index, i, modifier;
657 if (count == MAX_ACTIVITIES) {
658 ALOGE("Trying to add more than supported activities!\n");
662 modifier = get_modifier_as_int(name);
664 ALOGE("Invalid channel name as modifier: %s\n", name);
670 for (i = 0; i < MAX_EVENTS_PER_ACTIVITY; i++) {
671 supported_activities[index].event[i] = NULL;
672 supported_activities[index].monitored[i] = false;
674 supported_activities[index].modifier = modifier;
675 supported_activities[index].event_count = 0;
676 supported_activities[index].sensor_catalog_index = sensor_catalog_index;
677 supported_activities[index].channel_index = channel_index;
678 supported_activities[index].dev_num = dev_num;
679 supported_activities[index].event_fd = -1;
681 supported_activity_names[index] = name;
684 static bool is_activity_valid(const char *activity_name)
688 /* Look if this activity has not been already added */
689 for (i = 1; i <= count; i++)
690 if (strcmp(supported_activity_names[i], activity_name) == 0)
693 /* Check that the found activity is recognized by this API */
694 for (i = 0; i < MAX_ACTIVITIES; i++)
695 if (strcmp(sysfs_activity_names[i], activity_name) == 0)
701 /* Get all possible activities provided by the IIO sensors */
702 static void discover_activity_events(void)
704 channel_descriptor_t *chann;
705 int i, num_channels, dev_num;
707 char event_sensors[catalog_size];
709 /* Discover event sensors */
710 for (dev_num = 0; dev_num < MAX_DEVICES; dev_num++) {
711 discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
712 for (index = 0; index < catalog_size; index++) {
713 if (!event_sensors[index])
716 num_channels = sensor_catalog[index].num_channels;
717 for (i = 0; i < num_channels; i++) {
718 chann = sensor_catalog[index].channel + i;
720 if (is_activity_valid(chann->name))
721 add_activity(index, i, dev_num, chann->name);
726 ALOGI("Discovered %d activities\n", count);
729 static int open_module(const struct hw_module_t *module, const char *id,
730 struct hw_device_t **device)
732 static struct activity_recognition_device activity_dev;
735 if (strncmp(id, ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, sizeof(ACTIVITY_RECOGNITION_HARDWARE_INTERFACE)) != 0)
738 activity_dev.common.tag = HARDWARE_DEVICE_TAG;
739 activity_dev.common.version = ACTIVITY_RECOGNITION_API_VERSION_0_1;
740 activity_dev.common.module = (struct hw_module_t *) module;
741 activity_dev.common.close = close_device;
743 activity_dev.register_activity_callback = register_activity_callback;
744 activity_dev.enable_activity_event = enable_activity_event;
745 activity_dev.disable_activity_event = disable_activity_event;
746 activity_dev.flush = flush;
748 *device = &activity_dev.common;
750 if (instances_count == 0) {
751 discover_activity_events();
752 ret = set_up_control_data();
754 ALOGI("Initialized activity recognition HAL (exit code %i)\n", ret);
762 static struct hw_module_methods_t module_methods = {
767 static int get_supported_activities_list(struct activity_recognition_module *module __attribute((unused)),
768 char const* const* *activity_list)
770 *activity_list = supported_activity_names + 1;
775 /* Module descriptor visible to the Android framework. */
776 struct activity_recognition_module __attribute__ ((visibility ("default")))
777 HAL_MODULE_INFO_SYM = {
779 .tag = HARDWARE_MODULE_TAG,
780 .module_api_version = MODULE_VERSION,
781 .hal_api_version = HAL_VERSION,
782 .id = ACTIVITY_RECOGNITION_HARDWARE_MODULE_ID,
784 .author = MODULE_AUTHOR,
785 .methods = &module_methods,
787 .get_supported_activities_list = get_supported_activities_list