From 619a86ae590be43929cffa3d534b70f4d947f935 Mon Sep 17 00:00:00 2001 From: Benjamin Schwartz Date: Wed, 16 Dec 2020 15:05:06 -0800 Subject: [PATCH] BatteryStatsService: Allow unknown wakeup string Also clean up wakeup reason concatenation code. Cannot find a reason for there to be restrictions on the wakeup reason strings that this service is enforcing. It is the job of the Suspend Control Service to ensure valid wakeup reasons are supplied. Bug: 174106197 Test: Generated a bugreport and viewed wakeups in historian Change-Id: I4cc79bec0e1face40d51e35dec1d443fa1165959 --- ..._android_server_am_BatteryStatsService.cpp | 66 ++----------------- 1 file changed, 7 insertions(+), 59 deletions(-) diff --git a/services/core/jni/com_android_server_am_BatteryStatsService.cpp b/services/core/jni/com_android_server_am_BatteryStatsService.cpp index 00342866aa301..f076ca9afbea9 100644 --- a/services/core/jni/com_android_server_am_BatteryStatsService.cpp +++ b/services/core/jni/com_android_server_am_BatteryStatsService.cpp @@ -46,6 +46,8 @@ #include #include +#include + using android::hardware::hidl_vec; using android::hardware::Return; using android::hardware::Void; @@ -200,67 +202,13 @@ static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobject outBuf) return 0; } - char* mergedreasonpos = mergedreason; - int i = 0; - for (auto wakeupReason : wakeupReasons) { - auto reasonline = const_cast(wakeupReason.c_str()); - char* pos = reasonline; - char* endPos; - int len; - // First field is the index or 'Abort'. - int irq = (int)strtol(pos, &endPos, 10); - if (pos != endPos) { - // Write the irq number to the merged reason string. - len = snprintf(mergedreasonpos, remainreasonlen, i == 0 ? "%d" : ":%d", irq); - } else { - // The first field is not an irq, it may be the word Abort. - const size_t abortPrefixLen = strlen("Abort:"); - if (strncmp(pos, "Abort:", abortPrefixLen) != 0) { - // Ooops. - ALOGE("Bad reason line: %s", reasonline); - continue; - } + std::string mergedReasonStr = ::android::base::Join(wakeupReasons, ":"); + strncpy(mergedreason, mergedReasonStr.c_str(), remainreasonlen); + mergedreason[remainreasonlen - 1] = '\0'; - // Write 'Abort' to the merged reason string. - len = snprintf(mergedreasonpos, remainreasonlen, i == 0 ? "Abort" : ":Abort"); - endPos = pos + abortPrefixLen; - } - pos = endPos; + ALOGV("Got %d reasons", (int)wakeupReasons.size()); - if (len >= 0 && len < remainreasonlen) { - mergedreasonpos += len; - remainreasonlen -= len; - } - - // Skip whitespace; rest of the buffer is the reason string. - while (*pos == ' ') { - pos++; - } - - // Chop newline at end. - char* endpos = pos; - while (*endpos != 0) { - if (*endpos == '\n') { - *endpos = 0; - break; - } - endpos++; - } - - len = snprintf(mergedreasonpos, remainreasonlen, ":%s", pos); - if (len >= 0 && len < remainreasonlen) { - mergedreasonpos += len; - remainreasonlen -= len; - } - i++; - } - - ALOGV("Got %d reasons", i); - if (i > 0) { - *mergedreasonpos = 0; - } - - return mergedreasonpos - mergedreason; + return strlen(mergedreason); } // The caller must be holding gPowerHalMutex.