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 #include "packet/raw_builder.h"
33
34 namespace py = pybind11;
35
36 namespace bluetooth {
37
38 namespace hci {
39 void define_hci_packets_submodule(py::module&);
40 }
41 namespace l2cap {
42 void define_l2cap_packets_submodule(py::module&);
43 }
44 namespace security {
45 void define_smp_packets_submodule(py::module&);
46 }
47
48 namespace packet {
49
50 using ::bluetooth::hci::Address;
51 using ::bluetooth::hci::ClassOfDevice;
52 using ::bluetooth::packet::BasePacketBuilder;
53 using ::bluetooth::packet::BaseStruct;
54 using ::bluetooth::packet::BitInserter;
55 using ::bluetooth::packet::CustomTypeChecker;
56 using ::bluetooth::packet::Iterator;
57 using ::bluetooth::packet::kLittleEndian;
58 using ::bluetooth::packet::PacketBuilder;
59 using ::bluetooth::packet::PacketStruct;
60 using ::bluetooth::packet::PacketView;
61 using ::bluetooth::packet::RawBuilder;
62 using ::bluetooth::packet::parser::ChecksumTypeChecker;
63
PYBIND11_MODULE(bluetooth_packets_python3,m)64 PYBIND11_MODULE(bluetooth_packets_python3, m) {
65 py::class_<BasePacketBuilder, std::shared_ptr<BasePacketBuilder>>(m, "BasePacketBuilder");
66 py::class_<RawBuilder, BasePacketBuilder, std::shared_ptr<RawBuilder>>(m, "RawBuilder")
67 .def(py::init([](std::vector<uint8_t> bytes) { return std::make_unique<RawBuilder>(bytes); }))
68 .def("Serialize", [](RawBuilder& builder) {
69 std::vector<uint8_t> packet;
70 BitInserter it(packet);
71 builder.Serialize(it);
72 std::string result = std::string(packet.begin(), packet.end());
73 return py::bytes(result);
74 });
75 py::class_<PacketBuilder<kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<kLittleEndian>>>(
76 m, "PacketBuilderLittleEndian");
77 py::class_<PacketBuilder<!kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<!kLittleEndian>>>(
78 m, "PacketBuilderBigEndian");
79 py::class_<BaseStruct, std::shared_ptr<BaseStruct>>(m, "BaseStruct");
80 py::class_<PacketStruct<kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<kLittleEndian>>>(
81 m, "PacketStructLittleEndian");
82 py::class_<PacketStruct<!kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<!kLittleEndian>>>(
83 m, "PacketStructBigEndian");
84 py::class_<Iterator<kLittleEndian>>(m, "IteratorLittleEndian");
85 py::class_<Iterator<!kLittleEndian>>(m, "IteratorBigEndian");
86 py::class_<PacketView<kLittleEndian>>(m, "PacketViewLittleEndian")
87 .def(py::init([](std::vector<uint8_t> bytes) {
88 // Make a copy
89 auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes);
90 return std::make_unique<PacketView<kLittleEndian>>(bytes_shared);
91 }))
92 .def("GetBytes", [](const PacketView<kLittleEndian> view) {
93 std::string result;
94 for (auto it = view.begin(); it != view.end(); it++) {
95 result += *it;
96 }
97 return py::bytes(result);
98 });
99 py::class_<PacketView<!kLittleEndian>>(m, "PacketViewBigEndian").def(py::init([](std::vector<uint8_t> bytes) {
100 // Make a copy
101 auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes);
102 return std::make_unique<PacketView<!kLittleEndian>>(bytes_shared);
103 }));
104
105 py::module hci_m = m.def_submodule("hci_packets", "A submodule of hci_packets");
106 bluetooth::hci::define_hci_packets_submodule(hci_m);
107
108 py::class_<Address>(hci_m, "Address")
109 .def(py::init<>())
110 .def("__repr__", [](const Address& a) { return a.ToString(); })
111 .def("__str__", [](const Address& a) { return a.ToString(); });
112
113 py::class_<ClassOfDevice>(hci_m, "ClassOfDevice")
114 .def(py::init<>())
115 .def("__repr__", [](const ClassOfDevice& c) { return c.ToString(); })
116 .def("__str__", [](const ClassOfDevice& c) { return c.ToString(); });
117
118 py::module l2cap_m = m.def_submodule("l2cap_packets", "A submodule of l2cap_packets");
119 bluetooth::l2cap::define_l2cap_packets_submodule(l2cap_m);
120 py::module security_m = m.def_submodule("security_packets", "A submodule of security_packets");
121 bluetooth::security::define_smp_packets_submodule(security_m);
122 }
123
124 } // namespace packet
125 } // namespace bluetooth
126