1 /**
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 #define LOG_TAG "dumpstate"
18 
19 #include <android/os/IncidentReportArgs.h>
20 
21 #include <cutils/log.h>
22 
23 namespace android {
24 namespace os {
25 
IncidentReportArgs()26 IncidentReportArgs::IncidentReportArgs()
27     :mSections(),
28      mAll(false),
29      mDest(-1)
30 {
31 }
32 
IncidentReportArgs(const IncidentReportArgs & that)33 IncidentReportArgs::IncidentReportArgs(const IncidentReportArgs& that)
34     :mSections(that.mSections),
35      mHeaders(that.mHeaders),
36      mAll(that.mAll),
37      mDest(that.mDest)
38 {
39 }
40 
~IncidentReportArgs()41 IncidentReportArgs::~IncidentReportArgs()
42 {
43 }
44 
45 status_t
writeToParcel(Parcel * out) const46 IncidentReportArgs::writeToParcel(Parcel* out) const
47 {
48     status_t err;
49 
50     err = out->writeInt32(mAll);
51     if (err != NO_ERROR) {
52         return err;
53     }
54 
55     err = out->writeInt32(mSections.size());
56     if (err != NO_ERROR) {
57         return err;
58     }
59 
60     for (set<int>::const_iterator it=mSections.begin(); it!=mSections.end(); it++) {
61         err = out->writeInt32(*it);
62         if (err != NO_ERROR) {
63             return err;
64         }
65     }
66 
67     err = out->writeInt32(mHeaders.size());
68     if (err != NO_ERROR) {
69         return err;
70     }
71 
72     for (vector<vector<uint8_t>>::const_iterator it = mHeaders.begin(); it != mHeaders.end(); it++) {
73         err = out->writeByteVector(*it);
74         if (err != NO_ERROR) {
75             return err;
76         }
77     }
78 
79     err = out->writeInt32(mDest);
80     if (err != NO_ERROR) {
81         return err;
82     }
83 
84     return NO_ERROR;
85 }
86 
87 status_t
readFromParcel(const Parcel * in)88 IncidentReportArgs::readFromParcel(const Parcel* in)
89 {
90     status_t err;
91 
92     int32_t all;
93     err = in->readInt32(&all);
94     if (err != NO_ERROR) {
95         return err;
96     }
97     if (all != 0) {
98         mAll = all;
99     }
100 
101     mSections.clear();
102     int32_t sectionCount;
103     err = in->readInt32(&sectionCount);
104     if (err != NO_ERROR) {
105         return err;
106     }
107     for (int i=0; i<sectionCount; i++) {
108         int32_t section;
109         err = in->readInt32(&section);
110         if (err != NO_ERROR) {
111             return err;
112         }
113 
114         mSections.insert(section);
115     }
116 
117     int32_t headerCount;
118     err = in->readInt32(&headerCount);
119     if (err != NO_ERROR) {
120         return err;
121     }
122     mHeaders.resize(headerCount);
123     for (int i=0; i<headerCount; i++) {
124         err = in->readByteVector(&mHeaders[i]);
125         if (err != NO_ERROR) {
126             return err;
127         }
128     }
129 
130     int32_t dest;
131     err = in->readInt32(&dest);
132     if (err != NO_ERROR) {
133         return err;
134     }
135     mDest = dest;
136 
137     return OK;
138 }
139 
140 void
setAll(bool all)141 IncidentReportArgs::setAll(bool all)
142 {
143     mAll = all;
144     if (all) {
145         mSections.clear();
146     }
147 }
148 
149 void
setDest(int dest)150 IncidentReportArgs::setDest(int dest)
151 {
152     mDest = dest;
153 }
154 
155 void
addSection(int section)156 IncidentReportArgs::addSection(int section)
157 {
158     if (!mAll) {
159         mSections.insert(section);
160     }
161 }
162 
163 void
addHeader(const IncidentHeaderProto & headerProto)164 IncidentReportArgs::addHeader(const IncidentHeaderProto& headerProto)
165 {
166     vector<uint8_t> header;
167     auto serialized = headerProto.SerializeAsString();
168     if (serialized.empty()) return;
169     for (auto it = serialized.begin(); it != serialized.end(); it++) {
170         header.push_back((uint8_t)*it);
171     }
172     mHeaders.push_back(header);
173 }
174 
175 bool
containsSection(int section) const176 IncidentReportArgs::containsSection(int section) const
177 {
178      return mAll || mSections.find(section) != mSections.end();
179 }
180 
181 void
merge(const IncidentReportArgs & that)182 IncidentReportArgs::merge(const IncidentReportArgs& that)
183 {
184     if (mAll) {
185         return;
186     } else if (that.mAll) {
187         mAll = true;
188         mSections.clear();
189     } else {
190         for (set<int>::const_iterator it=that.mSections.begin();
191                 it!=that.mSections.end(); it++) {
192             mSections.insert(*it);
193         }
194     }
195 }
196 
197 }
198 }
199