RD Server Response for RD Client Publish
authorHabib Virji <habib.virji@samsung.com>
Sat, 5 Sep 2015 22:51:42 +0000 (23:51 +0100)
committerMadan Lanka <lanka.madan@samsung.com>
Mon, 21 Sep 2015 08:44:24 +0000 (08:44 +0000)
RD server response for the RD Client Publish.

Include change to adapt recent changes in cbor payloadType handling.

Change-Id: I55e9cacaa1a81c87c8bcc0e80c3b57f6fd98f8fc
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2385
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
resource/csdk/stack/include/payload_logging.h
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/src/ocstack.c
service/resource-directory/include/rd_payload.h
service/resource-directory/include/rd_types.h
service/resource-directory/src/rd_payload.c
service/resource-directory/src/rd_server.c

index 797c8b3..0253180 100644 (file)
@@ -261,7 +261,7 @@ static inline void OCPayloadLog(LogLevel level, OCPayload* payload)
             break;
 #ifdef WITH_RD
         case PAYLOAD_TYPE_RD:
-            OCRDPayloadLog(level, tag, (OCRDPayload*)payload);
+            OCRDPayloadLog(level, PL_TAG, (OCRDPayload*)payload);
             break;
 #endif
         default:
index c927807..04f709b 100644 (file)
@@ -853,6 +853,13 @@ HandleResourceWithEntityHandler (OCServerRequest *request,
         type = PAYLOAD_TYPE_SECURITY;
 
     }
+#ifdef WITH_RD
+
+    if (request && request->resourceUrl && strcmp(request->resourceUrl, OC_RSRVD_RD_URI) == 0)
+    {
+        type = PAYLOAD_TYPE_RD;
+    }
+#endif
     result = FormOCEntityHandlerRequest(&ehRequest,
                                         (OCRequestHandle)request,
                                         request->method,
index 474da86..6b904b0 100644 (file)
@@ -1173,6 +1173,12 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res
                     {
                         type = PAYLOAD_TYPE_PLATFORM;
                     }
+#ifdef WITH_RD
+                    else if (strcmp(cbNode->requestUri, OC_RSRVD_RD_URI) == 0)
+                    {
+                        type = PAYLOAD_TYPE_RD;
+                    }
+#endif
                     else
                     {
                         OC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
@@ -1187,9 +1193,21 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res
                          cbNode->method == OC_REST_OBSERVE_ALL ||
                          cbNode->method == OC_REST_DELETE)
                 {
-                    OC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
-                            cbNode->method, cbNode->requestUri);
-                    type = PAYLOAD_TYPE_REPRESENTATION;
+#ifdef WITH_RD
+                    char targetUri[MAX_URI_LENGTH];
+                    snprintf(targetUri, MAX_URI_LENGTH, "%s?rt=%s",
+                            OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
+                    if (strcmp(targetUri, cbNode->requestUri) == 0)
+                    {
+                        type = PAYLOAD_TYPE_RD;
+                    }
+#endif
+                    if (type == PAYLOAD_TYPE_INVALID)
+                    {
+                        OC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
+                                cbNode->method, cbNode->requestUri);
+                        type = PAYLOAD_TYPE_REPRESENTATION;
+                    }
                 }
                 else
                 {
@@ -4163,4 +4181,3 @@ OCStackResult CAResultToOCResult(CAResult_t caResult)
             return OC_STACK_ERROR;
     }
 }
-
index 9146975..dee78c0 100644 (file)
@@ -24,6 +24,7 @@
 #include <cbor.h>
 
 #include "rd_types.h"
+#include "logger.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -42,7 +43,7 @@ extern "C" {
  * failed in creating CBOR.
  */
 OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload,
