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 #include <cstring> 17 #include <memory> 18 19 #include <pybind11/pybind11.h> 20 #include <pybind11/stl.h> 21 22 #include "hci/address.h" 23 #include "hci/class_of_device.h" 24 #include "packet/base_packet_builder.h" 25 #include "packet/bit_inserter.h" 26 #include "packet/iterator.h" 27 #include "packet/packet_builder.h" 28 #include "packet/packet_struct.h" 29 #include "packet/packet_view.h" 30 #include "packet/parser/checksum_type_checker.h" 31 #include "packet/parser/custom_type_checker.h" 32 33 namespace py = pybind11; 34 35 namespace bluetooth { 36 37 namespace hci { 38 void define_hci_packets_submodule(py::module&); 39 } 40 namespace l2cap { 41 void define_l2cap_packets_submodule(py::module&); 42 } 43 namespace security { 44 void define_smp_packets_submodule(py::module&); 45 } 46 47 namespace packet { 48 49 using ::bluetooth::hci::Address; 50 using ::bluetooth::hci::ClassOfDevice; 51 using ::bluetooth::packet::BasePacketBuilder; 52 using ::bluetooth::packet::BaseStruct; 53 using ::bluetooth::packet::BitInserter; 54 using ::bluetooth::packet::CustomTypeChecker; 55 using ::bluetooth::packet::Iterator; 56 using ::bluetooth::packet::kLittleEndian; 57 using ::bluetooth::packet::PacketBuilder; 58 using ::bluetooth::packet::PacketStruct; 59 using ::bluetooth::packet::PacketView; 60 using ::bluetooth::packet::parser::ChecksumTypeChecker; 61 62 PYBIND11_MODULE(bluetooth_packets_python3, m) { 63 py::class_<BasePacketBuilder, std::shared_ptr<BasePacketBuilder>>(m, "BasePacketBuilder"); 64 py::class_<PacketBuilder<kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<kLittleEndian>>>( 65 m, "PacketBuilderLittleEndian"); 66 py::class_<PacketBuilder<!kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<!kLittleEndian>>>( 67 m, "PacketBuilderBigEndian"); 68 py::class_<BaseStruct, std::shared_ptr<BaseStruct>>(m, "BaseStruct"); 69 py::class_<PacketStruct<kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<kLittleEndian>>>( 70 m, "PacketStructLittleEndian"); 71 py::class_<PacketStruct<!kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<!kLittleEndian>>>( 72 m, "PacketStructBigEndian"); 73 py::class_<Iterator<kLittleEndian>>(m, "IteratorLittleEndian"); 74 py::class_<Iterator<!kLittleEndian>>(m, "IteratorBigEndian"); 75 py::class_<PacketView<kLittleEndian>>(m, "PacketViewLittleEndian").def(py::init([](std::vector<uint8_t> bytes) { 76 // Make a copy 77 auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes); 78 return std::make_unique<PacketView<kLittleEndian>>(bytes_shared); 79 })); 80 py::class_<PacketView<!kLittleEndian>>(m, "PacketViewBigEndian").def(py::init([](std::vector<uint8_t> bytes) { 81 // Make a copy 82 auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes); 83 return std::make_unique<PacketView<!kLittleEndian>>(bytes_shared); 84 })); 85 86 py::module hci_m = m.def_submodule("hci_packets", "A submodule of hci_packets"); 87 bluetooth::hci::define_hci_packets_submodule(hci_m); 88 89 py::class_<Address>(hci_m, "Address") 90 .def(py::init<>()) 91 .def("__repr__", [](const Address& a) { return a.ToString(); }) 92 .def("__str__", [](const Address& a) { return a.ToString(); }); 93 94 py::class_<ClassOfDevice>(hci_m, "ClassOfDevice") 95 .def(py::init<>()) 96 .def("__repr__", [](const ClassOfDevice& c) { return c.ToString(); }) 97 .def("__str__", [](const ClassOfDevice& c) { return c.ToString(); }); 98 99 py::module l2cap_m = m.def_submodule("l2cap_packets", "A submodule of l2cap_packets"); 100 bluetooth::l2cap::define_l2cap_packets_submodule(l2cap_m); 101 py::module security_m = m.def_submodule("security_packets", "A submodule of security_packets"); 102 bluetooth::security::define_smp_packets_submodule(security_m); 103 } 104 105 } // namespace packet 106 } // namespace bluetooth 107