Skip to content

Commit

Permalink
reset, down, and up bluetooth HCI device in an attempt to get it more…
Browse files Browse the repository at this point in the history
… consistent when reading h5074 devices
  • Loading branch information
William C Bonner committed Nov 9, 2023
1 parent 2ca2c26 commit 169a2a5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ if (POLICY CMP0141)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

if (POLICY CMP0115)
cmake_policy(SET CMP0115 NEW)
endif()

project (GoveeBTTempLogger
VERSION 2.20231109.1
VERSION 2.20231109.2
DESCRIPTION "Listen and log Govee Thermometer Bluetooth Low Energy Advertisments"
HOMEPAGE_URL https://github.com/wcbonner/GoveeBTTempLogger
)
Expand Down
45 changes: 45 additions & 0 deletions goveebttemplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,51 @@ int main(int argc, char **argv)
std::cerr << "[ ] Error: Cannot open device: " << strerror(errno) << std::endl;
else
{
// (2023-11-09) I'm resetting, downing, and upping the device in an attempt to have the device always in the same state as if I'd powered off the pi.
// Reset HCI device
if (ioctl(BlueToothDevice_Handle, HCIDEVRESET, BlueToothDevice_ID) < 0)
{
if (ConsoleVerbosity > 0)
std::cout << "[ ] Error: Reset failed device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
else
std::cerr << "Error: Reset failed device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
}
else
if (ConsoleVerbosity > 0)
std::cout << "[ ] Reset device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;

// Stop HCI device
if (ioctl(BlueToothDevice_Handle, HCIDEVDOWN, BlueToothDevice_ID) < 0)
{
if (ConsoleVerbosity > 0)
std::cout << "[ ] Error: Cannot down device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
else
std::cerr << "Error: Cannot down device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
}
else
if (ConsoleVerbosity > 0)
std::cout << "[ ] DOWN device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;

// Start HCI device
if (ioctl(BlueToothDevice_Handle, HCIDEVUP, BlueToothDevice_ID) < 0)
{
if (errno == EALREADY)
{
if (ConsoleVerbosity > 0)
std::cout << "[ ] Already UP device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
else
std::cerr << "Already UP device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
}
else
if (ConsoleVerbosity > 0)
std::cout << "[ ] Error: Cannot init device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
else
std::cerr << "Error: Cannot init device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;
}
else
if (ConsoleVerbosity > 0)
std::cout << "[ ] UP device: hci" << BlueToothDevice_ID << ". " << strerror(errno) << "(" << errno << ")" << std::endl;

int on = 1; // Nonblocking on = 1, off = 0;
if (ioctl(BlueToothDevice_Handle, FIONBIO, (char*)&on) < 0)
std::cerr << "[ ] Error: Could set device to non-blocking: " << strerror(errno) << std::endl;
Expand Down

0 comments on commit 169a2a5

Please sign in to comment.