IoT-243 Connectivity Abstraction single-threaded module missing definition for CASend...
[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 //Hardcoded coap data for Test
55 static char coapData[500] =
56     "{\"oc:\[{href\":\"/a/light\",\"ref\":{\"power\":\"20\",\"state\":\"true\"}}]}";
57
58 void testInitializeBTInterface(void);
59 void testTerminateBTInterface(void);
60 void testInitializeWIFIInterface(void);
61 void testTerminateWIFIInterface(void);
62 void testInitializeBLEInterface(void);
63 void testTerminateBLEInterface(void);
64
65 typedef struct ConnectivityHandlerList
66 {
67     CAConnectivityType_t type;
68     CAConnectivityHandler_t handler;
69     struct ConnectivityHandlerList *nextHandler;
70 } ConnectivityHandlerList;
71
72 static ConnectivityHandlerList *gConnectivityHandlers = NULL;
73
74 void initializeThreadPool(CAConnectivityType_t type)
75 {
76 #ifdef BT_ADAPTER_TEST
77     if (CA_EDR == type && NULL == gBTThreadPool)
78     {
79         if (CA_STATUS_OK != u_thread_pool_init(3, &gBTThreadPool))
80         {
81             printf("Failed to create thread pool for BT adapter!\n");
82             return;
83         }
84     }
85 #endif
86 #ifdef WIFI_ADAPTER_TEST
87     if (CA_WIFI == type && NULL == gWiFiThreadPool)
88     {
89         if (CA_STATUS_OK != u_thread_pool_init(3, &gWiFiThreadPool))
90         {
91             printf("Failed to create thread pool for WIFI adapter!\n");
92             return;
93         }
94     }
95 #endif
96 #ifdef BLE_ADAPTER_TEST
97     if (CA_LE == type && NULL == gLEThreadPool)
98     {
99         if (CA_STATUS_OK != u_thread_pool_init(5, &gLEThreadPool))
100         {
101             printf("Failed to create thread pool for BLE adapter!\n");
102             return;
103         }
104     }
105 #endif
106 }
107
108
109 void storeInterfaceCallbacks(ConnectivityHandlerList *newHandler)
110 {
111     printf("storeInterfaceCallbacks Entry in Sample\n");
112     newHandler->nextHandler = NULL;
113     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
114
115     if (!tempConnectivityHandlers)
116     {
117         gConnectivityHandlers = newHandler;
118         printf("storeInterfaceCallbacks Exit in Sample\n");
119         return;
120     }
121
122     while (tempConnectivityHandlers->nextHandler)
123     {
124         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
125     }
126
127     tempConnectivityHandlers->nextHandler = newHandler;
128     printf("storeInterfaceCallbacks Exit in Sample\n");
129 }
130
131 void interfaceRegisterCallback(CAConnectivityHandler_t handler,
132                                CAConnectivityType_t connType)
133 {
134     printf("interfaceRegisterCallback Entry in Sample\n");
135     ConnectivityHandlerList *newConnectivityHandler = (ConnectivityHandlerList *) malloc(sizeof(
136                 ConnectivityHandlerList));
137     if (NULL == newConnectivityHandler)
138     {
139         printf("Memory allocation failed\n");
140         return;
141     }
142
143     newConnectivityHandler->type = connType;
144     newConnectivityHandler->handler = handler;
145     storeInterfaceCallbacks(newConnectivityHandler);
146     printf("interfaceRegisterCallback Exit in Sample\n");
147 }
148
149 void networkPacketHandler(CARemoteEndpoint_t *object, void *data, uint32_t dataLength)
150 {
151     printf("networkPacketHandler Entry in Sample\n");
152     if (object == NULL || data == NULL)
153     {
154         printf("NULL Object\n");
155         return;
156     }
157
158     printf("Data Received from: ");
159     if (CA_EDR == object->connectivityType)
160     {
161         printf(object->addressInfo.BT.btMacAddress);
162     }
163     else if (CA_LE == object->connectivityType)
164     {
165         printf(object->addressInfo.LE.leMacAddress);
166     }
167     else if (CA_WIFI == object->connectivityType || CA_ETHERNET == object->connectivityType)
168     {
169         printf(object->addressInfo.IP.ipAddress);
170     }
171
172     printf("\nReceived Data [Length: %d]: %s\n", dataLength, (char *)data);
173     printf("networkPacketHandler Exit in Sample\n");
174 }
175
176 void networkInterfaceCallback(CALocalConnectivity_t *localEndPoint,
177                               CANetworkStatus_t networkConnectivityState)
178 {
179     printf("networkInterfaceCallback Entry in Sample\n");
180     if (localEndPoint == NULL)
181     {
182         printf("NULL Object\n");
183         return;
184     }
185
186     if (networkConnectivityState == CA_INTERFACE_UP)
187     {
188         printf("Network Status is UP\n");
189     }
190     else
191     {
192         printf("Network Status is DOWN\n");
193     }
194
195     printf("Address: ");
196     if (CA_EDR == localEndPoint->type)
197     {
198         printf("%s\n", localEndPoint->addressInfo.BT.btMacAddress);
199     }
200     else if (CA_LE == localEndPoint->type)
201     {
202         printf("%s\n", localEndPoint->addressInfo.LE.leMacAddress);
203     }
204     else if (CA_WIFI == localEndPoint->type || CA_ETHERNET == localEndPoint->type)
205     {
206         printf("%s\n", localEndPoint->addressInfo.IP.ipAddress);
207     }
208
209     printf("networkInterfaceCallback Exit in Sample\n");
210 }
211
212
213 void freeData(void *data)
214 {
215     printf("freeData Entry in Sample\n");
216     if (data)
217     {
218         free(data);
219     }
220     printf("freeData Exit in Sample\n");
221 }
222
223 int16_t selectConnectivityType()
224 {
225     int32_t cType;
226     printf("*******Select the Connectivity Type*******\n");
227     printf("  [1] WIFI \n");
228     printf("  [2] BT \n");
229     printf("  [3] BLE \n");
230
231     fflush(stdin);
232     scanf("%d", &cType);
233     if (cType < 1 && cType > 3)
234     {
235         printf("Invalid Selection!!!!\n");
236         return 0;
237     }
238     return (int16_t)cType;
239 }
240
241 int16_t interfaceStartAdapter(CAConnectivityType_t connType)
242 {
243     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
244     if (NULL == tempConnectivityHandlers)
245     {
246         printf("None of the interface is initialized\n");
247         return 0;
248     }
249
250     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
251     {
252         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
253     }
254
255     if (NULL == tempConnectivityHandlers)
256     {
257         printf( "No interface handler for type %d", connType);
258         return 0;
259     }
260
261     if (CA_STATUS_OK != tempConnectivityHandlers->handler.startAdapter())
262     {
263         printf("Failed to Start adapter\n");
264         return 0;
265     }
266
267     return 1;
268 }
269
270 int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverType)
271 {
272     printf("interfaceMulticastStartServer Starting...\n");
273     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
274     if (NULL == tempConnectivityHandlers)
275     {
276         printf("None of the interface is initialized\n");
277         return 0;
278     }
279
280     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
281     {
282         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
283     }
284
285     if (NULL == tempConnectivityHandlers)
286     {
287         printf( "No interface handler for type %d", connType);
288         return 0;
289     }
290
291     CAAdapterStartDiscoveryServer startServer = NULL;
292     switch (serverType)
293     {
294         case 1: //Discovery server
295             startServer = tempConnectivityHandlers->handler.startDiscoverServer;
296             break;
297         case 2: //Listening server
298             startServer = tempConnectivityHandlers->handler.startListenServer;
299             break;
300     }
301
302     if (startServer)
303     {
304         printf("Invoking start server method\n");
305         startServer();
306     }
307     return 0;
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 -1;
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 -1;
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 -1;
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 -1;
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     return 0;
450 }
451
452 void interfaceReadData(CAConnectivityType_t connType)
453 {
454     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
455     if (NULL == tempConnectivityHandlers)
456     {
457         printf("None of the interface is initialized\n");
458         return;
459     }
460
461     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
462     {
463         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
464     }
465
466     if (NULL == tempConnectivityHandlers)
467     {
468         printf( "No interface handler for type %d", connType);
469         return;
470     }
471
472     if (CA_STATUS_OK != tempConnectivityHandlers->handler.readData())
473     {
474         printf("Failed to Read Data\n");
475         return;
476     }
477
478     printf("Read Data is successfull\n");
479     return;
480 }
481
482 void interfaceGetNetworkInfo(CAConnectivityType_t connType)
483 {
484     int i = 0;
485
486     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
487     if (NULL == tempConnectivityHandlers)
488     {
489         printf("None of the interface is initialized\n");
490         return;
491     }
492
493     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
494     {
495         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
496     }
497
498     if (NULL == tempConnectivityHandlers)
499     {
500         printf( "No interface handler for type %d", connType);
501         return;
502     }
503
504     //Get the network interface info
505     CALocalConnectivity_t *info = NULL;
506     uint32_t size = 0;
507     if (CA_STATUS_OK != tempConnectivityHandlers->handler.GetnetInfo(&info, &size))
508     {
509         printf("Failed to get network info\n");
510         return;
511     }
512
513     if (0 >= size || info == NULL)
514     {
515         printf("No network found !!!\n");
516         return;
517     }
518
519     printf("Network Information: \n");
520     for (; i < size; i++)
521     {
522         if (connType == CA_WIFI)
523         {
524             printf("Type : %s\n", (connType == CA_ETHERNET) ? "CA_ETHERNET" : "CA_WIFI");
525             printf("Address : %s\n", info[i].addressInfo.IP.ipAddress);
526         }
527         else
528         {
529             printf("Type : %s\n", (connType == CA_EDR) ? "CA_EDR" : "CA_LE");
530             printf("Address : %s\n\n", info[i].addressInfo.BT.btMacAddress);
531         }
532     }
533 }
534
535 int16_t interfaceStopAdapter(CAConnectivityType_t connType)
536 {
537     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
538     if (NULL == tempConnectivityHandlers)
539     {
540         printf("None of the interface is initialized\n");
541         return 0;
542     }
543
544     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
545     {
546         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
547     }
548
549     if (NULL == tempConnectivityHandlers)
550     {
551         printf( "No interface handler for type %d", connType);
552         return 0;
553     }
554
555     if (CA_STATUS_OK != tempConnectivityHandlers->handler.stopAdapter())
556     {
557         printf("Failed to Stop adapter\n");
558         return 0;
559     }
560
561     printf("Stopped the adapter\n");
562     return 1;
563 }
564
565 void testInitializeInterface()
566 {
567     printf("testInitializeInterface Entry\n");
568
569     int16_t type = selectConnectivityType();
570     if (0 >= type || 3 < type)
571     {
572         printf("Invalid selection...\n");
573         return;
574     }
575
576     switch (type)
577     {
578 #ifdef WIFI_ADAPTER_TEST
579         case 1: //WIFI
580             {
581                 testInitializeWIFIInterface();
582             }
583             break;
584 #endif
585 #ifdef BT_ADAPTER_TEST
586         case 2:   //BT
587             {
588                 testInitializeBTInterface();
589             }
590             break;
591 #endif
592 #ifdef BLE_ADAPTER_TEST
593         case 3: //BLE
594             {
595                 testInitializeBLEInterface();
596             }
597             break;
598 #endif
599         default:
600             printf("Feature is not enabled or not implemented\n");
601     }
602 }
603
604 void testTerminateInterface()
605 {
606     int16_t type = selectConnectivityType();
607     if (0 >= type || 3 < type)
608     {
609         printf("Invalid selection...\n");
610         return;
611     }
612
613 #ifdef WIFI_ADAPTER_TEST
614     if (1 == type)   /* WIFI */
615     {
616         testTerminateWIFIInterface();
617     }
618 #endif
619 #ifdef BT_ADAPTER_TEST
620     if (2 == type)   /*BT*/
621     {
622         testTerminateBTInterface();
623     }
624 #endif
625 #ifdef BLE_ADAPTER_TEST
626     if (3 == type)   /*BLE*/
627     {
628         testTerminateBLEInterface();
629     }
630 #endif
631
632     ConnectivityHandlerList *currentConnectivityHandler = gConnectivityHandlers;
633     ConnectivityHandlerList *prevConnectivityHandler = NULL;
634
635     printf("Linked list delete start\n");
636     while (currentConnectivityHandler != NULL)
637     {
638         printf("Iterating through the list to find the matching interface\n");
639         if (currentConnectivityHandler->type == type)
640         {
641             printf("Matching interface found\n");
642             if (prevConnectivityHandler == NULL)
643             {
644                 currentConnectivityHandler = currentConnectivityHandler->nextHandler;
645                 freeData(gConnectivityHandlers);
646                 gConnectivityHandlers = NULL;
647                 printf( "Node deleted with interface type %d", type);
648             }
649             else
650             {
651                 prevConnectivityHandler->nextHandler = currentConnectivityHandler->nextHandler;
652                 freeData(currentConnectivityHandler);
653                 currentConnectivityHandler = prevConnectivityHandler->nextHandler;
654                 printf( "Node deleted with interface type %d from linked list", type);
655             }
656         }
657         else
658         {
659             prevConnectivityHandler = currentConnectivityHandler;
660             currentConnectivityHandler = currentConnectivityHandler->nextHandler;
661         }
662     }
663     gConnectivityHandlers = NULL;
664     return;
665 }
666
667 void testStartAdapter()
668 {
669     int type = selectConnectivityType();
670     if (0 >= type || 3 < type)
671     {
672         printf("Invalid selection...\n");
673         return;
674     }
675
676     switch (type)
677     {
678 #ifdef WIFI_ADAPTER_TEST
679         case 1: //WIFI
680             {
681                 interfaceStartAdapter(CA_WIFI);
682             }
683             break;
684 #endif
685 #ifdef BT_ADAPTER_TEST
686         case 2:   //BT
687             {
688                 interfaceStartAdapter(CA_EDR);
689             }
690             break;
691 #endif
692 #ifdef BLE_ADAPTER_TEST
693         case 3: //BLE
694             {
695                 interfaceStartAdapter(CA_LE);
696             }
697             break;
698 #endif
699         default:
700             printf("Feature is not enabled or not implemented\n");
701     }
702 }
703
704 void testStartServer(int serverType)
705 {
706     int type = selectConnectivityType();
707     if (0 >= type || 3 < type)
708     {
709         printf("Invalid selection...\n");
710         return;
711     }
712
713     switch (type)
714     {
715 #ifdef WIFI_ADAPTER_TEST
716         case 1: //WIFI
717             {
718                 interfaceMulticastStartServer(CA_WIFI, serverType);
719             }
720             break;
721 #endif
722 #ifdef BT_ADAPTER_TEST
723         case 2:   //BT
724             {
725                 interfaceMulticastStartServer(CA_EDR, serverType);
726             }
727             break;
728 #endif
729 #ifdef BLE_ADAPTER_TEST
730         case 3: //BLE
731             {
732                 interfaceMulticastStartServer(CA_LE, serverType);
733             }
734             break;
735 #endif
736         default:
737             printf("Feature is not enabled or not implemented\n");
738     }
739 }
740
741 void testSendUnicastData()
742 {
743     int16_t type = selectConnectivityType();
744     if (0 >= type || 3 < type)
745     {
746         printf("Invalid selection...\n");
747         return;
748     }
749
750     switch (type)
751     {
752 #ifdef WIFI_ADAPTER_TEST
753         case 1: //WIFI
754             {
755                 interfaceSendUnicastData(CA_WIFI);
756             }
757             break;
758 #endif
759 #ifdef BT_ADAPTER_TEST
760         case 2:   //BT
761             {
762                 interfaceSendUnicastData(CA_EDR);
763             }
764             break;
765 #endif
766 #ifdef BLE_ADAPTER_TEST
767         case 3: //BLE
768             {
769                 interfaceSendUnicastData(CA_LE);
770             }
771             break;
772 #endif
773         default:
774             printf("Feature is not enabled or not implemented\n");
775     }
776 }
777
778 void testSendMulticastData()
779 {
780     int16_t type = selectConnectivityType();
781     if (0 >= type || 3 < type)
782     {
783         printf("Invalid selection...\n");
784         return;
785     }
786
787     switch (type)
788     {
789 #ifdef WIFI_ADAPTER_TEST
790         case 1: //WIFI
791             {
792                 interfaceSendMulticastData(CA_WIFI);
793             }
794             break;
795 #endif
796 #ifdef BT_ADAPTER_TEST
797         case 2:   //BT
798             {
799                 interfaceSendMulticastData(CA_EDR);
800             }
801             break;
802 #endif
803 #ifdef BLE_ADAPTER_TEST
804         case 3: //BLE
805             {
806                 interfaceSendMulticastData(CA_LE);
807             }
808             break;
809 #endif
810         default:
811             printf("Feature is not enabled or not implemented\n");
812     }
813 }
814
815 void testReadData(void)
816 {
817     int16_t type = selectConnectivityType();
818     if (0 >= type || 3 < type)
819     {
820         printf("Invalid selection...\n");
821         return;
822     }
823
824     switch (type)
825     {
826 #ifdef WIFI_ADAPTER_TEST
827         case 1: //WIFI
828             {
829                 interfaceReadData(CA_WIFI);
830             }
831             break;
832 #endif
833 #ifdef BT_ADAPTER_TEST
834         case 2:   //BT
835             {
836                 interfaceReadData(CA_EDR);
837             }
838             break;
839 #endif
840 #ifdef BLE_ADAPTER_TEST
841         case 3: //BLE
842             {
843                 interfaceReadData(CA_LE);
844             }
845             break;
846 #endif
847         default:
848             printf("Feature is not enabled or not implemented\n");
849     }
850 }
851
852 void testGetNetworkInfo(void)
853 {
854     int16_t type = selectConnectivityType();
855     if (0 >= type || 3 < type)
856     {
857         printf("Invalid selection...\n");
858         return;
859     }
860
861     switch (type)
862     {
863 #ifdef WIFI_ADAPTER_TEST
864         case 1: //WIFI
865             {
866                 interfaceGetNetworkInfo(CA_WIFI);
867             }
868             break;
869 #endif
870 #ifdef BT_ADAPTER_TEST
871         case 2:   //BT
872             {
873                 interfaceGetNetworkInfo(CA_EDR);
874             }
875             break;
876 #endif
877 #ifdef BLE_ADAPTER_TEST
878         case 3: //BLE
879             {
880                 interfaceGetNetworkInfo(CA_LE);
881             }
882             break;
883 #endif
884         default:
885             printf("Feature is not enabled or not implemented\n");
886     }
887 }
888
889 void testStopAdapter()
890 {
891     int16_t type = selectConnectivityType();
892     if (0 >= type || 3 < type)
893     {
894         printf("Invalid selection...\n");
895         return;
896     }
897
898     switch (type)
899     {
900 #ifdef WIFI_ADAPTER_TEST
901         case 1: //WIFI
902             {
903                 interfaceStopAdapter(CA_WIFI);
904             }
905             break;
906 #endif
907 #ifdef BT_ADAPTER_TEST
908         case 2:   //BT
909             {
910                 interfaceStopAdapter(CA_EDR);
911             }
912             break;
913 #endif
914 #ifdef BLE_ADAPTER_TEST
915         case 3: //BLE
916             {
917                 interfaceStopAdapter(CA_LE);
918             }
919             break;
920 #endif
921         default:
922             printf("Feature is not enabled or not implemented\n");
923     }
924 }
925
926 #ifdef BT_ADAPTER_TEST
927 void testInitializeBTInterface(void)
928 {
929     printf("Initiazing EDR\n");
930
931     printf("Initializing BT Adapter threadpool\n");
932     initializeThreadPool(CA_EDR);
933
934     //Start bluetooth communication adapter
935     CAResult_t err = CAInitializeEDR(interfaceRegisterCallback, networkPacketHandler,
936                                      networkInterfaceCallback, gBTThreadPool);
937     if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
938     {
939         printf("Failed to initialize bluetooth communication adapter!\n");
940     }
941 }
942
943 void testTerminateBTInterface(void)
944 {
945     printf("Terminating EDR\n");
946
947     //Terminate the Bluetooth communication adapter
948     CATerminateEDR();
949
950     printf( "Terminating BT Adapter thread pool");
951     u_thread_pool_free(gBTThreadPool);
952     gBTThreadPool = NULL;
953 }
954 #endif //BT_ADAPTER_TEST
955
956 #ifdef WIFI_ADAPTER_TEST
957 void testInitializeWIFIInterface(void)
958 {
959     printf("testInitializeWIFIInterface IN\n");
960
961     printf("Initializing WIFI adapter threadpool\n");
962     initializeThreadPool(CA_WIFI);
963
964     //Start Wifi communication adapter
965     if (0 != CAInitializeWifi(interfaceRegisterCallback, networkPacketHandler,
966                               networkInterfaceCallback, gWiFiThreadPool))
967     {
968         printf("testInitializeWIFIInterface Failed to initialize bluetooth communication adapter\n");
969         return;
970     }
971
972     printf("testInitializeWIFIInterface OUT\n");
973 }
974
975 void testTerminateWIFIInterface(void)
976 {
977     printf("testTerminateWIFIInterface IN\n");
978
979     // Stop if Wifi communication adapter is running
980     interfaceStopAdapter(CA_WIFI);
981
982     // Freeing threadpool for wifi communication adapter
983     printf( "Terminating WIFI Adapter thread pool");
984     u_thread_pool_free(gWiFiThreadPool);
985     gWiFiThreadPool = NULL;
986
987     //Terminate the Wifi communication adapter
988     CATerminateWIfI();
989
990
991     printf("testTerminateWIFIInterface OUT\n");
992 }
993 #endif //WIFI_ADAPTER_TEST
994
995 #ifdef BLE_ADAPTER_TEST
996 void testInitializeBLEInterface(void)
997 {
998     printf("testInitializeBLEInterface IN\n");
999
1000     printf("Initializing BLE adapter threadpool\n");
1001     initializeThreadPool(CA_LE);
1002
1003     //Start bluetooth communication adapter
1004     if (0 != CAInitializeLE(interfaceRegisterCallback, networkPacketHandler,
1005                             networkInterfaceCallback, gLEThreadPool))
1006     {
1007         printf("testInitializeBLEInterface Failed due to CAInitializeLE\n");
1008         return;
1009     }
1010
1011     printf("testInitializeBLEInterface OUT\n");
1012 }
1013
1014 void testTerminateBLEInterface(void)
1015 {
1016     printf("testTerminateBLEInterface IN\n");
1017
1018     //Terminate the BLE server & Client
1019     CATerminateLE();
1020
1021     printf( "Terminating BLE Adapter thread pool");
1022     u_thread_pool_free(gLEThreadPool);
1023     gLEThreadPool = NULL;
1024
1025     printf("testTerminateBLEInterface OUT\n");
1026 }
1027 #endif //BLE_ADAPTER_TEST
1028
1029 static void testPrintHelp(void)
1030 {
1031     printf(" =====================================================================\n");
1032     printf("|                 Welcome to Connectivity Abstraction                 |\n");
1033     printf("|                   - CA Unit Test v1.0 -                             |\n");
1034     printf("|---------------------------------------------------------------------|\n");
1035     printf("|                           ** Options **                             |\n");
1036     printf("|  i - Initialize the Interface                                       |\n");
1037     printf("|  d - Terminate the Interface                                        |\n");
1038     printf("|  a - Start adapter                                                  |\n");
1039     printf("|  b - Stop adapter                                                   |\n");
1040     printf("|  sd- Start Discovery Server                                         |\n");
1041     printf("|  sl- Start Listening Server                                         |\n");
1042     printf("|  u - Send Unicast Data                                              |\n");
1043     printf("|  m - Send Multicast Data                                            |\n");
1044     printf("|  g - Get Network Info                                               |\n");
1045     printf("|  r - Read data synchronously                                        |\n");
1046     printf("|  x - quit the test.                                                 |\n");
1047     printf("|  h - Help menu.                                                     |\n");
1048     printf(" =====================================================================\n");
1049 }
1050
1051 static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer data)
1052 {
1053     gchar buf[10] = {'\0'};
1054     gsize read = 0;
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