//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include "iotivity_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+/** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */
+#ifdef ERROR
+#undef ERROR
+#endif
+#endif
#include <iostream>
#include <sstream>
+#include <getopt.h>
#include "ocstack.h"
#include "logger.h"
#include "occlientbasicops.h"
-#include "cJSON.h"
+#include "ocpayload.h"
+#include "payload_logging.h"
+#include "oic_string.h"
#include "common.h"
#define TAG "occlientbasicops"
-static int UNICAST_DISCOVERY = 0;
-static int TEST_CASE = 0;
+static int UnicastDiscovery = 0;
+static int TestCase = 0;
+static int ConnType = 0;
+static int DevOwner = 0;
+static int WithTcp = 0;
-static int IPV4_ADDR_SIZE = 16;
-static char UNICAST_DISCOVERY_QUERY[] = "coap://%s:6298/oic/res";
-static char MULTICAST_DISCOVERY_QUERY[] = "/oic/res";
+static char DISCOVERY_QUERY[] = "%s/oic/res";
+OCConnectivityType discoveryReqConnType = CT_ADAPTER_IP;
+static OCDevAddr endpoint;
-static std::string putPayload = "{\"oic\":[{\"rep\":{\"state\":\"off\",\"power\":10}}]}";
-static std::string coapServerIP;
-static std::string coapServerPort;
static std::string coapServerResource;
static int coapSecureResource;
static OCConnectivityType ocConnType;
-
//Secure Virtual Resource database for Iotivity Client application
//It contains Client's Identity and the PSK credentials
//of other devices which the client trusts
-static char CRED_FILE[] = "oic_svr_db_client.json";
-
+static char CRED_FILE_DEVOWNER[] = "oic_svr_db_client_devowner.dat";
+static char CRED_FILE_NONDEVOWNER[] = "oic_svr_db_client_nondevowner.dat";
+const char * OIC_RSRC_DOXM_URI = "/oic/sec/doxm";
+const char * OIC_RSRC_PSTAT_URI = "/oic/sec/pstat";
int gQuitFlag = 0;
}
}
+OCPayload* putPayload()
+{
+ OCRepPayload* payload = OCRepPayloadCreate();
+
+ if(!payload)
+ {
+ std::cout << "Failed to create put payload object"<<std::endl;
+ std::exit(1);
+ }
+
+ OCRepPayloadSetPropInt(payload, "power", 15);
+ OCRepPayloadSetPropBool(payload, "state", true);
+
+ return (OCPayload*) payload;
+}
+
static void PrintUsage()
{
- OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3>");
- OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
- OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
- OC_LOG(INFO, TAG, "-t 2 : Discover Resources and"
+ OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3> -c <0|1>");
+ OIC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
+ OIC_LOG(INFO, TAG, "-t 1 : Discover Resources");
+ OIC_LOG(INFO, TAG, "-t 2 : Discover Resources and"
" Initiate Nonconfirmable Get/Put/Post Requests");
- OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get/Put/Post Requests");
+ OIC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get/Put/Post Requests");
+ OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
+ OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
+ OIC_LOG(INFO, TAG, "-d 0 : Client as Non Device Owner");
+ OIC_LOG(INFO, TAG, "-d 1 : Client as Device Owner");
+ OIC_LOG(INFO, TAG, "-p 0 : Use UDP protocol");
+ OIC_LOG(INFO, TAG, "-p 1 : Use TCP protocol");
}
OCStackResult InvokeOCDoResource(std::ostringstream &query,
- OCMethod method, OCQualityOfService qos,
- OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
+ OCMethod method,
+ const OCDevAddr *dest,
+ OCQualityOfService qos,
+ OCClientResponseHandler cb,
+ OCHeaderOption * options, uint8_t numOptions)
{
OCStackResult ret;
OCCallbackData cbData;
cbData.context = NULL;
cbData.cd = NULL;
- ret = OCDoResource(NULL, method, query.str().c_str(), 0,
- (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload.c_str() : NULL,
- ocConnType, qos, &cbData, options, numOptions);
+ OCPayload* payload = (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload() : NULL;
+
+ ret = OCDoRequest(NULL, method, query.str().c_str(), dest,
+ payload, ocConnType, qos, &cbData, options, numOptions);
+
+ OCPayloadDestroy(payload);
if (ret != OC_STACK_OK)
{
- OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
+ OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
}
return ret;
}
-OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+OCStackApplicationResult putReqCB(void*, OCDoHandle, OCClientResponse * clientResponse)
{
- OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
+ OIC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
if(clientResponse)
{
- OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
+ OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+ OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OIC_LOG(INFO, TAG, "=============> Put Response");
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
+OCStackApplicationResult postReqCB(void *, OCDoHandle, OCClientResponse *clientResponse)
{
- OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
+ OIC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
if(clientResponse)
{
- OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
- clientResponse->resJSONPayload);
+ OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+ OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OIC_LOG(INFO, TAG, "=============> Post Response");
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+OCStackApplicationResult getReqCB(void*, OCDoHandle, OCClientResponse * clientResponse)
{
- OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
+ OIC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
if(clientResponse)
{
- OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
- OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
- OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
- clientResponse->resJSONPayload);
+ OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+ OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
+ OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
+ OIC_LOG(INFO, TAG, "=============> Get Response");
}
return OC_STACK_DELETE_TRANSACTION;
}
// This is a function called back when a device is discovered
-OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
+OCStackApplicationResult discoveryReqCB(void*, OCDoHandle,
OCClientResponse * clientResponse)
{
- uint8_t remoteIpAddr[4];
- uint16_t remotePortNu;
-
- OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
+ OIC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
if (clientResponse)
{
- OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
-
- OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
- remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
- OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
+ OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+ OIC_LOG_V(INFO, TAG,
+ "Device =============> Discovered @ %s:%d",
+ clientResponse->devAddr.addr,
+ clientResponse->devAddr.port);
- OC_LOG_V(INFO, TAG,
- "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
- clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
- remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
+ if (clientResponse->result == OC_STACK_OK)
+ {
+ OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
- ocConnType = clientResponse->connType;
+ ocConnType = clientResponse->connType;
+ endpoint = clientResponse->devAddr;
- if (parseClientResponse(clientResponse) != -1)
- {
- switch(TEST_CASE)
+ if (parseClientResponse(clientResponse) != -1)
{
- case TEST_NON_CON_OP:
- InitGetRequest(OC_LOW_QOS);
- InitPutRequest(OC_LOW_QOS);
- //InitPostRequest(OC_LOW_QOS);
- break;
- case TEST_CON_OP:
- InitGetRequest(OC_HIGH_QOS);
- InitPutRequest(OC_HIGH_QOS);
- //InitPostRequest(OC_HIGH_QOS);
- break;
+ switch(TestCase)
+ {
+ case TEST_NON_CON_OP:
+ InitGetRequest(OC_LOW_QOS);
+ InitPutRequest(OC_LOW_QOS);
+ InitPostRequest(OC_LOW_QOS);
+ break;
+ case TEST_CON_OP:
+ InitGetRequest(OC_HIGH_QOS);
+ InitPutRequest(OC_HIGH_QOS);
+ InitPostRequest(OC_HIGH_QOS);
+ break;
+ }
}
}
}
- return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
+ return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
}
-
int InitPutRequest(OCQualityOfService qos)
{
- OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+ OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
std::ostringstream query;
- query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
- << ":" << coapServerPort << coapServerResource;
- return (InvokeOCDoResource(query, OC_REST_PUT,
+ query << coapServerResource;
+ if(WithTcp)
+ {
+ endpoint.adapter = OC_ADAPTER_TCP;
+ }
+ endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
+ return (InvokeOCDoResource(query, OC_REST_PUT, &endpoint,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS), putReqCB, NULL, 0));
}
int InitPostRequest(OCQualityOfService qos)
{
OCStackResult result;
- OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+
+ OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
std::ostringstream query;
- query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
- << ":" << coapServerPort << coapServerResource;
+ query << coapServerResource;
+ if(WithTcp)
+ {
+ endpoint.adapter = OC_ADAPTER_TCP;
+ }
+ endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
// First POST operation (to create an LED instance)
- result = InvokeOCDoResource(query, OC_REST_POST,
+ result = InvokeOCDoResource(query, OC_REST_POST, &endpoint,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
postReqCB, NULL, 0);
if (OC_STACK_OK != result)
{
// Error can happen if for example, network connectivity is down
- OC_LOG(INFO, TAG, "First POST call did not succeed");
+ OIC_LOG(INFO, TAG, "First POST call did not succeed");
}
// Second POST operation (to create an LED instance)
- result = InvokeOCDoResource(query, OC_REST_POST,
+ result = InvokeOCDoResource(query, OC_REST_POST, &endpoint,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
postReqCB, NULL, 0);
if (OC_STACK_OK != result)
{
- OC_LOG(INFO, TAG, "Second POST call did not succeed");
+ OIC_LOG(INFO, TAG, "Second POST call did not succeed");
}
- // This POST operation will update the original resourced /a/led
- return (InvokeOCDoResource(query, OC_REST_POST,
- ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
- postReqCB, NULL, 0));
+ // This POST operation will update the original resourced /a/led (as long as
+ // the server is set to max 2 /lcd resources)
+ result = InvokeOCDoResource(query, OC_REST_POST, &endpoint,
+ ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
+ postReqCB, NULL, 0);
+ if (OC_STACK_OK != result)
+ {
+ OIC_LOG(INFO, TAG, "Third POST call did not succeed");
+ }
+ return result;
}
int InitGetRequest(OCQualityOfService qos)
{
- OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+ OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
std::ostringstream query;
- query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
- << ":" << coapServerPort << coapServerResource;
+ query << coapServerResource;
+ if(WithTcp)
+ {
+ endpoint.adapter = OC_ADAPTER_TCP;
+ }
+ endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
- return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
- OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
+ return (InvokeOCDoResource(query, OC_REST_GET, &endpoint,
+ ((qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS),
+ getReqCB, NULL, 0));
}
int InitDiscovery()
{
OCStackResult ret;
OCCallbackData cbData;
- char szQueryUri[MAX_URI_LENGTH] = { 0 };
- OCConnectivityType discoveryReqConnType;
+ char queryUri[200];
+ char ipaddr[100] = { '\0' };
- if (UNICAST_DISCOVERY)
+ if (UnicastDiscovery)
{
- char ipv4addr[IPV4_ADDR_SIZE];
- printf("Enter IPv4 address of the Server hosting secure resource (Ex: 11.12.13.14)\n");
- if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
+ OIC_LOG(INFO, TAG, "Enter IP address (with optional port) of the Server hosting resource\n");
+ OIC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
+ OIC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
+
+ if (fgets(ipaddr, sizeof (ipaddr), stdin))
{
- //Strip newline char from ipv4addr
- StripNewLineChar(ipv4addr);
- snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
+ StripNewLineChar(ipaddr); //Strip newline char from ipaddr
}
else
{
- OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
+ OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
return OC_STACK_INVALID_PARAM;
}
- discoveryReqConnType = OC_IPV4;
- }
- else
- {
- //Send discovery request on Wifi and Ethernet interface
- discoveryReqConnType = OC_ALL;
- strcpy(szQueryUri, MULTICAST_DISCOVERY_QUERY);
}
+ snprintf(queryUri, sizeof (queryUri), DISCOVERY_QUERY, ipaddr);
cbData.cb = discoveryReqCB;
cbData.context = NULL;
cbData.cd = NULL;
/* Start a discovery query*/
- OC_LOG_V(INFO, TAG, "Initiating %s Resource Discovery : %s\n",
- (UNICAST_DISCOVERY) ? "Unicast" : "Multicast",
- szQueryUri);
+ OIC_LOG_V(INFO, TAG, "Initiating %s Resource Discovery : %s\n",
+ (UnicastDiscovery) ? "Unicast" : "Multicast",
+ queryUri);
- ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0,
- discoveryReqConnType, OC_LOW_QOS,
- &cbData, NULL, 0);
+ ret = OCDoRequest(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT,
+ OC_LOW_QOS, &cbData, NULL, 0);
if (ret != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
return ret;
}
-FILE* client_fopen(const char *path, const char *mode)
+FILE* client_fopen_devowner(const char *path, const char *mode)
{
(void)path;
- return fopen(CRED_FILE, mode);
+ return fopen(CRED_FILE_DEVOWNER, mode);
}
+FILE* client_fopen_nondevowner(const char *path, const char *mode)
+{
+ (void)path;
+ return fopen(CRED_FILE_NONDEVOWNER, mode);
+}
int main(int argc, char* argv[])
{
int opt;
struct timespec timeout;
+ OCPersistentStorage ps;
- while ((opt = getopt(argc, argv, "u:t:")) != -1)
+ while ((opt = getopt(argc, argv, "u:t:c:d:p:")) != -1)
{
switch(opt)
{
case 'u':
- UNICAST_DISCOVERY = atoi(optarg);
+ UnicastDiscovery = atoi(optarg);
break;
case 't':
- TEST_CASE = atoi(optarg);
+ TestCase = atoi(optarg);
+ break;
+ case 'c':
+ ConnType = atoi(optarg);
+ break;
+ case 'd':
+ DevOwner = atoi(optarg);
+ break;
+ case 'p':
+ {
+ WithTcp = atoi(optarg);
+ if(WithTcp > 1)
+ {
+ PrintUsage();
+ return -1;
+ }
+ }
break;
default:
PrintUsage();
}
}
- if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
- (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
+ if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
+ (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS)||
+ (ConnType < CT_ADAPTER_DEFAULT || ConnType >= MAX_CT))
{
PrintUsage();
return -1;
}
+
+ if(ConnType == CT_ADAPTER_DEFAULT || ConnType == CT_IP)
+ {
+ discoveryReqConnType = CT_DEFAULT;
+ }
+ else
+ {
+ OIC_LOG(INFO, TAG, "Using Default Connectivity type");
+ PrintUsage();
+ }
+
+
// Initialize Persistent Storage for SVR database
- OCPersistentStorage ps = {};
- ps.open = client_fopen;
- ps.read = fread;
- ps.write = fwrite;
- ps.close = fclose;
- ps.unlink = unlink;
+ if (DevOwner)
+ ps = { client_fopen_devowner, fread, fwrite, fclose, unlink };
+ else
+ ps = { client_fopen_nondevowner, fread, fwrite, fclose, unlink };
OCRegisterPersistentStorageHandler(&ps);
/* Initialize OCStack*/
if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack init error");
+ OIC_LOG(ERROR, TAG, "OCStack init error");
return 0;
}
timeout.tv_nsec = 100000000L;
// Break from loop with Ctrl+C
- OC_LOG(INFO, TAG, "Entering occlient main loop...");
+ OIC_LOG(INFO, TAG, "Entering occlient main loop...");
signal(SIGINT, handleSigInt);
while (!gQuitFlag)
{
if (OCProcess() != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack process error");
+ OIC_LOG(ERROR, TAG, "OCStack process error");
return 0;
}
nanosleep(&timeout, NULL);
}
- OC_LOG(INFO, TAG, "Exiting occlient main loop...");
+ OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
if (OCStop() != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack stop error");
+ OIC_LOG(ERROR, TAG, "OCStack stop error");
}
return 0;
}
-std::string getIPAddrTBServer(OCClientResponse * clientResponse)
-{
- if(!clientResponse) return "";
- if(!clientResponse->addr) return "";
- uint8_t a, b, c, d = 0;
- if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
-
- char ipaddr[16] = {'\0'};
- // ostringstream not working correctly here, hence snprintf
- snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
- return std::string (ipaddr);
-}
-
-
-std::string getPortTBServer(OCClientResponse * clientResponse)
-{
- if(!clientResponse) return "";
- if(!clientResponse->addr) return "";
- uint16_t p = 0;
- if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
- std::ostringstream ss;
- ss << p;
- return ss.str();
-}
-
int parseClientResponse(OCClientResponse * clientResponse)
{
- int port = -1;
- cJSON * root = NULL;
- cJSON * oc = NULL;
+ OCResourcePayload* res = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
// Initialize all global variables
coapServerResource.clear();
- coapServerPort.clear();
- coapServerIP.clear();
coapSecureResource = 0;
- root = cJSON_Parse((char *)(clientResponse->resJSONPayload));
- if (!root)
+ while (res)
{
- return -1;
- }
-
- oc = cJSON_GetObjectItem(root,"oic");
- if (!oc)
- {
- return -1;
- }
-
- if (oc->type == cJSON_Array)
- {
- int numRsrcs = cJSON_GetArraySize(oc);
- for(int i = 0; i < numRsrcs; i++)
+ coapServerResource.assign(res->uri);
+ OIC_LOG_V(INFO, TAG, "Uri -- %s", coapServerResource.c_str());
+ if (0 == strcmp(coapServerResource.c_str(),OIC_RSRC_DOXM_URI))
{
- cJSON * resource = cJSON_GetArrayItem(oc, i);
- if (cJSON_GetObjectItem(resource, "href"))
+ OIC_LOG(INFO,TAG,"Skip: doxm is secure virtual resource");
+ res = res->next;
+ continue;
+ }
+ if (0 == strcmp(coapServerResource.c_str(),OIC_RSRC_PSTAT_URI))
+ {
+ OIC_LOG(INFO,TAG,"Skip: pstat is secure virtual resource");
+ res = res->next;
+ continue;
+ }
+ if (res->secure)
+ {
+ if(WithTcp)
{
- coapServerResource.assign(cJSON_GetObjectItem(resource, "href")->valuestring);
+#ifdef TCP_ADAPTER
+ OIC_LOG_V(INFO,TAG,"SECUREPORT tcp: %d",res->tcpPort);
+ endpoint.port = res->tcpPort;
+#endif
}
else
{
- coapServerResource = "";
+ OIC_LOG_V(INFO,TAG,"SECUREPORT udp: %d",res->port);
+ endpoint.port = res->port;
}
- OC_LOG_V(INFO, TAG, "Uri -- %s", coapServerResource.c_str());
+ coapSecureResource = 1;
+ }
- cJSON * prop = cJSON_GetObjectItem(resource,"prop");
- if (prop)
- {
- cJSON * policy = cJSON_GetObjectItem(prop,"p");
- if (policy)
- {
- // If this is a secure resource, the info about the port at which the
- // resource is hosted on server is embedded inside discovery JSON response
- if (cJSON_GetObjectItem(policy, "sec"))
- {
- if ((cJSON_GetObjectItem(policy, "sec")->valueint) == 1)
- {
- coapSecureResource = 1;
- }
- }
- OC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO");
- if (cJSON_GetObjectItem(policy, "port"))
- {
- port = cJSON_GetObjectItem(policy, "port")->valueint;
- OC_LOG_V(INFO, TAG, "Hosting Server Port (embedded inside JSON) -- %u", port);
-
- std::ostringstream ss;
- ss << port;
- coapServerPort = ss.str();
- }
- }
- }
+ OIC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO");
- // If we discovered a secure resource, exit from here
- if (coapSecureResource)
- {
- break;
- }
+ // If we discovered a secure resource, exit from here
+ if (coapSecureResource)
+ {
+ break;
}
- }
- cJSON_Delete(root);
- coapServerIP = getIPAddrTBServer(clientResponse);
- if (port == -1)
- {
- coapServerPort = getPortTBServer(clientResponse);
- OC_LOG_V(INFO, TAG, "Hosting Server Port -- %s", coapServerPort.c_str());
+ res = res->next;
}
+
return 0;
}