X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsamples%2Flinux%2FSimpleClientServer%2Focclientbasicops.cpp;h=13bc01ce4732014df2fc1e772e6bbef4db3c5f1b;hb=refs%2Ftags%2Ftizen_4.0.m2_release;hp=1f6051d1f8ee5f7e3192c501f132922502ffa36d;hpb=bbe6ea7b93285c7c35291a6db4b5da9aa57cf550;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp index 1f6051d..13bc01c 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp @@ -17,34 +17,42 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include "iotivity_config.h" #include #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif +#ifdef HAVE_WINDOWS_H +#include +#endif #include #include +#include +#include #include "ocstack.h" #include "logger.h" #include "occlientbasicops.h" -#include "cJSON.h" -#include "ocmalloc.h" +#include "ocpayload.h" +#include "payload_logging.h" +#include "oic_malloc.h" +#include "oic_string.h" +#include "common.h" #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 int IPV4_ADDR_SIZE = 16; -static int UNICAST_DISCOVERY = 0; -static int TEST_CASE = 0; +static int UnicastDiscovery = 0; +static int TestCase = 0; +static int Connectivity = 0; -static const char UNICAST_DISCOVERY_QUERY[] = "coap://%s:6298/oc/core"; -static std::string putPayload = "{\"state\":\"off\",\"power\":10}"; - -//The following variable determines the interface (wifi, ethernet etc.) -//to be used for sending unicast messages. Default set to WIFI. -static OCConnectivityType OC_CONNTYPE = OC_WIFI; -static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core"; +//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; @@ -60,17 +68,33 @@ void handleSigInt(int signum) } } +OCPayload* putPayload() +{ + OCRepPayload* payload = OCRepPayloadCreate(); + + if(!payload) + { + std::cout << "Failed to create put payload object"< -t <1|2|3> -c <0|1>"); - 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 " + OIC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate " "Confirmable Get/Put/Post Requests"); - OC_LOG(INFO, TAG, "-c <0|1> : Send unicast messages over Ethernet or WIFI."); - OC_LOG(INFO, TAG, "Default connectivityType WIFI"); + OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection"); + OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type"); } /* @@ -81,9 +105,12 @@ const ResourceNode * getResource() return resourceList; } -OCStackResult InvokeOCDoResource(std::ostringstream &query, OCMethod method, - OCConnectivityType connType, OCQualityOfService qos, - OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions) +OCStackResult InvokeOCDoResource(std::ostringstream &query, + OCMethod method, + const OCDevAddr *dest, + OCQualityOfService qos, + OCClientResponseHandler cb, + OCHeaderOption * options, uint8_t numOptions) { OCStackResult ret; OCCallbackData cbData; @@ -92,123 +119,103 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query, OCMethod method, cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; - ret = OCDoResource(NULL, method, query.str().c_str(), 0, - (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload.c_str() : NULL, - connType, 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, 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", + 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) { - uint8_t remoteIpAddr[4]; - uint16_t remotePortNu; - if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { - OC_LOG(INFO, TAG, "<====Callback Context for PUT received successfully====>"); + OIC_LOG(INFO, TAG, "<====Callback Context for PUT received successfully====>"); } else { - OC_LOG(ERROR, TAG, "<====Callback Context for PUT fail====>"); + OIC_LOG(ERROR, TAG, "<====Callback Context for PUT fail====>"); } if(clientResponse) { - OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, - remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); - OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); - - OC_LOG_V(INFO, TAG,"PUT Response: %s \nFrom %d.%d.%d.%d:%d\n", - clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], - remoteIpAddr[2], remoteIpAddr[3], remotePortNu); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG(INFO, TAG, ("=============> Put Response")); } else { - OC_LOG(ERROR, TAG, "<====PUT Callback fail to receive clientResponse====>\n"); + 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) { - uint8_t remoteIpAddr[4]; - uint16_t remotePortNu; - if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { - OC_LOG(INFO, TAG, "<====Callback Context for POST received successfully====>"); + OIC_LOG(INFO, TAG, "<====Callback Context for POST received successfully====>"); } else { - OC_LOG(ERROR, TAG, "<====Callback Context for POST fail====>"); + OIC_LOG(ERROR, TAG, "<====Callback Context for POST fail====>"); } if(clientResponse) { - OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, - remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); - OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); - - OC_LOG_V(INFO, TAG,"POST Response: %s \nFrom %d.%d.%d.%d:%d\n", - clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], - remoteIpAddr[2], remoteIpAddr[3], remotePortNu); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG(INFO, TAG, ("=============> Post Response")); } else { - OC_LOG(ERROR, TAG, "<====POST Callback fail to receive clientResponse====>\n"); + 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) { - uint8_t remoteIpAddr[4]; - uint16_t remotePortNu; - if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { - OC_LOG(INFO, TAG, "<====Callback Context for GET received successfully====>"); + OIC_LOG(INFO, TAG, "<====Callback Context for GET received successfully====>"); } else { - OC_LOG(ERROR, TAG, "<====Callback Context for GET fail====>"); + OIC_LOG(ERROR, TAG, "<====Callback Context for GET fail====>"); } if (clientResponse) { - 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, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG(INFO, TAG, ("=============> Get Response")); - OC_LOG_V(INFO, TAG,"Get Response: %s \nFrom %d.%d.%d.%d:%d\n", - clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], - remoteIpAddr[2], remoteIpAddr[3], remotePortNu); - - if (clientResponse->rcvdVendorSpecificHeaderOptions - && clientResponse->numRcvdVendorSpecificHeaderOptions) + if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0 ) { - OC_LOG (INFO, TAG, "Received vendor specific options"); + OIC_LOG (INFO, TAG, "Received vendor specific options"); uint8_t i = 0; OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions; for (i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++) { if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID) { - OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with", + OIC_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, + OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData, MAX_HEADER_OPTION_DATA_LENGTH); } } @@ -216,7 +223,7 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, } else { - OC_LOG(ERROR, TAG, "<====GET Callback fail to receive clientResponse====>\n"); + OIC_LOG(ERROR, TAG, "<====GET Callback fail to receive clientResponse====>\n"); } return OC_STACK_DELETE_TRANSACTION; } @@ -224,39 +231,34 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, /* * This is a function called back when a device is discovered */ -OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, - OCClientResponse * clientResponse) +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, "\n<====Callback Context for DISCOVERY query " + OIC_LOG(INFO, TAG, "\n<====Callback Context for DISCOVERY query " "received successfully====>"); } else { - OC_LOG(ERROR, TAG, "\n<====Callback Context for DISCOVERY fail====>"); + OIC_LOG(ERROR, TAG, "\n<====Callback Context for DISCOVERY fail====>"); } if (clientResponse) { - OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, - remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); - OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); - - OC_LOG_V(INFO, TAG, - "Device Discovered %s \n @ %d.%d.%d.%d:%d\n", - clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], - remoteIpAddr[2], remoteIpAddr[3], remotePortNu); + OIC_LOG_V(INFO, TAG, + "Device =============> Discovered @ %s:%d", + clientResponse->devAddr.addr, + clientResponse->devAddr.port); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); collectUniqueResource(clientResponse); } else { - OC_LOG(ERROR, TAG, "<====DISCOVERY Callback fail to receive clientResponse====>\n"); + OIC_LOG(ERROR, TAG, "<====DISCOVERY Callback fail to receive clientResponse====>\n"); } - return (UNICAST_DISCOVERY) ? + return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; } @@ -268,13 +270,13 @@ int InitPutRequest(OCQualityOfService qos) if(!resource) { - OC_LOG_V(ERROR, TAG, "Resource null, can't do PUT request\n"); + OIC_LOG_V(ERROR, TAG, "Resource null, can't do PUT request\n"); return -1; } - query << "coap://" << resource->ip << ":" << resource->port << resource->uri ; - OC_LOG_V(INFO, TAG,"Executing InitPutRequest, Query: %s", query.str().c_str()); + query << resource->uri; + OIC_LOG_V(INFO, TAG,"Executing InitPutRequest, Query: %s", query.str().c_str()); - return (InvokeOCDoResource(query, OC_REST_PUT, resource->connType, + return (InvokeOCDoResource(query, OC_REST_PUT, &resource->endpoint, ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS), putReqCB, NULL, 0)); } @@ -284,38 +286,38 @@ int InitPostRequest(OCQualityOfService qos) OCStackResult result; std::ostringstream query; //Get most recently inserted resource - const ResourceNode * resource = getResource(); + const ResourceNode *resource = getResource(); if(!resource) { - OC_LOG_V(ERROR, TAG, "Resource null, can't do POST request\n"); + OIC_LOG_V(ERROR, TAG, "Resource null, can't do POST request\n"); return -1; } - query << "coap://" << resource->ip << ":" << resource->port << resource->uri ; - OC_LOG_V(INFO, TAG,"Executing InitPostRequest, Query: %s", query.str().c_str()); + 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, resource->connType, + 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(ERROR, 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, resource->connType, + 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(ERROR, 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,resource->connType, + return (InvokeOCDoResource(query, OC_REST_POST, &resource->endpoint, ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS), postReqCB, NULL, 0)); } @@ -328,13 +330,13 @@ int InitGetRequest(OCQualityOfService qos) if(!resource) { - OC_LOG_V(ERROR, TAG, "Resource null, can't do GET request\n"); + OIC_LOG_V(ERROR, TAG, "Resource null, can't do GET request\n"); return -1; } - query << "coap://" << resource->ip << ":" << resource->port << resource->uri ; - OC_LOG_V(INFO, TAG,"Executing InitGetRequest, Query: %s", query.str().c_str()); + query << resource->uri; + OIC_LOG_V(INFO, TAG,"Executing InitGetRequest, Query: %s", query.str().c_str()); - return (InvokeOCDoResource(query, OC_REST_GET, resource->connType, + return (InvokeOCDoResource(query, OC_REST_GET, &resource->endpoint, (qos == OC_HIGH_QOS)?OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0)); } @@ -342,189 +344,44 @@ int InitDiscovery() { OCStackResult ret; OCCallbackData cbData; - /* Start a discovery query*/ - char szQueryUri[64] = {}; - if (UNICAST_DISCOVERY) - { - char ipv4addr[IPV4_ADDR_SIZE]; - printf("Enter IPv4 address of the Server hosting " - "resource (Ex: 192.168.0.15)\n"); - if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin)) + char queryUri[200]; + char ipaddr[100] = { '\0' }; + + if (UnicastDiscovery) + { + 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; } } - else - { - strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY); - } + + snprintf(queryUri, sizeof (queryUri), RESOURCE_DISCOVERY_QUERY, ipaddr); + cbData.cb = discoveryReqCB; cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; - if (UNICAST_DISCOVERY) - { - ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE, - OC_LOW_QOS, &cbData, NULL, 0); - } - else - { - ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL), - 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; } - - -const char * getIPAddr(const OCClientResponse * clientResponse) -{ - uint8_t a, b, c, d; - if(!clientResponse || 0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d)) - { - return ""; - } - - char * ipaddr = NULL; - if((ipaddr = (char *) OCCalloc(1, MAX_IP_ADDR_ST_SZ))) - { - snprintf(ipaddr, MAX_IP_ADDR_ST_SZ, "%d.%d.%d.%d", a,b,c,d); - } - else - { - OC_LOG(ERROR, TAG, "Memory not allocated to ipaddr"); - } - return ipaddr; -} - -const char * getPort(const OCClientResponse * clientResponse) -{ - uint16_t p = 0; - if(!clientResponse || 0 != OCDevAddrToPort(clientResponse->addr, &p) ) - { - return ""; - } - - char * port = NULL; - if((port = (char *) OCCalloc(1, MAX_PORT_ST_SZ))) - { - snprintf(port, MAX_PORT_ST_SZ, "%d", p); - } - else - { - OC_LOG(ERROR, TAG, "Memory not allocated to port"); - } - return port; -} - -int parseJSON(const char * resJSONPayload, char ** sid_c, - char *** uri_c, int * totalRes) -{ - cJSON * root = NULL; - cJSON * oc = NULL; - - root = cJSON_Parse((char *)(resJSONPayload)); - - if (!root) - { - OC_LOG(ERROR, TAG, "JSON Parsing Error"); - return OC_STACK_INVALID_JSON; - } - - oc = cJSON_GetObjectItem(root,"oc"); - if (!oc) - { - OC_LOG(ERROR, TAG, "Invalid JSON : Missing oc object"); - return OC_STACK_INVALID_JSON; - } - - * totalRes = cJSON_GetArraySize(oc); - - if(oc->type == cJSON_Array) - { - cJSON * resource = cJSON_GetArrayItem(oc, 0); - - if(!resource) - { - return OC_STACK_INVALID_JSON; - } - - if (cJSON_GetObjectItem(resource, "sid")) - { - char * sid = cJSON_GetObjectItem(resource, "sid")->valuestring; - if((* sid_c = (char *)OCCalloc(1, strlen (sid) + 1))) - { - memcpy(* sid_c, sid, strlen(sid) + 1); - } - else - { - OC_LOG(ERROR, TAG, "Memory not allocated to sid"); - return OC_STACK_NO_MEMORY; - } - } - else - { - OC_LOG(ERROR, TAG, "Invalid JSON : Missing sid object"); - return OC_STACK_INVALID_JSON; - } - - if(!(* uri_c = (char ** )OCMalloc ((* totalRes) * sizeof(char **)))) - { - OC_LOG(ERROR, TAG, "Memory not allocated to sid_c array"); - return OC_STACK_NO_MEMORY; - } - - int i = 0; - - while(true) - { - if (cJSON_GetObjectItem(resource, "href")) - { - char *uri= cJSON_GetObjectItem(resource, "href")->valuestring; - if(((*uri_c)[i] = (char *)OCCalloc(1, strlen (uri) + 1))) - { - memcpy((*uri_c)[i], uri, strlen(uri) + 1); - } - else - { - OC_LOG(ERROR, TAG, "Memory not allocated to uri"); - return OC_STACK_NO_MEMORY; - } - i++; - if(i >= (* totalRes)) - break; - resource = cJSON_GetArrayItem(oc, i); - } - else - { - OC_LOG(ERROR, TAG, "Invalid JSON : Missing uri object"); - return OC_STACK_INVALID_JSON; - } - } - } - else - { - return OC_STACK_INVALID_JSON; - OC_LOG(ERROR, TAG, "Invalid JSON : oc object type is not an array"); - } - return OC_STACK_OK; -} - void queryResource() { - printf("\n"); - switch(TEST_CASE) + switch(TestCase) { case TEST_DISCOVER_REQ: break; @@ -542,39 +399,50 @@ void queryResource() PrintUsage(); break; } - printf("\n"); } void collectUniqueResource(const OCClientResponse * clientResponse) { - char * sid = NULL; - char ** uri = NULL; - int totalRes = 0; + OCDiscoveryPayload* pay = (OCDiscoveryPayload*) clientResponse->payload; + OCResourcePayload* res = pay->resources; - if(parseJSON(clientResponse->resJSONPayload, & sid, & uri, &totalRes) - != OC_STACK_OK) - { - OC_LOG(ERROR, TAG, "Error while parsing JSON payload in OCClientResponse"); - } + // Including the NUL terminator, length of UUID string of the form: + // "a62389f7-afde-00b6-cd3e-12b97d2fcf09" +# define UUID_LENGTH 37 - int i; - for(i = 0; i < totalRes; i++) - { - if(insertResource(sid, uri[i], clientResponse) == 1) + 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) { - printf("%s%s%s%s\n",sid, ":", uri[i], " is new"); - printResourceList(); - queryResource(); + 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 { - printf("%s%s%s%s\n\n",sid, ":", uri[i], " has been seen before"); + OIC_LOG(ERROR, TAG, "Could Not Retrieve the Server ID"); } - } - OCFree(uri); - } + 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 @@ -584,12 +452,16 @@ 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 @@ -599,18 +471,18 @@ int insertResource(const char * sid, char const * uri, } //Creating new ResourceNode - if((iter = (ResourceNode *) OCMalloc(sizeof(ResourceNode)))) + if((iter = (ResourceNode *) OICMalloc(sizeof(ResourceNode)))) { - iter->sid = sid; - iter->uri = uri; - iter->ip = getIPAddr(clientResponse); - iter->port = getPort(clientResponse); - iter->connType = clientResponse->connType; + iter->sid = sid_cpy; + iter->uri = uri_cpy; + iter->endpoint = clientResponse->devAddr; iter->next = NULL; } else { - OC_LOG(ERROR, TAG, "Memory not allocated to ResourceNode"); + OIC_LOG(ERROR, TAG, "Memory not allocated to ResourceNode"); + OICFree(sid_cpy); + OICFree(uri_cpy); return -1; } @@ -631,52 +503,48 @@ void printResourceList() { ResourceNode * iter; iter = resourceList; - printf("\nResource List\n"); + OIC_LOG(INFO, TAG, "Resource List: "); while(iter) { - printf("*****************************************************\n"); - printf("sid = %s \n",iter->sid); - printf("uri = %s\n", iter->uri); - printf("ip = %s\n", iter->ip); - printf("port = %s\n", iter->port); - switch (iter->connType) + 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_ETHERNET: - printf("connType = %s\n","Ethernet"); - break; - case OC_WIFI: - printf("connType = %s\n","WiFi"); + case OC_ADAPTER_IP: + OIC_LOG(INFO, TAG, "connType = Default (IPv4)"); break; - case OC_LE: - printf("connType = %s\n","BLE"); + case OC_ADAPTER_GATT_BTLE: + OIC_LOG(INFO, TAG, "connType = BLE"); break; - case OC_EDR: - printf("connType = %s\n","BT"); + case OC_ADAPTER_RFCOMM_BTEDR: + OIC_LOG(INFO, TAG, "connType = BT"); break; - case OC_ALL: default: - printf("connType = %s\n","Invalid connType"); + OIC_LOG(INFO, TAG, "connType = Invalid connType"); break; } - printf("*****************************************************\n"); + OIC_LOG(INFO, TAG, "*****************************************************"); iter = iter->next; } } void freeResourceList() { - OC_LOG(INFO, TAG, "Freeing ResourceNode List"); + OIC_LOG(INFO, TAG, "Freeing ResourceNode List"); ResourceNode * temp; while(resourceList) { + temp = resourceList; resourceList = resourceList->next; - OCFree((void *)temp->sid); - OCFree((void *)temp->uri); - OCFree((void *)temp->ip); - OCFree((void *)temp->port); - OCFree(temp); + OICFree((void *)temp->sid); + OICFree((void *)temp->uri); + OICFree(temp); } + resourceList = NULL; } int main(int argc, char* argv[]) @@ -688,13 +556,13 @@ int main(int argc, char* argv[]) switch(opt) { case 'u': - UNICAST_DISCOVERY = atoi(optarg); + UnicastDiscovery = atoi(optarg); break; case 't': - TEST_CASE = atoi(optarg); + TestCase = atoi(optarg); break; case 'c': - OC_CONNTYPE = OCConnectivityType(atoi(optarg)); + Connectivity = atoi(optarg); break; default: PrintUsage(); @@ -702,20 +570,31 @@ int main(int argc, char* argv[]) } } - 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; } /* Initialize OCStack*/ - if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) + if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "OCStack init error"); + OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } + if(Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP) + { + ConnType = CT_ADAPTER_IP;//CT_DEFAULT; + } + else + { + OIC_LOG(INFO, TAG, "Default Connectivity type selected"); + PrintUsage(); + } + InitDiscovery(); // Break from loop with Ctrl+C @@ -725,17 +604,17 @@ int main(int argc, char* argv[]) { if (OCProcess() != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "OCStack process error"); + OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } sleep(2); } freeResourceList(); - 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; }