X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsamples%2Flinux%2FSimpleClientServer%2Focclientslow.cpp;h=c4b762baff10247fb78abfe57bac36685c78cd83;hb=refs%2Ftags%2Fsubmit%2Ftizen_4.0%2F20171010.021147;hp=b980443e41febca822e06e50b74ccf096440d6eb;hpb=7e4ed6ed318803c884fc65cc18ed403e6a81f251;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 b980443..c4b762b 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp @@ -18,25 +18,41 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include "iotivity_config.h" #include #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif +#ifdef HAVE_WINDOWS_H +#include +#endif #include #include +#include #include "ocstack.h" #include "logger.h" #include "occlientslow.h" +#include "oic_string.h" +#include "ocpayload.h" +#include "payload_logging.h" +#include "common.h" + +// Tracking user input +static int UnicastDiscovery = 0; +static int TestCase = 0; +static int ConnectivityType = 0; -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"; -static std::string putPayload = "{\"state\":\"off\",\"power\":10}"; -static std::string coapServerIP = "255.255.255.255"; -static std::string coapServerPort = "5683"; static std::string coapServerResource = "/a/led"; +//The following variable determines the interface protocol (IP, etc) +//to be used for sending unicast messages. Default set to IP. +static OCConnectivityType adapterType = CT_ADAPTER_IP; +static OCDevAddr endpoint; +static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res"; + int gQuitFlag = 0; /* SIGINT handler: set gQuitFlag to 1 for graceful termination */ @@ -50,73 +66,92 @@ void handleSigInt(int signum) 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 Initiate Nonconfirmable Get Request"); - OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request"); + OIC_LOG(INFO, TAG, "Usage : occlient -c <0|1> -u <0|1> -t <1|2|3>"); + OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection"); + OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type"); + 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 Request"); + OIC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request"); + OIC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate NonConfirmable Put Request"); + OIC_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); + OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); + OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); + OIC_LOG(INFO, TAG, "Get Response =============> "); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); if(clientResponse->rcvdVendorSpecificHeaderOptions && clientResponse->numRcvdVendorSpecificHeaderOptions) { - 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, - ((OCHeaderOption)rcvdOptions[i]).optionLength); + + OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData, + MAX_HEADER_OPTION_DATA_LENGTH); } } } @@ -124,33 +159,25 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse } // This is a function called back when a device is discovered -OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, +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, "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)); - OC_LOG_V(INFO, TAG, - "Device =============> Discovered %s @ %d.%d.%d.%d:%d", - clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], - remoteIpAddr[2], remoteIpAddr[3], remotePortNu); + OIC_LOG_V(INFO, TAG, "Discovered @ %s:%u =============> ", + clientResponse->devAddr.addr, clientResponse->devAddr.port); + OIC_LOG_PAYLOAD (INFO, clientResponse->payload); - parseClientResponse(clientResponse); + endpoint = clientResponse->devAddr; - switch(TEST_CASE) + switch(TestCase) { case TEST_NON_CON_OP: InitGetRequest(OC_LOW_QOS); @@ -158,75 +185,99 @@ 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; } } - return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; + return UnicastDiscovery ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; } int InitGetRequest(OCQualityOfService qos) { - OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__); + OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__); std::ostringstream query; - query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource; + query << coapServerResource; + OIC_LOG_V (INFO, TAG, "Performing GET with query : %s", query.str().c_str()); + return (InvokeOCDoResource(query, OC_REST_GET, &endpoint, + (qos == OC_HIGH_QOS)? OC_HIGH_QOS : OC_LOW_QOS, + getReqCB, NULL, 0)); +} - return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? - OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0)); +int InitPutRequest(OCQualityOfService qos) +{ + OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__); + std::ostringstream query; + query << coapServerResource; + OIC_LOG_V (INFO, TAG, "Performing PUT with query : %s", query.str().c_str()); + return (InvokeOCDoResource(query, OC_REST_PUT, &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) { - strcpy(szQueryUri, OC_WELL_KNOWN_QUERY); + 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 - // TODO-CA: The adapter type is set to all but should be configurable - add as API param - ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ETHERNET | OC_WIFI), - OC_LOW_QOS, &cbData, NULL, 0); -#else - ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0); -#endif + + 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; } 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; - while ((opt = getopt(argc, argv, "u:t:")) != -1) + while ((opt = getopt(argc, argv, "u:t:c:")) != -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': + ConnectivityType = atoi(optarg); break; default: PrintUsage(); @@ -234,88 +285,58 @@ 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) || + (ConnectivityType < CT_ADAPTER_DEFAULT || ConnectivityType >= 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 (OCInit(NULL, 0, OC_CLIENT) != 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(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP) { - OC_LOG(ERROR, TAG, "OCStack init error"); - return 0; + adapterType = CT_ADAPTER_IP; + } + else + { + OIC_LOG(INFO, TAG, "Default Connectivity type selected..."); + adapterType = CT_ADAPTER_IP; } InitDiscovery(); // 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; } sleep(2); } - 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(); -} - -std::string getQueryStrForGetPut(OCClientResponse * clientResponse) +std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/) { return "/a/led"; } -void parseClientResponse(OCClientResponse * clientResponse) -{ - coapServerIP = getIPAddrTBServer(clientResponse); - coapServerPort = getPortTBServer(clientResponse); - coapServerResource = getQueryStrForGetPut(clientResponse); -}