#include "oicgroup.h"
#include "cJSON.h"
-#include "ocmalloc.h"
+#include "cbor.h"
+#include "ocpayload.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
#include "occollection.h"
#include "logger.h"
#include "timer.h"
-#ifndef WITH_ARDUINO
-#include <pthread.h>
-#endif
+#include "platform_features.h"
-#define TAG PCF("OICGROUP")
+#define TAG "OIC_RI_GROUP"
#define DESC_DELIMITER "\""
#define ACTION_DELIMITER "*"
#define DO_ACTION "DoAction"
#define GET_ACTIONSET "GetActionSet"
#define ACTIONSET "ActionSet"
+#define CANCEL_ACTIONSET "CancelAction"
#define DELETE_ACTIONSET "DelActionSet"
-#define OIC_ACTION_PREFIX "{\"oc\":[{\"rep\":{"
+#define DEFAULT_CONTEXT_VALUE 0x99
+
#define VARIFY_POINTER_NULL(pointer, result, toExit) \
if(pointer == NULL) \
{\
#define OCFREE(pointer) \
{ \
- OCFree(pointer); \
+ OICFree(pointer); \
pointer = NULL; \
}
-#ifndef WITH_ARDUINO
-pthread_mutex_t lock;
+// Mutex implementation macros
+#if defined(HAVE_PTHREAD_H)
+
+ #include <pthread.h>
+ pthread_mutex_t lock;
+ #define MUTEX_LOCK(ARG_NAME) { pthread_mutex_lock(ARG_NAME); }
+ #define MUTEX_UNLOCK(ARG_NAME) { pthread_mutex_unlock(ARG_NAME); }
+
+#elif defined(HAVE_WINDOWS_H)
+
+ #include <windows.h>
+ CRITICAL_SECTION lock;
+ #define MUTEX_LOCK(ARG_NAME) { EnterCriticalSection(ARG_NAME); }
+ #define MUTEX_UNLOCK(ARG_NAME) { LeaveCriticalSection(ARG_NAME); }
+
+#elif defined(WITH_ARDUINO)
+
+ #define MUTEX_LOCK(ARG_NAME) { }
+ #define MUTEX_UNLOCK(ARG_NAME) { }
+
+#else
+
+ ERROR Need mutex implementation on this platform
+
#endif
enum ACTION_TYPE
void AddScheduledResource(ScheduledResourceInfo **head,
ScheduledResourceInfo* add)
{
- OC_LOG(INFO, TAG, PCF("AddScheduledResource Entering..."));
+ OIC_LOG(INFO, TAG, "AddScheduledResource Entering...");
-#ifndef WITH_ARDUINO
- pthread_mutex_lock(&lock);
-#endif
+ MUTEX_LOCK(&lock);
ScheduledResourceInfo *tmp = NULL;
if (*head != NULL)
{
*head = add;
}
-#ifndef WITH_ARDUINO
- pthread_mutex_unlock(&lock);
-#endif
+ MUTEX_UNLOCK(&lock);
}
ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head)
{
- OC_LOG(INFO, TAG, PCF("GetScheduledResource Entering..."));
+ OIC_LOG(INFO, TAG, "GetScheduledResource Entering...");
-#ifndef WITH_ARDUINO
- pthread_mutex_lock(&lock);
-#endif
+ MUTEX_LOCK(&lock);
time_t t_now;
ScheduledResourceInfo *tmp = NULL;
tmp = head;
-#ifndef WITH_ARDUINO
+#if !defined(WITH_ARDUINO)
time(&t_now);
#else
t_now = now();
while (tmp)
{
time_t diffTm = 0;
-#ifndef WITH_ARDUINO
diffTm = timespec_diff(tmp->time, t_now);
-#else
- diffTm = timespec_diff(tmp->time, t_now);
-#endif
if (diffTm <= (time_t) 0)
{
- OC_LOG(INFO, TAG, PCF("return Call INFO."));
+ OIC_LOG(INFO, TAG, "return Call INFO.");
goto exit;
}
}
exit:
-#ifndef WITH_ARDUINO
- pthread_mutex_unlock(&lock);
-#endif
+
+ MUTEX_UNLOCK(&lock);
+
if (tmp == NULL)
{
- OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
+ OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
}
return tmp;
}
ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName)
{
- OC_LOG(INFO, TAG, PCF("GetScheduledResourceByActionSetName Entering..."));
+ OIC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering...");
+
+ MUTEX_LOCK(&lock);
-#ifndef WITH_ARDUINO
- pthread_mutex_lock(&lock);
-#endif
ScheduledResourceInfo *tmp = NULL;
tmp = head;
{
if (strcmp(tmp->actionset->actionsetName, setName) == 0)
{
- OC_LOG(INFO, TAG, PCF("return Call INFO."));
+ OIC_LOG(INFO, TAG, "return Call INFO.");
goto exit;
}
tmp = tmp->next;
}
exit:
-#ifndef WITH_ARDUINO
- pthread_mutex_unlock(&lock);
-#endif
+
+ MUTEX_UNLOCK(&lock);
+
if (tmp == NULL)
{
- OC_LOG(INFO, TAG, PCF("Cannot Find Call Info."));
+ OIC_LOG(INFO, TAG, "Cannot Find Call Info.");
}
return tmp;
}
void RemoveScheduledResource(ScheduledResourceInfo **head,
ScheduledResourceInfo* del)
{
-#ifndef WITH_ARDUINO
- pthread_mutex_lock(&lock);
-#endif
- OC_LOG(INFO, TAG, PCF("RemoveScheduledResource Entering..."));
+
+ MUTEX_LOCK(&lock);
+
+ OIC_LOG(INFO, TAG, "RemoveScheduledResource Entering...");
ScheduledResourceInfo *tmp = NULL;
if (del == NULL)
{
+
+ MUTEX_UNLOCK(&lock);
+
return;
}
}
OCFREE(del)
-#ifndef WITH_ARDUINO
- pthread_mutex_unlock(&lock);
-#endif
+
+ MUTEX_UNLOCK(&lock);
+
}
typedef struct aggregatehandleinfo
return OC_STACK_ERROR;
}
-OCStackResult DeleteActionSets(OCResource** resource)
-{
- OCActionSet *pointer = (*resource)->actionsetHead;
- OCActionSet *pDel = pointer;
-
- while (pointer)
- {
- pDel = pointer;
- pointer = pointer->next;
-
- DeleteActionSet(&pDel);
- pDel->next = NULL;
- }
-
- (*resource)->actionsetHead = NULL;
- return OC_STACK_OK;
-}
-
OCStackResult GetActionSet(const char *actionName, OCActionSet *head,
OCActionSet** actionset)
{
}
-OCStackResult ExtractKeyValueFromRequest(char *request, char **key,
- char **value)
+OCStackResult ExtractKeyValueFromRequest(OCEntityHandlerRequest *ehRequest,
+ char **key, char **value)
{
OCStackResult result = OC_STACK_OK;
- size_t length = 0;
- char* pRequest = (char *) request + strlen(OIC_ACTION_PREFIX);
- char* iterToken, *iterTokenPtr;
+ char *actionSetStr = NULL;
- iterToken = (char *) strtok_r(pRequest, ":", &iterTokenPtr);
- VARIFY_POINTER_NULL(iterToken, result, exit);
- length = strlen(iterToken) + 1;
+ if( NULL == ehRequest->payload )
+ {
+ result = OC_STACK_ERROR;
+ goto exit;
+ }
- *key = (char *) OCMalloc(length);
- VARIFY_POINTER_NULL(*key, result, exit)
+ OCRepPayload* input;
- strncpy(*key, iterToken + 1, length);
- ((*key)[((length - 1) - 2)]) = '\0';
+ input = (OCRepPayload*)(ehRequest->payload);
- iterToken = (char *) strtok_r(NULL, "}", &iterTokenPtr);
- VARIFY_POINTER_NULL(iterToken, result, exit);
- length = strlen(iterToken) + 1;
+ if(OCRepPayloadGetPropString(input, ACTIONSET, &actionSetStr))
+ {
+ *key = OICStrdup(ACTIONSET);
+ VARIFY_POINTER_NULL(*key, result, exit);
- *value = (char *) OCMalloc(length);
- VARIFY_POINTER_NULL(*value, result, exit)
+ *value = OICStrdup(actionSetStr);
+ VARIFY_POINTER_NULL(*value, result, exit);
+ }
+ else if(OCRepPayloadGetPropString(input, DO_ACTION, &actionSetStr))
+ {
+ *key = OICStrdup(DO_ACTION);
+ VARIFY_POINTER_NULL(*key, result, exit);
- strncpy(*value, iterToken + 1, length);
- ((*value)[((length - 1) - 2)]) = '\0';
+ *value = OICStrdup(actionSetStr);
+ VARIFY_POINTER_NULL(*value, result, exit);
+ }
+ else if(OCRepPayloadGetPropString(input, GET_ACTIONSET, &actionSetStr))
+ {
+ *key = OICStrdup(GET_ACTIONSET);
+ VARIFY_POINTER_NULL(*key, result, exit);
+
+ *value = OICStrdup(actionSetStr);
+ VARIFY_POINTER_NULL(*value, result, exit);
+ }
+ else if(OCRepPayloadGetPropString(input, DELETE_ACTIONSET, &actionSetStr))
+ {
+ *key = OICStrdup(DELETE_ACTIONSET);
+ VARIFY_POINTER_NULL(*key, result, exit);
+
+ *value = OICStrdup(actionSetStr);
+ VARIFY_POINTER_NULL(*value, result, exit);
+ }
+ else if(OCRepPayloadGetPropString(input, CANCEL_ACTIONSET, &actionSetStr))
+ {
+ *key = OICStrdup(CANCEL_ACTIONSET);
+ VARIFY_POINTER_NULL(*key, result, exit);
+
+ *value = OICStrdup(actionSetStr);
+ VARIFY_POINTER_NULL(*value, result, exit);
+ }
+ else
+ {
+ result = OC_STACK_ERROR;
+ }
exit:
if (result != OC_STACK_OK)
OCFREE(*value)
}
+ OCFREE(actionSetStr);
+
return result;
}
OCStackResult ExtractActionSetNameAndDelaytime(char *pChar, char **setName,
long int *pa)
{
- char *token, *tokenPtr;
+ char *token = NULL, *tokenPtr = NULL;
OCStackResult result = OC_STACK_OK;
token = (char*) strtok_r(pChar, ACTION_DELIMITER, &tokenPtr);
- *setName = (char *) OCMalloc(strlen(token) + 1);
+ VARIFY_POINTER_NULL(token, result, exit)
+
+ *setName = (char *) OICMalloc(strlen(token) + 1);
VARIFY_POINTER_NULL(*setName, result, exit)
VARIFY_PARAM_NULL(token, result, exit)
strncpy(*setName, token, strlen(token) + 1);
OCAction *action = NULL;
OCCapability *capa = NULL;
- OC_LOG(INFO, TAG, PCF("Build ActionSet Instance."));
+ OIC_LOG(INFO, TAG, "Build ActionSet Instance.");
- *set = (OCActionSet*) OCMalloc(sizeof(OCActionSet));
+ *set = (OCActionSet*) OICMalloc(sizeof(OCActionSet));
VARIFY_POINTER_NULL(*set, result, exit)
iterToken = (char *) strtok_r(actiondesc, ACTION_DELIMITER, &iterTokenPtr);
+ VARIFY_POINTER_NULL(iterToken, result, exit);
// ActionSet Name
memset(*set, 0, sizeof(OCActionSet));
- (*set)->actionsetName = (char *) OCMalloc(strlen(iterToken) + 1);
+ (*set)->actionsetName = (char *) OICMalloc(strlen(iterToken) + 1);
VARIFY_POINTER_NULL((*set)->actionsetName, result, exit)
VARIFY_PARAM_NULL(iterToken, result, exit)
strncpy((*set)->actionsetName, iterToken, strlen(iterToken) + 1);
// yyyy-mm-dd hh:mm:ss d
iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
VARIFY_PARAM_NULL(iterToken, result, exit)
-#ifndef WITH_ARDUINO
- sscanf(iterToken, "%ld %d", &(*set)->timesteps, &(*set)->type);
+#if !defined(WITH_ARDUINO)
+ if( 2 != sscanf(iterToken, "%ld %u", &(*set)->timesteps, &(*set)->type) )
+ {
+ // If the return value should be 2, the number of items in the argument. Otherwise, it fails.
+ goto exit;
+ }
#endif
- OC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
+ OIC_LOG_V(INFO, TAG, "ActionSet Name : %s", (*set)->actionsetName);
iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr);
while (iterToken)
{
- desc = (char *) OCMalloc(strlen(iterToken) + 1);
+ desc = (char *) OICMalloc(strlen(iterToken) + 1);
VARIFY_POINTER_NULL(desc, result, exit)
VARIFY_PARAM_NULL(desc, result, exit)
strncpy(desc, iterToken, strlen(iterToken) + 1);
&descIterTokenPtr);
while (descIterToken)
{
- attr = (char *) OCMalloc(strlen(descIterToken) + 1);
+ attr = (char *) OICMalloc(strlen(descIterToken) + 1);
VARIFY_POINTER_NULL(attr, result, exit)
VARIFY_PARAM_NULL(descIterToken, result, exit)
strncpy(attr, descIterToken, strlen(descIterToken) + 1);
attrIterToken = (char *) strtok_r(attr, ATTR_ASSIGN,
&attrIterTokenPtr);
- key = (char *) OCMalloc(strlen(attrIterToken) + 1);
+ VARIFY_POINTER_NULL(attrIterToken, result, exit);
+
+ key = (char *) OICMalloc(strlen(attrIterToken) + 1);
VARIFY_POINTER_NULL(key, result, exit)
VARIFY_PARAM_NULL(attrIterToken, result, exit)
strncpy(key, attrIterToken, strlen(attrIterToken) + 1);
attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN,
&attrIterTokenPtr);
- value = (char *) OCMalloc(strlen(attrIterToken) + 1);
+ VARIFY_POINTER_NULL(attrIterToken, result, exit);
+ value = (char *) OICMalloc(strlen(attrIterToken) + 1);
VARIFY_POINTER_NULL(value, result, exit)
VARIFY_PARAM_NULL(attrIterToken, result, exit)
strncpy(value, attrIterToken, strlen(attrIterToken) + 1);
if (strcmp(key, "uri") == 0)
{
- OC_LOG(INFO, TAG, PCF("Build OCAction Instance."));
+ OIC_LOG(INFO, TAG, "Build OCAction Instance.");
- action = (OCAction*) OCMalloc(sizeof(OCAction));
+ action = (OCAction*) OICMalloc(sizeof(OCAction));
VARIFY_POINTER_NULL(action, result, exit)
memset(action, 0, sizeof(OCAction));
- action->resourceUri = (char *) OCMalloc(strlen(value) + 1);
+ action->resourceUri = (char *) OICMalloc(strlen(value) + 1);
VARIFY_POINTER_NULL(action->resourceUri, result, exit)
VARIFY_PARAM_NULL(value, result, exit)
strncpy(action->resourceUri, value, strlen(value) + 1);
{
if ((key != NULL) && (value != NULL))
{
- OC_LOG(INFO, TAG, PCF("Build OCCapability Instance."));
+ OIC_LOG(INFO, TAG, "Build OCCapability Instance.");
- capa = (OCCapability*) OCMalloc(sizeof(OCCapability));
+ capa = (OCCapability*) OICMalloc(sizeof(OCCapability));
VARIFY_POINTER_NULL(capa, result, exit)
memset(capa, 0, sizeof(OCCapability));
- capa->capability = (char *) OCMalloc(strlen(key) + 1);
+ capa->capability = (char *) OICMalloc(strlen(key) + 1);
VARIFY_POINTER_NULL(capa->capability, result, exit)
VARIFY_PARAM_NULL(key, result, exit)
strncpy(capa->capability, key, strlen(key) + 1);
- capa->status = (char *) OCMalloc(strlen(value) + 1);
+ capa->status = (char *) OICMalloc(strlen(value) + 1);
VARIFY_POINTER_NULL(capa->status, result, exit)
VARIFY_PARAM_NULL(value, result, exit)
strncpy(capa->status, value, strlen(value) + 1);
OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
{
+ // Can't use the macros here as they are hardcoded to 'exit' and will
+ // result in dereferencing a null pointer.
+ if (!actionset || !desc)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
char temp[1024] = { 0 };
- int remaining = 1023;
+ size_t remaining = sizeof(temp) - 1;
OCStackResult res = OC_STACK_ERROR;
+ char* actionTypeStr = NULL;
OCAction *action = actionset->head;
if (remaining >= strlen(actionset->actionsetName) + 1)
{
- strcat(temp, actionset->actionsetName);
+ strncat(temp, actionset->actionsetName, strlen(actionset->actionsetName));
remaining -= strlen(actionset->actionsetName);
- strcat(temp, ACTION_DELIMITER);
+ strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
remaining--;
}
else
goto exit;
}
+ actionTypeStr = (char *)malloc(1024);
+ if(actionTypeStr != NULL)
+ {
+ sprintf(actionTypeStr, "%ld %u", actionset->timesteps, actionset->type);
+ if(remaining >= strlen(actionTypeStr) + strlen(ACTION_DELIMITER) + 1)
+ {
+ strncat(temp, actionTypeStr, strlen(actionTypeStr));
+ remaining -= strlen(actionTypeStr);
+ strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
+ remaining -= strlen(ACTION_DELIMITER);
+ free(actionTypeStr);
+ }
+ else
+ {
+ free(actionTypeStr);
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
+ }
+ else
+ {
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
+
while (action != NULL)
{
+ if (remaining < (strlen("uri=") + strlen(action->resourceUri) + 1))
+ {
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
+
strcat(temp, "uri=");
remaining -= strlen("uri=");
strcat(temp, action->resourceUri);
OCCapability *capas = action->head;
while (capas != NULL)
{
+ if (remaining < (strlen(capas->capability)
+ + 1 + strlen(capas->status)))
+ {
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
+
strcat(temp, capas->capability);
remaining -= strlen(capas->capability);
strcat(temp, "=");
remaining--;
strcat(temp, capas->status);
- remaining -= strlen(capas->capability);
+ remaining -= strlen(capas->status);
capas = capas->next;
if (capas != NULL)
{
+ if (remaining < 1)
+ {
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
strcat(temp, "|");
+ remaining --;
}
}
action = action->next;
if (action != NULL)
{
+ if (remaining < strlen(ACTION_DELIMITER))
+ {
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
strcat(temp, ACTION_DELIMITER);
remaining--;
}
}
- *desc = (char *) OCMalloc(1024 - remaining);
+ *desc = OICStrdup(temp);
VARIFY_POINTER_NULL(*desc, res, exit);
- strcpy(*desc, temp);
return OC_STACK_OK;
exit:
OCFREE(*desc);
return res;
-
}
OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle,
OCClientResponse* clientResponse)
{
- OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
+ (void)context;
+ (void)clientResponse;
+ OIC_LOG(INFO, TAG, "Entering ActionSetCB");
ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle);
if (info)
{
- int idx;
+ OCEntityHandlerResponse response = { 0 };
- unsigned char *responseJson;
- responseJson = (unsigned char *) OCMalloc(
- (unsigned int) (strlen((char *) clientResponse->resJSONPayload)
- + 1));
+ response.ehResult = OC_EH_OK;
- if( responseJson == NULL )
+ if(NULL == clientResponse->payload)
+ {
+ OIC_LOG(ERROR, TAG, "Error sending response");
return OC_STACK_DELETE_TRANSACTION;
+ }
- // We need the body of response.
- // Copy the body from the response
- strcpy((char *) responseJson,
- ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN));
- idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN;
- // And insert NULL at the end of body.
- (responseJson[idx]) = 0;
-
- OCEntityHandlerResponse response = { 0 };
- response.ehResult = OC_EH_OK;
- response.payload = (char*)responseJson;
- response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1;
+ // Format the response. Note this requires some info about the request
+ response.requestHandle = info->ehRequest;
+ response.resourceHandle = info->collResource;
+ response.payload = clientResponse->payload;
+ response.numSendVendorSpecificHeaderOptions = 0;
+ memset(response.sendVendorSpecificHeaderOptions, 0,
+ sizeof response.sendVendorSpecificHeaderOptions);
+ memset(response.resourceUri, 0, sizeof response.resourceUri);
+ // Indicate that response is NOT in a persistent buffer
response.persistentBufferFlag = 0;
- response.requestHandle = (OCRequestHandle) info->ehRequest;
- response.resourceHandle = (OCResourceHandle) info->collResource;
- OCDoResponse(&response);
+ // Send the response
+ if (OCDoResponse(&response) != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, TAG, "Error sending response");
+ return OC_STACK_DELETE_TRANSACTION;
+ }
RemoveClientRequestInfo(&clientRequstList, info);
OCFREE(info)
- OCFREE(responseJson)
}
return OC_STACK_KEEP_TRANSACTION;
void ActionSetCD(void *context)
{
+ (void)context;
}
OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr,
char *jsonStr;
uint16_t jsonLen;
- OC_LOG(INFO, TAG, PCF("Entering BuildActionJSON"));
+ OIC_LOG(INFO, TAG, "Entering BuildActionJSON");
json = cJSON_CreateObject();
cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject());
jsonLen = strlen(jsonStr);
if (jsonLen < *remaining)
{
- strcat((char*) bufferPtr, jsonStr);
+ strncat((char*) bufferPtr, jsonStr, jsonLen);
*remaining -= jsonLen;
bufferPtr += jsonLen;
ret = OC_STACK_OK;
return ret;
}
+OCPayload* BuildActionCBOR(OCAction* action)
+{
+ OCRepPayload* payload = OCRepPayloadCreate();
+
+ if (!payload)
+ {
+ OIC_LOG(INFO, TAG, "Failed to create put payload object");
+ return NULL;
+ }
+
+ OCCapability* pointerCapa = action->head;
+ while (pointerCapa)
+ {
+ OCRepPayloadSetPropString(payload, pointerCapa->capability, pointerCapa->status);
+ pointerCapa = pointerCapa->next;
+ }
+
+ return (OCPayload*) payload;
+}
+
unsigned int GetNumOfTargetResource(OCAction *actionset)
{
int numOfResource = 0;
return numOfResource;
}
-
-#define DEFAULT_CONTEXT_VALUE 0x99
-
-OCStackResult SendAction(OCDoHandle *handle, const char *targetUri,
- const unsigned char *action)
+OCStackResult SendAction(OCDoHandle *handle, OCServerRequest* requestHandle, const char *targetUri,
+ OCPayload *payload)
{
- OCCallbackData cbdata;
- cbdata.cb = &ActionSetCB;
- cbdata.cd = NULL;
- cbdata.context = (void*)DEFAULT_CONTEXT_VALUE;
-// TODO: Selecting OC_IPV4.
-// It is temporary change as OC_ALL is not working currently. Remove this code and use OC_ALL
-// once it is functioning.
+ OCCallbackData cbData;
+ cbData.cb = &ActionSetCB;
+ cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
+ cbData.cd = NULL;
- return OCDoResource(handle, OC_REST_PUT, targetUri,
- NULL, (char *) action, OC_IPV4, OC_NA_QOS, &cbdata, NULL, 0);
+ return OCDoResource(handle, OC_REST_PUT, targetUri, &requestHandle->devAddr,
+ payload, CT_ADAPTER_IP, OC_NA_QOS, &cbData, NULL, 0);
}
OCStackResult DoAction(OCResource* resource, OCActionSet* actionset,
OCServerRequest* requestHandle)
{
OCStackResult result = OC_STACK_ERROR;
+
+ if( NULL == actionset->head)
+ {
+ return result;
+ }
+
OCAction *pointerAction = actionset->head;
while (pointerAction != NULL)
{
- unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 };
- unsigned char* actionDescPtr = actionDesc;
- uint16_t remaining = MAX_RESPONSE_LENGTH;
+ OCPayload* payload;
+ payload = BuildActionCBOR(pointerAction);
- strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX,
- strlen((const char *) OC_JSON_PREFIX) + 1);
- BuildActionJSON(pointerAction, actionDescPtr, &remaining);
- strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX,
- strlen((const char *) OC_JSON_SUFFIX));
+ if(payload == NULL)
+ {
+ return result;
+ }
- ClientRequestInfo *info = (ClientRequestInfo *) OCMalloc(
+ ClientRequestInfo *info = (ClientRequestInfo *) OICMalloc(
sizeof(ClientRequestInfo));
if( info == NULL )
+ {
+ OCFREE(payload);
return OC_STACK_NO_MEMORY;
+ }
memset(info, 0, sizeof(ClientRequestInfo));
info->collResource = resource;
info->ehRequest = requestHandle;
- result = SendAction(&info->required, pointerAction->resourceUri,
- actionDescPtr);
+ result = SendAction(&info->required, info->ehRequest, pointerAction->resourceUri,
+ payload);
+
if (result != OC_STACK_OK)
{
+ OICFree(info);
return result;
}
void DoScheduledGroupAction()
{
- OC_LOG(INFO, TAG, PCF("DoScheduledGroupAction Entering..."));
+ OIC_LOG(INFO, TAG, "DoScheduledGroupAction Entering...");
ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList);
if (info == NULL)
{
- OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+ OIC_LOG(INFO, TAG, "Target resource is NULL");
goto exit;
}
else if (info->resource == NULL)
{
- OC_LOG(INFO, TAG, PCF("Target resource is NULL"));
+ OIC_LOG(INFO, TAG, "Target resource is NULL");
goto exit;
}
else if (info->actionset == NULL)
{
- OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
+ OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
goto exit;
}
else if (info->ehRequest == NULL)
{
- OC_LOG(INFO, TAG, PCF("Target ActionSet is NULL"));
+ OIC_LOG(INFO, TAG, "Target ActionSet is NULL");
goto exit;
}
-#ifndef WITH_ARDUINO
- pthread_mutex_lock(&lock);
-#endif
+
+ MUTEX_LOCK(&lock);
+
DoAction(info->resource, info->actionset, info->ehRequest);
-#ifndef WITH_ARDUINO
- pthread_mutex_unlock(&lock);
-#endif
+
+ MUTEX_UNLOCK(&lock);
+
if (info->actionset->type == RECURSIVE)
{
ScheduledResourceInfo *schedule;
- schedule = (ScheduledResourceInfo *) OCMalloc(
+ schedule = (ScheduledResourceInfo *) OICMalloc(
sizeof(ScheduledResourceInfo));
if (schedule)
{
- OC_LOG(INFO, TAG, PCF("Building New Call Info."));
+ OIC_LOG(INFO, TAG, "Building New Call Info.");
memset(schedule, 0, sizeof(ScheduledResourceInfo));
if (info->actionset->timesteps > 0)
{
-#ifndef WITH_ARDUINO
- pthread_mutex_lock(&lock);
-#endif
+ MUTEX_LOCK(&lock);
schedule->resource = info->resource;
schedule->actionset = info->actionset;
schedule->ehRequest = info->ehRequest;
&schedule->timer_id,
&DoScheduledGroupAction);
- OC_LOG(INFO, TAG, PCF("Reregisteration."));
-#ifndef WITH_ARDUINO
- pthread_mutex_unlock(&lock);
-#endif
+ OIC_LOG(INFO, TAG, "Reregisteration.");
+ MUTEX_UNLOCK(&lock);
AddScheduledResource(&scheduleResourceList, schedule);
}
+ else
+ {
+ OICFree(schedule);
+ }
}
}
return;
}
-OCStackResult BuildCollectionGroupActionJSONResponse(
+OCStackResult BuildCollectionGroupActionCBORResponse(
OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource,
OCEntityHandlerRequest *ehRequest)
{
OCStackResult stackRet = OC_STACK_ERROR;
- OC_LOG(INFO, TAG, PCF("Group Action is requested."));
- // if (stackRet == OC_STACK_OK)
- {
- char *doWhat = NULL;
- char *details = NULL;
+ OIC_LOG(INFO, TAG, "Group Action is requested.");
- size_t bufferLength = 0;
- unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 };
- unsigned char *bufferPtr = NULL;
+ char *doWhat = NULL;
+ char *details = NULL;
- bufferPtr = buffer;
+#if defined(_WIN32)
+ static bool initializedCriticalSection = false;
- OCResource * collResource = (OCResource *) ehRequest->resource;
-
- char *jsonResponse;
+ if(false == initializedCriticalSection) {
+ /** @todo Find a way to DeleteCriticalSection somewhere. */
+ InitializeCriticalSection(&lock);
+ initializedCriticalSection = true;
+ }
+#endif
- stackRet = ExtractKeyValueFromRequest((char *) ehRequest->reqJSONPayload,
- &doWhat, &details);
+ stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details);
- if(stackRet != OC_STACK_OK)
- {
- OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
- return stackRet;
- }
+ if(stackRet != OC_STACK_OK)
+ {
+ OIC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet);
+ return stackRet;
+ }
- stackRet = OC_STACK_ERROR;
+ stackRet = OC_STACK_ERROR;
- cJSON *json;
- cJSON *format;
+ if (method == OC_REST_PUT)
+ {
+ OIC_LOG(INFO, TAG, "Group Action[PUT].");
- if (method == OC_REST_PUT)
+ if (strcmp(doWhat, ACTIONSET) == 0)
{
- json = cJSON_CreateObject();
- cJSON_AddStringToObject(json, "href", resource->uri);
- cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
-
- OC_LOG(INFO, TAG, PCF("Group Action[PUT]."));
+ OCActionSet *actionSet = NULL;
+ stackRet = BuildActionSetFromString(&actionSet, details);
- if (strcmp(doWhat, ACTIONSET) == 0)
+ if(stackRet == OC_STACK_OK)
{
- OCActionSet *actionSet = NULL;
- stackRet = BuildActionSetFromString(&actionSet, details);
-
- if(stackRet == OC_STACK_OK)
+ if (actionSet != NULL)
{
- if (actionSet != NULL)
+ stackRet = AddActionSet(&resource->actionsetHead,
+ actionSet);
+ if (stackRet == OC_STACK_ERROR)
{
- stackRet = AddActionSet(&resource->actionsetHead,
- actionSet);
- if (stackRet == OC_STACK_ERROR)
+ if(actionSet != NULL)
{
- if(actionSet != NULL)
- {
- DeleteActionSet( &actionSet );
- }
- OC_LOG(INFO, TAG, PCF("Duplicated ActionSet "));
+ DeleteActionSet( &actionSet );
}
+ OIC_LOG(INFO, TAG, "Duplicated ActionSet ");
}
}
else
{
stackRet = OC_STACK_ERROR;
+ goto exit;
}
+ }
+ else
+ {
+ stackRet = OC_STACK_ERROR;
+ }
+ }
+ else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
+ {
+ if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
+ {
+ stackRet = OC_STACK_OK;
}
- else if (strcmp(doWhat, DELETE_ACTIONSET) == 0)
+ else
{
- if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK)
- {
- stackRet = OC_STACK_OK;
- }
- else
- {
- stackRet = OC_STACK_ERROR;
- }
+ stackRet = OC_STACK_ERROR;
}
+ }
- jsonResponse = cJSON_Print(json);
- cJSON_Delete(json);
+ OCRepPayload* payload = OCRepPayloadCreate();
- strcat((char *) bufferPtr, jsonResponse);
+ if(!payload)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
+ stackRet = OC_STACK_ERROR;
+ }
+ else
+ {
+ OCEntityHandlerResponse response = { 0 };
- bufferLength = strlen((const char *) buffer);
- if (bufferLength > 0)
+ if(stackRet == OC_STACK_OK)
+ response.ehResult = OC_EH_OK;
+ else
+ response.ehResult = OC_EH_ERROR;
+
+ // Format the response. Note this requires some info about the request
+ response.requestHandle = ehRequest->requestHandle;
+ response.resourceHandle = ehRequest->resource;
+ response.payload = (OCPayload*) payload;
+ response.numSendVendorSpecificHeaderOptions = 0;
+ memset(response.sendVendorSpecificHeaderOptions, 0,
+ sizeof response.sendVendorSpecificHeaderOptions);
+ memset(response.resourceUri, 0, sizeof response. resourceUri);
+ // Indicate that response is NOT in a persistent buffer
+ response.persistentBufferFlag = 0;
+ response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
+
+ // Send the response
+ if (OCDoResponse(&response) != OC_STACK_OK)
{
- OCEntityHandlerResponse response = { 0 };
- if(stackRet == OC_STACK_OK)
- response.ehResult = OC_EH_OK;
- else
- response.ehResult = OC_EH_ERROR;
- response.payload = (char*)buffer;
- response.payloadSize = bufferLength + 1;
- response.persistentBufferFlag = 0;
- response.requestHandle =
- (OCRequestHandle) ehRequest->requestHandle;
- response.resourceHandle = (OCResourceHandle) collResource;
- stackRet = OCDoResponse(&response);
+ OIC_LOG(ERROR, TAG, "Error sending response");
+ stackRet = OC_STACK_ERROR;
}
}
+ }
+ else if (method == OC_REST_POST)
+ {
+ OCActionSet *actionset = NULL;
- if (method == OC_REST_POST)
+ OCRepPayload* payload = OCRepPayloadCreate();
+ OCRepPayloadSetUri(payload, resource->uri);
+
+ if ((strcmp(doWhat, DO_ACTION) == 0)
+ || (strcmp(doWhat, "DoScheduledAction") == 0))
{
- OCActionSet *actionset = NULL;
+ char *pActionsetName = NULL;
+ long int delay = -1;
- json = cJSON_CreateObject();
- cJSON_AddStringToObject(json, "href", resource->uri);
+ if (strcmp(doWhat, "DoScheduledAction") == 0)
+ {
+ stackRet = ExtractActionSetNameAndDelaytime(details,
+ &pActionsetName, &delay);
- if ((strcmp(doWhat, DO_ACTION) == 0)
- || (strcmp(doWhat, "DoScheduledAction") == 0))
+ OCFREE(details)
+ details = pActionsetName;
+ }
+ else
{
- char *pActionsetName = NULL;
- long int delay = -1;
+ stackRet = OC_STACK_OK;
+ }
- if (strcmp(doWhat, "DoScheduledAction") == 0)
+ if (stackRet == OC_STACK_OK)
+ {
+ if (GetActionSet(details, resource->actionsetHead,
+ &actionset) != OC_STACK_OK)
{
- stackRet = ExtractActionSetNameAndDelaytime(details,
- &pActionsetName, &delay);
-
- OCFREE(details)
- details = pActionsetName;
+ OIC_LOG(INFO, TAG, "ERROR");
+ stackRet = OC_STACK_ERROR;
}
- else
+
+ if (actionset == NULL)
{
- stackRet = OC_STACK_OK;
+ OIC_LOG(INFO, TAG, "Cannot Find ActionSet");
+ stackRet = OC_STACK_ERROR;
}
-
- if (stackRet == OC_STACK_OK)
+ else
{
- if (GetActionSet(details, resource->actionsetHead,
- &actionset) != OC_STACK_OK)
+ OIC_LOG(INFO, TAG, "Group Action[POST].");
+ if (actionset->type == NONE)
{
- OC_LOG(INFO, TAG, PCF("ERROR"));
- stackRet = OC_STACK_ERROR;
- }
-
- if (actionset == NULL)
- {
- OC_LOG(INFO, TAG, PCF("Cannot Find ActionSet"));
- stackRet = OC_STACK_ERROR;
+ OIC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
+ actionset->actionsetName);
+ unsigned int num = GetNumOfTargetResource(
+ actionset->head);
+
+ ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
+ HandleAggregateResponse;
+ ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
+ num + 1;
+
+ DoAction(resource, actionset,
+ (OCServerRequest*) ehRequest->requestHandle);
+ stackRet = OC_STACK_OK;
}
else
{
- OC_LOG(INFO, TAG, PCF("Group Action[POST]."));
- if (actionset->type == NONE)
- {
- OC_LOG_V(INFO, TAG, "Execute ActionSet : %s",
- actionset->actionsetName);
- unsigned int num = GetNumOfTargetResource(
- actionset->head);
-
- ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler =
- HandleAggregateResponse;
- ((OCServerRequest *) ehRequest->requestHandle)->numResponses =
- num + 1;
-
- DoAction(resource, actionset,
- (OCServerRequest*) ehRequest->requestHandle);
- stackRet = OC_STACK_OK;
- }
- else
- {
- OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
- actionset->actionsetName);
+ OIC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s",
+ actionset->actionsetName);
- delay =
- (delay == -1 ? actionset->timesteps : delay);
+ delay =
+ (delay == -1 ? actionset->timesteps : delay);
- ScheduledResourceInfo *schedule;
- schedule = (ScheduledResourceInfo *) OCMalloc(
- sizeof(ScheduledResourceInfo));
+ ScheduledResourceInfo *schedule;
+ schedule = (ScheduledResourceInfo *) OICMalloc(
+ sizeof(ScheduledResourceInfo));
- if (schedule)
+ if (schedule)
+ {
+ OIC_LOG(INFO, TAG, "Building New Call Info.");
+ memset(schedule, 0,
+ sizeof(ScheduledResourceInfo));
+ MUTEX_LOCK(&lock);
+ schedule->resource = resource;
+ schedule->actionset = actionset;
+ schedule->ehRequest =
+ (OCServerRequest*) ehRequest->requestHandle;
+ MUTEX_UNLOCK(&lock);
+ if (delay > 0)
+ {
+ OIC_LOG_V(INFO, TAG, "delay_time is %ld seconds.",
+ actionset->timesteps);
+ MUTEX_LOCK(&lock);
+ schedule->time = registerTimer(delay,
+ &schedule->timer_id,
+ &DoScheduledGroupAction);
+ MUTEX_UNLOCK(&lock);
+ AddScheduledResource(&scheduleResourceList,
+ schedule);
+ stackRet = OC_STACK_OK;
+ }
+ else
{
- OC_LOG(INFO, TAG, PCF("Building New Call Info."));
- memset(schedule, 0,
- sizeof(ScheduledResourceInfo));
-
- schedule->resource = resource;
- schedule->actionset = actionset;
- schedule->ehRequest =
- (OCServerRequest*) ehRequest->requestHandle;
-
- if (delay > 0)
- {
- OC_LOG_V(INFO, TAG, "delay_time is %lf seconds.",
- actionset->timesteps);
-
- schedule->time = registerTimer(delay,
- &schedule->timer_id,
- &DoScheduledGroupAction);
-
- AddScheduledResource(&scheduleResourceList,
- schedule);
- stackRet = OC_STACK_OK;
- }
- else
- {
- stackRet = OC_STACK_ERROR;
- }
+ stackRet = OC_STACK_ERROR;
}
}
}
}
}
- else if (strcmp(doWhat, "CancelAction") == 0)
- {
- ScheduledResourceInfo *info =
- GetScheduledResourceByActionSetName(scheduleResourceList, details);
+ }
+ else if (strcmp(doWhat, "CancelAction") == 0)
+ {
+ ScheduledResourceInfo *info =
+ GetScheduledResourceByActionSetName(scheduleResourceList, details);
- if(info != NULL)
- {
- unregisterTimer(info->timer_id);
+ if(info != NULL)
+ {
+ unregisterTimer(info->timer_id);
- RemoveScheduledResource(&scheduleResourceList, info);
- stackRet = OC_STACK_OK;
- }
- else
- {
- stackRet = OC_STACK_ERROR;
- }
+ RemoveScheduledResource(&scheduleResourceList, info);
+ stackRet = OC_STACK_OK;
}
+ else
+ {
+ stackRet = OC_STACK_ERROR;
+ }
+ }
- else if (strcmp(doWhat, GET_ACTIONSET) == 0)
+ else if (strcmp(doWhat, GET_ACTIONSET) == 0)
+ {
+ char *plainText = NULL;
+ OCActionSet *actionset = NULL;
+
+ GetActionSet(details, resource->actionsetHead, &actionset);
+ if (actionset != NULL)
{
- char *plainText = NULL;
- OCActionSet *actionset = NULL;
+ BuildStringFromActionSet(actionset, &plainText);
- cJSON_AddItemToObject(json, "rep", format =
- cJSON_CreateObject());
- GetActionSet(details, resource->actionsetHead, &actionset);
- if (actionset != NULL)
+ if (plainText != NULL)
{
- BuildStringFromActionSet(actionset, &plainText);
-
- if (plainText != NULL)
- {
- cJSON_AddStringToObject(format, ACTIONSET, plainText);
- }
- OCFree(plainText);
- stackRet = OC_STACK_OK;
+ OCRepPayloadSetPropString(payload, ACTIONSET, plainText);
}
+ OICFree(plainText);
+ stackRet = OC_STACK_OK;
}
+ }
-
- jsonResponse = cJSON_Print(json);
- cJSON_Delete(json);
-
- strcat((char *) bufferPtr, jsonResponse);
-
- bufferLength = strlen((const char *) buffer);
- if (bufferLength > 0)
+ if(!payload)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
+ stackRet = OC_STACK_ERROR;
+ }
+ else
+ {
+ OCEntityHandlerResponse response = { 0 };
+ if(stackRet == OC_STACK_OK)
+ response.ehResult = OC_EH_OK;
+ else
+ response.ehResult = OC_EH_ERROR;
+
+ // Format the response. Note this requires some info about the request
+ response.requestHandle = ehRequest->requestHandle;
+ response.resourceHandle = ehRequest->resource;
+ response.payload = (OCPayload*) payload;
+ response.numSendVendorSpecificHeaderOptions = 0;
+ memset(response.sendVendorSpecificHeaderOptions, 0,
+ sizeof response.sendVendorSpecificHeaderOptions);
+ memset(response.resourceUri, 0, sizeof response.resourceUri);
+ // Indicate that response is NOT in a persistent buffer
+ response.persistentBufferFlag = 0;
+ response.ehResult = (stackRet == OC_STACK_OK)?OC_EH_OK:OC_EH_ERROR;
+
+ // Send the response
+ if (OCDoResponse(&response) != OC_STACK_OK)
{
- OCEntityHandlerResponse response = { 0 };
- if(stackRet == OC_STACK_OK)
- response.ehResult = OC_EH_OK;
- else
- response.ehResult = OC_EH_ERROR;
- response.payload = (char *)buffer;
- response.payloadSize = bufferLength + 1;
- response.persistentBufferFlag = 0;
- response.requestHandle =
- (OCRequestHandle) ehRequest->requestHandle;
- response.resourceHandle = (OCResourceHandle) collResource;
- stackRet = OCDoResponse(&response);
+ OIC_LOG(ERROR, TAG, "Error sending response");
+ stackRet = OC_STACK_ERROR;
}
}
-
- OCFREE(doWhat)
- OCFREE(details)
}
+exit:
+
+ OCFREE(doWhat)
+ OCFREE(details)
+
return stackRet;
}