#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
+#include "iotivity_config.h"
#include <inttypes.h>
#include <string.h>
#include <ctype.h>
#include "cainterface.h"
#include "ocpayload.h"
#include "ocpayloadcbor.h"
-#include "platform_features.h"
+#include "cautilinterface.h"
+#include "oicgroup.h"
#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
#include "routingutility.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#include "coap_time.h"
-#include "utlist.h"
-#include "pdu.h"
+#include <coap/coap.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
OCDeviceEntityHandler defaultDeviceHandler;
void* defaultDeviceHandlerCallbackParameter = NULL;
-static const char COAP_TCP[] = "coap+tcp:";
-static const char COAPS_TCP[] = "coaps+tcp:";
+static const char COAP_TCP_SCHEME[] = "coap+tcp:";
+static const char COAPS_TCP_SCHEME[] = "coaps+tcp:";
static const char CORESPEC[] = "core";
+CAAdapterStateChangedCB g_adapterHandler = NULL;
+CAConnectionStateChangedCB g_connectionHandler = NULL;
+
//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------
static OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode);
/**
- * Convert OCStackResult to CAResponseResult_t.
- *
- * @param caCode OCStackResult code.
- * @param method OCMethod method the return code replies to.
- * @return ::CA_CONTENT on OK, some other value upon failure.
- */
-static CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
-
-/**
* Convert OCTransportFlags_t to CATransportModifiers_t.
*
* @param ocConType OCTransportFlags_t input.
*/
static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo);
+/**
+ * default adapter state change callback method
+ *
+ * @param adapter CA network adapter type.
+ * @param enabled current adapter state.
+ */
+static void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled);
+
+/**
+ * default connection state change callback method
+ *
+ * @param info CAEndpoint which has address, port and etc.
+ * @param isConnected current connection state.
+ */
+static void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected);
+
+/**
+ * Register network monitoring callback.
+ * Network status changes are delivered these callback.
+ * @param adapterHandler Adapter state monitoring callback.
+ * @param connectionHandler Connection state monitoring callback.
+ */
+static void OCSetNetworkMonitorHandler(CAAdapterStateChangedCB adapterHandler,
+ CAConnectionStateChangedCB connectionHandler);
+
//-----------------------------------------------------------------------------
// Internal functions
//-----------------------------------------------------------------------------
+bool checkProxyUri(OCHeaderOption *options, uint8_t numOptions)
+{
+ if (!options || 0 == numOptions)
+ {
+ OIC_LOG (INFO, TAG, "No options present");
+ return false;
+ }
+
+ for (uint8_t i = 0; i < numOptions; i++)
+ {
+ if (options[i].protocolID == OC_COAP_ID && options[i].optionID == OC_RSRVD_PROXY_OPTION_ID)
+ {
+ OIC_LOG(DEBUG, TAG, "Proxy URI is present");
+ return true;
+ }
+ }
+ return false;
+}
+
uint32_t GetTicks(uint32_t afterMilliSeconds)
{
coap_tick_t now;
*
* requestUri must be a char array of size CA_MAX_URI_LENGTH
*/
-static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint, char *resourceUri,
+static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint,
char *presenceUri, bool isMulticast)
{
VERIFY_NON_NULL(endpoint , FATAL, OC_STACK_INVALID_PARAM);
- VERIFY_NON_NULL(resourceUri, FATAL, OC_STACK_INVALID_PARAM);
VERIFY_NON_NULL(presenceUri, FATAL, OC_STACK_INVALID_PARAM);
if (isMulticast)
}
// check for unicast presence
- uriLen = FormCanonicalPresenceUri(endpoint, OC_RSRVD_PRESENCE_URI, presenceUri,
+ uriLen = FormCanonicalPresenceUri(endpoint, presenceUri,
responseInfo->isMulticast);
if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
{
ResetPresenceTTL(cbNode, maxAge);
cbNode->sequenceNumber = response.sequenceNumber;
-
- // Ensure that a filter is actually applied.
- if( resourceTypeName && cbNode->filterResourceType)
- {
- if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
- {
- goto exit;
- }
- }
}
}
else
{
// This is the multicast case
- OCMulticastNode* mcNode = NULL;
- mcNode = GetMCPresenceNode(presenceUri);
-
- if(mcNode != NULL)
+ OIC_LOG(INFO, TAG, "this is the multicast presence");
+ if (0 == maxAge)
{
- if(mcNode->nonce == response.sequenceNumber)
- {
- OIC_LOG(INFO, TAG, "No presence change (Multicast)");
- goto exit;
- }
- mcNode->nonce = response.sequenceNumber;
-
- if(maxAge == 0)
- {
- OIC_LOG(INFO, TAG, "Stopping presence");
- response.result = OC_STACK_PRESENCE_STOPPED;
- }
- }
- else
- {
- char* uri = OICStrdup(presenceUri);
- if (!uri)
- {
- OIC_LOG(INFO, TAG,
- "No Memory for URI to store in the presence node");
- result = OC_STACK_NO_MEMORY;
- goto exit;
- }
-
- result = AddMCPresenceNode(&mcNode, uri, response.sequenceNumber);
- if(result == OC_STACK_NO_MEMORY)
- {
- OIC_LOG(INFO, TAG,
- "No Memory for Multicast Presence Node");
- OICFree(uri);
- goto exit;
- }
- // presence node now owns uri
+ OIC_LOG(INFO, TAG, "Stopping presence");
+ response.result = OC_STACK_PRESENCE_STOPPED;
}
+ }
-
-
- // Ensure that a filter is actually applied.
- if(resourceTypeName && cbNode->filterResourceType)
+ // Ensure that a filter is actually applied.
+ if (resourceTypeName && cbNode->filterResourceType)
+ {
+ OIC_LOG_V(INFO, TAG, "find resource type : %s", resourceTypeName);
+ if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
{
- OIC_LOG_V(INFO, TAG, "find resource type : %s", resourceTypeName);
- if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
- {
- goto exit;
- }
+ goto exit;
}
}
+
OIC_LOG(INFO, TAG, "Callback for presence");
cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
#endif
else if (strcmp(cbNode->requestUri, OC_RSRVD_RD_URI) == 0)
{
- type = PAYLOAD_TYPE_RD;
+ type = PAYLOAD_TYPE_REPRESENTATION ;
}
#ifdef TCP_ADAPTER
- else if (strcmp(cbNode->requestUri, KEEPALIVE_RESOURCE_URI) == 0)
+ else if (strcmp(cbNode->requestUri, OC_KEEPALIVE_RESOURCE_URI) == 0)
{
type = PAYLOAD_TYPE_REPRESENTATION;
}
cbNode->method == OC_REST_OBSERVE_ALL ||
cbNode->method == OC_REST_DELETE)
{
- char targetUri[MAX_URI_LENGTH];
- snprintf(targetUri, MAX_URI_LENGTH, "%s?rt=%s", OC_RSRVD_RD_URI,
- OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
- if (strcmp(targetUri, cbNode->requestUri) == 0)
- {
- type = PAYLOAD_TYPE_RD;
- }
- else if (strcmp(OC_RSRVD_PLATFORM_URI, cbNode->requestUri) == 0)
+ if (cbNode->requestUri)
{
- type = PAYLOAD_TYPE_PLATFORM;
+ if (strcmp(OC_RSRVD_PLATFORM_URI, cbNode->requestUri) == 0)
+ {
+ type = PAYLOAD_TYPE_PLATFORM;
+ }
+ else if (strcmp(OC_RSRVD_DEVICE_URI, cbNode->requestUri) == 0)
+ {
+ type = PAYLOAD_TYPE_DEVICE;
+ }
+ if (type == PAYLOAD_TYPE_INVALID)
+ {
+ OIC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
+ cbNode->method, cbNode->requestUri);
+ type = PAYLOAD_TYPE_REPRESENTATION;
+ }
}
- else if (strcmp(OC_RSRVD_DEVICE_URI, cbNode->requestUri) == 0)
- {
- type = PAYLOAD_TYPE_DEVICE;
- }
- if (type == PAYLOAD_TYPE_INVALID)
+ else
{
- OIC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
- cbNode->method, cbNode->requestUri);
+ OIC_LOG(INFO, TAG, "No Request URI, PROXY URI");
type = PAYLOAD_TYPE_REPRESENTATION;
}
}
{
OIC_LOG(DEBUG, TAG, "Enter OCHandleRequests");
-#ifdef TCP_ADAPTER
- if (requestInfo->info.resourceUri &&
- strcmp(requestInfo->info.resourceUri, KEEPALIVE_RESOURCE_URI) == 0)
- {
- HandleKeepAliveRequest(endPoint, requestInfo);
- return;
- }
-#endif
-
OCStackResult requestResult = OC_STACK_ERROR;
if(myStackMode == OC_CLIENT)
CA_RESPONSE_DATA);
}
}
+ if (requestResult == OC_STACK_RESOURCE_ERROR
+ && serverRequest.observationOption == OC_OBSERVE_REGISTER)
+ {
+ OIC_LOG_V(ERROR, TAG, "Observe Registration failed due to resource error");
+ }
else if(!OCResultToSuccess(requestResult))
{
OIC_LOG_V(ERROR, TAG, "HandleStackRequests failed. error: %d", requestResult);
defaultDeviceHandler = NULL;
defaultDeviceHandlerCallbackParameter = NULL;
+ result = InitializeScheduleResourceList();
+ VERIFY_SUCCESS(result, OC_STACK_OK);
+
result = CAResultToOCResult(CAInitialize());
VERIFY_SUCCESS(result, OC_STACK_OK);
result = CAResultToOCResult(OCSelectNetwork());
VERIFY_SUCCESS(result, OC_STACK_OK);
+ result = CAResultToOCResult(CARegisterNetworkMonitorHandler(
+ OCDefaultAdapterStateChangedHandler, OCDefaultConnectionStateChangedHandler));
+ VERIFY_SUCCESS(result, OC_STACK_OK);
+
switch (myStackMode)
{
case OC_CLIENT:
OIC_LOG(ERROR, TAG, "Stack initialization error");
deleteAllResources();
CATerminate();
+ TerminateScheduleResourceList();
stackState = OC_STACK_UNINITIALIZED;
}
return result;
DeleteDeviceInfo();
DeletePlatformInfo();
CATerminate();
+ TerminateScheduleResourceList();
// Remove all observers
DeleteObserverList();
// Remove all the client callbacks
bool istcp = false;
if (prefixLen)
{
- if (((prefixLen == sizeof(COAP_TCP) - 1) && (!strncmp(fullUri, COAP_TCP, prefixLen)))
- || ((prefixLen == sizeof(COAPS_TCP) - 1) && (!strncmp(fullUri, COAPS_TCP, prefixLen))))
+ if (((prefixLen == sizeof(COAP_TCP_SCHEME) - 1) && (!strncmp(fullUri, COAP_TCP_SCHEME, prefixLen)))
+ || ((prefixLen == sizeof(COAPS_TCP_SCHEME) - 1) && (!strncmp(fullUri, COAPS_TCP_SCHEME, prefixLen))))
{
istcp = true;
}
}
static OCStackResult OCPreparePresence(CAEndpoint_t *endpoint,
- char *resourceUri,
char **requestUri,
bool isMulticast)
{
char uri[CA_MAX_URI_LENGTH];
- FormCanonicalPresenceUri(endpoint, resourceUri, uri, isMulticast);
+ FormCanonicalPresenceUri(endpoint, uri, isMulticast);
*requestUri = OICStrdup(uri);
if (!*requestUri)
// Validate input parameters
VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
- VERIFY_NON_NULL(requestUri , FATAL, OC_STACK_INVALID_URI);
OCStackResult result = OC_STACK_ERROR;
CAResult_t caResult;
adapter = (OCTransportAdapter)(connectivityType >> CT_ADAPTER_SHIFT);
flags = (OCTransportFlags)(connectivityType & CT_MASK_FLAGS);
- result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
- if (result != OC_STACK_OK)
+ if (requestUri)
{
- OIC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
+ result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
+ if (result != OC_STACK_OK)
+ {
+ OIC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
+ goto exit;
+ }
+ }
+ else if (!checkProxyUri(options, numOptions))
+ {
+ OIC_LOG(ERROR, TAG, "Request doesn't contain RequestURI/Proxy URI");
goto exit;
}
case OC_REST_GET:
case OC_REST_OBSERVE:
case OC_REST_OBSERVE_ALL:
- case OC_REST_CANCEL_OBSERVE:
requestInfo.method = CA_GET;
break;
case OC_REST_PUT:
requestInfo.info.type = qualityOfServiceToMessageType(qos);
requestInfo.info.token = token;
requestInfo.info.tokenLength = tokenLength;
- requestInfo.info.resourceUri = resourceUri;
if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
{
if (method == OC_REST_PRESENCE)
{
char *presenceUri = NULL;
- result = OCPreparePresence(&endpoint, resourceUri, &presenceUri,
+ result = OCPreparePresence(&endpoint, &presenceUri,
requestInfo.isMulticast);
if (OC_STACK_OK != result)
{
// Assign full presence uri as coap://ip:port/oic/ad to add to callback list.
// Presence notification will form a canonical uri to
// look for callbacks into the application.
+ if (resourceUri)
+ {
+ OICFree(resourceUri);
+ }
resourceUri = presenceUri;
}
#endif
+ // update resourceUri onto requestInfo after check presence uri
+ requestInfo.info.resourceUri = resourceUri;
+
ttl = GetTicks(MAX_CB_TIMEOUT_SECONDS * MILLISECONDS_PER_SECOND);
result = AddClientCB(&clientCB, cbData, token, tokenLength, &resHandle,
method, devAddr, resourceUri, resourceType, ttl);
resourceUri = NULL; // Client CB list entry now owns it
resourceType = NULL; // Client CB list entry now owns it
+#ifdef WITH_PRESENCE
+ if (method == OC_REST_PRESENCE)
+ {
+ if (requestInfo.isMulticast)
+ {
+ OIC_LOG(ERROR, TAG, "AddClientCB for presence done.");
+ goto exit;
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "this subscribe presence is unicast.");
+ }
+ }
+#endif
+
// send request
result = OCSendRequest(&endpoint, &requestInfo);
if (OC_STACK_OK != result)
#ifdef WITH_PRESENCE
OCStackResult OCStartPresence(const uint32_t ttl)
{
+ OIC_LOG(INFO, TAG, "Entering OCStartPresence");
uint8_t tokenLength = CA_MAX_TOKEN_LEN;
OCChangeResourceProperty(
&(((OCResource *)presenceResource.handle)->resourceProperties),
OCStackResult OCStopPresence()
{
+ OIC_LOG(INFO, TAG, "Entering OCStopPresence");
OCStackResult result = OC_STACK_ERROR;
if(presenceResource.handle)
OCStackResult SendPresenceNotification(OCResourceType *resourceType,
OCPresenceTrigger trigger)
{
+ OIC_LOG(INFO, TAG, "SendPresenceNotification");
OCResource *resPtr = NULL;
OCStackResult result = OC_STACK_ERROR;
OCMethod method = OC_REST_PRESENCE;
OCStackResult SendStopNotification()
{
+ OIC_LOG(INFO, TAG, "SendStopNotification");
OCResource *resPtr = NULL;
OCStackResult result = OC_STACK_ERROR;
OCMethod method = OC_REST_PRESENCE;
}
}
+#ifdef WITH_CHPROXY
+OCStackResult OCSetProxyURI(const char *uri)
+{
+ return CAResultToOCResult(CASetProxyUri(uri));
+}
+#endif
+
#if defined(RD_CLIENT) || defined(RD_SERVER)
OCStackResult OCBindResourceInsToResource(OCResourceHandle handle, uint8_t ins)
{
}
return OC_STACK_ERROR;
}
+
+OCStackResult OCSetHeaderOption(OCHeaderOption* ocHdrOpt, size_t* numOptions, uint16_t optionID,
+ void* optionData, size_t optionDataLength)
+{
+ if (!ocHdrOpt)
+ {
+ OIC_LOG (INFO, TAG, "Header options are NULL");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ if (!optionData)
+ {
+ OIC_LOG (INFO, TAG, "optionData are NULL");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ if (!numOptions)
+ {
+ OIC_LOG (INFO, TAG, "numOptions is NULL");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ if (*numOptions >= MAX_HEADER_OPTIONS)
+ {
+ OIC_LOG (INFO, TAG, "Exceeding MAX_HEADER_OPTIONS");
+ return OC_STACK_NO_MEMORY;
+ }
+
+ ocHdrOpt += *numOptions;
+ ocHdrOpt->protocolID = OC_COAP_ID;
+ ocHdrOpt->optionID = optionID;
+ ocHdrOpt->optionLength =
+ optionDataLength < MAX_HEADER_OPTION_DATA_LENGTH ?
+ optionDataLength : MAX_HEADER_OPTION_DATA_LENGTH;
+ memcpy(ocHdrOpt->optionData, (const void*) optionData, ocHdrOpt->optionLength);
+ *numOptions += 1;
+
+ return OC_STACK_OK;
+}
+OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions, uint16_t optionID,
+ void* optionData, size_t optionDataLength, uint16_t* receivedDataLength)
+{
+ if (!ocHdrOpt || !numOptions)
+ {
+ OIC_LOG (INFO, TAG, "No options present");
+ return OC_STACK_OK;
+ }
+
+ if (!optionData)
+ {
+ OIC_LOG (INFO, TAG, "optionData are NULL");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ if (!receivedDataLength)
+ {
+ OIC_LOG (INFO, TAG, "receivedDataLength is NULL");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ for (uint8_t i = 0; i < numOptions; i++)
+ {
+ if (ocHdrOpt[i].optionID == optionID)
+ {
+ if (optionDataLength >= ocHdrOpt->optionLength)
+ {
+ memcpy(optionData, ocHdrOpt->optionData, ocHdrOpt->optionLength);
+ *receivedDataLength = ocHdrOpt->optionLength;
+ return OC_STACK_OK;
+ }
+ else
+ {
+ OIC_LOG (ERROR, TAG, "optionDataLength is less than the length of received data");
+ return OC_STACK_ERROR;
+ }
+ }
+ }
+ return OC_STACK_OK;
+}
#endif
+
+void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
+{
+ OIC_LOG(DEBUG, TAG, "OCDefaultAdapterStateChangedHandler");
+ if (g_adapterHandler)
+ {
+ g_adapterHandler(adapter, enabled);
+ }
+}
+
+void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
+{
+ OIC_LOG(DEBUG, TAG, "OCDefaultConnectionStateChangedHandler");
+ if (g_connectionHandler)
+ {
+ g_connectionHandler(info, isConnected);
+ }
+}
+
+void OCSetNetworkMonitorHandler(CAAdapterStateChangedCB adapterHandler,
+ CAConnectionStateChangedCB connectionHandler)
+{
+ OIC_LOG(DEBUG, TAG, "OCSetNetworkMonitorHandler");
+ g_adapterHandler = adapterHandler;
+ g_connectionHandler = connectionHandler;
+}
+
+OCStackResult OCGetDeviceId(OCUUIdentity *deviceId)
+{
+ OicUuid_t oicUuid;
+ OCStackResult ret;
+
+ ret = GetDoxmDeviceID(&oicUuid);
+ if (OC_STACK_OK == ret)
+ {
+ memcpy(deviceId, &oicUuid, UUID_IDENTITY_SIZE);
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Device ID Get error");
+ }
+ return ret;
+}
+
+OCStackResult OCSetDeviceId(const OCUUIdentity *deviceId)
+{
+ OicUuid_t oicUuid;
+ OCStackResult ret;
+ OIC_LOG(ERROR, TAG, "Set deviceId DOXM");
+
+ memcpy(&oicUuid, deviceId, UUID_LENGTH);
+ for(int i=0;i < UUID_LENGTH; i++)
+ {
+ OIC_LOG_V(INFO, TAG, "Set Device Id %x", oicUuid.id[i]);
+ }
+ ret = SetDoxmDeviceID(&oicUuid);
+ return ret;
+}