Home | History | Annotate | Download | only in core
      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/event_loop_manager.h"
     18 
     19 #include "chre/platform/fatal_error.h"
     20 #include "chre/platform/memory.h"
     21 #include "chre/util/lock_guard.h"
     22 
     23 namespace chre {
     24 
     25 void freeEventDataCallback(uint16_t /*eventType*/, void *eventData) {
     26   memoryFree(eventData);
     27 }
     28 
     29 Nanoapp *EventLoopManager::validateChreApiCall(const char *functionName) {
     30   chre::Nanoapp *currentNanoapp = EventLoopManagerSingleton::get()
     31       ->getEventLoop().getCurrentNanoapp();
     32   CHRE_ASSERT_LOG(currentNanoapp, "%s called with no CHRE app context",
     33                   functionName);
     34   return currentNanoapp;
     35 }
     36 
     37 UniquePtr<char> EventLoopManager::debugDump() {
     38   constexpr size_t kDebugStringSize = 4096;
     39   char *debugStr = static_cast<char *>(memoryAlloc(kDebugStringSize));
     40   if (debugStr != nullptr) {
     41     size_t debugStrPos = 0;
     42     if (!mMemoryManager.logStateToBuffer(debugStr, &debugStrPos,
     43                                          kDebugStringSize)) {
     44       LOGE("Memory manager debug dump failed.");
     45     } else if (!mEventLoop.logStateToBuffer(debugStr, &debugStrPos,
     46                                             kDebugStringSize)) {
     47       LOGE("Event loop debug dump failed.");
     48     } else if (!mSensorRequestManager.logStateToBuffer(debugStr, &debugStrPos,
     49                                                        kDebugStringSize)) {
     50       LOGE("Sensor request manager debug dump failed.");
     51     } else if (!mGnssRequestManager.logStateToBuffer(debugStr, &debugStrPos,
     52                                                      kDebugStringSize)) {
     53       LOGE("GNSS request manager debug dump failed.");
     54     } else if (!mWifiRequestManager.logStateToBuffer(debugStr, &debugStrPos,
     55                                                      kDebugStringSize)) {
     56       LOGE("Wifi request manager debug dump failed.");
     57     } else if (!mWwanRequestManager.logStateToBuffer(debugStr, &debugStrPos,
     58                                                      kDebugStringSize)) {
     59       LOGE("WWAN request manager debug dump failed.");
     60     }
     61     LOGD("Debug dump used %zu bytes of log buffer", debugStrPos);
     62   }
     63 
     64   return UniquePtr<char>(debugStr);
     65 }
     66 
     67 bool EventLoopManager::deferCallback(SystemCallbackType type, void *data,
     68                                      SystemCallbackFunction *callback) {
     69   return mEventLoop.postEvent(static_cast<uint16_t>(type), data, callback,
     70                               kSystemInstanceId, kSystemInstanceId);
     71 }
     72 
     73 uint32_t EventLoopManager::getNextInstanceId() {
     74   ++mLastInstanceId;
     75 
     76   // ~4 billion instance IDs should be enough for anyone... if we need to
     77   // support wraparound for stress testing load/unload, then we can set a flag
     78   // when wraparound occurs and use EventLoop::findNanoappByInstanceId to ensure
     79   // we avoid conflicts
     80   if (mLastInstanceId == kBroadcastInstanceId
     81       || mLastInstanceId == kSystemInstanceId) {
     82     FATAL_ERROR("Exhausted instance IDs!");
     83   }
     84 
     85   return mLastInstanceId;
     86 }
     87 
     88 void EventLoopManager::lateInit() {
     89   mGnssRequestManager.init();
     90   mWifiRequestManager.init();
     91   mWwanRequestManager.init();
     92 }
     93 
     94 // Explicitly instantiate the EventLoopManagerSingleton to reduce codesize.
     95 template class Singleton<EventLoopManager>;
     96 
     97 }  // namespace chre
     98