Skip to content

Commit

Permalink
add error reporting and clean up some levels
Browse files Browse the repository at this point in the history
  • Loading branch information
William C Bonner committed Nov 20, 2024
1 parent 8d911ba commit 46be252
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ endif()
add_compile_definitions(_BLUEZ_HCI_)

project (GoveeBTTempLogger
VERSION 3.20241118.0
VERSION 3.20241120.0
DESCRIPTION "Listen and log Govee Thermometer Bluetooth Low Energy Advertisments via BlueZ and DBus"
HOMEPAGE_URL https://github.com/wcbonner/GoveeBTTempLogger
)
Expand Down
90 changes: 72 additions & 18 deletions goveebttemplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,8 @@ bool bluez_power_on(DBusConnection* dbus_conn, const char* adapter_path, const b
{
if (ConsoleVerbosity > 0)
ssOutput << "[ ] ";
ssOutput << "Can't allocate dbus_message_new_method_call: " << __FILE__ << "(" << __LINE__ << ")" << std::endl;
ssOutput << "Can't allocate dbus_message_new_method_call: " << __FILE__ << "(" << __LINE__ << ")";
rVal = false;
}
else
{
Expand Down Expand Up @@ -3668,12 +3669,11 @@ bool bluez_power_on(DBusConnection* dbus_conn, const char* adapter_path, const b
else
dbus_message_unref(dbus_reply);
dbus_message_unref(dbus_msg); // https://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gab69441efe683918f6a82469c8763f464
ssOutput << std::endl;
}
if (ConsoleVerbosity > 0)
std::cout << ssOutput.str();
else
std::cerr << ssOutput.str();
if (ConsoleVerbosity > 1)
std::cout << ssOutput.str() << std::endl;
else if (!rVal)
std::cerr << ssOutput.str() << std::endl;
return(rVal);
}
bool bluez_filter_le(DBusConnection* dbus_conn, const char* adapter_path, const bool DuplicateData = true, const bool bFilter = true)
Expand Down Expand Up @@ -3757,9 +3757,9 @@ bool bluez_filter_le(DBusConnection* dbus_conn, const char* adapter_path, const
dbus_message_unref(dbus_msg);
ssOutput << std::endl;
}
if (ConsoleVerbosity > 0)
if (ConsoleVerbosity > 1)
std::cout << ssOutput.str();
else
else if (!rVal)
std::cerr << ssOutput.str();
return(rVal);
}
Expand Down Expand Up @@ -4231,6 +4231,7 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
{
int rVal(0);
time_t TimeStart(0), TimeLog(0), TimeSVG(0);
std::ostringstream ssOutput;
// Main loop
bRun = true;
while (bRun)
Expand All @@ -4242,25 +4243,40 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
DBusConnection* dbus_conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga77ba5250adb84620f16007e1b023cf26
if (dbus_error_is_set(&dbus_error)) // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#gab0ed62e9fc2685897eb2d41467c89405
{
std::cout << "[" << getTimeISO8601(true) << "] Error connecting to the D-Bus system bus: " << dbus_error.message << std::endl;
if (ConsoleVerbosity > 0)
ssOutput << "[" << getTimeISO8601(true) << "] ";
ssOutput << "Error connecting to the D-Bus system bus: " << dbus_error.message;
dbus_error_free(&dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#gaac6c14ead14829ee4e090f39de6a7568
if (ConsoleVerbosity > 0)
std::cout << ssOutput.str() << std::endl;
else
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
}
else
{
if (ConsoleVerbosity > 0)
std::cout << "[" << getTimeISO8601(true) << "] D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Opened" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b
ssOutput << "[" << getTimeISO8601(true) << "] ";
ssOutput << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Opened"; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b
if (ConsoleVerbosity > 0)
std::cout << ssOutput.str() << std::endl;
else
std::cerr << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Opened" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
std::map<bdaddr_t, std::string> BlueZAdapterMap;
bluez_find_adapters(dbus_conn, BlueZAdapterMap);
if (BlueZAdapterMap.empty())
{
bRun = false;
rVal = 1;
if (ConsoleVerbosity > 0)
std::cout << "[" << getTimeISO8601(true) << "] Could not get list of adapters from BlueZ over DBus. Reverting to HCI interface." << std::endl;
ssOutput << "[" << getTimeISO8601(true) << "] ";
ssOutput << "Could not get list of adapters from BlueZ over DBus. Reverting to HCI interface.";
if (ConsoleVerbosity > 0)
std::cout << ssOutput.str() << std::endl;
else
std::cerr << "Could not get list of adapters from BlueZ over DBus. Reverting to HCI interface." << std::endl;
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
}
else
{
Expand All @@ -4281,13 +4297,23 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
MatchRules.push_back("type='signal',sender='org.bluez',member='PropertiesChanged'");
for (auto& MatchRule : MatchRules)
{
if (ConsoleVerbosity > 0)
ssOutput << "[ ] ";
ssOutput << "Add Match Rule: \"" << MatchRule << "\" ";
bool MatchRuleError(false);
dbus_error_init(&dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#ga8937f0b7cdf8554fa6305158ce453fbe
dbus_bus_add_match(dbus_conn, MatchRule.c_str(), &dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga4eb6401ba014da3dbe3dc4e2a8e5b3ef
if (dbus_error_is_set(&dbus_error))
{
std::cout << "Error adding a match rule on the D-Bus system bus: " << dbus_error.message << std::endl;
ssOutput << "Error adding a match rule on the D-Bus system bus: " << dbus_error.message;
dbus_error_free(&dbus_error);
MatchRuleError = true;
}
if (ConsoleVerbosity > 1)
std::cout << ssOutput.str() << std::endl;
else if (MatchRuleError)
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
}
time_t TimeNow(0);
do
Expand All @@ -4297,9 +4323,13 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
{
time(&TimeNow);
if (ConsoleVerbosity > 0)
std::cout << "[" << timeToISO8601(TimeNow, true) << "] D-Bus connection Closed" << std::endl;
ssOutput << "[" << timeToISO8601(TimeNow, true) << "] ";
ssOutput << "D-Bus connection Closed";
if (ConsoleVerbosity > 0)
std::cout << ssOutput.str() << std::endl;
else
std::cerr << "D-Bus connection Closed" << std::endl;
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
bRun = false;
}
else
Expand Down Expand Up @@ -4382,6 +4412,26 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
#else
} while (bRun && difftime(TimeNow, TimeStart) < (60 * 30)); // Maintain DBus connection for no more than 30 minutes
#endif // DEBUG
for (auto& MatchRule : MatchRules)
{
if (ConsoleVerbosity > 0)
ssOutput << "[ ] ";
ssOutput << "Remove Match Rule: \"" << MatchRule << "\" ";
bool MatchRuleError(false);
dbus_error_init(&dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#ga8937f0b7cdf8554fa6305158ce453fbe
dbus_bus_remove_match(dbus_conn, MatchRule.c_str(), &dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga4eb6401ba014da3dbe3dc4e2a8e5b3ef
if (dbus_error_is_set(&dbus_error))
{
ssOutput << "Error removing a match rule on the D-Bus system bus: " << dbus_error.message;
dbus_error_free(&dbus_error);
MatchRuleError = true;
}
if (ConsoleVerbosity > 1)
std::cout << ssOutput.str() << std::endl;
else if (MatchRuleError)
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
}
bluez_discovery(dbus_conn, BlueZAdapter.c_str(), false);
bluez_dbus_RemoveKnownDevices(dbus_conn, BlueZAdapter.c_str(), GoveeThermometers);
//bluez_filter_le(dbus_conn, BlueZAdapter.c_str(), false, false); // remove discovery filter
Expand All @@ -4391,9 +4441,13 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
usleep(1000000); // 1,000,000 = 1 second.
}
if (ConsoleVerbosity > 0)
std::cout << "[" << getTimeISO8601(true) << "] D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Closed" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b
ssOutput << "[" << getTimeISO8601(true) << "] ";
ssOutput << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Closed"; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b
if (ConsoleVerbosity > 0)
std::cout << ssOutput.str() << std::endl;
else
std::cerr << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Closed" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b
std::cerr << ssOutput.str() << std::endl;
ssOutput = std::ostringstream(); // reinitialize my output stringstream
dbus_connection_close(dbus_conn); // https://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga2522ac5075dfe0a1535471f6e045e1ee
dbus_connection_unref(dbus_conn); // https://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga6385ff09bc108238c4429e7c195dab25
}
Expand Down

0 comments on commit 46be252

Please sign in to comment.