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/context.h"
20 #include "chre/platform/fatal_error.h"
21 #include "chre/util/lock_guard.h"
22
23 namespace chre {
24
validateChreApiCall(const char * functionName)25 Nanoapp *EventLoopManager::validateChreApiCall(const char *functionName) {
26 chre::EventLoop *eventLoop = getCurrentEventLoop();
27 CHRE_ASSERT(eventLoop);
28
29 chre::Nanoapp *currentNanoapp = eventLoop->getCurrentNanoapp();
30 CHRE_ASSERT_LOG(currentNanoapp, "%s called with no CHRE app context", __func__);
31
32 return currentNanoapp;
33 }
34
createEventLoop()35 EventLoop *EventLoopManager::createEventLoop() {
36 // TODO: The current EventLoop implementation requires refactoring to properly
37 // support multiple EventLoop instances, for example the Event freeing
38 // mechanism is not thread-safe.
39 CHRE_ASSERT(mEventLoops.empty());
40 if (!mEventLoops.emplace_back(MakeUnique<EventLoop>())) {
41 return nullptr;
42 }
43
44 return mEventLoops.back().get();
45 }
46
deferCallback(SystemCallbackType type,void * data,SystemCallbackFunction * callback)47 bool EventLoopManager::deferCallback(SystemCallbackType type, void *data,
48 SystemCallbackFunction *callback) {
49 // TODO: when multiple EventLoops are supported, consider allowing the
50 // platform to define which EventLoop is used to process system callbacks.
51 CHRE_ASSERT(!mEventLoops.empty());
52 return mEventLoops[0]->postEvent(static_cast<uint16_t>(type), data, callback,
53 kSystemInstanceId, kSystemInstanceId);
54 }
55
findNanoappInstanceIdByAppId(uint64_t appId,uint32_t * instanceId,EventLoop ** eventLoop)56 bool EventLoopManager::findNanoappInstanceIdByAppId(
57 uint64_t appId, uint32_t *instanceId, EventLoop **eventLoop) {
58 bool found = false;
59
60 for (size_t i = 0; i < mEventLoops.size(); i++) {
61 if (mEventLoops[i]->findNanoappInstanceIdByAppId(appId, instanceId)) {
62 found = true;
63 if (eventLoop != nullptr) {
64 *eventLoop = mEventLoops[i].get();
65 }
66 break;
67 }
68 }
69
70 return found;
71 }
72
findNanoappByInstanceId(uint32_t instanceId,EventLoop ** eventLoop)73 Nanoapp *EventLoopManager::findNanoappByInstanceId(uint32_t instanceId,
74 EventLoop **eventLoop) {
75 Nanoapp *nanoapp = nullptr;
76 for (size_t i = 0; i < mEventLoops.size(); i++) {
77 nanoapp = mEventLoops[i]->findNanoappByInstanceId(instanceId);
78 if (nanoapp != nullptr) {
79 if (eventLoop != nullptr) {
80 *eventLoop = mEventLoops[i].get();
81 }
82 break;
83 }
84 }
85
86 return nanoapp;
87 }
88
getNextInstanceId()89 uint32_t EventLoopManager::getNextInstanceId() {
90 // TODO: this needs to be an atomic integer when we have > 1 event loop, or
91 // use a mutex
92 ++mLastInstanceId;
93
94 // ~4 billion instance IDs should be enough for anyone... if we need to
95 // support wraparound for stress testing load/unload, then we can set a flag
96 // when wraparound occurs and use EventLoop::findNanoappByInstanceId to ensure
97 // we avoid conflicts
98 if (mLastInstanceId == kBroadcastInstanceId
99 || mLastInstanceId == kSystemInstanceId) {
100 FATAL_ERROR("Exhausted instance IDs!");
101 }
102
103 return mLastInstanceId;
104 }
105
postEvent(uint16_t eventType,void * eventData,chreEventCompleteFunction * freeCallback,uint32_t senderInstanceId,uint32_t targetInstanceId)106 bool EventLoopManager::postEvent(uint16_t eventType, void *eventData,
107 chreEventCompleteFunction *freeCallback,
108 uint32_t senderInstanceId,
109 uint32_t targetInstanceId) {
110 LockGuard<Mutex> lock(mMutex);
111
112 // TODO: for unicast events, ideally we'd just post the event to the EventLoop
113 // that has the target
114 bool success = true;
115 for (size_t i = 0; i < mEventLoops.size(); i++) {
116 success &= mEventLoops[i]->postEvent(eventType, eventData, freeCallback,
117 senderInstanceId, targetInstanceId);
118 }
119
120 return success;
121 }
122
getGnssRequestManager()123 GnssRequestManager& EventLoopManager::getGnssRequestManager() {
124 return mGnssRequestManager;
125 }
126
getHostCommsManager()127 HostCommsManager& EventLoopManager::getHostCommsManager() {
128 return mHostCommsManager;
129 }
130
getSensorRequestManager()131 SensorRequestManager& EventLoopManager::getSensorRequestManager() {
132 return mSensorRequestManager;
133 }
134
getWifiRequestManager()135 WifiRequestManager& EventLoopManager::getWifiRequestManager() {
136 return mWifiRequestManager;
137 }
138
getWwanRequestManager()139 WwanRequestManager& EventLoopManager::getWwanRequestManager() {
140 return mWwanRequestManager;
141 }
142
143 } // namespace chre
144