• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
~Nanoapp()27 Nanoapp::~Nanoapp() {
28   CHRE_ASSERT_LOG(getTotalAllocatedBytes() == 0,
29       "Nanoapp ID=0x%016" PRIx64 " still has %zu allocated bytes!", getAppId(),
30       getTotalAllocatedBytes());
31 }
32 
isRegisteredForBroadcastEvent(uint16_t eventType) const33 bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const {
34   return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size());
35 }
36 
registerForBroadcastEvent(uint16_t eventId)37 bool Nanoapp::registerForBroadcastEvent(uint16_t eventId) {
38   if (isRegisteredForBroadcastEvent(eventId)) {
39     return false;
40   }
41 
42   if (!mRegisteredEvents.push_back(eventId)) {
43     FATAL_ERROR_OOM();
44   }
45 
46   return true;
47 }
48 
unregisterForBroadcastEvent(uint16_t eventId)49 bool Nanoapp::unregisterForBroadcastEvent(uint16_t eventId) {
50   size_t registeredEventIndex = mRegisteredEvents.find(eventId);
51   if (registeredEventIndex == mRegisteredEvents.size()) {
52     return false;
53   }
54 
55   mRegisteredEvents.erase(registeredEventIndex);
56   return true;
57 }
58 
configureNanoappInfoEvents(bool enable)59 void Nanoapp::configureNanoappInfoEvents(bool enable) {
60   if (enable) {
61     registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
62     registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
63   } else {
64     unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
65     unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
66   }
67 }
68 
configureHostSleepEvents(bool enable)69 void Nanoapp::configureHostSleepEvents(bool enable) {
70   if (enable) {
71     registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
72     registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
73   } else {
74     unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
75     unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
76   }
77 }
78 
processNextEvent()79 Event *Nanoapp::processNextEvent() {
80   Event *event = mEventQueue.pop();
81 
82   CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty");
83   if (event != nullptr) {
84     handleEvent(event->senderInstanceId, event->eventType, event->eventData);
85   }
86 
87   return event;
88 }
89 
logStateToBuffer(char * buffer,size_t * bufferPos,size_t bufferSize) const90 void Nanoapp::logStateToBuffer(char *buffer, size_t *bufferPos,
91                                size_t bufferSize) const {
92   PlatformNanoapp::logStateToBuffer(buffer, bufferPos, bufferSize);
93   debugDumpPrint(
94       buffer, bufferPos, bufferSize,
95       " Id=%" PRIu32 " AppId=0x%016" PRIx64
96       " ver=0x%" PRIx32 " targetAPI=0x%" PRIx32
97       " currentAllocatedBytes=%zu peakAllocatedBytes=%zu\n",
98       getInstanceId(), getAppId(), getAppVersion(), getTargetApiVersion(),
99       getTotalAllocatedBytes(), getPeakAllocatedBytes());
100 }
101 
102 }  // namespace chre
103