Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 47 | #define ACTIVITY_RESERVED (0) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 48 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 49 | #define ACTIVITY_IN_VEHICLE (1) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 50 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 51 | #define ACTIVITY_ON_BICYCLE (2) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 52 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 53 | #define ACTIVITY_WALKING (3) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 54 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 55 | #define ACTIVITY_RUNNING (4) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 56 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 57 | #define ACTIVITY_STILL (5) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 58 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 59 | #define ACTIVITY_TILTING (6) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 60 | |
| 61 | /* Values for activity_event.event_types. */ |
| 62 | enum { |
| 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 Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 73 | * activity_event_t.activity = ACTIVITY_RESERVED |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 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 |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 89 | * or ended. Eg event: (event_type="enter", activity="ON_FOOT", timestamp) |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 90 | */ |
| 91 | typedef struct activity_event { |
| 92 | /* One of the ACTIVITY_EVENT_TYPE_* constants defined above. */ |
| 93 | uint32_t event_type; |
| 94 | |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 95 | /* One of ACTIVITY_* constants defined above. */ |
| 96 | uint32_t activity; |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 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 | |
| 105 | typedef struct activity_recognition_module { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 106 | /** |
| 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 Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 112 | hw_module_t common; |
| 113 | |
| 114 | /* |
| 115 | * List of all activities supported by this module. Each activity is represented as an integer. |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 116 | * Each value in the list is one of the ACTIVITY_* constants defined above. Return |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 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 | |
| 123 | struct activity_recognition_device; |
| 124 | |
| 125 | typedef 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 Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 130 | void (*activity_callback)(const struct activity_recognition_callback_procs* procs, |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 131 | const activity_event_t* events, int count); |
| 132 | } activity_recognition_callback_procs_t; |
| 133 | |
| 134 | typedef struct activity_recognition_device { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 135 | /** |
| 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 Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 141 | 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 Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 151 | * 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 Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 166 | * Return 0 on success, negative errno code otherwise. |
| 167 | */ |
Aravind Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 168 | 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 Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 176 | |
| 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 Akella | 153c40c | 2014-05-07 12:01:43 -0700 | [diff] [blame^] | 187 | void (*reserved_procs[16 - 4])(void); |
Aravind Akella | 462eae3 | 2014-03-14 19:00:45 -0700 | [diff] [blame] | 188 | } activity_recognition_device_t; |
| 189 | |
| 190 | static 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 | |
| 196 | static 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 |