blob: 6058068be3317bb5e7c8e5f83100a7f2e105c79c [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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 REPORTER_H
18#define REPORTER_H
19
Yi Jin329130b2018-02-09 16:47:47 -080020#include "frameworks/base/libs/incident/proto/android/os/metadata.pb.h"
21
Joe Onorato1754d742016-11-21 17:51:35 -080022#include <android/os/IIncidentReportStatusListener.h>
23#include <android/os/IncidentReportArgs.h>
24
Yi Jin329130b2018-02-09 16:47:47 -080025#include <map>
Joe Onorato1754d742016-11-21 17:51:35 -080026#include <string>
27#include <vector>
28
29#include <time.h>
30
31using namespace android;
32using namespace android::os;
33using namespace std;
34
35// ================================================================================
36struct ReportRequest : public virtual RefBase
37{
38 IncidentReportArgs args;
39 sp<IIncidentReportStatusListener> listener;
40 int fd;
41 status_t err;
42
43 ReportRequest(const IncidentReportArgs& args,
44 const sp<IIncidentReportStatusListener> &listener, int fd);
45 virtual ~ReportRequest();
Yi Jinedfd5bb2017-09-06 17:09:11 -070046
47 bool ok(); // returns true if the request is ok for write.
Joe Onorato1754d742016-11-21 17:51:35 -080048};
49
50// ================================================================================
51class ReportRequestSet
52{
53public:
54 ReportRequestSet();
55 ~ReportRequestSet();
56
57 void add(const sp<ReportRequest>& request);
58 void setMainFd(int fd);
Yi Jin3ec5cc72018-01-26 13:42:43 -080059 void setMainDest(int dest);
Joe Onorato1754d742016-11-21 17:51:35 -080060
Joe Onorato1754d742016-11-21 17:51:35 -080061 typedef vector<sp<ReportRequest>>::iterator iterator;
62
63 iterator begin() { return mRequests.begin(); }
64 iterator end() { return mRequests.end(); }
65
Yi Jin99c248f2017-08-25 18:11:58 -070066 int mainFd() { return mMainFd; }
Yi Jin3ec5cc72018-01-26 13:42:43 -080067 int mainDest() { return mMainDest; }
Yi Jin329130b2018-02-09 16:47:47 -080068 IncidentMetadata& metadata() { return mMetadata; }
69
70 bool containsSection(int id);
71 IncidentMetadata::SectionStats* sectionStats(int id);
72
Joe Onorato1754d742016-11-21 17:51:35 -080073private:
74 vector<sp<ReportRequest>> mRequests;
Yi Jinadd11e92017-07-30 16:10:07 -070075 IncidentReportArgs mSections;
Joe Onorato1754d742016-11-21 17:51:35 -080076 int mMainFd;
Yi Jin3ec5cc72018-01-26 13:42:43 -080077 int mMainDest;
Yi Jin329130b2018-02-09 16:47:47 -080078
79 IncidentMetadata mMetadata;
80 map<int, IncidentMetadata::SectionStats*> mSectionStats;
Joe Onorato1754d742016-11-21 17:51:35 -080081};
82
83// ================================================================================
84class Reporter : public virtual RefBase
85{
86public:
87 enum run_report_status_t {
88 REPORT_FINISHED = 0,
89 REPORT_NEEDS_DROPBOX = 1
90 };
91
Joe Onorato1754d742016-11-21 17:51:35 -080092 ReportRequestSet batch;
93
Yi Jin329130b2018-02-09 16:47:47 -080094 Reporter(); // PROD must use this constructor.
95 Reporter(const char* directory); // For testing purpose only.
Joe Onorato1754d742016-11-21 17:51:35 -080096 virtual ~Reporter();
97
98 // Run the report as described in the batch and args parameters.
99 run_report_status_t runReport();
100
101 static run_report_status_t upload_backlog();
102
103private:
Yi Jinadd11e92017-07-30 16:10:07 -0700104 String8 mIncidentDirectory;
105
Joe Onorato1754d742016-11-21 17:51:35 -0800106 string mFilename;
107 off_t mMaxSize;
108 size_t mMaxCount;
109 time_t mStartTime;
110
111 status_t create_file(int* fd);
Yi Jinadd11e92017-07-30 16:10:07 -0700112
113 bool isTest = true; // default to true for testing
Joe Onorato1754d742016-11-21 17:51:35 -0800114};
115
116
117#endif // REPORTER_H