blob: ecac856f1dc8649e7e04caa2eeb6e3276672894a [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. */
47#define DETECTED_ACTIVITY_RESERVED (0)
48
49#define DETECTED_ACTIVITY_IN_VEHICLE (1)
50
51#define DETECTED_ACTIVITY_ON_BICYCLE (2)
52
53#define DETECTED_ACTIVITY_WALKING (3)
54
55#define DETECTED_ACTIVITY_RUNNING (4)
56
57#define DETECTED_ACTIVITY_STILL (5)
58
59#define DETECTED_ACTIVITY_TILTING (6)
60
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
73 * activity_event_t.detected_activity = DETECTED_ACTIVITY_RESERVED
74 * 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
89 * or ended. Eg event: (event_type="enter", detected_activity="ON_FOOT", timestamp)
90 */
91typedef struct activity_event {
92 /* One of the ACTIVITY_EVENT_TYPE_* constants defined above. */
93 uint32_t event_type;
94
95 /* Detected Activity. One of DETECTED_ACTIVITY_TYPE_* constants defined above. */
96 int32_t detected_activity;
97
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.
116 * Each value in the list is one of the DETECTED_ACTIVITY_* constants defined above. Return
117 * 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.
130 void (*activity_callback)(const struct activity_recognition_device* dev,
131 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 /*
151 * Activates and deactivates monitoring of activity transitions. Activities need not be reported
152 * as soon as they are detected. The detected activities are stored in a FIFO and reported in
153 * batches when the "max_batch_report_latency" expires or when the batch FIFO is full. The
154 * implementation should allow the AP to go into suspend mode while the activities are detected
155 * and stored in the batch FIFO. Whenever events need to be reported (like when the FIFO is full
156 * or when the max_batch_report_latency has expired for an activity, event pair), it should
157 * wake_up the AP so that no events are lost. Activities are stored as transitions and they are
158 * allowed to overlap with each other.
159 * detected_activity - The specific activity that needs to be monitored.
160 * event_type - Specific transition of the activity that needs to be monitored.
161 * enabled - Enable/Disable detection of an (detected_activity, event_type) pair. Each
162 * pair can be activated or deactivated independently of the other. The HAL
163 * implementation needs to keep track of which pairs are currently active
164 * and needs to detect only those activities.
165 * max_batch_report_latency - a transition can be delayed by at most
166 * “max_batch_report_latency” nanoseconds.
167 * Return 0 on success, negative errno code otherwise.
168 */
169 int (*monitor_activity_event)(const struct activity_recognition_device* dev,
170 int32_t detected_activity, int32_t event_type, int64_t max_batch_report_latency_ns,
171 int32_t enabled);
172
173 /*
174 * Flush all the batch FIFOs. Report all the activities that were stored in the FIFO so far as
175 * if max_batch_report_latency had expired. This shouldn't change the latency in any way. Add
176 * a flush_complete_event to indicate the end of the FIFO after all events are delivered.
177 * See ACTIVITY_EVENT_TYPE_FLUSH_COMPLETE for more details.
178 * Return 0 on success, negative errno code otherwise.
179 */
180 int (*flush)(const struct activity_recognition_device* dev);
181
182 // Must be set to NULL.
183 void (*reserved_procs[4])(void);
184} activity_recognition_device_t;
185
186static inline int activity_recognition_open(const hw_module_t* module,
187 activity_recognition_device_t** device) {
188 return module->methods->open(module,
189 ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, (hw_device_t**)device);
190}
191
192static inline int activity_recognition_close(activity_recognition_device_t* device) {
193 return device->common.close(&device->common);
194}
195
196__END_DECLS
197
198#endif // ANDROID_ACTIVITY_RECOGNITION_INTERFACE_H