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 27 bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const { 28 return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size()); 29 } 30 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 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 53 void Nanoapp::postEvent(Event *event) { 54 mEventQueue.push(event); 55 } 56 57 bool Nanoapp::hasPendingEvent() { 58 return !mEventQueue.empty(); 59 } 60 61 void Nanoapp::configureNanoappInfoEvents(bool enable) { 62 bool success; 63 if (enable) { 64 success = registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED); 65 success &= registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED); 66 } else { 67 success = unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED); 68 success &= unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED); 69 } 70 71 if (!success) { 72 // An app has failed to register for an event and may be stuck waiting for 73 // an event that it will never receive. This is considered a fatal situation 74 // as the nanoapp is stuck and there is no way to recover other than 75 // unloading the nanoapp. This can happen in an OOM situation. 76 FATAL_ERROR("Failed to configure nanoapp info events to %d", enable); 77 } 78 } 79 80 Event *Nanoapp::processNextEvent() { 81 Event *event = mEventQueue.pop(); 82 83 CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty"); 84 if (event != nullptr) { 85 handleEvent(event->senderInstanceId, event->eventType, event->eventData); 86 } 87 88 return event; 89 } 90 91 bool Nanoapp::logStateToBuffer(char *buffer, size_t *bufferPos, 92 size_t bufferSize) const { 93 bool success = PlatformNanoapp::logStateToBuffer(buffer, bufferPos, 94 bufferSize); 95 success &= debugDumpPrint(buffer, bufferPos, bufferSize, 96 " Id=%" PRIu32 " AppId=0x%016" PRIx64 97 " ver=0x%" PRIx32 " targetAPI=0x%" PRIx32 "\n", 98 getInstanceId(), getAppId(), 99 getAppVersion(), getTargetApiVersion()); 100 return success; 101 } 102 103 } // namespace chre 104