Merge "Implementation of connectivity abstraction feature release v0.2" into connecti...
[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 //Comment any below for blocking testing of specific adapters
37 //#define WIFI_ADAPTER_TEST
38 #define BT_ADAPTER_TEST
39 //#define BLE_ADAPTER_TEST
40
41 #if defined(WIFI_ADAPTER_TEST)
42 #include "cawifiadapter.h"
43 #include "caethernetadapter.h"
44 static u_thread_pool_t gWiFiThreadPool = NULL;
45 #elif defined(BT_ADAPTER_TEST)
46 #include "caedradapter.h"
47 static  u_thread_pool_t gBTThreadPool = NULL;
48 #elif defined(BLE_ADAPTER_TEST)
49 #include "caleadapter.h"
50 static u_thread_pool_t gLEThreadPool = NULL;
51 #endif
52
53 static GMainLoop *mainloop;
54 static GIOChannel *channel;
55 static guint g_test_io_watch_id;
56 static GError *g_err_Sample;
57
58 static CALocalConnectivity_t *localWifiEndpoint = NULL;
59 //Hardcoded coap data for Test
60 static char coapData[500] =
61     "{\"oc:\[{href\":\"/a/light\",\"ref\":{\"power\":\"20\",\"state\":\"true\"}}]}";
62
63 void testInitializeBTInterface(void);
64 void testTerminateBTInterface(void);
65 void testInitializeWIFIInterface(void);
66 void testTerminateWIFIInterface(void);
67 void testInitializeBLEInterface(void);
68 void testTerminateBLEInterface(void);
69
70 typedef struct ConnectivityHandlerList
71 {
72     CAConnectivityType_t type;
73     CAConnectivityHandler_t handler;
74     struct ConnectivityHandlerList *nextHandler;
75 } ConnectivityHandlerList;
76
77 static ConnectivityHandlerList *gConnectivityHandlers = NULL;
78
79 void initializeThreadPool(CAConnectivityType_t type)
80 {
81 #ifdef BT_ADAPTER_TEST
82         if (CA_EDR == type && NULL == gBTThreadPool)
83         {
84         if (CA_STATUS_OK != u_thread_pool_init(3, &gBTThreadPool))
85         {
86             printf("Failed to create thread pool for BT adapter!\n");
87             return;
88         }
89         }
90 #endif
91 #ifdef WIFI_ADAPTER_TEST
92         if (CA_WIFI == type && NULL == gWiFiThreadPool)
93         {
94         if (CA_STATUS_OK != u_thread_pool_init(3, &gWiFiThreadPool))
95         {
96             printf("Failed to create thread pool for BT adapter!\n");
97             return;
98         }
99         }
100 #endif
101 #ifdef BLE_ADAPTER_TEST
102         if (CA_LE == type && NULL == gLEThreadPool)
103         {
104         if (CA_STATUS_OK != u_thread_pool_init(3, &gLEThreadPool))
105         {
106             printf("Failed to create thread pool for BT adapter!\n");
107             return;
108         }
109         }
110 #endif
111 }
112
113
114 void storeInterfaceCallbacks(ConnectivityHandlerList *newHandler)
115 {
116     printf("storeInterfaceCallbacks Entry in Sample\n");
117     newHandler->nextHandler = NULL;
118     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
119
120     if (!tempConnectivityHandlers)
121     {
122         gConnectivityHandlers = newHandler;
123         printf("storeInterfaceCallbacks Exit in Sample\n");
124         return;
125     }
126
127     while (tempConnectivityHandlers->nextHandler)
128     {
129         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
130     }
131
132     tempConnectivityHandlers->nextHandler = newHandler;
133     printf("storeInterfaceCallbacks Exit in Sample\n");
134 }
135
136 void interfaceRegisterCallback(CAConnectivityHandler_t handler,
137                                CAConnectivityType_t connType)
138 {
139     printf("interfaceRegisterCallback Entry in Sample\n");
140     ConnectivityHandlerList *newConnectivityHandler = (ConnectivityHandlerList *) malloc(sizeof(
141                 ConnectivityHandlerList));
142     if (NULL == newConnectivityHandler)
143     {
144         printf("Memory allocation failed\n");
145         return;
146     }
147
148     newConnectivityHandler->type = connType;
149     newConnectivityHandler->handler = handler;
150     storeInterfaceCallbacks(newConnectivityHandler);
151     printf("interfaceRegisterCallback Exit in Sample\n");
152 }
153
154 void networkPacketHandler(CARemoteEndpoint_t *object, void *data, uint32_t dataLength)
155 {
156     printf("networkPacketHandler Entry in Sample\n");
157     if (object == NULL || data == NULL)
158     {
159         printf("NULL Object\n");
160         return;
161     }
162
163     printf("Data Received from: ");
164     if (CA_EDR == object->connectivityType)
165     {
166         printf(object->addressInfo.BT.btMacAddress);
167     }
168     else if (CA_LE == object->connectivityType)
169     {
170                 printf(object->addressInfo.LE.leMacAddress);
171     }
172     else if (CA_WIFI == object->connectivityType || CA_ETHERNET == object->connectivityType)
173     {
174         printf(object->addressInfo.IP.ipAddress);
175     }
176
177     printf("\nReceived Data [Length: %d]: %s\n",dataLength,(char *)data);
178     printf("networkPacketHandler Exit in Sample\n");
179 }
180
181 void networkInterfaceCallback(CALocalConnectivity_t *localEndPoint,
182                               CANetworkStatus_t networkConnectivityState)
183 {
184     printf("networkInterfaceCallback Entry in Sample\n");
185     if (localEndPoint == NULL)
186     {
187         printf("NULL Object\n");
188         return;
189     }
190
191     if (networkConnectivityState == CA_INTERFACE_UP)
192     {
193         printf("Network Status is UP\n");
194     }
195     else
196     {
197         printf("Network Status is DOWN\n");
198     }
199
200     printf("Address: ");
201     if (CA_EDR == localEndPoint->type)
202     {
203         printf("%s\n", localEndPoint->addressInfo.BT.btMacAddress);
204     }
205     else if (CA_LE == localEndPoint->type)
206     {
207         printf("%s\n", localEndPoint->addressInfo.LE.leMacAddress);
208     }
209     else if (CA_WIFI == localEndPoint->type || CA_ETHERNET == localEndPoint->type)
210     {
211         printf("%s\n", localEndPoint->addressInfo.IP.ipAddress);
212     }
213
214     printf("networkInterfaceCallback Exit in Sample\n");
215 }
216
217
218 void freeData(void *data)
219 {
220     printf("freeData Entry in Sample\n");
221     if (data)
222     {
223         free(data);
224     }
225     printf("freeData Exit in Sample\n");
226 }
227
228 int16_t selectConnectivityType()
229 {
230     int32_t cType;
231     printf("*******Select the Connectivity Type*******\n");
232     printf("  [1] WIFI \n");
233     printf("  [2] BT \n");
234     printf("  [3] BLE \n");
235
236     fflush(stdin);
237     scanf("%d", &cType);
238     if (cType < 1 && cType > 3)
239     {
240         printf("Invalid Selection!!!!\n");
241         return 0;
242     }
243     return (int16_t)cType;
244 }
245
246 int16_t interfaceStartAdapter(CAConnectivityType_t connType)
247 {
248     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
249     if (NULL == tempConnectivityHandlers)
250     {
251         printf("None of the interface is initialized\n");
252         return 0;
253     }
254
255     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
256     {
257         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
258     }
259
260     if (NULL == tempConnectivityHandlers)
261     {
262         printf( "No interface handler for type %d", connType);
263         return 0;
264     }
265
266     if (CA_STATUS_OK != tempConnectivityHandlers->handler.startAdapter())
267     {
268         printf("Failed to Start adapter\n");
269         return 0;
270     }
271
272     return 1;
273 }
274
275 int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverType)
276 {
277     printf("interfaceMulticastStartServer Starting...\n");
278     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
279     if (NULL == tempConnectivityHandlers)
280     {
281         printf("None of the interface is initialized\n");
282         return 0;
283     }
284
285     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
286     {
287         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
288     }
289
290     if (NULL == tempConnectivityHandlers)
291     {
292         printf( "No interface handler for type %d", connType);
293         return 0;
294     }
295
296     CAAdapterStartDiscoveryServer startServer = NULL;
297     switch (serverType)
298     {
299         case 1: //Discovery server
300             startServer = tempConnectivityHandlers->handler.startDiscoverServer;
301             break;
302         case 2: //Listening server
303             startServer = tempConnectivityHandlers->handler.startListenServer;
304             break;
305         case 3: //Notification server
306             startServer = tempConnectivityHandlers->handler.startNotifyServer;
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 void interfaceSendNotification(CAConnectivityType_t connType)
428 {
429     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
430     if (NULL == tempConnectivityHandlers)
431     {
432         printf(" None of the interface is initialized \n");
433         return;
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\n", connType);
444         return;
445     }
446
447     if (CA_WIFI == connType)
448     {
449         CARemoteEndpoint_t remoteEndpoint;
450         tempConnectivityHandlers->handler.sendNotification(&remoteEndpoint, coapData, strlen(coapData));
451     }
452     else if (CA_EDR == connType)
453     {
454         //create endpoint
455         CARemoteEndpoint_t endpoint;
456
457         //Get the device address from user
458         char deviceaddress[100] = {0};
459         printf("Enter the device address: \n");
460         scanf("%s", deviceaddress);
461
462         if (strlen(deviceaddress) == 0)
463         {
464             printf("Invlaid device address\n");
465             return;
466         }
467
468         //Get the service uuid from user
469         char uuid[100] = {0};
470         printf("Enter the service uuid: \n");
471         scanf("%s", uuid);
472
473         if (strlen(uuid) == 0)
474         {
475             printf("Invlaid service uuid\n");
476             return;
477         }
478
479         endpoint.connectivityType = CA_EDR;
480         strncpy(endpoint.addressInfo.BT.btMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
481         endpoint.addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
482         endpoint.resourceUri = strdup(uuid);
483
484         tempConnectivityHandlers->handler.sendNotification(&endpoint, coapData, strlen(coapData));
485         printf("Sent Unicast data \n");
486         free(endpoint.resourceUri);
487     }
488     else if (CA_LE == connType)
489     {
490         //create endpoint
491         CARemoteEndpoint_t endpoint;
492
493         //Get the device address from user
494         char deviceaddress[100] = {0};
495         printf("Enter the device address: \n");
496         scanf("%s", deviceaddress);
497
498         if (strlen(deviceaddress) == 0)
499         {
500             printf("Invlaid device address\n");
501             return;
502         }
503
504         //Get the service uuid from user
505         char uuid[100] = {0};
506         printf("Enter the service uuid: \n");
507         scanf("%s", uuid);
508
509         if (strlen(uuid) == 0)
510         {
511             printf("Invlaid service uuid\n");
512             return;
513         }
514
515         endpoint.connectivityType = CA_LE;
516         strncpy(endpoint.addressInfo.BT.btMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
517         endpoint.addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
518         endpoint.resourceUri = strdup(uuid);
519
520         tempConnectivityHandlers->handler.sendNotification(&endpoint, coapData, strlen(coapData));
521         printf("Sent Unicast data \n");
522         free(endpoint.resourceUri);
523         printf("Feature is not implemented !!\n");
524     }
525 }
526
527 int16_t interfaceSendMulticastData(CAConnectivityType_t connType)
528 {
529     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
530     if (NULL == tempConnectivityHandlers)
531     {
532         printf("None of the interface is initialized\n");
533         return 0;
534     }
535
536     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
537     {
538         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
539     }
540
541     if (NULL == tempConnectivityHandlers)
542     {
543         printf( "No interface handler for type %d", connType);
544         return 0;
545     }
546
547     if (connType == CA_WIFI)
548     {
549         tempConnectivityHandlers->handler.sendDataToAll(coapData,
550                 strlen(coapData) + 1);
551     }
552     else if (connType == CA_EDR || connType == CA_LE)
553     {
554         tempConnectivityHandlers->handler.sendDataToAll(coapData, strlen(coapData));
555     }
556 }
557
558 void interfaceReadData(CAConnectivityType_t connType)
559 {
560     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
561     if (NULL == tempConnectivityHandlers)
562     {
563         printf("None of the interface is initialized\n");
564         return;
565     }
566
567     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
568     {
569         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
570     }
571
572     if (NULL == tempConnectivityHandlers)
573     {
574         printf( "No interface handler for type %d", connType);
575         return;
576     }
577
578     if (CA_STATUS_OK != tempConnectivityHandlers->handler.readData())
579     {
580         printf("Failed to Read Data\n");
581         return;
582     }
583
584     printf("Read Data is successfull\n");
585     return;
586 }
587
588 void interfaceGetNetworkInfo(CAConnectivityType_t connType)
589 {
590     int i = 0;
591
592     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
593     if (NULL == tempConnectivityHandlers)
594     {
595         printf("None of the interface is initialized\n");
596         return;
597     }
598
599     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
600     {
601         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
602     }
603
604     if (NULL == tempConnectivityHandlers)
605     {
606         printf( "No interface handler for type %d", connType);
607         return;
608     }
609
610     //Get the network interface info
611     CALocalConnectivity_t *info = NULL;
612     uint32_t size = 0;
613     if (CA_STATUS_OK != tempConnectivityHandlers->handler.GetnetInfo(&info, &size))
614     {
615         printf("Failed to get network info\n");
616         return;
617     }
618
619     if (0 >= size || info == NULL)
620     {
621         printf("No network found !!!\n");
622         return;
623     }
624
625     printf("Network Information: \n");
626     for (; i < size; i++)
627     {
628         if (connType == CA_WIFI)
629         {
630             printf("Type : %s\n", (connType == CA_ETHERNET) ? "CA_ETHERNET" : "CA_WIFI");
631             printf("Address : %s\n", info[i].addressInfo.IP.ipAddress);
632         }
633         else
634         {
635             printf("Type : %s\n", (connType == CA_EDR) ? "CA_EDR" : "CA_LE");
636             printf("Address : %s\n\n", info[i].addressInfo.BT.btMacAddress);
637         }
638     }
639 }
640
641 int16_t interfaceStopAdapter(CAConnectivityType_t connType)
642 {
643     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
644     if (NULL == tempConnectivityHandlers)
645     {
646         printf("None of the interface is initialized\n");
647         return 0;
648     }
649
650     while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
651     {
652         tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
653     }
654
655     if (NULL == tempConnectivityHandlers)
656     {
657         printf( "No interface handler for type %d", connType);
658         return 0;
659     }
660
661     if (CA_STATUS_OK != tempConnectivityHandlers->handler.stopAdapter())
662     {
663         printf("Failed to Stop adapter\n");
664         return 0;
665     }
666
667     printf("Stopped the adapter\n");
668     return 1;
669 }
670
671 void testInitializeInterface()
672 {
673     printf("testInitializeInterface Entry\n");
674
675     int16_t 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                 testInitializeWIFIInterface();
688             }
689             break;
690 #endif
691 #ifdef BT_ADAPTER_TEST
692         case 2:   //BT
693             {
694                 testInitializeBTInterface();
695             }
696             break;
697 #endif
698 #ifdef BLE_ADAPTER_TEST
699         case 3: //BLE
700             {
701                 testInitializeBLEInterface();
702             }
703             break;
704 #endif
705         default:
706             printf("Feature is not enabled or not implemented\n");
707     }
708 }
709
710 void testTerminateInterface()
711 {
712     int16_t type = selectConnectivityType();
713     if (0 >= type || 3 < type)
714     {
715         printf("Invalid selection...\n");
716         return;
717     }
718
719 #ifdef WIFI_ADAPTER_TEST
720     if (1 == type)   /* WIFI */
721     {
722         testTerminateWIFIInterface();
723     }
724 #endif
725 #ifdef BT_ADAPTER_TEST
726     if (2 == type)   /*BT*/
727     {
728         testTerminateBTInterface();
729     }
730 #endif
731 #ifdef BLE_ADAPTER_TEST
732     if (3 == type)   /*BLE*/
733     {
734         testTerminateBLEInterface();
735     }
736 #endif
737
738     ConnectivityHandlerList *currentConnectivityHandler = gConnectivityHandlers;
739     ConnectivityHandlerList *prevConnectivityHandler = NULL;
740
741     printf("Linked list delete start\n");
742     while (currentConnectivityHandler != NULL)
743     {
744         printf("Iterating through the list to find the matching interface\n");
745         if (currentConnectivityHandler->type == type)
746         {
747             printf("Matching interface found\n");
748             if (prevConnectivityHandler == NULL)
749             {
750                 currentConnectivityHandler = currentConnectivityHandler->nextHandler;
751                 freeData(gConnectivityHandlers);
752                 gConnectivityHandlers = NULL;
753                 printf( "Node deleted with interface type %d", type);
754             }
755             else
756             {
757                 prevConnectivityHandler->nextHandler = currentConnectivityHandler->nextHandler;
758                 freeData(currentConnectivityHandler);
759                 currentConnectivityHandler = prevConnectivityHandler->nextHandler;
760                 printf( "Node deleted with interface type %d from linked list", type);
761             }
762         }
763         else
764         {
765             prevConnectivityHandler = currentConnectivityHandler;
766             currentConnectivityHandler = currentConnectivityHandler->nextHandler;
767         }
768     }
769     gConnectivityHandlers = NULL;
770     return;
771 }
772
773 void testStartAdapter()
774 {
775     int type = selectConnectivityType();
776     if (0 >= type || 3 < type)
777     {
778         printf("Invalid selection...\n");
779         return;
780     }
781
782     switch (type)
783     {
784 #ifdef WIFI_ADAPTER_TEST
785         case 1: //WIFI
786             {
787                 interfaceStartAdapter(CA_WIFI);
788             }
789             break;
790 #endif
791 #ifdef BT_ADAPTER_TEST
792         case 2:   //BT
793             {
794                 interfaceStartAdapter(CA_EDR);
795             }
796             break;
797 #endif
798 #ifdef BLE_ADAPTER_TEST
799         case 3: //BLE
800             {
801                 interfaceStartAdapter(CA_LE);
802             }
803             break;
804 #endif
805         default:
806             printf("Feature is not enabled or not implemented\n");
807     }
808 }
809
810 void testStartServer(int serverType)
811 {
812     int type = selectConnectivityType();
813     if (0 >= type || 3 < type)
814     {
815         printf("Invalid selection...\n");
816         return;
817     }
818
819     switch (type)
820     {
821 #ifdef WIFI_ADAPTER_TEST
822         case 1: //WIFI
823             {
824                 interfaceMulticastStartServer(CA_WIFI, serverType);
825             }
826             break;
827 #endif
828 #ifdef BT_ADAPTER_TEST
829         case 2:   //BT
830             {
831                 interfaceMulticastStartServer(CA_EDR, serverType);
832             }
833             break;
834 #endif
835 #ifdef BLE_ADAPTER_TEST
836         case 3: //BLE
837             {
838                 interfaceMulticastStartServer(CA_LE, serverType);
839             }
840             break;
841 #endif
842         default:
843             printf("Feature is not enabled or not implemented\n");
844     }
845 }
846
847 void testSendUnicastData()
848 {
849     int16_t type = selectConnectivityType();
850     if (0 >= type || 3 < type)
851     {
852         printf("Invalid selection...\n");
853         return;
854     }
855
856     switch (type)
857     {
858 #ifdef WIFI_ADAPTER_TEST
859         case 1: //WIFI
860             {
861                 interfaceSendUnicastData(CA_WIFI);
862             }
863             break;
864 #endif
865 #ifdef BT_ADAPTER_TEST
866         case 2:   //BT
867             {
868                 interfaceSendUnicastData(CA_EDR);
869             }
870             break;
871 #endif
872 #ifdef BLE_ADAPTER_TEST
873         case 3: //BLE
874             {
875                 interfaceSendUnicastData(CA_LE);
876             }
877             break;
878 #endif
879         default:
880             printf("Feature is not enabled or not implemented\n");
881     }
882 }
883
884 void testSendNotification()
885 {
886     int16_t type = selectConnectivityType();
887     if (0 >= type || 3 < type)
888     {
889         printf("Invalid selection...\n");
890         return;
891     }
892
893     switch (type)
894     {
895 #ifdef WIFI_ADAPTER_TEST
896         case 1: //WIFI
897             {
898                 interfaceSendNotification(CA_WIFI);
899             }
900             break;
901 #endif
902 #ifdef BT_ADAPTER_TEST
903         case 2:   //BT
904             {
905                 interfaceSendNotification(CA_EDR);
906             }
907             break;
908 #endif
909 #ifdef BLE_ADAPTER_TEST
910         case 3: //BLE
911             {
912                 interfaceSendNotification(CA_LE);
913             }
914             break;
915 #endif
916         default:
917             printf("Feature is not enabled or not implemented\n");
918     }
919 }
920
921 void testSendMulticastData()
922 {
923     int16_t type = selectConnectivityType();
924     if (0 >= type || 3 < type)
925     {
926         printf("Invalid selection...\n");
927         return;
928     }
929
930     switch (type)
931     {
932 #ifdef WIFI_ADAPTER_TEST
933         case 1: //WIFI
934             {
935                 interfaceSendMulticastData(CA_WIFI);
936             }
937             break;
938 #endif
939 #ifdef BT_ADAPTER_TEST
940         case 2:   //BT
941             {
942                 interfaceSendMulticastData(CA_EDR);
943             }
944             break;
945 #endif
946 #ifdef BLE_ADAPTER_TEST
947         case 3: //BLE
948             {
949                 interfaceSendMulticastData(CA_LE);
950             }
951             break;
952 #endif
953         default:
954             printf("Feature is not enabled or not implemented\n");
955     }
956 }
957
958 void testReadData(void)
959 {
960         int16_t type = selectConnectivityType();
961         if (0 >= type || 3 < type)
962         {
963                 printf("Invalid selection...\n");
964                 return;
965         }
966
967         switch (type)
968         {
969 #ifdef WIFI_ADAPTER_TEST
970                 case 1: //WIFI
971                         {
972                                 interfaceReadData(CA_WIFI);
973                         }
974                         break;
975 #endif
976 #ifdef BT_ADAPTER_TEST
977                 case 2:   //BT
978                         {
979                                 interfaceReadData(CA_EDR);
980                         }
981                         break;
982 #endif
983 #ifdef BLE_ADAPTER_TEST
984                 case 3: //BLE
985                         {
986                                 interfaceReadData(CA_LE);
987                         }
988                         break;
989 #endif
990                 default:
991                         printf("Feature is not enabled or not implemented\n");
992         }
993 }
994
995 void testGetNetworkInfo(void)
996 {
997     int16_t type = selectConnectivityType();
998     if (0 >= type || 3 < type)
999     {
1000         printf("Invalid selection...\n");
1001         return;
1002     }
1003
1004     switch (type)
1005     {
1006 #ifdef WIFI_ADAPTER_TEST
1007         case 1: //WIFI
1008             {
1009                 interfaceGetNetworkInfo(CA_WIFI);
1010             }
1011             break;
1012 #endif
1013 #ifdef BT_ADAPTER_TEST
1014         case 2:   //BT
1015             {
1016                 interfaceGetNetworkInfo(CA_EDR);
1017             }
1018             break;
1019 #endif
1020 #ifdef BLE_ADAPTER_TEST
1021         case 3: //BLE
1022             {
1023                 interfaceGetNetworkInfo(CA_LE);
1024             }
1025             break;
1026 #endif
1027         default:
1028             printf("Feature is not enabled or not implemented\n");
1029     }
1030 }
1031
1032 void testStopAdapter()
1033 {
1034     int16_t type = selectConnectivityType();
1035     if (0 >= type || 3 < type)
1036     {
1037         printf("Invalid selection...\n");
1038         return;
1039     }
1040
1041     switch (type)
1042     {
1043 #ifdef WIFI_ADAPTER_TEST
1044         case 1: //WIFI
1045             {
1046                 interfaceStopAdapter(CA_WIFI);
1047             }
1048             break;
1049 #endif
1050 #ifdef BT_ADAPTER_TEST
1051         case 2:   //BT
1052             {
1053                 interfaceStopAdapter(CA_EDR);
1054             }
1055             break;
1056 #endif
1057 #ifdef BLE_ADAPTER_TEST
1058         case 3: //BLE
1059             {
1060                 interfaceStopAdapter(CA_LE);
1061             }
1062             break;
1063 #endif
1064         default:
1065             printf("Feature is not enabled or not implemented\n");
1066     }
1067 }
1068
1069 #ifdef BT_ADAPTER_TEST
1070 void testInitializeBTInterface(void)
1071 {
1072     printf("Initiazing EDR\n");
1073
1074         printf("Initializing BT Adapter threadpool\n");
1075         initializeThreadPool(CA_EDR);
1076
1077     //Start bluetooth communication adapter
1078     CAResult_t err = CAInitializeEDR(interfaceRegisterCallback, networkPacketHandler,
1079                              networkInterfaceCallback, gBTThreadPool);
1080     if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
1081     {
1082         printf("Failed to initialize bluetooth communication adapter!\n");
1083     }
1084 }
1085
1086 void testTerminateBTInterface(void)
1087 {
1088     printf("Terminating EDR\n");
1089
1090     //Terminate the Bluetooth communication adapter
1091     CATerminateEDR();
1092
1093     printf( "Terminating BT Adapter thread pool");
1094     u_thread_pool_free(gBTThreadPool);
1095 }
1096 #endif //BT_ADAPTER_TEST
1097
1098 #ifdef WIFI_ADAPTER_TEST
1099 void testInitializeWIFIInterface(void)
1100 {
1101     printf("testInitializeWIFIInterface IN\n");
1102
1103         printf("Initializing WIFI adapter threadpool\n");
1104         initializeThreadPool(CA_WIFI);
1105
1106     //Start Wifi communication adapter
1107     if (0 != CAInitializeWifi(interfaceRegisterCallback, networkPacketHandler,
1108                               networkInterfaceCallback, gWiFiThreadPool))
1109     {
1110         printf("testInitializeWIFIInterface Failed to initialize bluetooth communication adapter\n");
1111         return;
1112     }
1113
1114     printf("testInitializeWIFIInterface OUT\n");
1115 }
1116
1117 void testTerminateWIFIInterface(void)
1118 {
1119     printf("testTerminateWIFIInterface IN\n");
1120
1121     // Stop if Wifi communication adapter is running
1122     interfaceStopAdapter(CA_WIFI);
1123
1124     // Freeing threadpool for wifi communication adapter
1125     printf( "Terminating WIFI Adapter thread pool");
1126     u_thread_pool_free(gWiFiThreadPool);
1127
1128     //Terminate the Wifi communication adapter
1129     CATerminateWIfI();
1130
1131
1132     printf("testTerminateWIFIInterface OUT\n");
1133 }
1134 #endif //WIFI_ADAPTER_TEST
1135
1136 #ifdef BLE_ADAPTER_TEST
1137 void testInitializeBLEInterface(void)
1138 {
1139     printf("testInitializeBLEInterface IN\n");
1140
1141         printf("Initializing BLE adapter threadpool\n");
1142         initializeThreadPool(CA_LE);
1143
1144     //Start bluetooth communication adapter
1145     if (0 != CAInitializeLE(interfaceRegisterCallback, networkPacketHandler,
1146                             networkInterfaceCallback, gLEThreadPool))
1147     {
1148         printf("testInitializeBLEInterface Failed due to CAInitializeLE\n");
1149         return;
1150     }
1151
1152     printf("testInitializeBLEInterface OUT\n");
1153 }
1154
1155 void testTerminateBLEInterface(void)
1156 {
1157     printf("testTerminateBLEInterface IN\n");
1158
1159     //Terminate the BLE server & Client
1160     CATerminateLE();
1161
1162     printf( "Terminating BLE Adapter thread pool");
1163     u_thread_pool_free(gLEThreadPool);
1164
1165     printf("testTerminateBLEInterface OUT\n");
1166 }
1167 #endif //BLE_ADAPTER_TEST
1168
1169 static void testPrintHelp(void)
1170 {
1171     printf(" =====================================================================\n");
1172     printf("|                 Welcome to Connectivity Abstraction                 |\n");
1173     printf("|                   - CA Unit Test v1.0 -                             |\n");
1174     printf("|---------------------------------------------------------------------|\n");
1175     printf("|                           ** Options **                             |\n");
1176     printf("|  i - Initialize the Interface                                       |\n");
1177     printf("|  d - Terminate the Interface                                        |\n");
1178     printf("|  a - Start adapter                                                  |\n");
1179     printf("|  b - Stop adapter                                                   |\n");
1180     printf("|  sd- Start Discovery Server                                         |\n");
1181     printf("|  sl- Start Listening Server                                         |\n");
1182     printf("|  sn- Start Notification Server                                      |\n");
1183     printf("|  u - Send Unicast Data                                              |\n");
1184     printf("|  m - Send Multicast Data                                            |\n");
1185     printf("|  n - Send Notification Data                                         |\n");
1186     printf("|  g - Get Network Info                                               |\n");
1187     printf("|  r - Read data synchronously                                        |\n");
1188     printf("|  x - quit the test.                                                 |\n");
1189     printf("|  h - Help menu.                                                         |\n");
1190     printf(" =====================================================================\n");
1191 }
1192
1193 static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer data)
1194 {
1195     gchar buf[10] = {'\0'};
1196     gsize read = 0;
1197
1198
1199     if (g_io_channel_read(channel, buf, 10, &read) != G_IO_ERROR_NONE)
1200     {
1201         printf("g_io_channel_read error!!!\n");
1202         return 1;
1203     }
1204     buf[read] = '\0';
1205     g_strstrip(buf);
1206
1207     /*if ((!has_register) && (((buf[0]!= 'i') && (buf[0]!= 'h') && (buf[0]!= 'q')) || (read != 2))) {
1208         testPrintHelp();
1209         printf("***Warning***: You should Register firstly!\n");
1210         return 1;
1211     }*/
1212     switch (buf[0])
1213     {
1214         case 'i':
1215             testInitializeInterface();
1216             break;
1217         case 'x':
1218             testTerminateInterface();
1219             if (g_source_remove(g_test_io_watch_id))
1220             {
1221                 printf("g_source_remove() OK!!!\n");
1222                 g_io_channel_shutdown(channel, TRUE, &g_err_Sample);
1223                 g_io_channel_unref(channel);
1224                 g_main_loop_quit(mainloop);
1225             }
1226             break;
1227         case 'd':
1228             testTerminateInterface();
1229             break;
1230         case 'a':
1231             testStartAdapter();
1232             break;
1233         case 'b':
1234             testStopAdapter();
1235             break;
1236         case 's':
1237             if (read == 3)
1238             {
1239                 if (buf[1] == 'd')
1240                 {
1241                     testStartServer(1);
1242                 }
1243                 if (buf[1] == 'l')
1244                 {
1245                     testStartServer(2);
1246                 }
1247                 if (buf[1] == 'n')
1248                 {
1249                     testStartServer(3);
1250                 }
1251             }
1252             break;
1253         case 'u':
1254             testSendUnicastData();
1255             break;
1256         case 'm':
1257             testSendMulticastData();
1258             break;
1259         case 'n':
1260             testSendNotification();
1261             break;
1262         case 'r':
1263             testReadData();
1264             break;
1265         case 'g':
1266             testGetNetworkInfo();
1267             break;
1268         case 'h':
1269             testPrintHelp();
1270     }
1271     return 1;
1272 }
1273
1274 int main(int argc, char *argv[])
1275 {
1276     printf("Starting sample\n");
1277     mainloop = g_main_loop_new(NULL, FALSE);
1278     channel = g_io_channel_unix_new(0);/* read from stdin */
1279     g_test_io_watch_id = g_io_add_watch(channel,
1280                                         (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), testThread,
1281                                         NULL);
1282     printf("CM Test Thread created...\n");
1283     testPrintHelp();
1284     g_main_loop_run(mainloop);
1285     return 0;
1286 }
1287