diff options
author | Kweku Adams <kwekua@google.com> | 2017-12-27 15:48:14 -0800 |
---|---|---|
committer | Kweku Adams <kwekua@google.com> | 2017-12-27 15:48:14 -0800 |
commit | 59e6fd792deb31512027548342b66af25056e237 (patch) | |
tree | 10de89905f5d49ca9354c492d35b22e10636e1ba | |
parent | cb37da8b13a8f70942d253ac56a9ad3ae3cbf7ba (diff) |
incidentd: Parsing battery_type data to proto.
Bug: 65750835
Test: atest incident_helper_test
Change-Id: I3afefcfbd68924f8b6bd384cf1c7a41daeb243e3
-rw-r--r-- | Android.bp | 1 | ||||
-rw-r--r-- | cmds/incident_helper/src/main.cpp | 3 | ||||
-rw-r--r-- | cmds/incident_helper/src/parsers/BatteryTypeParser.cpp | 60 | ||||
-rw-r--r-- | cmds/incident_helper/src/parsers/BatteryTypeParser.h | 36 | ||||
-rw-r--r-- | cmds/incident_helper/testdata/batterytype.txt | 1 | ||||
-rw-r--r-- | cmds/incident_helper/tests/BatteryTypeParser_test.cpp | 66 | ||||
-rw-r--r-- | core/proto/android/os/batterytype.proto | 25 | ||||
-rw-r--r-- | core/proto/android/os/incident.proto | 6 |
8 files changed, 198 insertions, 0 deletions
diff --git a/Android.bp b/Android.bp index 2dcbc92974a9..802ca3fc4335 100644 --- a/Android.bp +++ b/Android.bp @@ -721,6 +721,7 @@ gensrcs { ], srcs: [ + "core/proto/android/os/batterytype.proto", "core/proto/android/os/cpufreq.proto", "core/proto/android/os/cpuinfo.proto", "core/proto/android/os/kernelwake.proto", diff --git a/cmds/incident_helper/src/main.cpp b/cmds/incident_helper/src/main.cpp index c8a0883d493c..ab92473b8ba3 100644 --- a/cmds/incident_helper/src/main.cpp +++ b/cmds/incident_helper/src/main.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "incident_helper" +#include "parsers/BatteryTypeParser.h" #include "parsers/CpuFreqParser.h" #include "parsers/CpuInfoParser.h" #include "parsers/KernelWakesParser.h" @@ -63,6 +64,8 @@ static TextParserBase* selectParser(int section) { return new CpuInfoParser(); case 2004: return new CpuFreqParser(); + case 2006: + return new BatteryTypeParser(); default: return NULL; } diff --git a/cmds/incident_helper/src/parsers/BatteryTypeParser.cpp b/cmds/incident_helper/src/parsers/BatteryTypeParser.cpp new file mode 100644 index 000000000000..ced6cf807e0d --- /dev/null +++ b/cmds/incident_helper/src/parsers/BatteryTypeParser.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "incident_helper" + +#include <android/util/ProtoOutputStream.h> + +#include "frameworks/base/core/proto/android/os/batterytype.proto.h" +#include "ih_util.h" +#include "BatteryTypeParser.h" + +using namespace android::os; + +status_t +BatteryTypeParser::Parse(const int in, const int out) const +{ + Reader reader(in); + string line; + bool readLine = false; + + ProtoOutputStream proto; + + // parse line by line + while (reader.readLine(&line)) { + if (line.empty()) continue; + + if (readLine) { + fprintf(stderr, "Multiple lines in file. Unsure what to do.\n"); + break; + } + + proto.write(BatteryTypeProto::TYPE, line); + + readLine = true; + } + + if (!reader.ok(&line)) { + fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); + return -1; + } + + if (!proto.flush(out)) { + fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); + return -1; + } + fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.string(), proto.size()); + return NO_ERROR; +} diff --git a/cmds/incident_helper/src/parsers/BatteryTypeParser.h b/cmds/incident_helper/src/parsers/BatteryTypeParser.h new file mode 100644 index 000000000000..ac0c098965d3 --- /dev/null +++ b/cmds/incident_helper/src/parsers/BatteryTypeParser.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BATTERY_TYPE_PARSER_H +#define BATTERY_TYPE_PARSER_H + +#include "TextParserBase.h" + +using namespace android; + +/** + * Battery type parser, parses text in file + * /sys/class/power_supply/bms/battery_type. + */ +class BatteryTypeParser : public TextParserBase { +public: + BatteryTypeParser() : TextParserBase(String8("BatteryTypeParser")) {}; + ~BatteryTypeParser() {}; + + virtual status_t Parse(const int in, const int out) const; +}; + +#endif // BATTERY_TYPE_PARSER_H diff --git a/cmds/incident_helper/testdata/batterytype.txt b/cmds/incident_helper/testdata/batterytype.txt new file mode 100644 index 000000000000..c763d36ad811 --- /dev/null +++ b/cmds/incident_helper/testdata/batterytype.txt @@ -0,0 +1 @@ +random_battery_type_string diff --git a/cmds/incident_helper/tests/BatteryTypeParser_test.cpp b/cmds/incident_helper/tests/BatteryTypeParser_test.cpp new file mode 100644 index 000000000000..7fbe22df4b0a --- /dev/null +++ b/cmds/incident_helper/tests/BatteryTypeParser_test.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BatteryTypeParser.h" + +#include "frameworks/base/core/proto/android/os/batterytype.pb.h" + +#include <android-base/file.h> +#include <android-base/test_utils.h> +#include <gmock/gmock.h> +#include <google/protobuf/message_lite.h> +#include <gtest/gtest.h> +#include <string.h> +#include <fcntl.h> + +using namespace android::base; +using namespace android::os; +using namespace std; +using ::testing::StrEq; +using ::testing::Test; +using ::testing::internal::CaptureStderr; +using ::testing::internal::CaptureStdout; +using ::testing::internal::GetCapturedStderr; +using ::testing::internal::GetCapturedStdout; + +class BatteryTypeParserTest : public Test { +public: + virtual void SetUp() override { + ASSERT_TRUE(tf.fd != -1); + } + +protected: + TemporaryFile tf; + + const string kTestPath = GetExecutableDirectory(); + const string kTestDataPath = kTestPath + "/testdata/"; +}; + +TEST_F(BatteryTypeParserTest, Success) { + const string testFile = kTestDataPath + "batterytype.txt"; + BatteryTypeParser parser; + BatteryTypeProto expected; + + expected.set_type("random_battery_type_string"); + + int fd = open(testFile.c_str(), O_RDONLY); + ASSERT_TRUE(fd != -1); + + CaptureStdout(); + ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO)); + EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString()); + close(fd); +} diff --git a/core/proto/android/os/batterytype.proto b/core/proto/android/os/batterytype.proto new file mode 100644 index 000000000000..75d0dd3504e3 --- /dev/null +++ b/core/proto/android/os/batterytype.proto @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +syntax = "proto2"; + +package android.os; + +option java_multiple_files = true; + +message BatteryTypeProto { + optional string type = 1; +} diff --git a/core/proto/android/os/incident.proto b/core/proto/android/os/incident.proto index ac8f26d94ca2..09c08a9764ed 100644 --- a/core/proto/android/os/incident.proto +++ b/core/proto/android/os/incident.proto @@ -18,6 +18,7 @@ syntax = "proto2"; option java_multiple_files = true; option java_outer_classname = "IncidentProtoMetadata"; +import "frameworks/base/core/proto/android/os/batterytype.proto"; import "frameworks/base/core/proto/android/os/cpufreq.proto"; import "frameworks/base/core/proto/android/os/cpuinfo.proto"; import "frameworks/base/core/proto/android/os/incidentheader.proto"; @@ -94,6 +95,11 @@ message IncidentProto { (section).args = "/sys/devices/system/cpu/cpufreq/all_time_in_state" ]; + optional BatteryTypeProto battery_type = 2006 [ + (section).type = SECTION_FILE, + (section).args = "/sys/class/power_supply/bms/battery_type" + ]; + // System Services optional com.android.server.fingerprint.FingerprintServiceDumpProto fingerprint = 3000 [ (section).type = SECTION_DUMPSYS, |