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
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/platform/assert.h"
21 #include "chre/platform/fatal_error.h"
22 #include "chre/platform/log.h"
23 #include "chre/util/system/debug_dump.h"
24
25 namespace chre {
26
isRegisteredForBroadcastEvent(uint16_t eventType) const27 bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const {
28 return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size());
29 }
30
registerForBroadcastEvent(uint16_t eventId)31 bool Nanoapp::registerForBroadcastEvent(uint16_t eventId) {
32 if (isRegisteredForBroadcastEvent(eventId)) {
33 return false;
34 }
35
36 if (!mRegisteredEvents.push_back(eventId)) {
37 FATAL_ERROR("App failed to register for event: out of memory");
38 }
39
40 return true;
41 }
42
unregisterForBroadcastEvent(uint16_t eventId)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
postEvent(Event * event)53 void Nanoapp::postEvent(Event *event) {
54 mEventQueue.push(event);
55 }
56
hasPendingEvent()57 bool Nanoapp::hasPendingEvent() {
58 return !mEventQueue.empty();
59 }
60
configureNanoappInfoEvents(bool enable)61 void Nanoapp::configureNanoappInfoEvents(bool enable) {
62 if (enable) {
63 registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
64 registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
65 } else {
66 unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
67 unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
68 }
69 }
70
configureHostSleepEvents(bool enable)71 void Nanoapp::configureHostSleepEvents(bool enable) {
72 if (enable) {
73 registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
74 registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
75 } else {
76 unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
77 unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
78 }
79 }
80
processNextEvent()81 Event *Nanoapp::processNextEvent() {
82 Event *event = mEventQueue.pop();
83
84 CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty");
85 if (event != nullptr) {
86 handleEvent(event->senderInstanceId, event->eventType, event->eventData);
87 }
88
89 return event;
90 }
91
logStateToBuffer(char * buffer,size_t * bufferPos,size_t bufferSize) const92 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
104 } // namespace chre
105