2 // Copyright (c) 2015 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This file represents the entry point for the activity recognition HAL module.
21 #include <utils/Log.h>
22 #include <sys/epoll.h>
23 #include <sys/eventfd.h>
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
30 #include <hardware/sensors.h>
33 #include "activity_event_utils.h"
34 #include <linux/iio/events.h>
36 #define MODULE_VERSION 1
39 #define MODULE_NAME "Activity recognition HAL"
40 #define MODULE_AUTHOR "Intel"
42 #define CONTROL_FD (-1)
46 * This table maps syfs entries in scan_elements directories to sensor types,
47 * and will also be used to determine other sysfs names as well as the iio
48 * device number associated to a specific sensor.
50 sensor_catalog_entry_t sensor_catalog[] = {
53 .type = SENSOR_TYPE_SIGNIFICANT_MOTION,
58 DECLARE_VOID_CHANNEL("still")
61 { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "rising") },
62 { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "falling") },
66 DECLARE_VOID_CHANNEL("walking")
69 { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "rising") },
70 { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "falling") },
74 DECLARE_VOID_CHANNEL("running")
77 { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "rising") },
78 { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "falling") },
85 unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
87 /* All possible activities - see activity_recognition.h */
88 static char const* sysfs_activity_names[MAX_ACTIVITIES] = {
97 /* Internal HAL info */
98 static struct activity_event_info supported_activities[MAX_ACTIVITIES + 1];
99 /* Android framework level description (activities' name). The element on the
100 * first position (0) is reserved for the FLUSH COMPLETE event, thus we have
101 * (MAX_ACTIVITIES + 1) possible activities.
103 static char const* supported_activity_names[MAX_ACTIVITIES + 1];
104 /* Supported activities count */
105 static unsigned int count;
107 static int poll_fd, control_fd, exit_fd;
108 static pthread_t control_thread;
110 static activity_recognition_callback_procs_t activity_dev_callback;
111 static pthread_mutex_t callback_mutex;
113 static int instances_count;
115 static void register_activity_callback(const struct activity_recognition_device *dev __attribute((unused)),
116 const activity_recognition_callback_procs_t* callback)
118 pthread_mutex_lock(&callback_mutex);
119 activity_dev_callback.activity_callback = callback->activity_callback;
120 pthread_mutex_unlock(&callback_mutex);
123 static bool check_activity_event(uint32_t activity_handle, uint32_t event_type)
125 if (activity_handle > count)
128 /* Also return false if the handle is 0 - this is reserved for flush
129 * event, that is currently not supported. */
130 if (activity_handle == 0)
133 switch (event_type) {
134 case ACTIVITY_EVENT_ENTER:
135 case ACTIVITY_EVENT_EXIT:
137 case ACTIVITY_EVENT_FLUSH_COMPLETE:
138 /* Not supported yet */
144 static int set_activity_event_state(const struct activity_recognition_device *dev __attribute((unused)),
145 uint32_t activity_handle, uint32_t event_type,
148 uint64_t control_code;
151 /* Check received index boundaries */
152 if (!check_activity_event(activity_handle, event_type)) {
153 ALOGE("Received invalid <activity %d, event %d> %s request\n",
154 activity_handle, event_type, action);
158 control_code = get_control_code((uint8_t) 1,
159 (uint8_t) activity_handle,
160 (uint8_t) event_type);
162 ret = write(control_fd, &control_code, sizeof(control_code));
164 ALOGE("Error writing to control fd to %s activity event\n", action);
168 ALOGI("Sent %s <%s, %i> request\n", action, supported_activity_names[activity_handle], event_type);
173 static int enable_activity_event(const struct activity_recognition_device *dev,
174 uint32_t activity_handle, uint32_t event_type,
175 int64_t max_batch_report_latency_ns __attribute((unused)))
177 return set_activity_event_state(dev, activity_handle, event_type, "enable");
180 static int disable_activity_event(const struct activity_recognition_device *dev,
181 uint32_t activity_handle, uint32_t event_type)
183 return set_activity_event_state(dev, activity_handle, event_type, "disable");
187 * For now, just report that the function call has been made, since we yet do
188 * not have a batch FIFO device.
190 static int flush(const struct activity_recognition_device *dev __attribute((unused)))
192 ALOGV("Flushing...\n");
197 static void process_disabling_activity_ev(uint8_t activity, uint8_t event);
199 static int close_device(struct hw_device_t *device __attribute((unused)))
201 int j, ret, exit_ping;
204 if (!instances_count)
212 /* Send exit request to the worker thread and close resources. We can
213 * write anything to the exit fd, we just need the event.
216 write(exit_fd, &exit_ping, sizeof(exit_ping));
218 /* Wait for worker thread to finish in order to release shared
221 pthread_join(control_thread, NULL);
223 /* Close exit fd after sending the canceling request. */
224 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
226 ALOGE("Error deleting exit fd from polling pool\n");
229 /* Clean control data. */
230 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
232 ALOGE("Error deleting control fd from polling pool\n");
235 /* Disable all monitored <activity, event> pairs. This step should be
236 * the last one, after worker thread has ended in order to avoid
237 * supported_activities data corruption and avoid using another lock.
239 for (i = 1; i <= count; i++)
240 for (j = 0; j < MAX_EVENTS_PER_ACTIVITY; j++)
241 if (supported_activities[i].monitored[j])
242 process_disabling_activity_ev(
244 supported_activities[i].event[j]->event_type);
248 pthread_mutex_destroy(&callback_mutex);
250 ALOGI("Successfully closed device\n");
256 * Finds the event given by type in the sensor's channel structure and retrieves
258 * Equivalence (activity HAL naming - sensor HAL naming):
259 * ACTIVITY_EVENT_ENTER - "rising"
260 * ACTIVITY_EVENT_EXIT - "falling"
262 static int get_ev_index(int ev_type, channel_descriptor_t *chann)
268 case ACTIVITY_EVENT_ENTER:
271 case ACTIVITY_EVENT_EXIT:
279 for (i = 0; i < chann->num_events; i++) {
280 if (strcmp(ev_dir, chann->event[i].dir) == 0)
287 static int set_event_enabling(int dev_num, const char *en_path, int value)
292 ret = snprintf(path, sizeof(EVENTS_PATH) + sizeof(en_path), EVENTS_PATH "%s", dev_num, en_path);
296 return sysfs_write_int(path, value);
300 static void process_enabling_activity_ev(uint8_t activity, uint8_t event)
302 struct activity_event_info *activ = supported_activities + activity;
303 channel_descriptor_t *chann;
304 struct activity_event *ev;
306 struct epoll_event ev_data;
307 int dev_fd, ev_index, ret;
309 bool open_now = false;
311 /* Allocate event structure and populate it */
312 ev = malloc(sizeof(*ev));
314 ALOGE("Error allocating activity event for enabling\n");
318 ev->event_type = (uint32_t) event;
319 ev->activity = (uint32_t) activity;
322 /* The event fd is one per device, so we need to check if we have not
323 * retrieved it already when monitoring another <activity, event> pair.
324 * If it has not been retrieved, get it and update all other activities
325 * associated with the same device.
327 if (activ->event_fd == -1) {
328 ret = snprintf(path, sizeof(DEV_FILE_PATH), DEV_FILE_PATH, activ->dev_num);
332 dev_fd = open(path, O_RDONLY | O_NONBLOCK);
336 ret = ioctl(dev_fd, IIO_GET_EVENT_FD_IOCTL, &activ->event_fd);
343 ev_data.events = EPOLLIN;
344 ev_data.data.fd = activ->event_fd;
345 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, activ->event_fd, &ev_data);
349 /* Update all other activities generated by this device */
350 for (i = 1; i <= count; i++)
351 if (supported_activities[i].dev_num == activ->dev_num)
352 supported_activities[i].event_fd = activ->event_fd;
355 /* Activate the event */
356 chann = sensor_catalog[activ->sensor_catalog_index].channel + activ->channel_index;
357 ev_index = get_ev_index((int)event, chann);
359 ALOGE("Invalid event index: %d\n", ev_index);
362 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 1);
366 /* Internally mark that the <activity, event> pair is being monitored.
367 * We keep the same event index in our activity structure as is in the
368 * channel descriptor structure.
370 activ->event[ev_index] = ev;
371 activ->monitored[ev_index] = true;
372 activ->event_count++;
378 close(activ->event_fd);
379 for (i = 1; i <= count; i++)
380 if (supported_activities[i].dev_num == activ->dev_num)
381 supported_activities[i].event_fd = -1;
387 static bool device_not_monitored(int dev_num)
391 for (i = 1; i <= count; i++)
392 if (supported_activities[i].dev_num == dev_num &&
393 supported_activities[i].event_count > 0) {
400 static void process_disabling_activity_ev(uint8_t activity, uint8_t event)
402 struct activity_event_info *activ = supported_activities + activity;
403 channel_descriptor_t *chann;
407 /* Deactivate the event. */
408 chann = sensor_catalog[activ->sensor_catalog_index].channel + activ->channel_index;
409 ev_index = get_ev_index((int)event, chann);
411 ALOGE("Invalid event index: %d\n", ev_index);
413 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 0);
415 ALOGE("Could not deactivate event - writing error\n");
418 /* Mark that the <activity, event> pair is not monitored any longer. */
419 activ->monitored[ev_index] = false;
420 activ->event_count--;
422 /* Close the event fd if this is the last pair monitored for the given
423 * device and remove it from the polling pool.
425 if (device_not_monitored(activ->dev_num)) {
426 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, activ->event_fd, NULL);
428 ALOGE("Error removing event fd from polling pool\n");
432 close(activ->event_fd);
433 for (i = 1; i <= count; i++)
434 if (supported_activities[i].dev_num == activ->dev_num)
435 supported_activities[i].event_fd = -1;
438 /* Free resources. */
439 free(activ->event[ev_index]);
440 activ->event[ev_index] = NULL;
443 static void process_control_event(void)
445 struct control_event_data control_data;
446 uint64_t control_code;
449 /* Read control data from the control fd and interpret it */
450 ret = read(control_fd, &control_code, sizeof(control_code));
452 ALOGW("Error reading from control fd\n");
456 get_control_data(control_code, &control_data);
458 if (control_data.enable)
459 process_enabling_activity_ev(control_data.activity, control_data.event);
461 process_disabling_activity_ev(control_data.activity, control_data.event);
464 static int get_activity_index(int modifier)
468 /* Start from 1 since 0 is reserved for FLUSH_COMPLETE event. */
469 for (i = 1; i <= count; i++)
470 if (supported_activities[i].modifier == modifier)
476 static void process_activity_event(int fd, struct activity_event events[], int *count)
478 struct iio_event_data event;
479 int ret, chann_type, ev_type, ev_dir, ev_modifier, index, activity_index;
481 /* Retrieve event. */
482 ret = read(fd, &event, sizeof(event));
484 ALOGE("Error reading event\n");
488 /* Extract fields we are interested in and check the generated event. */
489 chann_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event.id);
490 if (chann_type != IIO_ACTIVITY) {
491 ALOGW("Event came from other than an activity channel\n");
495 ev_modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event.id);
496 switch (ev_modifier) {
498 case IIO_MOD_WALKING:
499 case IIO_MOD_RUNNING:
500 activity_index = get_activity_index(ev_modifier);
501 if (activity_index >= 0)
504 ALOGW("Incompatible modifier - none of the supported activities is present\n");
508 ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event.id);
509 if (ev_type != IIO_EV_TYPE_THRESH) {
510 ALOGW("Event type is not threshold\n");
514 ev_dir = IIO_EVENT_CODE_EXTRACT_DIR(event.id);
516 case IIO_EV_DIR_RISING:
517 ev_dir = ACTIVITY_EVENT_ENTER;
519 case IIO_EV_DIR_FALLING:
520 ev_dir = ACTIVITY_EVENT_EXIT;
523 ALOGW("Incompatible event direction - only RISING and FALLING supported\n");
527 /* Add the activity event to the array for further processing. */
529 events[index].event_type = ev_dir;
530 events[index].activity = activity_index;
531 events[index].timestamp = event.timestamp;
537 static void* events_routine(void *arg __attribute((unused)))
539 struct epoll_event events[MAX_ACTIVITIES + 2];
540 struct activity_event data_events[MAX_ACTIVITIES];
541 int no_events, i, no_activity_events;
544 ALOGV("Waiting for sensor events ...\n");
546 no_activity_events = 0;
547 no_events = epoll_wait(poll_fd, events, MAX_ACTIVITIES + 2, -1);
548 if (no_events == -1) {
549 ALOGE("epoll_wait error %s\n", strerror(errno));
553 for (i = 0; i < no_events; i++)
554 if (events[i].events == EPOLLIN) {
555 int data = events[i].data.fd;
558 process_activity_event(data,
560 &no_activity_events);
563 process_control_event();
568 ALOGW("Invalid event user data: %d \n", events[i].data.fd);
572 ALOGW("Epoll events %i not expected\n", events[i].events);
574 /* Call the callback function for the retrieved events (if it
577 pthread_mutex_lock(&callback_mutex);
578 if (activity_dev_callback.activity_callback) {
579 activity_dev_callback.activity_callback(
580 &activity_dev_callback,
584 pthread_mutex_unlock(&callback_mutex);
588 static int set_up_control_data(void)
590 struct epoll_event control_ev, exit_ev;
593 ret = pthread_mutex_init(&callback_mutex, NULL);
597 /* Maximum fds is maximum activities + 1 control fd + 1 exit fd */
598 poll_fd = epoll_create(MAX_ACTIVITIES + 2);
602 goto poll_control_err;
604 control_fd = eventfd(0, 0);
605 if (control_fd == -1) {
607 goto poll_control_err;
610 control_ev.events = EPOLLIN;
611 /* Set data field to event file descriptor */
612 control_ev.data.fd = CONTROL_FD;
613 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, control_fd, &control_ev);
615 goto control_data_err;
617 exit_fd = eventfd(0, 0);
619 ALOGE("Error allocating exit fd\n");
620 goto exit_control_err;
622 exit_ev.events = EPOLLIN;
623 exit_ev.data.fd = EXIT_FD;
624 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, exit_fd, &exit_ev);
626 ALOGE("Error adding exit fd to the polling pool\n");
630 /* Start worker thread to wait on all event sources */
631 ret = pthread_create(&control_thread, NULL, events_routine, NULL);
638 epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
642 epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
651 /* Returns the IIO_MOD_* equivalent to the given name. */
652 static int get_modifier_as_int(const char* mod)
654 if (strncmp(mod, "still", sizeof("still")) == 0)
655 return IIO_MOD_STILL;
657 if (strncmp(mod, "walking", sizeof("walking")) == 0)
658 return IIO_MOD_WALKING;
660 if (strncmp(mod, "running", sizeof("running")) == 0)
661 return IIO_MOD_RUNNING;
666 static void add_activity(int sensor_catalog_index, int channel_index,
667 int dev_num, const char *name)
669 int index, i, modifier;
671 if (count == MAX_ACTIVITIES) {
672 ALOGE("Trying to add more than supported activities!\n");
676 modifier = get_modifier_as_int(name);
678 ALOGE("Invalid channel name as modifier: %s\n", name);
684 for (i = 0; i < MAX_EVENTS_PER_ACTIVITY; i++) {
685 supported_activities[index].event[i] = NULL;
686 supported_activities[index].monitored[i] = false;
688 supported_activities[index].modifier = modifier;
689 supported_activities[index].event_count = 0;
690 supported_activities[index].sensor_catalog_index = sensor_catalog_index;
691 supported_activities[index].channel_index = channel_index;
692 supported_activities[index].dev_num = dev_num;
693 supported_activities[index].event_fd = -1;
695 supported_activity_names[index] = name;
698 static bool is_activity_valid(const char *activity_name)
702 /* Look if this activity has not been already added */
703 for (i = 1; i <= count; i++)
704 if (strcmp(supported_activity_names[i], activity_name) == 0)
707 /* Check that the found activity is recognized by this API */
708 for (i = 0; i < MAX_ACTIVITIES; i++)
709 if (strcmp(sysfs_activity_names[i], activity_name) == 0)
715 /* Get all possible activities provided by the IIO sensors */
716 static void discover_activity_events(void)
718 channel_descriptor_t *chann;
719 int i, num_channels, dev_num;
721 char event_sensors[catalog_size];
723 /* Discover event sensors */
724 for (dev_num = 0; dev_num < MAX_DEVICES; dev_num++) {
725 discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
726 for (index = 0; index < catalog_size; index++) {
727 if (!event_sensors[index])
730 num_channels = sensor_catalog[index].num_channels;
731 for (i = 0; i < num_channels; i++) {
732 chann = sensor_catalog[index].channel + i;
734 if (is_activity_valid(chann->name))
735 add_activity(index, i, dev_num, chann->name);
740 ALOGI("Discovered %d activities\n", count);
743 static int open_module(const struct hw_module_t *module, const char *id,
744 struct hw_device_t **device)
746 static struct activity_recognition_device activity_dev;
749 if (strncmp(id, ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, sizeof(ACTIVITY_RECOGNITION_HARDWARE_INTERFACE)) != 0)
752 activity_dev.common.tag = HARDWARE_DEVICE_TAG;
753 activity_dev.common.version = ACTIVITY_RECOGNITION_API_VERSION_0_1;
754 activity_dev.common.module = (struct hw_module_t *) module;
755 activity_dev.common.close = close_device;
757 activity_dev.register_activity_callback = register_activity_callback;
758 activity_dev.enable_activity_event = enable_activity_event;
759 activity_dev.disable_activity_event = disable_activity_event;
760 activity_dev.flush = flush;
762 *device = &activity_dev.common;
764 if (instances_count == 0) {
765 discover_activity_events();
766 ret = set_up_control_data();
768 ALOGI("Initialized activity recognition HAL (exit code %i)\n", ret);
776 static struct hw_module_methods_t module_methods = {
781 static int get_supported_activities_list(struct activity_recognition_module *module __attribute((unused)),
782 char const* const* *activity_list)
784 *activity_list = supported_activity_names + 1;
789 /* Module descriptor visible to the Android framework. */
790 struct activity_recognition_module __attribute__ ((visibility ("default")))
791 HAL_MODULE_INFO_SYM = {
793 .tag = HARDWARE_MODULE_TAG,
794 .module_api_version = MODULE_VERSION,
795 .hal_api_version = HAL_VERSION,
796 .id = ACTIVITY_RECOGNITION_HARDWARE_MODULE_ID,
798 .author = MODULE_AUTHOR,
799 .methods = &module_methods,
801 .get_supported_activities_list = get_supported_activities_list