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
23namespace chre {
24
25void freeEventDataCallback(uint16_t /*eventType*/, void *eventData) {
26  memoryFree(eventData);
27}
28
29Nanoapp *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
37UniquePtr<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 (!mGnssManager.logStateToBuffer(debugStr, &debugStrPos,
52                                              kDebugStringSize)) {
53      LOGE("GNSS 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
67uint32_t EventLoopManager::getNextInstanceId() {
68  ++mLastInstanceId;
69
70  // ~4 billion instance IDs should be enough for anyone... if we need to
71  // support wraparound for stress testing load/unload, then we can set a flag
72  // when wraparound occurs and use EventLoop::findNanoappByInstanceId to ensure
73  // we avoid conflicts
74  if (mLastInstanceId == kBroadcastInstanceId
75      || mLastInstanceId == kSystemInstanceId) {
76    FATAL_ERROR("Exhausted instance IDs!");
77  }
78
79  return mLastInstanceId;
80}
81
82void EventLoopManager::lateInit() {
83  mGnssManager.init();
84  mWifiRequestManager.init();
85  mWwanRequestManager.init();
86
87#ifdef CHRE_AUDIO_SUPPORT_ENABLED
88  mAudioRequestManager.init();
89#endif  // CHRE_AUDIO_SUPPORT_ENABLED
90}
91
92// Explicitly instantiate the EventLoopManagerSingleton to reduce codesize.
93template class Singleton<EventLoopManager>;
94
95}  // namespace chre
96