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 std::string getQueryStrForGetPut();
42 #define TAG ("occlient")
43 #define DEFAULT_CONTEXT_VALUE 0x99
44 #ifndef MAX_LENGTH_IPv4_ADDR
45 #define MAX_LENGTH_IPv4_ADDR 16
57 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
58 TEST_UNKNOWN_RESOURCE_GET_BATCH,
59 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
64 * List of connectivity types that can be initiated from the client
65 * Required for user input validation
68 CT_ADAPTER_DEFAULT = 0,
71 } CLIENT_ConnectivityType_TYPE;
73 unsigned static int TestType = TEST_INVALID;
74 unsigned static int ConnectivityType = 0;
82 testToTextMap queryInterface[] = {
83 {"invalid", TEST_INVALID},
84 {"?if=oic.if.baseline", TEST_GET_DEFAULT},
85 {"?if=oic.if.b", TEST_GET_BATCH},
86 {"?if=oic.if.ll", TEST_GET_LINK_LIST},
87 {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
88 {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
89 {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
90 {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
91 {"?if=oic.if.b", TEST_PUT_BATCH},
92 {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
96 //The following variable determines the interface protocol (IP, etc)
97 //to be used for sending unicast messages. Default set to IP.
98 static OCConnectivityType ConnType = CT_ADAPTER_IP;
99 static const char * RESOURCE_DISCOVERY_QUERY = "/oic/res";
101 // The handle for the observe registration
102 OCDoHandle gObserveDoHandle;
103 // After this crosses a threshold client deregisters for further observations
104 int gNumObserveNotifies = 1;
107 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
108 void handleSigInt(int signum)
110 if (signum == SIGINT)
116 // Forward Declaration
117 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
118 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
119 int InitObserveRequest(OCClientResponse * clientResponse);
120 int InitPutRequest(OCClientResponse * clientResponse);
121 int InitGetRequest(OCClientResponse * clientResponse);
124 OCPayload* putPayload()
126 OCRepPayload* payload = OCRepPayloadCreate();
130 std::cout << "Failed to create put payload object"<<std::endl;
134 OCRepPayloadSetPropInt(payload, "power", 15);
135 OCRepPayloadSetPropBool(payload, "state", true);
137 return (OCPayload*) payload;
142 OIC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
143 OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
144 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
145 OIC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
146 "available resource using default interface.");
147 OIC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
148 "available resource using batch interface.");
149 OIC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
150 "available resource using link list interface.");
151 OIC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
152 "available resource using default interface.");
153 OIC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
154 "available resource using batch interface.");
155 OIC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
156 "available resource using link list interface.");
157 OIC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
158 "unavailable resource using default interface.");
159 OIC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
160 "unavailable resource using batch interface.");
161 OIC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
162 "unavailable resource using link list interface.");
165 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
166 OCClientResponse * clientResponse)
168 if(clientResponse == NULL)
170 OIC_LOG(INFO, TAG, "The clientResponse is NULL");
171 return OC_STACK_DELETE_TRANSACTION;
173 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
175 OIC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
176 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
179 return OC_STACK_KEEP_TRANSACTION;
182 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
183 OCClientResponse * clientResponse)
185 OIC_LOG_V(INFO, TAG, "StackResult: %s",
186 getResult(clientResponse->result));
187 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
189 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
190 if(clientResponse->sequenceNumber == 0)
192 OIC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
193 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
197 OIC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
198 gNumObserveNotifies);
199 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);;
200 gNumObserveNotifies++;
201 if (gNumObserveNotifies == 3)
203 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
205 OIC_LOG(ERROR, TAG, "Observe cancel error");
210 if(TestType == TEST_PUT_DEFAULT || TestType == TEST_PUT_BATCH || TestType == TEST_PUT_LINK_LIST)
212 InitPutRequest(clientResponse);
214 return OC_STACK_KEEP_TRANSACTION;
217 // This is a function called back when a device is discovered
218 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
219 OCClientResponse * clientResponse)
222 "Entering discoveryReqCB (Application Layer CB)");
223 OIC_LOG_V(INFO, TAG, "StackResult: %s",
224 getResult(clientResponse->result));
226 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
228 OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
232 "Device =============> Discovered @ %s:%d",
233 clientResponse->devAddr.addr,
234 clientResponse->devAddr.port);
235 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
237 ConnType = clientResponse->connType;
239 if(TestType == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TestType == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
240 TestType == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
242 InitGetRequestToUnavailableResource(clientResponse);
246 InitGetRequest(clientResponse);
248 return OC_STACK_KEEP_TRANSACTION;
252 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
255 OCCallbackData cbData;
256 std::ostringstream getQuery;
257 getQuery << "/SomeUnknownResource";
258 cbData.cb = getReqCB;
259 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
262 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
263 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
265 if (ret != OC_STACK_OK)
267 OIC_LOG(ERROR, TAG, "OCStack resource error");
273 int InitObserveRequest(OCClientResponse * clientResponse)
276 OCCallbackData cbData;
278 std::ostringstream obsReg;
279 obsReg << getQueryStrForGetPut();
280 cbData.cb = getReqCB;
281 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
283 OIC_LOG_V(INFO, TAG, "OBSERVE payload from client =");
284 OCPayload* payload = putPayload();
285 OIC_LOG_PAYLOAD(INFO, payload);
286 OCPayloadDestroy(payload);
288 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(),
289 &clientResponse->devAddr, 0, ConnType,
290 OC_LOW_QOS, &cbData, NULL, 0);
291 if (ret != OC_STACK_OK)
293 OIC_LOG(ERROR, TAG, "OCStack resource error");
297 gObserveDoHandle = handle;
303 int InitPutRequest(OCClientResponse * clientResponse)
306 OCCallbackData cbData;
307 //* Make a PUT query*/
308 std::ostringstream getQuery;
309 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
310 clientResponse->devAddr.port <<
311 "/a/room" << queryInterface[TestType].text;
312 cbData.cb = putReqCB;
313 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
315 OIC_LOG_V(INFO, TAG, "PUT payload from client = ");
316 OCPayload* payload = putPayload();
317 OIC_LOG_PAYLOAD(INFO, payload);
318 OCPayloadDestroy(payload);
320 ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(),
321 &clientResponse->devAddr, putPayload(), ConnType,
322 OC_LOW_QOS, &cbData, NULL, 0);
323 if (ret != OC_STACK_OK)
325 OIC_LOG(ERROR, TAG, "OCStack resource error");
331 int InitGetRequest(OCClientResponse * clientResponse)
334 OCCallbackData cbData;
336 //* Make a GET query*/
337 std::ostringstream getQuery;
338 getQuery << "/a/room" << queryInterface[TestType].text;
340 std::cout << "Get Query: " << getQuery.str() << std::endl;
342 cbData.cb = getReqCB;
343 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
345 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
346 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
348 if (ret != OC_STACK_OK)
350 OIC_LOG(ERROR, TAG, "OCStack resource error");
358 OCCallbackData cbData;
359 /* Start a discovery query*/
360 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
362 strcpy(szQueryUri, RESOURCE_DISCOVERY_QUERY);
364 cbData.cb = discoveryReqCB;
365 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
367 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, ConnType,
370 if (ret != OC_STACK_OK)
372 OIC_LOG(ERROR, TAG, "OCStack resource error");
377 int main(int argc, char* argv[])
381 while ((opt = getopt(argc, argv, "t:c:")) != -1)
386 TestType = atoi(optarg);
389 ConnectivityType = atoi(optarg);
396 if ((TestType <= TEST_INVALID || TestType >= MAX_TESTS) ||
397 ConnectivityType >= MAX_CT)
403 /* Initialize OCStack*/
404 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
406 OIC_LOG(ERROR, TAG, "OCStack init error");
410 if(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP)
412 ConnType = CT_ADAPTER_IP;
416 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
417 ConnType = CT_ADAPTER_IP;
422 // Break from loop with Ctrl+C
423 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
424 signal(SIGINT, handleSigInt);
428 if (OCProcess() != OC_STACK_OK)
430 OIC_LOG(ERROR, TAG, "OCStack process error");
435 } OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
437 if (OCStop() != OC_STACK_OK)
439 OIC_LOG(ERROR, TAG, "OCStack stop error");
445 std::string getQueryStrForGetPut()