blob: cfc65ce07bd2b195b465bf39b008867649ec1e67 [file] [log] [blame]
Brian Duddiee64f1802016-11-03 17:13:42 -07001/*
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 Duddie9d5b5002017-03-22 16:13:24 -070019#include "chre/core/event_loop_manager.h"
Brian Duddiee64f1802016-11-03 17:13:42 -070020#include "chre/platform/assert.h"
Andrew Rossignol1b895c72016-12-21 17:41:14 -080021#include "chre/platform/fatal_error.h"
Brian Duddiee64f1802016-11-03 17:13:42 -070022#include "chre/platform/log.h"
Arthur Ishigurod0530f42017-06-16 13:26:37 -070023#include "chre/util/system/debug_dump.h"
Brian Duddiee64f1802016-11-03 17:13:42 -070024
25namespace chre {
26
Brian Duddiee64f1802016-11-03 17:13:42 -070027bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const {
Andrew Rossignol6248f7a2017-05-01 16:24:06 -070028 return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size());
Brian Duddiee64f1802016-11-03 17:13:42 -070029}
30
31bool Nanoapp::registerForBroadcastEvent(uint16_t eventId) {
Andrew Rossignol1b895c72016-12-21 17:41:14 -080032 if (isRegisteredForBroadcastEvent(eventId)) {
33 return false;
34 }
35
36 if (!mRegisteredEvents.push_back(eventId)) {
Andrew Rossignol0f2e64a2018-10-31 13:26:43 -070037 FATAL_ERROR_OOM();
Andrew Rossignol1b895c72016-12-21 17:41:14 -080038 }
39
40 return true;
Brian Duddiee64f1802016-11-03 17:13:42 -070041}
42
Andrew Rossignol29b8ae82017-01-26 11:49:45 -080043bool 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 Duddiee64f1802016-11-03 17:13:42 -070053void Nanoapp::postEvent(Event *event) {
54 mEventQueue.push(event);
55}
56
Brian Duddiee64f1802016-11-03 17:13:42 -070057bool Nanoapp::hasPendingEvent() {
58 return !mEventQueue.empty();
59}
60
Andrew Rossignol6248f7a2017-05-01 16:24:06 -070061void Nanoapp::configureNanoappInfoEvents(bool enable) {
Andrew Rossignol6248f7a2017-05-01 16:24:06 -070062 if (enable) {
Brian Duddie33e9d2a2018-02-21 15:50:52 -080063 registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
64 registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
Andrew Rossignol6248f7a2017-05-01 16:24:06 -070065 } else {
Brian Duddie33e9d2a2018-02-21 15:50:52 -080066 unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
67 unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
Andrew Rossignol6248f7a2017-05-01 16:24:06 -070068 }
69}
70
Andrew Rossignolb3ef65f2017-11-17 11:33:40 -080071void Nanoapp::configureHostSleepEvents(bool enable) {
Andrew Rossignolb3ef65f2017-11-17 11:33:40 -080072 if (enable) {
Brian Duddie33e9d2a2018-02-21 15:50:52 -080073 registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
74 registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
Andrew Rossignolb3ef65f2017-11-17 11:33:40 -080075 } else {
Brian Duddie33e9d2a2018-02-21 15:50:52 -080076 unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
77 unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
Andrew Rossignolb3ef65f2017-11-17 11:33:40 -080078 }
79}
80
Brian Duddiee64f1802016-11-03 17:13:42 -070081Event *Nanoapp::processNextEvent() {
82 Event *event = mEventQueue.pop();
83
Brian Duddiea2f5add2017-03-19 18:47:19 -070084 CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty");
Brian Duddiee64f1802016-11-03 17:13:42 -070085 if (event != nullptr) {
Brian Duddie9d5b5002017-03-22 16:13:24 -070086 handleEvent(event->senderInstanceId, event->eventType, event->eventData);
Brian Duddiee64f1802016-11-03 17:13:42 -070087 }
88
89 return event;
90}
91
Arthur Ishigurod0530f42017-06-16 13:26:37 -070092bool 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 Duddiee64f1802016-11-03 17:13:42 -0700104} // namespace chre