OSDN Git Service

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