unload_tester.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <cinttypes>
  17. #include "chre_api/chre.h"
  18. #include "chre/core/event_loop.h"
  19. #include "chre/core/event_loop_manager.h"
  20. #include "chre/platform/assert.h"
  21. #include "chre/platform/log.h"
  22. #include "chre/platform/static_nanoapp_init.h"
  23. #include "chre/util/nanoapp/app_id.h"
  24. #include "chre/util/time.h"
  25. /**
  26. * @file
  27. * A nanoapp exclusively for testing, which unloads the spammer nanoapp after a
  28. * short delay. Must only be compiled as a static/internal nanoapp.
  29. */
  30. namespace chre {
  31. namespace {
  32. constexpr uint32_t kAppVersion = 99;
  33. void handleUnload(uint16_t /* eventType */, void * /* data */) {
  34. EventLoop& eventLoop = EventLoopManagerSingleton::get()->getEventLoop();
  35. uint32_t instanceId;
  36. LOGD("About to unload spammer nanoapp");
  37. if (!eventLoop.findNanoappInstanceIdByAppId(kSpammerAppId, &instanceId)) {
  38. LOGE("Couldn't unload nanoapp: not found");
  39. } else if (!eventLoop.unloadNanoapp(instanceId, true)) {
  40. LOGE("Failed to unload nanoapp");
  41. }
  42. }
  43. bool nanoappStart() {
  44. LOGI("Unload tester started as instance %" PRIu32, chreGetInstanceId());
  45. constexpr uint64_t kTimerDuration = Seconds(2).toRawNanoseconds();
  46. uint32_t timerHandle = chreTimerSet(kTimerDuration,
  47. nullptr, true /* oneShot */);
  48. CHRE_ASSERT_LOG(timerHandle != CHRE_TIMER_INVALID, "Couldn't start timer!");
  49. bool eventSent = chreSendEvent(CHRE_EVENT_FIRST_USER_VALUE, nullptr, nullptr,
  50. chreGetInstanceId());
  51. CHRE_ASSERT_LOG(eventSent, "Couldn't send event to self!");
  52. struct chreNanoappInfo info;
  53. bool gotInfo = chreGetNanoappInfoByInstanceId(chreGetInstanceId(), &info);
  54. CHRE_ASSERT(gotInfo);
  55. CHRE_ASSERT(info.appId == chreGetAppId());
  56. CHRE_ASSERT(info.appId == kUnloadTesterAppId);
  57. CHRE_ASSERT(info.version == kAppVersion);
  58. CHRE_ASSERT(info.instanceId == chreGetInstanceId());
  59. chreConfigureNanoappInfoEvents(true);
  60. return true;
  61. }
  62. void nanoappHandleEvent(uint32_t senderInstanceId,
  63. uint16_t eventType,
  64. const void *eventData) {
  65. if (eventType == CHRE_EVENT_FIRST_USER_VALUE
  66. && senderInstanceId == chreGetInstanceId()) {
  67. struct chreNanoappInfo info;
  68. if (!chreGetNanoappInfoByAppId(kSpammerAppId, &info)) {
  69. LOGW("Couldn't get spammer's app info - not running?");
  70. }
  71. } else if (eventType == CHRE_EVENT_TIMER) {
  72. // We can't do the unload from the context of another nanoapp's handle
  73. // event callback, so get into the system context
  74. EventLoopManagerSingleton::get()->deferCallback(
  75. SystemCallbackType::HandleUnloadNanoapp, nullptr, handleUnload);
  76. } else if (eventType == CHRE_EVENT_NANOAPP_STARTED
  77. || eventType == CHRE_EVENT_NANOAPP_STOPPED) {
  78. const auto *info = static_cast<const chreNanoappInfo *>(eventData);
  79. if (info->appId == kSpammerAppId) {
  80. LOGD("Received %s event for spammer instance %" PRIu32,
  81. (eventType == CHRE_EVENT_NANOAPP_STARTED) ? "start" : "stop",
  82. info->instanceId);
  83. }
  84. }
  85. }
  86. void nanoappEnd() {}
  87. } // anonymous namespace
  88. } // namespace chre
  89. CHRE_STATIC_NANOAPP_INIT(UnloadTester, chre::kUnloadTesterAppId, kAppVersion);