From 322aeccbe9065863246078da3a945e8f9983d2c5 Mon Sep 17 00:00:00 2001 From: Paul Nettle Date: Fri, 1 Sep 2017 13:23:34 -0500 Subject: Cleaned up the logging a bit, improved server termination status returned from ggkWait. --- src/DBusInterface.cpp | 4 ++ src/DBusInterface.h | 4 ++ src/DBusMethod.cpp | 4 ++ src/DBusMethod.h | 6 ++- src/DBusObject.cpp | 4 ++ src/DBusObject.h | 4 ++ src/DBusObjectPath.h | 4 ++ src/GattCharacteristic.cpp | 6 ++- src/GattCharacteristic.h | 4 ++ src/GattDescriptor.cpp | 6 ++- src/GattDescriptor.h | 4 ++ src/GattInterface.cpp | 4 ++ src/GattInterface.h | 4 ++ src/GattProperty.cpp | 4 ++ src/GattProperty.h | 4 ++ src/GattService.cpp | 4 ++ src/GattService.h | 4 ++ src/GattUuid.h | 4 ++ src/Gobbledegook.cpp | 114 ++++++++++++++++++++++++++++++++++----------- src/HciAdapter.cpp | 8 +++- src/HciAdapter.h | 4 ++ src/HciSocket.cpp | 4 ++ src/HciSocket.h | 4 ++ src/Init.cpp | 55 +++++++++++----------- src/Init.h | 4 ++ src/Logger.cpp | 68 ++++++++++++++------------- src/Logger.h | 4 ++ src/Mgmt.cpp | 23 +++++++-- src/Mgmt.h | 4 ++ src/Server.cpp | 4 ++ src/Server.h | 4 ++ src/ServerUtils.cpp | 4 ++ src/ServerUtils.h | 4 ++ src/TickEvent.h | 4 ++ src/Utils.cpp | 4 ++ src/Utils.h | 6 ++- 36 files changed, 303 insertions(+), 97 deletions(-) diff --git a/src/DBusInterface.cpp b/src/DBusInterface.cpp index d96171e..a855f52 100644 --- a/src/DBusInterface.cpp +++ b/src/DBusInterface.cpp @@ -58,6 +58,8 @@ #include "DBusObject.h" #include "Logger.h" +namespace ggk { + // // Construction // @@ -200,3 +202,5 @@ std::string DBusInterface::generateIntrospectionXML(int depth) const return xml; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/DBusInterface.h b/src/DBusInterface.h index 8598675..1dc8971 100644 --- a/src/DBusInterface.h +++ b/src/DBusInterface.h @@ -40,6 +40,8 @@ #include "DBusMethod.h" #include "DBusObjectPath.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Forward declarations // --------------------------------------------------------------------------------------------------------------------------------- @@ -138,3 +140,5 @@ protected: std::list methods; std::list events; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/DBusMethod.cpp b/src/DBusMethod.cpp index 716a454..2fe108e 100644 --- a/src/DBusMethod.cpp +++ b/src/DBusMethod.cpp @@ -39,6 +39,8 @@ #include "DBusMethod.h" +namespace ggk { + // Instantiate a named method on a given interface (pOwner) with a given set of arguments and a callback delegate DBusMethod::DBusMethod(const DBusInterface *pOwner, const std::string &name, const char *pInArgs[], const char *pOutArgs, Callback callback) : pOwner(pOwner), name(name), callback(callback) @@ -86,3 +88,5 @@ std::string DBusMethod::generateIntrospectionXML(int depth) const return xml; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/DBusMethod.h b/src/DBusMethod.h index 89bc7e3..e63b84b 100644 --- a/src/DBusMethod.h +++ b/src/DBusMethod.h @@ -46,6 +46,8 @@ #include "DBusObjectPath.h" #include "Logger.h" +namespace ggk { + struct DBusInterface; struct DBusMethod @@ -102,7 +104,7 @@ struct DBusMethod return; } - Logger::info(SSTR << "Calling method: [" << path << "]:[" << interfaceName << "]:[" << methodName << "]"); + Logger::trace(SSTR << "Calling method: [" << path << "]:[" << interfaceName << "]:[" << methodName << "]"); callback(*static_cast(pOwner), pConnection, methodName, pParameters, pInvocation, pUserData); } @@ -116,3 +118,5 @@ private: std::string outArgs; Callback callback; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/DBusObject.cpp b/src/DBusObject.cpp index c476ea9..49d715a 100644 --- a/src/DBusObject.cpp +++ b/src/DBusObject.cpp @@ -56,6 +56,8 @@ #include "GattUuid.h" #include "Logger.h" +namespace ggk { + // Construct a root object with no parent // // We'll include a publish flag since only root objects can be published @@ -284,3 +286,5 @@ void DBusObject::emitSignal(GDBusConnection *pBusConnection, const std::string & } } + +}; // namespace ggk \ No newline at end of file diff --git a/src/DBusObject.h b/src/DBusObject.h index 123964e..79564ba 100644 --- a/src/DBusObject.h +++ b/src/DBusObject.h @@ -40,6 +40,8 @@ #include "DBusInterface.h" #include "DBusObjectPath.h" +namespace ggk { + struct GattProperty; struct GattService; struct GattUuid; @@ -133,3 +135,5 @@ private: std::list children; DBusObject *pParent; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/DBusObjectPath.h b/src/DBusObjectPath.h index 78cba15..c1e4af5 100644 --- a/src/DBusObjectPath.h +++ b/src/DBusObjectPath.h @@ -39,6 +39,8 @@ #include #include +namespace ggk { + struct DBusObjectPath { // Default constructor (creates a root path) @@ -170,3 +172,5 @@ inline std::ostream& operator +(std::ostream &os, const DBusObjectPath &path) os << path.toString(); return os; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattCharacteristic.cpp b/src/GattCharacteristic.cpp index 2c43c9e..9bfafd9 100644 --- a/src/GattCharacteristic.cpp +++ b/src/GattCharacteristic.cpp @@ -41,6 +41,8 @@ #include "Utils.h" #include "Logger.h" +namespace ggk { + // // Standard constructor // @@ -170,7 +172,7 @@ bool GattCharacteristic::callOnUpdatedValue(GDBusConnection *pConnection, void * return false; } - Logger::info(SSTR << "Calling OnUpdatedValue function for interface at path '" << getPath() << "'"); + Logger::trace(SSTR << "Calling OnUpdatedValue function for interface at path '" << getPath() << "'"); return pOnUpdatedValueFunc(*this, pConnection, pUserData); } @@ -215,3 +217,5 @@ void GattCharacteristic::sendChangeNotificationVariant(GDBusConnection *pBusConn GVariant *pSasv = g_variant_new("(sa{sv})", "org.bluez.GattCharacteristic1", &builder); owner.emitSignal(pBusConnection, "org.freedesktop.DBus.Properties", "PropertiesChanged", pSasv); } + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattCharacteristic.h b/src/GattCharacteristic.h index e7052dc..3815b2a 100644 --- a/src/GattCharacteristic.h +++ b/src/GattCharacteristic.h @@ -41,6 +41,8 @@ #include "TickEvent.h" #include "GattInterface.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Forward declarations // --------------------------------------------------------------------------------------------------------------------------------- @@ -217,3 +219,5 @@ protected: GattService &service; UpdatedValueCallback pOnUpdatedValueFunc; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattDescriptor.cpp b/src/GattDescriptor.cpp index a0e1829..bd29889 100644 --- a/src/GattDescriptor.cpp +++ b/src/GattDescriptor.cpp @@ -38,6 +38,8 @@ #include "Utils.h" #include "Logger.h" +namespace ggk { + // // Standard constructor // @@ -171,6 +173,8 @@ bool GattDescriptor::callOnUpdatedValue(GDBusConnection *pConnection, void *pUse return false; } - Logger::info(SSTR << "Calling OnUpdatedValue function for interface at path '" << getPath() << "'"); + Logger::trace(SSTR << "Calling OnUpdatedValue function for interface at path '" << getPath() << "'"); return pOnUpdatedValueFunc(*this, pConnection, pUserData); } + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattDescriptor.h b/src/GattDescriptor.h index cdda126..43adc61 100644 --- a/src/GattDescriptor.h +++ b/src/GattDescriptor.h @@ -40,6 +40,8 @@ #include "Utils.h" #include "GattInterface.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Forward declarations // --------------------------------------------------------------------------------------------------------------------------------- @@ -181,3 +183,5 @@ protected: GattCharacteristic &characteristic; UpdatedValueCallback pOnUpdatedValueFunc; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattInterface.cpp b/src/GattInterface.cpp index bb80481..ee59afd 100644 --- a/src/GattInterface.cpp +++ b/src/GattInterface.cpp @@ -36,6 +36,8 @@ #include "DBusObject.h" #include "Logger.h" +namespace ggk { + // // Standard constructor // @@ -121,3 +123,5 @@ std::string GattInterface::generateIntrospectionXML(int depth) const return xml; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattInterface.h b/src/GattInterface.h index 3c4075e..70e8ede 100644 --- a/src/GattInterface.h +++ b/src/GattInterface.h @@ -43,6 +43,8 @@ #include "Server.h" #include "Utils.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Forward declarations // --------------------------------------------------------------------------------------------------------------------------------- @@ -229,3 +231,5 @@ protected: std::list properties; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattProperty.cpp b/src/GattProperty.cpp index b829ac8..18b8df3 100644 --- a/src/GattProperty.cpp +++ b/src/GattProperty.cpp @@ -37,6 +37,8 @@ #include "Utils.h" #include "GattProperty.h" +namespace ggk { + // Constructs a named property // // In general, properties should not be constructed directly as properties are typically instanticated by adding them to to an @@ -183,3 +185,5 @@ std::string GattProperty::generateIntrospectionXML(int depth) const return xml; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattProperty.h b/src/GattProperty.h index b340945..fb15c49 100644 --- a/src/GattProperty.h +++ b/src/GattProperty.h @@ -35,6 +35,8 @@ #include #include +namespace ggk { + struct DBusObjectPath; // Representation of a GATT Property @@ -104,3 +106,5 @@ private: GDBusInterfaceGetPropertyFunc getterFunc; GDBusInterfaceSetPropertyFunc setterFunc; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattService.cpp b/src/GattService.cpp index b5b0b84..7f6d324 100644 --- a/src/GattService.cpp +++ b/src/GattService.cpp @@ -42,6 +42,8 @@ #include "DBusObject.h" #include "GattCharacteristic.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Representation of a Bluetooth GATT Service // --------------------------------------------------------------------------------------------------------------------------------- @@ -91,3 +93,5 @@ GattCharacteristic &GattService::gattCharacteristicBegin(const std::string &path characteristic.addProperty("Flags", flags); return characteristic; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattService.h b/src/GattService.h index b9e15ba..a66527c 100644 --- a/src/GattService.h +++ b/src/GattService.h @@ -42,6 +42,8 @@ #include "TickEvent.h" #include "GattInterface.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Forward declarations // --------------------------------------------------------------------------------------------------------------------------------- @@ -97,3 +99,5 @@ struct GattService : GattInterface // Returns a string identifying the type of interface virtual const std::string getInterfaceType() const { return GattService::kInterfaceType; } }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/GattUuid.h b/src/GattUuid.h index ee89332..9d95754 100644 --- a/src/GattUuid.h +++ b/src/GattUuid.h @@ -69,6 +69,8 @@ #include #include "Logger.h" +namespace ggk { + // "0000180A-0000-1000-8000-00805f9b34fb" struct GattUuid { @@ -280,3 +282,5 @@ private: std::string uuid; int bitCount; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/Gobbledegook.cpp b/src/Gobbledegook.cpp index f571be7..43e7e76 100644 --- a/src/Gobbledegook.cpp +++ b/src/Gobbledegook.cpp @@ -54,22 +54,45 @@ #include "Logger.h" #include "Server.h" -// During initialization, we'll check for complation at this interval -static const int kMaxAsyncInitCheckIntervalMS = 10; +namespace ggk +{ + // During initialization, we'll check for complation at this interval + static const int kMaxAsyncInitCheckIntervalMS = 10; + + // Our server thread + static std::thread serverThread; + + // The current server state + volatile static GGKServerRunState serverRunState = EUninitialized; -// Our server thread -static std::thread serverThread; + // The current server health + volatile static GGKServerHealth serverHealth = EOk; -// The current server state -volatile static GGKServerRunState serverRunState = EUninitialized; + // We store the old GLib print handler and error print handler so we can restore if + static GPrintFunc printHandlerGLib; + static GPrintFunc printerrHandlerGLib; + static GLogFunc logHandlerGLib; -// The current server health -volatile static GGKServerHealth serverHealth = EOk; + // Our update queue + typedef std::tuple QueueEntry; + std::deque updateQueue; + std::mutex updateQueueMutex; + + // Internal method to set the run state of the server + void setServerRunState(GGKServerRunState newState) + { + Logger::status(SSTR << "** SERVER RUN STATE CHANGED: " << ggkGetServerRunStateString(serverRunState) << " -> " << ggkGetServerRunStateString(newState)); + serverRunState = newState; + } + + // Internal method to set the health of the server + void setServerHealth(GGKServerHealth newState) + { + serverHealth = newState; + } +}; // namespace ggk -// Our update queue -typedef std::tuple QueueEntry; -std::deque updateQueue; -std::mutex updateQueueMutex; +using namespace ggk; // --------------------------------------------------------------------------------------------------------------------------------- // _ _ _ _ _ @@ -228,13 +251,6 @@ const char *ggkGetServerRunStateString(GGKServerRunState state) } } -// Internal method to set the run state of the server -void setServerRunState(GGKServerRunState newState) -{ - Logger::status(SSTR << "** SERVER RUN STATE CHANGED: " << ggkGetServerRunStateString(serverRunState) << " -> " << ggkGetServerRunStateString(newState)); - serverRunState = newState; -} - // Convenience method to check ServerRunState for a running server int ggkIsServerRunning() { @@ -271,12 +287,6 @@ const char *ggkGetServerHealthString(GGKServerHealth state) } } -// Internal method to set the health of the server -void setServerHealth(GGKServerHealth newState) -{ - serverHealth = newState; -} - // --------------------------------------------------------------------------------------------------------------------------------- // ____ _ _ _ // / ___|| |_ ___ _ __ | |_| |__ ___ ___ ___ _ ____ _____ _ __ @@ -330,9 +340,13 @@ int ggkShutdownAndWait() // Typically, a call to this method would follow `ggkTriggerShutdown()`. int ggkWait() { + int result = 0; try { + Logger::info("Stopping GGK server"); serverThread.join(); + + result = 1; } catch(std::system_error &ex) { @@ -354,8 +368,12 @@ int ggkWait() } } - // Return true if we're stopped, otherwise false - return ggkGetServerRunState() == EStopped ? 1 : 0; + // Restore the GLib output functions + g_set_print_handler(printHandlerGLib); + g_set_printerr_handler(printerrHandlerGLib); + g_log_set_default_handler(logHandlerGLib, nullptr); + + return result; } // --------------------------------------------------------------------------------------------------------------------------------- @@ -387,6 +405,46 @@ int ggkWait() // safely for an indefinite period of time. int ggkStart(GGKServerDataGetter getter, GGKServerDataSetter setter, int maxAsyncInitTimeoutMS) { + // + // Start by capturing the GLib output + // + + // Redirect GLib output to this log method + printHandlerGLib = g_set_print_handler([](const gchar *string) + { + Logger::info(string); + }); + printerrHandlerGLib = g_set_printerr_handler([](const gchar *string) + { + Logger::error(string); + }); + logHandlerGLib = g_log_set_default_handler([](const gchar *log_domain, GLogLevelFlags log_levels, const gchar *message, gpointer /*user_data*/) + { + std::string str = std::string(log_domain) + ": " + message; + if ((log_levels & (G_LOG_FLAG_RECURSION|G_LOG_FLAG_FATAL)) != 0) + { + Logger::fatal(str); + } + else if ((log_levels & (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_ERROR)) != 0) + { + Logger::error(str); + } + else if ((log_levels & G_LOG_LEVEL_WARNING) != 0) + { + Logger::warn(str); + } + else if ((log_levels & G_LOG_LEVEL_DEBUG) != 0) + { + Logger::debug(str); + } + else + { + Logger::info(str); + } + }, nullptr); + + Logger::info("Starting GGK server"); + // Allocate our server TheServer = std::make_shared(getter, setter); @@ -430,4 +488,4 @@ int ggkStart(GGKServerDataGetter getter, GGKServerDataSetter setter, int maxAsyn // Everything looks good return 1; -} \ No newline at end of file +} diff --git a/src/HciAdapter.cpp b/src/HciAdapter.cpp index 8e906e6..6d99d69 100644 --- a/src/HciAdapter.cpp +++ b/src/HciAdapter.cpp @@ -59,6 +59,8 @@ #include "Utils.h" #include "Logger.h" +namespace ggk { + static const int kMinCommandCode = 0x0001; static const int kMaxCommandCode = 0x0043; static const char *kCommandCodeNames[kMaxCommandCode] = @@ -356,12 +358,12 @@ bool HciAdapter::filterAndValidateEvents(uint16_t commandCode, std::vectorheader.code - kMinEventType]; std::string commandCodeName = pEvent->commandCode < kMinCommandCode || pEvent->commandCode > kMaxCommandCode ? "Unknown" : kCommandCodeNames[pEvent->commandCode - kMinCommandCode]; - Logger::info(SSTR << " + Received event type " << Utils::hex(pEvent->header.code) << " (" << eventTypeName << ")"); + Logger::debug(SSTR << " + Received event type " << Utils::hex(pEvent->header.code) << " (" << eventTypeName << ")"); // Success event for our command? if (pEvent->header.code != 1 && pEvent->commandCode != commandCode) { - Logger::warn(SSTR << " + Skipping event type " << Utils::hex(pEvent->header.code) << " (" << eventTypeName << ")"); + Logger::debug(SSTR << " + Skipping event type " << Utils::hex(pEvent->header.code) << " (" << eventTypeName << ")"); } else { @@ -382,3 +384,5 @@ bool HciAdapter::filterAndValidateEvents(uint16_t commandCode, std::vector #include +namespace ggk { + class HciSocket { public: @@ -88,3 +90,5 @@ private: const int kRetryIntervalMS = 20; const int kMaxRetryTimeMS = 5000; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/Init.cpp b/src/Init.cpp index 5ed25e2..436a4fa 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -60,6 +60,8 @@ #include "Logger.h" #include "Init.h" +namespace ggk { + // // Constants // @@ -174,7 +176,7 @@ bool idleFunc(void *pUserData) // Is it a characteristic? if (std::shared_ptr pCharacteristic = TRY_GET_CONST_INTERFACE_OF_TYPE(pInterface, GattCharacteristic)) { - Logger::info(SSTR << "Processing updated value for interface '" << interfaceName << "' at path '" << objectPath << "'"); + Logger::trace(SSTR << "Processing updated value for interface '" << interfaceName << "' at path '" << objectPath << "'"); pCharacteristic->callOnUpdatedValue(pBusConnection, pUserData); } } @@ -420,7 +422,7 @@ GVariant *onGetProperty return nullptr; } - Logger::info(SSTR << "Calling property getter: " << propertyPath); + Logger::trace(SSTR << "Calling property getter: " << propertyPath); GVariant *pResult = pProperty->getGetterFunc()(pConnection, pSender, objectPath.c_str(), pInterfaceName, pPropertyName, ppError, pUserData); if (nullptr == pResult) @@ -465,7 +467,7 @@ gboolean onSetProperty return false; } - Logger::info(SSTR << "Calling property getter: " << propertyPath); + Logger::trace(SSTR << "Calling property getter: " << propertyPath); if (!pProperty->getSetterFunc()(pConnection, pSender, objectPath.c_str(), pInterfaceName, pPropertyName, pValue, ppError, pUserData)) { g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(set) failed: " + propertyPath).c_str(), pSender); @@ -490,7 +492,7 @@ gboolean onSetProperty void setRetryFailure() { retryTimeStart = time(nullptr); - Logger::info(SSTR << " + Will retry this failed operation in about " << kRetryDelaySeconds << " seconds"); + Logger::debug(SSTR << " + Will retry this failed operation in about " << kRetryDelaySeconds << " seconds"); } // --------------------------------------------------------------------------------------------------------------------------------- @@ -532,7 +534,7 @@ void doRegisterApplication() else { g_variant_unref(pVariant); - Logger::info(SSTR << "Application registered"); + Logger::trace(SSTR << "Application registered"); bApplicationRegistered = true; } @@ -662,7 +664,7 @@ void configureAdapter() Mgmt mgmt; // Find out what our current settings are - Logger::info(SSTR << "Getting device information"); + Logger::trace(SSTR << "Getting device information"); Mgmt::ControllerInformation *pInfo = mgmt.getControllerInformation(); if (nullptr == pInfo) { @@ -673,7 +675,7 @@ void configureAdapter() // We need it off to start with if ((pInfo->currentSettings & Mgmt::EHciPowered) != 0) { - Logger::info(SSTR << "Powering off"); + Logger::trace(SSTR << "Powering off"); if (!mgmt.setPowered(false)) { setRetryFailure(); @@ -685,7 +687,7 @@ void configureAdapter() bool bredrCurrentState = (pInfo->currentSettings & Mgmt::EHciBasicRate_EnhancedDataRate) != 0 ? true:false; if (TheServer->getEnableBREDR() != bredrCurrentState) { - Logger::info(SSTR << (TheServer->getEnableBREDR() ? "Enabling":"Disabling") << " BR/EDR"); + Logger::trace(SSTR << (TheServer->getEnableBREDR() ? "Enabling":"Disabling") << " BR/EDR"); if (!mgmt.setBredr(TheServer->getEnableBREDR())) { setRetryFailure(); @@ -697,7 +699,7 @@ void configureAdapter() bool scCurrentState = (pInfo->currentSettings & Mgmt::EHciSecureConnections) != 0 ? true:false; if (TheServer->getEnableSecureConnection() != scCurrentState) { - Logger::info(SSTR << (TheServer->getEnableSecureConnection() ? "Enabling":"Disabling") << " Secure Connections"); + Logger::trace(SSTR << (TheServer->getEnableSecureConnection() ? "Enabling":"Disabling") << " Secure Connections"); if (!mgmt.setSecureConnections(TheServer->getEnableSecureConnection() ? 1 : 0)) { setRetryFailure(); @@ -709,7 +711,7 @@ void configureAdapter() bool bondableCurrentState = (pInfo->currentSettings & Mgmt::EHciBondable) != 0 ? true:false; if (TheServer->getEnableBondable() != bondableCurrentState) { - Logger::info(SSTR << (TheServer->getEnableBondable() ? "Enabling":"Disabling") << " Bondable"); + Logger::trace(SSTR << (TheServer->getEnableBondable() ? "Enabling":"Disabling") << " Bondable"); if (!mgmt.setBondable(TheServer->getEnableBondable())) { setRetryFailure(); @@ -721,7 +723,7 @@ void configureAdapter() bool connectableCurrentState = (pInfo->currentSettings & Mgmt::EHciConnectable) != 0 ? true:false; if (TheServer->getEnableConnectable() != connectableCurrentState) { - Logger::info(SSTR << (TheServer->getEnableConnectable() ? "Enabling":"Disabling") << " Connectable"); + Logger::trace(SSTR << (TheServer->getEnableConnectable() ? "Enabling":"Disabling") << " Connectable"); if (!mgmt.setConnectable(TheServer->getEnableConnectable())) { setRetryFailure(); @@ -732,7 +734,7 @@ void configureAdapter() // Enable the LE state (we always set this state if it's not set) if ((pInfo->currentSettings & Mgmt::EHciLowEnergy) == 0) { - Logger::info(SSTR << "Enabling LE"); + Logger::trace(SSTR << "Enabling LE"); if (!mgmt.setLE(true)) { setRetryFailure(); @@ -744,7 +746,7 @@ void configureAdapter() bool advertisingCurrentState = (pInfo->currentSettings & Mgmt::EHciAdvertising) != 0 ? true:false; if (TheServer->getEnableAdvertising() != advertisingCurrentState) { - Logger::info(SSTR << (TheServer->getEnableAdvertising() ? "Enabling":"Disabling") << " Advertising"); + Logger::trace(SSTR << (TheServer->getEnableAdvertising() ? "Enabling":"Disabling") << " Advertising"); if (!mgmt.setAdvertising(TheServer->getEnableAdvertising() ? 1 : 0)) { setRetryFailure(); @@ -758,7 +760,7 @@ void configureAdapter() if (Mgmt::truncateName(kCustomGlobalAdvertisingName) != pInfo->name || Mgmt::truncateShortName(kCustomGlobalAdvertisingShortName) != pInfo->shortName) { - Logger::info(SSTR << "Setting name to '" << kCustomGlobalAdvertisingName << "'"); + Logger::trace(SSTR << "Setting name to '" << kCustomGlobalAdvertisingName << "'"); if (!mgmt.setName(kCustomGlobalAdvertisingName.c_str(), kCustomGlobalAdvertisingName.c_str())) { setRetryFailure(); @@ -768,7 +770,7 @@ void configureAdapter() } // Turn it back on - Logger::info(SSTR << "Powering on"); + Logger::trace(SSTR << "Powering on"); if (!mgmt.setPowered(true)) { setRetryFailure(); @@ -856,7 +858,7 @@ void findAdapterInterface() // If we didn't find the adapter object, reset things and we'll try again later if (nullptr == pBluezAdapterObject || nullptr == pBluezDeviceObject) { - Logger::info(SSTR << "Unable to find BlueZ objects outside of object list"); + Logger::warn(SSTR << "Unable to find BlueZ objects outside of object list"); bluezGattManagerInterfaceName.clear(); } @@ -1058,7 +1060,7 @@ void initializationStateProcessor() // if (nullptr == pBusConnection) { - Logger::info(SSTR << "Acquiring bus connection"); + Logger::trace(SSTR << "Acquiring bus connection"); doBusAcquire(); return; } @@ -1068,7 +1070,7 @@ void initializationStateProcessor() // if (!bOwnedNameAcquired) { - Logger::info(SSTR << "Acquiring owned name: '" << kServerOwnedName << "'"); + Logger::trace(SSTR << "Acquiring owned name: '" << kServerOwnedName << "'"); doOwnedNameAcquire(); return; } @@ -1088,7 +1090,7 @@ void initializationStateProcessor() // if (bluezGattManagerInterfaceName.empty()) { - Logger::debug(SSTR << "Finding BlueZ GattManager1 interface"); + Logger::trace(SSTR << "Finding BlueZ GattManager1 interface"); findAdapterInterface(); return; } @@ -1098,7 +1100,7 @@ void initializationStateProcessor() // if (!bAdapterConfigured) { - Logger::info(SSTR << "Configuring BlueZ adapter '" << bluezGattManagerInterfaceName << "'"); + Logger::trace(SSTR << "Configuring BlueZ adapter '" << bluezGattManagerInterfaceName << "'"); configureAdapter(); return; } @@ -1108,7 +1110,7 @@ void initializationStateProcessor() // if (registeredObjectIds.empty()) { - Logger::info(SSTR << "Registering with D-Bus"); + Logger::trace(SSTR << "Registering with D-Bus"); registerObjects(); return; } @@ -1116,7 +1118,7 @@ void initializationStateProcessor() // Register our appliation with the BlueZ GATT manager if (!bApplicationRegistered) { - Logger::info(SSTR << "Registering application with BlueZ GATT manager"); + Logger::trace(SSTR << "Registering application with BlueZ GATT manager"); doRegisterApplication(); return; @@ -1165,7 +1167,7 @@ void runServerThread() // There are alternatives, but using async methods is the recommended way. initializationStateProcessor(); - Logger::info(SSTR << "Starting main loop"); + Logger::trace(SSTR << "Starting main loop"); pMainLoop = g_main_loop_new(NULL, FALSE); // Add the idle function @@ -1192,16 +1194,15 @@ void runServerThread() { Logger::error(SSTR << "Unable to add idle to main loop"); } - else - { - Logger::info("Idle function added successfully"); - } g_main_loop_run(pMainLoop); // We have stopped setServerRunState(EStopped); + Logger::info("GGK server stopped"); // Cleanup uninit(); } + +}; // namespace ggk \ No newline at end of file diff --git a/src/Init.h b/src/Init.h index cad7785..2dc709a 100644 --- a/src/Init.h +++ b/src/Init.h @@ -32,6 +32,8 @@ #pragma once +namespace ggk { + // Trigger a graceful, asynchronous shutdown of the server // // This method is non-blocking and as such, will only trigger the shutdown process but not wait for it @@ -41,3 +43,5 @@ void shutdown(); // // This method should not be called directly, instead, direct your attention over to `ggkStart()` void runServerThread(); + +}; // namespace ggk \ No newline at end of file diff --git a/src/Logger.cpp b/src/Logger.cpp index 7607ac8..b61daa3 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -59,6 +59,8 @@ #include "Logger.h" +namespace ggk { + // // Log receiver delegates // @@ -93,108 +95,110 @@ GGKLogReceiver Logger::logReceiverTrace = nullptr; // Register logging receiver for DEBUG logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerDebugReceiver(GGKLogReceiver receiver) { logReceiverDebug = receiver; } +void Logger::registerDebugReceiver(GGKLogReceiver receiver) { Logger::logReceiverDebug = receiver; } // Register logging receiver for INFO logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerInfoReceiver(GGKLogReceiver receiver) { logReceiverInfo = receiver; } +void Logger::registerInfoReceiver(GGKLogReceiver receiver) { Logger::logReceiverInfo = receiver; } // Register logging receiver for STATUS logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerStatusReceiver(GGKLogReceiver receiver) { logReceiverStatus = receiver; } +void Logger::registerStatusReceiver(GGKLogReceiver receiver) { Logger::logReceiverStatus = receiver; } // Register logging receiver for WARN logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerWarnReceiver(GGKLogReceiver receiver) { logReceiverWarn = receiver; } +void Logger::registerWarnReceiver(GGKLogReceiver receiver) { Logger::logReceiverWarn = receiver; } // Register logging receiver for ERROR logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerErrorReceiver(GGKLogReceiver receiver) { logReceiverError = receiver; } +void Logger::registerErrorReceiver(GGKLogReceiver receiver) { Logger::logReceiverError = receiver; } // Register logging receiver for FATAL logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerFatalReceiver(GGKLogReceiver receiver) { logReceiverFatal = receiver; } +void Logger::registerFatalReceiver(GGKLogReceiver receiver) { Logger::logReceiverFatal = receiver; } // Register logging receiver for ALWAYS logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerAlwaysReceiver(GGKLogReceiver receiver) { logReceiverAlways = receiver; } +void Logger::registerAlwaysReceiver(GGKLogReceiver receiver) { Logger::logReceiverAlways = receiver; } // Register logging receiver for TRACE logging. To register a logging level, simply call with a delegate that performs the // appropriate logging action. To unregister, call with `nullptr` -void Logger::registerTraceReceiver(GGKLogReceiver receiver) { logReceiverTrace = receiver; } +void Logger::registerTraceReceiver(GGKLogReceiver receiver) { Logger::logReceiverTrace = receiver; } // // Logging actions // // Log a DEBUG entry with a C string -void Logger::debug(const char *pText) { if (nullptr != logReceiverDebug) { logReceiverDebug(pText); } } +void Logger::debug(const char *pText) { if (nullptr != Logger::logReceiverDebug) { Logger::logReceiverDebug(pText); } } // Log a DEBUG entry with a string -void Logger::debug(const std::string &text) { if (nullptr != logReceiverDebug) { debug(text.c_str()); } } +void Logger::debug(const std::string &text) { if (nullptr != Logger::logReceiverDebug) { debug(text.c_str()); } } // Log a DEBUG entry using a stream -void Logger::debug(const std::ostream &text) { if (nullptr != logReceiverDebug) { debug(static_cast(text).str().c_str()); } } +void Logger::debug(const std::ostream &text) { if (nullptr != Logger::logReceiverDebug) { debug(static_cast(text).str().c_str()); } } // Log a INFO entry with a C string -void Logger::info(const char *pText) { if (nullptr != logReceiverInfo) { logReceiverInfo(pText); } } +void Logger::info(const char *pText) { if (nullptr != Logger::logReceiverInfo) { Logger::logReceiverInfo(pText); } } // Log a INFO entry with a string -void Logger::info(const std::string &text) { if (nullptr != logReceiverInfo) { info(text.c_str()); } } +void Logger::info(const std::string &text) { if (nullptr != Logger::logReceiverInfo) { info(text.c_str()); } } // Log a INFO entry using a stream -void Logger::info(const std::ostream &text) { if (nullptr != logReceiverInfo) { info(static_cast(text).str().c_str()); } } +void Logger::info(const std::ostream &text) { if (nullptr != Logger::logReceiverInfo) { info(static_cast(text).str().c_str()); } } // Log a STATUS entry with a C string -void Logger::status(const char *pText) { if (nullptr != logReceiverStatus) { logReceiverStatus(pText); } } +void Logger::status(const char *pText) { if (nullptr != Logger::logReceiverStatus) { Logger::logReceiverStatus(pText); } } // Log a STATUS entry with a string -void Logger::status(const std::string &text) { if (nullptr != logReceiverStatus) { status(text.c_str()); } } +void Logger::status(const std::string &text) { if (nullptr != Logger::logReceiverStatus) { status(text.c_str()); } } // Log a STATUS entry using a stream -void Logger::status(const std::ostream &text) { if (nullptr != logReceiverStatus) { status(static_cast(text).str().c_str()); } } +void Logger::status(const std::ostream &text) { if (nullptr != Logger::logReceiverStatus) { status(static_cast(text).str().c_str()); } } // Log a WARN entry with a C string -void Logger::warn(const char *pText) { if (nullptr != logReceiverWarn) { logReceiverWarn(pText); } } +void Logger::warn(const char *pText) { if (nullptr != Logger::logReceiverWarn) { Logger::logReceiverWarn(pText); } } // Log a WARN entry with a string -void Logger::warn(const std::string &text) { if (nullptr != logReceiverWarn) { warn(text.c_str()); } } +void Logger::warn(const std::string &text) { if (nullptr != Logger::logReceiverWarn) { warn(text.c_str()); } } // Log a WARN entry using a stream -void Logger::warn(const std::ostream &text) { if (nullptr != logReceiverWarn) { warn(static_cast(text).str().c_str()); } } +void Logger::warn(const std::ostream &text) { if (nullptr != Logger::logReceiverWarn) { warn(static_cast(text).str().c_str()); } } // Log a ERROR entry with a C string -void Logger::error(const char *pText) { if (nullptr != logReceiverError) { logReceiverError(pText); } } +void Logger::error(const char *pText) { if (nullptr != Logger::logReceiverError) { Logger::logReceiverError(pText); } } // Log a ERROR entry with a string -void Logger::error(const std::string &text) { if (nullptr != logReceiverError) { error(text.c_str()); } } +void Logger::error(const std::string &text) { if (nullptr != Logger::logReceiverError) { error(text.c_str()); } } // Log a ERROR entry using a stream -void Logger::error(const std::ostream &text) { if (nullptr != logReceiverError) { error(static_cast(text).str().c_str()); } } +void Logger::error(const std::ostream &text) { if (nullptr != Logger::logReceiverError) { error(static_cast(text).str().c_str()); } } // Log a FATAL entry with a C string -void Logger::fatal(const char *pText) { if (nullptr != logReceiverFatal) { logReceiverFatal(pText); } } +void Logger::fatal(const char *pText) { if (nullptr != Logger::logReceiverFatal) { Logger::logReceiverFatal(pText); } } // Log a FATAL entry with a string -void Logger::fatal(const std::string &text) { if (nullptr != logReceiverFatal) { fatal(text.c_str()); } } +void Logger::fatal(const std::string &text) { if (nullptr != Logger::logReceiverFatal) { fatal(text.c_str()); } } // Log a FATAL entry using a stream -void Logger::fatal(const std::ostream &text) { if (nullptr != logReceiverFatal) { fatal(static_cast(text).str().c_str()); } } +void Logger::fatal(const std::ostream &text) { if (nullptr != Logger::logReceiverFatal) { fatal(static_cast(text).str().c_str()); } } // Log a ALWAYS entry with a C string -void Logger::always(const char *pText) { if (nullptr != logReceiverAlways) { logReceiverAlways(pText); } } +void Logger::always(const char *pText) { if (nullptr != Logger::logReceiverAlways) { Logger::logReceiverAlways(pText); } } // Log a ALWAYS entry with a string -void Logger::always(const std::string &text) { if (nullptr != logReceiverAlways) { always(text.c_str()); } } +void Logger::always(const std::string &text) { if (nullptr != Logger::logReceiverAlways) { always(text.c_str()); } } // Log a ALWAYS entry using a stream -void Logger::always(const std::ostream &text) { if (nullptr != logReceiverAlways) { always(static_cast(text).str().c_str()); } } +void Logger::always(const std::ostream &text) { if (nullptr != Logger::logReceiverAlways) { always(static_cast(text).str().c_str()); } } // Log a TRACE entry with a C string -void Logger::trace(const char *pText) { if (nullptr != logReceiverTrace) { logReceiverTrace(pText); } } +void Logger::trace(const char *pText) { if (nullptr != Logger::logReceiverTrace) { Logger::logReceiverTrace(pText); } } // Log a TRACE entry with a string -void Logger::trace(const std::string &text) { if (nullptr != logReceiverTrace) { trace(text.c_str()); } } +void Logger::trace(const std::string &text) { if (nullptr != Logger::logReceiverTrace) { trace(text.c_str()); } } // Log a TRACE entry using a stream -void Logger::trace(const std::ostream &text) { if (nullptr != logReceiverTrace) { trace(static_cast(text).str().c_str()); } } +void Logger::trace(const std::ostream &text) { if (nullptr != Logger::logReceiverTrace) { trace(static_cast(text).str().c_str()); } } + +}; // namespace ggk \ No newline at end of file diff --git a/src/Logger.h b/src/Logger.h index 08c1221..51fd5f4 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -37,6 +37,8 @@ #include "../include/Gobbledegook.h" +namespace ggk { + // Our handy stringstream macro #define SSTR std::ostringstream().flush() @@ -183,3 +185,5 @@ private: // The registered log receiver for TRACE logs - a nullptr will cause the logging for that receiver to be ignored static GGKLogReceiver logReceiverTrace; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/Mgmt.cpp b/src/Mgmt.cpp index d2ced63..c956e99 100644 --- a/src/Mgmt.cpp +++ b/src/Mgmt.cpp @@ -39,6 +39,8 @@ #include "Logger.h" #include "Utils.h" +namespace ggk { + // Construct the Mgmt device // // Set `controllerIndex` to the zero-based index of the device as recognized by the OS. If this parameter is omitted, the index @@ -81,7 +83,7 @@ int Mgmt::getVersion() response.toHost(); - Logger::info(SSTR << " + Version response has version=" << Utils::hex(response.version) << " and revision=" << Utils::hex(response.revision)); + Logger::debug(SSTR << " + Version response has version=" << Utils::hex(response.version) << " and revision=" << Utils::hex(response.revision)); return (response.version << 16) | response.revision; } @@ -177,7 +179,7 @@ bool Mgmt::setName(std::string name, std::string shortName) return false; } - Logger::info(SSTR << " + Name set to '" << request.name << "', short name set to '" << request.shortName << "'"); + Logger::trace(SSTR << " + Name set to '" << request.name << "', short name set to '" << request.shortName << "'"); return true; } @@ -212,13 +214,24 @@ bool Mgmt::setState(const char *pSettingName, uint16_t commandCode, uint16_t con SResponse response; if (!hciAdapter.sendCommand(request, response, sizeof(response))) { - Logger::warn(SSTR << " + Failed to set " << pSettingName << " state to: " << static_cast(newState)); + // Setting power to 0 doesn't actually return a response event if it's connected (it receives a disconnect instead). + // + // This is a failure on the part of GGK because it should be handling events more correctly. For now, though, we'll just + // turn those failures into debug log messages so our logs aren't cluttered up with false failures. + if (commandCode == 0x0005) + { + Logger::debug(SSTR << " + Failed to set " << pSettingName << " state to: " << static_cast(newState)); + } + else + { + Logger::info(SSTR << " + Failed to set " << pSettingName << " state to: " << static_cast(newState)); + } return false; } response.toHost(); - Logger::info(SSTR << " + " << pSettingName << " set to " << static_cast(newState) << ": " << controllerSettingsString(response.currentSettings)); + Logger::trace(SSTR << " + " << pSettingName << " set to " << static_cast(newState) << ": " << controllerSettingsString(response.currentSettings)); return true; } @@ -337,3 +350,5 @@ std::string Mgmt::truncateShortName(const std::string &name) return name.substr(0, kMaxShortNameLength); } + +}; // namespace ggk \ No newline at end of file diff --git a/src/Mgmt.h b/src/Mgmt.h index c58c9bc..dfd5b95 100644 --- a/src/Mgmt.h +++ b/src/Mgmt.h @@ -41,6 +41,8 @@ #include "HciAdapter.h" #include "Utils.h" +namespace ggk { + struct Mgmt { // @@ -211,3 +213,5 @@ private: // A constant referring to a 'non-controller' (for commands that do not require a controller index) static const uint16_t kNonController = 0xffff; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/Server.cpp b/src/Server.cpp index 3ab15bf..a369b32 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -172,6 +172,8 @@ #include "GattCharacteristic.h" #include "GattDescriptor.h" +namespace ggk { + // There's a good chance there will be a bunch of unused parameters from the lambda macros #if defined(__GNUC__) && defined(__clang__) #pragma clang diagnostic push @@ -616,3 +618,5 @@ const GattProperty *Server::findProperty(const DBusObjectPath &objectPath, const return nullptr; } + +}; // namespace ggk diff --git a/src/Server.h b/src/Server.h index 8880f22..8312022 100644 --- a/src/Server.h +++ b/src/Server.h @@ -42,6 +42,8 @@ #include "DBusInterface.h" #include "DBusObject.h" +namespace ggk { + // // Forward declarations // @@ -144,3 +146,5 @@ private: // Our one and only server. It's a global. extern std::shared_ptr TheServer; + +}; // namespace ggk diff --git a/src/ServerUtils.cpp b/src/ServerUtils.cpp index bee20cf..2cfa0e4 100644 --- a/src/ServerUtils.cpp +++ b/src/ServerUtils.cpp @@ -49,6 +49,8 @@ #include "Logger.h" #include "Utils.h" +namespace ggk { + // Adds an object to the tree of managed objects as returned from the `GetManagedObjects` method call from the D-Bus interface // `org.freedesktop.DBus.ObjectManager`. // @@ -312,3 +314,5 @@ GVariant *ServerUtils::gvariantLocalTime() GVariant * const pVariant = g_variant_builder_end(&builder); return pVariant; } + +}; // namespace ggk \ No newline at end of file diff --git a/src/ServerUtils.h b/src/ServerUtils.h index 3ab9511..1b6c5be 100644 --- a/src/ServerUtils.h +++ b/src/ServerUtils.h @@ -37,6 +37,8 @@ #include #include +namespace ggk { + struct ServerUtils { // Builds the response to the method call `GetManagedObjects` from the D-Bus interface `org.freedesktop.DBus.ObjectManager` @@ -60,3 +62,5 @@ struct ServerUtils // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml static GVariant *gvariantLocalTime(); }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/TickEvent.h b/src/TickEvent.h index c519a0b..e72ae9b 100644 --- a/src/TickEvent.h +++ b/src/TickEvent.h @@ -52,6 +52,8 @@ #include "DBusObjectPath.h" #include "Logger.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Forward declarations // --------------------------------------------------------------------------------------------------------------------------------- @@ -147,3 +149,5 @@ private: Callback callback; void *pUserData; }; + +}; // namespace ggk \ No newline at end of file diff --git a/src/Utils.cpp b/src/Utils.cpp index eaeb703..ca3aaa5 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -43,6 +43,8 @@ #include "Utils.h" +namespace ggk { + // --------------------------------------------------------------------------------------------------------------------------------- // Handy string functions // --------------------------------------------------------------------------------------------------------------------------------- @@ -429,3 +431,5 @@ std::string Utils::stringFromGVariantByteArray(const GVariant *pVariant) memcpy(array.data(), pPtr, size); return array.data(); } + +}; // namespace ggk \ No newline at end of file diff --git a/src/Utils.h b/src/Utils.h index 2eee95a..59506de 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -48,6 +48,8 @@ #include "DBusObjectPath.h" +namespace ggk { + struct Utils { // ----------------------------------------------------------------------------------------------------------------------------- @@ -209,4 +211,6 @@ struct Utils // Convert a 32-bit value from host format to HCI format static uint32_t endianToHci(uint32_t value) {return htole32(value);} -}; \ No newline at end of file +}; + +}; // namespace ggk \ No newline at end of file -- cgit v1.2.3