xref: /aosp_15_r20/system/chre/apps/power_test/common/power_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2019 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/util/macros.h"
20 #include "chre_api/chre.h"
21 #include "common.h"
22 #include "generated/chre_power_test_generated.h"
23 #include "include/request_manager.h"
24 #include "request_manager.h"
25 
26 #ifdef CHRE_NANOAPP_INTERNAL
27 namespace chre {
28 namespace {
29 #endif  // CHRE_NANOAPP_INTERNAL
30 
31 using chre::power_test::MessageType;
32 
nanoappStart()33 bool nanoappStart() {
34   LOGI("App started on platform ID %" PRIx64, chreGetPlatformId());
35 
36   RequestManagerSingleton::init();
37 
38   return true;
39 }
40 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)41 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
42                         const void *eventData) {
43   UNUSED_VAR(senderInstanceId);
44 
45   switch (eventType) {
46     case CHRE_EVENT_MESSAGE_FROM_HOST: {
47       auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
48       RequestManagerSingleton::get()->handleMessageFromHost(*msg);
49       break;
50     }
51     case CHRE_EVENT_TIMER:
52       RequestManagerSingleton::get()->handleTimerEvent(eventData);
53       break;
54     case CHRE_EVENT_WIFI_NAN_IDENTIFIER_RESULT: {
55       auto *event =
56           static_cast<const struct chreWifiNanIdentifierEvent *>(eventData);
57       RequestManagerSingleton::get()->handleNanIdResult(event);
58       break;
59     }
60     case CHRE_EVENT_WIFI_NAN_DISCOVERY_RESULT: {
61       auto *event =
62           static_cast<const struct chreWifiNanDiscoveryEvent *>(eventData);
63       LOGD("NAN discovery subId %" PRIu32 " pubId %" PRIu32, event->subscribeId,
64            event->publishId);
65       RequestManagerSingleton::get()->requestNanRanging(event);
66       break;
67     }
68     case CHRE_EVENT_WIFI_NAN_SESSION_LOST: {
69       auto *event =
70           static_cast<const struct chreWifiNanSessionLostEvent *>(eventData);
71       LOGD("NAN lost session ID %" PRIu32 " peer ID %" PRIu32, event->id,
72            event->peerId);
73       break;
74     }
75     case CHRE_EVENT_WIFI_NAN_SESSION_TERMINATED: {
76       auto *event =
77           static_cast<const struct chreWifiNanSessionTerminatedEvent *>(
78               eventData);
79       LOGD("NAN session ID %" PRIu32 " terminated due to %d", event->id,
80            event->reason);
81       break;
82     }
83     case CHRE_EVENT_WIFI_ASYNC_RESULT: {
84       const struct chreAsyncResult *event =
85           static_cast<const struct chreAsyncResult *>(eventData);
86       LOGD("Wifi async result type %" PRIu8 " success %d error %" PRIu8,
87            event->requestType, event->success, event->errorCode);
88       break;
89     }
90     case CHRE_EVENT_WIFI_SCAN_RESULT: {
91       const struct chreWifiScanEvent *event =
92           static_cast<const struct chreWifiScanEvent *>(eventData);
93       LOGD("Wifi scan received with %" PRIu8 " results, scanType %" PRIu8
94            ", radioChainPref %" PRIu8,
95            event->resultCount, event->scanType, event->radioChainPref);
96       break;
97     }
98     case CHRE_EVENT_WIFI_RANGING_RESULT: {
99       auto *event = static_cast<const struct chreWifiRangingEvent *>(eventData);
100       LOGD("Wifi ranging result received with %" PRIu8 " results",
101            event->resultCount);
102       for (uint8_t i = 0; i < event->resultCount; ++i) {
103         LOGD("Ranging result #%" PRIu8 " status %" PRIu8 " rssi %" PRId8
104              " distance (mm) %" PRIu32,
105              i, event->results[i].status, event->results[i].rssi,
106              event->results[i].distance);
107       }
108       break;
109     }
110     case CHRE_EVENT_GNSS_ASYNC_RESULT: {
111       const struct chreAsyncResult *event =
112           static_cast<const struct chreAsyncResult *>(eventData);
113       LOGD("GNSS async result type %" PRIu8 " success %d error %" PRIu8,
114            event->requestType, event->success, event->errorCode);
115       break;
116     }
117     case CHRE_EVENT_GNSS_LOCATION:
118       LOGD("GNSS location received");
119       break;
120     case CHRE_EVENT_GNSS_DATA:
121       LOGD("GNSS measurement received");
122       break;
123     case CHRE_EVENT_WWAN_CELL_INFO_RESULT:
124       LOGD("Cell info received");
125       break;
126     case CHRE_EVENT_SENSOR_SAMPLING_CHANGE: {
127       const struct chreSensorSamplingStatusEvent *event =
128           static_cast<const struct chreSensorSamplingStatusEvent *>(eventData);
129       LOGD("Sensor sampling status change handle %" PRIu32
130            " enabled %d interval %" PRIu64 " latency %" PRIu64,
131            event->sensorHandle, event->status.enabled, event->status.interval,
132            event->status.latency);
133       break;
134     }
135     case CHRE_EVENT_AUDIO_DATA: {
136       const struct chreAudioDataEvent *event =
137           static_cast<const struct chreAudioDataEvent *>(eventData);
138       LOGD("Audio data received with %" PRIu32 " samples", event->sampleCount);
139       break;
140     }
141     case CHRE_EVENT_AUDIO_SAMPLING_CHANGE: {
142       const struct chreAudioSourceStatusEvent *event =
143           static_cast<const struct chreAudioSourceStatusEvent *>(eventData);
144       LOGD("Audio sampling status event for handle %" PRIu32 ", suspended: %d",
145            event->handle, event->status.suspended);
146       break;
147     }
148     default:
149       // TODO: Make this log less as sensor events will spam the logcat if debug
150       // logging is enabled.
151       LOGV("Received event type %" PRIu16, eventType);
152   }
153 }
154 
nanoappEnd()155 void nanoappEnd() {
156   RequestManagerSingleton::deinit();
157   LOGI("Stopped");
158 }
159 
160 #ifdef CHRE_NANOAPP_INTERNAL
161 }  // anonymous namespace
162 }  // namespace chre
163 
164 #include "chre/platform/static_nanoapp_init.h"
165 #include "chre/util/nanoapp/app_id.h"
166 #include "chre/util/system/napp_permissions.h"
167 
168 using chre::NanoappPermissions;
169 
170 CHRE_STATIC_NANOAPP_INIT(PowerTest, chre::kPowerTestAppId, 0,
171                          NanoappPermissions::CHRE_PERMS_AUDIO |
172                              NanoappPermissions::CHRE_PERMS_GNSS |
173                              NanoappPermissions::CHRE_PERMS_WIFI |
174                              NanoappPermissions::CHRE_PERMS_WWAN)
175 #endif  // CHRE_NANOAPP_INTERNAL
176