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 bool WwanRequestManager::logStateToBuffer(char *buffer, size_t *bufferPos,
78 size_t bufferSize) const {
79 bool success = debugDumpPrint(buffer, bufferPos, bufferSize, "\nWWAN:\n");
80 if (mCellInfoRequestingNanoappInstanceId.has_value()) {
81 success &= debugDumpPrint(buffer, bufferPos, bufferSize,
82 " WWAN request pending nanoappId=%" PRIu32 "\n",
83 mCellInfoRequestingNanoappInstanceId.value());
84 }
85 return success;
86 }
87
handleFreeCellInfoResult(chreWwanCellInfoResult * result)88 void WwanRequestManager::handleFreeCellInfoResult(
89 chreWwanCellInfoResult *result) {
90 if (mCellInfoRequestingNanoappInstanceId.has_value()) {
91 Nanoapp *nanoapp = EventLoopManagerSingleton::get()->getEventLoop()
92 .findNanoappByInstanceId(*mCellInfoRequestingNanoappInstanceId);
93 if (nanoapp != nullptr) {
94 nanoapp->unregisterForBroadcastEvent(CHRE_EVENT_WWAN_CELL_INFO_RESULT);
95 } else {
96 LOGE("Freeing cell info for non-existent nanoapp");
97 }
98
99 mCellInfoRequestingNanoappInstanceId.reset();
100 } else {
101 LOGE("Cell info released with no pending request");
102 }
103
104 mPlatformWwan.releaseCellInfoResult(result);
105 }
106
freeCellInfoResultCallback(uint16_t eventType,void * eventData)107 void WwanRequestManager::freeCellInfoResultCallback(uint16_t eventType,
108 void *eventData) {
109 auto *result = static_cast<chreWwanCellInfoResult *>(eventData);
110 EventLoopManagerSingleton::get()->getWwanRequestManager()
111 .handleFreeCellInfoResult(result);
112 }
113
114 } // namespace chre
115