Skip to content

Commit

Permalink
Fixed bug in GIL handling. Improved one example.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdewald committed Aug 18, 2024
1 parent 45134c2 commit e9b5ab8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The format is based on `Keep a Changelog`_, and this project adheres to `Semanti

- (SimpleBluez) Fixed improper handling of non `org.Bluez.Service1` objects within a `org.bluez.Device1` object. *(Thanks Kober Engineering!)*
- (MacOS) Fixed incorrect storage and retrieval with standard Bluetooth UUIDs inside the peripheral class. *(Thanks TellowKrinkle!)*
- (Python) Fixed incorrect handling of the GIL in certain functions. *(Thanks nomenquis and Medra AI!)*


[0.7.X]
Expand Down
3 changes: 3 additions & 0 deletions examples/simplepyble/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@
for characteristic in service.characteristics():
print(f" Characteristic: {characteristic.uuid()}")

capabilities = " ".join(characteristic.capabilities())
print(f" Capabilities: {capabilities}")

peripheral.disconnect()
38 changes: 24 additions & 14 deletions simplepyble/src/wrap_peripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ constexpr auto kDocsPeripheralSetCallbackOnDisconnected = R"pbdoc(
Set callback on disconnected
)pbdoc";

// clang-format off

void wrap_peripheral(py::module& m) {
// TODO: Add __str__ and __repr__ methods
py::class_<SimpleBLE::Peripheral>(m, "Peripheral", kDocsPeripheral)
Expand Down Expand Up @@ -148,43 +150,49 @@ void wrap_peripheral(py::module& m) {
"write_request",
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic, py::bytes payload) {
// Note py::bytes implicitly converts to std::string
p.write_request(service, characteristic, SimpleBLE::ByteArray(payload));
SimpleBLE::ByteArray cpp_payload(payload);
py::gil_scoped_release release;
p.write_request(service, characteristic, cpp_payload);
},
py::call_guard<py::gil_scoped_release>(),
kDocsPeripheralWriteRequest)
.def(
"write_command",
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic, py::bytes payload) {
// Note py::bytes implicitly converts to std::string
p.write_command(service, characteristic, SimpleBLE::ByteArray(payload));
SimpleBLE::ByteArray cpp_payload(payload);
py::gil_scoped_release release;
p.write_command(service, characteristic, cpp_payload);
},
py::call_guard<py::gil_scoped_release>(),
kDocsPeripheralWriteCommand)
.def(
"notify",
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic,
std::function<void(py::bytes payload)> cb) {
p.notify(service, characteristic, [cb](SimpleBLE::ByteArray payload) { cb(py::bytes(payload)); });
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic, std::function<void(py::bytes payload)> cb) {
p.notify(service, characteristic, [cb](SimpleBLE::ByteArray payload) {
py::gil_scoped_acquire gil;
cb(py::bytes(payload));
});
},
kDocsPeripheralNotify)
.def(
"indicate",
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic,
std::function<void(py::bytes payload)> cb) {
p.indicate(service, characteristic, [cb](SimpleBLE::ByteArray payload) { cb(py::bytes(payload)); });
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic, std::function<void(py::bytes payload)> cb) {
p.indicate(service, characteristic, [cb](SimpleBLE::ByteArray payload) {
py::gil_scoped_acquire gil;
cb(py::bytes(payload));
});
},
kDocsPeripheralIndicate)
.def("unsubscribe", &SimpleBLE::Peripheral::unsubscribe, kDocsPeripheralUnsubscribe)

.def(
"descriptor_read",
[](SimpleBLE::Peripheral& p, std::string const& service, std::string const& characteristic,
std::string const& descriptor) { return py::bytes(p.read(service, characteristic, descriptor)); },
[](SimpleBLE::Peripheral& p, std::string const& service, std::string const& characteristic, std::string const& descriptor) {
return py::bytes(p.read(service, characteristic, descriptor));
},
kDocsPeripheralDescriptorRead)
.def(
"descriptor_write",
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic, std::string const& descriptor,
py::bytes payload) {
[](SimpleBLE::Peripheral& p, std::string service, std::string characteristic, std::string const& descriptor, py::bytes payload) {
// Note py::bytes implicitly converts to std::string
p.write(service, characteristic, descriptor, SimpleBLE::ByteArray(payload));
},
Expand All @@ -195,3 +203,5 @@ void wrap_peripheral(py::module& m) {
.def("set_callback_on_disconnected", &SimpleBLE::Peripheral::set_callback_on_disconnected,
py::keep_alive<1, 2>(), kDocsPeripheralSetCallbackOnDisconnected);
}

// clang-format on

0 comments on commit e9b5ab8

Please sign in to comment.