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