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