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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
35 #include "ocpayload.h"
36 #include "payload_logging.h"
39 std::string getQueryStrForGetPut();
41 #define TAG ("occlient")
42 #define DEFAULT_CONTEXT_VALUE 0x99
43 #ifndef MAX_LENGTH_IPv4_ADDR
44 #define MAX_LENGTH_IPv4_ADDR 16
56 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
57 TEST_UNKNOWN_RESOURCE_GET_BATCH,
58 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
63 * List of connectivity types that can be initiated from the client
64 * Required for user input validation
67 CT_ADAPTER_DEFAULT = 0,
70 } CLIENT_ConnectivityType_TYPE;
72 unsigned static int TestType = TEST_INVALID;
73 unsigned static int ConnectivityType = 0;
81 testToTextMap queryInterface[] = {
82 {"invalid", TEST_INVALID},
83 {"?if=oic.if.baseline", TEST_GET_DEFAULT},
84 {"?if=oic.if.b", TEST_GET_BATCH},
85 {"?if=oic.if.ll", TEST_GET_LINK_LIST},
86 {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
87 {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
88 {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
89 {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
90 {"?if=oic.if.b", TEST_PUT_BATCH},
91 {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
95 //The following variable determines the interface protocol (IP, etc)
96 //to be used for sending unicast messages. Default set to IP.
97 static OCConnectivityType ConnType = CT_ADAPTER_IP;
98 static const char * RESOURCE_DISCOVERY_QUERY = "/oic/res";
100 // The handle for the observe registration
101 OCDoHandle gObserveDoHandle;
102 // After this crosses a threshold client deregisters for further observations
103 int gNumObserveNotifies = 1;
106 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
107 void handleSigInt(int signum)
109 if (signum == SIGINT)
115 // Forward Declaration
116 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
117 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
118 int InitObserveRequest(OCClientResponse * clientResponse);
119 int InitPutRequest(OCClientResponse * clientResponse);
120 int InitGetRequest(OCClientResponse * clientResponse);
123 OCPayload* putPayload()
125 OCRepPayload* payload = OCRepPayloadCreate();
129 std::cout << "Failed to create put payload object"<<std::endl;
133 OCRepPayloadSetPropInt(payload, "power", 15);
134 OCRepPayloadSetPropBool(payload, "state", true);
136 return (OCPayload*) payload;
141 OIC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
142 OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
143 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
144 OIC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
145 "available resource using default interface.");
146 OIC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
147 "available resource using batch interface.");
148 OIC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
149 "available resource using link list interface.");
150 OIC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
151 "available resource using default interface.");
152 OIC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
153 "available resource using batch interface.");
154 OIC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
155 "available resource using link list interface.");
156 OIC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
157 "unavailable resource using default interface.");
158 OIC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
159 "unavailable resource using batch interface.");
160 OIC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
161 "unavailable resource using link list interface.");
164 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
165 OCClientResponse * clientResponse)
167 if(clientResponse == NULL)
169 OIC_LOG(INFO, TAG, "The clientResponse is NULL");
170 return OC_STACK_DELETE_TRANSACTION;
172 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
174 OIC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
175 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
178 return OC_STACK_KEEP_TRANSACTION;
181 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
182 OCClientResponse * clientResponse)
184 OIC_LOG_V(INFO, TAG, "StackResult: %s",
185 getResult(clientResponse->result));
186 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
188 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
189 if(clientResponse->sequenceNumber == 0)
191 OIC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
192 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
196 OIC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
197 gNumObserveNotifies);
198 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);;
199 gNumObserveNotifies++;
200 if (gNumObserveNotifies == 3)
202 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
204 OIC_LOG(ERROR, TAG, "Observe cancel error");
209 if(TestType == TEST_PUT_DEFAULT || TestType == TEST_PUT_BATCH || TestType == TEST_PUT_LINK_LIST)
211 InitPutRequest(clientResponse);
213 return OC_STACK_KEEP_TRANSACTION;
216 // This is a function called back when a device is discovered
217 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
218 OCClientResponse * clientResponse)
221 "Entering discoveryReqCB (Application Layer CB)");
222 OIC_LOG_V(INFO, TAG, "StackResult: %s",
223 getResult(clientResponse->result));
225 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
227 OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
231 "Device =============> Discovered @ %s:%d",
232 clientResponse->devAddr.addr,
233 clientResponse->devAddr.port);
234 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
236 ConnType = clientResponse->connType;
238 if(TestType == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TestType == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
239 TestType == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
241 InitGetRequestToUnavailableResource(clientResponse);
245 InitGetRequest(clientResponse);
247 return OC_STACK_KEEP_TRANSACTION;
251 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
254 OCCallbackData cbData;
255 std::ostringstream getQuery;
256 getQuery << "/SomeUnknownResource";
257 cbData.cb = getReqCB;
258 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
261 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
262 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
264 if (ret != OC_STACK_OK)
266 OIC_LOG(ERROR, TAG, "OCStack resource error");
272 int InitObserveRequest(OCClientResponse * clientResponse)
275 OCCallbackData cbData;
277 std::ostringstream obsReg;
278 obsReg << getQueryStrForGetPut();
279 cbData.cb = getReqCB;
280 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
282 OIC_LOG_V(INFO, TAG, "OBSERVE payload from client =");
283 OCPayload* payload = putPayload();
284 OIC_LOG_PAYLOAD(INFO, payload);
285 OCPayloadDestroy(payload);
287 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(),
288 &clientResponse->devAddr, 0, ConnType,
289 OC_LOW_QOS, &cbData, NULL, 0);
290 if (ret != OC_STACK_OK)
292 OIC_LOG(ERROR, TAG, "OCStack resource error");
296 gObserveDoHandle = handle;
302 int InitPutRequest(OCClientResponse * clientResponse)
305 OCCallbackData cbData;
306 //* Make a PUT query*/
307 std::ostringstream getQuery;
308 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
309 clientResponse->devAddr.port <<
310 "/a/room" << queryInterface[TestType].text;
311 cbData.cb = putReqCB;
312 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
314 OIC_LOG_V(INFO, TAG, "PUT payload from client = ");
315 OCPayload* payload = putPayload();
316 OIC_LOG_PAYLOAD(INFO, payload);
317 OCPayloadDestroy(payload);
319 ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(),
320 &clientResponse->devAddr, putPayload(), ConnType,
321 OC_LOW_QOS, &cbData, NULL, 0);
322 if (ret != OC_STACK_OK)
324 OIC_LOG(ERROR, TAG, "OCStack resource error");
330 int InitGetRequest(OCClientResponse * clientResponse)
333 OCCallbackData cbData;
335 //* Make a GET query*/
336 std::ostringstream getQuery;
337 getQuery << "/a/room" << queryInterface[TestType].text;
339 std::cout << "Get Query: " << getQuery.str() << std::endl;
341 cbData.cb = getReqCB;
342 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
344 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
345 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
347 if (ret != OC_STACK_OK)
349 OIC_LOG(ERROR, TAG, "OCStack resource error");
357 OCCallbackData cbData;
358 /* Start a discovery query*/
359 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
361 strcpy(szQueryUri, RESOURCE_DISCOVERY_QUERY);
363 cbData.cb = discoveryReqCB;
364 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
366 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, ConnType,
369 if (ret != OC_STACK_OK)
371 OIC_LOG(ERROR, TAG, "OCStack resource error");
376 int main(int argc, char* argv[])
380 while ((opt = getopt(argc, argv, "t:c:")) != -1)
385 TestType = atoi(optarg);
388 ConnectivityType = atoi(optarg);
395 if ((TestType <= TEST_INVALID || TestType >= MAX_TESTS) ||
396 ConnectivityType >= MAX_CT)
402 /* Initialize OCStack*/
403 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
405 OIC_LOG(ERROR, TAG, "OCStack init error");
409 if(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP)
411 ConnType = CT_ADAPTER_IP;
415 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
416 ConnType = CT_ADAPTER_IP;
421 // Break from loop with Ctrl+C
422 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
423 signal(SIGINT, handleSigInt);
427 if (OCProcess() != OC_STACK_OK)
429 OIC_LOG(ERROR, TAG, "OCStack process error");
434 } OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
436 if (OCStop() != OC_STACK_OK)
438 OIC_LOG(ERROR, TAG, "OCStack stop error");
444 std::string getQueryStrForGetPut()