// For POSIX.1-2001 base specification,
// Refer http://pubs.opengroup.org/onlinepubs/009695399/
#define _POSIX_C_SOURCE 200112L
+
+#ifdef WITH_ARDUINO
#include <string.h>
+#else
+#include <strings.h>
+#endif
+
#include "ocresource.h"
#include "ocresourcehandler.h"
#include "ocobserve.h"
return OC_STACK_OK;
}
+#ifdef TCP_ADAPTER
+/* This method will retrieve the tcp port */
+static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port)
+{
+ uint16_t p = 0;
+
+ if (endpoint->adapter == OC_ADAPTER_IP)
+ {
+ if (endpoint->flags & OC_IP_USE_V4)
+ {
+ p = caglobals.tcp.ipv4.port;
+ }
+ else if (endpoint->flags & OC_IP_USE_V6)
+ {
+ p = caglobals.tcp.ipv6.port;
+ }
+ }
+
+ *port = p;
+ return OC_STACK_OK;
+}
+#endif
+
/*
* Function will extract 0, 1 or 2 filters from query.
* More than 2 filters or unsupported filters will result in error.
{
return OC_STACK_INVALID_QUERY;
}
- else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
+ else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
{
*filterOne = value; // if
}
- else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
+ else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
{
*filterTwo = value; // rt
}
keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
}
- OIC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
+ OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
return OC_STACK_OK;
}
{
return OC_STACK_INVALID_PARAM;
}
- uint16_t port = 0;
+ uint16_t securePort = 0;
if (resourcePtr->resourceProperties & OC_SECURE)
{
- if (GetSecurePortInfo(devAddr, &port) != OC_STACK_OK)
+ if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
{
- port = 0;
+ securePort = 0;
}
}
if (rdResponse)
{
- port = devAddr->port;
+ securePort = devAddr->port;
}
- OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
+ uint16_t tcpPort = 0;
+#ifdef TCP_ADAPTER
+ if (GetTCPPortInfo(devAddr, &tcpPort) != OC_STACK_OK)
+ {
+ tcpPort = 0;
+ }
+ OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
+#else
+ OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
+#endif
+
return OC_STACK_OK;
}
while (interfacePtr)
{
- if (strcmp (interfacePtr->name, interfaceFilter) == 0)
+ if ((strcmp (interfacePtr->name, interfaceFilter) == 0) &&
+ (strcmp (OC_RSRVD_INTERFACE_LL, interfaceFilter) == 0 ||
+ strcmp (OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter) == 0))
{
return true;
}
* Function will return true if all non null AND non empty filters passed in find a match.
*/
static bool includeThisResourceInResponse(OCResource *resource,
- char *interfaceFilter,
- char *resourceTypeFilter)
+ char *interfaceFilter,
+ char *resourceTypeFilter)
{
if (!resource)
{
return false;
}
- if ( resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
+ if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
{
/*
* At least one valid filter should be available to
* include the resource in discovery response
*/
- if (!((interfaceFilter && *interfaceFilter ) ||
- (resourceTypeFilter && *resourceTypeFilter)))
+ if (!(resourceTypeFilter && *resourceTypeFilter))
{
OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
resource", resource->uri);
// Step 1: Generate the response to discovery request
if (virtualUriInRequest == OC_WELL_KNOWN_URI)
{
- char *filterOne = NULL;
- char *filterTwo = NULL;
+ if (request->method == OC_REST_PUT || request->method == OC_REST_POST || request->method == OC_REST_DELETE)
+ {
+ OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d", request->resourceUrl, request->method);
+ return OC_STACK_UNAUTHORIZED_REQ;
+ }
+
+ char *interfaceQuery = NULL;
+ char *resourceTypeQuery = NULL;
discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
- &filterOne, &filterTwo);
+ &interfaceQuery, &resourceTypeQuery);
+ bool interfaceQueryAllocated = false;
+ if (!interfaceQuery && !resourceTypeQuery)
+ {
+ interfaceQueryAllocated = true;
+ interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
+ }
if (discoveryResult == OC_STACK_OK)
{
- payload = (OCPayload*)OCDiscoveryPayloadCreate();
+ payload = (OCPayload *)OCDiscoveryPayloadCreate();
- if(payload)
+ if (payload)
{
- ((OCDiscoveryPayload*)payload)->sid = (uint8_t*)OICCalloc(1, UUID_SIZE);
- memcpy(((OCDiscoveryPayload*)payload)->sid, OCGetServerInstanceID(), UUID_SIZE);
+ OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
+ discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
+ VERIFY_NON_NULL(discPayload->sid, ERROR, OC_STACK_NO_MEMORY);
- bool foundResourceAtRD = false;
- for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
+ const char* uid = OCGetServerInstanceIDString();
+ if (uid)
{
-#ifdef WITH_RD
- if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
+ memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
+ }
+
+ if (!resourceTypeQuery && interfaceQuery && (0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
+ {
+ for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
{
- OCResource *resource = NULL;
- OCDevAddr devAddr;
- discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo,
- &resource, &devAddr);
- if (discoveryResult != OC_STACK_OK)
+ bool result = false;
+ if (resource->resourceProperties & OC_DISCOVERABLE)
{
- break;
+ result = true;
}
- discoveryResult = BuildVirtualResourceResponse(resource,
- (OCDiscoveryPayload*)payload,
- &devAddr, true);
- if (payload)
+
+ if (result)
{
- ((OCDiscoveryPayload*)payload)->baseURI = OICStrdup(devAddr.addr);
+ discoveryResult = BuildVirtualResourceResponse(resource,
+ discPayload, &request->devAddr, false);
}
- foundResourceAtRD = true;
}
+ }
+ else
+ {
+ if (interfaceQuery && (0 != strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
+ {
+ discPayload->uri = OICStrdup(OC_RSRVD_WELL_KNOWN_URI);
+ VERIFY_NON_NULL(discPayload->uri, ERROR, OC_STACK_NO_MEMORY);
+ if (savedDeviceInfo.deviceName)
+ {
+ discPayload->name = OICStrdup(savedDeviceInfo.deviceName);
+ VERIFY_NON_NULL(discPayload->name, ERROR, OC_STACK_NO_MEMORY);
+ }
+ discPayload->type = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
+ VERIFY_NON_NULL(discPayload->type, ERROR, OC_STACK_NO_MEMORY);
+ OCResourcePayloadAddStringLL(&discPayload->interface, OC_RSRVD_INTERFACE_LL);
+ OCResourcePayloadAddStringLL(&discPayload->interface, OC_RSRVD_INTERFACE_DEFAULT);
+ VERIFY_NON_NULL(discPayload->interface, ERROR, OC_STACK_NO_MEMORY);
+ }
+ bool foundResourceAtRD = false;
+ for (;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
+ {
+#ifdef WITH_RD
+ if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
+ {
+ OCResource *resource1 = NULL;
+ OCDevAddr devAddr;
+ discoveryResult = checkResourceExistsAtRD(interfaceQuery,
+ resourceTypeQuery, &resource1, &devAddr);
+ if (discoveryResult != OC_STACK_OK)
+ {
+ break;
+ }
+ discoveryResult = BuildVirtualResourceResponse(resource1,
+ discPayload, &devAddr, true);
+ if (payload)
+ {
+ discPayload->baseURI = OICStrdup(devAddr.addr);
+ }
+ OICFree(resource1->uri);
+ for (OCResourceType *rsrcRt = resource1->rsrcType, *rsrcRtNext = NULL; rsrcRt; )
+ {
+ rsrcRtNext = rsrcRt->next;
+ OICFree(rsrcRt->resourcetypename);
+ OICFree(rsrcRt);
+ rsrcRt = rsrcRtNext;
+ }
+
+ for (OCResourceInterface *rsrcPtr = resource1->rsrcInterface, *rsrcNext = NULL; rsrcPtr; )
+ {
+ rsrcNext = rsrcPtr->next;
+ OICFree(rsrcPtr->name);
+ OICFree(rsrcPtr);
+ rsrcPtr = rsrcNext;
+ }
+ foundResourceAtRD = true;
+ }
#endif
- if(!foundResourceAtRD && includeThisResourceInResponse(resource, filterOne, filterTwo))
+ if (!foundResourceAtRD && includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
+ {
+ discoveryResult = BuildVirtualResourceResponse(resource,
+ discPayload, &request->devAddr, false);
+ }
+ }
+ // Set discoveryResult appropriately if no 'valid' resources are available
+ if (discPayload->resources == NULL && !foundResourceAtRD)
{
- discoveryResult = BuildVirtualResourceResponse(resource,
- (OCDiscoveryPayload*)payload,
- &request->devAddr, false);
+ discoveryResult = OC_STACK_NO_RESOURCE;
}
}
- // Set discoveryResult appropriately if no 'valid' resources are available
- if (((OCDiscoveryPayload*)payload)->resources == NULL && !foundResourceAtRD)
- {
- discoveryResult = OC_STACK_NO_RESOURCE;
- }
}
else
{
{
OIC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
}
+ if (interfaceQueryAllocated)
+ {
+ OICFree(interfaceQuery);
+ }
}
else if (virtualUriInRequest == OC_DEVICE_URI)
{
- const OicUuid_t* deviceId = OCGetServerInstanceID();
+ if (request->method == OC_REST_PUT || request->method == OC_REST_POST || request->method == OC_REST_DELETE)
+ {
+ OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d", request->resourceUrl, request->method);
+ return OC_STACK_UNAUTHORIZED_REQ;
+ }
+
+ const char* deviceId = OCGetServerInstanceIDString();
if (!deviceId)
{
discoveryResult = OC_STACK_ERROR;
}
else
{
- payload = (OCPayload*) OCDevicePayloadCreate((const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
- OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
+ payload = (OCPayload*) OCDevicePayloadCreate(deviceId, savedDeviceInfo.deviceName,
+ savedDeviceInfo.types, savedDeviceInfo.specVersion, savedDeviceInfo.dataModelVersion);
if (!payload)
{
discoveryResult = OC_STACK_NO_MEMORY;
}
else if (virtualUriInRequest == OC_PLATFORM_URI)
{
+ if (request->method == OC_REST_PUT || request->method == OC_REST_POST || request->method == OC_REST_DELETE)
+ {
+ OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d", request->resourceUrl, request->method);
+ return OC_STACK_UNAUTHORIZED_REQ;
+ }
+
payload = (OCPayload*)OCPlatformPayloadCreate(&savedPlatformInfo);
if (!payload)
{
}
else
{
- result = OC_STACK_OK;
-
// The error in observeResult for the request will be used when responding to this
// request by omitting the observation option/sequence number.
request->observeResult = OC_STACK_ERROR;
OIC_LOG(ERROR, TAG, "Observer Addition failed");
ehFlag = OC_REQUEST_FLAG;
+ FindAndDeleteServerRequest(request);
+ goto exit;
}
}
}
else
{
- result = OC_STACK_OK;
request->observeResult = OC_STACK_ERROR;
OIC_LOG(ERROR, TAG, "Observer Removal failed");
+ FindAndDeleteServerRequest(request);
+ goto exit;
}
}
else
OIC_LOG(INFO, TAG, "Deleting device info.");
OICFree(savedDeviceInfo.deviceName);
+ OCFreeOCStringLL(savedDeviceInfo.types);
+ OICFree(savedDeviceInfo.specVersion);
+ OICFree(savedDeviceInfo.dataModelVersion);
savedDeviceInfo.deviceName = NULL;
+ savedDeviceInfo.specVersion = NULL;
+ savedDeviceInfo.dataModelVersion = NULL;
}
static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
return OC_STACK_NO_MEMORY;
}
+ if (info.types)
+ {
+ savedDeviceInfo.types = CloneOCStringLL(info.types);
+ if(!savedDeviceInfo.types && info.types)
+ {
+ DeleteDeviceInfo();
+ return OC_STACK_NO_MEMORY;
+ }
+ }
+
+ if (info.specVersion)
+ {
+ savedDeviceInfo.specVersion = OICStrdup(info.specVersion);
+ if(!savedDeviceInfo.specVersion && info.specVersion)
+ {
+ DeleteDeviceInfo();
+ return OC_STACK_NO_MEMORY;
+ }
+ }
+ else
+ {
+ savedDeviceInfo.specVersion = OICStrdup(OC_SPEC_VERSION);
+ if(!savedDeviceInfo.specVersion && OC_SPEC_VERSION)
+ {
+ DeleteDeviceInfo();
+ return OC_STACK_NO_MEMORY;
+ }
+ }
+
+ if (info.dataModelVersion)
+ {
+ savedDeviceInfo.dataModelVersion = OICStrdup(info.dataModelVersion);
+ if(!savedDeviceInfo.dataModelVersion && info.dataModelVersion)
+ {
+ DeleteDeviceInfo();
+ return OC_STACK_NO_MEMORY;
+ }
+ }
+ else
+ {
+ savedDeviceInfo.dataModelVersion = OICStrdup(OC_DATA_MODEL_VERSION);
+ if(!savedDeviceInfo.dataModelVersion && OC_DATA_MODEL_VERSION)
+ {
+ DeleteDeviceInfo();
+ return OC_STACK_NO_MEMORY;
+ }
+ }
+
return OC_STACK_OK;
}
VERIFY_SUCCESS(res, OC_STACK_OK);
- if(OCGetServerInstanceID() == NULL)
+ if (OCGetServerInstanceIDString() == NULL)
{
OIC_LOG(INFO, TAG, "Device ID generation failed");
res = OC_STACK_ERROR;