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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
29 #include "ocpayload.h"
30 #include "payload_logging.h"
32 const char *getResult(OCStackResult result);
33 std::string getQueryStrForGetPut();
35 #define TAG ("occlient")
36 #define DEFAULT_CONTEXT_VALUE 0x99
37 #ifndef MAX_LENGTH_IPv4_ADDR
38 #define MAX_LENGTH_IPv4_ADDR 16
50 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
51 TEST_UNKNOWN_RESOURCE_GET_BATCH,
52 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
57 * List of connectivity types that can be initiated from the client
58 * Required for user input validation
61 CT_ADAPTER_DEFAULT = 0,
64 } CLIENT_ConnectivityType_TYPE;
66 unsigned static int TestType = TEST_INVALID;
67 unsigned static int ConnectivityType = 0;
75 testToTextMap queryInterface[] = {
76 {"invalid", TEST_INVALID},
77 {"?if=oic.if.baseline", TEST_GET_DEFAULT},
78 {"?if=oic.if.b", TEST_GET_BATCH},
79 {"?if=oic.if.ll", TEST_GET_LINK_LIST},
80 {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
81 {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
82 {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
83 {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
84 {"?if=oic.if.b", TEST_PUT_BATCH},
85 {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
89 //The following variable determines the interface protocol (IP, etc)
90 //to be used for sending unicast messages. Default set to IP.
91 static OCConnectivityType ConnType = CT_ADAPTER_IP;
92 static const char * RESOURCE_DISCOVERY_QUERY = "/oic/res";
94 // The handle for the observe registration
95 OCDoHandle gObserveDoHandle;
96 // After this crosses a threshold client deregisters for further observations
97 int gNumObserveNotifies = 1;
100 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
101 void handleSigInt(int signum)
103 if (signum == SIGINT)
109 // Forward Declaration
110 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
111 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
112 int InitObserveRequest(OCClientResponse * clientResponse);
113 int InitPutRequest(OCClientResponse * clientResponse);
114 int InitGetRequest(OCClientResponse * clientResponse);
117 OCPayload* putPayload()
119 OCRepPayload* payload = OCRepPayloadCreate();
123 std::cout << "Failed to create put payload object"<<std::endl;
127 OCRepPayloadSetPropInt(payload, "power", 15);
128 OCRepPayloadSetPropBool(payload, "state", true);
130 return (OCPayload*) payload;
135 OIC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
136 OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
137 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
138 OIC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
139 "available resource using default interface.");
140 OIC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
141 "available resource using batch interface.");
142 OIC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
143 "available resource using link list interface.");
144 OIC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
145 "available resource using default interface.");
146 OIC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
147 "available resource using batch interface.");
148 OIC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
149 "available resource using link list interface.");
150 OIC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
151 "unavailable resource using default interface.");
152 OIC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
153 "unavailable resource using batch interface.");
154 OIC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
155 "unavailable resource using link list interface.");
158 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
159 OCClientResponse * clientResponse)
161 if(clientResponse == NULL)
163 OIC_LOG(INFO, TAG, "The clientResponse is NULL");
164 return OC_STACK_DELETE_TRANSACTION;
166 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
168 OIC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
169 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
172 return OC_STACK_KEEP_TRANSACTION;
175 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
176 OCClientResponse * clientResponse)
178 OIC_LOG_V(INFO, TAG, "StackResult: %s",
179 getResult(clientResponse->result));
180 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
182 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
183 if(clientResponse->sequenceNumber == 0)
185 OIC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
186 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
190 OIC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
191 gNumObserveNotifies);
192 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);;
193 gNumObserveNotifies++;
194 if (gNumObserveNotifies == 3)
196 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
198 OIC_LOG(ERROR, TAG, "Observe cancel error");
203 if(TestType == TEST_PUT_DEFAULT || TestType == TEST_PUT_BATCH || TestType == TEST_PUT_LINK_LIST)
205 InitPutRequest(clientResponse);
207 return OC_STACK_KEEP_TRANSACTION;
210 // This is a function called back when a device is discovered
211 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
212 OCClientResponse * clientResponse)
215 "Entering discoveryReqCB (Application Layer CB)");
216 OIC_LOG_V(INFO, TAG, "StackResult: %s",
217 getResult(clientResponse->result));
219 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
221 OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
225 "Device =============> Discovered @ %s:%d",
226 clientResponse->devAddr.addr,
227 clientResponse->devAddr.port);
228 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
230 ConnType = clientResponse->connType;
232 if(TestType == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TestType == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
233 TestType == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
235 InitGetRequestToUnavailableResource(clientResponse);
239 InitGetRequest(clientResponse);
241 return OC_STACK_KEEP_TRANSACTION;
245 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
248 OCCallbackData cbData;
249 std::ostringstream getQuery;
250 getQuery << "/SomeUnknownResource";
251 cbData.cb = getReqCB;
252 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
255 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
256 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
258 if (ret != OC_STACK_OK)
260 OIC_LOG(ERROR, TAG, "OCStack resource error");
266 int InitObserveRequest(OCClientResponse * clientResponse)
269 OCCallbackData cbData;
271 std::ostringstream obsReg;
272 obsReg << getQueryStrForGetPut();
273 cbData.cb = getReqCB;
274 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
276 OIC_LOG_V(INFO, TAG, "OBSERVE payload from client =");
277 OCPayload* payload = putPayload();
278 OIC_LOG_PAYLOAD(INFO, payload);
279 OCPayloadDestroy(payload);
281 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(),
282 &clientResponse->devAddr, 0, ConnType,
283 OC_LOW_QOS, &cbData, NULL, 0);
284 if (ret != OC_STACK_OK)
286 OIC_LOG(ERROR, TAG, "OCStack resource error");
290 gObserveDoHandle = handle;
296 int InitPutRequest(OCClientResponse * clientResponse)
299 OCCallbackData cbData;
300 //* Make a PUT query*/
301 std::ostringstream getQuery;
302 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
303 clientResponse->devAddr.port <<
304 "/a/room" << queryInterface[TestType].text;
305 cbData.cb = putReqCB;
306 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
308 OIC_LOG_V(INFO, TAG, "PUT payload from client = ");
309 OCPayload* payload = putPayload();
310 OIC_LOG_PAYLOAD(INFO, payload);
311 OCPayloadDestroy(payload);
313 ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(),
314 &clientResponse->devAddr, putPayload(), ConnType,
315 OC_LOW_QOS, &cbData, NULL, 0);
316 if (ret != OC_STACK_OK)
318 OIC_LOG(ERROR, TAG, "OCStack resource error");
324 int InitGetRequest(OCClientResponse * clientResponse)
327 OCCallbackData cbData;
329 //* Make a GET query*/
330 std::ostringstream getQuery;
331 getQuery << "/a/room" << queryInterface[TestType].text;
333 std::cout << "Get Query: " << getQuery.str() << std::endl;
335 cbData.cb = getReqCB;
336 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
338 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(),
339 &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS,
341 if (ret != OC_STACK_OK)
343 OIC_LOG(ERROR, TAG, "OCStack resource error");
351 OCCallbackData cbData;
352 /* Start a discovery query*/
353 char szQueryUri[64] = { 0 };
355 strcpy(szQueryUri, RESOURCE_DISCOVERY_QUERY);
357 cbData.cb = discoveryReqCB;
358 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
360 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, ConnType,
363 if (ret != OC_STACK_OK)
365 OIC_LOG(ERROR, TAG, "OCStack resource error");
370 int main(int argc, char* argv[])
374 while ((opt = getopt(argc, argv, "t:c:")) != -1)
379 TestType = atoi(optarg);
382 ConnectivityType = atoi(optarg);
389 if ((TestType <= TEST_INVALID || TestType >= MAX_TESTS) ||
390 ConnectivityType >= MAX_CT)
396 /* Initialize OCStack*/
397 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
399 OIC_LOG(ERROR, TAG, "OCStack init error");
403 if(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP)
405 ConnType = CT_ADAPTER_IP;
409 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
410 ConnType = CT_ADAPTER_IP;
415 // Break from loop with Ctrl+C
416 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
417 signal(SIGINT, handleSigInt);
421 if (OCProcess() != OC_STACK_OK)
423 OIC_LOG(ERROR, TAG, "OCStack process error");
428 } OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
430 if (OCStop() != OC_STACK_OK)
432 OIC_LOG(ERROR, TAG, "OCStack stop error");
438 std::string getQueryStrForGetPut()