b2da299cba051d158fa8c858d0bc7fab0319b09e
[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 <unistd.h>
25 #include "cacommon.h"
26 #include "cainterface.h"
27
28 #define MAX_BUF_LEN 1024
29 #define MAX_OPT_LEN 16
30 #define TRUE 1
31 #define FALSE 0
32
33 char get_menu();
34 void process();
35
36 void start_listening_server();
37 void start_discovery_server();
38 void find_resource();
39 void send_request();
40 void send_response();
41 void advertise_resource();
42 void send_notification();
43 void select_network();
44 void unselect_network();
45 void handle_request_response();
46 void find_fixed_resource();
47 void get_network_info();
48
49 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
50 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
51 void send_request_tmp(CARemoteEndpoint_t* endpoint, CAToken_t token);
52 int received = FALSE;
53
54 static CAToken_t gLastRequestToken = NULL;
55
56 int main()
57 {
58     system("clear");
59
60     printf("=============================================\n");
61     printf("\t\tsample main\n");
62     printf("=============================================\n");
63
64     CAResult_t res = CAInitialize();
65     if (res != CA_STATUS_OK)
66     {
67         printf("CAInitialize fail\n");
68         return 0;
69     }
70
71     // network enable
72     // default
73     printf("select default network(WIFI)\n");
74     res = CASelectNetwork(CA_WIFI);
75     if (res != CA_STATUS_OK)
76     {
77         printf("CASelectNetwork fail\n");
78         return 0;
79     }
80
81     // set handler.
82     res = CARegisterHandler(request_handler, response_handler);
83     if (res != CA_STATUS_OK)
84     {
85         printf("CARegisterHandler fail\n");
86         return 0;
87     }
88
89     process();
90
91     CATerminate();
92
93     return 0;
94 }
95
96 void process()
97 {
98     while (1)
99     {
100         char menu = get_menu();
101
102         switch (menu)
103         {
104             case 'm': // menu
105             case 'M':
106                 continue;
107
108             case 'q': // quit
109             case 'Q':
110                 printf("quit..!!\n");
111                 return;
112
113             case 's': // start server
114             case 'S':
115                 start_listening_server();
116                 break;
117
118             case 'c': // start client
119             case 'D':
120                 start_discovery_server();
121                 break;
122
123             case 'f': // find resource
124             case 'F':
125                 find_resource();
126                 break;
127
128             case 'r': // send request
129             case 'R':
130                 send_request();
131                 break;
132
133             case 'a': // advertise resource
134             case 'A':
135                 advertise_resource();
136                 break;
137
138             case 'b': // send notification
139             case 'B':
140                 send_notification();
141                 break;
142
143             case 'n': // select network
144             case 'N':
145                 select_network();
146                 break;
147
148             case 'x': // unselect network
149             case 'X':
150                 unselect_network();
151                 break;
152
153             case 'h': // handle request response
154             case 'H':
155                 handle_request_response();
156                 break;
157             case 'y':
158             case 'Y':
159                 while (1)
160                 {
161                     received = FALSE;
162                     find_fixed_resource();
163                     while (received == FALSE)
164                     {
165                         sleep(1);
166                         handle_request_response();
167
168                     }
169                 }
170                 break;
171             case 'z':
172             case 'Z':
173                 start_listening_server();
174                 while (1)
175                 {
176                     sleep(1);
177                     handle_request_response();
178                 }
179                 break;
180
181             case 'g': // get network information
182             case 'G':
183                 get_network_info();
184                 break;
185
186             default:
187                 printf("not supported menu!!\n");
188                 break;
189         }
190     }
191
192 }
193
194 void start_listening_server()
195 {
196     printf("start listening server!!\n");
197
198     CAResult_t res = CAStartListeningServer();
199     if (res != CA_STATUS_OK)
200     {
201         printf("start listening server fail\n");
202     }
203     else
204     {
205         printf("start listening server success\n");
206     }
207 }
208
209 void start_discovery_server()
210 {
211     printf("start discovery client!!\n");
212
213     CAResult_t res = CAStartDiscoveryServer();
214     if (res != CA_STATUS_OK)
215     {
216         printf("start discovery client fail\n");
217     }
218     else
219     {
220         printf("start discovery client success\n");
221     }
222 }
223
224 void find_fixed_resource()
225 {
226     char buf[MAX_BUF_LEN] =
227     { 0, };
228     strcpy(buf, "a/light");
229
230
231
232     // create token
233     CAToken_t token = NULL;
234     CAResult_t res = CAGenerateToken(&token);
235     if (res != CA_STATUS_OK)
236     {
237         printf("token generate error!!");
238         token = NULL;
239     }
240
241     printf("generated token %s\n", (token != NULL) ? token : "");
242
243     res = CAFindResource(buf, token);
244     if (res != CA_STATUS_OK)
245     {
246         printf("find resource error!!\n");
247     }
248     else
249     {
250         printf("find resource to %s URI\n", buf);
251     }
252
253     // delete token
254     /*
255     if (token != NULL)
256     {
257         CADestroyToken(token);
258     }
259     */
260
261     printf("=============================================\n");
262 }
263
264 void find_resource()
265 {
266     char buf[MAX_BUF_LEN];
267
268     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
269
270     printf("\n=============================================\n");
271     printf("ex) a/light\n");
272     printf("reference uri : ");
273
274     gets(buf);
275
276     // create token
277     CAToken_t token = NULL;
278     CAResult_t res = CAGenerateToken(&token);
279     if (res != CA_STATUS_OK)
280     {
281         printf("token generate error!!\n");
282         token = NULL;
283     }
284
285     printf("generated token %s\n", (token != NULL) ? token : "");
286
287     res = CAFindResource(buf, token);
288     if (res != CA_STATUS_OK)
289     {
290         printf("find resource error!!\n");
291     }
292     else
293     {
294         printf("find resource to %s URI\n", buf);
295
296         gLastRequestToken = token;
297     }
298
299     // delete token
300     /*
301     if (token != NULL)
302     {
303         CADestroyToken(token);
304     }
305     */
306
307     printf("=============================================\n");
308 }
309
310 void send_request()
311 {
312     char buf[MAX_BUF_LEN];
313
314     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
315
316     printf("\n=============================================\n");
317     printf("10.11.12.13:4545/resource_uri ( for IP )\n");
318     printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
319     printf("uri : ");
320
321     gets(buf);
322
323     // create remote endpoint
324     CARemoteEndpoint_t* endpoint = NULL;
325     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
326
327     if (res != CA_STATUS_OK)
328     {
329         printf("create remote endpoint error!!\n");
330         CADestroyRemoteEndpoint(endpoint);
331         return;
332     }
333
334     // create token
335     CAToken_t token = NULL;
336     res = CAGenerateToken(&token);
337
338     if (res != CA_STATUS_OK)
339     {
340         printf("token generate error!!\n");
341         token = NULL;
342     }
343
344     printf("generated token %s\n", (token != NULL) ? token : "");
345
346     CAInfo_t requestData;
347     memset(&requestData, 0, sizeof(CAInfo_t));
348     requestData.token = token;
349     requestData.payload = "Temp Json Payload";
350
351     CARequestInfo_t requestInfo;
352     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
353     requestInfo.method = CA_GET;
354     requestInfo.info = requestData;
355
356     // send request
357     CASendRequest(endpoint, &requestInfo);
358
359     if (token != NULL)
360     {
361         CADestroyToken(token);
362     }
363
364     // destroy remote endpoint
365     if (endpoint != NULL)
366     {
367         CADestroyRemoteEndpoint(endpoint);
368     }
369
370     printf("=============================================\n");
371 }
372
373 void advertise_resource()
374 {
375     char buf[MAX_BUF_LEN];
376
377     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
378
379     printf("\n=============================================\n");
380     printf("uri : ");
381
382     scanf("%s", buf);
383
384     int optionNum = 0;
385     char optionData[MAX_OPT_LEN];
386
387     printf("Option Num : ");
388     scanf("%d", &optionNum);
389     CAHeaderOption_t *headerOpt;
390     headerOpt = (CAHeaderOption_t*) malloc(sizeof(CAHeaderOption_t) * optionNum);
391     if (NULL == headerOpt)
392     {
393         printf("Memory allocation failed!\n");
394         return;
395     }
396     memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
397
398     int i;
399     for (i = 0; i < optionNum; i++)
400     {
401         int optionID = 0;
402         printf("[%d] Option ID : ", i + 1);
403         scanf("%d", &optionID);
404         headerOpt[i].optionID = optionID;
405
406         memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
407         printf("[%d] Option Data : ", i + 1);
408         scanf("%s", optionData);
409         memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
410         printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
411
412         headerOpt[i].optionLength = (uint16_t) strlen(optionData);
413     }
414     printf("\n=============================================\n");
415
416     // create token
417     CAToken_t token = NULL;
418     CAResult_t res = CAGenerateToken(&token);
419     if (res != CA_STATUS_OK)
420     {
421         printf("token generate error!!\n");
422         token = NULL;
423     }
424
425     printf("generated token %s\n", (token != NULL) ? token : "");
426
427     CAAdvertiseResource(buf, token, headerOpt, (uint8_t) optionNum);
428
429     // delete token
430     /*
431     if (token != NULL)
432     {
433         CADestroyToken(token);
434     }
435     */
436
437     free(headerOpt);
438
439 }
440
441 void send_notification()
442 {
443     char buf[MAX_BUF_LEN];
444
445     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
446
447     printf("\n=============================================\n");
448     printf("10.11.12.13:4545/resource_uri ( for IP )\n");
449     printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
450     printf("uri : ");
451
452     gets(buf);
453
454     // create remote endpoint
455     CARemoteEndpoint_t* endpoint = NULL;
456     CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
457
458     if (res != CA_STATUS_OK)
459     {
460         printf("create remote endpoint error!!\n");
461         CADestroyRemoteEndpoint(endpoint);
462         return;
463     }
464
465     CAInfo_t respondeData;
466     memset(&respondeData, 0, sizeof(CAInfo_t));
467     respondeData.token = "client token";
468     respondeData.payload = "Temp Notification Data";
469
470     CAResponseInfo_t responseInfo;
471     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
472     responseInfo.result = CA_CONTENT;
473     responseInfo.info = respondeData;
474
475     // send request
476     res = CASendNotification(endpoint, &responseInfo);
477     if (res != CA_STATUS_OK)
478     {
479         printf("send notification error\n");
480     }
481     else
482     {
483         printf("send notification success\n");
484     }
485
486     // destroy remote endpoint
487     if (endpoint != NULL)
488     {
489         CADestroyRemoteEndpoint(endpoint);
490     }
491
492     printf("\n=============================================\n");
493 }
494
495 void select_network()
496 {
497     char buf[MAX_BUF_LEN];
498
499     printf("\n=============================================\n");
500     printf("\tselect network\n");
501     printf("ETHERNET : 0\n");
502     printf("WIFI : 1\n");
503     printf("EDR : 2\n");
504     printf("LE : 3\n");
505     printf("select : ");
506
507     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
508     gets(buf);
509
510     int number = buf[0] - '0';
511
512     number = (number < 0 || number > 3) ? 1 : number;
513
514     CAResult_t res = CASelectNetwork(1 << number);
515     if (res != CA_STATUS_OK)
516     {
517         printf("select network error\n");
518     }
519     else
520     {
521         printf("select network success\n");
522     }
523
524     printf("=============================================\n");
525 }
526
527 void unselect_network()
528 {
529     char buf[MAX_BUF_LEN];
530
531     printf("\n=============================================\n");
532     printf("\tunselect enabled network\n");
533     printf("ETHERNET : 0\n");
534     printf("WIFI : 1\n");
535     printf("EDR : 2\n");
536     printf("LE : 3\n");
537     printf("select : ");
538
539     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
540     gets(buf);
541
542     int number = buf[0] - '0';
543
544     number = (number < 0 || number > 3) ? 1 : number;
545
546     CAResult_t res = CAUnSelectNetwork(1 << number);
547     if (res != CA_STATUS_OK)
548     {
549         printf("unselect network error\n");
550     }
551     else
552     {
553         printf("unselect network success\n");
554     }
555
556     printf("=============================================\n");
557 }
558
559 char get_menu()
560 {
561     char buf[MAX_BUF_LEN];
562
563     printf("\n=============================================\n");
564     printf("\t\tMenu\n");
565     printf("\ts : start server\n");
566     printf("\tc : start client\n");
567     printf("\tf : find resource\n");
568     printf("\tr : send request\n");
569     printf("\ta : advertise resource\n");
570     printf("\tb : send notification\n");
571     printf("\tn : select network\n");
572     printf("\tx : unselect network\n");
573     printf("\tg : get network information\n");
574     printf("\th : handle request response\n");
575     printf("\ty : run static client\n");
576     printf("\tz : run static server\n");
577     printf("\tq : quit\n");
578     printf("=============================================\n");
579     printf("select : ");
580
581     memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
582
583     gets(buf);
584
585     return buf[0];
586 }
587
588 void handle_request_response()
589 {
590     printf("handle_request_response\n");
591
592     CAResult_t res = CAHandleRequestResponse();
593     if (res != CA_STATUS_OK)
594     {
595         printf("handle request error\n");
596     }
597     else
598     {
599         printf("handle request success\n");
600     }
601 }
602
603 void get_network_info()
604 {
605     int index;
606
607     CALocalConnectivity_t* tempInfo;
608     uint32_t tempSize = 0;
609
610     tempInfo = (CALocalConnectivity_t*) malloc(sizeof(CALocalConnectivity_t));
611
612     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
613     if (res != CA_STATUS_OK)
614     {
615         printf("get network information error\n");
616         return;
617     }
618
619     printf("network info total size is %d\n", tempSize);
620     for (index = 0; index < tempSize; index++)
621     {
622         if (tempInfo == NULL)
623         {
624             break;
625         }
626
627         printf("type : %d, address : %s\n",
628                 tempInfo->type,
629                 tempInfo->addressInfo.IP.ipAddress);
630         tempInfo++;
631     }
632
633 }
634
635 void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
636 {
637     /*
638     printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
639             (object != NULL) ? object->resourceUri : "",
640             (requestInfo != NULL) ? requestInfo->info.payload : "");
641     */
642     printf("[CALLBACK] request_handler, uri: %s, data: %s, token: %s \n",
643             (object != NULL) ? object->resourceUri : "",
644             (requestInfo != NULL) ? requestInfo->info.payload : "",
645             (requestInfo->info.token != NULL) ? requestInfo->info.token : "");
646
647     if (gLastRequestToken != NULL && requestInfo->info.token != NULL 
648         && (strcmp((char*)gLastRequestToken, requestInfo->info.token) == 0))
649     {
650         printf("token is same. received request of it's own. skip.. \n");
651
652         return;
653     }
654
655     printf("[CALLBACK] request_handler, address : %s port:%d \n",
656             (object != NULL) ? object->addressInfo.IP.ipAddress : "",
657                (object != NULL) ? object->addressInfo.IP.port : 0);
658
659     if (requestInfo->info.options)
660     {
661         uint32_t len = requestInfo->info.numOptions;
662         uint32_t i;
663         for (i = 0; i < len; i++)
664         {
665             printf("[CALLBACK] request_handler, option ID : %d\n",
666                     requestInfo->info.options[i].optionID);
667             printf("[CALLBACK] request_handler, options data length : %d\n",
668                     requestInfo->info.options[i].optionLength);
669             printf("[CALLBACK] request_handler, options data : %s\n",
670                     requestInfo->info.options[i].optionData);
671         }
672     }
673
674     printf("send response with URI\n");
675     send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
676
677     received = TRUE;
678
679 }
680
681 void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
682 {
683
684     printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
685             (object != NULL) ? object->resourceUri : "",
686             (responseInfo != NULL) ? responseInfo->info.payload : "");
687
688     printf("[CALLBACK] response_handler, address : %s port :%d\n",
689             (object != NULL) ? object->addressInfo.IP.ipAddress : "",
690             (object != NULL) ? object->addressInfo.IP.port : 0);
691
692     if (responseInfo->info.options)
693     {
694         uint32_t len = responseInfo->info.numOptions;
695         uint32_t i;
696         for (i = 0; i < len; i++)
697         {
698             printf("[CALLBACK] response_handler, option ID : %d\n",
699                     responseInfo->info.options[i].optionID);
700             printf("[CALLBACK] response_handler, options data length : %d\n",
701                     responseInfo->info.options[i].optionLength);
702             printf("[CALLBACK] response_handler, options data : %s\n",
703                     responseInfo->info.options[i].optionData);
704         }
705     }
706     received = TRUE;
707
708     //printf("send request with URI\n");
709     //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
710 }
711
712 void send_response(CARemoteEndpoint_t* endpoint, CAToken_t request_token)
713 {
714
715     printf("\n=============================================\n");
716
717     CAInfo_t responseData;
718     //responseData = (CAInfo*) malloc(sizeof(CAInfo));
719     memset(&responseData, 0, sizeof(CAInfo_t));
720     responseData.token = request_token;
721     responseData.payload = "response payload";
722
723     CAResponseInfo_t responseInfo;
724     //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
725     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
726     responseInfo.result = 203;
727     responseInfo.info = responseData;
728
729     // send request (connectivityType from remoteEndpoint of request Info)
730     CAResult_t res = CASendResponse(endpoint, &responseInfo);
731     if (res != CA_STATUS_OK)
732     {
733         printf("send response error\n");
734     }
735     else
736     {
737         printf("send response success\n");
738     }
739
740     printf("=============================================\n");
741
742 }
743
744 void send_request_tmp(CARemoteEndpoint_t* endpoint, CAToken_t token)
745 {
746
747     printf("\n=============================================\n");
748
749     CAInfo_t requestData;
750     memset(&requestData, 0, sizeof(CAInfo_t));
751     requestData.token = token;
752     requestData.payload = "Temp Json Payload";
753
754     CARequestInfo_t requestInfo;
755     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
756     requestInfo.method = CA_GET;
757     requestInfo.info = requestData;
758
759     // send request
760     endpoint->connectivityType = CA_WIFI;
761
762     CAResult_t res = CASendRequest(endpoint, &requestInfo);
763     if (res != CA_STATUS_OK)
764     {
765         printf("send request error\n");
766     }
767     else
768     {
769         printf("send request success\n");
770     }
771
772     printf("=============================================\n");
773
774 }
775