From: Yamin Al-Mousa Date: Fri, 19 Dec 2014 23:32:15 +0000 (-0800) Subject: Registering observers in the stack X-Git-Tag: 1.2.0+RC1~1855^2~324 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f07893cfa6b6d496e82580fcba0ee27b1b769b3c;p=platform%2Fupstream%2Fiotivity.git Registering observers in the stack Changes made to store information regarding remote end points for observers to send subsequent notifications. Change-Id: I46c15b32919c1b1edd7297ace9676fdeab44808b Signed-off-by: Yamin Al-Mousa --- diff --git a/resource/csdk/stack/include/internal/ocobserve.h b/resource/csdk/stack/include/internal/ocobserve.h index 50db45d..00b291c 100644 --- a/resource/csdk/stack/include/internal/ocobserve.h +++ b/resource/csdk/stack/include/internal/ocobserve.h @@ -27,13 +27,6 @@ #define MAX_OBSERVER_FAILED_COMM (2) #define MAX_OBSERVER_NON_COUNT (3) -#ifdef CA_INT -// NOTE: These values are based on the observe option values as defined in the CoAP RFC -// The values should not be changed unless there is a change in the RFC. -#define OC_RESOURCE_OBSERVE_REGISTER (0) -#define OC_RESOURCE_OBSERVE_DEREGISTER (1) -#endif // CA_INT - /* This information is stored for each registerd observer */ typedef struct ResourceObserver { // Observation Identifier for request @@ -48,6 +41,13 @@ typedef struct ResourceObserver { OCResource *resource; // IP address & port of client registered for observe OCDevAddr *addr; +#ifdef CA_INT + /** Remote Endpoint address **/ + CAAddress_t addressInfo; + /** Connectivity of the endpoint**/ + CAConnectivityType_t connectivityType; + char CAToken[32]; // TODO-CA: What is max CAToken_t length? Get rid of magic number +#endif // Quality of service of the request OCQualityOfService qos; // number of times the server failed to reach the observer @@ -91,11 +91,26 @@ ResourceObserver* GetObserverUsingToken (const OCCoAPToken * token); ResourceObserver* GetObserverUsingId (const OCObservationId observeId); #ifdef CA_INT +OCStackResult AddCAObserver (const char *resUri, + const char *query, + OCObservationId obsId, + OCCoAPToken *token, + OCDevAddr *addr, + OCResource *resHandle, + OCQualityOfService qos, + CAAddress_t *addressInfo, + CAConnectivityType_t connectivityType, + char *CAtoken); + OCStackResult CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt, OCHeaderOption *ocHdrOpt, uint8_t numOptions, uint8_t observeFlag); +OCStackResult +GetObserveHeaderOption (uint32_t * observationOption, + CAHeaderOption_t *options, + uint8_t * numOptions); #endif // CA_INT #endif //OC_OBSERVE_H diff --git a/resource/csdk/stack/src/ocobserve.c b/resource/csdk/stack/src/ocobserve.c index 6053120..f4df785 100644 --- a/resource/csdk/stack/src/ocobserve.c +++ b/resource/csdk/stack/src/ocobserve.c @@ -274,6 +274,75 @@ exit: return OC_STACK_ERROR; } +#ifdef CA_INT +OCStackResult AddCAObserver (const char *resUri, + const char *query, + OCObservationId obsId, + OCCoAPToken *token, + OCDevAddr *addr, + OCResource *resHandle, + OCQualityOfService qos, + CAAddress_t *addressInfo, + CAConnectivityType_t connectivityType, + char *CAtoken) +{ + ResourceObserver *obsNode = NULL; + + obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver)); + if (obsNode) + { + obsNode->observeId = obsId; + + obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1); + VERIFY_NON_NULL (obsNode->resUri); + memcpy (obsNode->resUri, resUri, strlen(resUri)+1); + + obsNode->qos = qos; + if(query) + { + obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1); + VERIFY_NON_NULL (obsNode->query); + memcpy (obsNode->query, query, strlen(query)+1); + } + + if(token) + { + obsNode->token.tokenLength = token->tokenLength; + memcpy (obsNode->token.token, token->token, token->tokenLength); + } + + if(addr) + { + obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr)); + VERIFY_NON_NULL (obsNode->addr); + memcpy (obsNode->addr, addr, sizeof(OCDevAddr)); + } + + obsNode->addressInfo = *addressInfo; + obsNode->connectivityType = connectivityType; + if(CAtoken) + { + strncpy(obsNode->CAToken, CAtoken, sizeof(obsNode->CAToken) - 1); + } + + obsNode->resource = resHandle; + + LL_APPEND (serverObsList, obsNode); + return OC_STACK_OK; + } + +exit: + if (obsNode) + { + OCFree(obsNode->resUri); + OCFree(obsNode->query); + OCFree(obsNode->addr); + OCFree(obsNode); + } + return OC_STACK_NO_MEMORY; +} +#endif //CA_INT + OCStackResult AddObserver (const char *resUri, const char *query, OCObservationId obsId, @@ -424,4 +493,32 @@ CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt, *caHdrOpt = tmpHdrOpt; return OC_STACK_OK; } + +OCStackResult +GetObserveHeaderOption (uint32_t * observationOption, + CAHeaderOption_t *options, + uint8_t * numOptions) +{ + *observationOption = OC_OBSERVE_NO_OPTION; + uint8_t i = 0; + uint8_t c = 0; + for(i = 0; i < *numOptions; i++) + { + if(options[i].protocolID == CA_COAP_ID && + options[i].optionID == COAP_OPTION_OBSERVE) + { + *observationOption = options[i].optionData[0]; + for(c = i; c < *numOptions-1; c++) + { + options[i].protocolID = options[i+1].protocolID; + options[i].optionID = options[i+1].optionID; + options[i].optionLength = options[i+1].optionLength; + memcpy(options[i].optionData, options[i+1].optionData, options[i+1].optionLength); + } + (*numOptions)--; + return OC_STACK_OK; + } + } + return OC_STACK_OK; +} #endif // CA_INT diff --git a/resource/csdk/stack/src/ocresource.c b/resource/csdk/stack/src/ocresource.c index 78cb387..95c1f56 100644 --- a/resource/csdk/stack/src/ocresource.c +++ b/resource/csdk/stack/src/ocresource.c @@ -519,11 +519,19 @@ HandleResourceWithEntityHandler (OCServerRequest *request, OC_LOG(INFO, TAG, PCF("Registering observation requested")); result = GenerateObserverId(&ehRequest.obsInfo.obsId); VERIFY_SUCCESS(result, OC_STACK_OK); - +#ifdef CA_INT + result = AddCAObserver ((const char*)(request->resourceUrl), + (const char *)(request->query), + ehRequest.obsInfo.obsId, &request->requestToken, + &request->requesterAddr, resource, request->qos, + &request->addressInfo, request->connectivityType, + request->token); +#else result = AddObserver ((const char*)(request->resourceUrl), (const char *)(request->query), ehRequest.obsInfo.obsId, &request->requestToken, &request->requesterAddr, resource, request->qos); +#endif //CA_INT if(result == OC_STACK_OK) { OC_LOG(DEBUG, TAG, PCF("Added observer successfully")); diff --git a/resource/csdk/stack/src/ocserverrequest.c b/resource/csdk/stack/src/ocserverrequest.c index c98c7ba..3cca1ec 100644 --- a/resource/csdk/stack/src/ocserverrequest.c +++ b/resource/csdk/stack/src/ocserverrequest.c @@ -411,6 +411,7 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse) responseInfo.info.type = CA_MSG_NONCONFIRM; break; } + responseInfo.info.token = serverRequest->token; responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions; if (ehResponse->numSendVendorSpecificHeaderOptions) @@ -418,7 +419,6 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse) memcpy(responseInfo.info.options, ehResponse->sendVendorSpecificHeaderOptions, sizeof(OCHeaderOption) * ehResponse->numSendVendorSpecificHeaderOptions); } - // Allocate memory for the payload. char *payload = (char *)OCMalloc(MAX_RESPONSE_LENGTH); if(!payload) @@ -426,7 +426,6 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse) return OC_STACK_NO_MEMORY; } memset(payload, 0, MAX_RESPONSE_LENGTH); - // Put the JSON prefix and suffix around the payload strcpy(payload, (const char *)OC_JSON_PREFIX); strcat(payload, (const char *)ehResponse->payload); diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index e5b5d73..23bd367 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -299,6 +299,7 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1; memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload, strlen(requestInfo->info.payload)); + serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1; } else { @@ -342,7 +343,7 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* memcpy (&(serverRequest.requestToken.token), requestInfo->info.token, strlen(requestInfo->info.token)); serverRequest.requestToken.tokenLength = strlen(requestInfo->info.token); - serverRequest.observationOption = OC_OBSERVE_NO_OPTION; + if (requestInfo->info.type == CA_MSG_CONFIRM) { serverRequest.qos = OC_HIGH_QOS; @@ -380,6 +381,8 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* // copy vendor specific header options // TODO-CA: CA is including non-vendor header options as well, like observe. // Need to filter those out + GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &(requestInfo->info.numOptions)); + printf("\n*****************************\nobservation is %u\n**********************************\n",serverRequest.observationOption); if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS) { // TODO-CA: Need to send an error indicating the num of options is incorrect @@ -1245,7 +1248,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL)) { result = CreateObserveHeaderOption (&(requestData.options), options, - numOptions, OC_RESOURCE_OBSERVE_REGISTER); + numOptions, OC_OBSERVE_REGISTER); if (result != OC_STACK_OK) { goto exit;