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 #include "occlientbasicops.h"
32 static int UNICAST_DISCOVERY = 0;
33 static int TEST_CASE = 0;
34 static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
35 static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
36 static std::string coapServerIP = "255.255.255.255";
37 static std::string coapServerPort = "5683";
38 static std::string coapServerResource = "/a/led";
42 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
43 void handleSigInt(int signum)
51 static void PrintUsage()
53 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3>");
54 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
55 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
56 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and"
57 " Initiate Nonconfirmable Get/Put/Post Requests");
58 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get/Put/Post Requests");
61 OCStackResult InvokeOCDoResource(std::ostringstream &query,
62 OCMethod method, OCQualityOfService qos,
63 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
66 OCCallbackData cbData;
70 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
73 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
74 (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload.c_str() : NULL,
75 qos, &cbData, options, numOptions);
77 if (ret != OC_STACK_OK)
79 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
85 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
87 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
89 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
94 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
95 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
97 return OC_STACK_DELETE_TRANSACTION;
100 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
102 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
104 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
109 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
110 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
111 clientResponse->resJSONPayload);
113 return OC_STACK_DELETE_TRANSACTION;
116 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
118 if(clientResponse == NULL)
120 OC_LOG(INFO, TAG, "The clientResponse is NULL");
121 return OC_STACK_DELETE_TRANSACTION;
123 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
125 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
128 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
129 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
130 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
131 clientResponse->resJSONPayload);
133 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
134 clientResponse->numRcvdVendorSpecificHeaderOptions)
136 OC_LOG (INFO, TAG, "Received vendor specific options");
138 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
139 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
141 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
143 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
144 ((OCHeaderOption)rcvdOptions[i]).optionID );
145 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
146 ((OCHeaderOption)rcvdOptions[i]).optionLength);
150 return OC_STACK_DELETE_TRANSACTION;
153 // This is a function called back when a device is discovered
154 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
155 OCClientResponse * clientResponse)
157 uint8_t remoteIpAddr[4];
158 uint16_t remotePortNu;
160 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
162 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
167 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
169 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
170 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
171 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
174 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
175 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
176 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
178 parseClientResponse(clientResponse);
182 case TEST_NON_CON_OP:
183 InitGetRequest(OC_LOW_QOS);
185 InitPostRequest(OC_LOW_QOS);
188 InitGetRequest(OC_HIGH_QOS);
190 InitPostRequest(OC_HIGH_QOS);
198 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
204 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
205 std::ostringstream query;
206 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
207 return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
210 int InitPostRequest(OCQualityOfService qos)
212 OCStackResult result;
213 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
214 std::ostringstream query;
215 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
217 // First POST operation (to create an LED instance)
218 result = InvokeOCDoResource(query, OC_REST_POST,
219 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
221 if (OC_STACK_OK != result)
223 // Error can happen if for example, network connectivity is down
224 OC_LOG(INFO, TAG, "First POST call did not succeed");
227 // Second POST operation (to create an LED instance)
228 result = InvokeOCDoResource(query, OC_REST_POST,
229 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
231 if (OC_STACK_OK != result)
233 OC_LOG(INFO, TAG, "Second POST call did not succeed");
236 // This POST operation will update the original resourced /a/led
237 return (InvokeOCDoResource(query, OC_REST_POST,
238 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
239 postReqCB, NULL, 0));
242 int InitGetRequest(OCQualityOfService qos)
244 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
245 std::ostringstream query;
246 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
248 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
249 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
255 OCCallbackData cbData;
257 /* Start a discovery query*/
258 char szQueryUri[64] = { 0 };
259 if (UNICAST_DISCOVERY)
261 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
265 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
267 cbData.cb = discoveryReqCB;
268 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
270 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
271 if (ret != OC_STACK_OK)
273 OC_LOG(ERROR, TAG, "OCStack resource error");
278 int main(int argc, char* argv[])
280 uint8_t addr[20] = {0};
281 uint8_t* paddr = NULL;
282 uint16_t port = USE_RANDOM_PORT;
283 uint8_t ifname[] = "eth0";
286 while ((opt = getopt(argc, argv, "u:t:")) != -1)
291 UNICAST_DISCOVERY = atoi(optarg);
294 TEST_CASE = atoi(optarg);
302 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
303 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
310 /*Get Ip address on defined interface and initialize coap on it with random port number
311 * this port number will be used as a source port in all coap communications*/
312 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
313 sizeof(addr)) == ERR_SUCCESS)
315 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
319 /* Initialize OCStack*/
320 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
322 OC_LOG(ERROR, TAG, "OCStack init error");
328 // Break from loop with Ctrl+C
329 OC_LOG(INFO, TAG, "Entering occlient main loop...");
330 signal(SIGINT, handleSigInt);
333 if (OCProcess() != OC_STACK_OK)
335 OC_LOG(ERROR, TAG, "OCStack process error");
341 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
343 if (OCStop() != OC_STACK_OK)
345 OC_LOG(ERROR, TAG, "OCStack stop error");
351 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
353 if(!clientResponse) return "";
354 if(!clientResponse->addr) return "";
355 uint8_t a, b, c, d = 0;
356 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
358 char ipaddr[16] = {'\0'};
359 // ostringstream not working correctly here, hence snprintf
360 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
361 return std::string (ipaddr);
365 std::string getPortTBServer(OCClientResponse * clientResponse)
367 if(!clientResponse) return "";
368 if(!clientResponse->addr) return "";
370 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
371 std::ostringstream ss;
376 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
381 void parseClientResponse(OCClientResponse * clientResponse)
383 coapServerIP = getIPAddrTBServer(clientResponse);
384 coapServerPort = getPortTBServer(clientResponse);
385 coapServerResource = getQueryStrForGetPut(clientResponse);