Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 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 <unistd.h> |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 18 | #include <cerrno> |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 19 | #include <cstdio> |
| 20 | #include <filesystem> |
| 21 | #include <fstream> |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 22 | #include <iostream> |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 23 | #include <queue> |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 24 | #include <regex> |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 25 | #include <sstream> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "declarations.h" |
Myles Watson | 945f4c3 | 2019-09-25 15:33:39 -0700 | [diff] [blame] | 29 | #include "struct_parser_generator.h" |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 30 | |
| 31 | #include "language_y.h" |
| 32 | |
| 33 | void yylex_init(void**); |
| 34 | void yylex_destroy(void*); |
| 35 | void yyset_debug(int, void*); |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 36 | void yyset_in(FILE*, void*); |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 37 | |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 38 | namespace { |
| 39 | |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 40 | void parse_namespace(const std::string& root_namespace, const std::filesystem::path& input_file_relative_path, |
| 41 | std::vector<std::string>* token) { |
Myles Watson | fd06da0 | 2019-10-17 09:12:36 -0700 | [diff] [blame] | 42 | std::filesystem::path gen_namespace = root_namespace / input_file_relative_path; |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 43 | std::string gen_namespace_str = gen_namespace; |
| 44 | std::regex path_tokenizer("/"); |
| 45 | auto it = std::sregex_token_iterator(gen_namespace_str.cbegin(), gen_namespace_str.cend(), path_tokenizer, -1); |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 46 | std::sregex_token_iterator it_end = {}; |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 47 | for (; it != it_end; ++it) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 48 | token->push_back(it->str()); |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | void generate_namespace_open(const std::vector<std::string>& token, std::ostream& output) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 53 | for (const auto& ns : token) { |
| 54 | output << "namespace " << ns << " {" << std::endl; |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void generate_namespace_close(const std::vector<std::string>& token, std::ostream& output) { |
| 59 | for (auto it = token.rbegin(); it != token.rend(); ++it) { |
| 60 | output << "} //namespace " << *it << std::endl; |
| 61 | } |
| 62 | } |
| 63 | |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 64 | bool parse_declarations_one_file(const std::filesystem::path& input_file, Declarations* declarations) { |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 65 | void* scanner; |
| 66 | yylex_init(&scanner); |
| 67 | |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 68 | FILE* in_file = fopen(input_file.string().c_str(), "r"); |
| 69 | if (in_file == nullptr) { |
| 70 | std::cerr << "can't open " << input_file << ": " << strerror(errno) << std::endl; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | yyset_in(in_file, scanner); |
| 75 | |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 76 | int ret = yy::parser(scanner, declarations).parse(); |
| 77 | if (ret != 0) { |
| 78 | std::cerr << "yylex parsing failed: returned " << ret << std::endl; |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | yylex_destroy(scanner); |
| 83 | |
| 84 | fclose(in_file); |
| 85 | |
| 86 | // Set endianess before returning |
| 87 | for (auto& s : declarations->type_defs_queue_) { |
| 88 | if (s.second->GetDefinitionType() == TypeDef::Type::STRUCT) { |
| 89 | auto* struct_def = dynamic_cast<StructDef*>(s.second); |
| 90 | struct_def->SetEndianness(declarations->is_little_endian); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | for (auto& packet_def : declarations->packet_defs_queue_) { |
| 95 | packet_def.second.SetEndianness(declarations->is_little_endian); |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool generate_cpp_headers_one_file(const Declarations& decls, const std::filesystem::path& input_file, |
| 102 | const std::filesystem::path& include_dir, const std::filesystem::path& out_dir, |
| 103 | const std::string& root_namespace) { |
| 104 | auto gen_relative_path = input_file.lexically_relative(include_dir).parent_path(); |
| 105 | |
| 106 | auto input_filename = input_file.filename().string().substr(0, input_file.filename().string().find(".pdl")); |
| 107 | auto gen_path = out_dir / gen_relative_path; |
| 108 | |
| 109 | std::filesystem::create_directories(gen_path); |
| 110 | |
| 111 | auto gen_file = gen_path / (input_filename + ".h"); |
| 112 | |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 113 | std::ofstream out_file; |
| 114 | out_file.open(gen_file); |
| 115 | if (!out_file.is_open()) { |
| 116 | std::cerr << "can't open " << gen_file << std::endl; |
| 117 | return false; |
| 118 | } |
| 119 | |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 120 | out_file << "\n\n"; |
Myles Watson | 24bbd91 | 2019-04-05 08:45:43 -0700 | [diff] [blame] | 121 | out_file << "#pragma once\n"; |
| 122 | out_file << "\n\n"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 123 | out_file << "#include <stdint.h>\n"; |
| 124 | out_file << "#include <string>\n"; |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 125 | out_file << "#include <functional>\n"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 126 | out_file << "\n\n"; |
| 127 | out_file << "#include \"os/log.h\"\n"; |
| 128 | out_file << "#include \"packet/base_packet_builder.h\"\n"; |
| 129 | out_file << "#include \"packet/bit_inserter.h\"\n"; |
Myles Watson | 5485226 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 130 | out_file << "#include \"packet/iterator.h\"\n"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 131 | out_file << "#include \"packet/packet_builder.h\"\n"; |
Myles Watson | 5485226 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 132 | out_file << "#include \"packet/packet_struct.h\"\n"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 133 | out_file << "#include \"packet/packet_view.h\"\n"; |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 134 | out_file << "#include \"packet/parser/checksum_type_checker.h\"\n"; |
Myles Watson | 2569ca7 | 2019-07-15 14:39:23 -0700 | [diff] [blame] | 135 | out_file << "#include \"packet/parser/custom_type_checker.h\"\n"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 136 | out_file << "\n\n"; |
| 137 | |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 138 | for (const auto& c : decls.type_defs_queue_) { |
Myles Watson | 0961778 | 2019-07-02 10:14:47 -0700 | [diff] [blame] | 139 | if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM || |
| 140 | c.second->GetDefinitionType() == TypeDef::Type::CHECKSUM) { |
| 141 | ((CustomFieldDef*)c.second)->GenInclude(out_file); |
| 142 | } |
Myles Watson | af1b2d1 | 2019-04-12 10:39:59 -0700 | [diff] [blame] | 143 | } |
| 144 | out_file << "\n\n"; |
| 145 | |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 146 | std::vector<std::string> namespace_list; |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 147 | parse_namespace(root_namespace, gen_relative_path, &namespace_list); |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 148 | generate_namespace_open(namespace_list, out_file); |
Myles Watson | af1b2d1 | 2019-04-12 10:39:59 -0700 | [diff] [blame] | 149 | out_file << "\n\n"; |
| 150 | |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 151 | for (const auto& c : decls.type_defs_queue_) { |
Myles Watson | 0961778 | 2019-07-02 10:14:47 -0700 | [diff] [blame] | 152 | if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM || |
| 153 | c.second->GetDefinitionType() == TypeDef::Type::CHECKSUM) { |
| 154 | ((CustomFieldDef*)c.second)->GenUsing(out_file); |
| 155 | } |
Myles Watson | af1b2d1 | 2019-04-12 10:39:59 -0700 | [diff] [blame] | 156 | } |
| 157 | out_file << "\n\n"; |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 158 | |
| 159 | out_file << "using ::bluetooth::packet::BasePacketBuilder;"; |
| 160 | out_file << "using ::bluetooth::packet::BitInserter;"; |
Myles Watson | de15b64 | 2019-07-02 12:02:47 -0700 | [diff] [blame] | 161 | out_file << "using ::bluetooth::packet::CustomTypeChecker;"; |
Myles Watson | 5485226 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 162 | out_file << "using ::bluetooth::packet::Iterator;"; |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 163 | out_file << "using ::bluetooth::packet::kLittleEndian;"; |
| 164 | out_file << "using ::bluetooth::packet::PacketBuilder;"; |
Myles Watson | 5485226 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 165 | out_file << "using ::bluetooth::packet::PacketStruct;"; |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 166 | out_file << "using ::bluetooth::packet::PacketView;"; |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 167 | out_file << "using ::bluetooth::packet::parser::ChecksumTypeChecker;"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 168 | out_file << "\n\n"; |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 169 | |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 170 | for (const auto& e : decls.type_defs_queue_) { |
| 171 | if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 172 | const auto* enum_def = dynamic_cast<const EnumDef*>(e.second); |
| 173 | EnumGen gen(*enum_def); |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 174 | gen.GenDefinition(out_file); |
| 175 | out_file << "\n\n"; |
| 176 | } |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 177 | } |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 178 | for (const auto& e : decls.type_defs_queue_) { |
| 179 | if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 180 | const auto* enum_def = dynamic_cast<const EnumDef*>(e.second); |
| 181 | EnumGen gen(*enum_def); |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 182 | gen.GenLogging(out_file); |
| 183 | out_file << "\n\n"; |
| 184 | } |
| 185 | } |
| 186 | for (const auto& ch : decls.type_defs_queue_) { |
| 187 | if (ch.second->GetDefinitionType() == TypeDef::Type::CHECKSUM) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 188 | const auto* checksum_def = dynamic_cast<const ChecksumDef*>(ch.second); |
| 189 | checksum_def->GenChecksumCheck(out_file); |
Myles Watson | c093d28 | 2019-04-18 09:09:01 -0700 | [diff] [blame] | 190 | } |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 191 | } |
Myles Watson | de15b64 | 2019-07-02 12:02:47 -0700 | [diff] [blame] | 192 | out_file << "\n/* Done ChecksumChecks */\n"; |
| 193 | |
| 194 | for (const auto& c : decls.type_defs_queue_) { |
| 195 | if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM && c.second->size_ == -1 /* Variable Size */) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 196 | const auto* custom_field_def = dynamic_cast<const CustomFieldDef*>(c.second); |
| 197 | custom_field_def->GenCustomFieldCheck(out_file, decls.is_little_endian); |
Myles Watson | de15b64 | 2019-07-02 12:02:47 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | out_file << "\n"; |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 201 | |
Myles Watson | 5485226 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 202 | for (auto& s : decls.type_defs_queue_) { |
| 203 | if (s.second->GetDefinitionType() == TypeDef::Type::STRUCT) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 204 | const auto* struct_def = dynamic_cast<const StructDef*>(s.second); |
| 205 | struct_def->GenDefinition(out_file); |
Myles Watson | 5485226 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 206 | out_file << "\n"; |
| 207 | } |
| 208 | } |
| 209 | |
Myles Watson | 945f4c3 | 2019-09-25 15:33:39 -0700 | [diff] [blame] | 210 | { |
| 211 | StructParserGenerator spg(decls); |
| 212 | spg.Generate(out_file); |
| 213 | out_file << "\n\n"; |
| 214 | } |
| 215 | |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 216 | for (const auto& packet_def : decls.packet_defs_queue_) { |
| 217 | packet_def.second.GenParserDefinition(out_file); |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 218 | out_file << "\n\n"; |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 221 | for (const auto& packet_def : decls.packet_defs_queue_) { |
| 222 | packet_def.second.GenBuilderDefinition(out_file); |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 223 | out_file << "\n\n"; |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 224 | } |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 225 | |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 226 | generate_namespace_close(namespace_list, out_file); |
| 227 | |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 228 | out_file.close(); |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
Hansong Zhang | 934cc3d | 2019-04-04 16:23:32 -0700 | [diff] [blame] | 233 | } // namespace |
| 234 | |
Steven Moreland | 64e0450 | 2019-09-24 18:37:14 -0700 | [diff] [blame] | 235 | // TODO(b/141583809): stop leaks |
| 236 | extern "C" const char* __asan_default_options() { |
| 237 | return "detect_leaks=0"; |
| 238 | } |
| 239 | |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 240 | int main(int argc, const char** argv) { |
| 241 | std::filesystem::path out_dir; |
| 242 | std::filesystem::path include_dir; |
Myles Watson | fd06da0 | 2019-10-17 09:12:36 -0700 | [diff] [blame] | 243 | std::string root_namespace = "bluetooth"; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 244 | std::queue<std::filesystem::path> input_files; |
| 245 | const std::string arg_out = "--out="; |
| 246 | const std::string arg_include = "--include="; |
Myles Watson | fd06da0 | 2019-10-17 09:12:36 -0700 | [diff] [blame] | 247 | const std::string arg_namespace = "--root_namespace="; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 248 | |
| 249 | for (int i = 1; i < argc; i++) { |
| 250 | std::string arg = argv[i]; |
| 251 | if (arg.find(arg_out) == 0) { |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 252 | out_dir = std::filesystem::current_path() / std::filesystem::path(arg.substr(arg_out.size())); |
| 253 | } else if (arg.find(arg_include) == 0) { |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 254 | include_dir = std::filesystem::current_path() / std::filesystem::path(arg.substr(arg_include.size())); |
Myles Watson | fd06da0 | 2019-10-17 09:12:36 -0700 | [diff] [blame] | 255 | } else if (arg.find(arg_namespace) == 0) { |
| 256 | root_namespace = arg.substr(arg_namespace.size()); |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 257 | } else { |
| 258 | input_files.emplace(std::filesystem::current_path() / std::filesystem::path(arg)); |
| 259 | } |
| 260 | } |
| 261 | if (out_dir == std::filesystem::path() || include_dir == std::filesystem::path()) { |
Myles Watson | fd06da0 | 2019-10-17 09:12:36 -0700 | [diff] [blame] | 262 | std::cerr << "Usage: bt-packetgen --out=OUT --include=INCLUDE --root=NAMESPACE input_files..." << std::endl; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | while (!input_files.empty()) { |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 267 | Declarations declarations; |
| 268 | if (!parse_declarations_one_file(input_files.front(), &declarations)) { |
| 269 | std::cerr << "Cannot parse " << input_files.front() << " correctly" << std::endl; |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 270 | return 2; |
| 271 | } |
Jack He | a7e25ac | 2019-11-18 18:40:32 -0800 | [diff] [blame^] | 272 | if (!generate_cpp_headers_one_file(declarations, input_files.front(), include_dir, out_dir, root_namespace)) { |
| 273 | std::cerr << "Didn't generate cpp headers for " << input_files.front() << std::endl; |
| 274 | return 3; |
| 275 | } |
Hansong Zhang | 05c079b | 2019-04-04 13:24:43 -0700 | [diff] [blame] | 276 | input_files.pop(); |
| 277 | } |
| 278 | |
| 279 | return 0; |
Ajay Panicker | 4d6b760 | 2019-02-06 14:02:36 -0800 | [diff] [blame] | 280 | } |