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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 const char *getResult(OCStackResult result);
31 std::string getIPAddrTBServer(OCClientResponse * clientResponse);
32 std::string getPortTBServer(OCClientResponse * clientResponse);
33 std::string getQueryStrForGetPut(const char * responsePayload);
35 #define TAG PCF("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_CONNECTIVITY_TYPE;
66 unsigned static int TEST = TEST_INVALID;
67 unsigned static int CONNECTIVITY = 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},
88 static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
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);
120 OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
121 OC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
122 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
123 OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
124 "available resource using default interface.");
125 OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
126 "available resource using batch interface.");
127 OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
128 "available resource using link list interface.");
129 OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
130 "available resource using default interface.");
131 OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
132 "available resource using batch interface.");
133 OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
134 "available resource using link list interface.");
135 OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
136 "unavailable resource using default interface.");
137 OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
138 "unavailable resource using batch interface.");
139 OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
140 "unavailable resource using link list interface.");
143 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
145 if(clientResponse == NULL)
147 OC_LOG(INFO, TAG, "The clientResponse is NULL");
148 return OC_STACK_DELETE_TRANSACTION;
150 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
152 OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
153 OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
156 return OC_STACK_KEEP_TRANSACTION;
159 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
161 OC_LOG_V(INFO, TAG, "StackResult: %s",
162 getResult(clientResponse->result));
163 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
165 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
166 if(clientResponse->sequenceNumber == 0)
168 OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
169 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
173 OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
174 gNumObserveNotifies);
175 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
176 gNumObserveNotifies++;
177 if (gNumObserveNotifies == 3)
179 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
181 OC_LOG(ERROR, TAG, "Observe cancel error");
186 if(TEST == TEST_PUT_DEFAULT || TEST == TEST_PUT_BATCH || TEST == TEST_PUT_LINK_LIST)
188 InitPutRequest(clientResponse);
190 return OC_STACK_KEEP_TRANSACTION;
194 // This is a function called back when a device is discovered
195 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
196 OCClientResponse * clientResponse)
199 "Entering discoveryReqCB (Application Layer CB)");
200 OC_LOG_V(INFO, TAG, "StackResult: %s",
201 getResult(clientResponse->result));
203 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
205 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
209 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
210 clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
212 if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
213 TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
215 InitGetRequestToUnavailableResource(clientResponse);
219 InitGetRequest(clientResponse);
221 return OC_STACK_KEEP_TRANSACTION;
225 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
228 OCCallbackData cbData;
229 std::ostringstream getQuery;
230 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
231 clientResponse->devAddr.port << "/SomeUnknownResource";
232 cbData.cb = getReqCB;
233 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
236 ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
238 if (ret != OC_STACK_OK)
240 OC_LOG(ERROR, TAG, "OCStack resource error");
246 int InitObserveRequest(OCClientResponse * clientResponse)
249 OCCallbackData cbData;
251 std::ostringstream obsReg;
252 obsReg << "coap://" << clientResponse->devAddr.addr << ":" <<
253 clientResponse->devAddr.addr <<
254 getQueryStrForGetPut(clientResponse->resJSONPayload);
255 cbData.cb = getReqCB;
256 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
258 OC_LOG_V(INFO, TAG, "OBSERVE payload from client = %s ", putPayload.c_str());
260 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_CONNTYPE,
261 OC_LOW_QOS, &cbData, NULL, 0);
262 if (ret != OC_STACK_OK)
264 OC_LOG(ERROR, TAG, "OCStack resource error");
268 gObserveDoHandle = handle;
274 int InitPutRequest(OCClientResponse * clientResponse)
277 OCCallbackData cbData;
278 //* Make a PUT query*/
279 std::ostringstream getQuery;
280 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
281 clientResponse->devAddr.port <<
282 "/a/room" << queryInterface[TEST].text;
283 cbData.cb = putReqCB;
284 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
286 OC_LOG_V(INFO, TAG, "PUT payload from client = %s ", putPayload.c_str());
288 ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
289 OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0);
290 if (ret != OC_STACK_OK)
292 OC_LOG(ERROR, TAG, "OCStack resource error");
298 int InitGetRequest(OCClientResponse * clientResponse)
301 OCCallbackData cbData;
303 //* Make a GET query*/
304 std::ostringstream getQuery;
305 getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
306 clientResponse->devAddr.port <<
307 "/a/room" << queryInterface[TEST].text;
309 std::cout << "Get Query: " << getQuery.str() << std::endl;
311 cbData.cb = getReqCB;
312 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
314 ret = OCDoResource(NULL, OC_REST_GET,
315 getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
317 if (ret != OC_STACK_OK)
319 OC_LOG(ERROR, TAG, "OCStack resource error");
327 OCCallbackData cbData;
328 /* Start a discovery query*/
329 char szQueryUri[64] = { 0 };
331 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
333 cbData.cb = discoveryReqCB;
334 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
336 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
339 if (ret != OC_STACK_OK)
341 OC_LOG(ERROR, TAG, "OCStack resource error");
346 int main(int argc, char* argv[])
350 while ((opt = getopt(argc, argv, "t:c:")) != -1)
358 CONNECTIVITY = atoi(optarg);
365 if ((TEST <= TEST_INVALID || TEST >= MAX_TESTS) ||
366 (CONNECTIVITY < CT_ADAPTER_DEFAULT || CONNECTIVITY >= MAX_CT))
372 /* Initialize OCStack*/
373 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
375 OC_LOG(ERROR, TAG, "OCStack init error");
379 if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IP)
381 OC_CONNTYPE = CT_ADAPTER_IP;
385 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
386 OC_CONNTYPE = CT_ADAPTER_IP;
391 // Break from loop with Ctrl+C
392 OC_LOG(INFO, TAG, "Entering occlient main loop...");
393 signal(SIGINT, handleSigInt);
397 if (OCProcess() != OC_STACK_OK)
399 OC_LOG(ERROR, TAG, "OCStack process error");
404 } OC_LOG(INFO, TAG, "Exiting occlient main loop...");
406 if (OCStop() != OC_STACK_OK)
408 OC_LOG(ERROR, TAG, "OCStack stop error");
414 std::string getQueryStrForGetPut(const char * responsePayload)
417 std::string jsonPayload(responsePayload);