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 following variable determines the interface (wifi, ethernet etc.)
79 //to be used for sending unicast messages. Default set to WIFI.
80 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
81 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
83 // The handle for the observe registration
84 OCDoHandle gObserveDoHandle;
85 // After this crosses a threshold client deregisters for further observations
86 int gNumObserveNotifies = 1;
89 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
90 void handleSigInt(int signum) {
91 if (signum == SIGINT) {
96 // Forward Declaration
97 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
98 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
99 int InitObserveRequest(OCClientResponse * clientResponse);
100 int InitPutRequest(OCClientResponse * clientResponse);
101 int InitGetRequest(OCClientResponse * clientResponse);
106 OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
107 OC_LOG(INFO, TAG, "-c <0|1> : Send messages over Ethernet or WIFI");
108 OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an"\
109 "available resource using default interface.");
110 OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an"\
111 "available resource using batch interface.");
112 OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an"\
113 "available resource using link list interface.");
114 OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an"\
115 "available resource using default interface.");
116 OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an"\
117 "available resource using batch interface.");
118 OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an"\
119 "available resource using link list interface.");
120 OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an"\
121 "unavailable resource using default interface.");
122 OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an"\
123 "unavailable resource using batch interface.");
124 OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an"\
125 "unavailable resource using link list interface.");
128 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
129 if(clientResponse == NULL)
131 OC_LOG(INFO, TAG, "The clientResponse is NULL");
132 return OC_STACK_DELETE_TRANSACTION;
134 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
135 OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
136 OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
139 return OC_STACK_KEEP_TRANSACTION;
142 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
143 OC_LOG_V(INFO, TAG, "StackResult: %s",
144 getResult(clientResponse->result));
145 if(ctx == (void*)DEFAULT_CONTEXT_VALUE) {
146 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
147 if(clientResponse->sequenceNumber == 0) {
148 OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
149 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
152 OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d", gNumObserveNotifies);
153 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
154 gNumObserveNotifies++;
155 if (gNumObserveNotifies == 3)
157 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
158 OC_LOG(ERROR, TAG, "Observe cancel error");
163 if(TEST == TEST_PUT_DEFAULT || TEST == TEST_PUT_BATCH || TEST == TEST_PUT_LINK_LIST)
165 InitPutRequest(clientResponse);
167 return OC_STACK_KEEP_TRANSACTION;
171 // This is a function called back when a device is discovered
172 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
173 OCClientResponse * clientResponse) {
174 uint8_t remoteIpAddr[4];
175 uint16_t remotePortNu;
178 "Entering discoveryReqCB (Application Layer CB)");
179 OC_LOG_V(INFO, TAG, "StackResult: %s",
180 getResult(clientResponse->result));
182 if (ctx == (void*) DEFAULT_CONTEXT_VALUE) {
183 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
186 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
187 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
188 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
191 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
192 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
193 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
195 if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
196 TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
198 InitGetRequestToUnavailableResource(clientResponse);
202 InitGetRequest(clientResponse);
204 return OC_STACK_KEEP_TRANSACTION;
208 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
211 OCCallbackData cbData;
213 std::ostringstream getQuery;
214 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << "/SomeUnknownResource";
215 cbData.cb = getReqCB;
216 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
219 ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
221 if (ret != OC_STACK_OK)
223 OC_LOG(ERROR, TAG, "OCStack resource error");
229 int InitObserveRequest(OCClientResponse * clientResponse)
232 OCCallbackData cbData;
234 std::ostringstream obsReg;
235 obsReg << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) << getQueryStrForGetPut(clientResponse->resJSONPayload);
236 cbData.cb = getReqCB;
237 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
239 OC_LOG_V(INFO, TAG, "OBSERVE payload from client = %s ", putPayload.c_str());
241 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_CONNTYPE,
242 OC_LOW_QOS, &cbData, NULL, 0);
243 if (ret != OC_STACK_OK)
245 OC_LOG(ERROR, TAG, "OCStack resource error");
249 gObserveDoHandle = handle;
255 int InitPutRequest(OCClientResponse * clientResponse)
258 OCCallbackData cbData;
260 //* Make a PUT query*/
261 std::ostringstream getQuery;
262 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) <<
263 "/a/room" << queryInterface[TEST].text;
264 cbData.cb = putReqCB;
265 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
267 OC_LOG_V(INFO, TAG, "PUT payload from client = %s ", putPayload.c_str());
269 ret = OCDoResource(&handle, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
270 OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0);
271 if (ret != OC_STACK_OK)
273 OC_LOG(ERROR, TAG, "OCStack resource error");
279 int InitGetRequest(OCClientResponse * clientResponse)
282 OCCallbackData cbData;
285 uint8_t remoteIpAddr[4];
286 uint16_t remotePortNu;
288 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
289 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
290 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
292 //* Make a GET query*/
293 std::ostringstream getQuery;
294 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" << getPortTBServer(clientResponse) <<
295 "/a/room" << queryInterface[TEST].text;
297 std::cout << "Get Query: " << getQuery.str() << std::endl;
299 cbData.cb = getReqCB;
300 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
302 ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
304 if (ret != OC_STACK_OK)
306 OC_LOG(ERROR, TAG, "OCStack resource error");
314 OCCallbackData cbData;
316 /* Start a discovery query*/
317 char szQueryUri[64] = { 0 };
319 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
321 cbData.cb = discoveryReqCB;
322 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
324 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
327 if (ret != OC_STACK_OK)
329 OC_LOG(ERROR, TAG, "OCStack resource error");
334 int main(int argc, char* argv[]) {
335 uint8_t addr[20] = {0};
336 uint8_t* paddr = NULL;
337 uint16_t port = USE_RANDOM_PORT;
338 uint8_t ifname[] = "eth0";
341 while ((opt = getopt(argc, argv, "t:c:")) != -1)
349 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
356 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS){
361 /*Get Ip address on defined interface and initialize coap on it with random port number
362 * this port number will be used as a source port in all coap communications*/
363 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
364 sizeof(addr)) == ERR_SUCCESS)
366 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
370 /* Initialize OCStack*/
371 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
372 OC_LOG(ERROR, TAG, "OCStack init error");
378 // Break from loop with Ctrl+C
379 OC_LOG(INFO, TAG, "Entering occlient main loop...");
380 signal(SIGINT, handleSigInt);
383 if (OCProcess() != OC_STACK_OK) {
384 OC_LOG(ERROR, TAG, "OCStack process error");
390 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
392 if (OCStop() != OC_STACK_OK) {
393 OC_LOG(ERROR, TAG, "OCStack stop error");
399 std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
400 if(!clientResponse) return "";
401 if(!clientResponse->addr) return "";
402 uint8_t a, b, c, d = 0;
403 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
405 char ipaddr[16] = {'\0'};
406 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
407 //printf("IP address string of the TB server = %s\n", *out_ipaddr);
408 return std::string (ipaddr);
412 std::string getPortTBServer(OCClientResponse * clientResponse){
413 if(!clientResponse) return "";
414 if(!clientResponse->addr) return "";
416 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
417 std::ostringstream ss;
422 std::string getQueryStrForGetPut(unsigned const char * responsePayload){
424 std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));