1 /*
2  * Copyright (C) 2020 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.h>
20 
21 #include "chre/util/nanoapp/log.h"
22 #include "chre/util/time.h"
23 
24 #define LOG_TAG "[DebugDumpWorld]"
25 
26 /**
27  * A nanoapp that log debug data on receiving CHRE_EVENT_DEBUG_DUMP.
28  */
29 
30 #ifdef CHRE_NANOAPP_INTERNAL
31 namespace chre {
32 namespace {
33 #endif  // CHRE_NANOAPP_INTERNAL
34 
35 namespace {
36 
37 uint32_t gEventCount = 0;
38 uint64_t gDwellTimeNs = 0;
39 
40 }  // namespace
41 
nanoappStart()42 bool nanoappStart() {
43   LOGI("Debug dump world start");
44   chreConfigureDebugDumpEvent(true /* enable */);
45   return true;
46 }
47 
nanoappEnd()48 void nanoappEnd() {
49   LOGI("Debug dump world end");
50 
51   // No need to disable debug dump event delivery since nanoapps can't receive
52   // events after nanoappEnd anyway.
53 }
54 
handleDebugDumpEvent()55 void handleDebugDumpEvent() {
56   // CHRE adds the nanoapp name / ID to the debug dump automatically.
57   chreDebugDumpLog("  Debug event count: %" PRIu32 "\n", ++gEventCount);
58   chreDebugDumpLog("  Total dwell time: %" PRIu64 " us\n",
59                    gDwellTimeNs / chre::kOneMicrosecondInNanoseconds);
60 }
61 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)62 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
63                         const void *eventData) {
64   uint64_t tic = chreGetTime();
65   switch (eventType) {
66     case CHRE_EVENT_DEBUG_DUMP:
67       LOGI("Receiving debug dump event");
68       handleDebugDumpEvent();
69       break;
70     default:
71       LOGW("Unknown event type %" PRIu16 " received from sender %" PRIu32,
72            eventType, senderInstanceId);
73       break;
74   }
75   gDwellTimeNs += chreGetTime() - tic;
76 }
77 
78 #ifdef CHRE_NANOAPP_INTERNAL
79 }  // anonymous namespace
80 }  // namespace chre
81 
82 #include "chre/platform/static_nanoapp_init.h"
83 #include "chre/util/nanoapp/app_id.h"
84 #include "chre/util/system/napp_permissions.h"
85 
86 CHRE_STATIC_NANOAPP_INIT(DebugDumpWorld, chre::kDebugDumpWorldAppId, 0,
87                          chre::NanoappPermissions::CHRE_PERMS_NONE);
88 #endif  // CHRE_NANOAPP_INTERNAL
89