1 /*
2 * Copyright 2019 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 #include "fields/payload_field.h"
18
19 #include "util.h"
20
21 const std::string PayloadField::kFieldType = "PayloadField";
22
PayloadField(std::string modifier,ParseLocation loc)23 PayloadField::PayloadField(std::string modifier, ParseLocation loc)
24 : PacketField("payload", loc), size_field_(nullptr), size_modifier_(modifier) {}
25
SetSizeField(const SizeField * size_field)26 void PayloadField::SetSizeField(const SizeField* size_field) {
27 if (size_field_ != nullptr) {
28 ERROR(this, size_field_, size_field)
29 << "The size field for the payload has already been assigned.";
30 }
31
32 size_field_ = size_field;
33 }
34
GetFieldType() const35 const std::string& PayloadField::GetFieldType() const { return PayloadField::kFieldType; }
36
GetSize() const37 Size PayloadField::GetSize() const {
38 if (size_field_ == nullptr) {
39 if (!size_modifier_.empty()) {
40 ERROR(this) << "Missing size field for payload with size modifier.";
41 }
42 return Size();
43 }
44
45 std::string dynamic_size =
46 "(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "() * 8)";
47 if (!size_modifier_.empty()) {
48 dynamic_size += "- (" + size_modifier_.substr(1) + " * 8)";
49 }
50
51 return dynamic_size;
52 }
53
GetDataType() const54 std::string PayloadField::GetDataType() const { return "PacketView"; }
55
GenExtractor(std::ostream &,int,bool) const56 void PayloadField::GenExtractor(std::ostream&, int, bool) const {
57 ERROR(this) << __func__ << " should never be called. ";
58 }
59
GetGetterFunctionName() const60 std::string PayloadField::GetGetterFunctionName() const { return "GetPayload"; }
61
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const62 void PayloadField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
63 s << "PacketView<kLittleEndian> " << GetGetterFunctionName() << "() const {";
64 s << "ASSERT(was_validated_);";
65 s << "size_t end_index = size();";
66 s << "auto to_bound = begin();";
67 GenBounds(s, start_offset, end_offset, GetSize());
68 s << "return GetLittleEndianSubview(field_begin, field_end);";
69 s << "}\n\n";
70 }
71
GetBuilderParameterType() const72 std::string PayloadField::GetBuilderParameterType() const {
73 return "std::unique_ptr<BasePacketBuilder>";
74 }
75
BuilderParameterMustBeMoved() const76 bool PayloadField::BuilderParameterMustBeMoved() const { return true; }
77
GenBuilderParameterFromView(std::ostream & s) const78 void PayloadField::GenBuilderParameterFromView(std::ostream& s) const {
79 s << "std::make_unique<RawBuilder>(std::vector<uint8_t>(view.GetPayload().begin(), "
80 "view.GetPayload().end()))";
81 }
82
HasParameterValidator() const83 bool PayloadField::HasParameterValidator() const { return false; }
84
GenParameterValidator(std::ostream &) const85 void PayloadField::GenParameterValidator(std::ostream&) const {
86 // There is no validation needed for a payload
87 }
88
GenInserter(std::ostream &) const89 void PayloadField::GenInserter(std::ostream&) const {
90 ERROR() << __func__ << " Should never be called.";
91 }
92
GenValidator(std::ostream &) const93 void PayloadField::GenValidator(std::ostream&) const {
94 // Do nothing
95 }
96
GenStringRepresentation(std::ostream & s,std::string) const97 void PayloadField::GenStringRepresentation(std::ostream& s, std::string) const {
98 // TODO: we should parse the child packets
99 s << "\"PAYLOAD[]\"";
100 }
101