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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
34 #include "ocpayload.h"
35 #include "payload_logging.h"
37 const char *getResult(OCStackResult result);
38 std::string getQueryStrForGetPut();
40 #define TAG ("occlient")
41 #define DEFAULT_CONTEXT_VALUE 0x99
42 #ifndef MAX_LENGTH_IPv4_ADDR
43 #define MAX_LENGTH_IPv4_ADDR 16
55 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
56 TEST_UNKNOWN_RESOURCE_GET_BATCH,
57 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
62 * List of connectivity types that can be initiated from the client
63 * Required for user input validation
66 CT_ADAPTER_DEFAULT = 0,
69 } CLIENT_ConnectivityType_TYPE;
71 unsigned static int TestType = TEST_INVALID;
72 unsigned static int ConnectivityType = 0;
80 testToTextMap queryInterface[] = {
81 {"invalid", TEST_INVALID},
82 {"?if=oic.if.baseline", TEST_GET_DEFAULT},
83 {"?if=oic.if.b", TEST_GET_BATCH},
84 {"?if=oic.if.ll", TEST_GET_LINK_LIST},
85 {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
86 {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
87 {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
88 {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
89 {"?if=oic.if.b", TEST_PUT_BATCH},
90 {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
94 //The following variable determines the interface protocol (IP, etc)
95 //to be used for sending unicast messages. Default set to IP.
96 static OCConnectivityType ConnType = CT_ADAPTER_IP;
97 static const char * RESOURCE_DISCOVERY_QUERY = "/oic/res";
99 // The handle for the observe registration
100 OCDoHandle gObserveDoHandle;
101 // After this crosses a threshold client deregisters for further observations
102 int gNumObserveNotifies = 1;
105 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
106 void handleSigInt(int signum)
108 if (signum == SIGINT)
114 // Forward Declaration
115 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
116 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
117 int InitObserveRequest(OCClientResponse * clientResponse);
118 int InitPutRequest(OCClientResponse * clientResponse);
119 int InitGetRequest(OCClientResponse * clientResponse);
122 OCPayload* putPayload()
124 OCRepPayload* payload = OCRepPayloadCreate();
128 std::cout << "Failed to create put payload object"<<std::endl;
132 OCRepPayloadSetPropInt(payload, "power", 15);
133 OCRepPayloadSetPropBool(payload, "state", true);
135 return (OCPayload*) payload;
140 OIC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
141 OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
142 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
143 OIC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
144 "available resource using default interface.");
145 OIC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
146 "available resource using batch interface.");
147 OIC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
148 "available resource using link list interface.");
149 OIC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
150 "available resource using default interface.");
151 OIC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
152 "available resource using batch interface.");
153 OIC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
154 "available resource using link list interface.");
155 OIC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
156 "unavailable resource using default interface.");
157 OIC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
158 "unavailable resource using batch interface.");
159 OIC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
160 "unavailable resource using link list interface.");
163 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
164 OCClientResponse * clientResponse)
166 if(clientResponse == NULL)
168 OIC_LOG(INFO, TAG, "The clientResponse is NULL");
169 return OC_STACK_DELETE_TRANSACTION;
171 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
173 OIC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
174 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
177 return OC_STACK_KEEP_TRANSACTION;
180 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
181 OCClientResponse * clientResponse)
183 OIC_LOG_V(INFO, TAG, "StackResult: %s",
184 getResult(clientResponse->result));
185 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
187 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
188 if(clientResponse->sequenceNumber == 0)
190 OIC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
191 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
195 OIC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
196 gNumObserveNotifies);
197 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);;
198 gNumObserveNotifies++;
199 if (gNumObserveNotifies == 3)
201 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
203 OIC_LOG(ERROR, TAG, "Observe cancel error");
208 if(TestType == TEST_PUT_DEFAULT || TestType == TEST_PUT_BATCH || TestType == TEST_PUT_LINK_LIST)
210 InitPutRequest(clientResponse);
212 return OC_STACK_KEEP_TRANSACTION;
215 // This is a function called back when a device is discovered
216 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
217 OCClientResponse * clientResponse)
220 "Entering discoveryReqCB (Application Layer CB)");
221 OIC_LOG_V(INFO, TAG, "StackResult: %s",
222 getResult(clientResponse->result));
224 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
226 OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
230 "Device =============> Discovered @ %s:%d",
231 clientResponse->devAddr.addr,
232 clientResponse->devAddr.port);
233 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
235 ConnType = clientResponse->connType;
237 if(TestType == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TestType == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
238 TestType == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
240 InitGetRequestToUnavailableResource(clientResponse);
244 InitGetRequest(clientResponse);
246 return OC_STACK_KEEP_TRANSACTION;
250 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
253 OCCallbackData cbData;
254 std::ostringstream getQuery;
255 getQuery << "/SomeUnknownResource";
256 cbData.cb = getReqCB;
257 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
260 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
261 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
263 if (ret != OC_STACK_OK)
265 OIC_LOG(ERROR, TAG, "OCStack resource error");
271 int InitObserveRequest(OCClientResponse * clientResponse)
274 OCCallbackData cbData;
276 std::ostringstream obsReg;
277 obsReg << getQueryStrForGetPut();
278 cbData.cb = getReqCB;
279 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
281 OIC_LOG_V(INFO, TAG, "OBSERVE payload from client =");
282 OCPayload* payload = putPayload();
283 OIC_LOG_PAYLOAD(INFO, payload);
284 OCPayloadDestroy(payload);
286 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(),
287 &clientResponse->devAddr, 0, ConnType,
288 OC_LOW_QOS, &cbData, NULL, 0);
289 if (ret != OC_STACK_OK)
291 OIC_LOG(ERROR, TAG, "OCStack resource error");
295 gObserveDoHandle = handle;
301 int InitPutRequest(OCClientResponse * clientResponse)
304 OCCallbackData cbData;
305 //* Make a PUT query*/
306 std::ostringstream getQuery;
307 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
308 clientResponse->devAddr.port <<
309 "/a/room" << queryInterface[TestType].text;
310 cbData.cb = putReqCB;
311 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
313 OIC_LOG_V(INFO, TAG, "PUT payload from client = ");
314 OCPayload* payload = putPayload();
315 OIC_LOG_PAYLOAD(INFO, payload);
316 OCPayloadDestroy(payload);
318 ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(),
319 &clientResponse->devAddr, putPayload(), ConnType,
320 OC_LOW_QOS, &cbData, NULL, 0);
321 if (ret != OC_STACK_OK)
323 OIC_LOG(ERROR, TAG, "OCStack resource error");
329 int InitGetRequest(OCClientResponse * clientResponse)
332 OCCallbackData cbData;
334 //* Make a GET query*/
335 std::ostringstream getQuery;
336 getQuery << "/a/room" << queryInterface[TestType].text;
338 std::cout << "Get Query: " << getQuery.str() << std::endl;
340 cbData.cb = getReqCB;
341 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
343 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
344 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
346 if (ret != OC_STACK_OK)
348 OIC_LOG(ERROR, TAG, "OCStack resource error");
356 OCCallbackData cbData;
357 /* Start a discovery query*/
358 char szQueryUri[64] = { 0 };
360 strcpy(szQueryUri, RESOURCE_DISCOVERY_QUERY);
362 cbData.cb = discoveryReqCB;
363 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
365 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, ConnType,
368 if (ret != OC_STACK_OK)
370 OIC_LOG(ERROR, TAG, "OCStack resource error");
375 int main(int argc, char* argv[])
379 while ((opt = getopt(argc, argv, "t:c:")) != -1)
384 TestType = atoi(optarg);
387 ConnectivityType = atoi(optarg);
394 if ((TestType <= TEST_INVALID || TestType >= MAX_TESTS) ||
395 ConnectivityType >= MAX_CT)
401 /* Initialize OCStack*/
402 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
404 OIC_LOG(ERROR, TAG, "OCStack init error");
408 if(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP)
410 ConnType = CT_ADAPTER_IP;
414 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
415 ConnType = CT_ADAPTER_IP;
420 // Break from loop with Ctrl+C
421 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
422 signal(SIGINT, handleSigInt);
426 if (OCProcess() != OC_STACK_OK)
428 OIC_LOG(ERROR, TAG, "OCStack process error");
433 } OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
435 if (OCStop() != OC_STACK_OK)
437 OIC_LOG(ERROR, TAG, "OCStack stop error");
443 std::string getQueryStrForGetPut()