nanoapp.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2016 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/nanoapp.h"
  17. #include "chre/core/event_loop_manager.h"
  18. #include "chre/platform/assert.h"
  19. #include "chre/platform/fatal_error.h"
  20. #include "chre/platform/log.h"
  21. #include "chre/util/system/debug_dump.h"
  22. namespace chre {
  23. Nanoapp::~Nanoapp() {
  24. CHRE_ASSERT_LOG(getTotalAllocatedBytes() == 0,
  25. "Nanoapp ID=0x%016" PRIx64 " still has %zu allocated bytes!", getAppId(),
  26. getTotalAllocatedBytes());
  27. }
  28. bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType) const {
  29. return (mRegisteredEvents.find(eventType) != mRegisteredEvents.size());
  30. }
  31. bool Nanoapp::registerForBroadcastEvent(uint16_t eventId) {
  32. if (isRegisteredForBroadcastEvent(eventId)) {
  33. return false;
  34. }
  35. if (!mRegisteredEvents.push_back(eventId)) {
  36. FATAL_ERROR_OOM();
  37. }
  38. return true;
  39. }
  40. bool Nanoapp::unregisterForBroadcastEvent(uint16_t eventId) {
  41. size_t registeredEventIndex = mRegisteredEvents.find(eventId);
  42. if (registeredEventIndex == mRegisteredEvents.size()) {
  43. return false;
  44. }
  45. mRegisteredEvents.erase(registeredEventIndex);
  46. return true;
  47. }
  48. void Nanoapp::configureNanoappInfoEvents(bool enable) {
  49. if (enable) {
  50. registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
  51. registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
  52. } else {
  53. unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
  54. unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
  55. }
  56. }
  57. void Nanoapp::configureHostSleepEvents(bool enable) {
  58. if (enable) {
  59. registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
  60. registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
  61. } else {
  62. unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
  63. unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
  64. }
  65. }
  66. Event *Nanoapp::processNextEvent() {
  67. Event *event = mEventQueue.pop();
  68. CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty");
  69. if (event != nullptr) {
  70. handleEvent(event->senderInstanceId, event->eventType, event->eventData);
  71. }
  72. return event;
  73. }
  74. void Nanoapp::logStateToBuffer(char *buffer, size_t *bufferPos,
  75. size_t bufferSize) const {
  76. PlatformNanoapp::logStateToBuffer(buffer, bufferPos, bufferSize);
  77. debugDumpPrint(
  78. buffer, bufferPos, bufferSize,
  79. " Id=%" PRIu32 " AppId=0x%016" PRIx64
  80. " ver=0x%" PRIx32 " targetAPI=0x%" PRIx32
  81. " currentAllocatedBytes=%zu peakAllocatedBytes=%zu\n",
  82. getInstanceId(), getAppId(), getAppVersion(), getTargetApiVersion(),
  83. getTotalAllocatedBytes(), getPeakAllocatedBytes());
  84. }
  85. } // namespace chre