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