From a39f962d10f9017fca649f21228648bf653f6a26 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Thu, 19 Mar 2015 23:43:53 -0700 Subject: [PATCH] Unify caprotocolmessage to fix bugs in different versions. Unified the caprotocolmessage sources to a single version that brings together bug fixes that had gone individually into either one copy or the other, but not both. Will also allow testing of the arduino-centric code under Linux builds. Follow-up to https://gerrit.iotivity.org/gerrit/#/c/470/ Fixes IoT-400 Change-Id: I47b726c940ef1dd45a26cf160b92e1cbbd7f5f2e Signed-off-by: Jon A. Cruz Reviewed-on: https://gerrit.iotivity.org/gerrit/583 Tested-by: jenkins-iotivity Reviewed-by: Erich Keane --- resource/csdk/connectivity/inc/caprotocolmessage.h | 36 +- .../inc/caprotocolmessage_singlethread.h | 208 ------ resource/csdk/connectivity/src/SConscript | 2 +- .../src/caconnectivitymanager_singlethread.c | 2 +- .../src/camessagehandler_singlethread.c | 2 +- resource/csdk/connectivity/src/caprotocolmessage.c | 269 ++++---- .../src/caprotocolmessage_singlethread.c | 750 --------------------- .../src/caretransmission_singlethread.c | 2 +- 8 files changed, 169 insertions(+), 1102 deletions(-) delete mode 100644 resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h delete mode 100644 resource/csdk/connectivity/src/caprotocolmessage_singlethread.c diff --git a/resource/csdk/connectivity/inc/caprotocolmessage.h b/resource/csdk/connectivity/inc/caprotocolmessage.h index 0dd65a4..e0e9b94 100644 --- a/resource/csdk/connectivity/inc/caprotocolmessage.h +++ b/resource/csdk/connectivity/inc/caprotocolmessage.h @@ -45,10 +45,10 @@ typedef uint32_t code_t; * @brief generates pdu structure from the given information. * @param uri [IN] uri information of the pdu * @param code [IN] code of the pdu packet - * @param info [IN] pdu information such as request code, response code, payload + * @param info [IN] pdu information such as request code, response code and payload * @return coap_pdu_t created pdu */ -coap_pdu_t *CAGeneratePdu(const char *uri, const uint32_t code, const CAInfo_t info); +coap_pdu_t *CAGeneratePdu(const char *uri, uint32_t code, const CAInfo_t info); /** * function for generating @@ -62,8 +62,8 @@ coap_pdu_t *CAGeneratePdu(const char *uri, const uint32_t code, const CAInfo_t i * @param buflen [IN] Buffer Length for outUri parameter * @return None */ -void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, char *outUri, - uint32_t buflen); +void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, + char *outUri, uint32_t buflen); /** * @brief extracts response information from received pdu. @@ -73,24 +73,24 @@ void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, * @param buflen [IN] Buffer Length for outUri parameter * @return None */ -void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, char *outUri, - uint32_t buflen); +void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, + char *outUri, uint32_t buflen); /** * @brief creates pdu from the request information * @param code [IN] request or response code - * @param options [OUT] options for the request and response + * @param options [OUT] options for the request and response * @param info [IN] information to create pdu * @param payload [IN] payload for the request or response consumed * @return coap_pdu_t */ -coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, const CAInfo_t info, - const char *payload); +coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, + const CAInfo_t info, const char *payload); /** * @brief parse the URI and creates the options * @param uriInfo [IN] uri information - * @param options [OUT] options information + * @param options [OUT] options information * @return None */ void CAParseURI(const char *uriInfo, coap_list_t **options); @@ -102,20 +102,22 @@ void CAParseURI(const char *uriInfo, coap_list_t **options); * @param optlist [OUT] options information * @return None */ -void CAParseHeadOption(const uint32_t code, const CAInfo_t info, coap_list_t **optlist); +void CAParseHeadOption(uint32_t code, const CAInfo_t info, coap_list_t **optlist); /** - * @brief creates option node from key length and data + * Creates option node from key length and data. + * Need to replace queue head if new node has to be added before the existing + * queue head * @param key [IN] key for the that needs to be sent * @param length [IN] length of the data that needs to be sent * @param data [IN] data that needs to be sent * @return created list */ -coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length, const uint8_t *data); +coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, + const uint8_t *data); /** * @brief order the inserted options - * need to replace queue head if new node has to be added before the existing queue head * @param a [IN] option 1 for insertion * @param b [IN] option 2 for insertion * @return 0 or 1 @@ -133,7 +135,7 @@ uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter); * @brief gets option data * @param data [IN] data that is received * @param length [IN] length of the data - * @param option [OUT] option + * @param option [OUT] result of the operation * @param buflen [IN] buffer length of the result * @return option count */ @@ -148,8 +150,8 @@ uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uin * @param buflen [IN] Buffer Length for outUri parameter * @return None */ -void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, char *outUri, - uint32_t buflen); +void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, + char *outUri, uint32_t buflen); /** * @brief create pdu from received data diff --git a/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h b/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h deleted file mode 100644 index 1581559..0000000 --- a/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h +++ /dev/null @@ -1,208 +0,0 @@ -/* **************************************************************** - * - * Copyright 2014 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -/** - * @file - * - * This file contains common function for handling protocol messages. - */ - -#ifndef __CA_PROTOCOL_MESSAGE_SINGLETHREAD_H_ -#define __CA_PROTOCOL_MESSAGE_SINGLETHREAD_H_ - -#include "cacommon.h" -#include "config.h" -#include "coap.h" -#include "debug.h" - -#define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) ) -#define ntohs(x) htons(x) - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef uint32_t code_t; - -/** - * @brief generates pdu structure from the given information. - * @param uri [IN] uri information of the pdu - * @param code [IN] code of the pdu packet - * @param info [IN] pdu information such as request code ,response code and payload - * @return coap_pdu_t created pdu - */ -coap_pdu_t *CAGeneratePdu(const char *uri, uint32_t code, const CAInfo_t info); - -/** - * function for generating - */ - -/** - * @brief extracts request information from received pdu. - * @param pdu [IN] received pdu - * @param outReqInfo [OUT] request info structure made from received pdu - * @param outUri [OUT] uri received in the received pdu - * @param buflen [IN] Buffer Length for outUri parameter - * @return NONE - */ -void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, - char *outUri, uint32_t bufLen); - -/** - * @brief extracts response information from received pdu. - * @param pdu [IN] received pdu - * @param outResInfo [OUT] response info structure made from received pdu - * @param outUri [OUT] uri received in the received pdu - * @param buflen [IN] Buffer Length for outUri parameter - * @return NONE - */ -void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, - char *outUri, uint32_t bufLen); - -/** - * @brief creates pdu from the request information - * @param code [IN] request or response code - * @param options [OUT] options for the request and response - * @param info [IN] information to create pdu - * @param payload [IN] payload for the request or response consumed - * @return coap_pdu_t - */ -coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, - const CAInfo_t info, const char *payload); - -/** - * @brief parse the URI and creates the options - * @param uriInfo [IN] uri information - * @param options [OUT] options information - * @return None - */ -void CAParseURI(const char *uriInfo, coap_list_t **options); - -/** - * @brief create option list from header information in the info - * @param code [IN] uri information - * @param info [IN] options information - * @param optlist [OUT] options information - * @return None - */ -void CAParseHeadOption(uint32_t code, const CAInfo_t info, coap_list_t **optlist); - -/** - * @brief creates option node from key length and data - * need to replace queue head if new node has to be added before the existing queue head - * @param key [IN] key for the that needs to be sent - * @param length [IN] length of the data that needs to be sent - * @param data [IN] data that needs to be sent - * @return created list - */ -coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, - const uint8_t *data); - -/** - * @brief order the inserted options - * @param a [IN] option 1 for insertion - * @param b [IN] option 2 for insertion - * @return 0 or 1 - */ -int CAOrderOpts(void *a, void *b); - -/** - * @brief number of options count - * @param opt_iter [IN] option iteration for count - * @return number of options - */ -uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter); - -/** - * @brief gets option data - * @param data [IN] data that is received - * @param length [IN] length of the data - * @param option [OUT] result of the operation - * @param buflen [IN] buffer length of the result - * @return option count - */ -uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen); - -/** - * @brief extracts request information from received pdu. - * @param pdu [IN] received pdu - * @param outCode [OUT] code of the received pdu - * @param outInfo [OUT] request info structure made from received pdu - * @param outUri [OUT] uri received in the received pdu - * @param buflen [IN] Buffer Length for outUri parameter - * @return None - */ -void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, - char *outUri, uint32_t bufLen); - -/** - * @brief create pdu fromn received data - * @param data [IN] received data - * @param length [IN] length of the data received - * @param outCode [OUT] code received - * @return None - */ -coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode); - -/** - * @brief generates the token - * @param token [OUT] generated token - * @param tokenLength [IN] length of the token - * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h) - */ -CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength); - -/** - * @brief destroys the token - * @param token [IN] generated token - * @return none - */ -void CADestroyTokenInternal(CAToken_t token); - -/** - * @brief destroy the ca info structure - * @param info [IN] info structure created from received packet - * @return none - */ -void CADestroyInfo(CAInfo_t *info); - -/** - * @brief gets message type from PDU binary data - * @param pdu [IN] pdu data - * @param size [IN] size of pdu data - * @return message type - */ -CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size); - -/** - * @brief gets message ID PDU binary data - * @param pdu [IN] pdu data - * @param size [IN] size of pdu data - * @return message ID - */ -uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif //#ifndef __CA_PROTOCOL_MESSAGE_SINGLETHREAD_H_ - diff --git a/resource/csdk/connectivity/src/SConscript b/resource/csdk/connectivity/src/SConscript index b8a163b..4d57217 100644 --- a/resource/csdk/connectivity/src/SConscript +++ b/resource/csdk/connectivity/src/SConscript @@ -41,7 +41,7 @@ if ca_os == 'arduino': ca_path + 'cainterfacecontroller_singlethread.c', ca_path + 'camessagehandler_singlethread.c', ca_path + 'canetworkconfigurator_singlethread.c', - ca_path + 'caprotocolmessage_singlethread.c', + ca_path + 'caprotocolmessage.c', ca_path + 'caremotehandler.c', ca_path + 'caretransmission_singlethread.c', ] diff --git a/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c b/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c index ad92f51..caea74d 100644 --- a/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c +++ b/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c @@ -24,7 +24,7 @@ #include "cainterface.h" #include "caremotehandler.h" -#include "caprotocolmessage_singlethread.h" +#include "caprotocolmessage.h" #include "canetworkconfigurator.h" #include "logger.h" diff --git a/resource/csdk/connectivity/src/camessagehandler_singlethread.c b/resource/csdk/connectivity/src/camessagehandler_singlethread.c index eb12d91..8fe6eb7 100644 --- a/resource/csdk/connectivity/src/camessagehandler_singlethread.c +++ b/resource/csdk/connectivity/src/camessagehandler_singlethread.c @@ -28,7 +28,7 @@ #include "cainterface.h" #include "caremotehandler.h" #include "cainterfacecontroller_singlethread.h" -#include "caprotocolmessage_singlethread.h" +#include "caprotocolmessage.h" #include "logger.h" #include "config.h" /* for coap protocol */ #include "coap.h" diff --git a/resource/csdk/connectivity/src/caprotocolmessage.c b/resource/csdk/connectivity/src/caprotocolmessage.c index 5f2fed7..69da34b 100644 --- a/resource/csdk/connectivity/src/caprotocolmessage.c +++ b/resource/csdk/connectivity/src/caprotocolmessage.c @@ -30,21 +30,37 @@ #define _DEFAULT_SOURCE #define _BSD_SOURCE +// Include files from the arduino platform do not provide these conversions: +#ifdef ARDUINO +#define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) ) +#define ntohs(x) htons(x) +#else +#define HAVE_TIME_H 1 +#endif + #include #include #include +#ifdef HAVE_TIME_H #include +#endif #include "caprotocolmessage.h" #include "logger.h" #include "oic_malloc.h" +// ARM GCC compiler doesnt define srandom function. +#if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM) +#define HAVE_SRANDOM 1 +#endif + #define TAG "CA" #define CA_BUFSIZE 128 #define CA_PDU_MIN_SIZE 4 static const char COAP_HEADER[] = "coap://[::]/"; + static uint32_t SEED = 0; /** @@ -60,80 +76,80 @@ static uint32_t SEED = 0; static void CAParseUriPartial(const unsigned char *str, size_t length, int target, coap_list_t **optlist); -void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, char *outUri, - uint32_t buflen) +void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, + char *outUri, uint32_t buflen) { - OIC_LOG(DEBUG, TAG, "CAGetRequestInfoFromPdu IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (NULL == pdu) { + OIC_LOG(ERROR, TAG, "pdu NULL"); return; } uint32_t code = CA_NOT_FOUND; CAGetInfoFromPDU(pdu, &code, &(outReqInfo->info), outUri, buflen); outReqInfo->method = code; - OIC_LOG(DEBUG, TAG, "CAGetRequestInfoFromPdu OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); } -void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, char *outUri, - uint32_t buflen) +void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, + char *outUri, uint32_t buflen) { - OIC_LOG(DEBUG, TAG, "CAGetResponseInfoFromPdu IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (NULL == pdu) { + OIC_LOG(ERROR, TAG, "pdu NULL"); return; } uint32_t code = CA_NOT_FOUND; CAGetInfoFromPDU(pdu, &code, &(outResInfo->info), outUri, buflen); outResInfo->result = code; - OIC_LOG(DEBUG, TAG, "CAGetResponseInfoFromPdu OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); } -coap_pdu_t *CAGeneratePdu(const char *uri, const uint32_t code, const CAInfo_t info) +coap_pdu_t *CAGeneratePdu(const char *uri, uint32_t code, const CAInfo_t info) { - OIC_LOG(DEBUG, TAG, "CAGeneratePdu IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (NULL == uri) { + OIC_LOG(ERROR, TAG, "uri NULL"); return NULL; } uint32_t length = strlen(uri); if (CA_MAX_URI_LENGTH < length) { - OIC_LOG(ERROR, TAG, "check URI length.."); + OIC_LOG(ERROR, TAG, "URI len err"); return NULL; } uint32_t uriLength = length + sizeof(COAP_HEADER); - char *coapUri = NULL; - coapUri = (char *) OICCalloc(1, uriLength * sizeof(char)); + char *coapUri = (char *) OICCalloc(1, uriLength * sizeof(char)); if (NULL == coapUri) { - OIC_LOG(ERROR, TAG, "CAGeneratePdu, Memory allocation failed !"); + OIC_LOG(ERROR, TAG, "error"); return NULL; } - coap_list_t *optlist = NULL; - if (NULL != coapUri) - { - strcat(coapUri, COAP_HEADER); - strcat(coapUri, uri); + strcat(coapUri, COAP_HEADER); + strcat(coapUri, uri); - // parsing options in URI - CAParseURI(coapUri, &optlist); - OICFree(coapUri); - coapUri = NULL; + // parsing options in URI + coap_list_t *optlist = NULL; + CAParseURI(coapUri, &optlist); + OICFree(coapUri); + coapUri = NULL; - // parsing options in HeadOption - CAParseHeadOption(code, info, &optlist); - } + // parsing options in HeadOption + CAParseHeadOption(code, info, &optlist); - coap_pdu_t *pdu; - if (!(pdu = CAGeneratePduImpl((code_t) code, optlist, info, info.payload))) + coap_pdu_t *pdu = CAGeneratePduImpl((code_t) code, optlist, info, info.payload); + if (NULL == pdu) { + OIC_LOG(ERROR, TAG, "pdu NULL"); return NULL; } @@ -141,18 +157,18 @@ coap_pdu_t *CAGeneratePdu(const char *uri, const uint32_t code, const CAInfo_t i coap_delete_list(optlist); // pdu print method : coap_show_pdu(pdu); - OIC_LOG(DEBUG, TAG, "CAGeneratePdu OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return pdu; } coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode) { - OIC_LOG(DEBUG, TAG, "CAParsePDU IN"); + OIC_LOG(DEBUG, TAG, "IN"); coap_pdu_t *outpdu = coap_new_pdu(); if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu)) { - OIC_LOG(ERROR, TAG, "coap_pdu_parse failed"); + OIC_LOG(ERROR, TAG, "pdu parse failed"); coap_delete_pdu(outpdu); return NULL; } @@ -160,14 +176,15 @@ coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode) if (outCode) { (*outCode) = (uint32_t) outpdu->hdr->code; - }OIC_LOG(DEBUG, TAG, "CAParsePDU OUT"); + } + OIC_LOG(DEBUG, TAG, "OUT"); return outpdu; } -coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, const CAInfo_t info, - const char *payload) +coap_pdu_t *CAGeneratePduImpl(code_t code, coap_list_t *options, + const CAInfo_t info, const char *payload) { - OIC_LOG(DEBUG, TAG, "CAGeneratePduImpl IN"); + OIC_LOG(DEBUG, TAG, "IN"); coap_pdu_t *pdu = coap_new_pdu(); if (!pdu) @@ -176,7 +193,7 @@ coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, const CAI return NULL; } - OIC_LOG_V(DEBUG, TAG, "messageId is %d", info.messageId); + OIC_LOG_V(DEBUG, TAG, "msgId: %d", info.messageId); if (CA_MSG_ACKNOWLEDGE == info.type || CA_MSG_RESET == info.type) { pdu->hdr->id = htons(info.messageId); @@ -187,9 +204,8 @@ coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, const CAI if (info.messageId == 0) { /* initialize message id */ - prng((uint8_t * ) &message_id, sizeof(uint16_t)); - - OIC_LOG_V(DEBUG, TAG, "generate the message id(%d)", message_id); + prng((unsigned char *)&message_id, sizeof(message_id)); + OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id); } else { @@ -205,42 +221,43 @@ coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, const CAI { uint32_t tokenLength = info.tokenLength; OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); - OIC_LOG_BUFFER(DEBUG, TAG, info.token, tokenLength); + OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info.token, tokenLength); - int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *) info.token); + int32_t ret = coap_add_token(pdu, tokenLength, (const unsigned char *) info.token); if (0 == ret) { - OIC_LOG(DEBUG, TAG, "cannot add token to request"); + OIC_LOG(DEBUG, TAG, "can't add token"); } } if (options) { - coap_list_t *opt; - for (opt = options; opt; opt = opt->next) + for (coap_list_t *opt = options; opt; opt = opt->next) { OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.", - COAP_OPTION_DATA(*(coap_option *) opt->data)); - coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option * ) opt->data), - COAP_OPTION_LENGTH(*(coap_option * ) opt->data), - COAP_OPTION_DATA(*(coap_option * ) opt->data)); + COAP_OPTION_DATA(*(coap_option *) opt->data)); + coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data), + COAP_OPTION_LENGTH(*(coap_option *) opt->data), + COAP_OPTION_DATA(*(coap_option *) opt->data)); } } if (NULL != payload) { uint32_t len = strlen(payload); - OIC_LOG_V(DEBUG, TAG, "coap_add_data, payload: %s", payload); - coap_add_data(pdu, len, (const uint8_t *) payload); + OIC_LOG_V(DEBUG, TAG, "add data, payload:%s", payload); + coap_add_data(pdu, len, (const unsigned char *) payload); } - OIC_LOG(DEBUG, TAG, "CAGeneratePduImpl OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return pdu; } void CAParseURI(const char *uriInfo, coap_list_t **optlist) { - OIC_LOG(DEBUG, TAG, "CAParseURI IN");OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo); + OIC_LOG(DEBUG, TAG, "IN"); + + OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo); /* split arg into Uri-* options */ coap_uri_t uri; @@ -248,12 +265,11 @@ void CAParseURI(const char *uriInfo, coap_list_t **optlist) if (uri.port != COAP_DEFAULT_PORT) { - unsigned char portbuf[2]; - coap_insert( - optlist, - CACreateNewOptionNode(COAP_OPTION_URI_PORT, - coap_encode_var_bytes(portbuf, uri.port), portbuf), - CAOrderOpts); + unsigned char portbuf[2] = {0}; + coap_insert(optlist, + CACreateNewOptionNode(COAP_OPTION_URI_PORT, + coap_encode_var_bytes(portbuf, uri.port), portbuf), + CAOrderOpts); } @@ -261,7 +277,7 @@ void CAParseURI(const char *uriInfo, coap_list_t **optlist) CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY, optlist); - OIC_LOG(DEBUG, TAG, "CAParseURI OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); } void CAParseUriPartial(const unsigned char *str, size_t length, @@ -286,11 +302,10 @@ void CAParseUriPartial(const unsigned char *str, size_t length, size_t prevIdx = 0; while (res--) { - coap_insert( - optlist, - CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf), - COAP_OPT_VALUE(pBuf)), - CAOrderOpts); + coap_insert(optlist, + CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf), + COAP_OPT_VALUE(pBuf)), + CAOrderOpts); size_t optSize = COAP_OPT_SIZE(pBuf); if ((prevIdx + optSize) < buflen) @@ -307,42 +322,39 @@ void CAParseUriPartial(const unsigned char *str, size_t length, } } -void CAParseHeadOption(const uint32_t code, const CAInfo_t info, coap_list_t **optlist) +void CAParseHeadOption(uint32_t code, const CAInfo_t info, coap_list_t **optlist) { - OIC_LOG(DEBUG, TAG, "CAParseHeadOption IN"); + OIC_LOG(DEBUG, TAG, "IN"); - OIC_LOG_V(DEBUG, TAG, "start parse Head Option : %d", info.numOptions); + OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info.numOptions); - uint32_t i; - for (i = 0; i < info.numOptions; i++) + for (uint32_t i = 0; i < info.numOptions; i++) { uint32_t id = info.options[i].optionID; if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id) { - OIC_LOG_V(DEBUG, TAG, "it is not Header Option : %d", id); + OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id); } else { - OIC_LOG_V(DEBUG, TAG, "Head Option ID: %d", info.options[i].optionID); - - OIC_LOG_V(DEBUG, TAG, "Head Option data: %s", info.options[i].optionData); - - OIC_LOG_V(DEBUG, TAG, "Head Option length: %d", info.options[i].optionLength); - - coap_insert( - optlist, - CACreateNewOptionNode(info.options[i].optionID, info.options[i].optionLength, - info.options[i].optionData), - CAOrderOpts); + OIC_LOG_V(DEBUG, TAG, "Head Opt ID: %d", info.options[i].optionID); + OIC_LOG_V(DEBUG, TAG, "Head Opt data: %s", info.options[i].optionData); + OIC_LOG_V(DEBUG, TAG, "Head Opt len: %d", info.options[i].optionLength); + + coap_insert(optlist, + CACreateNewOptionNode(info.options[i].optionID, + info.options[i].optionLength, + info.options[i].optionData), CAOrderOpts); } } - OIC_LOG(DEBUG, TAG, "CAParseHeadOption OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); } -coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length, const uint8_t *data) +coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, + const uint8_t *data) { - OIC_LOG(DEBUG, TAG, "CACreateNewOptionNode IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (!data) { @@ -353,7 +365,7 @@ coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length, co coap_option *option = coap_malloc(sizeof(coap_option) + length + 1); if (!option) { - OIC_LOG(DEBUG, TAG, "Out of memory"); + OIC_LOG(ERROR, TAG, "Out of memory"); return NULL; } memset(option, 0, sizeof(coap_option) + length + 1); @@ -367,18 +379,18 @@ coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length, co if (!node) { - OIC_LOG(DEBUG, TAG, "coap_new_listnode returns NULL"); + OIC_LOG(DEBUG, TAG, "new_listnode rets NULL"); coap_free(option); return NULL; } //coap_free(option); - OIC_LOG(DEBUG, TAG, "CACreateNewOptionNode OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return node; } int CAOrderOpts(void *a, void *b) { - OIC_LOG(DEBUG, TAG, "CAOrderOpts IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (!a || !b) { return a < b ? -1 : 1; @@ -389,13 +401,13 @@ int CAOrderOpts(void *a, void *b) return -1; } - OIC_LOG(DEBUG, TAG, "CAOrderOpts OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return COAP_OPTION_KEY(*(coap_option *)a) == COAP_OPTION_KEY(*(coap_option * )b); } uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter) { - OIC_LOG(DEBUG, TAG, "CAGetOptionCount IN"); + OIC_LOG(DEBUG, TAG, "IN"); uint32_t count = 0; coap_opt_t *option; @@ -407,14 +419,14 @@ uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter) } } - OIC_LOG(DEBUG, TAG, "CAGetOptionCount OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return count; } -void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, char *outUri, - uint32_t buflen) +void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, + char *outUri, uint32_t buflen) { - OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (!pdu || !outCode || !outInfo || !outUri) { @@ -436,6 +448,7 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf memset(outInfo, 0, sizeof(*outInfo)); outInfo->numOptions = count; + // set type outInfo->type = pdu->hdr->type; @@ -447,14 +460,13 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t)); if (outInfo->options == NULL) { - OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU, Memory allocation failed !"); + OIC_LOG(ERROR, TAG, "Out of memory"); return; } } - char buf[COAP_MAX_PDU_SIZE] = { 0 }; coap_opt_t *option; - char optionResult[CA_MAX_URI_LENGTH] = { 0 }; + char optionResult[CA_MAX_URI_LENGTH] = {0}; uint32_t idx = 0; uint32_t optionLength = 0; bool isfirstsetflag = false; @@ -462,8 +474,9 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf while ((option = coap_option_next(&opt_iter))) { - if (CAGetOptionData((uint8_t *) (COAP_OPT_VALUE(option)), COAP_OPT_LENGTH(option), - (uint8_t *) buf, sizeof(buf))) + char buf[COAP_MAX_PDU_SIZE] = {0}; + if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)), + COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf))) { OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf); uint32_t bufLength = strlen(buf); @@ -580,12 +593,12 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf // set payload data if (NULL != pdu->data) { - uint32_t payloadLength = strlen((char*) pdu->data); + uint32_t payloadLength = strlen((const char *) pdu->data) + 1; OIC_LOG(DEBUG, TAG, "inside pdu->data"); outInfo->payload = (char *) OICMalloc(payloadLength + 1); if (outInfo->payload == NULL) { - OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU, Memory allocation failed !"); + OIC_LOG(ERROR, TAG, "Out of memory"); OICFree(outInfo->options); OICFree(outInfo->token); return; @@ -600,20 +613,23 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf { memcpy(outUri, optionResult, length); outUri[length] = '\0'; +#ifdef ARDUINO + OIC_LOG_V(DEBUG, TAG, "made URL:%s\n", optionResult); +#else OIC_LOG_V(DEBUG, TAG, "made URL : %s, %s\n", optionResult, outUri); +#endif } - OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return; exit: OIC_LOG(ERROR, TAG, "buffer too small"); OICFree(outInfo->options); - return; } CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) { - OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal IN"); + OIC_LOG(DEBUG, TAG, "IN"); if(!token) { @@ -629,14 +645,22 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) if (SEED == 0) { +#ifdef ARDUINO + SEED = now(); +#else SEED = time(NULL); +#endif if (SEED == -1) { OIC_LOG(DEBUG, TAG, "Failed to Create Seed!"); SEED = 0; return CA_STATUS_FAILED; } +#if HAVE_SRANDOM srandom(SEED); +#else + srand(SEED); +#endif } // memory allocation @@ -648,51 +672,54 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) } // set random byte - uint8_t index; - for (index = 0; index < tokenLength; index++) + for (uint8_t index = 0; index < tokenLength; index++) { // use valid characters +#ifdef ARDUINO + temp[index] = rand() & 0x00FF; +#else temp[index] = random() & 0x00FF; +#endif } // save token *token = temp; OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); - OIC_LOG_BUFFER(DEBUG, TAG, *token, tokenLength); + OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)token, tokenLength); - OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } void CADestroyTokenInternal(CAToken_t token) { - OIC_LOG(DEBUG, TAG, "CADestroyTokenInternal IN"); + OIC_LOG(DEBUG, TAG, "IN"); OICFree(token); - OIC_LOG(DEBUG, TAG, "CADestroyTokenInternal OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); } void CADestroyInfo(CAInfo_t *info) { - OIC_LOG(DEBUG, TAG, "CADestroyInfo IN"); + OIC_LOG(DEBUG, TAG, "IN"); if (NULL != info) { if (NULL != info->options) { - OIC_LOG(DEBUG, TAG, "free options in CAInfo"); + OIC_LOG(DEBUG, TAG, "free opt"); OICFree(info->options); } if (NULL != info->token) { - OIC_LOG(DEBUG, TAG, "free token in CAInfo"); + OIC_LOG(DEBUG, TAG, "free tok"); OICFree(info->token); } if (NULL != info->payload) { - OIC_LOG(DEBUG, TAG, "free payload in CAInfo"); + OIC_LOG(DEBUG, TAG, "free payld"); OICFree(info->payload); } } @@ -706,19 +733,13 @@ uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uin if (0 == buflen || 0 == len) { - OIC_LOG(ERROR, TAG, "buflen or len is not available"); - return 0; - } - - if (NULL == data) - { - OIC_LOG(ERROR, TAG, "data not available"); + OIC_LOG(ERROR, TAG, "len 0"); return 0; } - if (NULL == option) + if (NULL == data || NULL == option) { - OIC_LOG(ERROR, TAG, "option pointer is null"); + OIC_LOG(ERROR, TAG, "data/option NULL"); return 0; } @@ -739,6 +760,7 @@ CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size // pdu minimum size is 4 byte. if (size < CA_PDU_MIN_SIZE) { + OIC_LOG(DEBUG, TAG, "min size"); return CA_MSG_NONCONFIRM; } @@ -756,6 +778,7 @@ uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size) // pdu minimum size is 4 byte. if (size < CA_PDU_MIN_SIZE) { + OIC_LOG(DEBUG, TAG, "min size"); return 0; } diff --git a/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c b/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c deleted file mode 100644 index 4588eae..0000000 --- a/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c +++ /dev/null @@ -1,750 +0,0 @@ -/****************************************************************** - * - * Copyright 2014 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -#include -#include -#include - -#include "caprotocolmessage_singlethread.h" -#include "logger.h" -#include "oic_malloc.h" - -// ARM GCC compiler doesnt define srandom function. -#if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM) -#define HAVE_SRANDOM 1 -#endif - -#define TAG "CPM" - -#define CA_BUFSIZE 128 -#define CA_PDU_MIN_SIZE 4 -static const char COAP_HEADER[] = "coap://[::]/"; - -static uint32_t SEED = 0; - -void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo, - char *outUri, uint32_t buflen) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "pdu NULL"); - return; - } - - uint32_t code = CA_NOT_FOUND; - CAGetInfoFromPDU(pdu, &code, &(outReqInfo->info), outUri, buflen); - outReqInfo->method = code; - OIC_LOG(DEBUG, TAG, "OUT"); -} - -void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, - char *outUri, uint32_t buflen) -{ - OIC_LOG(DEBUG, TAG, "IN"); - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "pdu NULL"); - return; - } - - uint32_t code = CA_NOT_FOUND; - CAGetInfoFromPDU(pdu, &code, &(outResInfo->info), outUri, buflen); - outResInfo->result = code; - OIC_LOG(DEBUG, TAG, "OUT"); -} - -coap_pdu_t *CAGeneratePdu(const char *uri, uint32_t code, const CAInfo_t info) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL == uri) - { - OIC_LOG(ERROR, TAG, "uri NULL"); - return NULL; - } - - uint32_t length = strlen(uri); - if (CA_MAX_URI_LENGTH < length) - { - OIC_LOG(ERROR, TAG, "URI len err"); - return NULL; - } - - uint32_t uriLength = length + sizeof(COAP_HEADER); - char *coapUri = (char *) OICCalloc(uriLength, sizeof(char)); - if (NULL == coapUri) - { - OIC_LOG(ERROR, TAG, "CAGeneratePdu, Memory allocation failed !"); - return NULL; - } - - strcat(coapUri, COAP_HEADER); - strcat(coapUri, uri); - - // parsing options in URI - coap_list_t *optlist = NULL; - CAParseURI(coapUri, &optlist); - OICFree(coapUri); - - // parsing options in HeadOption - CAParseHeadOption(code, info, &optlist); - - coap_pdu_t *pdu = CAGeneratePduImpl((code_t) code, optlist, info, info.payload); - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "pdu NULL"); - return NULL; - } - - // free option list - coap_delete_list(optlist); - - // pdu print method : coap_show_pdu(pdu); - OIC_LOG(DEBUG, TAG, "OUT"); - return pdu; -} - -coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode) -{ - OIC_LOG(DEBUG, TAG, "IN"); - coap_pdu_t *outpdu = coap_new_pdu(); - - if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu)) - { - OIC_LOG(ERROR, TAG, "pdu parse failed"); - coap_delete_pdu(outpdu); - return NULL; - } - - if (outCode) - { - (*outCode) = (uint32_t) outpdu->hdr->code; - } - OIC_LOG(DEBUG, TAG, "OUT"); - return outpdu; -} - -coap_pdu_t *CAGeneratePduImpl(code_t code, coap_list_t *options, - const CAInfo_t info, const char *payload) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - coap_pdu_t *pdu = coap_new_pdu(); - - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "malloc failed"); - return NULL; - } - - OIC_LOG_V(DEBUG, TAG, "msgId: %d", info.messageId); - if(CA_MSG_ACKNOWLEDGE == info.type || CA_MSG_RESET == info.type) - { - pdu->hdr->id = htons(info.messageId); - } - else - { - uint16_t message_id; - if (info.messageId == 0) - { - /* initialize message id */ - prng((unsigned char * )&message_id, sizeof(unsigned short)); - OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id); - } - else - { - /* use saved message id */ - message_id = info.messageId; - } - pdu->hdr->id = htons(message_id); - } - pdu->hdr->type = info.type; - pdu->hdr->code = COAP_RESPONSE_CODE(code); - - if (info.token) - { - uint32_t tokenLength = info.tokenLength; - OIC_LOG_V(DEBUG, TAG, "tokenLength : %d, token : ", tokenLength); - OIC_LOG_BUFFER(DEBUG, TAG, info.token, tokenLength); - - int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *) info.token); - if (0 == ret) - { - OIC_LOG(DEBUG, TAG, "cant add token"); - } - } - - if(options) - { - coap_list_t *opt; - for (opt = options; opt; opt = opt->next) - { - OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.", - COAP_OPTION_DATA(*(coap_option * )opt->data)); - coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option * )opt->data), - COAP_OPTION_LENGTH(*(coap_option * )opt->data), - COAP_OPTION_DATA(*(coap_option * )opt->data)); - } - } - - if (NULL != payload) - { - uint32_t len = strlen(payload); - OIC_LOG_V(DEBUG, TAG, "add data,payload:%s", payload); - coap_add_data(pdu, len, (const unsigned char *) payload); - } - - OIC_LOG(DEBUG, TAG, "OUT"); - return pdu; -} - -void CAParseURI(const char *uriInfo, coap_list_t **optlist) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo); - - /* split arg into Uri-* options */ - coap_uri_t uri; - coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri); - - if (uri.port != COAP_DEFAULT_PORT) - { - unsigned char portbuf[2] = {0}; - coap_insert(optlist, - CACreateNewOptionNode(COAP_OPTION_URI_PORT, - coap_encode_var_bytes(portbuf, uri.port), portbuf), - CAOrderOpts); - } - - unsigned char uriBuffer[CA_BUFSIZE] = {0}; - unsigned char *tempBuf = uriBuffer; - if (uri.path.length) - { - uint32_t buflen = CA_BUFSIZE; - int32_t res = coap_split_path(uri.path.s, uri.path.length, tempBuf, &buflen); - - if (res > 0) - { - uint32_t prevIdx = 0; - while (res--) - { - coap_insert(optlist, - CACreateNewOptionNode(COAP_OPTION_URI_PATH, COAP_OPT_LENGTH(tempBuf), - COAP_OPT_VALUE(tempBuf)), CAOrderOpts); - uint32_t optSize = COAP_OPT_SIZE(tempBuf); - uint32_t nextIdx = prevIdx + optSize; - if (nextIdx < buflen) - { - tempBuf += optSize; - prevIdx += nextIdx; - } - } - } - } - - if (uri.query.length) - { - size_t buflen = CA_BUFSIZE; - tempBuf = uriBuffer; - int32_t res = coap_split_query(uri.query.s, uri.query.length, tempBuf, &buflen); - if (res > 0) - { - uint32_t prevIdx = 0; - while (res--) - { - coap_insert(optlist, - CACreateNewOptionNode(COAP_OPTION_URI_QUERY, COAP_OPT_LENGTH(tempBuf), - COAP_OPT_VALUE(tempBuf)), CAOrderOpts); - - uint32_t optSize = COAP_OPT_SIZE(tempBuf); - uint32_t nextIdx = prevIdx + optSize; - if (nextIdx < buflen) - { - tempBuf += optSize; - prevIdx += nextIdx; - } - } - } - } - - OIC_LOG(DEBUG, TAG, "OUT"); -} - -void CAParseHeadOption(uint32_t code, const CAInfo_t info, coap_list_t **optlist) -{ - OIC_LOG(DEBUG, TAG, "IN"); - OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info.numOptions); - - uint32_t i; - for (i = 0; i < info.numOptions; i++) - { - uint32_t id = info.options[i].optionID; - if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id) - { - OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id); - } - else - { - OIC_LOG_V(DEBUG, TAG, "Head Opt ID: %d", info.options[i].optionID); - OIC_LOG_V(DEBUG, TAG, "Head Opt data: %s", info.options[i].optionData); - OIC_LOG_V(DEBUG, TAG, "Head Opt len: %d", info.options[i].optionLength); - - coap_insert(optlist, - CACreateNewOptionNode(info.options[i].optionID, - info.options[i].optionLength, - info.options[i].optionData), CAOrderOpts); - } - } - - OIC_LOG(DEBUG, TAG, "OUT"); -} - -coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, - const uint8_t *data) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if (!data) - { - OIC_LOG(ERROR, TAG, "invalid pointer parameter"); - return NULL; - } - - coap_option *option = coap_malloc(sizeof(coap_option) + length + 1); - if (!option) - { - OIC_LOG(ERROR, TAG, "malloc failed"); - return NULL; - } - memset(option, 0, sizeof(coap_option) + length + 1); - - COAP_OPTION_KEY(*option) = key; - COAP_OPTION_LENGTH(*option) = length; - memcpy(COAP_OPTION_DATA(*option), data, length); - - /* we can pass NULL here as delete function since option is released automatically */ - coap_list_t *node = coap_new_listnode(option, NULL); - - if (!node) - { - OIC_LOG(DEBUG, TAG, "new_listnode rets NULL"); - coap_free(option); - return NULL; - } - - OIC_LOG(DEBUG, TAG, "OUT"); - return node; -} - -int CAOrderOpts(void *a, void *b) -{ - OIC_LOG(DEBUG, TAG, "IN"); - if (!a || !b) - { - return a < b ? -1 : 1; - } - - if (COAP_OPTION_KEY(*(coap_option *)a) < COAP_OPTION_KEY(*(coap_option * )b)) - { - return -1; - } - - OIC_LOG(DEBUG, TAG, "OUT"); - return COAP_OPTION_KEY(*(coap_option *)a) == COAP_OPTION_KEY(*(coap_option * )b); -} - -uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter) -{ - OIC_LOG(DEBUG, TAG, "IN"); - uint32_t count = 0; - coap_opt_t *option; - - while ((option = coap_option_next(&opt_iter))) - { - if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type) - { - count++; - } - } - - OIC_LOG(DEBUG, TAG, "OUT"); - return count; -} - -void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo, - char *outUri, uint32_t buflen) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if (!pdu || !outCode || !outInfo || !outUri) - { - OIC_LOG(ERROR, TAG, "NULL pointer param"); - return; - } - - coap_opt_iterator_t opt_iter; - coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL); - - // set code - if (outCode) - { - (*outCode) = (uint32_t) pdu->hdr->code; - } - - // init HeaderOption list - uint32_t count = CAGetOptionCount(opt_iter); - - memset(outInfo, 0, sizeof(*outInfo)); - outInfo->numOptions = count; - - // set type - outInfo->type = pdu->hdr->type; - - // set message id - outInfo->messageId = ntohs(pdu->hdr->id); - - if (count > 0) - { - outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t)); - if (outInfo->options == NULL) - { - OIC_LOG(ERROR, TAG, "malloc failed"); - return; - } - } - - coap_opt_t *option; - char optionResult[CA_MAX_URI_LENGTH] = {0,}; - uint32_t idx = 0; - uint32_t optionLength = 0; - bool isfirstsetflag = false; - bool isQueryBeingProcessed = false; - - while ((option = coap_option_next(&opt_iter))) - { - - char buf[COAP_MAX_PDU_SIZE] = {0}; /* need some space for output creation */ - if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)), - COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf))) - { - OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf); - uint32_t bufLength = strlen((const char *) buf); - if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type) - { - if (false == isfirstsetflag) - { - isfirstsetflag = true; - optionResult[optionLength] = '/'; - optionLength++; - // Make sure there is enough room in the optionResult buffer - if ((optionLength + bufLength) < sizeof(optionResult)) - { - memcpy(optionResult + optionLength, buf, bufLength); - optionLength += bufLength; - } - else - { - goto exit; - } - } - else - { - if (COAP_OPTION_URI_PATH == opt_iter.type) - { - // Make sure there is enough room in the optionResult buffer - if (optionLength < sizeof(optionResult)) - { - optionResult[optionLength] = '/'; - optionLength++; - } - else - { - goto exit; - } - } - else if (COAP_OPTION_URI_QUERY == opt_iter.type) - { - if(false == isQueryBeingProcessed) - { - // Make sure there is enough room in the optionResult buffer - if (optionLength < sizeof(optionResult)) - { - optionResult[optionLength] = '?'; - optionLength++; - isQueryBeingProcessed = true; - } - else - { - goto exit; - } - } - else - { - // Make sure there is enough room in the optionResult buffer - if (optionLength < sizeof(optionResult)) - { - optionResult[optionLength] = '&'; - optionLength++; - } - else - { - goto exit; - } - } - } - // Make sure there is enough room in the optionResult buffer - if ((optionLength + bufLength) < sizeof(optionResult)) - { - memcpy(optionResult + optionLength, buf, bufLength); - optionLength += bufLength; - } - else - { - goto exit; - } - } - } - else - { - if (idx < count) - { - uint32_t length = bufLength; - - if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH) - { - outInfo->options[idx].optionID = opt_iter.type; - outInfo->options[idx].optionLength = length; - outInfo->options[idx].protocolID = CA_COAP_ID; - memcpy(outInfo->options[idx].optionData, buf, length); - idx++; - } - } - } - } - } - - // set token data - if (pdu->hdr->token_length > 0) - { - OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length > 0"); - outInfo->token = (char *) OICMalloc(pdu->hdr->token_length); - if (NULL == outInfo->token) - { - OIC_LOG(ERROR, TAG, "memory allocation failed"); - OICFree(outInfo->options); - return; - } - memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length); - } - - outInfo->tokenLength = pdu->hdr->token_length; - - // set payload data - if (NULL != pdu->data) - { - uint32_t payloadLength = strlen((const char *) pdu->data) + 1; - OIC_LOG(DEBUG, TAG, "inside pdu->data"); - outInfo->payload = (char *) OICMalloc(payloadLength + 1); - if (outInfo->payload == NULL) - { - OIC_LOG(ERROR, TAG, "malloc failed"); - OICFree(outInfo->options); - OICFree(outInfo->token); - return; - } - memcpy(outInfo->payload, pdu->data, payloadLength); - outInfo->payload[payloadLength] = '\0'; - } - uint32_t length = strlen(optionResult); - OIC_LOG_V(DEBUG, TAG, "made URL length: %d, %d, %d...\n", length, buflen, strlen(outUri)); - if (buflen >= length) - { - memcpy(outUri, optionResult, length); - outUri[length] = '\0'; - OIC_LOG_V(DEBUG, TAG, "made URL:%s\n", optionResult); - } - - OIC_LOG(DEBUG, TAG, "OUT"); - return; - -exit: - OIC_LOG(ERROR, TAG, "buffer too small"); - OICFree(outInfo->options); - return; -} - -CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if(!token) - { - OIC_LOG(ERROR, TAG, "invalid token pointer"); - return CA_STATUS_INVALID_PARAM; - } - - if((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength)) - { - OIC_LOG(ERROR, TAG, "invalid token length"); - return CA_STATUS_INVALID_PARAM; - } - - if (SEED == 0) - { - SEED = now(); - if (SEED == -1) - { - OIC_LOG(DEBUG, TAG, "Failed to Create Seed!"); - SEED = 0; - return CA_STATUS_FAILED; - } -#if HAVE_SRANDOM - srandom(SEED); -#else - srand(SEED); -#endif - } - - // memory allocation - char *temp = (char *) OICCalloc(tokenLength, sizeof(char)); - if (NULL == temp) - { - OIC_LOG(ERROR, TAG, "Calloc failed"); - return CA_MEMORY_ALLOC_FAILED; - } - - // set random byte - for (uint8_t index = 0; index < tokenLength; index++) - { - temp[index] = rand() & 0x00FF; - } - - // save token - *token = temp; - - OIC_LOG_V(DEBUG, TAG, "gen token tokenLength : %d, token : ", tokenLength); - OIC_LOG_BUFFER(DEBUG, TAG, *token, tokenLength); - OIC_LOG(DEBUG, TAG, "OUT"); - return CA_STATUS_OK; -} - -void CADestroyTokenInternal(CAToken_t token) -{ - OIC_LOG(DEBUG, TAG, "IN"); - OICFree(token); - OIC_LOG(DEBUG, TAG, "OUT"); -} - -void CADestroyInfo(CAInfo_t *info) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL != info) - { - if (NULL != info->options) - { - OIC_LOG(DEBUG, TAG, "free opt"); - OICFree(info->options); - } - - if (NULL != info->token) - { - OIC_LOG(DEBUG, TAG, "free tok"); - OICFree(info->token); - } - - if (NULL != info->payload) - { - OIC_LOG(DEBUG, TAG, "free payld"); - OICFree(info->payload); - } - } - - OIC_LOG(DEBUG, TAG, "OUT"); -} - -uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen) -{ - assert(data || len == 0); - - if (0 == buflen|| 0 == len) - { - OIC_LOG(ERROR, TAG, "len 0"); - return 0; - } - - if (NULL == data || NULL == option) - { - OIC_LOG(ERROR, TAG, "data/option NULL"); - return 0; - } - - if (buflen <= len) - { - OIC_LOG(ERROR, TAG, "option buffer too small"); - return 0; - } - - memcpy(option, data, len); - option[len] = '\0'; - - return len; -} - -CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size) -{ - // pdu minimum size is 4 byte. - if (size < CA_PDU_MIN_SIZE) - { - OIC_LOG(DEBUG, TAG, "min size"); - return CA_MSG_NONCONFIRM; - } - - coap_hdr_t *hdr = (coap_hdr_t *) pdu; - - if(NULL == hdr) - { - return CA_MSG_NONCONFIRM; - } - - return (CAMessageType_t) hdr->type; -} - -uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size) -{ - // pdu minimum size is 4 byte. - if (size < CA_PDU_MIN_SIZE) - { - OIC_LOG(DEBUG, TAG, "min size"); - return 0; - } - - coap_hdr_t *hdr = (coap_hdr_t *) pdu; - - if(NULL == hdr) - { - return 0; - } - - return ntohs(hdr->id); -} diff --git a/resource/csdk/connectivity/src/caretransmission_singlethread.c b/resource/csdk/connectivity/src/caretransmission_singlethread.c index 24d8646..3fd26be 100644 --- a/resource/csdk/connectivity/src/caretransmission_singlethread.c +++ b/resource/csdk/connectivity/src/caretransmission_singlethread.c @@ -24,7 +24,7 @@ #include #include "caremotehandler.h" -#include "caprotocolmessage_singlethread.h" +#include "caprotocolmessage.h" #include "oic_malloc.h" #include "logger.h" -- 2.7.4