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 
GenPyBind11Include(std::ostream & s) const46 void CustomFieldDef::GenPyBind11Include(std::ostream& s) const {
47   s << "#include \"" << include_ << util::CamelCaseToUnderScore(GetTypeName()) << "_pybind11_type_caster.h\"\n";
48 }
49 
GenUsing(std::ostream & s) const50 void CustomFieldDef::GenUsing(std::ostream& s) const {
51   s << "using ::bluetooth::";
52   for (const auto& c : include_) {
53     switch (c) {
54       case '/':
55         s << "::";
56         break;
57       default:
58         s << c;
59     }
60   }
61   s << GetTypeName() << ";";
62 }
63 
GenFixedSizeCustomFieldCheck(std::ostream & s) const64 void CustomFieldDef::GenFixedSizeCustomFieldCheck(std::ostream& s) const {
65   s << "static_assert(std::is_base_of_v<CustomFieldFixedSizeInterface<" << name_ << ">, " << name_ << ">, \"";
66   s << name_ << " is not a valid fixed size custom field type. Please see README for more details.\");";
67   s << "static_assert(CustomFieldFixedSizeInterface<" << name_ << ">::length() * 8 == " << size_
68     << ", \"CustomFieldFixedSizeInterface<" << name_ << ">::length * 8 should match PDL defined size (in bits) "
69     << size_ << "\");";
70 }
71 
GenCustomFieldCheck(std::ostream & s,bool little_endian) const72 void CustomFieldDef::GenCustomFieldCheck(std::ostream& s, bool little_endian) const {
73   s << "static_assert(CustomTypeChecker<" << name_ << ", ";
74   s << (little_endian ? "" : "!") << "kLittleEndian>::value, \"";
75   s << name_ << " is not a valid custom field type. Please see README for more details.\");";
76 }
77