From: Abhishek Sharma Date: Fri, 16 Sep 2016 13:01:13 +0000 (+0530) Subject: Added client sample for proxy X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=02488c95057b3cf160ad60feea3b960352b36c70;p=contrib%2Fiotivity.git Added client sample for proxy Removed proxy changes from csdk sample and added separate sample for proxy. Change-Id: I45b31b46ee61eee7dd440dad0b2957345a75b1d0 Signed-off-by: Abhishek Sharma Reviewed-on: https://gerrit.iotivity.org/gerrit/11869 Reviewed-by: jihwan seo Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp index f71ce90..c99f939 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp @@ -66,10 +66,6 @@ static OCDevAddr serverAddr; static char discoveryAddr[100]; static std::string coapServerResource = "/a/light"; -// Following resource is used to verify coap-http proxy -static std::string coapProxyResource = OC_RSRVD_PROXY_URI; -static std::string httpResource; // Will be taken as user input - #ifdef WITH_PRESENCE // The handle for observe registration OCDoHandle gPresenceHandle; @@ -109,7 +105,7 @@ OCPayload* putPayload() static void PrintUsage() { - OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..21> -c <0|1>"); + OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..20> -c <0|1>"); OIC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources"); OIC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)"); OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type"); @@ -144,7 +140,6 @@ static void PrintUsage() "add vendor specific header options"); OIC_LOG(INFO, TAG, "-t 19 : Discover Platform"); OIC_LOG(INFO, TAG, "-t 20 : Discover Devices"); - OIC_LOG(INFO, TAG, "-t 21 -p \"http_uri\": Discover Proxy and Initiate Nonconfirmable Get Request"); } OCStackResult InvokeOCDoResource(std::ostringstream &query, @@ -163,8 +158,7 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query, cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; - const char *uri = query.str().length() ? query.str().c_str() : NULL; - ret = OCDoResource(&handle, method, uri, remoteAddr, + ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr, (method == OC_REST_PUT) ? putPayload() : NULL, (ConnType), qos, &cbData, options, numOptions); @@ -409,12 +403,9 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/, OCResourcePayload *resource = (OCResourcePayload*) payload->resources; int found = 0; - - std::string resourceToFind = (TestCase == TEST_PROXY_GET_REQ_NON) ? - coapProxyResource : coapServerResource; while (resource) { - if(resource->uri && strcmp(resource->uri, resourceToFind.c_str()) == 0) + if(resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0) { found = 1; break; @@ -424,7 +415,7 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/, if(!found) { - OIC_LOG_V (INFO, TAG, "No %s in payload", resourceToFind.c_str()); + OIC_LOG_V (INFO, TAG, "No %s in payload", coapServerResource.c_str()); return OC_STACK_KEEP_TRANSACTION; } @@ -464,9 +455,6 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/, case TEST_OBS_REQ_CON: InitObserveRequest(OC_HIGH_QOS); break; - case TEST_PROXY_GET_REQ_NON: - InitProxyGetRequest(OC_LOW_QOS); - break; #ifdef WITH_PRESENCE case TEST_OBS_PRESENCE: case TEST_OBS_PRESENCE_WITH_FILTER: @@ -700,25 +688,6 @@ int InitDeleteRequest(OCQualityOfService qos) return result; } -int InitProxyGetRequest(OCQualityOfService qos) -{ - OIC_LOG(INFO, TAG, "InitProxyGetRequest"); - OCHeaderOption option; - memset(&option, 0, sizeof(option)); - - option.protocolID = OC_COAP_ID; - option.optionID = OC_RSRVD_PROXY_OPTION_ID; - memcpy(option.optionData, (uint8_t *)httpResource.c_str(), httpResource.length()); - option.optionLength = httpResource.length(); - - std::ostringstream query; - // A request with proxy uri shall not have resource uri - // query << coapProxyResource; - - return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, - (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, &option, 1)); -} - int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery) { @@ -838,7 +807,7 @@ int main(int argc, char* argv[]) { int opt; - while ((opt = getopt(argc, argv, "u:t:c:p:")) != -1) + while ((opt = getopt(argc, argv, "u:t:c:")) != -1) { switch(opt) { @@ -851,12 +820,6 @@ int main(int argc, char* argv[]) case 'c': Connectivity = atoi(optarg); break; - case 'p': - if(optarg) - { - httpResource = optarg; - } - break; default: PrintUsage(); return -1; @@ -865,8 +828,7 @@ int main(int argc, char* argv[]) if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) || (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) || - (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT) || - (TestCase == TEST_PROXY_GET_REQ_NON && httpResource.length() == 0) ) + (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT)) { PrintUsage(); return -1; diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.h b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.h index caa0ec9..10d0c85 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.h +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.h @@ -63,7 +63,6 @@ typedef enum { TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS, TEST_DISCOVER_PLATFORM_REQ, TEST_DISCOVER_DEV_REQ, - TEST_PROXY_GET_REQ_NON, MAX_TESTS } CLIENT_TEST; @@ -108,7 +107,6 @@ int InitGetRequest(OCQualityOfService qos); int InitDeviceDiscovery(OCQualityOfService qos); int InitPlatformDiscovery(OCQualityOfService qos); int InitDiscovery(OCQualityOfService qos); -int InitProxyGetRequest(OCQualityOfService qos); /* Call delete operation on already deleted resource */ void* RequestDeleteDeathResourceTask(void* myqos); diff --git a/service/coap-http-proxy/SConscript b/service/coap-http-proxy/SConscript index cc3aa2c..1643e0d 100644 --- a/service/coap-http-proxy/SConscript +++ b/service/coap-http-proxy/SConscript @@ -43,7 +43,7 @@ src_dir = env.get('SRC_DIR') local_env.AppendUnique(CPPPATH = ['include', os.path.join(src_dir, 'resource/csdk/stack/include'), os.path.join(src_dir, 'resource/csdk/connectivity/common/inc/'), - os.path.join(src_dir, 'resource/csdk/connectivity/lib/libcoap-4.1.1'), + os.path.join(src_dir, 'resource/csdk/connectivity/lib/libcoap-4.1.1/include/coap/'), os.path.join(src_dir, 'extlibs/cjson'), ]) local_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap']) diff --git a/service/coap-http-proxy/samples/SConscript b/service/coap-http-proxy/samples/SConscript index fe165a2..963b509 100644 --- a/service/coap-http-proxy/samples/SConscript +++ b/service/coap-http-proxy/samples/SConscript @@ -48,7 +48,7 @@ if env.get('SECURED') == '1': # Source files and Targets ###################################################################### proxy_server = proxy_sample_app_env.Program('proxy_main', 'proxy_main.c') - +proxy_client = proxy_sample_app_env.Program('proxy_client', 'proxy_client.c') Alias("coap_http_proxy", [proxy_server]) env.AppendTarget('coap_http_proxy') diff --git a/service/coap-http-proxy/samples/proxy_client.c b/service/coap-http-proxy/samples/proxy_client.c new file mode 100644 index 0000000..b3ad8a9 --- /dev/null +++ b/service/coap-http-proxy/samples/proxy_client.c @@ -0,0 +1,391 @@ +//****************************************************************** +// +// Copyright 2016 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "iotivity_config.h" +#include +#include +#include +#include + +#ifdef HAVE_UNISTD_H +#include +#endif + +#include +#include "ocstack.h" +#include "logger.h" +#include "ocpayload.h" +#include "payload_logging.h" + +/** + * List of methods that can be initiated from proxy client + */ +typedef enum { + TEST_DISCOVER_REQ = 1, + TEST_GET_REQ, + TEST_POST_REQ, + TEST_PUT_REQ, + TEST_DELETE_REQ, + MAX_TESTS +} clientTests_t; + +#define TAG "PROXY_CLIENT" +#define MAX_HTTP_URI_LENGTH 1024 +// Tracking user input +static clientTests_t testCase = MAX_TESTS; +static const char* discoveryQuery = "/oic/res"; +static OCDevAddr serverAddr; +OCConnectivityType connType; + +// Will be taken as user input +static char httpResource[MAX_HTTP_URI_LENGTH]; + +int gQuitFlag = 0; +/* SIGINT handler: set gQuitFlag to 1 for graceful termination */ +void handleSigInt(int signum) +{ + if (signum == SIGINT) + { + gQuitFlag = 1; + } +} + +OCPayload* putPayload() +{ + OCRepPayload* payload = OCRepPayloadCreate(); + if (!payload) + { + OIC_LOG(INFO, TAG, "Failed to create payload object"); + return NULL; + } + + /* Set properties for your http server here. + */ + OCRepPayloadSetPropInt(payload, "power", 10); + OCRepPayloadSetPropBool(payload, "state", true); + + return (OCPayload*)payload; +} + +static void PrintUsage() +{ + OIC_LOG(INFO, TAG, "Usage : proxy_client -t <1..5>"); + OIC_LOG(INFO, TAG, "-t 1 : Discover Proxy"); + OIC_LOG(INFO, TAG, "-t 2 -p \"http_uri\": Discover Proxy and Initiate GET Request"); + OIC_LOG(INFO, TAG, "-t 3 -p \"http_uri\": Discover Proxy and Initiate POST Request"); + OIC_LOG(INFO, TAG, "-t 4 -p \"http_uri\": Discover Proxy and Initiate PUT Request"); + OIC_LOG(INFO, TAG, "-t 5 -p \"http_uri\": Discover Proxy and Initiate DELETE Request"); +} + +OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) +{ + UNUSED(ctx); + UNUSED(handle); + if (clientResponse) + { + OIC_LOG_V(INFO, TAG, "StackResult: %u", clientResponse->result); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG(INFO, TAG, ("=============> Put Response")); + } + else + { + OIC_LOG_V(INFO, TAG, "%s received Null clientResponse", __func__); + } + return OC_STACK_DELETE_TRANSACTION; +} + +OCStackApplicationResult postReqCB(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) +{ + UNUSED(ctx); + UNUSED(handle); + if (clientResponse) + { + OIC_LOG_V(INFO, TAG, "StackResult: %u", clientResponse->result); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG(INFO, TAG, ("=============> Post Response")); + } + else + { + OIC_LOG_V(INFO, TAG, "%s received Null clientResponse", __func__); + } + return OC_STACK_DELETE_TRANSACTION; +} + +OCStackApplicationResult deleteReqCB(void* ctx, + OCDoHandle handle, + OCClientResponse* clientResponse) +{ + UNUSED(ctx); + UNUSED(handle); + if (clientResponse) + { + OIC_LOG_V(INFO, TAG, "StackResult: %d", clientResponse->result); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + OIC_LOG(INFO, TAG, ("=============> Delete Response")); + } + else + { + OIC_LOG_V(INFO, TAG, "%s received Null clientResponse", __func__); + } + return OC_STACK_DELETE_TRANSACTION; +} + +OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) +{ + UNUSED(ctx); + UNUSED(handle); + if (!clientResponse) + { + OIC_LOG_V(INFO, TAG, "%s received NULL clientResponse", __func__); + return OC_STACK_DELETE_TRANSACTION; + } + + OIC_LOG_V(INFO, TAG, "StackResult: %d", 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) + { + 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) + { + 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, + ((OCHeaderOption)rcvdOptions[i]).optionLength); + } + } + } + return OC_STACK_DELETE_TRANSACTION; +} + +int InitProxyRequest() +{ + OIC_LOG(INFO, TAG, "InitProxyRequest"); + OCHeaderOption option; + memset(&option, 0, sizeof(option)); + option.protocolID = OC_COAP_ID; + option.optionID = OC_RSRVD_PROXY_OPTION_ID; + strncpy((char*)option.optionData, httpResource, sizeof(option.optionData)); + size_t opLen = strlen(httpResource); + option.optionLength = opLen < sizeof(option.optionData) ? opLen : sizeof(option.optionData); + + // A request with proxy uri shall not have resource uri + OCDoHandle handle; + OCCallbackData cbData; + cbData.context = NULL; + cbData.cd = NULL; + OCPayload* payload = NULL; + OCMethod method; + + switch(testCase) + { + case TEST_DISCOVER_REQ: + return 1; + case TEST_GET_REQ: + method = OC_REST_GET; + cbData.cb = getReqCB; + break; + case TEST_POST_REQ: + method = OC_REST_POST; + cbData.cb = postReqCB; + payload = putPayload(); + break; + case TEST_PUT_REQ: + method = OC_REST_PUT; + cbData.cb = putReqCB; + payload = putPayload(); + break; + case TEST_DELETE_REQ: + method = OC_REST_DELETE; + cbData.cb = deleteReqCB; + break; + default: + return 1; + } + + OCStackResult ret = OCDoResource(&handle, method, NULL, &serverAddr, + payload, connType, OC_LOW_QOS, &cbData, &option, 1); + if (ret != OC_STACK_OK) + { + OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method); + return 1; + } + + return 0; +} + +// This is a function called back when a device is discovered +OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, + OCClientResponse* clientResponse) +{ + UNUSED(ctx); + UNUSED(handle); + if (clientResponse) + { + OIC_LOG_V(INFO, TAG, "StackResult: %d", clientResponse->result); + OIC_LOG_V(INFO, TAG, "Discovered on %u", clientResponse->connType); + OIC_LOG_V(INFO, TAG, "Device =============> Discovered @ %s:%u", + clientResponse->devAddr.addr, + clientResponse->devAddr.port); + OIC_LOG_PAYLOAD(INFO, clientResponse->payload); + serverAddr = clientResponse->devAddr; + connType = clientResponse->connType; + OCDiscoveryPayload* payload = (OCDiscoveryPayload*) clientResponse->payload; + if (!payload) + { + // Receive other discovery responses + return OC_STACK_KEEP_TRANSACTION; + } + + int found = 0; + OCResourcePayload* resource = (OCResourcePayload*) payload->resources; + while (resource) + { + if (resource->uri && strcmp(resource->uri, OC_RSRVD_PROXY_URI) == 0) + { + found = 1; + break; + } + resource = resource->next; + } + + if (!found) + { + OIC_LOG(INFO, TAG, "No proxy in payload"); + return OC_STACK_KEEP_TRANSACTION; + } + + switch (testCase) + { + case TEST_DISCOVER_REQ: + break; + case TEST_GET_REQ: + case TEST_POST_REQ: + case TEST_PUT_REQ: + case TEST_DELETE_REQ: + InitProxyRequest(); + break; + default: + PrintUsage(); + break; + } + } + else + { + OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse"); + } + return OC_STACK_KEEP_TRANSACTION; +} + +int InitDiscovery() +{ + OCStackResult ret; + OCCallbackData cbData; + cbData.cb = discoveryReqCB; + cbData.context = NULL; + cbData.cd = NULL; + + ret = OCDoResource(NULL, OC_REST_DISCOVER, discoveryQuery, NULL, 0, CT_DEFAULT, + OC_LOW_QOS, &cbData, NULL, 0); + if (ret != OC_STACK_OK) + { + OIC_LOG_V(ERROR, TAG, "OCDoResource error [%d]", ret); + } + return ret; +} + +int main(int argc, char* argv[]) +{ + int opt; + while ((opt = getopt(argc, argv, "t:p:")) != -1) + { + switch (opt) + { + case 't': + testCase = atoi(optarg); + break; + case 'p': + if (optarg) + { + /* Copy maximum upto "sizeof(httpResource) - 1" to safeguard against + buffer overrun. Last byte is already NULL. */ + strncpy(httpResource, optarg, sizeof(httpResource) - 1); + } + break; + default: + PrintUsage(); + return -1; + } + } + + if ((testCase < TEST_DISCOVER_REQ || testCase >= MAX_TESTS) || + (testCase != TEST_DISCOVER_REQ && httpResource[0] == '\0')) + { + PrintUsage(); + return -1; + } + + if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK) + { + OIC_LOG(ERROR, TAG, "OCStack initialization error"); + return -1; + } + + if (InitDiscovery()) + { + OIC_LOG(ERROR, TAG, "Failure initiating discovery"); + return -1; + } + + // Break from loop with Ctrl+C + OIC_LOG(INFO, TAG, "Entering proxy_client main loop."); + signal(SIGINT, handleSigInt); + while (!gQuitFlag) + { + + if (OCProcess() != OC_STACK_OK) + { + OIC_LOG(ERROR, TAG, "OCStack process error"); + return -1; + } + + sleep(1); + } + + OIC_LOG(INFO, TAG, "Exiting proxy_client main loop."); + if (OCStop() != OC_STACK_OK) + { + OIC_LOG(ERROR, TAG, "OCStack stop error"); + return -1; + } + + return 0; +} +