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"
31 const char *getResult(OCStackResult result);
32 std::string getIPAddrTBServer(OCClientResponse * clientResponse);
33 std::string getPortTBServer(OCClientResponse * clientResponse);
34 std::string getQueryStrForGetPut();
36 #define TAG PCF("occlient")
37 #define DEFAULT_CONTEXT_VALUE 0x99
38 #ifndef MAX_LENGTH_IPv4_ADDR
39 #define MAX_LENGTH_IPv4_ADDR 16
51 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
52 TEST_UNKNOWN_RESOURCE_GET_BATCH,
53 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
58 * List of connectivity types that can be initiated from the client
59 * Required for user input validation
62 CT_ADAPTER_DEFAULT = 0,
65 } CLIENT_CONNECTIVITY_TYPE;
67 unsigned static int TEST = TEST_INVALID;
68 unsigned static int CONNECTIVITY = 0;
76 testToTextMap queryInterface[] = {
77 {"invalid", TEST_INVALID},
78 {"?if=oic.if.baseline", TEST_GET_DEFAULT},
79 {"?if=oic.if.b", TEST_GET_BATCH},
80 {"?if=oic.if.ll", TEST_GET_LINK_LIST},
81 {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
82 {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
83 {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
84 {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
85 {"?if=oic.if.b", TEST_PUT_BATCH},
86 {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
90 //The following variable determines the interface protocol (IP, etc)
91 //to be used for sending unicast messages. Default set to IP.
92 static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
93 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
95 // The handle for the observe registration
96 OCDoHandle gObserveDoHandle;
97 // After this crosses a threshold client deregisters for further observations
98 int gNumObserveNotifies = 1;
101 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
102 void handleSigInt(int signum)
104 if (signum == SIGINT)
110 // Forward Declaration
111 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
112 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
113 int InitObserveRequest(OCClientResponse * clientResponse);
114 int InitPutRequest(OCClientResponse * clientResponse);
115 int InitGetRequest(OCClientResponse * clientResponse);
118 OCPayload* putPayload()
120 OCRepPayload* payload = OCRepPayloadCreate();
124 std::cout << "Failed to create put payload object"<<std::endl;
128 OCRepPayloadSetPropInt(payload, "power", 15);
129 OCRepPayloadSetPropBool(payload, "state", true);
131 return (OCPayload*) payload;
136 OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
137 OC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
138 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
139 OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
140 "available resource using default interface.");
141 OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
142 "available resource using batch interface.");
143 OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
144 "available resource using link list interface.");
145 OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
146 "available resource using default interface.");
147 OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
148 "available resource using batch interface.");
149 OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
150 "available resource using link list interface.");
151 OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
152 "unavailable resource using default interface.");
153 OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
154 "unavailable resource using batch interface.");
155 OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
156 "unavailable resource using link list interface.");
159 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
160 OCClientResponse * clientResponse)
162 if(clientResponse == NULL)
164 OC_LOG(INFO, TAG, "The clientResponse is NULL");
165 return OC_STACK_DELETE_TRANSACTION;
167 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
169 OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
170 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
173 return OC_STACK_KEEP_TRANSACTION;
176 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
177 OCClientResponse * clientResponse)
179 OC_LOG_V(INFO, TAG, "StackResult: %s",
180 getResult(clientResponse->result));
181 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
183 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
184 if(clientResponse->sequenceNumber == 0)
186 OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
187 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
191 OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
192 gNumObserveNotifies);
193 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);;
194 gNumObserveNotifies++;
195 if (gNumObserveNotifies == 3)
197 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
199 OC_LOG(ERROR, TAG, "Observe cancel error");
204 if(TEST == TEST_PUT_DEFAULT || TEST == TEST_PUT_BATCH || TEST == TEST_PUT_LINK_LIST)
206 InitPutRequest(clientResponse);
208 return OC_STACK_KEEP_TRANSACTION;
212 // This is a function called back when a device is discovered
213 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
214 OCClientResponse * clientResponse)
217 "Entering discoveryReqCB (Application Layer CB)");
218 OC_LOG_V(INFO, TAG, "StackResult: %s",
219 getResult(clientResponse->result));
221 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
223 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
227 "Device =============> Discovered @ %s:%d",
228 clientResponse->devAddr.addr,
229 clientResponse->devAddr.port);
230 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
232 OC_CONNTYPE = clientResponse->connType;
234 if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
235 TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
237 InitGetRequestToUnavailableResource(clientResponse);
241 InitGetRequest(clientResponse);
243 return OC_STACK_KEEP_TRANSACTION;
247 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
250 OCCallbackData cbData;
251 std::ostringstream getQuery;
252 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
253 clientResponse->devAddr.port << "/SomeUnknownResource";
254 cbData.cb = getReqCB;
255 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
258 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
260 if (ret != OC_STACK_OK)
262 OC_LOG(ERROR, TAG, "OCStack resource error");
268 int InitObserveRequest(OCClientResponse * clientResponse)
271 OCCallbackData cbData;
273 std::ostringstream obsReg;
274 obsReg << "coap://" << clientResponse->devAddr.addr << ":" <<
275 clientResponse->devAddr.addr <<
276 getQueryStrForGetPut();
277 cbData.cb = getReqCB;
278 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
280 OC_LOG_V(INFO, TAG, "OBSERVE payload from client =");
281 OCPayload* payload = putPayload();
282 OC_LOG_PAYLOAD(INFO, TAG, payload);
283 OCPayloadDestroy(payload);
285 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_CONNTYPE,
286 OC_LOW_QOS, &cbData, NULL, 0);
287 if (ret != OC_STACK_OK)
289 OC_LOG(ERROR, TAG, "OCStack resource error");
293 gObserveDoHandle = handle;
299 int InitPutRequest(OCClientResponse * clientResponse)
302 OCCallbackData cbData;
303 //* Make a PUT query*/
304 std::ostringstream getQuery;
305 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
306 clientResponse->devAddr.port <<
307 "/a/room" << queryInterface[TEST].text;
308 cbData.cb = putReqCB;
309 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
311 OC_LOG_V(INFO, TAG, "PUT payload from client = ");
312 OCPayload* payload = putPayload();
313 OC_LOG_PAYLOAD(INFO, TAG, payload);
314 OCPayloadDestroy(payload);
316 ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload(),
317 OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0);
318 if (ret != OC_STACK_OK)
320 OC_LOG(ERROR, TAG, "OCStack resource error");
326 int InitGetRequest(OCClientResponse * clientResponse)
329 OCCallbackData cbData;
331 //* Make a GET query*/
332 std::ostringstream getQuery;
333 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
334 clientResponse->devAddr.port <<
335 "/a/room" << queryInterface[TEST].text;
337 std::cout << "Get Query: " << getQuery.str() << std::endl;
339 cbData.cb = getReqCB;
340 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
342 ret = OCDoResource(NULL, OC_REST_GET,
343 getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
345 if (ret != OC_STACK_OK)
347 OC_LOG(ERROR, TAG, "OCStack resource error");
355 OCCallbackData cbData;
356 /* Start a discovery query*/
357 char szQueryUri[64] = { 0 };
359 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
361 cbData.cb = discoveryReqCB;
362 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
364 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
367 if (ret != OC_STACK_OK)
369 OC_LOG(ERROR, TAG, "OCStack resource error");
374 int main(int argc, char* argv[])
378 while ((opt = getopt(argc, argv, "t:c:")) != -1)
386 CONNECTIVITY = atoi(optarg);
393 if ((TEST <= TEST_INVALID || TEST >= MAX_TESTS) ||
394 CONNECTIVITY >= MAX_CT)
400 /* Initialize OCStack*/
401 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
403 OC_LOG(ERROR, TAG, "OCStack init error");
407 if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IP)
409 OC_CONNTYPE = CT_ADAPTER_IP;
413 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
414 OC_CONNTYPE = CT_ADAPTER_IP;
419 // Break from loop with Ctrl+C
420 OC_LOG(INFO, TAG, "Entering occlient main loop...");
421 signal(SIGINT, handleSigInt);
425 if (OCProcess() != OC_STACK_OK)
427 OC_LOG(ERROR, TAG, "OCStack process error");
432 } OC_LOG(INFO, TAG, "Exiting occlient main loop...");
434 if (OCStop() != OC_STACK_OK)
436 OC_LOG(ERROR, TAG, "OCStack stop error");
442 std::string getQueryStrForGetPut()