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" |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 24 | |
| 25 | namespace chre { |
| 26 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 27 | bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const { |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 28 | return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size()); |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool Nanoapp::registerForBroadcastEvent(uint16_t eventId) { |
Andrew Rossignol | 1b895c7 | 2016-12-21 17:41:14 -0800 | [diff] [blame] | 32 | if (isRegisteredForBroadcastEvent(eventId)) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if (!mRegisteredEvents.push_back(eventId)) { |
Andrew Rossignol | 0f2e64a | 2018-10-31 13:26:43 -0700 | [diff] [blame^] | 37 | FATAL_ERROR_OOM(); |
Andrew Rossignol | 1b895c7 | 2016-12-21 17:41:14 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | return true; |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Andrew Rossignol | 29b8ae8 | 2017-01-26 11:49:45 -0800 | [diff] [blame] | 43 | bool Nanoapp::unregisterForBroadcastEvent(uint16_t eventId) { |
| 44 | size_t registeredEventIndex = mRegisteredEvents.find(eventId); |
| 45 | if (registeredEventIndex == mRegisteredEvents.size()) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | mRegisteredEvents.erase(registeredEventIndex); |
| 50 | return true; |
| 51 | } |
| 52 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 53 | void Nanoapp::postEvent(Event *event) { |
| 54 | mEventQueue.push(event); |
| 55 | } |
| 56 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 57 | bool Nanoapp::hasPendingEvent() { |
| 58 | return !mEventQueue.empty(); |
| 59 | } |
| 60 | |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 61 | void Nanoapp::configureNanoappInfoEvents(bool enable) { |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 62 | if (enable) { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 63 | registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED); |
| 64 | registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED); |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 65 | } else { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 66 | unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED); |
| 67 | unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED); |
Andrew Rossignol | 6248f7a | 2017-05-01 16:24:06 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 71 | void Nanoapp::configureHostSleepEvents(bool enable) { |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 72 | if (enable) { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 73 | registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE); |
| 74 | registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP); |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 75 | } else { |
Brian Duddie | 33e9d2a | 2018-02-21 15:50:52 -0800 | [diff] [blame] | 76 | unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE); |
| 77 | unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP); |
Andrew Rossignol | b3ef65f | 2017-11-17 11:33:40 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 81 | Event *Nanoapp::processNextEvent() { |
| 82 | Event *event = mEventQueue.pop(); |
| 83 | |
Brian Duddie | a2f5add | 2017-03-19 18:47:19 -0700 | [diff] [blame] | 84 | CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty"); |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 85 | if (event != nullptr) { |
Brian Duddie | 9d5b500 | 2017-03-22 16:13:24 -0700 | [diff] [blame] | 86 | handleEvent(event->senderInstanceId, event->eventType, event->eventData); |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return event; |
| 90 | } |
| 91 | |
Arthur Ishiguro | d0530f4 | 2017-06-16 13:26:37 -0700 | [diff] [blame] | 92 | bool Nanoapp::logStateToBuffer(char *buffer, size_t *bufferPos, |
| 93 | size_t bufferSize) const { |
| 94 | bool success = PlatformNanoapp::logStateToBuffer(buffer, bufferPos, |
| 95 | bufferSize); |
| 96 | success &= debugDumpPrint(buffer, bufferPos, bufferSize, |
| 97 | " Id=%" PRIu32 " AppId=0x%016" PRIx64 |
| 98 | " ver=0x%" PRIx32 " targetAPI=0x%" PRIx32 "\n", |
| 99 | getInstanceId(), getAppId(), |
| 100 | getAppVersion(), getTargetApiVersion()); |
| 101 | return success; |
| 102 | } |
| 103 | |
Brian Duddie | e64f180 | 2016-11-03 17:13:42 -0700 | [diff] [blame] | 104 | } // namespace chre |