wwan_request_manager.cc 3.9 KB

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