-                                uint8_t **outPayload, size_t *size);
+                                uint8_t *outPayload, size_t *size);
 
 /**
  * Converts CBOR to OCRDPayload.
index 31dfd14..879f876 100644 (file)
@@ -98,10 +98,12 @@ typedef struct
  */
 typedef enum
 {
-    /** Value pf the RD discovery payload. */
+    /** Value of the RD discovery payload. */
     RD_PAYLOAD_TYPE_DISCOVERY,
     /** Value of the RD publish payload. */
-    RD_PAYLOAD_TYPE_PUBLISH
+    RD_PAYLOAD_TYPE_PUBLISH,
+    /** Value of the RD response in the payload. */
+    RD_PAYLOAD_TYPE_RESPONSE
 } OCRDPayloadType;
 
 /**
index c025f78..cb0b629 100644 (file)
 
 #include "octypes.h"
 #include "ocstack.h"
-#include "logger.h"
 
 #define TAG PCF("OCRDPayload")
 
-#define CBOR_ROOT_ARRAY_LENGTH 2
+#define CBOR_ROOT_ARRAY_LENGTH 1
 #define CBOR_LINK_ARRAY_LENGTH 3
 
 static void linksPayloadDestroy(OCRDLinksPayload *linkPayload)
@@ -46,7 +45,7 @@ static void linksPayloadDestroy(OCRDLinksPayload *linkPayload)
     }
 }
 
-OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPayload, size_t *size)
+OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size)
 {
     if (!outPayload || !size)
     {
@@ -54,19 +53,9 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
         return OC_STACK_ERROR;
     }
 
-    *outPayload = (uint8_t *)OICCalloc(1, MAX_REQUEST_LENGTH);
-    if (!*outPayload)
-    {
-        goto no_memory;
-    }
-
-    *size = MAX_REQUEST_LENGTH;
-
-    OCRDPayloadLog(DEBUG, TAG, rdPayload);
-
     CborEncoder encoder;
     int flags = 0;
-    cbor_encoder_init(&encoder, *outPayload, *size, flags);
+    cbor_encoder_init(&encoder, outPayload, *size, flags);
 
     CborEncoder rootArray;
     CborError cborEncoderResult;
@@ -77,13 +66,6 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
         goto exit;
     }
 
-    cborEncoderResult = cbor_encode_uint(&rootArray, rdPayload->base.type);
-    if (CborNoError != cborEncoderResult)
-    {
-        OC_LOG_V(ERROR, TAG, "Failed setting rdPayload->base.type.");
-        goto exit;
-    }
-
     CborEncoder map;
     cborEncoderResult = cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
     if (CborNoError != cborEncoderResult)
@@ -254,23 +236,11 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
         goto exit;
     }
 
-    *size = encoder.ptr - *outPayload;
-    uint8_t *tempPayload = (uint8_t *)OICRealloc(*outPayload, *size);
-    if (!tempPayload)
-    {
-        goto no_memory;
-    }
-
-    *outPayload = tempPayload;
+    *size = encoder.ptr - outPayload;
     return OC_STACK_OK;
 
-no_memory:
-    OC_LOG_V(ERROR, TAG, "Memory allocation failed.");
-    OICFree(*outPayload);
-    return OC_STACK_NO_MEMORY;
-
 exit:
-    OICFree(*outPayload);
+    OICFree(outPayload);
     return OC_STACK_ERROR;
 }
 
@@ -340,7 +310,7 @@ OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPay
                 goto no_memory;
             }
         }
-        else
+        else if (RD_PAYLOAD_TYPE_PUBLISH == payloadType)
         {    // TTL
             int ttl = 0;
             cborFindResult = cbor_value_map_find_value(rdCBORPayload, OC_RSRVD_TTL, &curVal);
index b95def3..2d320e8 100644 (file)
@@ -105,6 +105,21 @@ static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *
         OCRDStorePublishedResources(payload->rdPublish);
     }
 
+    OCRDPayload *rdPayload = OCRDPayloadCreate(RD_PAYLOAD_TYPE_DISCOVERY);
+    if (!rdPayload)
+    {
+        return OC_STACK_NO_MEMORY;
+    }
+
+    OCRDPayloadLog(DEBUG, TAG, rdPayload);
+    rdPayload->payloadType = RD_PAYLOAD_TYPE_RESPONSE;
+
+    if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK)
+    {
+        OC_LOG(ERROR, TAG, "Sending response failed.");
+        ehResult = OC_EH_ERROR;
+    }
+
     return ehResult;
 }