#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
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
}\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
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
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
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
{\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
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
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
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
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
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
// 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
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
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
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
}\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
*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