1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "iotivity_config.h"
36 #include "ocpayload.h"
37 #include "payload_logging.h"
40 #include "oic_string.h"
42 std::string getQueryStrForGetPut();
44 #define TAG ("occlient")
45 #define DEFAULT_CONTEXT_VALUE 0x99
46 #ifndef MAX_LENGTH_IPv4_ADDR
47 #define MAX_LENGTH_IPv4_ADDR 16
59 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
60 TEST_UNKNOWN_RESOURCE_GET_BATCH,
61 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
68 * List of connectivity types that can be initiated from the client
69 * Required for user input validation
72 CT_ADAPTER_DEFAULT = 0,
75 } CLIENT_ConnectivityType_TYPE;
77 unsigned static int TestType = TEST_INVALID;
78 unsigned static int ConnectivityType = 0;
86 testToTextMap queryInterface[] = {
87 {"invalid", TEST_INVALID},
88 {"?if=oic.if.baseline", TEST_GET_DEFAULT},
89 {"?if=oic.if.b", TEST_GET_BATCH},
90 {"?if=oic.if.ll", TEST_GET_LINK_LIST},
91 {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
92 {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
93 {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
94 {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
95 {"?if=oic.if.b", TEST_PUT_BATCH},
96 {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
98 {NULL, TEST_GET_NULL},
102 //The following variable determines the interface protocol (IP, etc)
103 //to be used for sending unicast messages. Default set to IP.
104 static OCConnectivityType ConnType = CT_ADAPTER_IP;
105 static const char * RESOURCE_DISCOVERY_QUERY = "/oic/res";
107 // The handle for the observe registration
108 OCDoHandle gObserveDoHandle;
109 // After this crosses a threshold client deregisters for further observations
110 int gNumObserveNotifies = 1;
113 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
114 void handleSigInt(int signum)
116 if (signum == SIGINT)
122 // Forward Declaration
123 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
124 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
125 int InitObserveRequest(OCClientResponse * clientResponse);
126 int InitPutRequest(OCClientResponse * clientResponse);
127 int InitGetRequest(OCClientResponse * clientResponse);
130 OCPayload* putPayload()
132 OCRepPayload* payload = OCRepPayloadCreate();
136 std::cout << "Failed to create put payload object"<<std::endl;
140 OCRepPayloadSetPropInt(payload, "power", 15);
141 OCRepPayloadSetPropBool(payload, "state", true);
143 return (OCPayload*) payload;
148 OIC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
149 OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
150 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
151 OIC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
152 "available resource using default interface.");
153 OIC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
154 "available resource using batch interface.");
155 OIC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
156 "available resource using link list interface.");
157 OIC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
158 "available resource using default interface.");
159 OIC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
160 "available resource using batch interface.");
161 OIC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
162 "available resource using link list interface.");
163 OIC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
164 "unavailable resource using default interface.");
165 OIC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
166 "unavailable resource using batch interface.");
167 OIC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
168 "unavailable resource using link list interface.");
169 OIC_LOG(INFO, TAG, "Test Case 10 : Discover Resources && Initiate GET Request on an "\
171 OIC_LOG(INFO, TAG, "Test Case 11 : Discover Resources && Initiate GET Request on an "\
176 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
177 OCClientResponse * clientResponse)
179 if(clientResponse == NULL)
181 OIC_LOG(INFO, TAG, "The clientResponse is NULL");
182 return OC_STACK_DELETE_TRANSACTION;
184 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
186 OIC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
187 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
190 return OC_STACK_KEEP_TRANSACTION;
193 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
194 OCClientResponse * clientResponse)
196 OIC_LOG_V(INFO, TAG, "StackResult: %s",
197 getResult(clientResponse->result));
198 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
200 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
201 if(clientResponse->sequenceNumber == 0)
203 OIC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
204 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
208 OIC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
209 gNumObserveNotifies);
210 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);;
211 gNumObserveNotifies++;
212 if (gNumObserveNotifies == 3)
214 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
216 OIC_LOG(ERROR, TAG, "Observe cancel error");
221 if(TestType == TEST_PUT_DEFAULT || TestType == TEST_PUT_BATCH || TestType == TEST_PUT_LINK_LIST)
223 InitPutRequest(clientResponse);
225 return OC_STACK_KEEP_TRANSACTION;
228 // This is a function called back when a device is discovered
229 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
230 OCClientResponse * clientResponse)
233 "Entering discoveryReqCB (Application Layer CB)");
234 OIC_LOG_V(INFO, TAG, "StackResult: %s",
235 getResult(clientResponse->result));
237 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
239 OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
243 "Device =============> Discovered @ %s:%d",
244 clientResponse->devAddr.addr,
245 clientResponse->devAddr.port);
246 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
248 ConnType = clientResponse->connType;
250 if(TestType == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TestType == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
251 TestType == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
253 InitGetRequestToUnavailableResource(clientResponse);
257 InitGetRequest(clientResponse);
259 return OC_STACK_KEEP_TRANSACTION;
263 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
266 OCCallbackData cbData;
267 std::ostringstream getQuery;
268 getQuery << "/SomeUnknownResource";
269 cbData.cb = getReqCB;
270 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
273 ret = OCDoRequest(NULL, OC_REST_GET, getQuery.str().c_str(),
274 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
276 if (ret != OC_STACK_OK)
278 OIC_LOG(ERROR, TAG, "OCStack resource error");
284 int InitObserveRequest(OCClientResponse * clientResponse)
287 OCCallbackData cbData;
289 std::ostringstream obsReg;
290 obsReg << getQueryStrForGetPut();
291 cbData.cb = getReqCB;
292 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
294 OIC_LOG_V(INFO, TAG, "OBSERVE payload from client =");
295 OCPayload* payload = putPayload();
296 OIC_LOG_PAYLOAD(INFO, payload);
297 OCPayloadDestroy(payload);
299 ret = OCDoRequest(&handle, OC_REST_OBSERVE, obsReg.str().c_str(),
300 &clientResponse->devAddr, 0, ConnType,
301 OC_LOW_QOS, &cbData, NULL, 0);
302 if (ret != OC_STACK_OK)
304 OIC_LOG(ERROR, TAG, "OCStack resource error");
308 gObserveDoHandle = handle;
314 int InitPutRequest(OCClientResponse * clientResponse)
317 OCCallbackData cbData;
318 //* Make a PUT query*/
319 std::ostringstream getQuery;
320 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
321 clientResponse->devAddr.port <<
322 "/a/room" << queryInterface[TestType].text;
323 cbData.cb = putReqCB;
324 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
326 OIC_LOG_V(INFO, TAG, "PUT payload from client = ");
327 OCPayload* payload = putPayload();
328 OIC_LOG_PAYLOAD(INFO, payload);
330 ret = OCDoRequest(NULL, OC_REST_PUT, getQuery.str().c_str(),
331 &clientResponse->devAddr, payload, ConnType,
332 OC_LOW_QOS, &cbData, NULL, 0);
334 OCPayloadDestroy(payload);
336 if (ret != OC_STACK_OK)
338 OIC_LOG(ERROR, TAG, "OCStack resource error");
344 int InitGetRequest(OCClientResponse * clientResponse)
347 OCCallbackData cbData;
349 //* Make a GET query*/
350 std::ostringstream getQuery;
351 getQuery << "/a/room" << queryInterface[TestType].text;
353 std::cout << "Get Query: " << getQuery.str() << std::endl;
355 cbData.cb = getReqCB;
356 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
358 ret = OCDoRequest(NULL, OC_REST_GET, getQuery.str().c_str(),
359 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
361 if (ret != OC_STACK_OK)
363 OIC_LOG(ERROR, TAG, "OCStack resource error");
371 OCCallbackData cbData;
372 /* Start a discovery query*/
373 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
375 OICStrcpy(szQueryUri, sizeof(szQueryUri), RESOURCE_DISCOVERY_QUERY);
377 cbData.cb = discoveryReqCB;
378 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
380 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, ConnType,
383 if (ret != OC_STACK_OK)
385 OIC_LOG(ERROR, TAG, "OCStack resource error");
390 int main(int argc, char* argv[])
394 while ((opt = getopt(argc, argv, "t:c:")) != -1)
399 TestType = atoi(optarg);
402 ConnectivityType = atoi(optarg);
409 if ((TestType <= TEST_INVALID || TestType >= MAX_TESTS) ||
410 ConnectivityType >= MAX_CT)
416 /* Initialize OCStack*/
417 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
419 OIC_LOG(ERROR, TAG, "OCStack init error");
423 if(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP)
425 ConnType = CT_ADAPTER_IP;
429 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
430 ConnType = CT_ADAPTER_IP;
435 // Break from loop with Ctrl+C
436 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
437 signal(SIGINT, handleSigInt);
441 if (OCProcess() != OC_STACK_OK)
443 OIC_LOG(ERROR, TAG, "OCStack process error");
448 } OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
450 if (OCStop() != OC_STACK_OK)
452 OIC_LOG(ERROR, TAG, "OCStack stop error");
458 std::string getQueryStrForGetPut()