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 
freeEventDataCallback(uint16_t,void * eventData)25 void freeEventDataCallback(uint16_t /*eventType*/, void *eventData) {
26   memoryFree(eventData);
27 }
28 
validateChreApiCall(const char * functionName)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 
debugDump()37 UniquePtr<char> EventLoopManager::debugDump() {
38   constexpr size_t kDebugStringSize = 4096;
39   char *debugStr = static_cast<char *>(memoryAlloc(kDebugStringSize));
40   if (debugStr == nullptr) {
41     LOG_OOM();
42   } else {
43     size_t debugStrPos = 0;
44     mMemoryManager.logStateToBuffer(debugStr, &debugStrPos, kDebugStringSize);
45     mEventLoop.logStateToBuffer(debugStr, &debugStrPos, kDebugStringSize);
46     mSensorRequestManager.logStateToBuffer(debugStr, &debugStrPos,
47                                            kDebugStringSize);
48 #ifdef CHRE_GNSS_SUPPORT_ENABLED
49     mGnssManager.logStateToBuffer(debugStr, &debugStrPos, kDebugStringSize);
50 #endif  // CHRE_GNSS_SUPPORT_ENABLED
51 #ifdef CHRE_WIFI_SUPPORT_ENABLED
52     mWifiRequestManager.logStateToBuffer(debugStr, &debugStrPos,
53                                          kDebugStringSize);
54 #endif  // CHRE_WIFI_SUPPORT_ENABLED
55 #ifdef CHRE_WWAN_SUPPORT_ENABLED
56     mWwanRequestManager.logStateToBuffer(debugStr, &debugStrPos,
57                                          kDebugStringSize);
58 #endif  // CHRE_WWAN_SUPPORT_ENABLED
59 #ifdef CHRE_AUDIO_SUPPORT_ENABLED
60     mAudioRequestManager.logStateToBuffer(debugStr, &debugStrPos,
61                                           kDebugStringSize);
62 #endif  // CHRE_AUDIO_SUPPORT_ENABLED
63 
64     LOGD("Debug dump used %zu bytes of log buffer", debugStrPos);
65   }
66 
67   return UniquePtr<char>(debugStr);
68 }
69 
getNextInstanceId()70 uint32_t EventLoopManager::getNextInstanceId() {
71   ++mLastInstanceId;
72 
73   // ~4 billion instance IDs should be enough for anyone... if we need to
74   // support wraparound for stress testing load/unload, then we can set a flag
75   // when wraparound occurs and use EventLoop::findNanoappByInstanceId to ensure
76   // we avoid conflicts
77   if (mLastInstanceId == kBroadcastInstanceId
78       || mLastInstanceId == kSystemInstanceId) {
79     FATAL_ERROR("Exhausted instance IDs!");
80   }
81 
82   return mLastInstanceId;
83 }
84 
lateInit()85 void EventLoopManager::lateInit() {
86   mSensorRequestManager.init();
87 
88 #ifdef CHRE_GNSS_SUPPORT_ENABLED
89   mGnssManager.init();
90 #endif  // CHRE_GNSS_SUPPORT_ENABLED
91 
92 #ifdef CHRE_WIFI_SUPPORT_ENABLED
93   mWifiRequestManager.init();
94 #endif  // CHRE_WIFI_SUPPORT_ENABLED
95 
96 #ifdef CHRE_WWAN_SUPPORT_ENABLED
97   mWwanRequestManager.init();
98 #endif  // CHRE_WWAN_SUPPORT_ENABLED
99 
100 #ifdef CHRE_AUDIO_SUPPORT_ENABLED
101   mAudioRequestManager.init();
102 #endif  // CHRE_AUDIO_SUPPORT_ENABLED
103 }
104 
105 // Explicitly instantiate the EventLoopManagerSingleton to reduce codesize.
106 template class Singleton<EventLoopManager>;
107 
108 }  // namespace chre
109