Implementation of connectivity abstraction feature Release v0.61
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caprotocolmessage.c
index 988d700..ee9deaf 100644 (file)
@@ -40,8 +40,8 @@
 #include <time.h>\r
 #endif //#ifdef __ARDUINO__\r
 \r
-uint32_t CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, \r
-                                            char *outUri)\r
+uint32_t CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo,\r
+                                 char *outUri)\r
 {\r
     OIC_LOG(DEBUG, TAG, "CAGetRequestInfoFromPdu IN");\r
 \r
@@ -55,8 +55,8 @@ uint32_t CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqI
     return 1;\r
 }\r
 \r
-uint32_t CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, \r
-                                            char *outUri)\r
+uint32_t CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,\r
+                                  char *outUri)\r
 {\r
     OIC_LOG(DEBUG, TAG, "CAGetResponseInfoFromPdu IN");\r
     if (NULL == pdu)\r
@@ -139,7 +139,7 @@ coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
 }\r
 \r
 coap_pdu_t *CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *options,\r
-        const char* payload, const CAInfo_t info)\r
+                                             const char *payload, const CAInfo_t info)\r
 {\r
     OIC_LOG(DEBUG, TAG, "CACreatePDUforRequestWithPayload IN");\r
 \r
@@ -153,19 +153,30 @@ coap_pdu_t *CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *opt
     if (!(pdu = coap_new_pdu()))\r
         return NULL;\r
 \r
-    /* initialize message id */\r
     unsigned short message_id;\r
-    prng((unsigned char * )&message_id, sizeof(unsigned short));\r
+\r
+    if (info.messageId == 0)\r
+    {\r
+        /* initialize message id */\r
+        prng((unsigned char * )&message_id, sizeof(unsigned short));\r
+        ++message_id;\r
+\r
+        OIC_LOG_V(DEBUG, TAG, "generate the message id(%d)", message_id);\r
+    }\r
+    else\r
+    {\r
+        /* use saved message id */\r
+        message_id = info.messageId;\r
+    }\r
 \r
     pdu->hdr->type = info.type;\r
-    ++message_id;\r
     pdu->hdr->id = htons(message_id);\r
     pdu->hdr->code = code;\r
 \r
     if (info.token)\r
     {\r
         pdu->hdr->token_length = CA_MAX_TOKEN_LEN;\r
-        if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char*) info.token))\r
+        if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char *) info.token))\r
         {\r
             OIC_LOG(DEBUG, TAG, "cannot add token to request");\r
         }\r
@@ -195,8 +206,8 @@ coap_pdu_t *CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *opt
     return pdu;\r
 }\r
 \r
-coap_pdu_t* CACreatePDUforRequest(const code_t code, coap_list_t *options, \r
-    const CAInfo_t info)\r
+coap_pdu_t *CACreatePDUforRequest(const code_t code, coap_list_t *options,\r
+                                  const CAInfo_t info)\r
 {\r
     OIC_LOG(DEBUG, TAG, "CACreatePDUforRequest IN");\r
 \r
@@ -209,19 +220,30 @@ coap_pdu_t* CACreatePDUforRequest(const code_t code, coap_list_t *options,
         return NULL;\r
     }\r
 \r
-    /* initialize message id */\r
     unsigned short message_id;\r
-    prng((unsigned char * )&message_id, sizeof(unsigned short));\r
+\r
+    if (info.messageId == 0)\r
+    {\r
+        /* initialize message id */\r
+        prng((unsigned char * )&message_id, sizeof(unsigned short));\r
+        ++message_id;\r
+\r
+        OIC_LOG_V(DEBUG, TAG, "generate the message id(%d)", message_id);\r
+    }\r
+    else\r
+    {\r
+        /* use saved message id */\r
+        message_id = info.messageId;\r
+    }\r
 \r
     pdu->hdr->type = info.type;\r
-    ++message_id;\r
     pdu->hdr->id = htons(message_id);\r
     pdu->hdr->code = code;\r
 \r
     OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, strlen(info.token));\r
     pdu->hdr->token_length = CA_MAX_TOKEN_LEN;\r
 \r
-    if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char*) info.token))\r
+    if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char *) info.token))\r
     {\r
         OIC_LOG(DEBUG, TAG, "cannot add token to request");\r
     }\r
@@ -256,7 +278,7 @@ void CAParseURI(const char *uriInfo, coap_list_t **optlist)
     {\r
         coap_insert(optlist,\r
                     CACreateNewOptionNode(COAP_OPTION_URI_PORT,\r
-                                          coap_encode_var_bytes(portbuf, uri.port), portbuf), \r
+                                          coap_encode_var_bytes(portbuf, uri.port), portbuf),\r
                                           CAOrderOpts);\r
     }\r
 \r
