I have a C++ program that statically links against libbluetooth
/BlueZ
, and I would like to port it to Rust as an exercise.
One particularly ugly bit of the C++ code reads data from a UNIX file descriptor via read()
, and the resulting buffer is then cast to a struct via reinterpret_cast
. Unfortunately, I have no idea how to achieve a similar thing in Rust. The idea is to capture instances of le_advertising_info
from libbluetooth
.
C++11 Code:
std::uint8_t buf [HCI_MAX_EVENT_SIZE];
evt_le_meta_event* evt;
le_advertising_info* info;
if (read(_deviceFD, buf, sizeof (buf)) >= HCI_EVENT_HDR_SIZE) {
evt = reinterpret_cast<evt_le_meta_event*>(buf + HCI_EVENT_HDR_SIZE + 1);
if (evt != nullptr && evt->subevent == EVT_LE_ADVERTISING_REPORT) {
void* offset = evt->data + 1;
for (auto i = 0; i < evt->data [0]; i++) {
info = reinterpret_cast<le_advertising_info*>(offset);
if (info != nullptr) {
if (isBeacon(*info)) {
writeLogEntry(*info);
}
offset = info->data + info->length + 2;
}
}
}
}
Some pointers on how to port this bit to Rust (in an elegant and safe fashion) are greatly appreciated.