From 10a4e4c39ae4120076f6648cab9f3efe1c80c0de Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 30 Mar 2018 22:35:23 +0100 Subject: maybe it compiles? --- src/Server.cpp | 227 ++++++----------------------------------------------- src/standalone.cpp | 45 +++-------- 2 files changed, 33 insertions(+), 239 deletions(-) diff --git a/src/Server.cpp b/src/Server.cpp index d6c9357..bc3433e 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -271,7 +271,7 @@ Server::Server(const std::string &serviceName, const std::string &advertisingNam // Standard characteristic "ReadValue" method call .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA { - self.methodReturnValue(pInvocation, "Acme Inc.", true); + self.methodReturnValue(pInvocation, "thenautilus", true); }) .gattCharacteristicEnd() @@ -284,144 +284,31 @@ Server::Server(const std::string &serviceName, const std::string &advertisingNam // Standard characteristic "ReadValue" method call .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA { - self.methodReturnValue(pInvocation, "Marvin-PA", true); + self.methodReturnValue(pInvocation, "thermo-store", true); }) .gattCharacteristicEnd() .gattServiceEnd() - // Battery Service (0x180F) - // - // This is a fake battery service that conforms to org.bluetooth.service.battery_service. For details, see: - // - // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.battery_service.xml - // - // We also handle updates to the battery level from inside the server (see onUpdatedValue). There is an external method - // (see main.cpp) that updates our battery level and posts an update using ggkPushUpdateQueue. Those updates are used - // to notify us that our value has changed, which translates into a call to `onUpdatedValue` from the idleFunc (see - // Init.cpp). - .gattServiceBegin("battery", "180F") - - // Characteristic: Battery Level (0x2A19) - // - // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml - .gattCharacteristicBegin("level", "2A19", {"read", "notify"}) - - // Standard characteristic "ReadValue" method call - .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA - { - uint8_t batteryLevel = self.getDataValue("battery/level", 0); - self.methodReturnValue(pInvocation, batteryLevel, true); - }) - - // Handle updates to the battery level - // - // Here we use the onUpdatedValue to set a callback that isn't exposed to BlueZ, but rather allows us to manage - // updates to our value. These updates may have come from our own server or some other source. - // - // We can handle updates in any way we wish, but the most common use is to send a change notification. - .onUpdatedValue(CHARACTERISTIC_UPDATED_VALUE_CALLBACK_LAMBDA - { - uint8_t batteryLevel = self.getDataValue("battery/level", 0); - self.sendChangeNotificationValue(pConnection, batteryLevel); - return true; - }) - - .gattCharacteristicEnd() - .gattServiceEnd() - - // Current Time Service (0x1805) - // - // This is a time service that conforms to org.bluetooth.service.current_time. For details, see: - // - // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml - // - // Like the battery service, this also makes use of events. This one updates the time every tick. - // - // This showcases the use of events (see the call to .onEvent() below) for periodic actions. In this case, the action - // taken is to update time every tick. This probably isn't a good idea for a production service, but it has been quite - // useful for testing to ensure we're connected and updating. - .gattServiceBegin("time", "1805") - - // Characteristic: Current Time (0x2A2B) - // - // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml - .gattCharacteristicBegin("current", "2A2B", {"read", "notify"}) - - // Standard characteristic "ReadValue" method call - .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA - { - self.methodReturnVariant(pInvocation, ServerUtils::gvariantCurrentTime(), true); - }) + // Thermo store service + .gattServiceBegin("thermo", "00000001") - // Update the time every tick of the periodic timer - // - // We'll send an change notification to any subscribed clients with the latest value - .onEvent(1, nullptr, CHARACTERISTIC_EVENT_CALLBACK_LAMBDA - { - self.sendChangeNotificationVariant(pConnection, ServerUtils::gvariantCurrentTime()); - }) - - .gattCharacteristicEnd() - - // Characteristic: Local Time Information (0x2A0F) - // - // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml - .gattCharacteristicBegin("local", "2A0F", {"read"}) + // Characteristic: String value + .gattCharacteristicBegin("room1", "00000001-1", {"read", "write"}) // Standard characteristic "ReadValue" method call .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA { - self.methodReturnVariant(pInvocation, ServerUtils::gvariantLocalTime(), true); - }) - - .gattCharacteristicEnd() - .gattServiceEnd() - - // Custom read/write text string service (00000001-1E3C-FAD4-74E2-97A033F1BFAA) - // - // This service will return a text string value (default: 'Hello, world!'). If the text value is updated, it will notify - // that the value has been updated and provide the new text from that point forward. - .gattServiceBegin("text", "00000001-1E3C-FAD4-74E2-97A033F1BFAA") - - // Characteristic: String value (custom: 00000002-1E3C-FAD4-74E2-97A033F1BFAA) - .gattCharacteristicBegin("string", "00000002-1E3C-FAD4-74E2-97A033F1BFAA", {"read", "write", "notify"}) - - // Standard characteristic "ReadValue" method call - .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA - { - const char *pTextString = self.getDataPointer("text/string", ""); - self.methodReturnValue(pInvocation, pTextString, true); + int8_t temperature = self.getDataValue("room-1", 0); + self.methodReturnValue(pInvocation, temperature, true); }) // Standard characteristic "WriteValue" method call .onWriteValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA { - // Update the text string value - GVariant *pAyBuffer = g_variant_get_child_value(pParameters, 0); - self.setDataPointer("text/string", Utils::stringFromGVariantByteArray(pAyBuffer).c_str()); - - // Since all of these methods (onReadValue, onWriteValue, onUpdateValue) are all part of the same - // Characteristic interface (which just so happens to be the same interface passed into our self - // parameter) we can that parameter to call our own onUpdatedValue method - self.callOnUpdatedValue(pConnection, pUserData); - - // Note: Even though the WriteValue method returns void, it's important to return like this, so that a - // dbus "method_return" is sent, otherwise the client gets an error (ATT error code 0x0e"unlikely"). - // Only "write-without-response" works without this - self.methodReturnVariant(pInvocation, NULL); - }) - - // Here we use the onUpdatedValue to set a callback that isn't exposed to BlueZ, but rather allows us to manage - // updates to our value. These updates may have come from our own server or some other source. - // - // We can handle updates in any way we wish, but the most common use is to send a change notification. - .onUpdatedValue(CHARACTERISTIC_UPDATED_VALUE_CALLBACK_LAMBDA - { - const char *pTextString = self.getDataPointer("text/string", ""); - self.sendChangeNotificationValue(pConnection, pTextString); - return true; + GVariant *pValue = g_variant_get_child_value(pParameters, 0); + self.setDataValue("room-1", g_variant_get_int16(pValue)); }) // GATT Descriptor: Characteristic User Description (0x2901) @@ -432,97 +319,29 @@ Server::Server(const std::string &serviceName, const std::string &advertisingNam // Standard descriptor "ReadValue" method call .onReadValue(DESCRIPTOR_METHOD_CALLBACK_LAMBDA { - const char *pDescription = "A mutable text string used for testing. Read and write to me, it tickles!"; + const char *pDescription = "temperature in room 1"; self.methodReturnValue(pInvocation, pDescription, true); }) .gattDescriptorEnd() .gattCharacteristicEnd() - .gattServiceEnd() - - // Custom ASCII time string service - // - // This service will simply return the result of asctime() of the current local time. It's a nice test service to provide - // a new value each time it is read. - - // Service: ASCII Time (custom: 00000001-1E3D-FAD4-74E2-97A033F1BFEE) - .gattServiceBegin("ascii_time", "00000001-1E3D-FAD4-74E2-97A033F1BFEE") - - // Characteristic: ASCII Time String (custom: 00000002-1E3D-FAD4-74E2-97A033F1BFEE) - .gattCharacteristicBegin("string", "00000002-1E3D-FAD4-74E2-97A033F1BFEE", {"read"}) + + // Characteristic: String value + .gattCharacteristicBegin("room2", "00000001-2", {"read", "write"}) // Standard characteristic "ReadValue" method call .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA { - // Get our local time string using asctime() - time_t timeVal = time(nullptr); - struct tm *pTimeStruct = localtime(&timeVal); - std::string timeString = Utils::trim(asctime(pTimeStruct)); - - self.methodReturnValue(pInvocation, timeString, true); + int8_t temperature = self.getDataValue("room-2", 0); + self.methodReturnValue(pInvocation, temperature, true); }) - // GATT Descriptor: Characteristic User Description (0x2901) - // - // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_user_description.xml - .gattDescriptorBegin("description", "2901", {"read"}) - - // Standard descriptor "ReadValue" method call - .onReadValue(DESCRIPTOR_METHOD_CALLBACK_LAMBDA - { - const char *pDescription = "Returns the local time (as reported by POSIX asctime()) each time it is read"; - self.methodReturnValue(pInvocation, pDescription, true); - }) - - .gattDescriptorEnd() - - .gattCharacteristicEnd() - .gattServiceEnd() - - // Custom CPU information service (custom: 0000B001-1E3D-FAD4-74E2-97A033F1BFEE) - // - // This is a cheezy little service that reads the CPU info from /proc/cpuinfo and returns the count and model of the - // CPU. It may not work on all platforms, but it does provide yet another example of how to do things. - - // Service: CPU Information (custom: 0000B001-1E3D-FAD4-74E2-97A033F1BFEE) - .gattServiceBegin("cpu", "0000B001-1E3D-FAD4-74E2-97A033F1BFEE") - - // Characteristic: CPU Count (custom: 0000B002-1E3D-FAD4-74E2-97A033F1BFEE) - .gattCharacteristicBegin("count", "0000B002-1E3D-FAD4-74E2-97A033F1BFEE", {"read"}) - - // Standard characteristic "ReadValue" method call - .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA - { - int16_t cpuCount = 0; - ServerUtils::getCpuInfo(cpuCount); - self.methodReturnValue(pInvocation, cpuCount, true); - }) - - // GATT Descriptor: Characteristic User Description (0x2901) - // - // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_user_description.xml - .gattDescriptorBegin("description", "2901", {"read"}) - - // Standard descriptor "ReadValue" method call - .onReadValue(DESCRIPTOR_METHOD_CALLBACK_LAMBDA - { - const char *pDescription = "This might represent the number of CPUs in the system"; - self.methodReturnValue(pInvocation, pDescription, true); - }) - - .gattDescriptorEnd() - - .gattCharacteristicEnd() - - // Characteristic: CPU Model (custom: 0000B003-1E3D-FAD4-74E2-97A033F1BFEE) - .gattCharacteristicBegin("model", "0000B003-1E3D-FAD4-74E2-97A033F1BFEE", {"read"}) - - // Standard characteristic "ReadValue" method call - .onReadValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA + // Standard characteristic "WriteValue" method call + .onWriteValue(CHARACTERISTIC_METHOD_CALLBACK_LAMBDA { - int16_t cpuCount = 0; - self.methodReturnValue(pInvocation, ServerUtils::getCpuInfo(cpuCount), true); + GVariant *pValue = g_variant_get_child_value(pParameters, 0); + self.setDataValue("room-2", g_variant_get_int16(pValue)); }) // GATT Descriptor: Characteristic User Description (0x2901) @@ -533,14 +352,16 @@ Server::Server(const std::string &serviceName, const std::string &advertisingNam // Standard descriptor "ReadValue" method call .onReadValue(DESCRIPTOR_METHOD_CALLBACK_LAMBDA { - const char *pDescription = "Possibly the model of the CPU in the system"; + const char *pDescription = "temperature in room 2"; self.methodReturnValue(pInvocation, pDescription, true); }) .gattDescriptorEnd() .gattCharacteristicEnd() - .gattServiceEnd(); // << -- NOTE THE SEMICOLON + + .gattServiceEnd() + ; // << -- NOTE THE SEMICOLON // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // ____ _____ ___ _____ diff --git a/src/standalone.cpp b/src/standalone.cpp index 20e4ade..16225f0 100644 --- a/src/standalone.cpp +++ b/src/standalone.cpp @@ -106,6 +106,7 @@ #include #include #include +#include #include "../include/Gobbledegook.h" @@ -120,11 +121,8 @@ static const int kMaxAsyncInitTimeoutMS = 30 * 1000; // Server data values // -// The battery level ("battery/level") reported by the server (see Server.cpp) -static uint8_t serverDataBatteryLevel = 78; - // The text string ("text/string") used by our custom text string service (see Server.cpp) -static std::string serverDataTextString = "Hello, world!"; +static std::map temperatures; // // Logging @@ -192,17 +190,9 @@ const void *dataGetter(const char *pName) } std::string strName = pName; + auto it = temperatures.find(strName); + if (it != temperatures.end()) return &(*it); - if (strName == "battery/level") - { - return &serverDataBatteryLevel; - } - else if (strName == "text/string") - { - return serverDataTextString.c_str(); - } - - LogWarn((std::string("Unknown name for server data getter request: '") + pName + "'").c_str()); return nullptr; } @@ -227,22 +217,10 @@ int dataSetter(const char *pName, const void *pData) std::string strName = pName; - if (strName == "battery/level") - { - serverDataBatteryLevel = *static_cast(pData); - LogDebug((std::string("Server data: battery level set to ") + std::to_string(serverDataBatteryLevel)).c_str()); - return 1; - } - else if (strName == "text/string") - { - serverDataTextString = static_cast(pData); - LogDebug((std::string("Server data: text string set to '") + serverDataTextString + "'").c_str()); - return 1; - } - - LogWarn((std::string("Unknown name for server data setter request: '") + pName + "'").c_str()); - - return 0; + int16_t temp = *static_cast(pData); + temperatures[strName]=temp; + LogDebug((std::string("Server data: temperature of ")+strName+" set to " + std::to_string(temp)).c_str()); + return 1; } // @@ -299,20 +277,15 @@ int main(int argc, char **ppArgv) // This first parameter (the service name) must match tha name configured in the D-Bus permissions. See the Readme.md file // for more information. // - if (!ggkStart("gobbledegook", "Gobbledegook", "Gobbledegook", dataGetter, dataSetter, kMaxAsyncInitTimeoutMS)) + if (!ggkStart("termostore", "termostore", "thermo", dataGetter, dataSetter, kMaxAsyncInitTimeoutMS)) { return -1; } // Wait for the server to start the shutdown process - // - // While we wait, every 15 ticks, drop the battery level by one percent until we reach 0 while (ggkGetServerRunState() < EStopping) { std::this_thread::sleep_for(std::chrono::seconds(15)); - - serverDataBatteryLevel = std::max(serverDataBatteryLevel - 1, 0); - ggkNofifyUpdatedCharacteristic("/com/gobbledegook/battery/level"); } // Wait for the server to come to a complete stop (CTRL-C from the command line) -- cgit v1.2.3