• 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 <pybind11/pybind11.h>
17 #include <pybind11/stl.h>
18 
19 #include <cstring>
20 #include <memory>
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/checksum_type_checker.h"
27 #include "packet/custom_type_checker.h"
28 #include "packet/iterator.h"
29 #include "packet/packet_builder.h"
30 #include "packet/packet_struct.h"
31 #include "packet/packet_view.h"
32 #include "packet/raw_builder.h"
33 
34 namespace py = pybind11;
35 
36 namespace bluetooth {
37 
38 namespace l2cap {
39 void define_l2cap_packets_submodule(py::module&);
40 }
41 namespace security {
42 void define_smp_packets_submodule(py::module&);
43 }
44 
45 namespace packet {
46 
47 using ::bluetooth::hci::Address;
48 using ::bluetooth::hci::ClassOfDevice;
49 using ::bluetooth::packet::BasePacketBuilder;
50 using ::bluetooth::packet::BaseStruct;
51 using ::bluetooth::packet::BitInserter;
52 using ::bluetooth::packet::CustomTypeChecker;
53 using ::bluetooth::packet::Iterator;
54 using ::bluetooth::packet::kLittleEndian;
55 using ::bluetooth::packet::PacketBuilder;
56 using ::bluetooth::packet::PacketStruct;
57 using ::bluetooth::packet::PacketView;
58 using ::bluetooth::packet::RawBuilder;
59 using ::bluetooth::packet::parser::ChecksumTypeChecker;
60 
PYBIND11_MODULE(bluetooth_packets_python3,m)61 PYBIND11_MODULE(bluetooth_packets_python3, m) {
62   py::class_<BasePacketBuilder, std::shared_ptr<BasePacketBuilder>>(m, "BasePacketBuilder");
63   py::class_<RawBuilder, BasePacketBuilder, std::shared_ptr<RawBuilder>>(m, "RawBuilder")
64       .def(py::init([](std::vector<uint8_t> bytes) { return std::make_unique<RawBuilder>(bytes); }))
65       .def(py::init([](std::string bytes) {
66         return std::make_unique<RawBuilder>(std::vector<uint8_t>(bytes.begin(), bytes.end()));
67       }))
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 byte : view) {
95           result += byte;
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 l2cap_m = m.def_submodule("l2cap_packets", "A submodule of l2cap_packets");
106   bluetooth::l2cap::define_l2cap_packets_submodule(l2cap_m);
107   py::module security_m = m.def_submodule("security_packets", "A submodule of security_packets");
108   bluetooth::security::define_smp_packets_submodule(security_m);
109 }
110 
111 }  // namespace packet
112 }  // namespace bluetooth
113