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