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 "custom_field_def.h"
18
19 #include "util.h"
20
CustomFieldDef(std::string name,std::string include)21 CustomFieldDef::CustomFieldDef(std::string name, std::string include) : TypeDef(name), include_(include) {}
22
CustomFieldDef(std::string name,std::string include,int size)23 CustomFieldDef::CustomFieldDef(std::string name, std::string include, int size)
24 : TypeDef(name, size), include_(include) {
25 if (size % 8 != 0) {
26 ERROR() << "Custom fields must be byte aligned.";
27 }
28 }
29
GetNewField(const std::string & name,ParseLocation loc) const30 PacketField* CustomFieldDef::GetNewField(const std::string& name, ParseLocation loc) const {
31 if (size_ == -1) {
32 return new CustomField(name, name_, loc);
33 } else {
34 return new CustomFieldFixedSize(name, name_, size_, loc);
35 }
36 }
37
GetDefinitionType() const38 TypeDef::Type CustomFieldDef::GetDefinitionType() const {
39 return TypeDef::Type::CUSTOM;
40 }
41
GenInclude(std::ostream & s) const42 void CustomFieldDef::GenInclude(std::ostream& s) const {
43 s << "#include \"" << include_ << util::CamelCaseToUnderScore(GetTypeName()) << ".h\"\n";
44 }
45
GenUsing(std::ostream & s) const46 void CustomFieldDef::GenUsing(std::ostream& s) const {
47 s << "using ::bluetooth::";
48 for (const auto& c : include_) {
49 switch (c) {
50 case '/':
51 s << "::";
52 break;
53 default:
54 s << c;
55 }
56 }
57 s << GetTypeName() << ";";
58 }
59
GenFixedSizeCustomFieldCheck(std::ostream & s) const60 void CustomFieldDef::GenFixedSizeCustomFieldCheck(std::ostream& s) const {
61 s << "static_assert(std::is_base_of_v<CustomFieldFixedSizeInterface<" << name_ << ">, " << name_ << ">, \"";
62 s << name_ << " is not a valid fixed size custom field type. Please see README for more details.\");";
63 s << "static_assert(CustomFieldFixedSizeInterface<" << name_ << ">::length() * 8 == " << size_
64 << ", \"CustomFieldFixedSizeInterface<" << name_ << ">::length * 8 should match PDL defined size (in bits) "
65 << size_ << "\");";
66 }
67
GenCustomFieldCheck(std::ostream & s,bool little_endian) const68 void CustomFieldDef::GenCustomFieldCheck(std::ostream& s, bool little_endian) const {
69 s << "static_assert(CustomTypeChecker<" << name_ << ", ";
70 s << (little_endian ? "" : "!") << "kLittleEndian>::value, \"";
71 s << name_ << " is not a valid custom field type. Please see README for more details.\");";
72 }
73