CoAP over TCP transmission over BLE support for arduino
authorvimala.v <vimala.v@samsung.com>
Fri, 26 Feb 2016 08:46:15 +0000 (14:16 +0530)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 2 Mar 2016 03:55:16 +0000 (03:55 +0000)
Change-Id: I5b7a775a85fab47c04c8dbbf45b064e796985209
Signed-off-by: vimala.v <vimala.v@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5225
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/connectivity/inc/caleinterface.h
resource/csdk/connectivity/lib/libcoap-4.1.1/pdu.h
resource/csdk/connectivity/src/bt_le_adapter/arduino/cableserver.cpp
resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c

index b1701a1..a027a60 100644 (file)
@@ -411,7 +411,6 @@ void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback);
  *                     adapter.
  */
 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback);
-
 #ifdef __cplusplus
 }
 #endif
index 6f8f175..d461913 100644 (file)
@@ -316,7 +316,10 @@ typedef struct
 
 /** Options in coap_pdu_t are accessed with the macro COAP_OPTION. */
 #define COAP_OPTION(node) ((coap_option *)(node)->options)
-
+#ifdef __cplusplus
+extern "C"
+{
+#endif
 #ifdef WITH_LWIP
 /**
  * Creates a CoAP PDU from an lwIP @p pbuf, whose reference is passed on to
@@ -550,5 +553,7 @@ int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data);
  * destroyed with the pdu.
  */
 int coap_get_data(const coap_pdu_t *pdu, size_t *len, unsigned char **data);
-
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
 #endif /* _PDU_H_ */
index bf74ee1..8cd2fee 100644 (file)
 #include <boards.h>
 #include <RBL_nRF8001.h>
 
+#include "pdu.h"
 #include "caleinterface.h"
 #include "oic_malloc.h"
 #include "caadapterutils.h"
 #include "cafragmentation.h"
 
 #define TAG "LES"
+/**
+ * Maximum TCP header length.
+ */
+#define TCP_MAX_HEADER_LENGTH 6
 
 /**
- * @var    g_bleServerDataReceivedCallback
- * @brief  Maintains the callback to be notified on receival of network packets from other
- *           BLE devices
+ * Maintains the callback to be notified on receival of network packets from other
+ * BLE devices
  */
 static CABLEDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
 
 /**
- * @def MAX_EVENT_COUNT
- * @brief Maximum number of tries to get the event on BLE Shield address.
+ * Maximum number of tries to get the event on BLE Shield address.
  */
 #define MAX_EVENT_COUNT 20
 
@@ -53,16 +56,21 @@ static bool g_serverRunning = false;
 static uint8_t *g_coapBuffer = NULL;
 
 /**
- * @var g_receivedDataLen
- * @brief Actual length of data received.
+ * Actual length of data received.
  */
-static uint32_t g_receivedDataLen = 0;
+static size_t g_receivedDataLen = 0;
 
 /**
- * @var g_packetDataLen
- * @brief Total Length of data that is being fragmented.
+ * Total Length of data that is being fragmented.
  */
-static uint32_t g_packetDataLen = 0;
+static size_t g_packetDataLen = 0;
+
+void CAGetTCPHeaderDetails(unsigned char* recvBuffer, size_t *headerlen)
+{
+    coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
+        ((unsigned char *)recvBuffer)[0] >> 4);
+    *headerlen = coap_get_tcp_header_length_for_transport(transport);
+}
 
 void CACheckLEDataInternal()
 {
@@ -74,32 +82,43 @@ void CACheckLEDataInternal()
         if (NULL == g_coapBuffer)
         {
             OIC_LOG(DEBUG, TAG, "IN");
-            uint8_t headerArray[CA_HEADER_LENGTH];
-            while (CAIsLEDataAvailable() && g_receivedDataLen < CA_HEADER_LENGTH)
+            size_t bufSize = TCP_MAX_HEADER_LENGTH;
+            g_coapBuffer = (uint8_t *)OICCalloc(bufSize, 1);
+            if (NULL == g_coapBuffer)
             {
-                headerArray[g_receivedDataLen++] = CALEReadData();
+                OIC_LOG(ERROR, TAG, "malloc");
+                return;
             }
 
-            g_packetDataLen = CAParseHeader(headerArray, CA_HEADER_LENGTH);
+            g_coapBuffer[g_receivedDataLen++] = CALEReadData();
+            size_t headerLen;
+            CAGetTCPHeaderDetails(g_coapBuffer, &headerLen);
+            OIC_LOG_V(INFO, TAG, "hdr len %d", headerLen);
+            while (CAIsLEDataAvailable() && g_receivedDataLen < headerLen)
+            {
+                g_coapBuffer[g_receivedDataLen++] = CALEReadData();
+            }
 
+            g_packetDataLen = coap_get_total_message_length(g_coapBuffer, g_receivedDataLen);
+            OIC_LOG_V(INFO, TAG, "pkt len %d", g_packetDataLen);
             if (g_packetDataLen > COAP_MAX_PDU_SIZE)
             {
                 OIC_LOG(ERROR, TAG, "len > pdu_size");
                 return;
             }
 
-            g_coapBuffer = (uint8_t *)OICCalloc((size_t)g_packetDataLen, 1);
-            if (NULL == g_coapBuffer)
+            bufSize = g_packetDataLen;
+            uint8_t *newBuf = (uint8_t *)OICRealloc(g_coapBuffer, bufSize);
+            if (NULL == newBuf)
             {
                 OIC_LOG(ERROR, TAG, "malloc");
+                OICFree(g_coapBuffer);
+                g_coapBuffer = NULL;
                 return;
             }
-
-            OIC_LOG(DEBUG, TAG, "OUT");
-            g_receivedDataLen = 0;
+            g_coapBuffer = newBuf;
         }
 
-        OIC_LOG(DEBUG, TAG, "IN");
         while (CAIsLEDataAvailable())
         {
             OIC_LOG(DEBUG, TAG, "In While loop");
@@ -117,6 +136,7 @@ void CACheckLEDataInternal()
                 }
 
                 g_receivedDataLen = 0;
+                OICFree(g_coapBuffer);
                 g_coapBuffer = NULL;
                 break;
             }
