blob: 6ae90b766c4af1a57a0b86062f33a11b0c4e58fd [file] [log] [blame]
Aravind Akella462eae32014-03-14 19:00:45 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
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 * Activity Recognition HAL. The goal is to provide low power, low latency, always-on activity
19 * recognition implemented in hardware (i.e. these activity recognition algorithms/classifers
20 * should NOT be run on the AP). By low power we mean that this may be activated 24/7 without
21 * impacting the battery drain speed (goal in order of 1mW including the power for sensors).
22 * This HAL does not specify the input sources that are used towards detecting these activities.
23 * It has one monitor interface which can be used to batch activities for always-on
24 * activity_recognition and if the latency is zero, the same interface can be used for low latency
25 * detection.
26 */
27
28#ifndef ANDROID_ACTIVITY_RECOGNITION_INTERFACE_H
29#define ANDROID_ACTIVITY_RECOGNITION_INTERFACE_H
30
31#include <hardware/hardware.h>
32
33__BEGIN_DECLS
34
35#define ACTIVITY_RECOGNITION_HEADER_VERSION 1
36#define ACTIVITY_RECOGNITION_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION_2(0, 1, ACTIVITY_RECOGNITION_HEADER_VERSION)
37
38#define ACTIVITY_RECOGNITION_HARDWARE_MODULE_ID "activity_recognition"
39#define ACTIVITY_RECOGNITION_HARDWARE_INTERFACE "activity_recognition_hw_if"
40
41/*
42 * Define constants for various activity types. Multiple activities may be active at the same time
43 * and sometimes none of these activities may be active.
44 */
45
46/* Reserved. get_supported_activities_list() should not return this activity. */
Aravind Akella153c40c2014-05-07 12:01:43 -070047#define ACTIVITY_RESERVED (0)
Aravind Akella462eae32014-03-14 19:00:45 -070048
Aravind Akella153c40c2014-05-07 12:01:43 -070049#define ACTIVITY_IN_VEHICLE (1)
Aravind Akella462eae32014-03-14 19:00:45 -070050
Aravind Akella153c40c2014-05-07 12:01:43 -070051#define ACTIVITY_ON_BICYCLE (2)
Aravind Akella462eae32014-03-14 19:00:45 -070052
Aravind Akella153c40c2014-05-07 12:01:43 -070053#define ACTIVITY_WALKING (3)
Aravind Akella462eae32014-03-14 19:00:45 -070054
Aravind Akella153c40c2014-05-07 12:01:43 -070055#define ACTIVITY_RUNNING (4)
Aravind Akella462eae32014-03-14 19:00:45 -070056
Aravind Akella153c40c2014-05-07 12:01:43 -070057#define ACTIVITY_STILL (5)
Aravind Akella462eae32014-03-14 19:00:45 -070058
Aravind Akella153c40c2014-05-07 12:01:43 -070059#define ACTIVITY_TILTING (6)
Aravind Akella462eae32014-03-14 19:00:45 -070060
61/* Values for activity_event.event_types. */
62enum {
63 /*
64 * A flush_complete event which indicates that a flush() has been successfully completed. This
65 * does not correspond to any activity/event. An event of this type should be added to the end
66 * of a batch FIFO and it indicates that all the events in the batch FIFO have been successfully
67 * reported to the framework. An event of this type should be generated only if flush() has been
68 * explicitly called and if the FIFO is empty at the time flush() is called it should trivially
69 * return a flush_complete_event to indicate that the FIFO is empty.
70 *
71 * A flush complete event should have the following parameters set.
72 * activity_event_t.event_type = ACTIVITY_EVENT_TYPE_FLUSH_COMPLETE
Aravind Akella153c40c2014-05-07 12:01:43 -070073 * activity_event_t.activity = ACTIVITY_RESERVED
Aravind Akella462eae32014-03-14 19:00:45 -070074 * activity_event_t.timestamp = 0
75 * activity_event_t.reserved = 0
76 * See (*flush)() for more details.
77 */
78 ACTIVITY_EVENT_TYPE_FLUSH_COMPLETE = 0,
79
80 /* Signifies entering an activity. */
81 ACTIVITY_EVENT_TYPE_ENTER = 1,
82
83 /* Signifies exiting an activity. */
84 ACTIVITY_EVENT_TYPE_EXIT = 2
85};
86
87/*
88 * Each event is a separate activity with event_type indicating whether this activity has started
Aravind Akella153c40c2014-05-07 12:01:43 -070089 * or ended. Eg event: (event_type="enter", activity="ON_FOOT", timestamp)
Aravind Akella462eae32014-03-14 19:00:45 -070090 */
91typedef struct activity_event {
92 /* One of the ACTIVITY_EVENT_TYPE_* constants defined above. */
93 uint32_t event_type;
94
Aravind Akella153c40c2014-05-07 12:01:43 -070095 /* One of ACTIVITY_* constants defined above. */
96 uint32_t activity;
Aravind Akella462eae32014-03-14 19:00:45 -070097
98 /* Time at which the transition/event has occurred in nanoseconds using elapsedRealTimeNano. */
99 int64_t timestamp;
100
101 /* Set to zero. */
102 int32_t reserved[4];
103} activity_event_t;
104
105typedef struct activity_recognition_module {
Stewart Miles84d35492014-05-01 09:03:27 -0700106 /**
107 * Common methods of the activity recognition module. This *must* be the first member of
108 * activity_recognition_module as users of this structure will cast a hw_module_t to
109 * activity_recognition_module pointer in contexts where it's known the hw_module_t
110 * references an activity_recognition_module.
111 */
Aravind Akella462eae32014-03-14 19:00:45 -0700112 hw_module_t common;
113
114 /*
115 * List of all activities supported by this module. Each activity is represented as an integer.
Aravind Akella153c40c2014-05-07 12:01:43 -0700116 * Each value in the list is one of the ACTIVITY_* constants defined above. Return
Aravind Akella462eae32014-03-14 19:00:45 -0700117 * value is the size of this list.
118 */
119 int (*get_supported_activities_list)(struct activity_recognition_module* module,
120 int** activity_list);
121} activity_recognition_module_t;
122
123struct activity_recognition_device;
124
125typedef struct activity_recognition_callback_procs {
126 // Callback for activity_data. This is guaranteed to not invoke any HAL methods.
127 // Memory allocated for the events can be reused after this method returns.
128 // events - Array of activity_event_t s that are reported.
129 // count - size of the array.
Aravind Akella153c40c2014-05-07 12:01:43 -0700130 void (*activity_callback)(const struct activity_recognition_callback_procs* procs,
Aravind Akella462eae32014-03-14 19:00:45 -0700131 const activity_event_t* events, int count);
132} activity_recognition_callback_procs_t;
133
134typedef struct activity_recognition_device {
Stewart Miles84d35492014-05-01 09:03:27 -0700135 /**
136 * Common methods of the activity recognition device. This *must* be the first member of
137 * activity_recognition_device as users of this structure will cast a hw_device_t to
138 * activity_recognition_device pointer in contexts where it's known the hw_device_t
139 * references an activity_recognition_device.
140 */
Aravind Akella462eae32014-03-14 19:00:45 -0700141 hw_device_t common;
142
143 /*
144 * Sets the callback to invoke when there are events to report. This call overwrites the
145 * previously registered callback (if any).
146 */
147 void (*register_activity_callback)(const struct activity_recognition_device* dev,
148 const activity_recognition_callback_procs_t* callback);
149
150 /*
Aravind Akella153c40c2014-05-07 12:01:43 -0700151 * Activates monitoring of activity transitions. Activities need not be reported as soon as they
152 * are detected. The detected activities are stored in a FIFO and reported in batches when the
153 * "max_batch_report_latency" expires or when the batch FIFO is full. The implementation should
154 * allow the AP to go into suspend mode while the activities are detected and stored in the
155 * batch FIFO. Whenever events need to be reported (like when the FIFO is full or when the
156 * max_batch_report_latency has expired for an activity, event pair), it should wake_up the AP
157 * so that no events are lost. Activities are stored as transitions and they are allowed to
158 * overlap with each other. Each (activity, event_type) pair can be activated or deactivated
159 * independently of the other. The HAL implementation needs to keep track of which pairs are
160 * currently active and needs to detect only those pairs.
161 *
162 * activity - The specific activity that needs to be detected.
163 * event_type - Specific transition of the activity that needs to be detected.
164 * max_batch_report_latency_ns - a transition can be delayed by at most
165 * “max_batch_report_latency” nanoseconds.
Aravind Akella462eae32014-03-14 19:00:45 -0700166 * Return 0 on success, negative errno code otherwise.
167 */
Aravind Akella153c40c2014-05-07 12:01:43 -0700168 int (*enable_activity_event)(const struct activity_recognition_device* dev,
169 uint32_t activity, uint32_t event_type, int64_t max_batch_report_latency_ns);
170
171 /*
172 * Disables detection of a specific (activity, event_type) pair.
173 */
174 int (*disable_activity_event)(const struct activity_recognition_device* dev,
175 uint32_t activity, uint32_t event_type);
Aravind Akella462eae32014-03-14 19:00:45 -0700176
177 /*
178 * Flush all the batch FIFOs. Report all the activities that were stored in the FIFO so far as
179 * if max_batch_report_latency had expired. This shouldn't change the latency in any way. Add
180 * a flush_complete_event to indicate the end of the FIFO after all events are delivered.
181 * See ACTIVITY_EVENT_TYPE_FLUSH_COMPLETE for more details.
182 * Return 0 on success, negative errno code otherwise.
183 */
184 int (*flush)(const struct activity_recognition_device* dev);
185
186 // Must be set to NULL.
Aravind Akella153c40c2014-05-07 12:01:43 -0700187 void (*reserved_procs[16 - 4])(void);
Aravind Akella462eae32014-03-14 19:00:45 -0700188} activity_recognition_device_t;
189
190static inline int activity_recognition_open(const hw_module_t* module,
191 activity_recognition_device_t** device) {
192 return module->methods->open(module,
193 ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, (hw_device_t**)device);
194}
195
196static inline int activity_recognition_close(activity_recognition_device_t* device) {
197 return device->common.close(&device->common);
198}
199
200__END_DECLS
201
202#endif // ANDROID_ACTIVITY_RECOGNITION_INTERFACE_H