X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsamples%2Flinux%2FSimpleClientServer%2Focclientslow.cpp;h=e875cf2de8b07ad876db3030d1e05c9de9bbb145;hb=8c01dff2c5bc5496f7dc1632c498943ec6ecb015;hp=5fc29c66102f3445ed0ff1f7381ba65ad4984520;hpb=935fdb9b67b6c10d007e652e9e2e028fd6ccfe09;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp index 5fc29c6..e875cf2 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp @@ -28,20 +28,23 @@ #include "ocstack.h" #include "logger.h" #include "occlientslow.h" +#include "ocpayload.h" +// Tracking user input static int UNICAST_DISCOVERY = 0; static int TEST_CASE = 0; -static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:6298/oc/core"; -static std::string putPayload = "{\"state\":\"off\",\"power\":10}"; +static int CONNECTIVITY = 0; + +static const char * UNICAST_DISCOVERY_QUERY = "coap://%s/oic/res"; static std::string coapServerIP = "255.255.255.255"; -static std::string coapServerPort = "5683"; +static uint16_t coapServerPort = 5683; static std::string coapServerResource = "/a/led"; -//The following variable determines the interface protocol (IPv4, IPv6, etc) -//to be used for sending unicast messages. Default set to IPv4. -static OCConnectivityType OC_CONNTYPE = OC_IPV4; -static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core"; -static int IPV4_ADDR_SIZE = 16; +//The following variable determines the interface protocol (IP, etc) +//to be used for sending unicast messages. Default set to IP. +static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP; +static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res"; +static int IPV4_ADDR_SIZE = 24; void StripNewLineChar(char* str); int gQuitFlag = 0; @@ -57,12 +60,31 @@ void handleSigInt(int signum) static void PrintUsage() { - OC_LOG(INFO, TAG, "Usage : occlient -c <0|1> -u <0|1> -t <1|2|3>"); - OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)"); + OC_LOG(INFO, TAG, "Usage : occlient -c <0|1|2> -u <0|1> -t <1|2|3>"); + OC_LOG(INFO, TAG, "-c 0 : Default auto-selection"); + OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type"); 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 Initiate Nonconfirmable Get Request"); OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request"); + OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate NonConfirmable Put Request"); + OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Confirmable Put Request"); +} + +OCPayload* putPayload() +{ + OCRepPayload* payload = OCRepPayloadCreate(); + + if(!payload) + { + std::cout << "Failed to create put payload object"<result)); OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); - OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", - clientResponse->resJSONPayload); + OC_LOG(INFO, TAG, "Get Response =============> "); + OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload); if(clientResponse->rcvdVendorSpecificHeaderOptions && clientResponse->numRcvdVendorSpecificHeaderOptions) @@ -129,9 +152,6 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse 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"); @@ -141,14 +161,9 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, { 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); - - OC_LOG_V(INFO, TAG, - "Device =============> Discovered %s @ %d.%d.%d.%d:%d", - clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], - remoteIpAddr[2], remoteIpAddr[3], remotePortNu); + OC_LOG_V(INFO, TAG, "Discovered @ %s:%u =============> ", + clientResponse->devAddr.addr, clientResponse->devAddr.port); + OC_LOG_PAYLOAD (INFO, TAG, clientResponse->payload); parseClientResponse(clientResponse); @@ -160,6 +175,12 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); break; + case TEST_NON_CON_PUT: + InitPutRequest(OC_LOW_QOS); + break; + case TEST_CON_PUT: + InitPutRequest(OC_HIGH_QOS); + break; default: PrintUsage(); break; @@ -175,11 +196,21 @@ int InitGetRequest(OCQualityOfService qos) OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__); std::ostringstream query; query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource; - + OC_LOG_V (INFO, TAG, "Performing GET with query : %s", query.str().c_str()); return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0)); } +int InitPutRequest(OCQualityOfService qos) +{ + OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__); + std::ostringstream query; + query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource; + OC_LOG_V (INFO, TAG, "Performing PUT with query : %s", query.str().c_str()); + return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? + OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0)); +} + int InitDiscovery() { OCStackResult ret; @@ -189,7 +220,8 @@ int InitDiscovery() if (UNICAST_DISCOVERY) { char ipv4addr[IPV4_ADDR_SIZE]; - printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n"); + OC_LOG(INFO, TAG, "Enter IPv4:port of the Server hosting resource"\ + "(Ex: 192.168.0.15:1234)"); if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin)) { //Strip newline char from ipv4addr @@ -216,7 +248,7 @@ int InitDiscovery() } else { - ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_ALL, + ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0); } if (ret != OC_STACK_OK) @@ -241,9 +273,7 @@ int main(int argc, char* argv[]) TEST_CASE = atoi(optarg); break; case 'c': - // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported - // OC_CONNTYPE = OCConnectivityType(atoi(optarg)); - OC_CONNTYPE = OC_IPV4; + CONNECTIVITY = atoi(optarg); break; default: PrintUsage(); @@ -252,7 +282,8 @@ int main(int argc, char* argv[]) } if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) || - (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) ) + (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) || + (CONNECTIVITY < CT_ADAPTER_DEFAULT || CONNECTIVITY >= MAX_CT)) { PrintUsage(); return -1; @@ -265,6 +296,16 @@ int main(int argc, char* argv[]) return 0; } + if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IP) + { + OC_CONNTYPE = CT_ADAPTER_IP; + } + else + { + OC_LOG(INFO, TAG, "Default Connectivity type selected..."); + OC_CONNTYPE = CT_ADAPTER_IP; + } + InitDiscovery(); // Break from loop with Ctrl+C @@ -290,30 +331,6 @@ int main(int argc, char* argv[]) 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"; @@ -321,8 +338,8 @@ std::string getQueryStrForGetPut(OCClientResponse * clientResponse) void parseClientResponse(OCClientResponse * clientResponse) { - coapServerIP = getIPAddrTBServer(clientResponse); - coapServerPort = getPortTBServer(clientResponse); + coapServerIP = clientResponse->devAddr.addr; + coapServerPort = clientResponse->devAddr.port; coapServerResource = getQueryStrForGetPut(clientResponse); }