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, const void *cookie) {
35   CHRE_ASSERT(nanoapp);
36 
37   bool success = false;
38   if (!mCellInfoRequestingNanoappInstanceId.has_value()) {
39     success = mPlatformWwan.requestCellInfo();
40     if (success) {
41       nanoapp->registerForBroadcastEvent(CHRE_EVENT_WWAN_CELL_INFO_RESULT);
42       mCellInfoRequestingNanoappInstanceId = nanoapp->getInstanceId();
43       mCellInfoRequestingNanoappCookie = cookie;
44     }
45   } else {
46     LOGE("Cell info request made while a request is in flight");
47   }
48 
49   return success;
50 }
51 
handleCellInfoResult(struct chreWwanCellInfoResult * result)52 void WwanRequestManager::handleCellInfoResult(
53     struct chreWwanCellInfoResult *result) {
54   auto callback = [](uint16_t /*type*/, void *data, void * /*extraData*/) {
55     auto *cellInfoResult = static_cast<chreWwanCellInfoResult *>(data);
56     EventLoopManagerSingleton::get()
57         ->getWwanRequestManager()
58         .handleCellInfoResultSync(cellInfoResult);
59   };
60 
61   EventLoopManagerSingleton::get()->deferCallback(
62       SystemCallbackType::WwanHandleCellInfoResult, result, callback);
63 }
64 
handleCellInfoResultSync(chreWwanCellInfoResult * result)65 void WwanRequestManager::handleCellInfoResultSync(
66     chreWwanCellInfoResult *result) {
67   if (mCellInfoRequestingNanoappInstanceId.has_value()) {
68     result->cookie = mCellInfoRequestingNanoappCookie;
69     EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
70         CHRE_EVENT_WWAN_CELL_INFO_RESULT, result, freeCellInfoResultCallback,
71         mCellInfoRequestingNanoappInstanceId.value());
72   } else {
73     LOGE("Cell info results received unexpectedly");
74   }
75 }
76 
logStateToBuffer(DebugDumpWrapper & debugDump) const77 void WwanRequestManager::logStateToBuffer(DebugDumpWrapper &debugDump) const {
78   debugDump.print("\nWWAN:\n");
79   if (mCellInfoRequestingNanoappInstanceId.has_value()) {
80     debugDump.print(" WWAN request pending nanoappId=%" PRIu32 "\n",
81                     mCellInfoRequestingNanoappInstanceId.value());
82   }
83 }
84 
handleFreeCellInfoResult(chreWwanCellInfoResult * result)85 void WwanRequestManager::handleFreeCellInfoResult(
86     chreWwanCellInfoResult *result) {
87   if (mCellInfoRequestingNanoappInstanceId.has_value()) {
88     Nanoapp *nanoapp =
89         EventLoopManagerSingleton::get()
90             ->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()
110       ->getWwanRequestManager()
111       .handleFreeCellInfoResult(result);
112 }
113 
114 }  // namespace chre
115