Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / linux / server.c
index 828183c..be7302d 100644 (file)
@@ -1,4 +1,4 @@
-/******************************************************************
+/* ****************************************************************
  *
  * Copyright 2015 Intel Corporation All Rights Reserved.
  *
  *
  ******************************************************************/
 
+
 #include "server.h"
+#include "service.h"
+#include "peripheral.h"
 
-#include "cacommon.h"
 #include "logger.h"
 
 #include <assert.h>
 // Logging tag.
 static char const TAG[] = "BLE_SERVER";
 
-// ---------------------------------------------------------------------
-//                      GATT Request Handling
-// ---------------------------------------------------------------------
-void CAGattServerHandleRequestData()
-{
-}
-
-// ---------------------------------------------------------------------
-//                      GATT Response Handling
-// ---------------------------------------------------------------------
-/**
- * Send response data to the GATT client.
- *
- * Respone data will be sent to the client through the given response
- * @a characteristic proxy as a GATT characteristic notification.
- *
- * @param[in] characteristic The D-Bus proxy for the response
- *                           characteristic through which the
- *                           notification will be sent.
- * @param[in] data           The byte array to be sent.
- * @param[in] length         The number of elements in the byte
- *                           array.
- */
-static bool CAGattServerSendResponseNotification(
-    GattCharacteristic1 * characteristic,
-    char const * data,
+static CAResult_t CAGattServerSendResponseNotificationImpl(
+    CAGattService * service,
+    uint8_t const * data,
     size_t length)
 {
+    assert(service != NULL);
+
+    GattCharacteristic1 * const characteristic =
+        service->response_characteristic.characteristic;
+
     if (!gatt_characteristic1_get_notifying(characteristic))
     {
         OIC_LOG(WARNING,
@@ -64,7 +48,7 @@ static bool CAGattServerSendResponseNotification(
                 "Client must enable notifications. "
                 "No response was sent.");
 
-        return false;
+        return CA_STATUS_FAILED;
     }
 
     GVariant * const value =
@@ -73,38 +57,78 @@ static bool CAGattServerSendResponseNotification(
                                   length,
                                   sizeof(data[0]));
 
-    /**
-     * Send the response fragment by setting the "Value" property on
-     * the response characteristic, and emitting the
-     * @c org.freedesktop.Dbus.Properties.PropertiesChanged signal,
-     * accordingly.
-     *
-     * @todo Do we need to explicitly emit the @c GObject @c notify or
-     *       @c org.freedesktop.Dbus.Properties.PropertiesChanged
-     *       signal here?
-     */
+    /*
+      Send the response fragment by setting the "Value" property on
+      the response characteristic, and emitting the
+      org.freedesktop.Dbus.Properties.PropertiesChanged signal,
+      accordingly.
+
+      @todo Do we need to explicitly emit the GObject notify or
+            org.freedesktop.Dbus.Properties.PropertiesChanged
+            signal here?
+
+      @todo It feels like this function should be part of the
+            response characteristic implementation.
+
+      @todo Do we need to decrease the ref count after setting the
+            value, i.e. is ownership of the value transferred to the
+            characteristic object?
+    */
     gatt_characteristic1_set_value(characteristic, value);
 
-    return true;
+    return CA_STATUS_OK;
 }
 
-bool CAGattServerSendResponse(void const * method_info,
-                              void const * data,
-                              size_t length)
+typedef struct _CAGattServerResponseInfo
 {
-    assert(method_info != NULL);
+    uint8_t const * const data;
+    size_t const length;
+    CAResult_t result;
 
-    CAGattResponseInfo const * const info = method_info;
+} CAGattServerResponseInfo;
 
-    GattCharacteristic1 * const characteristic =
-        info->characteristic;
+static void CAGattServerSendResponseNotificationToService(gpointer data,
+                                                          gpointer user_data)
+{
+    CAGattService * const service = data;
+    CAGattServerResponseInfo * const info = user_data;
+
+    CAResult_t const result =
+        CAGattServerSendResponseNotificationImpl(service,
+                                                 info->data,
+                                                 info->length);
 
-    if (!CAGattServerSendResponseNotification(characteristic,
-                                              (char const *) data,
-                                              length))
+    if (result != CA_STATUS_OK)
     {
-        return false;
+        // Propagate failure if any send operation fails.
+        info->result = result;
     }
+}
+
+CAResult_t CAGattServerSendResponseNotification(
+    char const * address,
+    uint8_t const * data,
+    size_t length)
+{
+    CAGattService * const s = CAGattServiceDecodeAddress(address);
+
+    return CAGattServerSendResponseNotificationImpl(s, data, length);
+}
+
+CAResult_t CAGattServerSendResponseNotificationToAll(
+    uint8_t const * data,
+    size_t length)
+{
+    CAGattServerResponseInfo info =
+    {
+        .data   = data,
+        .length = length,
+        .result = CA_STATUS_OK
+    };
+
+    CAPeripheralForEachService(
+        CAGattServerSendResponseNotificationToService,
+        &info);
 
-    return true;
+    return info.result;
 }