blob: 3d3c1bdd6b9d36fe1adf773f6ff9ae397e01c573 [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 {
106 hw_module_t common;
107
108 /*
109 * List of all activities supported by this module. Each activity is represented as an integer.
110 * Each value in the list is one of the DETECTED_ACTIVITY_* constants defined above. Return
111 * value is the size of this list.
112 */
113 int (*get_supported_activities_list)(struct activity_recognition_module* module,
114 int** activity_list);
115} activity_recognition_module_t;
116
117struct activity_recognition_device;
118
119typedef struct activity_recognition_callback_procs {
120 // Callback for activity_data. This is guaranteed to not invoke any HAL methods.
121 // Memory allocated for the events can be reused after this method returns.
122 // events - Array of activity_event_t s that are reported.
123 // count - size of the array.
124 void (*activity_callback)(const struct activity_recognition_device* dev,
125 const activity_event_t* events, int count);
126} activity_recognition_callback_procs_t;
127
128typedef struct activity_recognition_device {
129 hw_device_t common;
130
131 /*
132 * Sets the callback to invoke when there are events to report. This call overwrites the
133 * previously registered callback (if any).
134 */
135 void (*register_activity_callback)(const struct activity_recognition_device* dev,
136 const activity_recognition_callback_procs_t* callback);
137
138 /*
139 * Activates and deactivates monitoring of activity transitions. Activities need not be reported
140 * as soon as they are detected. The detected activities are stored in a FIFO and reported in
141 * batches when the "max_batch_report_latency" expires or when the batch FIFO is full. The
142 * implementation should allow the AP to go into suspend mode while the activities are detected
143 * and stored in the batch FIFO. Whenever events need to be reported (like when the FIFO is full
144 * or when the max_batch_report_latency has expired for an activity, event pair), it should
145 * wake_up the AP so that no events are lost. Activities are stored as transitions and they are
146 * allowed to overlap with each other.
147 * detected_activity - The specific activity that needs to be monitored.
148 * event_type - Specific transition of the activity that needs to be monitored.
149 * enabled - Enable/Disable detection of an (detected_activity, event_type) pair. Each
150 * pair can be activated or deactivated independently of the other. The HAL
151 * implementation needs to keep track of which pairs are currently active
152 * and needs to detect only those activities.
153 * max_batch_report_latency - a transition can be delayed by at most
154 * “max_batch_report_latency” nanoseconds.
155 * Return 0 on success, negative errno code otherwise.
156 */
157 int (*monitor_activity_event)(const struct activity_recognition_device* dev,
158 int32_t detected_activity, int32_t event_type, int64_t max_batch_report_latency_ns,
159 int32_t enabled);
160
161 /*
162 * Flush all the batch FIFOs. Report all the activities that were stored in the FIFO so far as
163 * if max_batch_report_latency had expired. This shouldn't change the latency in any way. Add
164 * a flush_complete_event to indicate the end of the FIFO after all events are delivered.
165 * See ACTIVITY_EVENT_TYPE_FLUSH_COMPLETE for more details.
166 * Return 0 on success, negative errno code otherwise.
167 */
168 int (*flush)(const struct activity_recognition_device* dev);
169
170 // Must be set to NULL.
171 void (*reserved_procs[4])(void);
172} activity_recognition_device_t;
173
174static inline int activity_recognition_open(const hw_module_t* module,
175 activity_recognition_device_t** device) {
176 return module->methods->open(module,
177 ACTIVITY_RECOGNITION_HARDWARE_INTERFACE, (hw_device_t**)device);
178}
179
180static inline int activity_recognition_close(activity_recognition_device_t* device) {
181 return device->common.close(&device->common);
182}
183
184__END_DECLS
185
186#endif // ANDROID_ACTIVITY_RECOGNITION_INTERFACE_H