Merge "Implementation of connectivity abstraction feature release v0.2" into connecti...
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / casample.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 <ctype.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <glib.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29 #include "cacommon.h"
30 #include "cainterface.h"
31
32 static GMainLoop *mainloop;
33 static GIOChannel *channel;
34 static guint g_test_io_watch_id;
35 static GError *g_err_Sample;
36
37 pthread_t thread;
38
39 #define MAX_BUF_LEN 1024
40 #define MAX_OPT_LEN 16
41
42 char get_menu();
43 void process();
44
45 void initialize();
46 void start_listening_server();
47 void start_discovery_server();
48 void find_resource();
49 void send_request();
50 void send_response();
51 void advertise_resource();
52 void send_notification();
53 void select_network();
54 void unselect_network();
55 void handle_request_response();
56
57 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
58 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
59 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
60 void terminate();
61
62 void pthread_func()
63 {
64     g_main_loop_run(mainloop);
65 }
66
67 int main()
68 {
69     system("clear");
70
71     printf("=============================================\n");
72     printf("\t\tsample main\n");
73     printf("=============================================\n");
74
75     process();
76
77     return 0;
78 }
79
80 void process()
81 {
82     while (1)
83     {
84         char menu = toupper(get_menu());
85
86         switch (menu)
87         {
88             case 'Q': // quits the sample program
89                 printf("quit..!!\n");
90                 g_main_loop_quit(mainloop);
91                 return;
92
93             case 'I': // Initialize interface
94                 initialize();
95
96             case 'S': // start server
97                 start_listening_server();
98                 break;
99
100             case 'C': // start client
101                 start_discovery_server();
102                 break;
103
104             case 'F': // find resource
105                 find_resource();
106                 break;
107
108             case 'R': // send request
109                 send_request();
110                 break;
111
112             case 'A': // advertise resource
113                 advertise_resource();
114                 break;
115
116             case 'N': // select network
117                 select_network();
118                 break;
119
120             case 'X': // unselect network
121                 unselect_network();
122                 break;
123
124             case 'H': // handle request response
125                 handle_request_response();
126                 break;
127
128             case 'T': // Terminate interface
129                 terminate();
130                 break;
131
132             default:
133                 printf("not supported menu!!\n");
134                 break;
135         }
136     }
137
138 }
139
140 void initialize()
141 {
142     mainloop = g_main_loop_new(NULL, FALSE);
143     pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
144
145     CAInitialize();
146
147     // network enable
148     // default
149     printf("select default network(WIFI)\n");
150     CASelectNetwork(CA_WIFI);
151
152     // set handler.
153     CARegisterHandler(request_handler, response_handler);
154 }
155
156 void start_listening_server()
157 {
158     printf("start listening server!!\n");
159
160     CAStartListeningServer();
161 }
162
163 void start_discovery_server()
164 {
165     printf("start discovery server at client!!\n");
166
167     CAStartDiscoveryServer();
168 }
169
170 void find_resource()
171 {
172     char buf[MAX_BUF_LEN];
173
174     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
175
176     printf("\n=============================================\n");
177     printf("ex) a/light\n");
178     printf("reference uri : ");
179
180     gets(buf);
181
182     CAResult_t res = CAFindResource(buf);
183
184     if (res != CA_STATUS_OK)
185     {
186         printf("find resource error:%d !!\n", res);
187     }
188     else
189     {
190         printf("find resource for %s URI\n", buf);
191     }
192
193     printf("=============================================\n");
194 }
195
196 void send_request()
197 {
198     char buf[MAX_BUF_LEN];
199
200     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
201
202     printf("\n=============================================\n");
203     printf("10.11.12.13:4545/resource_uri ( for IP )\n");
204     printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
205     printf("uri : ");
206
207     gets(buf);
208
209     // create remote endpoint
210     CARemoteEndpoint_t *endpoint = NULL;
211     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
212
213     if (res != CA_STATUS_OK)
214     {
215         printf("create remote endpoint error!!");
216         CADestroyRemoteEndpoint(endpoint);
217         return;
218     }
219
220     // create token
221     CAToken_t token = NULL;
222     res = CAGenerateToken(&token);
223
224     if (res != CA_STATUS_OK)
225     {
226         printf("token generate error!!");
227         token = NULL;
228     }
229
230     printf("generated token %s\n", (token != NULL) ? token : "");
231
232     CAInfo_t requestData;
233     memset(&requestData, 0, sizeof(CAInfo_t));
234     requestData.token = token;
235     requestData.payload = "Temp Json Payload";
236
237     CARequestInfo_t requestInfo;
238     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
239     requestInfo.method = CA_GET;
240     requestInfo.info = requestData;
241
242     // send request
243     CASendRequest(endpoint, &requestInfo);
244
245     if (token != NULL)
246     {
247         CADestroyToken(token);
248     }
249
250     // destroy remote endpoint
251     if (endpoint != NULL)
252     {
253         CADestroyRemoteEndpoint(endpoint);
254     }
255
256     printf("=============================================\n");
257 }
258
259 void advertise_resource()
260 {
261     char buf[MAX_BUF_LEN];
262
263     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
264
265     printf("\n=============================================\n");
266     printf("uri : ");
267
268     scanf("%s", buf);
269
270     int optionNum = 0;
271     char optionData[MAX_OPT_LEN];
272
273     printf("Option Num : ");
274     scanf("%d", &optionNum);
275     CAHeaderOption_t *headerOpt;
276
277     if (optionNum > 0)
278     {
279         headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
280         if (NULL == headerOpt)
281         {
282             printf("memory allocation failed!\n");
283             return;
284         }
285         memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
286     }
287
288     int i;
289     for (i = 0; i < optionNum; i++)
290     {
291         int optionID = 0;
292         printf("[%d] Option ID : ", i + 1);
293         scanf("%d", &optionID);
294         headerOpt[i].optionID = optionID;
295
296         memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
297         printf("[%d] Option Data : ", i + 1);
298         scanf("%s", optionData);
299         memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
300         printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
301
302         headerOpt[i].optionLength = (uint16_t) strlen(optionData);
303     }
304     printf("\n=============================================\n");
305
306     CAAdvertiseResource(buf, headerOpt, (uint8_t) optionNum);
307
308     free(headerOpt);
309
310 }
311
312 void send_notification()
313 {
314     char buf[MAX_BUF_LEN];
315
316     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
317
318     printf("\n=============================================\n");
319     printf("10.11.12.13:4545/resource_uri ( for IP )\n");
320     printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
321     printf("uri : ");
322
323     gets(buf);
324
325     // create remote endpoint
326     CARemoteEndpoint_t *endpoint = NULL;
327     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
328
329     if (res != CA_STATUS_OK)
330     {
331         printf("create remote endpoint error!!");
332         CADestroyRemoteEndpoint(endpoint);
333         return;
334     }
335
336     CAInfo_t respondeData;
337     memset(&respondeData, 0, sizeof(CAInfo_t));
338     respondeData.token = "client token";
339     respondeData.payload = "Temp Notification Data";
340
341     CAResponseInfo_t responseInfo;
342     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
343     responseInfo.result = CA_CONTENT;
344     responseInfo.info = respondeData;
345
346     // send request
347     CASendNotification(endpoint, &responseInfo);
348
349     // destroy remote endpoint
350     if (endpoint != NULL)
351     {
352         CADestroyRemoteEndpoint(endpoint);
353     }
354     printf("\n=============================================\n");
355 }
356
357 void select_network()
358 {
359     char buf[MAX_BUF_LEN];
360
361     printf("\n=============================================\n");
362     printf("\tselect network\n");
363     printf("ETHERNET : 0\n");
364     printf("WIFI : 1\n");
365     printf("EDR : 2\n");
366     printf("LE : 3\n");
367     printf("select : ");
368
369     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
370     gets(buf);
371
372     int number = buf[0] - '0';
373
374     number = (number < 0 || number > 3) ? 1 : number;
375
376     CASelectNetwork(1 << number);
377
378     printf("=============================================\n");
379 }
380
381 void unselect_network()
382 {
383     char buf[MAX_BUF_LEN];
384
385     printf("\n=============================================\n");
386     printf("\tunselect enabled network\n");
387     printf("ETHERNET : 0\n");
388     printf("WIFI : 1\n");
389     printf("EDR : 2\n");
390     printf("LE : 3\n");
391     printf("select : ");
392
393     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
394     gets(buf);
395
396     int number = buf[0] - '0';
397
398     number = (number < 0 || number > 3) ? 1 : number;
399
400     CAUnSelectNetwork(1 << number);
401     printf("Terminating...\n");
402     CATerminate();
403     //pthread_join(thread, NULL);
404     printf("=============================================\n");
405 }
406
407 char get_menu()
408 {
409     char buf[MAX_BUF_LEN];
410
411     printf("\n=============================================\n");
412     printf("\t\tMenu\n");
413     printf("\ti : Initialize\n");
414     printf("\ts : start server\n");
415     printf("\tc : start client\n");
416     printf("\tf : find resource\n");
417     printf("\tr : send request\n");
418     printf("\ta : advertise resource\n");
419     printf("\tn : select network\n");
420     printf("\tx : unselect network\n");
421     printf("\th : handle request response\n");
422     printf("\tt : terminate\n");
423     printf("\tq : quit\n");
424     printf("=============================================\n");
425     printf("select : ");
426
427     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
428
429     gets(buf);
430
431     return buf[0];
432 }
433
434 void handle_request_response()
435 {
436     printf("handle_request_response\n");
437     CAHandleRequestResponse();
438 }
439
440 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
441 {
442
443     printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
444             (object != NULL) ? object->resourceUri : "",
445             (requestInfo != NULL) ? requestInfo->info.payload : "");
446
447     printf("[CALLBACK] request_handler, address : %s\n",
448             (object != NULL) ? object->addressInfo.IP.ipAddress : "");
449
450     if (requestInfo->info.options)
451     {
452         uint32_t len = requestInfo->info.numOptions;
453         uint32_t i;
454         for (i = 0; i < len; i++)
455         {
456             printf("[CALLBACK] request_handler, option ID : %d\n",
457                     requestInfo->info.options[i].optionID);
458             printf("[CALLBACK] request_handler, options data length : %d\n",
459                     requestInfo->info.options[i].optionLength);
460             printf("[CALLBACK] request_handler, options data : %s\n",
461                     requestInfo->info.options[i].optionData);
462         }
463     }
464
465     printf("send response with URI\n");
466     send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
467
468 }
469
470 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
471 {
472
473     printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
474             (object != NULL) ? object->resourceUri : "",
475             (responseInfo != NULL) ? responseInfo->info.payload : "");
476
477     printf("[CALLBACK] response_handler, address : %s\n",
478             (object != NULL) ? object->addressInfo.IP.ipAddress : "");
479
480     if (responseInfo->info.options)
481     {
482         uint32_t len = responseInfo->info.numOptions;
483         uint32_t i;
484         for (i = 0; i < len; i++)
485         {
486             printf("[CALLBACK] response_handler, option ID : %d\n",
487                     responseInfo->info.options[i].optionID);
488             printf("[CALLBACK] response_handler, options data length : %d\n",
489                     responseInfo->info.options[i].optionLength);
490             printf("[CALLBACK] response_handler, options data : %s\n",
491                     responseInfo->info.options[i].optionData);
492         }
493     }
494
495 }
496
497 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
498 {
499
500     printf("\n=============================================\n");
501
502     CAInfo_t responseData;
503     //responseData = (CAInfo*) malloc(sizeof(CAInfo));
504     memset(&responseData, 0, sizeof(CAInfo_t));
505     responseData.token = request_token;
506     responseData.payload = "response payload";
507
508     CAResponseInfo_t responseInfo;
509     //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
510     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
511     responseInfo.result = 203;
512     responseInfo.info = responseData;
513
514     // send request (connectivityType from remoteEndpoint of request Info)
515     CASendResponse(endpoint, &responseInfo);
516
517     printf("=============================================\n");
518
519 }
520
521 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
522 {
523
524     printf("\n=============================================\n");
525
526     CAInfo_t requestData;
527     memset(&requestData, 0, sizeof(CAInfo_t));
528     requestData.token = token;
529     requestData.payload = "Temp Json Payload";
530
531     CARequestInfo_t requestInfo;
532     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
533     requestInfo.method = CA_GET;
534     requestInfo.info = requestData;
535
536     // send request
537     endpoint->connectivityType = CA_WIFI;
538     CASendRequest(endpoint, &requestInfo);
539
540     printf("=============================================\n");
541
542 }
543
544 void terminate()
545 {
546     unselect_network();
547 }