Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "chre/core/nanoapp.h" |
| 18 | |
Brian Duddie | 9d5b500 | 2017-03-22 16:13:24 -0700 | [diff] [blame] | 19 | #include "chre/core/event_loop_manager.h" |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 20 | #include "chre/platform/assert.h" |
Andrew Rossignol | 1b895c7 | 2016-12-21 17:41:14 -0800 | [diff] [blame] | 21 | #include "chre/platform/fatal_error.h" |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 22 | #include "chre/platform/log.h" |
Arthur Ishiguro | d0530f4 | 2017-06-16 13:26:37 -0700 | [diff] [blame] | 23 | #include "chre/util/system/debug_dump.h" |
Arthur Ishiguro | 82af7e9 | 2021-06-14 15:19:35 -0700 | [diff] [blame] | 24 | #include "chre_api/chre/gnss.h" |
Brian Duddie | e8e5e7f | 2020-04-10 18:21:39 -0700 | [diff] [blame] | 25 | #include "chre_api/chre/version.h" |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 26 | |
Stan Rokita | b3b6b82 | 2019-07-29 12:57:04 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
Arthur Ishiguro | 82af7e9 | 2021-06-14 15:19:35 -0700 | [diff] [blame] | 29 | #if CHRE_FIRST_SUPPORTED_API_VERSION < CHRE_API_VERSION_1_5 |
| 30 | #define CHRE_GNSS_MEASUREMENT_BACK_COMPAT_ENABLED |
| 31 | #endif |
| 32 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 33 | namespace chre { |
| 34 | |
Stan Rokita | aed1e97 | 2019-08-08 14:05:33 -0700 | [diff] [blame] | 35 | constexpr size_t Nanoapp::kMaxSizeWakeupBuckets; |
| 36 | |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 37 | Nanoapp::Nanoapp() { |
| 38 | // Push first bucket onto wakeup bucket queue |
| 39 | cycleWakeupBuckets(1); |
| 40 | } |
| 41 | |
Anthony Stange | fd7d418 | 2019-04-05 14:29:48 -0400 | [diff] [blame] | 42 | Nanoapp::~Nanoapp() { |
karthik bharadwaj | fbf1545 | 2019-10-11 14:12:51 -0700 | [diff] [blame] | 43 | const size_t totalAllocatedBytes = getTotalAllocatedBytes(); |
| 44 | |
| 45 | if (totalAllocatedBytes > 0) { |
| 46 | // TODO: Consider asserting here |
| 47 | LOGE("Nanoapp ID=0x%016" PRIx64 " still has %zu allocated bytes!", |
| 48 | getAppId(), totalAllocatedBytes); |
| 49 | } |
Anthony Stange | fd7d418 | 2019-04-05 14:29:48 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Anthony Stange | def6edb | 2021-04-13 21:53:42 +0000 | [diff] [blame] | 52 | bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType, |
| 53 | uint16_t targetGroupIdMask) const { |
| 54 | bool registered = false; |
| 55 | size_t foundIndex = registrationIndex(eventType); |
| 56 | if (foundIndex < mRegisteredEvents.size()) { |
| 57 | const EventRegistration ® = mRegisteredEvents[foundIndex]; |
| 58 | if (targetGroupIdMask & reg.groupIdMask) { |
| 59 | registered = true; |
| 60 | } |
| 61 | } |
| 62 | return registered; |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Anthony Stange | def6edb | 2021-04-13 21:53:42 +0000 | [diff] [blame] | 65 | void Nanoapp::registerForBroadcastEvent(uint16_t eventType, |
| 66 | uint16_t groupIdMask) { |
| 67 | size_t foundIndex = registrationIndex(eventType); |
| 68 | if (foundIndex < mRegisteredEvents.size()) { |
| 69 | mRegisteredEvents[foundIndex].groupIdMask |= groupIdMask; |
| 70 | } else if (!mRegisteredEvents.push_back( |
| 71 | EventRegistration(eventType, groupIdMask))) { |
Andrew Rossignol | 0f2e64a | 2018-10-31 13:26:43 -0700 | [diff] [blame] | 72 | FATAL_ERROR_OOM(); |
Andrew Rossignol | 1b895c7 | 2016-12-21 17:41:14 -0800 | [diff] [blame] | 73 | } |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Anthony Stange | def6edb | 2021-04-13 21:53:42 +0000 | [diff] [blame] | 76 | void Nanoapp::unregisterForBroadcastEvent(uint16_t eventType, |
| 77 | uint16_t groupIdMask) { |
| 78 | size_t foundIndex = registrationIndex(eventType); |
| 79 | if (foundIndex < mRegisteredEvents.size()) { |
| 80 | EventRegistration ® = mRegisteredEvents[foundIndex]; |
| 81 | reg.groupIdMask &= ~groupIdMask; |
| 82 | if (reg.groupIdMask == 0) { |
| 83 | mRegisteredEvents.erase(foundIndex); |
| 84 | } |
Andrew Rossignol | 29b8ae8 | 2017-01-26 11:49:45 -0800 | [diff] [blame] | 85 | } |
Andrew Rossignol | 29b8ae8 | 2017-01-26 11:49:45 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 88 | void Nanoapp::configureNanoappInfoEvents(bool enable) { |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 89 | if (enable) { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 90 | registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED); |
| 91 | registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED); |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 92 | } else { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 93 | unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED); |
| 94 | unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED); |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 98 | void Nanoapp::configureHostSleepEvents(bool enable) { |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 99 | if (enable) { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 100 | registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE); |
| 101 | registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP); |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 102 | } else { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 103 | unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE); |
| 104 | unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP); |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Meng-hsuan Chung | a697f4e | 2020-03-09 16:46:58 -0700 | [diff] [blame] | 108 | void Nanoapp::configureDebugDumpEvent(bool enable) { |
| 109 | if (enable) { |
| 110 | registerForBroadcastEvent(CHRE_EVENT_DEBUG_DUMP); |
| 111 | } else { |
| 112 | unregisterForBroadcastEvent(CHRE_EVENT_DEBUG_DUMP); |
| 113 | } |
| 114 | } |
| 115 | |
karthik bharadwaj | c107950 | 2021-03-17 12:32:18 -0700 | [diff] [blame] | 116 | void Nanoapp::configureUserSettingEvent(uint8_t setting, bool enable) { |
| 117 | if (enable) { |
| 118 | registerForBroadcastEvent(CHRE_EVENT_SETTING_CHANGED_FIRST_EVENT + setting); |
| 119 | } else { |
| 120 | unregisterForBroadcastEvent(CHRE_EVENT_SETTING_CHANGED_FIRST_EVENT + |
| 121 | setting); |
| 122 | } |
| 123 | } |
| 124 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 125 | Event *Nanoapp::processNextEvent() { |
| 126 | Event *event = mEventQueue.pop(); |
| 127 | |
Brian Duddie | a2f5add | 2017-03-19 18:47:19 -0700 | [diff] [blame] | 128 | CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty"); |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 129 | if (event != nullptr) { |
Arthur Ishiguro | 82af7e9 | 2021-06-14 15:19:35 -0700 | [diff] [blame] | 130 | if (event->eventType == CHRE_EVENT_GNSS_DATA) { |
| 131 | handleGnssMeasurementDataEvent(event); |
| 132 | } else { |
| 133 | handleEvent(event->senderInstanceId, event->eventType, event->eventData); |
| 134 | } |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return event; |
| 138 | } |
| 139 | |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 140 | void Nanoapp::blameHostWakeup() { |
Stan Rokita | b3b6b82 | 2019-07-29 12:57:04 -0700 | [diff] [blame] | 141 | if (mWakeupBuckets.back() < UINT16_MAX) ++mWakeupBuckets.back(); |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void Nanoapp::cycleWakeupBuckets(size_t numBuckets) { |
Stan Rokita | b3b6b82 | 2019-07-29 12:57:04 -0700 | [diff] [blame] | 145 | numBuckets = std::min(numBuckets, kMaxSizeWakeupBuckets); |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 146 | for (size_t i = 0; i < numBuckets; ++i) { |
Stan Rokita | b3b6b82 | 2019-07-29 12:57:04 -0700 | [diff] [blame] | 147 | if (mWakeupBuckets.full()) { |
| 148 | mWakeupBuckets.erase(0); |
| 149 | } |
| 150 | mWakeupBuckets.push_back(0); |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Stan Rokita | aed1e97 | 2019-08-08 14:05:33 -0700 | [diff] [blame] | 154 | void Nanoapp::logStateToBuffer(DebugDumpWrapper &debugDump) const { |
Brian Duddie | e8e5e7f | 2020-04-10 18:21:39 -0700 | [diff] [blame] | 155 | debugDump.print(" Id=%" PRIu32 " 0x%016" PRIx64 " ", getInstanceId(), |
| 156 | getAppId()); |
Stan Rokita | aed1e97 | 2019-08-08 14:05:33 -0700 | [diff] [blame] | 157 | PlatformNanoapp::logStateToBuffer(debugDump); |
Brian Duddie | e8e5e7f | 2020-04-10 18:21:39 -0700 | [diff] [blame] | 158 | debugDump.print(" v%" PRIu32 ".%" PRIu32 ".%" PRIu32 " tgtAPI=%" PRIu32 |
| 159 | ".%" PRIu32 " curAlloc=%zu peakAlloc=%zu", |
| 160 | CHRE_EXTRACT_MAJOR_VERSION(getAppVersion()), |
| 161 | CHRE_EXTRACT_MINOR_VERSION(getAppVersion()), |
| 162 | CHRE_EXTRACT_PATCH_VERSION(getAppVersion()), |
| 163 | CHRE_EXTRACT_MAJOR_VERSION(getTargetApiVersion()), |
| 164 | CHRE_EXTRACT_MINOR_VERSION(getTargetApiVersion()), |
| 165 | getTotalAllocatedBytes(), getPeakAllocatedBytes()); |
| 166 | debugDump.print(" hostWakeups=[ cur->"); |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 167 | // Get buckets latest -> earliest except last one |
| 168 | for (size_t i = mWakeupBuckets.size() - 1; i > 0; --i) { |
Stan Rokita | aed1e97 | 2019-08-08 14:05:33 -0700 | [diff] [blame] | 169 | debugDump.print("%" PRIu16 ", ", mWakeupBuckets[i]); |
Stan Rokita | 6e0d5ae | 2019-07-18 16:26:47 -0700 | [diff] [blame] | 170 | } |
Brian Duddie | e8e5e7f | 2020-04-10 18:21:39 -0700 | [diff] [blame] | 171 | // Earliest bucket gets no comma |
Stan Rokita | aed1e97 | 2019-08-08 14:05:33 -0700 | [diff] [blame] | 172 | debugDump.print("%" PRIu16 " ]\n", mWakeupBuckets.front()); |
Arthur Ishiguro | d0530f4 | 2017-06-16 13:26:37 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Anthony Stange | b346bb0 | 2021-02-12 15:44:18 +0000 | [diff] [blame] | 175 | bool Nanoapp::permitPermissionUse(uint32_t permission) const { |
| 176 | return !supportsAppPermissions() || |
| 177 | ((getAppPermissions() & permission) == permission); |
| 178 | } |
| 179 | |
Anthony Stange | def6edb | 2021-04-13 21:53:42 +0000 | [diff] [blame] | 180 | size_t Nanoapp::registrationIndex(uint16_t eventType) const { |
| 181 | size_t foundIndex = 0; |
| 182 | for (; foundIndex < mRegisteredEvents.size(); ++foundIndex) { |
| 183 | const EventRegistration ® = mRegisteredEvents[foundIndex]; |
| 184 | if (reg.eventType == eventType) { |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | return foundIndex; |
| 189 | } |
| 190 | |
Arthur Ishiguro | 82af7e9 | 2021-06-14 15:19:35 -0700 | [diff] [blame] | 191 | void Nanoapp::handleGnssMeasurementDataEvent(const Event *event) { |
| 192 | #ifdef CHRE_GNSS_MEASUREMENT_BACK_COMPAT_ENABLED |
| 193 | const struct chreGnssDataEvent *data = |
| 194 | static_cast<const struct chreGnssDataEvent *>(event->eventData); |
| 195 | if (getTargetApiVersion() < CHRE_API_VERSION_1_5 && |
| 196 | data->measurement_count > CHRE_GNSS_MAX_MEASUREMENT_PRE_1_5) { |
| 197 | chreGnssDataEvent localEvent; |
| 198 | memcpy(&localEvent, data, sizeof(struct chreGnssDataEvent)); |
| 199 | localEvent.measurement_count = CHRE_GNSS_MAX_MEASUREMENT_PRE_1_5; |
| 200 | handleEvent(event->senderInstanceId, event->eventType, &localEvent); |
| 201 | } else |
| 202 | #endif // CHRE_GNSS_MEASUREMENT_BACK_COMPAT_ENABLED |
| 203 | { |
| 204 | handleEvent(event->senderInstanceId, event->eventType, event->eventData); |
| 205 | } |
| 206 | } |
| 207 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 208 | } // namespace chre |