OSDN Git Service

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