Merge "Changing Malloc call in CA OCDoResource." into connectivity-abstraction
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / occlientslow.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <iostream>
27 #include <sstream>
28 #include "ocstack.h"
29 #include "logger.h"
30 #include "occlientslow.h"
31
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";
39
40 #ifdef CA_INT
41 //The following variable determines the interface (wifi, ethernet etc.)
42 //to be used for sending unicast messages. Default set to WIFI.
43 static OCConnectivityType CA_CONNTYPE = OC_WIFI;
44 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
45 #endif
46
47 int gQuitFlag = 0;
48
49 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
50 void handleSigInt(int signum)
51 {
52     if (signum == SIGINT)
53     {
54         gQuitFlag = 1;
55     }
56 }
57
58 static void PrintUsage()
59 {
60 #ifdef CA_INT
61     OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3> -c <0|1|2|3>");
62     OC_LOG(INFO, TAG, "-c <0|1|2|3> : Send unicast messages over Ethernet, WIFI, EDR or LE");
63 #else
64     OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3>");
65 #endif
66     OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
67     OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
68     OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
69     OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
70 }
71
72 OCStackResult InvokeOCDoResource(std::ostringstream &query,
73         OCMethod method, OCQualityOfService qos,
74         OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
75 {
76     OCStackResult ret;
77     OCCallbackData cbData;
78     OCDoHandle handle;
79
80     cbData.cb = cb;
81     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
82     cbData.cd = NULL;
83
84 #ifdef CA_INT
85     ret = OCDoResource(&handle, method, query.str().c_str(), 0,
86             NULL, CA_CONNTYPE, qos, &cbData, options, numOptions);
87 #else
88     ret = OCDoResource(&handle, method, query.str().c_str(), 0,
89             NULL, qos, &cbData, options, numOptions);
90 #endif
91
92     if (ret != OC_STACK_OK)
93     {
94         OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
95     }
96
97     return ret;
98 }
99
100 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
101 {
102     if(clientResponse == NULL)
103     {
104         OC_LOG(INFO, TAG, "The clientResponse is NULL");
105         return   OC_STACK_DELETE_TRANSACTION;
106     }
107     if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
108     {
109         OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
110     }
111
112     OC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
113     OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
114     OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
115             clientResponse->resJSONPayload);
116
117     if(clientResponse->rcvdVendorSpecificHeaderOptions &&
118             clientResponse->numRcvdVendorSpecificHeaderOptions)
119     {
120         OC_LOG (INFO, TAG, "Received vendor specific options");
121         uint8_t i = 0;
122         OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
123         for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
124         {
125             if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
126             {
127                 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
128                         ((OCHeaderOption)rcvdOptions[i]).optionID );
129                 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
130                         ((OCHeaderOption)rcvdOptions[i]).optionLength);
131             }
132         }
133     }
134     return OC_STACK_DELETE_TRANSACTION;
135 }
136
137 // This is a function called back when a device is discovered
138 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
139         OCClientResponse * clientResponse)
140 {
141     uint8_t remoteIpAddr[4];
142     uint16_t remotePortNu;
143
144     if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
145     {
146         OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
147     }
148
149     if (clientResponse)
150     {
151         OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
152
153         OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
154                 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
155         OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
156
157         OC_LOG_V(INFO, TAG,
158                 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
159                 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
160                 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
161
162         parseClientResponse(clientResponse);
163
164         switch(TEST_CASE)
165         {
166             case TEST_NON_CON_OP:
167                 InitGetRequest(OC_LOW_QOS);
168                 break;
169             case TEST_CON_OP:
170                 InitGetRequest(OC_HIGH_QOS);
171                 break;
172             default:
173                 PrintUsage();
174                 break;
175         }
176     }
177
178     return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
179
180 }
181
182 int InitGetRequest(OCQualityOfService qos)
183 {
184     OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
185     std::ostringstream query;
186     query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
187
188     return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
189             OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
190 }
191
192 int InitDiscovery()
193 {
194     OCStackResult ret;
195     OCCallbackData cbData;
196     OCDoHandle handle;
197     /* Start a discovery query*/
198     char szQueryUri[64] = { 0 };
199     if (UNICAST_DISCOVERY)
200     {
201         strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
202     }
203     else
204     {
205 #ifdef CA_INT
206         strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
207 #else
208         strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
209 #endif
210     }
211     cbData.cb = discoveryReqCB;
212     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
213     cbData.cd = NULL;
214 #ifdef CA_INT
215     if(UNICAST_DISCOVERY)
216     {
217         ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, CA_CONNTYPE,
218                 OC_LOW_QOS, &cbData, NULL, 0);
219     }
220     else
221     {
222         ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
223                 OC_LOW_QOS, &cbData, NULL, 0);
224     }
225 #else
226     ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
227 #endif
228     if (ret != OC_STACK_OK)
229     {
230         OC_LOG(ERROR, TAG, "OCStack resource error");
231     }
232     return ret;
233 }
234
235 int main(int argc, char* argv[])
236 {
237     uint8_t addr[20] = {0};
238     uint8_t* paddr = NULL;
239     uint16_t port = USE_RANDOM_PORT;
240     uint8_t ifname[] = "eth0";
241     int opt;
242
243 #ifdef CA_INT
244     while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
245 #else
246     while ((opt = getopt(argc, argv, "u:t:")) != -1)
247 #endif
248     {
249         switch(opt)
250         {
251             case 'u':
252                 UNICAST_DISCOVERY = atoi(optarg);
253                 break;
254             case 't':
255                 TEST_CASE = atoi(optarg);
256                 break;
257             #ifdef CA_INT
258             case 'c':
259                 CA_CONNTYPE = OCConnectivityType(atoi(optarg));
260                 break;
261             #endif
262             default:
263                 PrintUsage();
264                 return -1;
265         }
266     }
267
268     if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
269             (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
270     {
271         PrintUsage();
272         return -1;
273     }
274
275
276     /*Get Ip address on defined interface and initialize coap on it with random port number
277      * this port number will be used as a source port in all coap communications*/
278     if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
279                 sizeof(addr)) == ERR_SUCCESS)
280     {
281         OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
282         paddr = addr;
283     }
284
285     /* Initialize OCStack*/
286     if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
287     {
288         OC_LOG(ERROR, TAG, "OCStack init error");
289         return 0;
290     }
291
292     InitDiscovery();
293
294     // Break from loop with Ctrl+C
295     OC_LOG(INFO, TAG, "Entering occlient main loop...");
296     signal(SIGINT, handleSigInt);
297     while (!gQuitFlag)
298     {
299         if (OCProcess() != OC_STACK_OK)
300         {
301             OC_LOG(ERROR, TAG, "OCStack process error");
302             return 0;
303         }
304
305         sleep(2);
306     }
307     OC_LOG(INFO, TAG, "Exiting occlient main loop...");
308
309     if (OCStop() != OC_STACK_OK)
310     {
311         OC_LOG(ERROR, TAG, "OCStack stop error");
312     }
313
314     return 0;
315 }
316
317 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
318 {
319     if(!clientResponse) return "";
320     if(!clientResponse->addr) return "";
321     uint8_t a, b, c, d = 0;
322     if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
323
324     char ipaddr[16] = {'\0'};
325     // ostringstream not working correctly here, hence snprintf
326     snprintf(ipaddr,  sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
327     return std::string (ipaddr);
328 }
329
330
331 std::string getPortTBServer(OCClientResponse * clientResponse)
332 {
333     if(!clientResponse) return "";
334     if(!clientResponse->addr) return "";
335     uint16_t p = 0;
336     if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
337     std::ostringstream ss;
338     ss << p;
339     return ss.str();
340 }
341
342 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
343 {
344     return "/a/led";
345 }
346
347 void parseClientResponse(OCClientResponse * clientResponse)
348 {
349     coapServerIP = getIPAddrTBServer(clientResponse);
350     coapServerPort = getPortTBServer(clientResponse);
351     coapServerResource = getQueryStrForGetPut(clientResponse);
352 }