OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into mr1
[android-x86/hardware-intel-libsensors.git] / activity_event_entry.c
1 /*
2  * Copyright (C) 2015 Intel Corporation.
3  *
4  * This file represents the entry point for the activity recognition HAL module.
5  */
6
7 #include <utils/Log.h>
8 #include <sys/epoll.h>
9 #include <sys/eventfd.h>
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <errno.h>
14 #include <pthread.h>
15 #include <fcntl.h>
16 #include <hardware/sensors.h>
17
18 #include "common.h"
19 #include "activity_event_utils.h"
20
21 #define MODULE_VERSION          1
22 #define HAL_VERSION             0
23
24 #define MODULE_NAME             "Activity recognition HAL"
25 #define MODULE_AUTHOR           "Intel"
26
27 #define CONTROL_FD              (-1)
28 #define EXIT_FD                 (-2)
29
30 /*
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.
34  */
35 sensor_catalog_entry_t sensor_catalog[] = {
36         {
37                 .tag            = "activity",
38                 .type           = SENSOR_TYPE_SIGNIFICANT_MOTION,
39                 .num_channels   = 3,
40                 .is_virtual     = 0,
41                 .channel        = {
42                         {
43                                 DECLARE_VOID_CHANNEL("still")
44                                 .num_events = 2,
45                                 .event = {
46                                         { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "rising") },
47                                         { DECLARE_GENERIC_EVENT("activity", "still", "thresh", "falling") },
48                                 },
49                         },
50                         {
51                                 DECLARE_VOID_CHANNEL("walking")
52                                 .num_events = 2,
53                                 .event = {
54                                         { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "rising") },
55                                         { DECLARE_GENERIC_EVENT("activity", "walking", "thresh", "falling") },
56                                 },
57                         },
58                         {
59                                 DECLARE_VOID_CHANNEL("running")
60                                 .num_events = 2,
61                                 .event = {
62                                         { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "rising") },
63                                         { DECLARE_GENERIC_EVENT("activity", "running", "thresh", "falling") },
64                                 },
65                         },
66                 },
67         },
68 };
69
70 unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
71
72 /* All possible activities - see activity_recognition.h */
73 static char const* sysfs_activity_names[MAX_ACTIVITIES] = {
74         "in_vehicle",
75         "on_bicycle",
76         "walking",
77         "running",
78         "still",
79         "tilting",
80 };
81
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.
87  */
88 static char const* supported_activity_names[MAX_ACTIVITIES + 1];
89 /* Supported activities count */
90 static unsigned int count;
91
92 static int poll_fd, control_fd, exit_fd;
93 static pthread_t control_thread;
94
95 static activity_recognition_callback_procs_t activity_dev_callback;
96 static pthread_mutex_t callback_mutex;
97
98 static int instances_count;
99
100 static void register_activity_callback(const struct activity_recognition_device *dev __attribute((unused)),
101                                        const activity_recognition_callback_procs_t* callback)
102 {
103         pthread_mutex_lock(&callback_mutex);
104         activity_dev_callback.activity_callback = callback->activity_callback;
105         pthread_mutex_unlock(&callback_mutex);
106 }
107
108 static bool check_activity_event(uint32_t activity_handle, uint32_t event_type)
109 {
110         if (activity_handle > count)
111                 return false;
112
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)
116                 return 0;
117
118         switch (event_type) {
119                 case ACTIVITY_EVENT_ENTER:
120                 case ACTIVITY_EVENT_EXIT:
121                         return true;
122                 case ACTIVITY_EVENT_FLUSH_COMPLETE:
123                         /* Not supported yet */
124                 default:
125                         return false;
126         }
127 }
128
129 static int set_activity_event_state(const struct activity_recognition_device *dev __attribute((unused)),
130                                     uint32_t activity_handle, uint32_t event_type,
131                                     char const *action)
132 {
133         uint64_t control_code;
134         int ret;
135
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);
140                 return -EINVAL;
141         }
142
143         control_code = get_control_code((uint8_t) 1,
144                                         (uint8_t) activity_handle,
145                                         (uint8_t) event_type);
146
147         ret = write(control_fd, &control_code, sizeof(control_code));
148         if (ret < 0) {
149                 ALOGE("Error writing to control fd to %s activity event\n", action);
150                 return errno;
151         }
152
153         ALOGI("Sent %s <%s, %i> request\n", action, supported_activity_names[activity_handle], event_type);
154
155         return 0;
156 }
157
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)))
161 {
162         return set_activity_event_state(dev, activity_handle, event_type, "enable");
163 }
164
165 static int disable_activity_event(const struct activity_recognition_device *dev,
166                                   uint32_t activity_handle, uint32_t event_type)
167 {
168         return set_activity_event_state(dev, activity_handle, event_type, "disable");
169 }
170
171 /**
172  * For now, just report that the function call has been made, since we yet do
173  * not have a batch FIFO device.
174  */
175 static int flush(const struct activity_recognition_device *dev __attribute((unused)))
176 {
177         ALOGV("Flushing...\n");
178
179         return 0;
180 }
181
182 static void process_disabling_activity_ev(uint8_t activity, uint8_t event);
183
184 static int close_device(struct hw_device_t *device __attribute((unused)))
185 {
186         int j, ret, exit_ping;
187         unsigned int i;
188
189         if (!instances_count)
190                 return -EINVAL;
191
192         instances_count--;
193
194         if (instances_count)
195                 return 0;
196
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.
199          */
200         exit_ping = 1;
201         write(exit_fd, &exit_ping, sizeof(exit_ping));
202
203         /* Wait for worker thread to finish in order to release shared
204          * resources.
205          */
206         pthread_join(control_thread, NULL);
207
208         /* Close exit fd after sending the canceling request. */
209         ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
210         if (ret == -1)
211                 ALOGE("Error deleting exit fd from polling pool\n");
212         close(exit_fd);
213
214         /* Clean control data. */
215         ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
216         if (ret == -1)
217                 ALOGE("Error deleting control fd from polling pool\n");
218         close(control_fd);
219
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.
223          */
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(
228                                                               (uint8_t) i,
229                                                               supported_activities[i].event[j]->event_type);
230
231         close(poll_fd);
232
233         pthread_mutex_destroy(&callback_mutex);
234
235         ALOGI("Successfully closed device\n");
236
237         return 0;
238 }
239
240 /*
241  * Finds the event given by type in the sensor's channel structure and retrieves
242  * its index.
243  * Equivalence (activity HAL naming - sensor HAL naming):
244  *      ACTIVITY_EVENT_ENTER - "rising"
245  *      ACTIVITY_EVENT_EXIT - "falling"
246  */
247 static int get_ev_index(int ev_type, channel_descriptor_t *chann)
248 {
249         int i;
250         char const *ev_dir;
251
252         switch (ev_type) {
253                 case ACTIVITY_EVENT_ENTER:
254                         ev_dir = "rising";
255                         break;
256                 case ACTIVITY_EVENT_EXIT:
257                         ev_dir = "falling";
258                         break;
259                 default:
260                         ev_dir = NULL;
261                         return -1;
262         }
263
264         for (i = 0; i < chann->num_events; i++) {
265                 if (strcmp(ev_dir, chann->event[i].dir) == 0)
266                         return i;
267         }
268
269         return -1;
270 }
271
272 static int set_event_enabling(int dev_num, const char *en_path, int value)
273 {
274         char path[PATH_MAX];
275         int ret;
276
277         ret = snprintf(path, sizeof(EVENTS_PATH) + sizeof(en_path), EVENTS_PATH "%s", dev_num, en_path);
278         if (ret < 0)
279                 return ret;
280
281         return sysfs_write_int(path, value);
282
283 }
284
285 static void process_enabling_activity_ev(uint8_t activity, uint8_t event)
286 {
287         struct activity_event_info *activ = supported_activities + activity;
288         channel_descriptor_t *chann;
289         struct activity_event *ev;
290         char path[PATH_MAX];
291         struct epoll_event ev_data;
292         int dev_fd, ev_index, ret;
293         unsigned int i;
294         bool open_now = false;
295
296         /* Allocate event structure and populate it */
297         ev = malloc(sizeof(*ev));
298         if (!ev) {
299                 ALOGE("Error allocating activity event for enabling\n");
300                 return;
301         }
302
303         ev->event_type  = (uint32_t) event;
304         ev->activity    = (uint32_t) activity;
305         ev->timestamp   = -1;
306
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.
311          */
312         if (activ->event_fd == -1) {
313                 ret = snprintf(path, sizeof(DEV_FILE_PATH), DEV_FILE_PATH, activ->dev_num);
314                 if (ret < 0)
315                         goto dev_err;
316
317                 dev_fd = open(path, O_RDONLY | O_NONBLOCK);
318                 if (dev_fd < 0)
319                         goto dev_err;
320
321                 ret = ioctl(dev_fd, IIO_GET_EVENT_FD_IOCTL, &activ->event_fd);
322                 close(dev_fd);
323                 if (ret < 0)
324                         goto dev_err;
325
326                 open_now = true;
327
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);
331                 if (ret == -1)
332                         goto event_err;
333
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;
338         }
339
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);
343         if (ev_index < 0) {
344                 ALOGE("Invalid event index: %d\n", ev_index);
345                 goto event_err;
346         }
347         ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 1);
348         if (ret < 0)
349                 goto event_err;
350
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.
354          */
355         activ->event[ev_index]          = ev;
356         activ->monitored[ev_index]      = true;
357         activ->event_count++;
358
359         return;
360
361 event_err:
362         if (open_now) {
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;
367         }
368 dev_err:
369         free(ev);
370 }
371
372 static bool device_not_monitored(int dev_num)
373 {
374         unsigned int i;
375
376         for (i = 1; i <= count; i++)
377                 if (supported_activities[i].dev_num == dev_num &&
378                     supported_activities[i].event_count > 0) {
379                         return false;
380                 }
381
382         return true;
383 }
384
385 static void process_disabling_activity_ev(uint8_t activity, uint8_t event)
386 {
387         struct activity_event_info *activ = supported_activities + activity;
388         channel_descriptor_t *chann;
389         int ev_index, ret;
390         unsigned int i;
391
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);
395         if (ev_index < 0)
396                 ALOGE("Invalid event index: %d\n", ev_index);
397         else {
398                 ret = set_event_enabling(activ->dev_num, chann->event[ev_index].ev_en_path, 0);
399                 if (ret < 0)
400                         ALOGE("Could not deactivate event - writing error\n");
401         }
402
403         /* Mark that the <activity, event> pair is not monitored any longer. */
404         activ->monitored[ev_index] = false;
405         activ->event_count--;
406
407         /* Close the event fd if this is the last pair monitored for the given
408          * device and remove it from the polling pool.
409          */
410         if (device_not_monitored(activ->dev_num)) {
411                 ret = epoll_ctl(poll_fd, EPOLL_CTL_DEL, activ->event_fd, NULL);
412                 if (ret == -1) {
413                         ALOGE("Error removing event fd from polling pool\n");
414                         return;
415                 }
416
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;
421         }
422
423         /* Free resources. */
424         free(activ->event[ev_index]);
425         activ->event[ev_index] = NULL;
426 }
427
428 static void process_control_event(void)
429 {
430         struct control_event_data control_data;
431         uint64_t control_code;
432         ssize_t ret;
433
434         /* Read control data from the control fd and interpret it */
435         ret = read(control_fd, &control_code, sizeof(control_code));
436         if (ret < 0) {
437                 ALOGW("Error reading from control fd\n");
438                 return;
439         }
440
441         get_control_data(control_code, &control_data);
442
443         if (control_data.enable)
444                 process_enabling_activity_ev(control_data.activity, control_data.event);
445         else
446                 process_disabling_activity_ev(control_data.activity, control_data.event);
447 }
448
449 static int get_activity_index(int modifier)
450 {
451         unsigned int i;
452
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)
456                         return i;
457
458         return -1;
459 }
460
461 static void process_activity_event(int fd, struct activity_event events[], int *count)
462 {
463         struct iio_event_data event;
464         int ret, chann_type, ev_type, ev_dir, ev_modifier, index, activity_index;
465
466         /* Retrieve event. */
467         ret = read(fd, &event, sizeof(event));
468         if (ret < 0) {
469                 ALOGE("Error reading event\n");
470                 return;
471         }
472
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");
477                 return;
478         }
479
480         ev_modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event.id);
481         switch (ev_modifier) {
482                 case IIO_MOD_STILL:
483                 case IIO_MOD_WALKING:
484                 case IIO_MOD_RUNNING:
485                         activity_index = get_activity_index(ev_modifier);
486                         if (activity_index >= 0)
487                                 break;
488                 default:
489                         ALOGW("Incompatible modifier - none of the supported activities is present\n");
490                         return;
491         }
492
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");
496                 return;
497         }
498
499         ev_dir = IIO_EVENT_CODE_EXTRACT_DIR(event.id);
500         switch (ev_dir) {
501                 case IIO_EV_DIR_RISING:
502                         ev_dir = ACTIVITY_EVENT_ENTER;
503                         break;
504                 case IIO_EV_DIR_FALLING:
505                         ev_dir = ACTIVITY_EVENT_EXIT;
506                         break;
507                 default:
508                         ALOGW("Incompatible event direction - only RISING and FALLING supported\n");
509                         return;
510         }
511
512         /* Add the activity event to the array for further processing. */
513         index = *count;
514         events[index].event_type        = ev_dir;
515         events[index].activity          = activity_index;
516         events[index].timestamp         = event.timestamp;
517
518         index++;
519         *count = index;
520 }
521
522 static void* events_routine(void *arg __attribute((unused)))
523 {
524         struct epoll_event events[MAX_ACTIVITIES + 2];
525         struct activity_event data_events[MAX_ACTIVITIES];
526         int no_events, i, no_activity_events;
527
528         while (1) {
529                 ALOGV("Waiting for sensor events ...\n");
530
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));
535                         continue;
536                 }
537
538                 for (i = 0; i < no_events; i++)
539                         if (events[i].events == EPOLLIN) {
540                                 int data = events[i].data.fd;
541
542                                 if (data >= 0)
543                                         process_activity_event(data,
544                                                                data_events,
545                                                                &no_activity_events);
546                                 else switch (data) {
547                                         case CONTROL_FD:
548                                                 process_control_event();
549                                                 break;
550                                         case EXIT_FD:
551                                                 return NULL;
552                                         default:
553                                                 ALOGW("Invalid event user data: %d \n", events[i].data.fd);
554                                                 break;
555                                 }
556                         } else
557                                 ALOGW("Epoll events %i not expected\n", events[i].events);
558
559                 /* Call the callback function for the retrieved events (if it
560                  * has been set).
561                  */
562                 pthread_mutex_lock(&callback_mutex);
563                 if (activity_dev_callback.activity_callback) {
564                         activity_dev_callback.activity_callback(
565                                                                 &activity_dev_callback,
566                                                                 data_events,
567                                                                 no_activity_events);
568                 }
569                 pthread_mutex_unlock(&callback_mutex);
570         }
571 }
572
573 static int set_up_control_data(void)
574 {
575         struct epoll_event control_ev, exit_ev;
576         int ret = 0;
577
578         ret = pthread_mutex_init(&callback_mutex, NULL);
579         if (ret)
580                 return ret;
581
582         /* Maximum fds is maximum activities + 1 control fd + 1 exit fd */
583         poll_fd = epoll_create(MAX_ACTIVITIES + 2);
584         if (poll_fd == -1)
585                 return errno;
586         if (ret)
587                 goto poll_control_err;
588
589         control_fd = eventfd(0, 0);
590         if (control_fd == -1) {
591                 ret = errno;
592                 goto poll_control_err;
593         }
594
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);
599         if (ret == -1)
600                 goto control_data_err;
601
602         exit_fd = eventfd(0, 0);
603         if (exit_fd == -1) {
604                 ALOGE("Error allocating exit fd\n");
605                 goto exit_control_err;
606         }
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);
610         if (ret == -1) {
611                 ALOGE("Error adding exit fd to the polling pool\n");
612                 goto exit_err;
613         }
614
615         /* Start worker thread to wait on all event sources */
616         ret = pthread_create(&control_thread, NULL, events_routine, NULL);
617         if (ret)
618                 goto thread_err;
619
620         return 0;
621
622 thread_err:
623         epoll_ctl(poll_fd, EPOLL_CTL_DEL, exit_fd, NULL);
624 exit_err:
625         close(exit_fd);
626 exit_control_err:
627         epoll_ctl(poll_fd, EPOLL_CTL_DEL, control_fd, NULL);
628 control_data_err:
629         close(control_fd);
630 poll_control_err:
631         close(poll_fd);
632
633         return ret;
634 }
635
636 /* Returns the IIO_MOD_* equivalent to the given name. */
637 static int get_modifier_as_int(const char* mod)
638 {
639         if (strncmp(mod, "still", sizeof("still")) == 0)
640                 return IIO_MOD_STILL;
641
642         if (strncmp(mod, "walking", sizeof("walking")) == 0)
643                 return IIO_MOD_WALKING;
644
645         if (strncmp(mod, "running", sizeof("running")) == 0)
646                 return IIO_MOD_RUNNING;
647
648         return -1;
649 }
650
651 static void add_activity(int sensor_catalog_index, int channel_index,
652                          int dev_num, const char *name)
653 {
654         int index, i, modifier;
655
656         if (count == MAX_ACTIVITIES) {
657                 ALOGE("Trying to add more than supported activities!\n");
658                 return;
659         }
660
661         modifier = get_modifier_as_int(name);
662         if (modifier < 0) {
663                 ALOGE("Invalid channel name as modifier: %s\n", name);
664                 return;
665         }
666
667         index = ++count;
668
669         for (i = 0; i < MAX_EVENTS_PER_ACTIVITY; i++) {
670                 supported_activities[index].event[i]            = NULL;
671                 supported_activities[index].monitored[i]        = false;
672         }
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;
679
680         supported_activity_names[index] = name;
681 }
682
683 static bool is_activity_valid(const char *activity_name)
684 {
685         unsigned int i;
686
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)
690                         return false;
691
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)
695                         return true;
696
697         return false;
698 }
699
700 /* Get all possible activities provided by the IIO sensors */
701 static void discover_activity_events(void)
702 {
703         channel_descriptor_t *chann;
704         int i, num_channels, dev_num;
705         unsigned int index;
706         char event_sensors[catalog_size];
707
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])
713                                 continue;
714
715                         num_channels = sensor_catalog[index].num_channels;
716                         for (i = 0; i < num_channels; i++) {
717                                 chann = sensor_catalog[index].channel + i;
718
719                                 if (is_activity_valid(chann->name))
720                                         add_activity(index, i, dev_num, chann->name);
721                         }
722                 }
723         }
724
725         ALOGI("Discovered %d activities\n", count);
726 }
727
728 static int open_module(const struct hw_module_t *module, const char *id,
729                        struct hw_device_t **device)
730 {
731         static struct activity_recognition_device activity_dev;
732         int ret = 0;
733
734         if (strncmp(id, ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, sizeof(ACTIVITY_RECOGNITION_HARDWARE_INTERFACE)) != 0)
735                 return -EINVAL;
736
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;
741
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;
746
747         *device = &activity_dev.common;
748
749         if (instances_count == 0) {
750                 discover_activity_events();
751                 ret = set_up_control_data();
752
753                 ALOGI("Initialized activity recognition HAL (exit code %i)\n", ret);
754         }
755
756         instances_count++;
757
758         return ret;
759 }
760
761 static struct hw_module_methods_t module_methods = {
762         .open = open_module
763 };
764
765
766 static int get_supported_activities_list(struct activity_recognition_module *module __attribute((unused)),
767                                          char const* const* *activity_list)
768 {
769         *activity_list = supported_activity_names + 1;
770
771         return count;
772 }
773
774 /* Module descriptor visible to the Android framework. */
775 struct activity_recognition_module __attribute__ ((visibility ("default")))
776         HAL_MODULE_INFO_SYM = {
777                 .common = {
778                         .tag                    = HARDWARE_MODULE_TAG,
779                         .module_api_version     = MODULE_VERSION,
780                         .hal_api_version        = HAL_VERSION,
781                         .id                     = ACTIVITY_RECOGNITION_HARDWARE_MODULE_ID,
782                         .name                   = MODULE_NAME,
783                         .author                 = MODULE_AUTHOR,
784                         .methods                = &module_methods,
785                 },
786                 .get_supported_activities_list = get_supported_activities_list
787
788 };