[IOT-3044]Adding token to empty signaling message 91/25491/8
authorkoushik.girijala <g.koushik@samsung.com>
Mon, 14 May 2018 10:12:03 +0000 (15:42 +0530)
committerNathan Heldt-Sheller <nathan.heldt-sheller@intel.com>
Tue, 26 Jun 2018 01:28:08 +0000 (01:28 +0000)
Support for adding token to empty signaling message
Draft changes in coap and IoTivity

Change-Id: I7be3da0c09b41516d57c02cef98a6815dd1950bb
Signed-off-by: koushik.girijala <g.koushik@samsung.com>
resource/csdk/connectivity/lib/libcoap-4.1.1/include/coap/pdu.h
resource/csdk/connectivity/lib/libcoap-4.1.1/pdu.c
resource/csdk/connectivity/src/caping.c
resource/csdk/connectivity/src/caprotocolmessage.c

index 417473f..9baa0ed 100644 (file)
@@ -582,6 +582,16 @@ int coap_add_token(coap_pdu_t *pdu,
 int coap_add_token2(coap_pdu_t *pdu, size_t len, const unsigned char *data,
                     coap_transport_t transport);
 
+/**
+ * @param pdu  The pdu pointer.
+ * @param len  The length of the new token.
+ * @param data The token to add.
+ * @param transport The transport type.
+ * @return A value greater than zero on success, or @c 0 on error.
+ */
+int coap_add_token_to_empty_message(coap_pdu_t *pdu, size_t len, const unsigned char *data,
+                                    coap_transport_t transport);
+
 /**
  * Get token from coap header
  *
@@ -604,7 +614,7 @@ void coap_get_token2(const coap_hdr_transport_t *pdu_hdr, coap_transport_t trans
                      unsigned char **token, unsigned int *token_length);
 
 /**
- * Adds option of given type to pdu that is passed as first parameter. 
+ * Adds option of given type to pdu that is passed as first parameter.
  * coap_add_option() destroys the PDU's data, so coap_add_data() must be called
  * after all options have been added. As coap_add_token() destroys the options
  * following the token, the token must be added before coap_add_option() is
index 4079fb8..895b17f 100644 (file)
@@ -625,6 +625,63 @@ int coap_add_token2(coap_pdu_t *pdu, size_t len, const unsigned char *data,
     return 1;
 }
 
+int coap_add_token_to_empty_message(coap_pdu_t *pdu, size_t len, const unsigned char *data,
+                                    coap_transport_t transport)
+{
+    const size_t HEADERLENGTH = len;
+    /* must allow for pdu == NULL as callers may rely on this */
+    if (!pdu || len > 8)
+        return 0;
+
+    unsigned char* token = NULL;
+    switch(transport)
+    {
+        case COAP_UDP:
+            pdu->transport_hdr->udp.token_length = len;
+            token = pdu->transport_hdr->udp.token;
+            pdu->length = HEADERLENGTH;
+            break;
+#ifdef WITH_TCP
+        case COAP_TCP:
+            pdu->transport_hdr->tcp.header_data[0] =
+                    pdu->transport_hdr->tcp.header_data[0] | len;
+            token = pdu->transport_hdr->tcp.token;
+            pdu->length = len + COAP_TCP_HEADER_NO_FIELD;
+            break;
+        case COAP_TCP_8BIT:
+            pdu->transport_hdr->tcp_8bit.header_data[0] =
+                    pdu->transport_hdr->tcp_8bit.header_data[0] | len;
+            token = pdu->transport_hdr->tcp_8bit.token;
+            pdu->length = len + COAP_TCP_HEADER_8_BIT;
+            break;
+        case COAP_TCP_16BIT:
+            pdu->transport_hdr->tcp_16bit.header_data[0] =
+                    pdu->transport_hdr->tcp_16bit.header_data[0] | len;
+            token = pdu->transport_hdr->tcp_16bit.token;
+            pdu->length = len + COAP_TCP_HEADER_16_BIT;
+            break;
+        case COAP_TCP_32BIT:
+            pdu->transport_hdr->tcp_32bit.header_data[0] =
+                    pdu->transport_hdr->tcp_32bit.header_data[0] | len;
+            token = pdu->transport_hdr->tcp_32bit.token;
+            pdu->length = len + COAP_TCP_HEADER_32_BIT;
+            break;
+#endif
+        default:
+            debug("it has wrong type\n");
+    }
+
+    if (len)
+    {
+        memcpy(token, data, len);
+    }
+
+    pdu->max_delta = 0;
+    pdu->data = NULL;
+
+    return 1;
+}
+
 void coap_get_token(const coap_hdr_t *pdu_hdr,
                     unsigned char **token, unsigned int *token_length)
 {
index c2d6589..4f71884 100644 (file)
@@ -52,6 +52,7 @@ static uint64_t g_timeout = CA_DEFAULT_PING_TIMEOUT;
 static void CADestroyPingInfo(PingInfo* pingInfo)
 {
     pingInfo->cbData.cd(pingInfo->cbData.context);
+    OICFree(pingInfo->token);
     OICFree(pingInfo);
 }
 
@@ -147,7 +148,9 @@ CAResult_t CASendPingMessage(const CAEndpoint_t *endpoint, bool withCustody, CAP
     // according to the timestamp
     cur->endpoint = *endpoint;
     cur->withCustody = withCustody;
-    OICStrcpy(cur->token, data->signalingInfo->info.tokenLength, data->signalingInfo->info.token);
+    cur->token = (char *)OICCalloc(data->signalingInfo->info.tokenLength, sizeof(char));
+    memcpy(cur->token, data->signalingInfo->info.token, data->signalingInfo->info.tokenLength);
+    cur->tokenLength = data->signalingInfo->info.tokenLength;
     cur->timeStamp = OICGetCurrentTime(TIME_IN_MS);
     cur->cbData = *cbData;
     oc_mutex_lock(g_pingInfoListMutex);
index 42a835a..650fd09 100644 (file)
@@ -374,10 +374,25 @@ coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
         OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
         OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
 
-        int32_t ret = coap_add_token2(pdu, tokenLength, (unsigned char *)info->token, *transport);
-        if (0 == ret)
+        if (info->payloadSize == 0 && (code == CA_CSM || code == CA_PING ||
+                                       code == CA_PONG || code == CA_RELEASE ||
+                                       code == CA_ABORT))
         {
-            OIC_LOG(ERROR, TAG, "can't add token");
+            int32_t ret = coap_add_token_to_empty_message(pdu, tokenLength,
+                            (unsigned char *)info->token, *transport);
+            if (0 == ret)
+            {
+                OIC_LOG(ERROR, TAG, "can't add token");
+            }
+        }
+        else
+        {
+            int32_t ret = coap_add_token2(pdu, tokenLength, (unsigned char *)info->token,
+                            *transport);
+            if (0 == ret)
+            {
+                OIC_LOG(ERROR, TAG, "can't add token");
+            }
         }
     }