1 /*
2 * Copyright (C) 2017 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/wwan_request_manager.h"
18
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/platform/fatal_error.h"
21 #include "chre/platform/log.h"
22 #include "chre/util/system/debug_dump.h"
23
24 namespace chre {
25
init()26 void WwanRequestManager::init() {
27 return mPlatformWwan.init();
28 }
29
getCapabilities()30 uint32_t WwanRequestManager::getCapabilities() {
31 return mPlatformWwan.getCapabilities();
32 }
33
requestCellInfo(Nanoapp * nanoapp,const void * cookie)34 bool WwanRequestManager::requestCellInfo(Nanoapp *nanoapp,
35 const void *cookie) {
36 CHRE_ASSERT(nanoapp);
37
38 bool success = false;
39 if (!mCellInfoRequestingNanoappInstanceId.has_value()) {
40 success = mPlatformWwan.requestCellInfo();
41 if (success) {
42 nanoapp->registerForBroadcastEvent(CHRE_EVENT_WWAN_CELL_INFO_RESULT);
43 mCellInfoRequestingNanoappInstanceId = nanoapp->getInstanceId();
44 mCellInfoRequestingNanoappCookie = cookie;
45 }
46 } else {
47 LOGE("Cell info request made while a request is in flight");
48 }
49
50 return success;
51 }
52
handleCellInfoResult(chreWwanCellInfoResult * result)53 void WwanRequestManager::handleCellInfoResult(chreWwanCellInfoResult *result) {
54 auto callback = [](uint16_t /* eventType */, void *eventData) {
55 auto *cellInfoResult = static_cast<chreWwanCellInfoResult *>(eventData);
56 EventLoopManagerSingleton::get()->getWwanRequestManager()
57 .handleCellInfoResultSync(cellInfoResult);
58 };
59
60 EventLoopManagerSingleton::get()->deferCallback(
61 SystemCallbackType::WwanHandleCellInfoResult, result, callback);
62 }
63
handleCellInfoResultSync(chreWwanCellInfoResult * result)64 void WwanRequestManager::handleCellInfoResultSync(
65 chreWwanCellInfoResult *result) {
66 if (mCellInfoRequestingNanoappInstanceId.has_value()) {
67 result->cookie = mCellInfoRequestingNanoappCookie;
68 EventLoopManagerSingleton::get()->getEventLoop()
69 .postEvent(CHRE_EVENT_WWAN_CELL_INFO_RESULT, result,
70 freeCellInfoResultCallback, kSystemInstanceId,
71 mCellInfoRequestingNanoappInstanceId.value());
72 } else {
73 LOGE("Cell info results received unexpectedly");
74 }
75 }
76
logStateToBuffer(char * buffer,size_t * bufferPos,size_t bufferSize) const77 void WwanRequestManager::logStateToBuffer(char *buffer, size_t *bufferPos,
78 size_t bufferSize) const {
79 debugDumpPrint(buffer, bufferPos, bufferSize, "\nWWAN:\n");
80 if (mCellInfoRequestingNanoappInstanceId.has_value()) {
81 debugDumpPrint(buffer, bufferPos, bufferSize,
82 " WWAN request pending nanoappId=%" PRIu32 "\n",
83 mCellInfoRequestingNanoappInstanceId.value());
84 }
85 }
86
handleFreeCellInfoResult(chreWwanCellInfoResult * result)87 void WwanRequestManager::handleFreeCellInfoResult(
88 chreWwanCellInfoResult *result) {
89 if (mCellInfoRequestingNanoappInstanceId.has_value()) {
90 Nanoapp *nanoapp = EventLoopManagerSingleton::get()->getEventLoop()
91 .findNanoappByInstanceId(*mCellInfoRequestingNanoappInstanceId);
92 if (nanoapp != nullptr) {
93 nanoapp->unregisterForBroadcastEvent(CHRE_EVENT_WWAN_CELL_INFO_RESULT);
94 } else {
95 LOGE("Freeing cell info for non-existent nanoapp");
96 }
97
98 mCellInfoRequestingNanoappInstanceId.reset();
99 } else {
100 LOGE("Cell info released with no pending request");
101 }
102
103 mPlatformWwan.releaseCellInfoResult(result);
104 }
105
freeCellInfoResultCallback(uint16_t eventType,void * eventData)106 void WwanRequestManager::freeCellInfoResultCallback(uint16_t eventType,
107 void *eventData) {
108 auto *result = static_cast<chreWwanCellInfoResult *>(eventData);
109 EventLoopManagerSingleton::get()->getWwanRequestManager()
110 .handleFreeCellInfoResult(result);
111 }
112
113 } // namespace chre
114