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 24 namespace chre { 25 26 uint32_t Nanoapp::getInstanceId() const { 27 return mInstanceId; 28 } 29 30 void Nanoapp::setInstanceId(uint32_t instanceId) { 31 mInstanceId = instanceId; 32 } 33 34 bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const { 35 return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size()); 36 } 37 38 bool Nanoapp::registerForBroadcastEvent(uint16_t eventId) { 39 if (isRegisteredForBroadcastEvent(eventId)) { 40 return false; 41 } 42 43 if (!mRegisteredEvents.push_back(eventId)) { 44 FATAL_ERROR("App failed to register for event: out of memory"); 45 } 46 47 return true; 48 } 49 50 bool Nanoapp::unregisterForBroadcastEvent(uint16_t eventId) { 51 size_t registeredEventIndex = mRegisteredEvents.find(eventId); 52 if (registeredEventIndex == mRegisteredEvents.size()) { 53 return false; 54 } 55 56 mRegisteredEvents.erase(registeredEventIndex); 57 return true; 58 } 59 60 void Nanoapp::postEvent(Event *event) { 61 mEventQueue.push(event); 62 } 63 64 bool Nanoapp::hasPendingEvent() { 65 return !mEventQueue.empty(); 66 } 67 68 Event *Nanoapp::processNextEvent() { 69 Event *event = mEventQueue.pop(); 70 71 CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty"); 72 if (event != nullptr) { 73 handleEvent(event->senderInstanceId, event->eventType, event->eventData); 74 } 75 76 return event; 77 } 78 79 } // namespace chre 80