[CA] [All] URI support , Optimization , License verification
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / interfacesample.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 <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <wifi.h>
28 #include <glib.h>
29
30 #include "cacommon.h"
31 #include "caadapterinterface.h"
32 #include "uthreadpool.h"
33
34 #define MOD_NAME "TizenSample"
35
36 #if defined(WIFI_ADAPTER_TEST)
37 #include "cawifiadapter.h"
38 static u_thread_pool_t gWiFiThreadPool = NULL;
39 #endif
40 #if defined(BT_ADAPTER_TEST)
41 #include "caedradapter.h"
42 static  u_thread_pool_t gBTThreadPool = NULL;
43 #endif
44 #if defined(BLE_ADAPTER_TEST)
45 #include "caleadapter.h"
46 static u_thread_pool_t gLEThreadPool = NULL;
47 #endif
48
49 static GMainLoop *mainloop = NULL;
50 static GIOChannel *channel = NULL;
51 static guint g_test_io_watch_id = 0;
52 static GError *g_err_Sample = NULL;
53
54 static CALocalConnectivity_t *localWifiEndpoint = NULL;
55 //Hardcoded coap data for Test
56 static char coapData[500] =
57     "{\"oc:\[{href\":\"/a/light\",\"ref\":{\"power\":\"20\",\"state\":\"true\"}}]}";
58
59 void testInitializeBTInterface(void);
60 void testTerminateBTInterface(void);
61 void testInitializeWIFIInterface(void);
62 void testTerminateWIFIInterface(void);
63 void testInitializeBLEInterface(void);
64 void testTerminateBLEInterface(void);
65
66 typedef struct ConnectivityHandlerList
67 {
68     CAConnectivityType_t type;
69     CAConnectivityHandler_t handler;
70     struct ConnectivityHandlerList *nextHandler;
71 } ConnectivityHandlerList;
72
73 static ConnectivityHandlerList *gConnectivityHandlers = NULL;
74
75 void initializeThreadPool(CAConnectivityType_t type)
76 {
77 #ifdef BT_ADAPTER_TEST
78     if (CA_EDR == type && NULL == gBTThreadPool)
79     {
80         if (CA_STATUS_OK != u_thread_pool_init(3, &gBTThreadPool))
81         {
82             printf("Failed to create thread pool for BT adapter!\n");
83             return;
84         }
85     }
86 #endif
87 #ifdef WIFI_ADAPTER_TEST
88     if (CA_WIFI == type && NULL == gWiFiThreadPool)
89     {
90         if (CA_STATUS_OK != u_thread_pool_init(3, &gWiFiThreadPool))
91         {
92             printf("Failed to create thread pool for WIFI adapter!\n");
93             return;
94         }
95     }
96 #endif
97 #ifdef BLE_ADAPTER_TEST
98     if (CA_LE == type && NULL == gLEThreadPool)
99     {
100         if (CA_STATUS_OK != u_thread_pool_init(5, &gLEThreadPool))
101         {
102             printf("Failed to create thread pool for BLE adapter!\n");
103             return;
104         }
105     }
106 #endif
107 }
108
109
110 void storeInterfaceCallbacks(ConnectivityHandlerList *newHandler)
111 {
112     printf("storeInterfaceCallbacks Entry in Sample\n");
113     newHandler->nextHandler = NULL;
114     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
115
116     if (!tempConnectivityHandlers)
117     {
118         gConnectivityHandlers = newHandler;
119         printf("storeInterfaceCallbacks Exit in Sample\n");
120         return;
121     }
122
123     while (tempConnectivityHandlers->nextHandler)
124     {
125         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
126     }
127
128     tempConnectivityHandlers->nextHandler = newHandler;
129     printf("storeInterfaceCallbacks Exit in Sample\n");
130 }
131
132 void interfaceRegisterCallback(CAConnectivityHandler_t handler,
133                                CAConnectivityType_t connType)
134 {
135     printf("interfaceRegisterCallback Entry in Sample\n");
136     ConnectivityHandlerList *newConnectivityHandler = (ConnectivityHandlerList *) malloc(sizeof(
137                 ConnectivityHandlerList));
138     if (NULL == newConnectivityHandler)
139     {
140         printf("Memory allocation failed\n");
141         return;
142     }
143
144     newConnectivityHandler->type = connType;
145     newConnectivityHandler->handler = handler;
146     storeInterfaceCallbacks(newConnectivityHandler);
147     printf("interfaceRegisterCallback Exit in Sample\n");
148 }
149
150 void networkPacketHandler(CARemoteEndpoint_t *object, void *data, uint32_t dataLength)
151 {
152     printf("networkPacketHandler Entry in Sample\n");
153     if (object == NULL || data == NULL)
154     {
155         printf("NULL Object\n");
156         return;
157     }
158
159     printf("Data Received from: ");
160     if (CA_EDR == object->connectivityType)
161     {
162         printf(object->addressInfo.BT.btMacAddress);
163     }
164     else if (CA_LE == object->connectivityType)
165     {
166         printf(object->addressInfo.LE.leMacAddress);
167     }
168     else if (CA_WIFI == object->connectivityType || CA_ETHERNET == object->connectivityType)
169     {
170         printf(object->addressInfo.IP.ipAddress);
171     }
172
173     printf("\nReceived Data [Length: %d]: %s\n", dataLength, (char *)data);
174     printf("networkPacketHandler Exit in Sample\n");
175 }
176
177 void networkInterfaceCallback(CALocalConnectivity_t *localEndPoint,
178                               CANetworkStatus_t networkConnectivityState)
179 {
180     printf("networkInterfaceCallback Entry in Sample\n");
181     if (localEndPoint == NULL)
182     {
183         printf("NULL Object\n");
184         return;
185     }
186
187     if (networkConnectivityState == CA_INTERFACE_UP)
188     {
189         printf("Network Status is UP\n");
190     }
191     else
192     {
193         printf("Network Status is DOWN\n");
194     }
195
196     printf("Address: ");
197     if (CA_EDR == localEndPoint->type)
198     {
199         printf("%s\n", localEndPoint->addressInfo.BT.btMacAddress);
200     }
201     else if (CA_LE == localEndPoint->type)
202     {
203         printf("%s\n", localEndPoint->addressInfo.LE.leMacAddress);
204     }
205     else if (CA_WIFI == localEndPoint->type || CA_ETHERNET == localEndPoint->type)
206     {
207         printf("%s\n", localEndPoint->addressInfo.IP.ipAddress);
208     }
209
210     printf("networkInterfaceCallback Exit in Sample\n");
211 }
212
213
214 void freeData(void *data)
215 {
216     printf("freeData Entry in Sample\n");
217     if (data)
218     {
219         free(data);
220     }
221     printf("freeData Exit in Sample\n");
222 }
223
224 int16_t selectConnectivityType()
225 {
226     int32_t cType;
227     printf("*******Select the Connectivity Type*******\n");
228     printf("  [1] WIFI \n");
229     printf("  [2] BT \n");
230     printf("  [3] BLE \n");
231
232     fflush(stdin);
233     scanf("%d", &cType);
234     if (cType < 1 && cType > 3)
235     {
236         printf("Invalid Selection!!!!\n");
237         return 0;
238     }
239     return (int16_t)cType;
240 }
241
242 int16_t interfaceStartAdapter(CAConnectivityType_t connType)
243 {
244     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
245     if (NULL == tempConnectivityHandlers)
246     {
247         printf("None of the interface is initialized\n");
248         return 0;
249     }
250
251     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
252     {
253         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
254     }
255
256     if (NULL == tempConnectivityHandlers)
257     {
258         printf( "No interface handler for type %d", connType);
259         return 0;
260     }
261
262     if (CA_STATUS_OK != tempConnectivityHandlers->handler.startAdapter())
263     {
264         printf("Failed to Start adapter\n");
265         return 0;
266     }
267
268     return 1;
269 }
270
271 int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverType)
272 {
273     printf("interfaceMulticastStartServer Starting...\n");
274     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
275     if (NULL == tempConnectivityHandlers)
276     {
277         printf("None of the interface is initialized\n");
278         return 0;
279     }
280
281     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
282     {
283         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
284     }
285
286     if (NULL == tempConnectivityHandlers)
287     {
288         printf( "No interface handler for type %d", connType);
289         return 0;
290     }
291
292     CAAdapterStartDiscoveryServer startServer = NULL;
293     switch (serverType)
294     {
295         case 1: //Discovery server
296             startServer = tempConnectivityHandlers->handler.startDiscoverServer;
297             break;
298         case 2: //Listening server
299             startServer = tempConnectivityHandlers->handler.startListenServer;
300             break;
301     }
302
303     if (startServer)
304     {
305         printf("Invoking start server method\n");
306         startServer();
307     }
308 }
309
310 int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
311 {
312     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
313     if (NULL == tempConnectivityHandlers)
314     {
315         printf(" None of the interface is initialized \n");
316         return 0;
317     }
318
319     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
320     {
321         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
322     }
323
324     if (NULL == tempConnectivityHandlers)
325     {
326         printf( "No interface handler for type %d", connType);
327         return 0;
328     }
329
330     if (CA_WIFI == connType)
331     {
332         CARemoteEndpoint_t endpoint;
333         char remoteIPAddress[CA_IPADDR_SIZE] = {0};
334         printf("\nEnter the Remote Endpoint IP: ");
335         scanf("%s", remoteIPAddress);
336         if (strlen(remoteIPAddress) == 0)
337         {
338             printf("Invalid device address\n");
339             return;
340         }
341         endpoint.connectivityType = CA_WIFI;
342         strncpy(endpoint.addressInfo.IP.ipAddress, remoteIPAddress, CA_IPADDR_SIZE);
343         endpoint.addressInfo.IP.port = 5683; /* Send the corresponding port here */
344
345         int sdatalen = tempConnectivityHandlers->handler.sendData(&endpoint, coapData,
346                        strlen(coapData));
347         if (sdatalen == strlen(coapData))
348         {
349             printf("Send Unicast data success\n");
350         }
351         else
352         {
353             printf("Send Unicast data failed\n");
354         }
355     }
356     else if (CA_EDR == connType)
357     {
358         //create endpoint
359         CARemoteEndpoint_t endpoint;
360
361         //Get the device address from user
362         char deviceaddress[100] = {0};
363         printf("Enter the device address: \n");
364         scanf("%s", deviceaddress);
365
366         if (strlen(deviceaddress) == 0)
367         {
368             printf("Invlaid device address\n");
369             return;
370         }
371
372         endpoint.connectivityType = CA_EDR;
373         strncpy(endpoint.addressInfo.BT.btMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
374         endpoint.addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
375         endpoint.resourceUri = NULL;
376
377         printf("Sent Unicast data to device: %s\n", endpoint.addressInfo.BT.btMacAddress);
378         tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData) + 1);
379     }
380     else if (CA_LE == connType)
381     {
382         //create endpoint
383         CARemoteEndpoint_t endpoint;
384
385         //Get the device address from user
386         char deviceaddress[100] = {0};
387         printf("Enter the device address: \n");
388         scanf("%s", deviceaddress);
389
390         if (strlen(deviceaddress) == 0)
391         {
392             printf("Invlaid device address\n");
393             return;
394         }
395
396         //Get the service uuid from user
397         char uuid[100] = {0};
398         printf("Enter the service uuid: \n");
399         scanf("%s", uuid);
400
401         if (strlen(uuid) == 0)
402         {
403             printf("Invlaid service uuid\n");
404             return;
405         }
406
407         endpoint.connectivityType = CA_LE;
408         strncpy(endpoint.addressInfo.LE.leMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
409         endpoint.addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
410         endpoint.resourceUri = strdup(uuid);
411
412         tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData));
413         printf("Sent Unicast data \n");
414         free(endpoint.resourceUri);
415     }
416
417     return 1;
418 }
419
420 int16_t interfaceSendMulticastData(CAConnectivityType_t connType)
421 {
422     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
423     if (NULL == tempConnectivityHandlers)
424     {
425         printf("None of the interface is initialized\n");
426         return 0;
427     }
428
429     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
430     {
431         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
432     }
433
434     if (NULL == tempConnectivityHandlers)
435     {
436         printf( "No interface handler for type %d", connType);
437         return 0;
438     }
439
440     if (connType == CA_WIFI)
441     {
442         tempConnectivityHandlers->handler.sendDataToAll(coapData,
443                 strlen(coapData) + 1);
444     }
445     else if (connType == CA_EDR || connType == CA_LE)
446     {
447         tempConnectivityHandlers->handler.sendDataToAll(coapData, strlen(coapData));
448     }
449 }
450
451 void interfaceReadData(CAConnectivityType_t connType)
452 {
453     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
454     if (NULL == tempConnectivityHandlers)
455     {
456         printf("None of the interface is initialized\n");
457         return;
458     }
459
460     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
461     {
462         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
463     }
464
465     if (NULL == tempConnectivityHandlers)
466     {
467         printf( "No interface handler for type %d", connType);
468         return;
469     }
470
471     if (CA_STATUS_OK != tempConnectivityHandlers->handler.readData())
472     {
473         printf("Failed to Read Data\n");
474         return;
475     }
476
477     printf("Read Data is successfull\n");
478     return;
479 }
480
481 void interfaceGetNetworkInfo(CAConnectivityType_t connType)
482 {
483     int i = 0;
484
485     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
486     if (NULL == tempConnectivityHandlers)
487     {
488         printf("None of the interface is initialized\n");
489         return;
490     }
491
492     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
493     {
494         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
495     }
496
497     if (NULL == tempConnectivityHandlers)
498     {
499         printf( "No interface handler for type %d", connType);
500         return;
501     }
502
503     //Get the network interface info
504     CALocalConnectivity_t *info = NULL;
505     uint32_t size = 0;
506     if (CA_STATUS_OK != tempConnectivityHandlers->handler.GetnetInfo(&info, &size))
507     {
508         printf("Failed to get network info\n");
509         return;
510     }
511
512     if (0 >= size || info == NULL)
513     {
514         printf("No network found !!!\n");
515         return;
516     }
517
518     printf("Network Information: \n");
519     for (; i < size; i++)
520     {
521         if (connType == CA_WIFI)
522         {
523             printf("Type : %s\n", (connType == CA_ETHERNET) ? "CA_ETHERNET" : "CA_WIFI");
524             printf("Address : %s\n", info[i].addressInfo.IP.ipAddress);
525         }
526         else
527         {
528             printf("Type : %s\n", (connType == CA_EDR) ? "CA_EDR" : "CA_LE");
529             printf("Address : %s\n\n", info[i].addressInfo.BT.btMacAddress);
530         }
531     }
532 }
533
534 int16_t interfaceStopAdapter(CAConnectivityType_t connType)
535 {
536     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
537     if (NULL == tempConnectivityHandlers)
538     {
539         printf("None of the interface is initialized\n");
540         return 0;
541     }
542
543     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
544     {
545         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
546     }
547
548     if (NULL == tempConnectivityHandlers)
549     {
550         printf( "No interface handler for type %d", connType);
551         return 0;
552     }
553
554     if (CA_STATUS_OK != tempConnectivityHandlers->handler.stopAdapter())
555     {
556         printf("Failed to Stop adapter\n");
557         return 0;
558     }
559
560     printf("Stopped the adapter\n");
561     return 1;
562 }
563
564 void testInitializeInterface()
565 {
566     printf("testInitializeInterface Entry\n");
567
568     int16_t type = selectConnectivityType();
569     if (0 >= type || 3 < type)
570     {
571         printf("Invalid selection...\n");
572         return;
573     }
574
575     switch (type)
576     {
577 #ifdef WIFI_ADAPTER_TEST
578         case 1: //WIFI
579             {
580                 testInitializeWIFIInterface();
581             }
582             break;
583 #endif
584 #ifdef BT_ADAPTER_TEST
585         case 2:   //BT
586             {
587                 testInitializeBTInterface();
588             }
589             break;
590 #endif
591 #ifdef BLE_ADAPTER_TEST
592         case 3: //BLE
593             {
594                 testInitializeBLEInterface();
595             }
596             break;
597 #endif
598         default:
599             printf("Feature is not enabled or not implemented\n");
600     }
601 }
602
603 void testTerminateInterface()
604 {
605     int16_t type = selectConnectivityType();
606     if (0 >= type || 3 < type)
607     {
608         printf("Invalid selection...\n");
609         return;
610     }
611
612 #ifdef WIFI_ADAPTER_TEST
613     if (1 == type)   /* WIFI */
614     {
615         testTerminateWIFIInterface();
616     }
617 #endif
618 #ifdef BT_ADAPTER_TEST
619     if (2 == type)   /*BT*/
620     {
621         testTerminateBTInterface();
622     }
623 #endif
624 #ifdef BLE_ADAPTER_TEST
625     if (3 == type)   /*BLE*/
626     {
627         testTerminateBLEInterface();
628     }
629 #endif
630
631     ConnectivityHandlerList *currentConnectivityHandler = gConnectivityHandlers;
632     ConnectivityHandlerList *prevConnectivityHandler = NULL;
633
634     printf("Linked list delete start\n");
635     while (currentConnectivityHandler != NULL)
636     {
637         printf("Iterating through the list to find the matching interface\n");
638         if (currentConnectivityHandler->type == type)
639         {
640             printf("Matching interface found\n");
641             if (prevConnectivityHandler == NULL)
642             {
643                 currentConnectivityHandler = currentConnectivityHandler->nextHandler;
644                 freeData(gConnectivityHandlers);
645                 gConnectivityHandlers = NULL;
646                 printf( "Node deleted with interface type %d", type);
647             }
648             else
649             {
650                 prevConnectivityHandler->nextHandler = currentConnectivityHandler->nextHandler;
651                 freeData(currentConnectivityHandler);
652                 currentConnectivityHandler = prevConnectivityHandler->nextHandler;
653                 printf( "Node deleted with interface type %d from linked list", type);
654             }
655         }
656         else
657         {
658             prevConnectivityHandler = currentConnectivityHandler;
659             currentConnectivityHandler = currentConnectivityHandler->nextHandler;
660         }
661     }
662     gConnectivityHandlers = NULL;
663     return;
664 }
665
666 void testStartAdapter()
667 {
668     int type = selectConnectivityType();
669     if (0 >= type || 3 < type)
670     {
671         printf("Invalid selection...\n");
672         return;
673     }
674
675     switch (type)
676     {
677 #ifdef WIFI_ADAPTER_TEST
678         case 1: //WIFI
679             {
680                 interfaceStartAdapter(CA_WIFI);
681             }
682             break;
683 #endif
684 #ifdef BT_ADAPTER_TEST
685         case 2:   //BT
686             {
687                 interfaceStartAdapter(CA_EDR);
688             }
689             break;
690 #endif
691 #ifdef BLE_ADAPTER_TEST
692         case 3: //BLE
693             {
694                 interfaceStartAdapter(CA_LE);
695             }
696             break;
697 #endif
698         default:
699             printf("Feature is not enabled or not implemented\n");
700     }
701 }
702
703 void testStartServer(int serverType)
704 {
705     int type = selectConnectivityType();
706     if (0 >= type || 3 < type)
707     {
708         printf("Invalid selection...\n");
709         return;
710     }
711
712     switch (type)
713     {
714 #ifdef WIFI_ADAPTER_TEST
715         case 1: //WIFI
716             {
717                 interfaceMulticastStartServer(CA_WIFI, serverType);
718             }
719             break;
720 #endif
721 #ifdef BT_ADAPTER_TEST
722         case 2:   //BT
723             {
724                 interfaceMulticastStartServer(CA_EDR, serverType);
725             }
726             break;
727 #endif
728 #ifdef BLE_ADAPTER_TEST
729         case 3: //BLE
730             {
731                 interfaceMulticastStartServer(CA_LE, serverType);
732             }
733             break;
734 #endif
735         default:
736             printf("Feature is not enabled or not implemented\n");
737     }
738 }
739
740 void testSendUnicastData()
741 {
742     int16_t type = selectConnectivityType();
743     if (0 >= type || 3 < type)
744     {
745         printf("Invalid selection...\n");
746         return;
747     }
748
749     switch (type)
750     {
751 #ifdef WIFI_ADAPTER_TEST
752         case 1: //WIFI
753             {
754                 interfaceSendUnicastData(CA_WIFI);
755             }
756             break;
757 #endif
758 #ifdef BT_ADAPTER_TEST
759         case 2:   //BT
760             {
761                 interfaceSendUnicastData(CA_EDR);
762             }
763             break;
764 #endif
765 #ifdef BLE_ADAPTER_TEST
766         case 3: //BLE
767             {
768                 interfaceSendUnicastData(CA_LE);
769             }
770             break;
771 #endif
772         default:
773             printf("Feature is not enabled or not implemented\n");
774     }
775 }
776
777 void testSendMulticastData()
778 {
779     int16_t type = selectConnectivityType();
780     if (0 >= type || 3 < type)
781     {
782         printf("Invalid selection...\n");
783         return;
784     }
785
786     switch (type)
787     {
788 #ifdef WIFI_ADAPTER_TEST
789         case 1: //WIFI
790             {
791                 interfaceSendMulticastData(CA_WIFI);
792             }
793             break;
794 #endif
795 #ifdef BT_ADAPTER_TEST
796         case 2:   //BT
797             {
798                 interfaceSendMulticastData(CA_EDR);
799             }
800             break;
801 #endif
802 #ifdef BLE_ADAPTER_TEST
803         case 3: //BLE
804             {
805                 interfaceSendMulticastData(CA_LE);
806             }
807             break;
808 #endif
809         default:
810             printf("Feature is not enabled or not implemented\n");
811     }
812 }
813
814 void testReadData(void)
815 {
816     int16_t type = selectConnectivityType();
817     if (0 >= type || 3 < type)
818     {
819         printf("Invalid selection...\n");
820         return;
821     }
822
823     switch (type)
824     {
825 #ifdef WIFI_ADAPTER_TEST
826         case 1: //WIFI
827             {
828                 interfaceReadData(CA_WIFI);
829             }
830             break;
831 #endif
832 #ifdef BT_ADAPTER_TEST
833         case 2:   //BT
834             {
835                 interfaceReadData(CA_EDR);
836             }
837             break;
838 #endif
839 #ifdef BLE_ADAPTER_TEST
840         case 3: //BLE
841             {
842                 interfaceReadData(CA_LE);
843             }
844             break;
845 #endif
846         default:
847             printf("Feature is not enabled or not implemented\n");
848     }
849 }
850
851 void testGetNetworkInfo(void)
852 {
853     int16_t type = selectConnectivityType();
854     if (0 >= type || 3 < type)
855     {
856         printf("Invalid selection...\n");
857         return;
858     }
859
860     switch (type)
861     {
862 #ifdef WIFI_ADAPTER_TEST
863         case 1: //WIFI
864             {
865                 interfaceGetNetworkInfo(CA_WIFI);
866             }
867             break;
868 #endif
869 #ifdef BT_ADAPTER_TEST
870         case 2:   //BT
871             {
872                 interfaceGetNetworkInfo(CA_EDR);
873             }
874             break;
875 #endif
876 #ifdef BLE_ADAPTER_TEST
877         case 3: //BLE
878             {
879                 interfaceGetNetworkInfo(CA_LE);
880             }
881             break;
882 #endif
883         default:
884             printf("Feature is not enabled or not implemented\n");
885     }
886 }
887
888 void testStopAdapter()
889 {
890     int16_t type = selectConnectivityType();
891     if (0 >= type || 3 < type)
892     {
893         printf("Invalid selection...\n");
894         return;
895     }
896
897     switch (type)
898     {
899 #ifdef WIFI_ADAPTER_TEST
900         case 1: //WIFI
901             {
902                 interfaceStopAdapter(CA_WIFI);
903             }
904             break;
905 #endif
906 #ifdef BT_ADAPTER_TEST
907         case 2:   //BT
908             {
909                 interfaceStopAdapter(CA_EDR);
910             }
911             break;
912 #endif
913 #ifdef BLE_ADAPTER_TEST
914         case 3: //BLE
915             {
916                 interfaceStopAdapter(CA_LE);
917             }
918             break;
919 #endif
920         default:
921             printf("Feature is not enabled or not implemented\n");
922     }
923 }
924
925 #ifdef BT_ADAPTER_TEST
926 void testInitializeBTInterface(void)
927 {
928     printf("Initiazing EDR\n");
929
930     printf("Initializing BT Adapter threadpool\n");
931     initializeThreadPool(CA_EDR);
932
933     //Start bluetooth communication adapter
934     CAResult_t err = CAInitializeEDR(interfaceRegisterCallback, networkPacketHandler,
935                                      networkInterfaceCallback, gBTThreadPool);
936     if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
937     {
938         printf("Failed to initialize bluetooth communication adapter!\n");
939     }
940 }
941
942 void testTerminateBTInterface(void)
943 {
944     printf("Terminating EDR\n");
945
946     //Terminate the Bluetooth communication adapter
947     CATerminateEDR();
948
949     printf( "Terminating BT Adapter thread pool");
950     u_thread_pool_free(gBTThreadPool);
951     gBTThreadPool = NULL;
952 }
953 #endif //BT_ADAPTER_TEST
954
955 #ifdef WIFI_ADAPTER_TEST
956 void testInitializeWIFIInterface(void)
957 {
958     printf("testInitializeWIFIInterface IN\n");
959
960     printf("Initializing WIFI adapter threadpool\n");
961     initializeThreadPool(CA_WIFI);
962
963     //Start Wifi communication adapter
964     if (0 != CAInitializeWifi(interfaceRegisterCallback, networkPacketHandler,
965                               networkInterfaceCallback, gWiFiThreadPool))
966     {
967         printf("testInitializeWIFIInterface Failed to initialize bluetooth communication adapter\n");
968         return;
969     }
970
971     printf("testInitializeWIFIInterface OUT\n");
972 }
973
974 void testTerminateWIFIInterface(void)
975 {
976     printf("testTerminateWIFIInterface IN\n");
977
978     // Stop if Wifi communication adapter is running
979     interfaceStopAdapter(CA_WIFI);
980
981     // Freeing threadpool for wifi communication adapter
982     printf( "Terminating WIFI Adapter thread pool");
983     u_thread_pool_free(gWiFiThreadPool);
984     gWiFiThreadPool = NULL;
985
986     //Terminate the Wifi communication adapter
987     CATerminateWIfI();
988
989
990     printf("testTerminateWIFIInterface OUT\n");
991 }
992 #endif //WIFI_ADAPTER_TEST
993
994 #ifdef BLE_ADAPTER_TEST
995 void testInitializeBLEInterface(void)
996 {
997     printf("testInitializeBLEInterface IN\n");
998
999     printf("Initializing BLE adapter threadpool\n");
1000     initializeThreadPool(CA_LE);
1001
1002     //Start bluetooth communication adapter
1003     if (0 != CAInitializeLE(interfaceRegisterCallback, networkPacketHandler,
1004                             networkInterfaceCallback, gLEThreadPool))
1005     {
1006         printf("testInitializeBLEInterface Failed due to CAInitializeLE\n");
1007         return;
1008     }
1009
1010     printf("testInitializeBLEInterface OUT\n");
1011 }
1012
1013 void testTerminateBLEInterface(void)
1014 {
1015     printf("testTerminateBLEInterface IN\n");
1016
1017     //Terminate the BLE server & Client
1018     CATerminateLE();
1019
1020     printf( "Terminating BLE Adapter thread pool");
1021     u_thread_pool_free(gLEThreadPool);
1022     gLEThreadPool = NULL;
1023
1024     printf("testTerminateBLEInterface OUT\n");
1025 }
1026 #endif //BLE_ADAPTER_TEST
1027
1028 static void testPrintHelp(void)
1029 {
1030     printf(" =====================================================================\n");
1031     printf("|                 Welcome to Connectivity Abstraction                 |\n");
1032     printf("|                   - CA Unit Test v1.0 -                             |\n");
1033     printf("|---------------------------------------------------------------------|\n");
1034     printf("|                           ** Options **                             |\n");
1035     printf("|  i - Initialize the Interface                                       |\n");
1036     printf("|  d - Terminate the Interface                                        |\n");
1037     printf("|  a - Start adapter                                                  |\n");
1038     printf("|  b - Stop adapter                                                   |\n");
1039     printf("|  sd- Start Discovery Server                                         |\n");
1040     printf("|  sl- Start Listening Server                                         |\n");
1041     printf("|  u - Send Unicast Data                                              |\n");
1042     printf("|  m - Send Multicast Data                                            |\n");
1043     printf("|  g - Get Network Info                                               |\n");
1044     printf("|  r - Read data synchronously                                        |\n");
1045     printf("|  x - quit the test.                                                 |\n");
1046     printf("|  h - Help menu.                                                     |\n");
1047     printf(" =====================================================================\n");
1048 }
1049
1050 static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer data)
1051 {
1052     gchar buf[10] = {'\0'};
1053     gsize read = 0;
1054
1055
1056     if (g_io_channel_read(channel, buf, 10, &read) != G_IO_ERROR_NONE)
1057     {
1058         printf("g_io_channel_read error!!!\n");
1059         return 1;
1060     }
1061     buf[read] = '\0';
1062     g_strstrip(buf);
1063
1064     /*if ((!has_register) && (((buf[0]!= 'i') && (buf[0]!= 'h') && (buf[0]!= 'q')) || (read != 2))) {
1065         testPrintHelp();
1066         printf("***Warning***: You should Register firstly!\n");
1067         return 1;
1068     }*/
1069     switch (buf[0])
1070     {
1071         case 'i':
1072             testInitializeInterface();
1073             break;
1074         case 'x':
1075             testTerminateInterface();
1076             if (g_source_remove(g_test_io_watch_id))
1077             {
1078                 printf("g_source_remove() OK!!!\n");
1079                 g_io_channel_shutdown(channel, TRUE, &g_err_Sample);
1080                 g_io_channel_unref(channel);
1081                 g_main_loop_quit(mainloop);
1082             }
1083             break;
1084         case 'd':
1085             testTerminateInterface();
1086             break;
1087         case 'a':
1088             testStartAdapter();
1089             break;
1090         case 'b':
1091             testStopAdapter();
1092             break;
1093         case 's':
1094             if (read == 3)
1095             {
1096                 if (buf[1] == 'd')
1097                 {
1098                     testStartServer(1);
1099                 }
1100                 if (buf[1] == 'l')
1101                 {
1102                     testStartServer(2);
1103                 }
1104             }
1105             break;
1106         case 'u':
1107             testSendUnicastData();
1108             break;
1109         case 'm':
1110             testSendMulticastData();
1111             break;
1112         case 'r':
1113             testReadData();
1114             break;
1115         case 'g':
1116             testGetNetworkInfo();
1117             break;
1118         case 'h':
1119             testPrintHelp();
1120     }
1121     return 1;
1122 }
1123
1124 int main(int argc, char *argv[])
1125 {
1126     printf("Starting sample\n");
1127     mainloop = g_main_loop_new(NULL, FALSE);
1128     channel = g_io_channel_unix_new(0);/* read from stdin */
1129     g_test_io_watch_id = g_io_add_watch(channel,
1130                                         (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), testThread,
1131                                         NULL);
1132     printf("CM Test Thread created...\n");
1133     testPrintHelp();
1134     g_main_loop_run(mainloop);
1135     return 0;
1136 }
1137