@@ -312,7 +334,7 @@ void CAParseHeadOption(const uint32_t code, const CAInfo_t info, coap_list_t **o
             OIC_LOG_V(DEBUG, TAG, "Head Option length: %d", info.options[i].optionLength);\r
 \r
             coap_insert(optlist,\r
-                        CACreateNewOptionNode(info.options[i].optionID, \r
+                        CACreateNewOptionNode(info.options[i].optionID,\r
                                             info.options[i].optionLength,\r
                                               info.options[i].optionData), CAOrderOpts);\r
         }\r
@@ -320,8 +342,8 @@ void CAParseHeadOption(const uint32_t code, const CAInfo_t info, coap_list_t **o
     OIC_LOG(DEBUG, TAG, "CAParseHeadOption OUT");\r
 }\r
 \r
-coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length, \r
-                                           const uint8_t *data)\r
+coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length,\r
+                                   const uint8_t *data)\r
 {\r
     OIC_LOG(DEBUG, TAG, "CACreateNewOptionNode IN");\r
     coap_option *option;\r
@@ -333,7 +355,7 @@ coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length,
         OIC_LOG(DEBUG, TAG, "Out of memory");\r
         return NULL;\r
     }\r
-    memset(option, 0, sizeof(coap_option)+length);\r
+    memset(option, 0, sizeof(coap_option) + length);\r
 \r
     COAP_OPTION_KEY(*option) = key;\r
     COAP_OPTION_LENGTH(*option) = length;\r
@@ -380,7 +402,9 @@ uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
         if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)\r
         {\r
 \r
-        } else {\r
+        }\r
+        else\r
+        {\r
             count++;\r
         }\r
     }\r
@@ -388,8 +412,8 @@ uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
     return count;\r
 }\r
 \r
-void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, \r
-                                char *outUri)\r
+void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo,\r
+                         char *outUri)\r
 {\r
     OIC_LOG(DEBUG, TAG, "CAGetRequestPDUInfo IN");\r
     char buf[COAP_MAX_PDU_SIZE]; /* need some space for output creation */\r
@@ -417,7 +441,10 @@ void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *out
     // set type\r
     outInfo->type = pdu->hdr->type;\r
 \r
-    if(count > 0)\r
+    // set message id\r
+    outInfo->messageId = ntohs(pdu->hdr->id);\r
+\r
+    if (count > 0)\r
     {\r
         outInfo->options = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * count);\r
         if (outInfo->options == NULL)\r
@@ -431,9 +458,9 @@ void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *out
     while ((option = coap_option_next(&opt_iter)))\r
     {\r
 \r
-        if (CAGetOptionData((uint8_t*)(COAP_OPT_VALUE(option)), \r
-            COAP_OPT_LENGTH(option), (uint8_t*)buf, sizeof(buf),\r
-                           encode))\r
+        if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),\r
+                            COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf),\r
+                            encode))\r
         {\r
             if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)\r
             {\r
@@ -459,14 +486,16 @@ void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *out
                     memcpy(optionResult + optionLength, buf, strlen((const char *) buf));\r
                     optionLength += strlen((const char *) buf);\r
                 }\r
-            } else {\r
+            }\r
+            else\r
+            {\r
 \r
                 if (idx < count)\r
                 {\r
                     uint32_t length = (uint32_t) strlen((const char *) buf);\r
 \r
-                if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)\r
-                {\r
+                    if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)\r
+                    {\r
                         outInfo->options[idx].optionID = opt_iter.type;\r
                         outInfo->options[idx].optionLength = length;\r
                         outInfo->options[idx].protocolID = CA_COAP_ID;\r
@@ -484,11 +513,11 @@ void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *out
         OIC_LOG(DEBUG, TAG, "inside pdu->hdr->token_length");\r
         outInfo->token = (char *) OICMalloc(CA_MAX_TOKEN_LEN);\r
         if (outInfo->token == NULL)\r
-         {\r
-             OIC_LOG(DEBUG, TAG, "CAGetRequestPDUInfo, Memory allocation failed !");\r
-             OICFree(outInfo->options);\r
-             return;\r
-         }\r
+        {\r
+            OIC_LOG(DEBUG, TAG, "CAGetRequestPDUInfo, Memory allocation failed !");\r
+            OICFree(outInfo->options);\r
+            return;\r
+        }\r
         memcpy(outInfo->token, pdu->hdr->token, CA_MAX_TOKEN_LEN);\r
     }\r
 \r
@@ -592,7 +621,7 @@ void CADeinitialize(CAInfo_t *info)
 }\r
 \r
 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *result,\r
-        uint32_t buflen, uint32_t encode_always)\r
+                         uint32_t buflen, uint32_t encode_always)\r
 {\r
     const unsigned char hex[] = "0123456789ABCDEF";\r
     uint32_t cnt = 0;\r
@@ -631,3 +660,25 @@ uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *result,
     *result = '\0';\r
     return cnt;\r
 }\r
+\r
+CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void* pdu, uint32_t size)\r
+{\r
+    // pdu minimum size is 4 byte.\r
+    if (size < 4)\r
+        return CA_MSG_NONCONFIRM;\r
+\r
+    coap_hdr_t* hdr = (coap_hdr_t*) pdu;\r
+\r
+    return (CAMessageType_t) hdr->type;\r
+}\r
+\r
+uint16_t CAGetMessageIdFromPduBinaryData(const void* pdu, uint32_t size)\r
+{\r
+    // pdu minimum size is 4 byte.\r
+    if (size < 4)\r
+        return 0;\r
+\r
+    coap_hdr_t* hdr = (coap_hdr_t*) pdu;\r
+\r
+    return ntohs(hdr->id);\r
+}\r