RD Client functionalty
authorHabib Virji <habib.virji@samsung.com>
Mon, 10 Aug 2015 14:44:25 +0000 (15:44 +0100)
committerMadan Lanka <lanka.madan@samsung.com>
Wed, 12 Aug 2015 10:01:59 +0000 (10:01 +0000)
Includes functionality too find the RD server.

Change-Id: I9e7bd8073adec899c1def72e64813e40643c233a
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2159
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-directory/SConscript
service/resource-directory/include/rd_client.h [new file with mode: 0644]
service/resource-directory/include/rd_payload.h
service/resource-directory/include/rd_types.h
service/resource-directory/src/rd_client.c [new file with mode: 0644]

index a710a0b..2671d3f 100755 (executable)
@@ -66,6 +66,7 @@ RD_SRC_DIR = 'src/'
 rd_src = [
                RD_SRC_DIR + 'rd_server.c',
                RD_SRC_DIR + 'rd_payload.c',
+               RD_SRC_DIR + 'rd_client.c',
         ]
 
 if target_os in ['tizen'] :
diff --git a/service/resource-directory/include/rd_client.h b/service/resource-directory/include/rd_client.h
new file mode 100644 (file)
index 0000000..e6c1db3
--- /dev/null
@@ -0,0 +1,48 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef _RESOURCE_DIRECTORY_CLIENT_H_
+#define _RESOURCE_DIRECTORY_CLIENT_H_
+
+// Iotivity Base CAPI
+#include "ocstack.h"
+
+#include "rd_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+/**
+ * Discovers the resource directory.
+ * This function searches a RD server and obtain the bias factor.
+ *
+ * @param cbBiasFactor callback function invoked when bias factor is returned by
+ *                     the Resource Directory Server
+ *
+ * @return ::OC_STACK_OK upon success, ::OC_STACK_ERROR in case of error.
+ */
+OCStackResult OCRDDiscover(OCRDBiasFactorCB cbBiasFactor);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif //_RESOURCE_DIRECTORY_CLIENT_H_
index 924e446..da219fe 100644 (file)
@@ -46,7 +46,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 a43beff..1c03479 100644 (file)
@@ -32,8 +32,24 @@ extern "C" {
 /** To represent resource type with rd.*/
 #define OC_RSRVD_RESOURCE_TYPE_RD        "oic.wk.rd"
 
+/** RD Discovery bias factor type. */
 #define OC_RSRVD_RD_DISCOVERY_SEL        "sel"
 
+/** Max ADDR SIZE */
+#define MAX_ADDR_STR_SIZE (40)
+
+/** Callback function for returning RDDiscovery Result. */
+typedef int (* OCRDBiasFactorCB)(char addr[MAX_ADDR_STR_SIZE], uint16_t port);
+
+/** Context structure used sending it as part of the callback context. */
+typedef struct
+{
+    /** Stores the context value of the message sent. */
+    void *context;
+    /** Pointing to the callback function that OCRDDiscover() received. */
+    OCRDBiasFactorCB cbFunc;
+} OCRDClientContextCB;
+
 /**
  * Structure holding discovery payload.
  */
diff --git a/service/resource-directory/src/rd_client.c b/service/resource-directory/src/rd_client.c
new file mode 100644 (file)
index 0000000..630d88a
--- /dev/null
@@ -0,0 +1,130 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include "logger.h"
+#include "oic_malloc.h"
+
+#include "rd_client.h"
+#include "rd_payload.h"
+
+#define DEFAULT_CONTEXT_VALUE 0x99
+
+#define TAG  PCF("RDClient")
+
+static void retreiveRDDetails(OCClientResponse *clientResponse, OCRDBiasFactorCB clientCB)
+{
+    OC_LOG_V(DEBUG, TAG, "\tAddress: %s : %d",  clientResponse->devAddr.addr,
+             clientResponse->devAddr.port);
+
+    OCRDPayload *payload = (OCRDPayload *) clientResponse->payload;
+    if (payload)
+    {
+        OC_LOG_V(DEBUG, TAG, "\tType: %d",  payload->base.type);
+        OC_LOG_V(DEBUG, TAG, "\tPayloadType: %d",  payload->payloadType);
+        OC_LOG_V(DEBUG, TAG, "\tBiasFactor: %d",  payload->rdDiscovery->sel);
+    }
+    // TODO: Multiple Resource Directory will have different biasFactor,
+    // 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);
+
+}
+
+static OCStackApplicationResult discoverRDCB(void *ctx,
+        OCDoHandle handle, OCClientResponse *clientResponse)
+{
+    OC_LOG(DEBUG, TAG, "Found Resource Directory");
+    OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION;
+
+    OCRDClientContextCB *cb = (OCRDClientContextCB*) ctx;
+    if (!cb)
+    {
+        OC_LOG(ERROR, TAG, "RD Context Invalid Parameters.");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    if (cb->context != (void *) DEFAULT_CONTEXT_VALUE)
+    {
+        OC_LOG(ERROR, TAG, "RD Context Invalid Context Value Parameters.");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    OC_LOG(DEBUG, TAG, "Callback Context for DISCOVER query received successfully.");
+
+    if (clientResponse && clientResponse->result == OC_STACK_OK)
+    {
+        retreiveRDDetails(clientResponse, cb->cbFunc);
+    }
+    else
+    {
+        OC_LOG(ERROR, TAG, "Discovery of RD Failed");
+    }
+
+    return ret;
+}
+
+OCStackResult OCRDDiscover(OCRDBiasFactorCB cbBiasFactor)
+{
+    OCStackResult result = OC_STACK_ERROR;
+    OCCallbackData cbData;
+    OCDoHandle handle;
+
+    if (!cbBiasFactor)
+    {
+        OC_LOG(DEBUG, TAG, "No callback function specified.");
+        return OC_STACK_INVALID_CALLBACK;
+    }
+
+    /* Start a discovery query*/
+    char queryUri[MAX_URI_LENGTH] = { '\0' };
+    snprintf(queryUri, MAX_URI_LENGTH, "coap://%s%s", OC_MULTICAST_PREFIX, OC_RSRVD_RD_URI);
+
+    OC_LOG_V(DEBUG, TAG, "Querying RD: %s\n", queryUri);
+
+    OCRDClientContextCB cbContext;
+    cbContext.context = (void *)DEFAULT_CONTEXT_VALUE;
+    cbContext.cbFunc = cbBiasFactor;
+
+    cbData.cb = discoverRDCB;
+    cbData.context = (void *)(&cbContext);
+    cbData.cd = NULL;
+
+    result = OCDoResource(&handle,
+                          OC_REST_DISCOVER,
+                          queryUri,
+                          0,
+                          0,
+                          CT_ADAPTER_IP,
+                          OC_LOW_QOS,
+                          &cbData,
+                          NULL,
+                          0);
+
+    if (result == OC_STACK_OK)
+    {
+        OC_LOG_V(DEBUG, TAG, "Resource Directory finding query send successful...");
+    }
+    else
+    {
+        OC_LOG_V(ERROR, TAG, "Resource Directory finding query send failed...");
+    }
+
+    return result;
+}