#include "oic_malloc.h"
#include "oic_string.h"
#include "ocrandom.h"
+#include "cacommonutil.h"
-#define TAG "CA_PRTCL_MSG"
+#define TAG "OIC_CA_PRTCL_MSG"
-/**
- * @def VERIFY_NON_NULL_RET
- * @brief Macro to verify the validity of input argument
- */
-#define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
- if (NULL == arg ){ \
- OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
- return ret; \
- }
-
-#define CA_BUFSIZE (128)
#define CA_PDU_MIN_SIZE (4)
#define CA_PORT_BUFFER_SIZE (4)
CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
CARequestInfo_t *outReqInfo)
{
- if (NULL == pdu || NULL == outReqInfo)
- {
- OIC_LOG(ERROR, TAG, "parameter is null");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(pdu, TAG, "pdu");
+ VERIFY_NON_NULL(outReqInfo, TAG, "outReqInfo");
uint32_t code = CA_NOT_FOUND;
CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
const CAEndpoint_t *endpoint)
{
- if (NULL == pdu || NULL == outResInfo)
- {
- OIC_LOG(ERROR, TAG, "parameter is null");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(pdu, TAG, "pdu");
+ VERIFY_NON_NULL(outResInfo, TAG, "outResInfo");
uint32_t code = CA_NOT_FOUND;
CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
CAErrorInfo_t *errorInfo)
{
- if (!pdu)
- {
- OIC_LOG(ERROR, TAG, "parameter is null");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(pdu, TAG, "pdu");
uint32_t code = 0;
CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
// and ACKNOWLEDGE can use empty message when code is empty.
if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
{
- if ((CA_EMPTY == code) && (info->payloadSize > 0 || info->payload
- || info->token || info->tokenLength > 0))
+ if (CA_EMPTY != code)
+ {
+ OIC_LOG(ERROR, TAG, "reset is not empty message");
+ return NULL;
+ }
+
+ if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0)
{
OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID");
return NULL;
coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
const CAEndpoint_t *endpoint)
{
- if (NULL == data)
- {
- OIC_LOG(ERROR, TAG, "data is null");
- return NULL;
- }
+ VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
+ VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
coap_transport_type transport;
-#ifdef TCP_ADAPTER
- if (CA_ADAPTER_TCP == endpoint->adapter)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
}
return NULL;
}
-#ifdef TCP_ADAPTER
- if (CA_ADAPTER_TCP == endpoint->adapter)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
OIC_LOG(INFO, TAG, "there is no version info in coap header");
}
VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
unsigned int length = COAP_MAX_PDU_SIZE;
-#ifdef TCP_ADAPTER
+#ifdef WITH_TCP
unsigned int msgLength = 0;
- if (CA_ADAPTER_TCP == endpoint->adapter)
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
if (options)
{
}
msgLength += optLength;
prevOptNumber = curOptNumber;
- OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%d], "
- "optLength[%d], msgLength[%d]",
+ OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
+ "optLength[%zu], msgLength[%d]",
curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
}
}
return NULL;
}
- OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
+ OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
*transport, info->payloadSize);
-#ifdef TCP_ADAPTER
- if (CA_ADAPTER_TCP == endpoint->adapter)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
coap_add_length(pdu, *transport, msgLength);
}
#ifdef WITH_BWT
if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
-#ifdef TCP_ADAPTER
- && CA_ADAPTER_TCP != endpoint->adapter
+#ifdef WITH_TCP
+ && !CAIsSupportedCoAPOverTCP(endpoint->adapter)
#endif
)
{
CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
{
- if (NULL == uriInfo)
- {
- OIC_LOG(ERROR, TAG, "uriInfo is null");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(uriInfo, TAG, "uriInfo");
+ VERIFY_NON_NULL(optlist, TAG, "optlist");
OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
- if (NULL == optlist)
- {
- OIC_LOG(ERROR, TAG, "optlist is null");
- return CA_STATUS_INVALID_PARAM;
- }
-
/* split arg into Uri-* options */
coap_uri_t uri;
coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
coap_list_t **optlist)
{
- if (!optlist)
- {
- OIC_LOG(ERROR, TAG, "optlist is null");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(optlist, TAG, "optlist");
if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
{
}
else if (str && length)
{
- unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
+ unsigned char uriBuffer[CA_MAX_URI_LENGTH] = { 0 };
unsigned char *pBuf = uriBuffer;
size_t buflen = sizeof(uriBuffer);
int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
{
(void)code;
- VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
+ VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM);
OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
{
coap_list_t* node = NULL;
uint8_t buf[3] = {0};
- switch (info->payloadFormat) {
+ switch (info->payloadFormat)
+ {
case CA_FORMAT_APPLICATION_CBOR:
node = CACreateNewOptionNode(
COAP_OPTION_CONTENT_FORMAT,
{
coap_list_t* node = NULL;
uint8_t buf[3] = {0};
- switch (info->acceptFormat) {
+ switch (info->acceptFormat)
+ {
case CA_FORMAT_APPLICATION_CBOR:
node = CACreateNewOptionNode(
COAP_OPTION_ACCEPT,
coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
{
- if (!data)
- {
- OIC_LOG(ERROR, TAG, "invalid pointer parameter");
- return NULL;
- }
+ VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
if (!option)
&& COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
&& COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
&& COAP_OPTION_CONTENT_FORMAT != opt_iter.type
- && COAP_OPTION_ACCEPT != opt_iter.type)
+ && COAP_OPTION_ACCEPT != opt_iter.type
+ && COAP_OPTION_URI_HOST != opt_iter.type && COAP_OPTION_URI_PORT != opt_iter.type
+ && COAP_OPTION_ETAG != opt_iter.type && COAP_OPTION_MAXAGE != opt_iter.type
+ && COAP_OPTION_PROXY_URI != opt_iter.type && COAP_OPTION_PROXY_SCHEME != opt_iter.type)
{
count++;
}
CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
uint32_t *outCode, CAInfo_t *outInfo)
{
- if (!pdu || !outCode || !outInfo)
- {
- OIC_LOG(ERROR, TAG, "NULL pointer param");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(pdu, TAG, "pdu");
+ VERIFY_NON_NULL(endpoint, TAG, "endpoint");
+ VERIFY_NON_NULL(outCode, TAG, "outCode");
+ VERIFY_NON_NULL(outInfo, TAG, "outInfo");
coap_transport_type transport;
-#ifdef TCP_ADAPTER
- if (CA_ADAPTER_TCP == endpoint->adapter)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
}
outInfo->numOptions = count;
-#ifdef TCP_ADAPTER
- if (CA_ADAPTER_TCP == endpoint->adapter)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
// set type
outInfo->type = CA_MSG_NONCONFIRM;
outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
}
else
+#else
+ (void) endpoint;
#endif
{
// set type
outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
}
OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
- opt_iter.type, (uint8_t)buf[0]);
+ opt_iter.type, (uint8_t)buf[0]);
+ }
+ else if (COAP_OPTION_URI_PORT == opt_iter.type ||
+ COAP_OPTION_URI_HOST == opt_iter.type ||
+ COAP_OPTION_ETAG == opt_iter.type ||
+ COAP_OPTION_MAXAGE == opt_iter.type ||
+ COAP_OPTION_PROXY_URI == opt_iter.type ||
+ COAP_OPTION_PROXY_SCHEME== opt_iter.type)
+ {
+ OIC_LOG_V(INFO, TAG, "option[%d] has an unsupported format [%d]",
+ opt_iter.type, (uint8_t)buf[0]);
}
else
{
if (optionResult[0] != '\0')
{
- OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
+ OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
outInfo->resourceUri = OICStrdup(optionResult);
if (!outInfo->resourceUri)
{
CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
const CAEndpoint_t *endpoint)
{
- if (NULL == pdu_hdr)
- {
- OIC_LOG(ERROR, TAG, "pdu_hdr is null");
- return CA_STATUS_INVALID_PARAM;
- }
-
- if (NULL == outInfo)
- {
- OIC_LOG(ERROR, TAG, "outInfo is null");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr");
+ VERIFY_NON_NULL(outInfo, TAG, "outInfo");
+ VERIFY_NON_NULL(endpoint, TAG, "endpoint");
coap_transport_type transport;
-#ifdef TCP_ADAPTER
- if (CA_ADAPTER_TCP == endpoint->adapter)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
{
transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
}
CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
{
- if (!token)
- {
- OIC_LOG(ERROR, TAG, "invalid token pointer");
- return CA_STATUS_INVALID_PARAM;
- }
+ VERIFY_NON_NULL(token, TAG, "token");
if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
{
OICFree(token);
}
-void CADestroyInfo(CAInfo_t *info)
-{
- if (NULL != info)
- {
- OIC_LOG(DEBUG, TAG, "free options");
- OICFree(info->options);
-
- OIC_LOG(DEBUG, TAG, "free token");
- OICFree(info->token);
-
- OIC_LOG(DEBUG, TAG, "free payload");
- OICFree(info->payload);
- }
-}
-
uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
uint8_t *option, uint32_t buflen)
{
}
coap_option_def_t* def = coap_opt_def(key);
- if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
+ if (NULL != def && coap_is_var_bytes(def) && 0 == len)
+ {
// A 0 length option is permitted in CoAP but the
// rest or the stack is unaware of variable byte encoding
// should remain that way so a 0 byte of length 1 is inserted.
CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
{
- if (NULL == pdu)
- {
- OIC_LOG(ERROR, TAG, "pdu is null");
- return CA_MSG_NONCONFIRM;
- }
+ VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM);
// pdu minimum size is 4 byte.
if (size < CA_PDU_MIN_SIZE)
uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
{
- if (NULL == pdu)
- {
- OIC_LOG(ERROR, TAG, "pdu is null");
- return 0;
- }
+ VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0);
// pdu minimum size is 4 byte.
if (size < CA_PDU_MIN_SIZE)
CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
{
- if (NULL == pdu)
- {
- OIC_LOG(ERROR, TAG, "pdu is null");
- return CA_NOT_FOUND;
- }
+ VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND);
// pdu minimum size is 4 byte.
if (size < CA_PDU_MIN_SIZE)
return CA_FORMAT_UNSUPPORTED;
}
}
+
+#ifdef WITH_BWT
+bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter)
+{
+ if (CA_ADAPTER_IP & adapter || CA_ADAPTER_NFC & adapter
+ || CA_DEFAULT_ADAPTER == adapter)
+ {
+ return true;
+ }
+ return false;
+}
+#endif
+
+#ifdef WITH_TCP
+bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter)
+{
+ if (CA_ADAPTER_GATT_BTLE & adapter || CA_ADAPTER_RFCOMM_BTEDR & adapter
+ || CA_ADAPTER_TCP & adapter || CA_DEFAULT_ADAPTER == adapter)
+ {
+ return true;
+ }
+ return false;
+}
+#endif