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(unsigned 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
49 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
50 TEST_UNKNOWN_RESOURCE_GET_BATCH,
51 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
55 unsigned static int TEST = TEST_INVALID;
59 unsigned char text[30];
63 testToTextMap queryInterface[] = {
64 {"invalid", TEST_INVALID},
65 {"?if=oc.mi.def", TEST_GET_DEFAULT},
66 {"?if=oc.mi.b", TEST_GET_BATCH},
67 {"?if=oc.mi.ll", TEST_GET_LINK_LIST},
68 {"?if=oc.mi.def", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
69 {"?if=oc.mi.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
70 {"?if=oc.mi.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
71 {"?if=oc.mi.def", TEST_PUT_DEFAULT},
72 {"?if=oc.mi.b", TEST_PUT_BATCH},
73 {"?if=oc.mi.ll", TEST_PUT_LINK_LIST},
76 static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
78 // The handle for the observe registration
79 OCDoHandle gObserveDoHandle;
80 // After this crosses a threshold client deregisters for further observations
81 int gNumObserveNotifies = 1;
84 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
85 void handleSigInt(int signum) {
86 if (signum == SIGINT) {
91 // Forward Declaration
92 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
93 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
94 int InitObserveRequest(OCClientResponse * clientResponse);
95 int InitPutRequest(OCClientResponse * clientResponse);
96 int InitGetRequest(OCClientResponse * clientResponse);
101 OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case>");
102 OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an"\
103 "available resource using default interface.");
104 OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an"\
105 "available resource using batch interface.");
106 OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an"\
107 "available resource using link list interface.");
108 OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an"\
109 "available resource using default interface.");
110 OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an"\
111 "available resource using batch interface.");
112 OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an"\
113 "available resource using link list interface.");
114 OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an"\
115 "unavailable resource using default interface.");
116 OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an"\
117 "unavailable resource using batch interface.");
118 OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an"\
119 "unavailable resource using link list interface.");
122 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
123 if(clientResponse == NULL)
125 OC_LOG(INFO, TAG, "The clientResponse is NULL");
126 return OC_STACK_DELETE_TRANSACTION;
128 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
129 OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
130 OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
133 return OC_STACK_KEEP_TRANSACTION;
136 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
137 OC_LOG_V(INFO, TAG, "StackResult: %s",
138 getResult(clientResponse->result));
139 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
140 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
141 if(clientResponse->sequenceNumber == 0) {
142 OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
143 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
146 OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d", gNumObserveNotifies);
147 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
148 gNumObserveNotifies++;
149 if (gNumObserveNotifies == 3)
151 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
152 OC_LOG(ERROR, TAG, "Observe cancel error");
157 if(TEST == TEST_PUT_DEFAULT || TEST == TEST_PUT_BATCH || TEST == TEST_PUT_LINK_LIST)
159 InitPutRequest(clientResponse);
161 return OC_STACK_KEEP_TRANSACTION;
165 // This is a function called back when a device is discovered
166 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
167 OCClientResponse * clientResponse) {
168 uint8_t remoteIpAddr[4];
169 uint16_t remotePortNu;
172 "Entering discoveryReqCB (Application Layer CB)");
173 OC_LOG_V(INFO, TAG, "StackResult: %s",
174 getResult(clientResponse->result));
176 if (ctx == (void*) DEFAULT_CONTEXT_VALUE) {
177 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
180 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
181 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
182 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
185 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
186 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
187 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
189 if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
190 TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
192 InitGetRequestToUnavailableResource(clientResponse);
196 InitGetRequest(clientResponse);
198 return OC_STACK_KEEP_TRANSACTION;
202 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
205 OCCallbackData cbData;
207 std::ostringstream getQuery;
208 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << "/SomeUnknownResource";
209 cbData.cb = getReqCB;
210 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
213 ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_LOW_QOS,
215 if (ret != OC_STACK_OK)
217 OC_LOG(ERROR, TAG, "OCStack resource error");
223 int InitObserveRequest(OCClientResponse * clientResponse)
226 OCCallbackData cbData;
228 std::ostringstream obsReg;
229 obsReg << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << getQueryStrForGetPut(clientResponse->resJSONPayload);
230 cbData.cb = getReqCB;
231 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
233 OC_LOG_V(INFO, TAG, "OBSERVE payload from client = %s ", putPayload.c_str());
235 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_LOW_QOS,
237 if (ret != OC_STACK_OK)
239 OC_LOG(ERROR, TAG, "OCStack resource error");
243 gObserveDoHandle = handle;
249 int InitPutRequest(OCClientResponse * clientResponse)
252 OCCallbackData cbData;
254 //* Make a PUT query*/
255 std::ostringstream getQuery;
256 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) <<
257 "/a/room" << queryInterface[TEST].text;
258 cbData.cb = putReqCB;
259 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
261 OC_LOG_V(INFO, TAG, "PUT payload from client = %s ", putPayload.c_str());
263 ret = OCDoResource(&handle, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
264 OC_LOW_QOS, &cbData, NULL, 0);
265 if (ret != OC_STACK_OK)
267 OC_LOG(ERROR, TAG, "OCStack resource error");
273 int InitGetRequest(OCClientResponse * clientResponse)
276 OCCallbackData cbData;
279 uint8_t remoteIpAddr[4];
280 uint16_t remotePortNu;
282 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
283 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
284 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
286 //* Make a GET query*/
287 std::ostringstream getQuery;
288 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) <<
289 "/a/room" << queryInterface[TEST].text;
291 std::cout << "Get Query: " << getQuery.str() << std::endl;
293 cbData.cb = getReqCB;
294 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
296 ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_LOW_QOS,
298 if (ret != OC_STACK_OK)
300 OC_LOG(ERROR, TAG, "OCStack resource error");
308 OCCallbackData cbData;
310 /* Start a discovery query*/
311 char szQueryUri[64] = { 0 };
313 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
315 cbData.cb = discoveryReqCB;
316 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
318 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS,
320 if (ret != OC_STACK_OK)
322 OC_LOG(ERROR, TAG, "OCStack resource error");
327 int main(int argc, char* argv[]) {
328 uint8_t addr[20] = {0};
329 uint8_t* paddr = NULL;
330 uint16_t port = USE_RANDOM_PORT;
331 uint8_t ifname[] = "eth0";
334 while ((opt = getopt(argc, argv, "t:")) != -1)
346 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS){
351 /*Get Ip address on defined interface and initialize coap on it with random port number
352 * this port number will be used as a source port in all coap communications*/
353 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
354 sizeof(addr)) == ERR_SUCCESS)
356 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
360 /* Initialize OCStack*/
361 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
362 OC_LOG(ERROR, TAG, "OCStack init error");
368 // Break from loop with Ctrl+C
369 OC_LOG(INFO, TAG, "Entering occlient main loop...");
370 signal(SIGINT, handleSigInt);
373 if (OCProcess() != OC_STACK_OK) {
374 OC_LOG(ERROR, TAG, "OCStack process error");
380 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
382 if (OCStop() != OC_STACK_OK) {
383 OC_LOG(ERROR, TAG, "OCStack stop error");
389 std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
390 if(!clientResponse) return "";
391 if(!clientResponse->addr) return "";
392 uint8_t a, b, c, d = 0;
393 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
395 char ipaddr[16] = {'\0'};
396 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
397 //printf("IP address string of the TB server = %s\n", *out_ipaddr);
398 return std::string (ipaddr);
402 std::string getPortTBServer(OCClientResponse * clientResponse){
403 if(!clientResponse) return "";
404 if(!clientResponse->addr) return "";
406 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
407 std::ostringstream ss;
412 std::string getQueryStrForGetPut(unsigned const char * responsePayload){
414 std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));