1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstring>
#include <memory>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "hci/address.h"
#include "hci/class_of_device.h"
#include "packet/base_packet_builder.h"
#include "packet/bit_inserter.h"
#include "packet/iterator.h"
#include "packet/packet_builder.h"
#include "packet/packet_struct.h"
#include "packet/packet_view.h"
#include "packet/parser/checksum_type_checker.h"
#include "packet/parser/custom_type_checker.h"
#include "packet/raw_builder.h"
namespace py = pybind11;
namespace bluetooth {
namespace hci {
void define_hci_packets_submodule(py::module&);
}
namespace l2cap {
void define_l2cap_packets_submodule(py::module&);
}
namespace security {
void define_smp_packets_submodule(py::module&);
}
namespace packet {
using ::bluetooth::hci::Address;
using ::bluetooth::hci::ClassOfDevice;
using ::bluetooth::packet::BasePacketBuilder;
using ::bluetooth::packet::BaseStruct;
using ::bluetooth::packet::BitInserter;
using ::bluetooth::packet::CustomTypeChecker;
using ::bluetooth::packet::Iterator;
using ::bluetooth::packet::kLittleEndian;
using ::bluetooth::packet::PacketBuilder;
using ::bluetooth::packet::PacketStruct;
using ::bluetooth::packet::PacketView;
using ::bluetooth::packet::RawBuilder;
using ::bluetooth::packet::parser::ChecksumTypeChecker;
PYBIND11_MODULE(bluetooth_packets_python3, m) {
py::class_<BasePacketBuilder, std::shared_ptr<BasePacketBuilder>>(m, "BasePacketBuilder");
py::class_<RawBuilder, BasePacketBuilder, std::shared_ptr<RawBuilder>>(m, "RawBuilder")
.def(py::init([](std::vector<uint8_t> bytes) { return std::make_unique<RawBuilder>(bytes); }))
.def(py::init([](std::string bytes) {
return std::make_unique<RawBuilder>(std::vector<uint8_t>(bytes.begin(), bytes.end()));
}))
.def("Serialize", [](RawBuilder& builder) {
std::vector<uint8_t> packet;
BitInserter it(packet);
builder.Serialize(it);
std::string result = std::string(packet.begin(), packet.end());
return py::bytes(result);
});
py::class_<PacketBuilder<kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<kLittleEndian>>>(
m, "PacketBuilderLittleEndian");
py::class_<PacketBuilder<!kLittleEndian>, BasePacketBuilder, std::shared_ptr<PacketBuilder<!kLittleEndian>>>(
m, "PacketBuilderBigEndian");
py::class_<BaseStruct, std::shared_ptr<BaseStruct>>(m, "BaseStruct");
py::class_<PacketStruct<kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<kLittleEndian>>>(
m, "PacketStructLittleEndian");
py::class_<PacketStruct<!kLittleEndian>, BaseStruct, std::shared_ptr<PacketStruct<!kLittleEndian>>>(
m, "PacketStructBigEndian");
py::class_<Iterator<kLittleEndian>>(m, "IteratorLittleEndian");
py::class_<Iterator<!kLittleEndian>>(m, "IteratorBigEndian");
py::class_<PacketView<kLittleEndian>>(m, "PacketViewLittleEndian")
.def(py::init([](std::vector<uint8_t> bytes) {
// Make a copy
auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes);
return std::make_unique<PacketView<kLittleEndian>>(bytes_shared);
}))
.def("GetBytes", [](const PacketView<kLittleEndian> view) {
std::string result;
for (auto byte : view) {
result += byte;
}
return py::bytes(result);
});
py::class_<PacketView<!kLittleEndian>>(m, "PacketViewBigEndian").def(py::init([](std::vector<uint8_t> bytes) {
// Make a copy
auto bytes_shared = std::make_shared<std::vector<uint8_t>>(bytes);
return std::make_unique<PacketView<!kLittleEndian>>(bytes_shared);
}));
py::module hci_m = m.def_submodule("hci_packets", "A submodule of hci_packets");
bluetooth::hci::define_hci_packets_submodule(hci_m);
py::class_<Address>(hci_m, "Address")
.def(py::init<>())
.def("__repr__", [](const Address& a) { return a.ToString(); })
.def("__str__", [](const Address& a) { return a.ToString(); });
py::class_<ClassOfDevice>(hci_m, "ClassOfDevice")
.def(py::init<>())
.def("__repr__", [](const ClassOfDevice& c) { return c.ToString(); })
.def("__str__", [](const ClassOfDevice& c) { return c.ToString(); });
py::module l2cap_m = m.def_submodule("l2cap_packets", "A submodule of l2cap_packets");
bluetooth::l2cap::define_l2cap_packets_submodule(l2cap_m);
py::module security_m = m.def_submodule("security_packets", "A submodule of security_packets");
bluetooth::security::define_smp_packets_submodule(security_m);
}
} // namespace packet
} // namespace bluetooth
|