X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsamples%2Flinux%2Fsecure%2Focclientbasicops.cpp;h=d6ce3071be5c0d474de1acb7712ae85da2531133;hb=c315c87e07c4080ecd0ef488e7a1047bc3c509b2;hp=c72458ac2b23ff761d28a75fff75175c9b013275;hpb=c7947c2aac0d24b278b7198c3f45e4bc1a70482e;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp b/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp index c72458a..d6ce307 100644 --- a/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp +++ b/resource/csdk/stack/samples/linux/secure/occlientbasicops.cpp @@ -18,13 +18,24 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include "iotivity_config.h" #include #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif +#ifdef HAVE_WINDOWS_H +#include +/** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */ +#ifdef ERROR +#undef ERROR +#endif +#endif #include #include +#include #include "ocstack.h" #include "logger.h" #include "occlientbasicops.h" @@ -37,6 +48,8 @@ static int UnicastDiscovery = 0; static int TestCase = 0; static int ConnType = 0; +static int DevOwner = 0; +static int WithTcp = 0; static char DISCOVERY_QUERY[] = "%s/oic/res"; OCConnectivityType discoveryReqConnType = CT_ADAPTER_IP; @@ -49,8 +62,10 @@ 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; @@ -81,14 +96,18 @@ OCPayload* putPayload() static void PrintUsage() { - OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -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 Confirmable Get/Put/Post Requests"); - OC_LOG(INFO, TAG, "-c 0 : Default auto-selection"); - OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type"); + 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, @@ -105,13 +124,16 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query, cbData.context = NULL; cbData.cd = NULL; - ret = OCDoResource(NULL, method, query.str().c_str(), dest, - (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload() : 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; @@ -119,40 +141,40 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query, 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_PAYLOAD(INFO, clientResponse->payload); - OC_LOG(INFO, TAG, ("=============> Put Response")); + 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 *, 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_PAYLOAD(INFO, clientResponse->payload); - OC_LOG(INFO, TAG, ("=============> Post Response")); + 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*, 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_PAYLOAD(INFO, clientResponse->payload); - OC_LOG(INFO, TAG, ("=============> Get Response")); + 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; } @@ -161,19 +183,19 @@ OCStackApplicationResult getReqCB(void*, OCDoHandle, OCClientResponse * clientRe OCStackApplicationResult discoveryReqCB(void*, OCDoHandle, OCClientResponse * clientResponse) { - 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)); - OC_LOG_V(INFO, TAG, + 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); if (clientResponse->result == OC_STACK_OK) { - OC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); ocConnType = clientResponse->connType; endpoint = clientResponse->devAddr; @@ -185,12 +207,12 @@ OCStackApplicationResult discoveryReqCB(void*, OCDoHandle, case TEST_NON_CON_OP: InitGetRequest(OC_LOW_QOS); InitPutRequest(OC_LOW_QOS); - //InitPostRequest(OC_LOW_QOS); + InitPostRequest(OC_LOW_QOS); break; case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); InitPutRequest(OC_HIGH_QOS); - //InitPostRequest(OC_HIGH_QOS); + InitPostRequest(OC_HIGH_QOS); break; } } @@ -200,12 +222,15 @@ OCStackApplicationResult discoveryReqCB(void*, OCDoHandle, 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 << 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)); @@ -214,9 +239,14 @@ int InitPutRequest(OCQualityOfService qos) 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 << coapServerResource; + if(WithTcp) + { + endpoint.adapter = OC_ADAPTER_TCP; + } endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE); // First POST operation (to create an LED instance) @@ -226,7 +256,7 @@ int InitPostRequest(OCQualityOfService qos) 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) @@ -235,20 +265,30 @@ int InitPostRequest(OCQualityOfService 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, &endpoint, - ((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 << coapServerResource; + if(WithTcp) + { + endpoint.adapter = OC_ADAPTER_TCP; + } endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE); return (InvokeOCDoResource(query, OC_REST_GET, &endpoint, @@ -265,9 +305,9 @@ int InitDiscovery() if (UnicastDiscovery) { - OC_LOG(INFO, TAG, "Enter IP address (with optional port) of the Server hosting resource\n"); - OC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n"); - OC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n"); + 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)) { @@ -275,7 +315,7 @@ int InitDiscovery() } else { - OC_LOG(ERROR, TAG, "!! Bad input for IP address. !!"); + OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!"); return OC_STACK_INVALID_PARAM; } } @@ -286,31 +326,37 @@ int InitDiscovery() cbData.cd = NULL; /* Start a discovery query*/ - OC_LOG_V(INFO, TAG, "Initiating %s Resource Discovery : %s\n", + OIC_LOG_V(INFO, TAG, "Initiating %s Resource Discovery : %s\n", (UnicastDiscovery) ? "Unicast" : "Multicast", queryUri); - ret = OCDoResource(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT, - 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:c:")) != -1) + while ((opt = getopt(argc, argv, "u:t:c:d:p:")) != -1) { switch(opt) { @@ -323,6 +369,19 @@ int main(int argc, char* argv[]) 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(); return -1; @@ -344,19 +403,22 @@ int main(int argc, char* argv[]) } else { - OC_LOG(INFO, TAG, "Using Default Connectivity type"); + OIC_LOG(INFO, TAG, "Using Default Connectivity type"); PrintUsage(); } // Initialize Persistent Storage for SVR database - OCPersistentStorage ps = { client_fopen, fread, fwrite, fclose, 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; } @@ -366,23 +428,23 @@ int main(int argc, char* argv[]) 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; @@ -399,15 +461,37 @@ int parseClientResponse(OCClientResponse * clientResponse) while (res) { coapServerResource.assign(res->uri); - OC_LOG_V(INFO, TAG, "Uri -- %s", coapServerResource.c_str()); - + OIC_LOG_V(INFO, TAG, "Uri -- %s", coapServerResource.c_str()); + if (0 == strcmp(coapServerResource.c_str(),OIC_RSRC_DOXM_URI)) + { + 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) { - endpoint.port = res->port; + if(WithTcp) + { +#ifdef TCP_ADAPTER + OIC_LOG_V(INFO,TAG,"SECUREPORT tcp: %d",res->tcpPort); + endpoint.port = res->tcpPort; +#endif + } + else + { + OIC_LOG_V(INFO,TAG,"SECUREPORT udp: %d",res->port); + endpoint.port = res->port; + } coapSecureResource = 1; } - OC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO"); + OIC_LOG_V(INFO, TAG, "Secure -- %s", coapSecureResource == 1 ? "YES" : "NO"); // If we discovered a secure resource, exit from here if (coapSecureResource)