1 /*
2  * Copyright (C) 2024 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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BATTERY_STATS_READER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BATTERY_STATS_READER_H_
19 
20 #include <chrono>
21 #include <cstdint>
22 #include <optional>
23 
24 #include "perfetto/base/status.h"
25 #include "perfetto/ext/base/string_view.h"
26 #include "src/trace_processor/importers/android_bugreport/android_dumpstate_event.h"
27 #include "src/trace_processor/importers/android_bugreport/chunked_line_reader.h"
28 
29 namespace perfetto ::trace_processor {
30 
31 class TraceProcessorContext;
32 
33 // Parses the battery stats checkin produded by (dumpsys batterystats -c),
34 class AndroidBatteryStatsReader : public ChunkedLineReader {
35  public:
36   explicit AndroidBatteryStatsReader(TraceProcessorContext* context);
37 
38   ~AndroidBatteryStatsReader() override;
39 
40   base::Status ParseLine(base::StringView line) override;
41   void EndOfStream(base::StringView leftovers) override;
42 
43  private:
44   // Called for each event parsed from the stream.
45   // `event_ts` is the ts of the event as read from the log.
46   // Default implementation just calls `SendToSorter`.
47   base::Status ProcessBatteryStatsHistoryEvent(
48       const base::StringView raw_event);
49 
50   // Sends the given event to the sorting stage.
51   // `event_ts` is the ts of the event as read from the log and will be
52   // converted to a trace_ts (with necessary clock conversions applied)
53   base::Status SendToSorter(std::chrono::nanoseconds event_ts,
54                             AndroidDumpstateEvent event);
55 
56   base::Status ProcessItemStr(base::StringView item);
57 
58   TraceProcessorContext* const context_;
59 
60   uint64_t current_timestamp_ms_;
61 };
62 
63 }  // namespace perfetto::trace_processor
64 
65 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_BATTERY_STATS_READER_H_
66