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