• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(py::init([](std::string bytes) {
69         return std::make_unique<RawBuilder>(std::vector<uint8_t>(bytes.begin(), bytes.end()));
70       }))
71       .def("Serialize", [](RawBuilder& builder) {
72         std::vector<uint8_t> packet;
73         BitInserter it(packet);
74         builder.Serialize(it);
75         std::string result = std::string(packet.begin(), packet.end());
76         return py::bytes(result);
77       });
78   py::class_<PacketBuilder<kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<kLittleEndian>>>(
79       m, "PacketBuilderLittleEndian");
80   py::class_<PacketBuilder<!kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<!kLittleEndian>>>(
81       m, "PacketBuilderBigEndian");
82   py::class_<BaseStruct, std::shared_ptr<BaseStruct>>(m, "BaseStruct");
83   py::class_<PacketStruct<kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<kLittleEndian>>>(
84       m, "PacketStructLittleEndian");
85   py::class_<PacketStruct<!kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<!kLittleEndian>>>(
86       m, "PacketStructBigEndian");
87   py::class_<Iterator<kLittleEndian>>(m, "IteratorLittleEndian");
88   py::class_<Iterator<!kLittleEndian>>(m, "IteratorBigEndian");
89   py::class_<PacketView<kLittleEndian>>(m, "PacketViewLittleEndian")
90       .def(py::init([](std::vector<uint8_t> bytes) {
91         // Make a copy
92         auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes);
93         return std::make_unique<PacketView<kLittleEndian>>(bytes_shared);
94       }))
95       .def("GetBytes", [](const PacketView<kLittleEndian> view) {
96         std::string result;
97         for (auto byte : view) {
98           result += byte;
99         }
100         return py::bytes(result);
101       });
102   py::class_<PacketView<!kLittleEndian>>(m, "PacketViewBigEndian").def(py::init([](std::vector<uint8_t> bytes) {
103     // Make a copy
104     auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes);
105     return std::make_unique<PacketView<!kLittleEndian>>(bytes_shared);
106   }));
107 
108   py::module hci_m = m.def_submodule("hci_packets", "A submodule of hci_packets");
109   bluetooth::hci::define_hci_packets_submodule(hci_m);
110 
111   py::class_<Address>(hci_m, "Address")
112       .def(py::init<>())
113       .def("__repr__", [](const Address& a) { return a.ToString(); })
114       .def("__str__", [](const Address& a) { return a.ToString(); });
115 
116   py::class_<ClassOfDevice>(hci_m, "ClassOfDevice")
117       .def(py::init<>())
118       .def("__repr__", [](const ClassOfDevice& c) { return c.ToString(); })
119       .def("__str__", [](const ClassOfDevice& c) { return c.ToString(); });
120 
121   py::module l2cap_m = m.def_submodule("l2cap_packets", "A submodule of l2cap_packets");
122   bluetooth::l2cap::define_l2cap_packets_submodule(l2cap_m);
123   py::module security_m = m.def_submodule("security_packets", "A submodule of security_packets");
124   bluetooth::security::define_smp_packets_submodule(security_m);
125 }
126 
127 }  // namespace packet
128 }  // namespace bluetooth
129