diff --git a/CMakeLists.txt b/CMakeLists.txt index 3012972..7f45220 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,8 +16,12 @@ if (POLICY CMP0141) set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$,$>,$<$:EditAndContinue>,$<$: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 ) diff --git a/goveebttemplogger.cpp b/goveebttemplogger.cpp index 94e99f7..b9e98e1 100644 --- a/goveebttemplogger.cpp +++ b/goveebttemplogger.cpp @@ -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;