],
}
+genrule {
+ name: "BluetoothGeneratedPackets_rust",
+ tools: [
+ "bluetooth_packetgen",
+ ],
+ cmd: "$(location bluetooth_packetgen) --include=system/bt/gd --out=$(genDir) $(in) --rust",
+ srcs: [
+ "hci/hci_packets.pdl",
+ ],
+ out: [
+ "hci/hci_packets.rs",
+ ],
+}
+
+rust_library {
+ name: "libbt_packets",
+ crate_name: "bt_packets",
+ srcs: ["rust/packets/lib.rs", ":BluetoothGeneratedPackets_rust"],
+ edition: "2018",
+ host_supported: true,
+}
+
// Generates binary schema data to be bundled and source file generated
genrule {
name: "BluetoothGeneratedDumpsysBinarySchema_bfbs",
_payload_,
}
-enum qualityReportId : 8 {
+enum QualityReportId : 8 {
MONITOR_MODE = 0x01,
APPROACH_LSTO = 0x02,
A2DP_AUDIO_CHOPPY = 0x03,
}
packet BqrEvent : VendorSpecificEvent (subevent_code = BQR_EVENT) {
- quality_report_id : qualityReportId,
+ quality_report_id : QualityReportId,
_payload_,
}
stream << " return os << " << e_.name_ << "Text(param);";
stream << "}\n";
}
+
+void EnumGen::GenRustDef(std::ostream& stream) {
+ stream << "pub enum " << e_.name_ << " {";
+ for (const auto& pair : e_.constants_) {
+ stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ",";
+ }
+ stream << "}";
+}
void GenLogging(std::ostream& stream);
+ void GenRustDef(std::ostream& stream);
+
EnumDef e_;
};
#include "declarations.h"
bool generate_rust_source_one_file(
- __attribute__((unused)) const Declarations& decls,
+ const Declarations& decls,
const std::filesystem::path& input_file,
const std::filesystem::path& include_dir,
const std::filesystem::path& out_dir,
return false;
}
- out_file << "// @generated rust packets from " << input_file.filename().string();
+ out_file << "// @generated rust packets from " << input_file.filename().string() << "\n\n";
+
+ for (const auto& e : decls.type_defs_queue_) {
+ if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) {
+ const auto* enum_def = dynamic_cast<const EnumDef*>(e.second);
+ EnumGen gen(*enum_def);
+ gen.GenRustDef(out_file);
+ out_file << "\n\n";
+ }
+ }
out_file.close();
return true;
return camel_case.str();
}
+inline std::string ConstantCaseToCamelCase(std::string value) {
+ if (value[0] < 'A' || value[0] > 'Z') {
+ ERROR() << value << " doesn't look like CONSTANT_CASE";
+ }
+
+ std::ostringstream camel_case;
+
+ bool capitalize = true;
+ for (unsigned char c : value) {
+ if (c == '_') {
+ capitalize = true;
+ } else {
+ if (capitalize) {
+ c = std::toupper(c);
+ capitalize = false;
+ } else {
+ c = std::tolower(c);
+ }
+ camel_case << c;
+ }
+ }
+
+ return camel_case.str();
+}
+
inline bool IsEnumCase(std::string value) {
if (value[0] < 'A' || value[0] > 'Z') {
return false;
"libgrpcio",
"libtokio",
"libprotobuf",
+ "libbt_packets",
],
host_supported: true,
}
--- /dev/null
+//! reimport of generated packets (to go away once rust_genrule exists)
+
+#![allow(clippy::all)]
+#![allow(unused)]
+#![allow(missing_docs)]
+
+pub mod hci {
+ include!(concat!(env!("OUT_DIR"), "/hci_packets.rs"));
+}