//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include "iotivity_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
-
-#include <set>
-#include <string>
+#endif
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+#include <stdint.h>
#include <sstream>
#include <iostream>
-#include <algorithm>
+#include <getopt.h>
#include "ocstack.h"
#include "logger.h"
#include "occlientbasicops.h"
+#include "ocpayload.h"
+#include "payload_logging.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
+#include "common.h"
-static int UNICAST_DISCOVERY = 0;
-static int TEST_CASE = 0;
-
-static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
+#define MAX_IP_ADDR_ST_SZ 16 //string size of "155.255.255.255" (15 + 1)
+#define MAX_PORT_ST_SZ 6 //string size of "65535" (5 + 1)
-static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
+static int UnicastDiscovery = 0;
+static int TestCase = 0;
+static int Connectivity = 0;
-static std::string coapServerIP = "255.255.255.255";
-static std::string coapServerPort = "5683";
-static std::string coapServerResource = "/a/led";
-
-#ifdef CA_INT
-//The following variable determines the interface (wifi, ethernet etc.)
-//to be used for sending unicast messages. Default set to WIFI.
-static OCConnectivityType CA_CONNTYPE = OC_WIFI;
-static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
-#endif
+//The following variable determines the interface protocol (IP, etc)
+//to be used for sending unicast messages. Default set to IP.
+static OCConnectivityType ConnType = CT_ADAPTER_IP;
+static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
int gQuitFlag = 0;
-namespace {
- typedef std::pair<bool, std::string> extract_result_t;
- typedef std::string sid_t;
- typedef std::set<std::string> SID_set_t;
-} // namespace
-
-void collateSIDs(const OCClientResponse * clientResponse, const std::string& server_ip);
-
-/* SIGINT handler: set gQuitFlag to 1 for graceful termination */
+struct ResourceNode *resourceList;
+/*
+ * SIGINT handler: set gQuitFlag to 1 for graceful termination
+ * */
void handleSigInt(int signum)
{
if (signum == SIGINT)
}
}
+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()
{
-#ifdef CA_INT
- OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3> -c <0|1|2|3>");
-#else
- OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3>");
-#endif
- OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
-#ifdef CA_INT
- OC_LOG(INFO, TAG, "-c <0|1|2|3> : Send unicast messages over Ethernet, WIFI, EDR or LE");
-#endif
- 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");
+}
+
+/*
+ * Returns the first resource in the list
+ */
+const ResourceNode * getResource()
+{
+ return resourceList;
}
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;
- OCDoHandle handle;
cbData.cb = cb;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
-#ifdef CA_INT
- ret = OCDoResource(&handle, method, query.str().c_str(), 0,
- (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload.c_str() : NULL,
- CA_CONNTYPE, qos, &cbData, options, numOptions);
-#else
- ret = OCDoResource(&handle, method, query.str().c_str(), 0,
- (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload.c_str() : NULL,
- qos, &cbData, options, numOptions);
-#endif
+ OCPayload* payload = (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload() : NULL;
+
+ ret = OCDoRequest(NULL, method, query.str().c_str(), dest,
+ payload, CT_DEFAULT, 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* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
{
if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
- OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
+ OIC_LOG(INFO, TAG, "<====Callback Context for PUT received successfully====>");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "<====Callback Context for PUT fail====>");
}
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_PAYLOAD(INFO, clientResponse->payload);
+ OIC_LOG(INFO, TAG, ("=============> Put Response"));
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "<====PUT Callback fail to receive clientResponse====>\n");
}
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
+OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
{
if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
- OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
+ OIC_LOG(INFO, TAG, "<====Callback Context for POST received successfully====>");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "<====Callback Context for POST fail====>");
}
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_PAYLOAD(INFO, clientResponse->payload);
+ OIC_LOG(INFO, TAG, ("=============> Post Response"));
}
+ else
+ {
+ OIC_LOG(ERROR, TAG, "<====POST Callback fail to receive clientResponse====>\n");
+ }
+
return OC_STACK_DELETE_TRANSACTION;
}
-OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
{
- if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
+ if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
{
- OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
+ OIC_LOG(INFO, TAG, "<====Callback Context for GET received successfully====>");
}
-
- if(clientResponse)
+ else
{
- 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(ERROR, TAG, "<====Callback Context for GET fail====>");
}
- if(clientResponse->rcvdVendorSpecificHeaderOptions &&
- clientResponse->numRcvdVendorSpecificHeaderOptions)
+
+ if (clientResponse)
{
- OC_LOG (INFO, TAG, "Received vendor specific options");
- uint8_t i = 0;
- OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
- for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
+ 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"));
+
+ if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0 )
{
- if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
+ OIC_LOG (INFO, TAG, "Received vendor specific options");
+ uint8_t i = 0;
+ OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
+ for (i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
{
- OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
- ((OCHeaderOption)rcvdOptions[i]).optionID );
- OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
- ((OCHeaderOption)rcvdOptions[i]).optionLength);
+ if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID)
+ {
+ OIC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
+ ((OCHeaderOption)rcvdOptions[i]).optionID );
+
+ OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
+ MAX_HEADER_OPTION_DATA_LENGTH);
+ }
}
}
}
+ else
+ {
+ OIC_LOG(ERROR, TAG, "<====GET Callback fail to receive clientResponse====>\n");
+ }
return OC_STACK_DELETE_TRANSACTION;
}
-// This is a function called back when a device is discovered
-OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
- OCClientResponse * clientResponse)
+/*
+ * This is a function called back when a device is discovered
+ */
+OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
+ OCClientResponse * clientResponse)
{
- uint8_t remoteIpAddr[4];
- uint16_t remotePortNu;
-
if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
{
- OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
+ OIC_LOG(INFO, TAG, "\n<====Callback Context for DISCOVERY query "
+ "received successfully====>");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "\n<====Callback Context for DISCOVERY fail====>");
}
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,
+ "Device =============> Discovered @ %s:%d",
+ clientResponse->devAddr.addr,
+ clientResponse->devAddr.port);
+ OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
- OC_LOG_V(INFO, TAG,
- "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
- clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
- remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
-
- parseClientResponse(clientResponse);
-
- collateSIDs(clientResponse, getIPAddrTBServer(clientResponse));
-
- switch(TEST_CASE)
- {
- case TEST_NON_CON_OP:
- InitGetRequest(OC_LOW_QOS);
- InitPutRequest();
- InitPostRequest(OC_LOW_QOS);
- break;
- case TEST_CON_OP:
- InitGetRequest(OC_HIGH_QOS);
- InitPutRequest();
- InitPostRequest(OC_HIGH_QOS);
- break;
- default:
- PrintUsage();
- break;
- }
+ collectUniqueResource(clientResponse);
}
-
- return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
+ else
+ {
+ OIC_LOG(ERROR, TAG, "<====DISCOVERY Callback fail to receive clientResponse====>\n");
+ }
+ return (UnicastDiscovery) ?
+ OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
}
-int InitPutRequest()
+int InitPutRequest(OCQualityOfService qos)
{
- OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
- return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
+ //Get most recently inserted resource
+ const ResourceNode * resource = getResource();
+
+ if(!resource)
+ {
+ OIC_LOG_V(ERROR, TAG, "Resource null, can't do PUT request\n");
+ return -1;
+ }
+ query << resource->uri;
+ OIC_LOG_V(INFO, TAG,"Executing InitPutRequest, Query: %s", query.str().c_str());
+
+ return (InvokeOCDoResource(query, OC_REST_PUT, &resource->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__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ //Get most recently inserted resource
+ const ResourceNode *resource = getResource();
+
+ if(!resource)
+ {
+ OIC_LOG_V(ERROR, TAG, "Resource null, can't do POST request\n");
+ return -1;
+ }
+
+ query << resource->uri;
+ OIC_LOG_V(INFO, TAG,"Executing InitPostRequest, Query: %s", query.str().c_str());
// First POST operation (to create an LED instance)
- result = InvokeOCDoResource(query, OC_REST_POST,
+ result = InvokeOCDoResource(query, OC_REST_POST, &resource->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(ERROR, 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, &resource->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(ERROR, TAG, "Second POST call did not succeed");
}
// This POST operation will update the original resourced /a/led
- return (InvokeOCDoResource(query, OC_REST_POST,
+ return (InvokeOCDoResource(query, OC_REST_POST, &resource->endpoint,
((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
postReqCB, NULL, 0));
}
int InitGetRequest(OCQualityOfService qos)
{
- OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
std::ostringstream query;
- query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
+ //Get most recently inserted resource
+ const ResourceNode * resource = getResource();
- return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
- OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
+ if(!resource)
+ {
+ OIC_LOG_V(ERROR, TAG, "Resource null, can't do GET request\n");
+ return -1;
+ }
+ query << resource->uri;
+ OIC_LOG_V(INFO, TAG,"Executing InitGetRequest, Query: %s", query.str().c_str());
+
+ return (InvokeOCDoResource(query, OC_REST_GET, &resource->endpoint,
+ (qos == OC_HIGH_QOS)?OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
}
int InitDiscovery()
{
OCStackResult ret;
OCCallbackData cbData;
- OCDoHandle handle;
- /* Start a discovery query*/
- char szQueryUri[64] = { 0 };
- if (UNICAST_DISCOVERY)
- {
- strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
- }
- else
+ char queryUri[200];
+ char ipaddr[100] = { '\0' };
+
+ if (UnicastDiscovery)
{
-#ifdef CA_INT
- strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
-#else
- strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
-#endif
+ 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))
+ {
+ StripNewLineChar(ipaddr); //Strip newline char from ipaddr
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
+ return OC_STACK_INVALID_PARAM;
+ }
}
+
+ snprintf(queryUri, sizeof (queryUri), RESOURCE_DISCOVERY_QUERY, ipaddr);
+
cbData.cb = discoveryReqCB;
cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
-#ifdef CA_INT
- if (UNICAST_DISCOVERY)
+
+ ret = OCDoRequest(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT,
+ OC_LOW_QOS, &cbData, NULL, 0);
+ if (ret != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
+ }
+ return ret;
+}
+
+void queryResource()
+{
+ switch(TestCase)
+ {
+ case TEST_DISCOVER_REQ:
+ break;
+ 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;
+ default:
+ PrintUsage();
+ break;
+ }
+}
+
+
+void collectUniqueResource(const OCClientResponse * clientResponse)
+{
+ OCDiscoveryPayload* pay = (OCDiscoveryPayload*) clientResponse->payload;
+ OCResourcePayload* res = pay->resources;
+
+ // Including the NUL terminator, length of UUID string of the form:
+ // "a62389f7-afde-00b6-cd3e-12b97d2fcf09"
+# define UUID_LENGTH 37
+
+ char sidStr[UUID_LENGTH];
+
+ while(res) {
+
+ int ret = snprintf(sidStr, UUID_LENGTH,
+ "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ pay->sid[0], pay->sid[1], pay->sid[2], pay->sid[3],
+ pay->sid[4], pay->sid[5], pay->sid[6], pay->sid[7],
+ pay->sid[8], pay->sid[9], pay->sid[10], pay->sid[11],
+ pay->sid[12], pay->sid[13], pay->sid[14], pay->sid[15]
+ );
+
+ if (ret == UUID_LENGTH - 1)
+ {
+ if(insertResource(sidStr, res->uri, clientResponse) == 1)
+ {
+ OIC_LOG_V(INFO,TAG,"%s%s%s%s\n",sidStr, ":", res->uri, " is new");
+ printResourceList();
+ queryResource();
+ }
+ else {
+ OIC_LOG_V(INFO,TAG,"%s%s%s%s\n",sidStr, ":", res->uri, " is old");
+ }
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Could Not Retrieve the Server ID");
+ }
+
+ res = res->next;
+ }
+}
+
+/* This function searches for the resource(sid:uri) in the ResourceList.
+ * If the Resource is found in the list then it returns 0 else insert
+ * the resource to front of the list and returns 1.
+ */
+int insertResource(const char * sid, char const * uri,
+ const OCClientResponse * clientResponse)
+{
+ ResourceNode * iter = resourceList;
+ char * sid_cpy = OICStrdup(sid);
+ char * uri_cpy = OICStrdup(uri);
+
+ //Checking if the resource(sid:uri) is new
+ while(iter)
+ {
+ if((strcmp(iter->sid, sid) == 0) && (strcmp(iter->uri, uri) == 0))
+ {
+ OICFree(sid_cpy);
+ OICFree(uri_cpy);
+ return 0;
+ }
+ else
+ {
+ iter = iter->next;
+ }
+ }
+
+ //Creating new ResourceNode
+ if((iter = (ResourceNode *) OICMalloc(sizeof(ResourceNode))))
{
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (CA_CONNTYPE),
- OC_LOW_QOS, &cbData, NULL, 0);
+ iter->sid = sid_cpy;
+ iter->uri = uri_cpy;
+ iter->endpoint = clientResponse->devAddr;
+ iter->next = NULL;
}
else
{
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
- OC_LOW_QOS, &cbData, NULL, 0);
+ OIC_LOG(ERROR, TAG, "Memory not allocated to ResourceNode");
+ OICFree(sid_cpy);
+ OICFree(uri_cpy);
+ return -1;
}
-#else
- ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
-#endif
- if (ret != OC_STACK_OK)
+
+ //Adding new ResourceNode to front of the ResourceList
+ if(!resourceList)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ resourceList = iter;
+ }
+ else
+ {
+ iter->next = resourceList;
+ resourceList = iter;
+ }
+ return 1;
+}
+
+void printResourceList()
+{
+ ResourceNode * iter;
+ iter = resourceList;
+ OIC_LOG(INFO, TAG, "Resource List: ");
+ while(iter)
+ {
+ OIC_LOG(INFO, TAG, "*****************************************************");
+ OIC_LOG_V(INFO, TAG, "sid = %s",iter->sid);
+ OIC_LOG_V(INFO, TAG, "uri = %s", iter->uri);
+ OIC_LOG_V(INFO, TAG, "ip = %s", iter->endpoint.addr);
+ OIC_LOG_V(INFO, TAG, "port = %d", iter->endpoint.port);
+ switch (iter->endpoint.adapter)
+ {
+ case OC_ADAPTER_IP:
+ OIC_LOG(INFO, TAG, "connType = Default (IPv4)");
+ break;
+ case OC_ADAPTER_GATT_BTLE:
+ OIC_LOG(INFO, TAG, "connType = BLE");
+ break;
+ case OC_ADAPTER_RFCOMM_BTEDR:
+ OIC_LOG(INFO, TAG, "connType = BT");
+ break;
+ default:
+ OIC_LOG(INFO, TAG, "connType = Invalid connType");
+ break;
+ }
+ OIC_LOG(INFO, TAG, "*****************************************************");
+ iter = iter->next;
}
- return ret;
+}
+
+void freeResourceList()
+{
+ OIC_LOG(INFO, TAG, "Freeing ResourceNode List");
+ ResourceNode * temp;
+ while(resourceList)
+ {
+
+ temp = resourceList;
+ resourceList = resourceList->next;
+ OICFree((void *)temp->sid);
+ OICFree((void *)temp->uri);
+ OICFree(temp);
+ }
+ resourceList = NULL;
}
int main(int argc, char* argv[])
{
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = USE_RANDOM_PORT;
- uint8_t ifname[] = "eth0";
int opt;
-
-#ifdef CA_INT
+ resourceList = NULL;
while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
-#else
- while ((opt = getopt(argc, argv, "u:t:")) != -1)
-#endif
{
switch(opt)
{
case 'u':
- UNICAST_DISCOVERY = atoi(optarg);
+ UnicastDiscovery = atoi(optarg);
break;
case 't':
- TEST_CASE = atoi(optarg);
+ TestCase = atoi(optarg);
break;
- #ifdef CA_INT
case 'c':
- CA_CONNTYPE = OCConnectivityType(atoi(optarg));
+ Connectivity = atoi(optarg);
break;
- #endif
default:
PrintUsage();
return -1;
}
}
- 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) ||
+ (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
{
PrintUsage();
return -1;
}
-
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == ERR_SUCCESS)
+ /* Initialize OCStack*/
+ if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
{
- OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
- paddr = addr;
+ OIC_LOG(ERROR, TAG, "OCStack init error");
+ return 0;
}
- /* Initialize OCStack*/
- if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
+ if(Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
{
- OC_LOG(ERROR, TAG, "OCStack init error");
- return 0;
+ ConnType = CT_ADAPTER_IP;//CT_DEFAULT;
+ }
+ else
+ {
+ OIC_LOG(INFO, TAG, "Default Connectivity type selected");
+ PrintUsage();
}
InitDiscovery();
{
if (OCProcess() != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack process error");
+ OIC_LOG(ERROR, TAG, "OCStack process error");
return 0;
}
-
sleep(2);
}
- OC_LOG(INFO, TAG, "Exiting occlient main loop...");
+ freeResourceList();
+ 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();
-}
-
-std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
-{
- return "/a/led";
-}
-
-/* You could do this with the JSON parser of your choice, a regular expression, PEG
-grammar, etc.. This "sample" version does not handle cases like escaping strings,
-but shows a simple way you might approach the task with just the C++98 library: */
-extract_result_t extract_value(const std::string& search_key, const std::string& input)
-{
- const std::string key('\"' + search_key + "\":\"");
-
- const size_t key_mark = input.find(key);
- const size_t key_edge = key_mark + key.length();
- const size_t val_mark = input.find_first_of("\"", key_edge);
-
- if(std::string::npos == key_mark || std::string::npos == val_mark) {
- std::ostringstream os;
-
- os << "extract_value(): \"" << search_key << "\" not found in input";
- OC_LOG(ERROR, TAG, os.str().c_str());
-
- return std::make_pair(false, "");
- }
-
- return std::make_pair(true, input.substr(key_edge, val_mark - key_edge));
-}
-
-extract_result_t parseSID(const OCClientResponse * const clientResponse)
-{
- const char* const& pl_in = reinterpret_cast<const char *>(clientResponse->resJSONPayload);
-
- const std::string pl(pl_in, strlen(pl_in));
-
- const extract_result_t sid = extract_value("sid", pl);
- const extract_result_t uri = extract_value("href", pl);
- //TODO-CA: It should be just enough to send the SID alone and not the combination
- // of it.
- return std::make_pair(sid.first and uri.first, sid.second + ':' + uri.second);
-}
-
-void parseClientResponse(OCClientResponse * clientResponse)
-{
- coapServerIP = getIPAddrTBServer(clientResponse);
- coapServerPort = getPortTBServer(clientResponse);
- coapServerResource = getQueryStrForGetPut(clientResponse);
-}
-
-void collateSIDs(const OCClientResponse * clientResponse, const std::string& server_ip)
-{
- static SID_set_t sids;
-
- const extract_result_t sid_result = parseSID(clientResponse);
-
- if(false == sid_result.first)
- return;
-
- const sid_t& sid = sid_result.second;
-
- /* ...there's no need for an application to take any special action, but we can tell
- if we've seen a resource before, regardless of the transport it arrive on: */
- std::ostringstream msg;
-
- if(not sids.insert(sid).second)
- msg << "SID " << sid << " has been seen before.\n";
- else
- msg << "SID " << sid << " is new.\n";
-
- OC_LOG(INFO, TAG, msg.str().c_str());
-}