blob: a53d71d3bdf5cbad9cbf35db1452eb92d7cfe233 [file] [log] [blame]
Ajay Panicker4d6b7602019-02-06 14:02:36 -08001/*
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 Hea7e25ac2019-11-18 18:40:32 -080018#include <cerrno>
Hansong Zhang05c079b2019-04-04 13:24:43 -070019#include <cstdio>
20#include <filesystem>
21#include <fstream>
Ajay Panicker4d6b7602019-02-06 14:02:36 -080022#include <iostream>
Hansong Zhang05c079b2019-04-04 13:24:43 -070023#include <queue>
Hansong Zhang934cc3d2019-04-04 16:23:32 -070024#include <regex>
Ajay Panicker4d6b7602019-02-06 14:02:36 -080025#include <sstream>
26#include <vector>
27
28#include "declarations.h"
Myles Watson945f4c32019-09-25 15:33:39 -070029#include "struct_parser_generator.h"
Ajay Panicker4d6b7602019-02-06 14:02:36 -080030
31#include "language_y.h"
32
33void yylex_init(void**);
34void yylex_destroy(void*);
35void yyset_debug(int, void*);
Hansong Zhang05c079b2019-04-04 13:24:43 -070036void yyset_in(FILE*, void*);
Ajay Panicker4d6b7602019-02-06 14:02:36 -080037
Hansong Zhang934cc3d2019-04-04 16:23:32 -070038namespace {
39
Jack Hea7e25ac2019-11-18 18:40:32 -080040void parse_namespace(const std::string& root_namespace, const std::filesystem::path& input_file_relative_path,
41 std::vector<std::string>* token) {
Myles Watsonfd06da02019-10-17 09:12:36 -070042 std::filesystem::path gen_namespace = root_namespace / input_file_relative_path;
Hansong Zhang934cc3d2019-04-04 16:23:32 -070043 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 Hea7e25ac2019-11-18 18:40:32 -080046 std::sregex_token_iterator it_end = {};
Hansong Zhang934cc3d2019-04-04 16:23:32 -070047 for (; it != it_end; ++it) {
Jack Hea7e25ac2019-11-18 18:40:32 -080048 token->push_back(it->str());
Hansong Zhang934cc3d2019-04-04 16:23:32 -070049 }
50}
51
52void generate_namespace_open(const std::vector<std::string>& token, std::ostream& output) {
Jack Hea7e25ac2019-11-18 18:40:32 -080053 for (const auto& ns : token) {
54 output << "namespace " << ns << " {" << std::endl;
Hansong Zhang934cc3d2019-04-04 16:23:32 -070055 }
56}
57
58void 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 Hea7e25ac2019-11-18 18:40:32 -080064bool parse_declarations_one_file(const std::filesystem::path& input_file, Declarations* declarations) {
Ajay Panicker4d6b7602019-02-06 14:02:36 -080065 void* scanner;
66 yylex_init(&scanner);
67
Hansong Zhang05c079b2019-04-04 13:24:43 -070068 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 Hea7e25ac2019-11-18 18:40:32 -080076 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
101bool 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 Zhang05c079b2019-04-04 13:24:43 -0700113 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 Zhang05c079b2019-04-04 13:24:43 -0700120 out_file << "\n\n";
Myles Watson24bbd912019-04-05 08:45:43 -0700121 out_file << "#pragma once\n";
122 out_file << "\n\n";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700123 out_file << "#include <stdint.h>\n";
124 out_file << "#include <string>\n";
Myles Watsonc093d282019-04-18 09:09:01 -0700125 out_file << "#include <functional>\n";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700126 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 Watson54852262019-02-06 14:02:36 -0800130 out_file << "#include \"packet/iterator.h\"\n";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700131 out_file << "#include \"packet/packet_builder.h\"\n";
Myles Watson54852262019-02-06 14:02:36 -0800132 out_file << "#include \"packet/packet_struct.h\"\n";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700133 out_file << "#include \"packet/packet_view.h\"\n";
Myles Watsonc093d282019-04-18 09:09:01 -0700134 out_file << "#include \"packet/parser/checksum_type_checker.h\"\n";
Myles Watson2569ca72019-07-15 14:39:23 -0700135 out_file << "#include \"packet/parser/custom_type_checker.h\"\n";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700136 out_file << "\n\n";
137
Myles Watsonc093d282019-04-18 09:09:01 -0700138 for (const auto& c : decls.type_defs_queue_) {
Myles Watson09617782019-07-02 10:14:47 -0700139 if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM ||
140 c.second->GetDefinitionType() == TypeDef::Type::CHECKSUM) {
141 ((CustomFieldDef*)c.second)->GenInclude(out_file);
142 }
Myles Watsonaf1b2d12019-04-12 10:39:59 -0700143 }
144 out_file << "\n\n";
145
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700146 std::vector<std::string> namespace_list;
Jack Hea7e25ac2019-11-18 18:40:32 -0800147 parse_namespace(root_namespace, gen_relative_path, &namespace_list);
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700148 generate_namespace_open(namespace_list, out_file);
Myles Watsonaf1b2d12019-04-12 10:39:59 -0700149 out_file << "\n\n";
150
Myles Watsonc093d282019-04-18 09:09:01 -0700151 for (const auto& c : decls.type_defs_queue_) {
Myles Watson09617782019-07-02 10:14:47 -0700152 if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM ||
153 c.second->GetDefinitionType() == TypeDef::Type::CHECKSUM) {
154 ((CustomFieldDef*)c.second)->GenUsing(out_file);
155 }
Myles Watsonaf1b2d12019-04-12 10:39:59 -0700156 }
157 out_file << "\n\n";
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700158
159 out_file << "using ::bluetooth::packet::BasePacketBuilder;";
160 out_file << "using ::bluetooth::packet::BitInserter;";
Myles Watsonde15b642019-07-02 12:02:47 -0700161 out_file << "using ::bluetooth::packet::CustomTypeChecker;";
Myles Watson54852262019-02-06 14:02:36 -0800162 out_file << "using ::bluetooth::packet::Iterator;";
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700163 out_file << "using ::bluetooth::packet::kLittleEndian;";
164 out_file << "using ::bluetooth::packet::PacketBuilder;";
Myles Watson54852262019-02-06 14:02:36 -0800165 out_file << "using ::bluetooth::packet::PacketStruct;";
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700166 out_file << "using ::bluetooth::packet::PacketView;";
Myles Watsonc093d282019-04-18 09:09:01 -0700167 out_file << "using ::bluetooth::packet::parser::ChecksumTypeChecker;";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700168 out_file << "\n\n";
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800169
Myles Watsonc093d282019-04-18 09:09:01 -0700170 for (const auto& e : decls.type_defs_queue_) {
171 if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) {
Jack Hea7e25ac2019-11-18 18:40:32 -0800172 const auto* enum_def = dynamic_cast<const EnumDef*>(e.second);
173 EnumGen gen(*enum_def);
Myles Watsonc093d282019-04-18 09:09:01 -0700174 gen.GenDefinition(out_file);
175 out_file << "\n\n";
176 }
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800177 }
Myles Watsonc093d282019-04-18 09:09:01 -0700178 for (const auto& e : decls.type_defs_queue_) {
179 if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) {
Jack Hea7e25ac2019-11-18 18:40:32 -0800180 const auto* enum_def = dynamic_cast<const EnumDef*>(e.second);
181 EnumGen gen(*enum_def);
Myles Watsonc093d282019-04-18 09:09:01 -0700182 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 Hea7e25ac2019-11-18 18:40:32 -0800188 const auto* checksum_def = dynamic_cast<const ChecksumDef*>(ch.second);
189 checksum_def->GenChecksumCheck(out_file);
Myles Watsonc093d282019-04-18 09:09:01 -0700190 }
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800191 }
Myles Watsonde15b642019-07-02 12:02:47 -0700192 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 Hea7e25ac2019-11-18 18:40:32 -0800196 const auto* custom_field_def = dynamic_cast<const CustomFieldDef*>(c.second);
197 custom_field_def->GenCustomFieldCheck(out_file, decls.is_little_endian);
Myles Watsonde15b642019-07-02 12:02:47 -0700198 }
199 }
200 out_file << "\n";
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800201
Myles Watson54852262019-02-06 14:02:36 -0800202 for (auto& s : decls.type_defs_queue_) {
203 if (s.second->GetDefinitionType() == TypeDef::Type::STRUCT) {
Jack Hea7e25ac2019-11-18 18:40:32 -0800204 const auto* struct_def = dynamic_cast<const StructDef*>(s.second);
205 struct_def->GenDefinition(out_file);
Myles Watson54852262019-02-06 14:02:36 -0800206 out_file << "\n";
207 }
208 }
209
Myles Watson945f4c32019-09-25 15:33:39 -0700210 {
211 StructParserGenerator spg(decls);
212 spg.Generate(out_file);
213 out_file << "\n\n";
214 }
215
Jack Hea7e25ac2019-11-18 18:40:32 -0800216 for (const auto& packet_def : decls.packet_defs_queue_) {
217 packet_def.second.GenParserDefinition(out_file);
Hansong Zhang05c079b2019-04-04 13:24:43 -0700218 out_file << "\n\n";
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800219 }
220
Jack Hea7e25ac2019-11-18 18:40:32 -0800221 for (const auto& packet_def : decls.packet_defs_queue_) {
222 packet_def.second.GenBuilderDefinition(out_file);
Hansong Zhang05c079b2019-04-04 13:24:43 -0700223 out_file << "\n\n";
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800224 }
Hansong Zhang05c079b2019-04-04 13:24:43 -0700225
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700226 generate_namespace_close(namespace_list, out_file);
227
Hansong Zhang05c079b2019-04-04 13:24:43 -0700228 out_file.close();
Hansong Zhang05c079b2019-04-04 13:24:43 -0700229
230 return true;
231}
232
Hansong Zhang934cc3d2019-04-04 16:23:32 -0700233} // namespace
234
Steven Moreland64e04502019-09-24 18:37:14 -0700235// TODO(b/141583809): stop leaks
236extern "C" const char* __asan_default_options() {
237 return "detect_leaks=0";
238}
239
Hansong Zhang05c079b2019-04-04 13:24:43 -0700240int main(int argc, const char** argv) {
241 std::filesystem::path out_dir;
242 std::filesystem::path include_dir;
Myles Watsonfd06da02019-10-17 09:12:36 -0700243 std::string root_namespace = "bluetooth";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700244 std::queue<std::filesystem::path> input_files;
245 const std::string arg_out = "--out=";
246 const std::string arg_include = "--include=";
Myles Watsonfd06da02019-10-17 09:12:36 -0700247 const std::string arg_namespace = "--root_namespace=";
Hansong Zhang05c079b2019-04-04 13:24:43 -0700248
249 for (int i = 1; i < argc; i++) {
250 std::string arg = argv[i];
251 if (arg.find(arg_out) == 0) {
Hansong Zhang05c079b2019-04-04 13:24:43 -0700252 out_dir = std::filesystem::current_path() / std::filesystem::path(arg.substr(arg_out.size()));
253 } else if (arg.find(arg_include) == 0) {
Hansong Zhang05c079b2019-04-04 13:24:43 -0700254 include_dir = std::filesystem::current_path() / std::filesystem::path(arg.substr(arg_include.size()));
Myles Watsonfd06da02019-10-17 09:12:36 -0700255 } else if (arg.find(arg_namespace) == 0) {
256 root_namespace = arg.substr(arg_namespace.size());
Hansong Zhang05c079b2019-04-04 13:24:43 -0700257 } 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 Watsonfd06da02019-10-17 09:12:36 -0700262 std::cerr << "Usage: bt-packetgen --out=OUT --include=INCLUDE --root=NAMESPACE input_files..." << std::endl;
Hansong Zhang05c079b2019-04-04 13:24:43 -0700263 return 1;
264 }
265
266 while (!input_files.empty()) {
Jack Hea7e25ac2019-11-18 18:40:32 -0800267 Declarations declarations;
268 if (!parse_declarations_one_file(input_files.front(), &declarations)) {
269 std::cerr << "Cannot parse " << input_files.front() << " correctly" << std::endl;
Hansong Zhang05c079b2019-04-04 13:24:43 -0700270 return 2;
271 }
Jack Hea7e25ac2019-11-18 18:40:32 -0800272 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 Zhang05c079b2019-04-04 13:24:43 -0700276 input_files.pop();
277 }
278
279 return 0;
Ajay Panicker4d6b7602019-02-06 14:02:36 -0800280}