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/core/event_loop_common.h" 20 #include "chre/event.h" 21 #include "chre/platform/atomic.h" 22 #include "chre/platform/fatal_error.h" 23 #include "chre/platform/memory.h" 24 #include "chre/util/lock_guard.h" 25 26 namespace chre { 27 validateChreApiCall(const char * functionName)28Nanoapp *EventLoopManager::validateChreApiCall(const char *functionName) { 29 chre::Nanoapp *currentNanoapp = 30 EventLoopManagerSingleton::get()->getEventLoop().getCurrentNanoapp(); 31 CHRE_ASSERT_LOG(currentNanoapp, "%s called with no CHRE app context", 32 functionName); 33 return currentNanoapp; 34 } 35 getNextInstanceId()36uint16_t EventLoopManager::getNextInstanceId() { 37 // Get the next available instance ID and mask off the upper 16 bit. 38 uint16_t instanceId = 39 static_cast<uint16_t>(mNextInstanceId.fetch_increment() & 0x0000FFFF); 40 41 // 65536 instance IDs should be enough for normal use cases. If we need to 42 // support wraparound for stress testing load/unload, then we can set a flag 43 // when wraparound occurs and use EventLoop::findNanoappByInstanceId to ensure 44 // we avoid conflicts 45 if (instanceId == kBroadcastInstanceId || instanceId == kSystemInstanceId) { 46 FATAL_ERROR("Exhausted instance IDs!"); 47 } 48 49 return instanceId; 50 } 51 lateInit()52void EventLoopManager::lateInit() { 53 #ifdef CHRE_SENSORS_SUPPORT_ENABLED 54 mSensorRequestManager.init(); 55 #endif // CHRE_SENSORS_SUPPORT_ENABLED 56 57 #ifdef CHRE_GNSS_SUPPORT_ENABLED 58 mGnssManager.init(); 59 #endif // CHRE_GNSS_SUPPORT_ENABLED 60 61 #ifdef CHRE_WIFI_SUPPORT_ENABLED 62 mWifiRequestManager.init(); 63 #endif // CHRE_WIFI_SUPPORT_ENABLED 64 65 #ifdef CHRE_WWAN_SUPPORT_ENABLED 66 mWwanRequestManager.init(); 67 #endif // CHRE_WWAN_SUPPORT_ENABLED 68 69 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 70 mAudioRequestManager.init(); 71 #endif // CHRE_AUDIO_SUPPORT_ENABLED 72 73 #ifdef CHRE_BLE_SUPPORT_ENABLED 74 mBleRequestManager.init(); 75 #endif // CHRE_BLE_SUPPORT_ENABLED 76 77 #ifdef CHRE_MESSAGE_ROUTER_SUPPORT_ENABLED 78 mChreMessageHubManager.init(); 79 #endif // CHRE_MESSAGE_ROUTER_SUPPORT_ENABLED 80 } 81 82 // Explicitly instantiate the EventLoopManagerSingleton to reduce codesize. 83 template class Singleton<EventLoopManager>; 84 85 } // namespace chre 86