index b873a8f..1e10f5f 100644 (file)
@@ -551,21 +551,6 @@ static void CALEClearSenderInfo()
     CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
 }
 
-static size_t CAGetMessageLengthFromData(const unsigned char *recvBuffer)
-{
-    coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
-            ((unsigned char *)recvBuffer)[0] >> 4);
-    size_t optPaylaodLen = coap_get_length_from_header((unsigned char *)recvBuffer,
-                                                        transport);
-    size_t headerLen = coap_get_tcp_header_length((unsigned char *)recvBuffer);
-
-    OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "option/paylaod length [%d]", optPaylaodLen);
-    OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "header length [%d]", headerLen);
-    OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "total data length [%d]", headerLen + optPaylaodLen);
-
-    return headerLen + optPaylaodLen;
-}
-
 static CAResult_t CAInitLEClientSenderQueue()
 {
     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
@@ -730,7 +715,7 @@ static void CALEDataReceiverHandler(void *threadData)
 
             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
 
-            newSender->totalDataLen = CAGetMessageLengthFromData(bleData->data);
+            newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
 
             if(!(newSender->totalDataLen))
             {
@@ -2273,30 +2258,13 @@ static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
 
 #ifdef SINGLE_THREAD
-    uint8_t header[CA_HEADER_LENGTH] = { 0 };
-
-    CAResult_t result =
-        CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
-
-    if (CA_STATUS_OK != result)
-    {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
-        return CA_STATUS_FAILED;
-    }
-
     if (!CAIsLEConnected())
     {
         OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
         return CA_STATUS_FAILED;
     }
 
-    result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
-    if (CA_STATUS_OK != result)
-    {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
-        return CA_STATUS_FAILED;
-    }
-
+    CAResult_t result = CA_STATUS_OK;
     const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
     for (uint32_t iter = 0; iter < dataLimit; iter++)
     {
@@ -2518,4 +2486,4 @@ static void CALEErrorHandler(const char *remoteAddress,
     CAFreeEndpoint(rep);
 
     OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
-}
+}
\ No newline at end of file