blob: 2a3abd7b4d976257b5e1b605731cab89b3f106f5 [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 */
Yi Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Joe Onorato1754d742016-11-21 17:51:35 -080017
18#ifndef REPORTER_H
19#define REPORTER_H
20
21#include <android/os/IIncidentReportStatusListener.h>
22#include <android/os/IncidentReportArgs.h>
23
Yi Jin329130b2018-02-09 16:47:47 -080024#include <map>
Joe Onorato1754d742016-11-21 17:51:35 -080025#include <string>
26#include <vector>
27
28#include <time.h>
29
Yi Jin4e843102018-02-14 15:36:18 -080030#include "Throttler.h"
31#include "frameworks/base/libs/incident/proto/android/os/metadata.pb.h"
32
Yi Jin6cacbcb2018-03-30 14:04:52 -070033namespace android {
34namespace os {
35namespace incidentd {
Joe Onorato1754d742016-11-21 17:51:35 -080036
37// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080038struct ReportRequest : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -080039 IncidentReportArgs args;
40 sp<IIncidentReportStatusListener> listener;
41 int fd;
42 status_t err;
43
Yi Jinb592e3b2018-02-01 15:17:04 -080044 ReportRequest(const IncidentReportArgs& args, const sp<IIncidentReportStatusListener>& listener,
45 int fd);
Joe Onorato1754d742016-11-21 17:51:35 -080046 virtual ~ReportRequest();
Yi Jinedfd5bb2017-09-06 17:09:11 -070047
Yi Jinb592e3b2018-02-01 15:17:04 -080048 bool ok(); // returns true if the request is ok for write.
Joe Onorato1754d742016-11-21 17:51:35 -080049};
50
51// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080052class ReportRequestSet {
Joe Onorato1754d742016-11-21 17:51:35 -080053public:
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; }
Yi Jin86dce412018-03-07 11:36:57 -080069 map<int, IncidentMetadata::SectionStats>& allSectionStats() { return mSectionStats; }
Yi Jin329130b2018-02-09 16:47:47 -080070
71 bool containsSection(int id);
72 IncidentMetadata::SectionStats* sectionStats(int id);
73
Joe Onorato1754d742016-11-21 17:51:35 -080074private:
75 vector<sp<ReportRequest>> mRequests;
Yi Jinadd11e92017-07-30 16:10:07 -070076 IncidentReportArgs mSections;
Joe Onorato1754d742016-11-21 17:51:35 -080077 int mMainFd;
Yi Jin3ec5cc72018-01-26 13:42:43 -080078 int mMainDest;
Yi Jin329130b2018-02-09 16:47:47 -080079
80 IncidentMetadata mMetadata;
Yi Jin86dce412018-03-07 11:36:57 -080081 map<int, IncidentMetadata::SectionStats> mSectionStats;
Joe Onorato1754d742016-11-21 17:51:35 -080082};
83
84// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080085class Reporter : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -080086public:
Yi Jinb592e3b2018-02-01 15:17:04 -080087 enum run_report_status_t { REPORT_FINISHED = 0, REPORT_NEEDS_DROPBOX = 1 };
Joe Onorato1754d742016-11-21 17:51:35 -080088
Joe Onorato1754d742016-11-21 17:51:35 -080089 ReportRequestSet batch;
90
Yi Jinb592e3b2018-02-01 15:17:04 -080091 Reporter(); // PROD must use this constructor.
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -080092 explicit Reporter(const char* directory); // For testing purpose only.
Joe Onorato1754d742016-11-21 17:51:35 -080093 virtual ~Reporter();
94
95 // Run the report as described in the batch and args parameters.
Yi Jin4e843102018-02-14 15:36:18 -080096 run_report_status_t runReport(size_t* reportByteSize);
Joe Onorato1754d742016-11-21 17:51:35 -080097
98 static run_report_status_t upload_backlog();
99
100private:
Yi Jinadd11e92017-07-30 16:10:07 -0700101 String8 mIncidentDirectory;
102
Joe Onorato1754d742016-11-21 17:51:35 -0800103 string mFilename;
104 off_t mMaxSize;
105 size_t mMaxCount;
106 time_t mStartTime;
107
108 status_t create_file(int* fd);
Yi Jinadd11e92017-07-30 16:10:07 -0700109
Yi Jinb592e3b2018-02-01 15:17:04 -0800110 bool isTest = true; // default to true for testing
Joe Onorato1754d742016-11-21 17:51:35 -0800111};
112
Yi Jin6cacbcb2018-03-30 14:04:52 -0700113} // namespace incidentd
114} // namespace os
115} // namespace android
116
Yi Jinb592e3b2018-02-01 15:17:04 -0800117#endif // REPORTER_H