Commit 2c2a423e authored by bg.chun's avatar bg.chun Committed by Ashok Babu Channa

[IOT-1207] Data structure for OCF Endpoint Impl

provide basic data structure, function, types for OCF Endpoint
See detail in Jira IOT-1207

Change-Id: I4bdcca71ffdab101c729406a6509b6f1103b9ca8
Signed-off-by: default avatarbg.chun <bg.chun@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9897Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: default avatarJaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: default avatarAshok Babu Channa <ashok.channa@samsung.com>
parent ac2775fa
......@@ -176,7 +176,8 @@ liboctbstack_src = [
OCTBSTACK_SRC + 'ocobserve.c',
OCTBSTACK_SRC + 'ocserverrequest.c',
OCTBSTACK_SRC + 'occollection.c',
OCTBSTACK_SRC + 'oicgroup.c'
OCTBSTACK_SRC + 'oicgroup.c',
OCTBSTACK_SRC + 'ocendpoint.c'
]
if with_tcp == True:
......
/* ****************************************************************
*
* Copyright 2016 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 APIs for Endpoint.
*/
#ifndef OC_ENDPOINT_H
#define OC_ENDPOINT_H
#include <stdio.h>
#include <stdint.h>
#include "octypes.h"
#include "cacommon.h"
#define OC_MAX_TPS_STR_SIZE (12)
#define OC_MAX_ADDR_STR_SIZE (46)
#define OC_MAX_PORT_STR_SIZE (6)
#define OC_ENDPOINT_TPS_TOKEN "://"
#define OC_ENDPOINT_ADDR_TOKEN ':'
#define OC_ENDPOINT_BRACKET_START '['
#define OC_ENDPOINT_BRACKET_END ']'
#define OC_ENDPOINT_IPV4_NULL_TOKEN '\0'
#define OC_ENDPOINT_IPV4_SEGMENT_TOKEN '.'
#define OC_ENDPOINT_IPV4_MIN_VALUE '0'
#define OC_ENDPOINT_IPV4_MAX_VALUE '9'
#define DEFAULT_ENDPOINT_PRI (1)
#define COAP_STR "coap"
#define COAPS_STR "coaps"
#ifdef TCP_ADAPTER
#define COAP_TCP_STR "coap+tcp"
#define COAPS_TCP_STR "coaps+tcp"
#endif
#ifdef HTTP_ADAPTER
#define HTTP_STR "http"
#define HTTPS_STR "https"
#endif
#ifdef EDR_ADAPTER
#define COAP_RFCOMM_STR "coap+rfcomm"
#endif
/*
* This function gets Supported TPS Flags in Device from givenFlag.
*
* @param[in] givenFlags Given Flags of Transport Protocol Suites.
* @param[out] out Result Flags of Transport Protocol Suites.
*
* @return ::OC_STACK_OK on success, some other value upon failure.
*/
OCStackResult OCGetSupportedEndpointFlags(const OCTpsSchemeFlags givenFlags,
OCTpsSchemeFlags* out);
/*
* This function gets Matched TPS Flags from given flags.
*
* @param[in] adapter Given Adapter of CATransportAdapter_t.
* @param[in] flags Given Flags of CATransportFlags_t.
* @param[out] out Result Flags of Transport Protocol Suites.
*
* @return ::OC_STACK_OK on success, some other value upon failure.
*/
OCStackResult OCGetMatchedTpsFlags(const CATransportAdapter_t adapter,
const CATransportFlags_t flags, OCTpsSchemeFlags* out);
/*
* This function returns pointer of endpoint string.
*
* @param[in] endpoint Pointer of Endpoint Payload.
*
* @return char pointer on success, NULL on failure.
*/
char* OCCreateEndpointString(const OCEndpointPayload* endpoint);
/*
* This function parse endpoint string from payload.
*
* @param[in] endpointStr Pointer of Endpoint string.
* @param[out] out Pointer of OCEndpointPayload.
*
* @return ::OC_STACK_OK on success, some other value upon failure.
*/
OCStackResult OCParseEndpointString(const char* endpointStr, OCEndpointPayload* out);
/*
* This function convert tps flag to endpoint string.
*
* @param[in] tps Given tps flag.
* @param[out] out Pointer of char pointer for create string.
*
* @return ::OC_STACK_OK on success, some other value upon failure.
*/
OCStackResult OCConvertTpsToString(const OCTpsSchemeFlags tps, char** out);
/*
* This function returns flags of supported TPS on stack.
*
* @return Bit combinations of supported OCTpsSchemeFlags.
*/
OCTpsSchemeFlags OCGetSupportedTpsFlags();
#endif //OC_ENDPOINT_H
\ No newline at end of file
......@@ -251,6 +251,9 @@ typedef struct OCResource {
/** Use UUID for universal uniqueness - used in /oic/res to identify the device. */
OCIdentity uniqueUUID;
};
/** Resource endpoint type(s). */
OCTpsSchemeFlags endpointType;
} OCResource;
......
......@@ -224,6 +224,15 @@ extern "C" {
/** For Server instance ID.*/
#define OC_RSRVD_SERVER_INSTANCE_ID "sid"
/** To represent endpoints.*/
#define OC_RSRVD_ENDPOINTS "eps"
/** To represent endpoint.*/
#define OC_RSRVD_ENDPOINT "ep"
/** To represent priority.*/
#define OC_RSRVD_PRIORITY "pri"
/**
* Platform.
*/
......@@ -1206,6 +1215,42 @@ typedef struct
OCStringLL *dataModelVersions;
} OCDeviceInfo;
/**
* This enum type for indicate Transport Protocol Suites
*/
typedef enum
{
/** For initialize */
OC_NO_TPS = 0,
/** coap + udp */
OC_COAP = 1,
/** coaps + udp */
OC_COAPS = (1 << 1),
#ifdef TCP_ADAPTER
/** coap + tcp */
OC_COAP_TCP = (1 << 2),
/** coaps + tcp */
OC_COAPS_TCP = (1 << 3),
#endif
#ifdef HTTP_ADAPTER
/** http + tcp */
OC_HTTP = (1 << 4),
/** https + tcp */
OC_HTTPS = (1 << 5),
#endif
#ifdef EDR_ADAPTER
/** coap + rfcomm */
OC_COAP_RFCOMM = (1 << 6),
#endif
/** Allow all endpoint.*/
OC_ALL = 0xffff
} OCTpsSchemeFlags;
#ifdef RA_ADAPTER
/**
* callback for bound JID
......@@ -1335,6 +1380,17 @@ typedef struct OCRepPayload
struct OCRepPayload* next;
} OCRepPayload;
// used inside a resource payload
typedef struct OCEndpointPayload
{
char* tps;
char* addr;
OCTransportFlags family;
uint16_t port;
uint16_t pri;
struct OCEndpointPayload* next;
} OCEndpointPayload;
// used inside a discovery payload
typedef struct OCResourcePayload
{
......@@ -1348,6 +1404,7 @@ typedef struct OCResourcePayload
uint16_t tcpPort;
#endif
struct OCResourcePayload* next;
OCEndpointPayload* eps;
} OCResourcePayload;
typedef struct OCDiscoveryPayload
......
/* ****************************************************************
*
* Copyright 2016 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 "ocendpoint.h"
#include "logger.h"
#include "oic_malloc.h"
#include "oic_string.h"
#include <string.h>
#include "cainterface.h"
#define VERIFY_NON_NULL(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
#define VERIFY_GT_ZERO(arg) { if (arg < 1) {OIC_LOG(FATAL, TAG, #arg " < 1"); goto exit;} }
#define TAG "OIC_RI_ENDPOINT"
OCStackResult OCGetSupportedEndpointFlags(const OCTpsSchemeFlags givenFlags, OCTpsSchemeFlags* out)
{
if (!out)
{
return OC_STACK_INVALID_PARAM;
}
CATransportAdapter_t SelectedNetwork = CAGetSelectedNetwork();
if ((givenFlags & (OC_COAP | OC_COAPS)) && (SelectedNetwork & CA_ADAPTER_IP))
{
*out = (OCTpsSchemeFlags)(*out | OC_COAP);
if (OC_SECURE)
{
*out = (OCTpsSchemeFlags)(*out | OC_COAPS);
}
}
#ifdef TCP_ADAPTER
if ((givenFlags & (OC_COAP_TCP | OC_COAPS_TCP)) && (SelectedNetwork & CA_ADAPTER_TCP))
{
*out = (OCTpsSchemeFlags)(*out | OC_COAP_TCP);
if (OC_SECURE)
{
*out = (OCTpsSchemeFlags)(*out | OC_COAPS_TCP);
}
}
#endif
#ifdef EDR_ADAPTER
if ((givenFlags & OC_COAP_RFCOMM) && (SelectedNetwork & CA_ADAPTER_RFCOMM_BTEDR))
{
*out = (OCTpsSchemeFlags)(*out | OC_COAP_RFCOMM);
}
#endif
return OC_STACK_OK;
}
OCStackResult OCGetMatchedTpsFlags(const CATransportAdapter_t adapter,
const CATransportFlags_t flags,
OCTpsSchemeFlags* out)
{
// return matched TPS Flags with given CA flags.
if (!out)
{
OIC_LOG(ERROR, TAG, "out param is NULL!!!");
return OC_STACK_INVALID_PARAM;
}
if ((adapter & OC_ADAPTER_IP) && (flags & (OC_IP_USE_V4 | OC_IP_USE_V6)))
{
if (flags & OC_FLAG_SECURE)
{
// OC_COAPS
// typecasting to support C90(arduino)
*out = (OCTpsSchemeFlags)(*out | OC_COAPS);
}
else
{ // OC_COAP
// typecasting to support C90(arduino)
*out = (OCTpsSchemeFlags)(*out | OC_COAP);
}
}
#ifdef TCP_ADAPTER
if ((adapter & OC_ADAPTER_TCP) && (flags & (OC_IP_USE_V4 | OC_IP_USE_V6)))
{
if (flags & OC_FLAG_SECURE)
{
// OC_COAPS_TCP
// typecasting to support C90(arduino)
*out = (OCTpsSchemeFlags)(*out | OC_COAPS_TCP);
}
else
{
// OC_COAP_TCP
// typecasting to support C90(arduino)
*out = (OCTpsSchemeFlags)(*out | OC_COAP_TCP);
}
}
#endif
#ifdef HTTP_ADAPTER
// OC_HTTP
// @todo iotivity not support http transport yet...
// OC_HTTPS
// @todo iotivity not support https transport yet...
#endif
#ifdef EDR_ADAPTER
// OC_COAP_RFCOMM
if ((adapter & OC_ADAPTER_RFCOMM_BTEDR) && (flags == OC_DEFAULT_FLAGS))
{
// typecasting to support C90(arduino)
*out = (OCTpsSchemeFlags)(*out | OC_COAP_RFCOMM);
}
#endif
return OC_STACK_OK;
}
OCStackResult OCConvertTpsToString(const OCTpsSchemeFlags tps, char** out)
{
// return given tps as string for payload
// OC_COAP_IPV4 -> OC_COAP
if (!out)
{
return OC_STACK_INVALID_PARAM;
}
switch (tps)
{
case OC_COAP:
*out = OICStrdup(COAP_STR);
break;
case OC_COAPS:
*out = OICStrdup(COAPS_STR);
break;
#ifdef TCP_ADAPTER
case OC_COAP_TCP:
*out = OICStrdup(COAP_TCP_STR);
break;
case OC_COAPS_TCP:
*out = OICStrdup(COAPS_TCP_STR);
break;
#endif
#ifdef HTTP_ADAPTER
case OC_HTTP:
*out = OICStrdup(HTTP_STR);
break;
case OC_HTTPS:
*out = OICStrdup(HTTPS_STR);
break;
#endif
#ifdef EDR_ADAPTER
case OC_COAP_RFCOMM:
*out = OICStrdup(COAP_RFCOMM_STR);
break;
#endif
default:
return OC_STACK_INVALID_PARAM;
}
VERIFY_NON_NULL(*out);
return OC_STACK_OK;
exit:
return OC_STACK_NO_MEMORY;
}
char* OCCreateEndpointString(const OCEndpointPayload* endpoint)
{
if (!endpoint)
{
return NULL;
}
char* buf = (char*)OICCalloc(MAX_ADDR_STR_SIZE, sizeof(char));
VERIFY_NON_NULL(buf);
if ((strcmp(endpoint->tps, COAP_STR) == 0) || (strcmp(endpoint->tps, COAPS_STR) == 0)
#ifdef TCP_ADAPTER
|| (strcmp(endpoint->tps, COAP_TCP_STR) == 0) ||(strcmp(endpoint->tps, COAPS_TCP_STR) == 0)
#endif
#ifdef HTTP_ADAPTER
|| (strcmp(endpoint->tps, HTTP_STR) == 0) ||(strcmp(endpoint->tps, HTTPS_STR) == 0)
#endif
)
{
// checking addr is ipv4 or not
if (endpoint->family == OC_IP_USE_V4)
{
// ipv4
sprintf(buf, "%s://%s:%d", endpoint->tps, endpoint->addr, endpoint->port);
}
else
{
// ipv6
sprintf(buf, "%s://[%s]:%d", endpoint->tps, endpoint->addr, endpoint->port);
}
}
#ifdef EDR_ADAPTER
else if ((strcmp(endpoint->tps, COAP_RFCOMM_STR) == 0))
{
// coap+rfcomm
sprintf(buf, "%s://%s", endpoint->tps, endpoint->addr);
}
#endif
else
{
OIC_LOG_V(ERROR, TAG, "Payload has invalid TPS!!! %s", endpoint->tps);
return NULL;
}
return buf;
exit:
return NULL;
}
OCStackResult OCParseEndpointString(const char* endpointStr, OCEndpointPayload* out)
{
if (!endpointStr || !out)
{
return OC_STACK_INVALID_PARAM;
}
char* tmp = NULL;
char* ret = NULL;
char* tps = NULL;
char* addr = NULL;
char* origin = NULL;
char* tokPos = NULL;
OCStackResult isEnabledAdapter = OC_STACK_ADAPTER_NOT_ENABLED;
OCTransportAdapter parsedAdapter = OC_DEFAULT_ADAPTER;
tps = (char*)OICCalloc(OC_MAX_TPS_STR_SIZE, sizeof(char));
VERIFY_NON_NULL(tps);
addr = (char*)OICCalloc(OC_MAX_ADDR_STR_SIZE, sizeof(char));
VERIFY_NON_NULL(addr);
origin = OICStrdup(endpointStr);
VERIFY_NON_NULL(origin);
// token start pos
tokPos = strstr(origin, OC_ENDPOINT_TPS_TOKEN);
VERIFY_NON_NULL(tokPos);
// copy tps
VERIFY_GT_ZERO(tokPos - origin);
ret = strncpy(tps, origin, tokPos - origin);
VERIFY_NON_NULL(ret);
OIC_LOG_V(INFO, TAG, "parsed tps is:%s", tps);
// check tps type
if (strcmp(tps, COAP_STR) == 0)
{
isEnabledAdapter = OC_STACK_OK;
parsedAdapter = OC_ADAPTER_IP;
}
else if (strcmp(tps, COAPS_STR) == 0)
{
isEnabledAdapter = OC_STACK_OK;
parsedAdapter = OC_ADAPTER_IP;
}
#ifdef TCP_ADAPTER
else if (strcmp(tps, COAP_TCP_STR) == 0)
{
isEnabledAdapter = OC_STACK_OK;
parsedAdapter = OC_ADAPTER_TCP;
}
else if (strcmp(tps, COAPS_TCP_STR) == 0)
{
isEnabledAdapter = OC_STACK_OK;
parsedAdapter = OC_ADAPTER_TCP;
}
#endif
#ifdef HTTP_ADAPTER
// @todo iotivity not support http, https transport yet...
#endif
#ifdef EDR_ADAPTER
else if (strcmp(tps, COAP_RFCOMM_STR) == 0)
{
isEnabledAdapter = OC_STACK_OK;
parsedAdapter = OC_ADAPTER_RFCOMM_BTEDR;
}
#endif
// ignore unrecognized tps type
if (isEnabledAdapter == OC_STACK_ADAPTER_NOT_ENABLED
&& parsedAdapter == OC_DEFAULT_ADAPTER)
{
OICFree(tps);
OICFree(addr);
OICFree(origin);
return OC_STACK_ADAPTER_NOT_ENABLED;
}
if (parsedAdapter == OC_ADAPTER_RFCOMM_BTEDR)
{
// copy addr
tokPos = tokPos + 3;
ret = strcpy(addr, tokPos);
VERIFY_NON_NULL(ret);
out->tps = tps;
out->addr = addr;
out->family = OC_DEFAULT_FLAGS;
out->port = 0;
}
else
{
// first check epl has square bracket
tmp = strchr(origin, OC_ENDPOINT_BRACKET_START);
if (tmp)
{
out->family = OC_IP_USE_V6;
tokPos = tokPos + 4;
tmp = strrchr(origin, OC_ENDPOINT_BRACKET_END);
VERIFY_NON_NULL(tmp);
}
else
{
out->family = OC_IP_USE_V4;
tokPos = tokPos + 3;
tmp = strrchr(origin, OC_ENDPOINT_ADDR_TOKEN);
VERIFY_NON_NULL(tmp);
}
// copy addr
VERIFY_GT_ZERO(tmp - tokPos);
ret = strncpy(addr, tokPos, tmp - tokPos);
VERIFY_NON_NULL(ret);
OIC_LOG_V(INFO, TAG, "parsed addr is:%s", addr);
tmp = strrchr(origin, OC_ENDPOINT_ADDR_TOKEN);
VERIFY_NON_NULL(tmp);
// port start pos
tokPos = tmp + 1;
VERIFY_GT_ZERO(atoi(tokPos));
OIC_LOG_V(INFO, TAG, "parsed port is:%s", tokPos);
out->tps = tps;
out->addr = addr;
out->port = atoi(tokPos);
}
OICFree(origin);
origin = NULL;
tokPos = NULL;
tmp = NULL;
ret = NULL;
return OC_STACK_OK;
exit:
OICFree(origin);
OICFree(tps);
OICFree(addr);
origin = NULL;
tokPos = NULL;
tmp = NULL;
ret = NULL;
tps = NULL;
addr = NULL;
return OC_STACK_ERROR;
}
OCTpsSchemeFlags OCGetSupportedTpsFlags()
{
OCTpsSchemeFlags ret = (OCTpsSchemeFlags)0;
CATransportAdapter_t SelectedNetwork = CAGetSelectedNetwork();
if (SelectedNetwork & CA_ADAPTER_IP)
{
ret = (OCTpsSchemeFlags)(ret | OC_COAP);
if (OC_SECURE)
{
ret = (OCTpsSchemeFlags)(ret | OC_COAPS);
}
}
#ifdef TCP_ADAPTER
else if (SelectedNetwork & CA_ADAPTER_TCP)
{
ret = (OCTpsSchemeFlags)(ret | OC_COAP_TCP);
if (OC_SECURE)
{
ret = (OCTpsSchemeFlags)(ret | OC_COAPS_TCP);
}
}
#endif
#ifdef EDR_ADAPTER
else if (SelectedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
{
ret = (OCTpsSchemeFlags)(ret | OC_COAP_RFCOMM);
}
#endif
return ret;
}
\ No newline at end of file