POST updates to include new URI and response status in CoAP header
authorVijay <vijay.s.kesavan@intel.com>
Wed, 15 Oct 2014 06:30:24 +0000 (23:30 -0700)
committerVijay <vijay.s.kesavan@intel.com>
Wed, 15 Oct 2014 06:30:24 +0000 (23:30 -0700)
This was previously submitted as review # 330 but ran into issues during rebase. Abandoning 330 and creating this one. Review comments in 330 have been addressed.

Change-Id: I3f39718975421a9e8e22e953623a81b9c7aac623

csdk/occoap/include/occoaphelper.h
csdk/occoap/src/occoap.c
csdk/occoap/src/occoaphelper.c
csdk/stack/include/ocstack.h
csdk/stack/samples/linux/SimpleClientServer/common.cpp
csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
csdk/stack/src/ocobserve.c
csdk/stack/src/ocresource.c

index 23f2fa5..273a9d8 100644 (file)
@@ -76,7 +76,7 @@ OCStackResult FormOCRequest(OCRequest * * requestLoc, OCQualityOfService qos,
 // Internal function to create OCEntityHandlerRequest at the server from a received coap pdu
 OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequestLoc,
         OCMethod method, unsigned char * resBuf, unsigned char * bufReqPayload,
-        unsigned char * queryBuf);
+        unsigned char * queryBuf, unsigned char *newResUriBuf);
 
 // Internal function to retrieve Uri and Query from received coap pdu
 OCStackResult ParseCoAPPdu(coap_pdu_t * pdu, unsigned char * uriBuf,
index 1ecadf6..765d571 100644 (file)
@@ -140,6 +140,7 @@ static void HandleCoAPRequests(struct coap_context_t *ctx,
     unsigned char rcvdUri[MAX_URI_LENGTH] = { 0 };
     unsigned char rcvdQuery[MAX_QUERY_LENGTH] = { 0 };
     unsigned char bufRes[MAX_RESPONSE_LENGTH] = { 0 };
+    unsigned char newResourceUri[MAX_RESPONSE_LENGTH] = { 0 };
     uint8_t * rcvObserveOption = NULL;
     unsigned char * bufReqPayload = NULL;
     uint32_t observeOption = OC_RESOURCE_NO_OBSERVE;
@@ -191,7 +192,7 @@ static void HandleCoAPRequests(struct coap_context_t *ctx,
 
     // fill OCEntityHandlerRequest structure
     result = FormOCEntityHandlerRequest(&entityHandlerRequest, ocMethod,
-                                        bufRes, bufReqPayload, rcvdQuery);
+                                        bufRes, bufReqPayload, rcvdQuery, newResourceUri);
     VERIFY_SUCCESS(result, OC_STACK_OK);
 
    // fill OCObserveReq
@@ -249,10 +250,20 @@ static void HandleCoAPRequests(struct coap_context_t *ctx,
     }
     else
     {
-        result = FormOptionList(&optList, &mediaType, &maxAge, 0, NULL, NULL,
-                0, NULL, 0, NULL,
-                request->entityHandlerRequest->sendVendorSpecificHeaderOptions,
-                request->entityHandlerRequest->numSendVendorSpecificHeaderOptions);
+        if (responseResult == OC_STACK_RESOURCE_CREATED)
+        {
+            result = FormOptionList(&optList, &mediaType, &maxAge, 0, NULL, NULL,
+                    strlen((char *)newResourceUri), newResourceUri, 0, NULL,
+                    request->entityHandlerRequest->sendVendorSpecificHeaderOptions,
+                    request->entityHandlerRequest->numSendVendorSpecificHeaderOptions);
+        }
+        else
+        {
+            result = FormOptionList(&optList, &mediaType, &maxAge, 0, NULL, NULL,
+                    0, NULL, 0, NULL,
+                    request->entityHandlerRequest->sendVendorSpecificHeaderOptions,
+                    request->entityHandlerRequest->numSendVendorSpecificHeaderOptions);
+        }
     }
 
     VERIFY_SUCCESS(result, OC_STACK_OK);
index 56d265d..829e807 100644 (file)
@@ -57,6 +57,10 @@ uint8_t OCToCoAPResponseCode(OCStackResult result)
             ret = COAP_RESPONSE_200;
             break;
 
+        case OC_STACK_RESOURCE_CREATED:
+            ret = COAP_RESPONSE_201;
+            break;
+
         case OC_STACK_RESOURCE_DELETED:
             ret = COAP_RESPONSE_202;
             break;
@@ -109,6 +113,10 @@ OCStackResult CoAPToOCResponseCode(uint8_t coapCode)
             ret = OC_STACK_OK;
             break;
 
+        case COAP_RESPONSE_201 :
+            ret = OC_STACK_RESOURCE_CREATED;
+            break;
+
         case COAP_RESPONSE_202 :
             ret = OC_STACK_RESOURCE_DELETED;
             break;
@@ -368,7 +376,7 @@ OCStackResult FormOCObserveReq(OCObserveReq ** observeReqLoc, uint32_t observeOp
 // Form the OCEntityHandlerRequest struct
 OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequestLoc,
         OCMethod method, unsigned char * resBuf, unsigned char * bufReqPayload,
-        unsigned char * queryBuf)
+        unsigned char * queryBuf, unsigned char *newResUriBuf)
 {
     if (entityHandlerRequestLoc)
     {
@@ -387,6 +395,7 @@ OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerR
         entityHandlerRequestLoc->resJSONPayloadLen = MAX_RESPONSE_LENGTH;
 
         entityHandlerRequestLoc->obsInfo = NULL;
+        entityHandlerRequestLoc->newResourceUri = newResUriBuf;
         return OC_STACK_OK;
     }
 
index 625b900..af6f77e 100644 (file)
@@ -134,7 +134,12 @@ typedef enum {
  * Declares Stack Results & Errors
  */
 typedef enum {
+    /* Success status code - START HERE */
     OC_STACK_OK = 0,
+    OC_STACK_RESOURCE_CREATED,
+    OC_STACK_RESOURCE_DELETED,
+    /* Success status code - END HERE */
+    /* Error status code - START HERE */
     OC_STACK_INVALID_URI,
     OC_STACK_INVALID_QUERY,
     OC_STACK_INVALID_IP,
@@ -153,7 +158,6 @@ typedef enum {
     OC_STACK_OBSERVER_NOT_FOUND,
     OC_STACK_OBSERVER_NOT_ADDED,
     OC_STACK_OBSERVER_NOT_REMOVED,
-    OC_STACK_RESOURCE_DELETED,
     #ifdef WITH_PRESENCE
     OC_STACK_PRESENCE_STOPPED,
     OC_STACK_PRESENCE_DO_NOT_HANDLE,
@@ -161,6 +165,7 @@ typedef enum {
     OC_STACK_INVALID_OPTION,
     OC_STACK_MALFORMED_RESPONSE,        /* the remote reply contained malformed data */
     OC_STACK_ERROR
+    /* Error status code - END HERE */
 } OCStackResult;
 
 /**
@@ -237,6 +242,9 @@ typedef struct {
     // An array of the vendor specific header options the entity handler wishes to use in response
     uint8_t numSendVendorSpecificHeaderOptions;
     OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
+    // URI of new resource that entity handler might create
+    unsigned char *newResourceUri;
+
 }OCEntityHandlerRequest;
 
 /**
@@ -299,6 +307,7 @@ typedef struct {
 typedef enum {
     OC_EH_OK = 0,
     OC_EH_ERROR,
+    OC_EH_RESOURCE_CREATED,
     OC_EH_RESOURCE_DELETED,
     OC_EH_FORBIDDEN
 } OCEntityHandlerResult;
index e40d31e..8664f2e 100644 (file)
@@ -26,6 +26,10 @@ const char *getResult(OCStackResult result) {
     switch (result) {
     case OC_STACK_OK:
         return "OC_STACK_OK";
+    case OC_STACK_RESOURCE_CREATED:
+        return "OC_STACK_RESOURCE_CREATED";
+    case OC_STACK_RESOURCE_DELETED:
+        return "OC_STACK_RESOURCE_DELETED";
     case OC_STACK_INVALID_URI:
         return "OC_STACK_INVALID_URI";
     case OC_STACK_INVALID_QUERY:
@@ -54,8 +58,6 @@ const char *getResult(OCStackResult result) {
         return "OC_STACK_SLOW_RESOURCE";
     case OC_STACK_NO_OBSERVERS:
         return "OC_STACK_NO_OBSERVERS";
-    case OC_STACK_RESOURCE_DELETED:
-        return "OC_STACK_RESOURCE_DELETED";
     #ifdef WITH_PRESENCE
     case OC_STACK_PRESENCE_DO_NOT_HANDLE:
         return "OC_STACK_PRESENCE_DO_NOT_HANDLE";
index a537de6..1efe2b7 100644 (file)
@@ -148,8 +148,9 @@ void ProcessPutRequest (OCEntityHandlerRequest *ehRequest)
     }
 }
 
-void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
+OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
 {
+    OCEntityHandlerResult ehResult = OC_EH_OK;
     const char respPLPost_newLed[] = "{\"href\":\"\",\"rep\":{\"createduri\":\"\"}}";
     char *respPLPost_led;
     cJSON *json;
@@ -173,7 +174,7 @@ void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
             // Create new LED instance
             char newLedUri[15] = "/a/led/";
             sprintf (newLedUri + strlen(newLedUri), "%d", gCurrLedInstance);
-            printf ("\n New resource URI: %s\n", newLedUri);
+            OC_LOG_V (INFO, TAG, "New resource URI: %s", newLedUri);
 
             json = cJSON_Parse((char *)respPLPost_newLed);
             cJSON_GetObjectItem(json,"href")->valuestring = resourceUri;
@@ -187,6 +188,8 @@ void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
                 gLedInstance[gCurrLedInstance].power = 0;
                 gCurrLedInstance++;
                 respPLPost_led = cJSON_Print(json);
+                strncpy ((char *)ehRequest->newResourceUri, newLedUri, MAX_URI_LENGTH);
+                ehResult = OC_EH_RESOURCE_CREATED;
             }
 
             json = cJSON_Parse((char *)respPLPost_newLed);
@@ -230,6 +233,7 @@ void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
         OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
                 ehRequest->resJSONPayloadLen);
     }
+    return ehResult;
 }
 
 OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest)
@@ -458,7 +462,7 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag,
             else if (OC_REST_POST == entityHandlerRequest->method)
             {
                 OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
-                ProcessPostRequest (entityHandlerRequest);
+                ehResult = ProcessPostRequest (entityHandlerRequest);
             }
             else if (OC_REST_DELETE == entityHandlerRequest->method)
             {
index 5aeefa3..655a01c 100644 (file)
@@ -55,7 +55,8 @@ OCStackResult OCObserverStatus(OCCoAPToken * token, uint8_t status)
         observer = GetObserverUsingToken (token);
         if(observer)
         {
-            FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes, NULL, NULL);
+            FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes, 
+                                        NULL, NULL, NULL);
             ehRequest.obsInfo = &observationInfo;
             ehRequest.obsInfo->action = OC_OBSERVE_DEREGISTER;
             ehRequest.obsInfo->obsId = observer->observeId;
@@ -96,7 +97,8 @@ OCStackResult OCObserverStatus(OCCoAPToken * token, uint8_t status)
         {
             if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
             {
-                FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes, NULL, NULL);
+                FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes, 
+                                            NULL, NULL, NULL);
                 ehRequest.obsInfo = &observationInfo;
                 ehRequest.obsInfo->action = OC_OBSERVE_DEREGISTER;
                 ehRequest.obsInfo->obsId = observer->observeId;
@@ -256,7 +258,7 @@ OCStackResult SendObserverNotification (OCMethod method, OCResource *resPtr, uin
                 // Invoke the entity handler for the client to process
                 // the query according to the new representation
                 FormOCEntityHandlerRequest(&entityHandlerReq, OC_REST_GET, bufRes,
-                        NULL, resourceObserver->query);
+                        NULL, resourceObserver->query, NULL);
                 entityHandlerReq.resource = (OCResourceHandle)resPtr;
 
                 // Even if entity handler for a resource is not successful
index 13d7927..69355d1 100644 (file)
@@ -344,6 +344,9 @@ OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
         case OC_EH_FORBIDDEN:
             result = OC_STACK_RESOURCE_ERROR;
             break;
+        case OC_EH_RESOURCE_CREATED:
+            result = OC_STACK_RESOURCE_CREATED;
+            break;
         case OC_EH_RESOURCE_DELETED:
             result = OC_STACK_NO_RESOURCE;
             break;
@@ -483,7 +486,7 @@ HandleResourceWithEntityHandler (OCRequest *request,
         }
     }
 
-    if (result == OC_STACK_OK)
+    if (result == OC_STACK_OK || OC_STACK_RESOURCE_CREATED)
     {
         ehRequest->resJSONPayloadLen = ehRequest->resJSONPayloadLen -
             strlen((char*)ehRequest->resJSONPayload);