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