X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsrc%2Foicgroup.c;h=a2968cd10e3b699d8ddcfed28594b52c7e1f153f;hb=dbcc3f47fe5abd2102750cdd3ace7f92619e8dd0;hp=d7eb9d911eb7dfaad40cc8d0d732810d08fb08eb;hpb=d0baf5ae6936a16de6a49588428204d8c386ac9e;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/stack/src/oicgroup.c b/resource/csdk/stack/src/oicgroup.c index d7eb9d9..a2968cd 100755 --- a/resource/csdk/stack/src/oicgroup.c +++ b/resource/csdk/stack/src/oicgroup.c @@ -20,6 +20,8 @@ #define _POSIX_C_SOURCE 200112L +#include "iotivity_config.h" + #include #include "oicgroup.h" @@ -32,11 +34,7 @@ #include "logger.h" #include "timer.h" -#ifndef WITH_ARDUINO -#include -#endif - -#define TAG "OICGROUP" +#define TAG "OIC_RI_GROUP" #define DESC_DELIMITER "\"" #define ACTION_DELIMITER "*" @@ -71,8 +69,30 @@ pointer = NULL; \ } -#ifndef WITH_ARDUINO -pthread_mutex_t lock; +// Mutex implementation macros +#if defined(HAVE_PTHREAD_H) + + #include + 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 + 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 @@ -98,11 +118,9 @@ ScheduledResourceInfo *scheduleResourceList = NULL; void AddScheduledResource(ScheduledResourceInfo **head, ScheduledResourceInfo* add) { - OC_LOG(INFO, TAG, "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) @@ -119,25 +137,21 @@ void AddScheduledResource(ScheduledResourceInfo **head, { *head = add; } -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + MUTEX_UNLOCK(&lock); } ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head) { - OC_LOG(INFO, TAG, "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(); @@ -148,15 +162,11 @@ ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head) 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, "return Call INFO."); + OIC_LOG(INFO, TAG, "return Call INFO."); goto exit; } @@ -165,23 +175,22 @@ ScheduledResourceInfo* GetScheduledResource(ScheduledResourceInfo *head) } exit: -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + + MUTEX_UNLOCK(&lock); + if (tmp == NULL) { - OC_LOG(INFO, TAG, "Cannot Find Call Info."); + OIC_LOG(INFO, TAG, "Cannot Find Call Info."); } return tmp; } ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo *head, char *setName) { - OC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering..."); + OIC_LOG(INFO, TAG, "GetScheduledResourceByActionSetName Entering..."); + + MUTEX_LOCK(&lock); -#ifndef WITH_ARDUINO - pthread_mutex_lock(&lock); -#endif ScheduledResourceInfo *tmp = NULL; tmp = head; @@ -191,7 +200,7 @@ ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo { if (strcmp(tmp->actionset->actionsetName, setName) == 0) { - OC_LOG(INFO, TAG, "return Call INFO."); + OIC_LOG(INFO, TAG, "return Call INFO."); goto exit; } tmp = tmp->next; @@ -199,12 +208,12 @@ ScheduledResourceInfo* GetScheduledResourceByActionSetName(ScheduledResourceInfo } exit: -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + + MUTEX_UNLOCK(&lock); + if (tmp == NULL) { - OC_LOG(INFO, TAG, "Cannot Find Call Info."); + OIC_LOG(INFO, TAG, "Cannot Find Call Info."); } return tmp; } @@ -212,17 +221,17 @@ exit: void RemoveScheduledResource(ScheduledResourceInfo **head, ScheduledResourceInfo* del) { -#ifndef WITH_ARDUINO - pthread_mutex_lock(&lock); -#endif - OC_LOG(INFO, TAG, "RemoveScheduledResource Entering..."); + + MUTEX_LOCK(&lock); + + OIC_LOG(INFO, TAG, "RemoveScheduledResource Entering..."); ScheduledResourceInfo *tmp = NULL; if (del == NULL) { -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + + MUTEX_UNLOCK(&lock); + return; } @@ -244,9 +253,9 @@ void RemoveScheduledResource(ScheduledResourceInfo **head, } OCFREE(del) -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + + MUTEX_UNLOCK(&lock); + } typedef struct aggregatehandleinfo @@ -496,24 +505,6 @@ OCStackResult FindAndDeleteActionSet(OCResource **resource, 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) { @@ -647,12 +638,13 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc) OCAction *action = NULL; OCCapability *capa = NULL; - OC_LOG(INFO, TAG, "Build ActionSet Instance."); + OIC_LOG(INFO, TAG, "Build ActionSet Instance."); *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)); @@ -667,7 +659,7 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc) // yyyy-mm-dd hh:mm:ss d iterToken = (char *) strtok_r(NULL, ACTION_DELIMITER, &iterTokenPtr); VARIFY_PARAM_NULL(iterToken, result, exit) -#ifndef WITH_ARDUINO +#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. @@ -675,7 +667,7 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc) } #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) @@ -704,6 +696,7 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc) attrIterToken = (char *) strtok_r(NULL, ATTR_ASSIGN, &attrIterTokenPtr); + VARIFY_POINTER_NULL(attrIterToken, result, exit); value = (char *) OICMalloc(strlen(attrIterToken) + 1); VARIFY_POINTER_NULL(value, result, exit) VARIFY_PARAM_NULL(attrIterToken, result, exit) @@ -711,7 +704,7 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc) if (strcmp(key, "uri") == 0) { - OC_LOG(INFO, TAG, "Build OCAction Instance."); + OIC_LOG(INFO, TAG, "Build OCAction Instance."); action = (OCAction*) OICMalloc(sizeof(OCAction)); VARIFY_POINTER_NULL(action, result, exit) @@ -725,7 +718,7 @@ OCStackResult BuildActionSetFromString(OCActionSet **set, char* actiondesc) { if ((key != NULL) && (value != NULL)) { - OC_LOG(INFO, TAG, "Build OCCapability Instance."); + OIC_LOG(INFO, TAG, "Build OCCapability Instance."); capa = (OCCapability*) OICMalloc(sizeof(OCCapability)); VARIFY_POINTER_NULL(capa, result, exit) @@ -785,14 +778,15 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc) char temp[1024] = { 0 }; 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 @@ -801,6 +795,31 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc) 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)) @@ -808,6 +827,7 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc) res = OC_STACK_ERROR; goto exit; } + strcat(temp, "uri="); remaining -= strlen("uri="); strcat(temp, action->resourceUri); @@ -841,6 +861,7 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc) goto exit; } strcat(temp, "|"); + remaining --; } } @@ -872,7 +893,7 @@ OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle, { (void)context; (void)clientResponse; - OC_LOG(INFO, TAG, "Entering ActionSetCB"); + OIC_LOG(INFO, TAG, "Entering ActionSetCB"); ClientRequestInfo *info = GetClientRequestInfo(clientRequstList, handle); @@ -884,7 +905,7 @@ OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle, if(NULL == clientResponse->payload) { - OC_LOG(ERROR, TAG, "Error sending response"); + OIC_LOG(ERROR, TAG, "Error sending response"); return OC_STACK_DELETE_TRANSACTION; } @@ -902,7 +923,7 @@ OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle, // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "Error sending response"); + OIC_LOG(ERROR, TAG, "Error sending response"); return OC_STACK_DELETE_TRANSACTION; } @@ -928,7 +949,7 @@ OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr, char *jsonStr; uint16_t jsonLen; - OC_LOG(INFO, TAG, "Entering BuildActionJSON"); + OIC_LOG(INFO, TAG, "Entering BuildActionJSON"); json = cJSON_CreateObject(); cJSON_AddItemToObject(json, "rep", body = cJSON_CreateObject()); @@ -946,7 +967,7 @@ OCStackResult BuildActionJSON(OCAction* action, unsigned char* bufferPtr, jsonLen = strlen(jsonStr); if (jsonLen < *remaining) { - strcat((char*) bufferPtr, jsonStr); + strncat((char*) bufferPtr, jsonStr, jsonLen); *remaining -= jsonLen; bufferPtr += jsonLen; ret = OC_STACK_OK; @@ -964,7 +985,7 @@ OCPayload* BuildActionCBOR(OCAction* action) if (!payload) { - OC_LOG(INFO, TAG, "Failed to create put payload object"); + OIC_LOG(INFO, TAG, "Failed to create put payload object"); return NULL; } @@ -1061,36 +1082,36 @@ OCStackResult DoAction(OCResource* resource, OCActionSet* actionset, void DoScheduledGroupAction() { - OC_LOG(INFO, TAG, "DoScheduledGroupAction Entering..."); + OIC_LOG(INFO, TAG, "DoScheduledGroupAction Entering..."); ScheduledResourceInfo* info = GetScheduledResource(scheduleResourceList); if (info == NULL) { - OC_LOG(INFO, TAG, "Target resource is NULL"); + OIC_LOG(INFO, TAG, "Target resource is NULL"); goto exit; } else if (info->resource == NULL) { - OC_LOG(INFO, TAG, "Target resource is NULL"); + OIC_LOG(INFO, TAG, "Target resource is NULL"); goto exit; } else if (info->actionset == NULL) { - OC_LOG(INFO, TAG, "Target ActionSet is NULL"); + OIC_LOG(INFO, TAG, "Target ActionSet is NULL"); goto exit; } else if (info->ehRequest == NULL) { - OC_LOG(INFO, TAG, "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) { @@ -1100,14 +1121,12 @@ void DoScheduledGroupAction() if (schedule) { - OC_LOG(INFO, TAG, "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; @@ -1116,10 +1135,8 @@ void DoScheduledGroupAction() &schedule->timer_id, &DoScheduledGroupAction); - OC_LOG(INFO, TAG, "Reregisteration."); -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + OIC_LOG(INFO, TAG, "Reregisteration."); + MUTEX_UNLOCK(&lock); AddScheduledResource(&scheduleResourceList, schedule); } else @@ -1142,16 +1159,26 @@ OCStackResult BuildCollectionGroupActionCBORResponse( { OCStackResult stackRet = OC_STACK_ERROR; - OC_LOG(INFO, TAG, "Group Action is requested."); + OIC_LOG(INFO, TAG, "Group Action is requested."); char *doWhat = NULL; char *details = NULL; +#if defined(_WIN32) + static bool initializedCriticalSection = false; + + if(false == initializedCriticalSection) { + /** @todo Find a way to DeleteCriticalSection somewhere. */ + InitializeCriticalSection(&lock); + initializedCriticalSection = true; + } +#endif + stackRet = ExtractKeyValueFromRequest(ehRequest, &doWhat, &details); if(stackRet != OC_STACK_OK) { - OC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet); + OIC_LOG_V(ERROR, TAG, "ExtractKeyValueFromRequest failed: %d", stackRet); return stackRet; } @@ -1159,7 +1186,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( if (method == OC_REST_PUT) { - OC_LOG(INFO, TAG, "Group Action[PUT]."); + OIC_LOG(INFO, TAG, "Group Action[PUT]."); if (strcmp(doWhat, ACTIONSET) == 0) { @@ -1178,7 +1205,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( { DeleteActionSet( &actionSet ); } - OC_LOG(INFO, TAG, "Duplicated ActionSet "); + OIC_LOG(INFO, TAG, "Duplicated ActionSet "); } } else @@ -1209,7 +1236,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( if(!payload) { - OC_LOG(ERROR, TAG, "Failed to allocate Payload"); + OIC_LOG(ERROR, TAG, "Failed to allocate Payload"); stackRet = OC_STACK_ERROR; } else @@ -1236,7 +1263,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "Error sending response"); + OIC_LOG(ERROR, TAG, "Error sending response"); stackRet = OC_STACK_ERROR; } } @@ -1272,21 +1299,21 @@ OCStackResult BuildCollectionGroupActionCBORResponse( if (GetActionSet(details, resource->actionsetHead, &actionset) != OC_STACK_OK) { - OC_LOG(INFO, TAG, "ERROR"); + OIC_LOG(INFO, TAG, "ERROR"); stackRet = OC_STACK_ERROR; } if (actionset == NULL) { - OC_LOG(INFO, TAG, "Cannot Find ActionSet"); + OIC_LOG(INFO, TAG, "Cannot Find ActionSet"); stackRet = OC_STACK_ERROR; } else { - OC_LOG(INFO, TAG, "Group Action[POST]."); + OIC_LOG(INFO, TAG, "Group Action[POST]."); if (actionset->type == NONE) { - OC_LOG_V(INFO, TAG, "Execute ActionSet : %s", + OIC_LOG_V(INFO, TAG, "Execute ActionSet : %s", actionset->actionsetName); unsigned int num = GetNumOfTargetResource( actionset->head); @@ -1302,7 +1329,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( } else { - OC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s", + OIC_LOG_V(INFO, TAG, "Execute Scheduled ActionSet : %s", actionset->actionsetName); delay = @@ -1314,32 +1341,24 @@ OCStackResult BuildCollectionGroupActionCBORResponse( if (schedule) { - OC_LOG(INFO, TAG, "Building New Call Info."); + OIC_LOG(INFO, TAG, "Building New Call Info."); memset(schedule, 0, sizeof(ScheduledResourceInfo)); -#ifndef WITH_ARDUINO - pthread_mutex_lock(&lock); -#endif + MUTEX_LOCK(&lock); schedule->resource = resource; schedule->actionset = actionset; schedule->ehRequest = (OCServerRequest*) ehRequest->requestHandle; -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + MUTEX_UNLOCK(&lock); if (delay > 0) { - OC_LOG_V(INFO, TAG, "delay_time is %ld seconds.", + OIC_LOG_V(INFO, TAG, "delay_time is %ld seconds.", actionset->timesteps); -#ifndef WITH_ARDUINO - pthread_mutex_lock(&lock); -#endif + MUTEX_LOCK(&lock); schedule->time = registerTimer(delay, &schedule->timer_id, &DoScheduledGroupAction); -#ifndef WITH_ARDUINO - pthread_mutex_unlock(&lock); -#endif + MUTEX_UNLOCK(&lock); AddScheduledResource(&scheduleResourceList, schedule); stackRet = OC_STACK_OK; @@ -1392,7 +1411,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( if(!payload) { - OC_LOG(ERROR, TAG, "Failed to allocate Payload"); + OIC_LOG(ERROR, TAG, "Failed to allocate Payload"); stackRet = OC_STACK_ERROR; } else @@ -1418,7 +1437,7 @@ OCStackResult BuildCollectionGroupActionCBORResponse( // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "Error sending response"); + OIC_LOG(ERROR, TAG, "Error sending response"); stackRet = OC_STACK_ERROR; } }