iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / linux / sample_main.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics 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 "cacommon.h"
25 #include "cainterface.h"
26
27 #define MAX_BUF_LEN 1024
28
29 char get_menu();
30 void process();
31
32 void start_listening_server();
33 void start_discovery_server();
34 void find_resource();
35 void send_request();
36 void send_response();
37 void advertise_resource();
38 void select_network();
39 void unselect_network();
40 void handle_request_response();
41
42 void request_handler(CARemoteEndpoint_t* object, CARequestInfo_t* requestInfo);
43 void response_handler(CARemoteEndpoint_t* object, CAResponseInfo_t* responseInfo);
44 void send_request_tmp(CARemoteEndpoint_t* endpoint, CAToken_t token);
45
46 int main()
47 {
48     system("clear");
49
50     printf("=============================================\n");
51     printf("\t\tsample main\n");
52     printf("=============================================\n");
53
54     CAInitialize();
55
56     // network enable
57     // default
58     printf("select default network(WIFI)\n");
59     CASelectNetwork(CA_WIFI);
60
61     // set handler.
62     CARegisterHandler(request_handler, response_handler);
63
64     process();
65
66     CATerminate();
67
68     return 0;
69 }
70
71 void process()
72 {
73     while (1)
74     {
75         char menu = get_menu();
76
77         switch (menu)
78         {
79             case 'm': // menu
80             case 'M':
81                 continue;
82
83             case 'q': // quit
84             case 'Q':
85                 printf("quit..!!\n");
86                 return;
87
88             case 's': // start listening server
89             case 'S':
90                 start_listening_server();
91                 break;
92
93             case 'd': // start discovery server
94             case 'D':
95                 start_discovery_server();
96                 break;
97
98             case 'f': // find resource
99             case 'F':
100                 find_resource();
101                 break;
102
103             case 'r': // send request
104             case 'R':
105                 send_request();
106                 break;
107
108             case 'a': // advertise resource
109             case 'A':
110                 advertise_resource();
111                 break;
112
113             case 'n': // select network
114             case 'N':
115                 select_network();
116                 break;
117
118             case 'x': // unselect network
119             case 'X':
120                 unselect_network();
121                 break;
122
123             case 'h': // handle request response
124             case 'H':
125                 handle_request_response();
126                 break;
127
128             default:
129                 printf("not supported menu!!\n");
130                 break;
131         }
132     }
133
134 }
135
136 void start_listening_server()
137 {
138     printf("start listening server!!\n");
139
140     CAStartListeningServer();
141 }
142
143 void start_discovery_server()
144 {
145     printf("start discovery server!!\n");
146
147     CAStartDiscoveryServer();
148 }
149
150 void find_resource()
151 {
152     char buf[MAX_BUF_LEN];
153
154     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
155
156     printf("\n=============================================\n");
157     printf("ex) a/light\n");
158     printf("reference uri : ");
159
160     gets(buf);
161
162     CAResult_t res = CAFindResource(buf);
163
164     if (res != CA_STATUS_OK)
165     {
166         printf("find resource error!!\n");
167     }
168     else
169     {
170         printf("find resource fo %s URI\n", buf);
171     }
172
173     printf("=============================================\n");
174 }
175
176 void send_request()
177 {
178     char buf[MAX_BUF_LEN];
179
180     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
181
182     printf("\n=============================================\n");
183     printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
184     printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
185     printf("uri : ");
186
187     gets(buf);
188
189     // create remote endpoint
190     CARemoteEndpoint_t* endpoint = NULL;
191     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
192     endpoint->connectivityType = CA_WIFI; //
193
194     if (res != CA_STATUS_OK)
195     {
196         printf("create remote endpoint error!!");
197         return;
198     }
199
200     // create token
201     CAToken_t token = NULL;
202     res = CAGenerateToken(&token);
203
204     if (res != CA_STATUS_OK)
205     {
206         printf("token generate error!!");
207         token = NULL;
208     }
209
210     printf("generated token %s\n", (token != NULL) ? token : "");
211
212     CAInfo_t requestData;
213     memset(&requestData, 0, sizeof(CAInfo_t));
214     requestData.token = token;
215     requestData.payload = "Temp Json Payload";
216
217     CARequestInfo_t requestInfo;
218     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
219     requestInfo.method = CA_GET;
220     requestInfo.info = requestData;
221
222     // send request
223     CASendRequest(endpoint, &requestInfo);
224
225     if (token != NULL)
226     {
227         CADestroyToken(token);
228     }
229
230     // destroy remote endpoint
231     if (endpoint != NULL)
232     {
233         CADestroyRemoteEndpoint(endpoint);
234     }
235
236     printf("=============================================\n");
237 }
238
239 void advertise_resource()
240 {
241     printf("\n=============================================\n");
242
243     printf("not implemented yet.\n");
244
245     printf("=============================================\n");
246 }
247
248 void select_network()
249 {
250     char buf[MAX_BUF_LEN];
251
252     printf("\n=============================================\n");
253     printf("\tselect network\n");
254     printf("ETHERNET : 0\n");
255     printf("WIFI : 1\n");
256     printf("EDR : 2\n");
257     printf("LE : 3\n");
258     printf("select : ");
259
260     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
261     gets(buf);
262
263     int number = buf[0] - '0';
264
265     number = (number < 0 || number > 3) ? 1 : number;
266
267     CASelectNetwork(1 << number);
268
269     printf("=============================================\n");
270 }
271
272 void unselect_network()
273 {
274     char buf[MAX_BUF_LEN];
275
276     printf("\n=============================================\n");
277     printf("\tunselect enabled network\n");
278     printf("ETHERNET : 0\n");
279     printf("WIFI : 1\n");
280     printf("EDR : 2\n");
281     printf("LE : 3\n");
282     printf("select : ");
283
284     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
285     gets(buf);
286
287     int number = buf[0] - '0';
288
289     number = (number < 0 || number > 3) ? 1 : number;
290
291     CAUnSelectNetwork(1 << number);
292
293     printf("=============================================\n");
294 }
295
296 char get_menu()
297 {
298     char buf[MAX_BUF_LEN];
299
300     printf("\n=============================================\n");
301     printf("\t\tMenu\n");
302     printf("\ts : start listening server\n");
303     printf("\td : start discovery server\n");
304     printf("\tf : find resource\n");
305     printf("\tr : send request\n");
306     printf("\ta : advertise resource\n");
307     printf("\tn : select network\n");
308     printf("\tx : unselect network\n");
309     printf("\th : handle request response\n");
310     printf("\tq : quit\n");
311     printf("=============================================\n");
312     printf("select : ");
313
314     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
315
316     gets(buf);
317
318     return buf[0];
319 }
320
321 void handle_request_response()
322 {
323     printf("handle_request_response\n");
324     CAHandleRequestResponse();
325 }
326
327 void request_handler(CARemoteEndpoint_t* object, CARequestInfo_t* requestInfo)
328 {
329
330     printf("request_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
331             (requestInfo != NULL) ? requestInfo->info.payload : "");
332
333     printf("send response with URI\n");
334     send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
335
336 }
337
338 void response_handler(CARemoteEndpoint_t* object, CAResponseInfo_t* responseInfo)
339 {
340
341     printf("response_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
342             (responseInfo != NULL) ? responseInfo->info.payload : "");
343
344     printf("send request after receivce response data\n");
345     send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
346 }
347
348 void send_response(CARemoteEndpoint_t* endpoint, CAToken_t request_token)
349 {
350
351     printf("\n=============================================\n");
352
353     CAInfo_t responseData;
354     //responseData = (CAInfo*) malloc(sizeof(CAInfo));
355     memset(&responseData, 0, sizeof(CAInfo_t));
356     responseData.token = request_token;
357     responseData.payload = "response payload";
358
359     CAResponseInfo_t responseInfo;
360     //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
361     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
362     responseInfo.result = 203;
363     responseInfo.info = responseData;
364
365     // send request
366     endpoint->connectivityType = CA_WIFI;
367     CASendResponse(endpoint, &responseInfo);
368
369     printf("=============================================\n");
370
371 }
372
373 void send_request_tmp(CARemoteEndpoint_t* endpoint, CAToken_t token)
374 {
375
376     printf("\n=============================================\n");
377
378     CAInfo_t requestData;
379     memset(&requestData, 0, sizeof(CAInfo_t));
380     requestData.token = token;
381     requestData.payload = "Temp Json Payload";
382
383     CARequestInfo_t requestInfo;
384     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
385     requestInfo.method = CA_GET;
386     requestInfo.info = requestData;
387
388     // send request
389     endpoint->connectivityType = CA_WIFI;
390     CASendRequest(endpoint, &requestInfo);
391
392     printf("=============================================\n");
393
394 }
395