RD client and server minor updates to handle sample apps
authorHabib Virji <habib.virji@samsung.com>
Wed, 19 Aug 2015 17:52:45 +0000 (18:52 +0100)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 21 Aug 2015 00:35:44 +0000 (00:35 +0000)
There are minor updates to handle discovery and publish on client and server side.

Change-Id: Id259eb4aa0eeab9808b7564a9f6844675f9830c2
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2247
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-directory/src/rd_client.c
service/resource-directory/src/rd_payload.c
service/resource-directory/src/rd_server.c

index 0077a95f6405447c518b644fc7f405249b7604a1..63077ca2cfce7e758837b57753adc1f9ef1b9523 100644 (file)
@@ -38,7 +38,7 @@ static OCStackResult sendRequest(OCMethod method, char *uri, OCDevAddr *addr,
     OCDoHandle handle;
     OCStackResult result;
 
-    result = OCDoResource(handle,
+    result = OCDoResource(&handle,
         method,
         uri,
         addr,
@@ -74,7 +74,7 @@ static OCStackApplicationResult handlePublishCB(void *ctx,
 
 static void retreiveRDDetails(OCClientResponse *clientResponse, OCRDBiasFactorCB clientCB)
 {
-    OC_LOG_V(DEBUG, TAG, "\tAddress of the RD: %s : %d",  clientResponse->devAddr.addr,
+    OC_LOG_V(DEBUG, TAG, "\tAddress of the RD: %s:%d", clientResponse->devAddr.addr,
             clientResponse->devAddr.port);
 
     OCRDPayload *payload = (OCRDPayload *) clientResponse->payload;
@@ -84,8 +84,10 @@ static void retreiveRDDetails(OCClientResponse *clientResponse, OCRDBiasFactorCB
     // needs to cache here detail
     // and after certain timeout then decide based on the biasFactor.
     //if (biasFactor > 75)
-    clientCB(clientResponse->devAddr.addr, clientResponse->devAddr.port);
-
+    if (clientCB)
+    {
+        clientCB(clientResponse->devAddr.addr, clientResponse->devAddr.port);
+    }
 }
 
 static OCStackApplicationResult handleDiscoverCB(void *ctx,
@@ -94,7 +96,7 @@ static OCStackApplicationResult handleDiscoverCB(void *ctx,
     OC_LOG(DEBUG, TAG, "Found Resource Directory");
     OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION;
 
-    OCRDClientContextCB *cb = (OCRDClientContextCB *) ctx;
+    OCRDClientContextCB *cb = (OCRDClientContextCB *)ctx;
     if (!cb)
     {
         OC_LOG(ERROR, TAG, "RD Context Invalid Parameters.");
@@ -118,6 +120,8 @@ static OCStackApplicationResult handleDiscoverCB(void *ctx,
         OC_LOG(ERROR, TAG, "Discovery of RD Failed");
     }
 
+    OICFree(cb);
+
     return ret;
 }
 
@@ -135,13 +139,19 @@ OCStackResult OCRDDiscover(OCRDBiasFactorCB cbBiasFactor)
 
     OC_LOG_V(DEBUG, TAG, "Querying RD: %s\n", queryUri);
 
-    OCRDClientContextCB cbContext;
-    cbContext.context = (void *)DEFAULT_CONTEXT_VALUE;
-    cbContext.cbFunc = cbBiasFactor;
+    OCRDClientContextCB *cbContext = OICCalloc(1, sizeof(OCRDClientContextCB));
+    if (!cbContext)
+    {
+        OC_LOG(ERROR, TAG, "Failed allocating memory.");
+        return OC_STACK_NO_MEMORY;
+    }
+
+    cbContext->context = (void *)DEFAULT_CONTEXT_VALUE;
+    cbContext->cbFunc = cbBiasFactor;
 
     OCCallbackData cbData;
     cbData.cb = handleDiscoverCB;
-    cbData.context = (void *)(&cbContext);
+    cbData.context = (void *)(cbContext);
     cbData.cd = NULL;
 
     return sendRequest(OC_REST_DISCOVER, queryUri, NULL, NULL, cbData);
index cb05de73620bf5d58ff86a7b907c112b48f200e2..c025f788833b2b86fef2240439c5f2f664667137 100644 (file)
@@ -62,13 +62,12 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
 
     *size = MAX_REQUEST_LENGTH;
 
+    OCRDPayloadLog(DEBUG, TAG, rdPayload);
+
     CborEncoder encoder;
     int flags = 0;
     cbor_encoder_init(&encoder, *outPayload, *size, flags);
 
-    OC_LOG_V(DEBUG, TAG, "RD Payload : %d", rdPayload->base.type);
-    OC_LOG_V(DEBUG, TAG, "RD Payload Type: %d", rdPayload->payloadType);
-
     CborEncoder rootArray;
     CborError cborEncoderResult;
     cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, CBOR_ROOT_ARRAY_LENGTH);
@@ -125,7 +124,6 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
                 OC_LOG_V(ERROR, TAG, "Failed setting discovery sel value.");
                 goto exit;
             }
-            OC_LOG_V(DEBUG, TAG, "RD Payload bias factor: %d", rdPayload->rdDiscovery->sel);
         }
         else
         {
@@ -264,7 +262,6 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
     }
 
     *outPayload = tempPayload;
-
     return OC_STACK_OK;
 
 no_memory:
@@ -427,7 +424,7 @@ OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPay
                     goto exit;
                 }
 
-                OCRDLinksPayloadCreate(href, itf, rt, &links);
+                OCRDLinksPayloadCreate(href, rt, itf, &links);
                 if (!links)
                 {
                     goto no_memory;
@@ -451,14 +448,14 @@ OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPay
             {
                 goto no_memory;
             }
+        }
 
-            OCRDPayloadLog(DEBUG, TAG, rdPayload);
-            cborFindResult = cbor_value_advance(rdCBORPayload);
-            if (CborNoError != cborFindResult)
-            {
-                OC_LOG_V(ERROR, TAG, "Failed advancing the payload.");
-                goto exit;
-            }
+        OCRDPayloadLog(DEBUG, TAG, rdPayload);
+        cborFindResult = cbor_value_advance(rdCBORPayload);
+        if (CborNoError != cborFindResult)
+        {
+            OC_LOG_V(ERROR, TAG, "Failed advancing the payload.");
+            goto exit;
         }
         *outPayload = (OCPayload *)rdPayload;
     }
@@ -623,25 +620,25 @@ void OCRDPublishPayloadLog(LogLevel level, const char *tag, const OCRDPublishPay
 {
     if (rdPublish)
     {
-        if (payload->rdPublish->deviceName.deviceName)
+        if (rdPublish->deviceName.deviceName)
         {
-            OC_LOG_V(level, tag, "RD Payload Pulish Name : %s", payload->rdPublish->deviceName.deviceName);
+            OC_LOG_V(level, tag, "RD Payload Pulish Name : %s", rdPublish->deviceName.deviceName);
         }
 
-        if (payload->rdPublish->deviceId.id)
+        if (rdPublish->deviceId.id)
         {
-            OC_LOG_V(level, tag, "RD Payload Publish ID : %s",  payload->rdPublish->deviceId.id);
+            OC_LOG_V(level, tag, "RD Payload Publish ID : %s",  rdPublish->deviceId.id);
         }
 
         OC_LOG_V(level, tag, "RD Payload Publish TTL : %d", rdPublish->ttl);
 
         if (rdPublish->links)
         {
-            for (OCRDLinksPayload *temp = payload->rdPublish->links; temp; temp = temp->next)
+            for (OCRDLinksPayload *temp = rdPublish->links; temp; temp = temp->next)
             {
+                OC_LOG_V(level, tag, "RD Payload Publish Link HREF : %s", temp->href);
                 OC_LOG_V(level, tag, "RD Payload Publish Link RT : %s", temp->rt);
                 OC_LOG_V(level, tag, "RD Payload Publish Link ITF : %s", temp->itf);
-                OC_LOG_V(level, tag, "RD Payload Publish Link HREF : %s", temp->href);
             }
         }
     }
index 228f59c1d72d9e7967a50bd40b4431bba244f69e..b95def34521bec499f6d8a5a6e48bb77faf7d263 100644 (file)
@@ -36,7 +36,7 @@ static OCStackResult sendResponse(const OCEntityHandlerRequest *ehRequest, OCRDP
     response.requestHandle = ehRequest->requestHandle;
     response.resourceHandle = ehRequest->resource;
     response.ehResult = OC_EH_OK;
-    response.payload = (OCPayload*)(&rdPayload);
+    response.payload = (OCPayload*)(rdPayload);
     response.payload->type = PAYLOAD_TYPE_RD;
 
     return OCDoResponse(&response);
@@ -105,16 +105,6 @@ static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *
         OCRDStorePublishedResources(payload->rdPublish);
     }
 
-    OC_LOG_V(DEBUG, TAG, "Sending success response");
-    OCRDPayload *rdPayload = OCRDPayloadCreate(RD_PAYLOAD_TYPE_PUBLISH);
-    if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK)
-    {
-        OC_LOG(ERROR, TAG, "Sending response failed.");
-        ehResult = OC_EH_ERROR;
-    }
-
-    OCRDPayloadDestroy(rdPayload);
-
     return ehResult;
 }