1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
31 #include "caadapterinterface.h"
32 #include "uthreadpool.h"
34 #define MOD_NAME "TizenSample"
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
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;
53 static GMainLoop *mainloop;
54 static GIOChannel *channel;
55 static guint g_test_io_watch_id;
56 static GError *g_err_Sample;
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\"}}]}";
63 void testInitializeBTInterface(void);
64 void testTerminateBTInterface(void);
65 void testInitializeWIFIInterface(void);
66 void testTerminateWIFIInterface(void);
67 void testInitializeBLEInterface(void);
68 void testTerminateBLEInterface(void);
70 typedef struct ConnectivityHandlerList
72 CAConnectivityType_t type;
73 CAConnectivityHandler_t handler;
74 struct ConnectivityHandlerList *nextHandler;
75 } ConnectivityHandlerList;
77 static ConnectivityHandlerList *gConnectivityHandlers = NULL;
79 void initializeThreadPool(CAConnectivityType_t type)
81 #ifdef BT_ADAPTER_TEST
82 if (CA_EDR == type && NULL == gBTThreadPool)
84 if (CA_STATUS_OK != u_thread_pool_init(3, &gBTThreadPool))
86 printf("Failed to create thread pool for BT adapter!\n");
91 #ifdef WIFI_ADAPTER_TEST
92 if (CA_WIFI == type && NULL == gWiFiThreadPool)
94 if (CA_STATUS_OK != u_thread_pool_init(3, &gWiFiThreadPool))
96 printf("Failed to create thread pool for BT adapter!\n");
101 #ifdef BLE_ADAPTER_TEST
102 if (CA_LE == type && NULL == gLEThreadPool)
104 if (CA_STATUS_OK != u_thread_pool_init(3, &gLEThreadPool))
106 printf("Failed to create thread pool for BT adapter!\n");
114 void storeInterfaceCallbacks(ConnectivityHandlerList *newHandler)
116 printf("storeInterfaceCallbacks Entry in Sample\n");
117 newHandler->nextHandler = NULL;
118 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
120 if (!tempConnectivityHandlers)
122 gConnectivityHandlers = newHandler;
123 printf("storeInterfaceCallbacks Exit in Sample\n");
127 while (tempConnectivityHandlers->nextHandler)
129 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
132 tempConnectivityHandlers->nextHandler = newHandler;
133 printf("storeInterfaceCallbacks Exit in Sample\n");
136 void interfaceRegisterCallback(CAConnectivityHandler_t handler,
137 CAConnectivityType_t connType)
139 printf("interfaceRegisterCallback Entry in Sample\n");
140 ConnectivityHandlerList *newConnectivityHandler = (ConnectivityHandlerList *) malloc(sizeof(
141 ConnectivityHandlerList));
142 if (NULL == newConnectivityHandler)
144 printf("Memory allocation failed\n");
148 newConnectivityHandler->type = connType;
149 newConnectivityHandler->handler = handler;
150 storeInterfaceCallbacks(newConnectivityHandler);
151 printf("interfaceRegisterCallback Exit in Sample\n");
154 void networkPacketHandler(CARemoteEndpoint_t *object, void *data, uint32_t dataLength)
156 printf("networkPacketHandler Entry in Sample\n");
157 if (object == NULL || data == NULL)
159 printf("NULL Object\n");
163 printf("Data Received from: ");
164 if (CA_EDR == object->connectivityType)
166 printf(object->addressInfo.BT.btMacAddress);
168 else if (CA_LE == object->connectivityType)
170 printf(object->addressInfo.LE.leMacAddress);
172 else if (CA_WIFI == object->connectivityType || CA_ETHERNET == object->connectivityType)
174 printf(object->addressInfo.IP.ipAddress);
177 printf("\nReceived Data [Length: %d]: %s\n",dataLength,(char *)data);
178 printf("networkPacketHandler Exit in Sample\n");
181 void networkInterfaceCallback(CALocalConnectivity_t *localEndPoint,
182 CANetworkStatus_t networkConnectivityState)
184 printf("networkInterfaceCallback Entry in Sample\n");
185 if (localEndPoint == NULL)
187 printf("NULL Object\n");
191 if (networkConnectivityState == CA_INTERFACE_UP)
193 printf("Network Status is UP\n");
197 printf("Network Status is DOWN\n");
201 if (CA_EDR == localEndPoint->type)
203 printf("%s\n", localEndPoint->addressInfo.BT.btMacAddress);
205 else if (CA_LE == localEndPoint->type)
207 printf("%s\n", localEndPoint->addressInfo.LE.leMacAddress);
209 else if (CA_WIFI == localEndPoint->type || CA_ETHERNET == localEndPoint->type)
211 printf("%s\n", localEndPoint->addressInfo.IP.ipAddress);
214 printf("networkInterfaceCallback Exit in Sample\n");
218 void freeData(void *data)
220 printf("freeData Entry in Sample\n");
225 printf("freeData Exit in Sample\n");
228 int16_t selectConnectivityType()
231 printf("*******Select the Connectivity Type*******\n");
232 printf(" [1] WIFI \n");
233 printf(" [2] BT \n");
234 printf(" [3] BLE \n");
238 if (cType < 1 && cType > 3)
240 printf("Invalid Selection!!!!\n");
243 return (int16_t)cType;
246 int16_t interfaceStartAdapter(CAConnectivityType_t connType)
248 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
249 if (NULL == tempConnectivityHandlers)
251 printf("None of the interface is initialized\n");
255 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
257 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
260 if (NULL == tempConnectivityHandlers)
262 printf( "No interface handler for type %d", connType);
266 if (CA_STATUS_OK != tempConnectivityHandlers->handler.startAdapter())
268 printf("Failed to Start adapter\n");
275 int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverType)
277 printf("interfaceMulticastStartServer Starting...\n");
278 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
279 if (NULL == tempConnectivityHandlers)
281 printf("None of the interface is initialized\n");
285 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
287 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
290 if (NULL == tempConnectivityHandlers)
292 printf( "No interface handler for type %d", connType);
296 CAAdapterStartDiscoveryServer startServer = NULL;
299 case 1: //Discovery server
300 startServer = tempConnectivityHandlers->handler.startDiscoverServer;
302 case 2: //Listening server
303 startServer = tempConnectivityHandlers->handler.startListenServer;
305 case 3: //Notification server
306 startServer = tempConnectivityHandlers->handler.startNotifyServer;
312 printf("Invoking start server method\n");
317 int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
319 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
320 if (NULL == tempConnectivityHandlers)
322 printf(" None of the interface is initialized \n");
326 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
328 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
331 if (NULL == tempConnectivityHandlers)
333 printf( "No interface handler for type %d", connType);
337 if (CA_WIFI == connType)
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)
345 printf("Invalid device address\n");
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 */
352 int sdatalen = tempConnectivityHandlers->handler.sendData(&endpoint, coapData,
354 if (sdatalen == strlen(coapData))
356 printf("Send Unicast data success\n");
360 printf("Send Unicast data failed\n");
363 else if (CA_EDR == connType)
366 CARemoteEndpoint_t endpoint;
368 //Get the device address from user
369 char deviceaddress[100] = {0};
370 printf("Enter the device address: \n");
371 scanf("%s", deviceaddress);
373 if (strlen(deviceaddress) == 0)
375 printf("Invlaid device address\n");
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;
384 printf("Sent Unicast data to device: %s\n", endpoint.addressInfo.BT.btMacAddress);
385 tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData)+1);
387 else if (CA_LE == connType)
390 CARemoteEndpoint_t endpoint;
392 //Get the device address from user
393 char deviceaddress[100] = {0};
394 printf("Enter the device address: \n");
395 scanf("%s", deviceaddress);
397 if (strlen(deviceaddress) == 0)
399 printf("Invlaid device address\n");
403 //Get the service uuid from user
404 char uuid[100] = {0};
405 printf("Enter the service uuid: \n");
408 if (strlen(uuid) == 0)
410 printf("Invlaid service uuid\n");
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);
419 tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData));
420 printf("Sent Unicast data \n");
421 free(endpoint.resourceUri);
427 void interfaceSendNotification(CAConnectivityType_t connType)
429 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
430 if (NULL == tempConnectivityHandlers)
432 printf(" None of the interface is initialized \n");
436 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
438 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
441 if (NULL == tempConnectivityHandlers)
443 printf( "No interface handler for type %d\n", connType);
447 if (CA_WIFI == connType)
449 CARemoteEndpoint_t remoteEndpoint;
450 tempConnectivityHandlers->handler.sendNotification(&remoteEndpoint, coapData, strlen(coapData));
452 else if (CA_EDR == connType)
455 CARemoteEndpoint_t endpoint;
457 //Get the device address from user
458 char deviceaddress[100] = {0};
459 printf("Enter the device address: \n");
460 scanf("%s", deviceaddress);
462 if (strlen(deviceaddress) == 0)
464 printf("Invlaid device address\n");
468 //Get the service uuid from user
469 char uuid[100] = {0};
470 printf("Enter the service uuid: \n");
473 if (strlen(uuid) == 0)
475 printf("Invlaid service uuid\n");
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);
484 tempConnectivityHandlers->handler.sendNotification(&endpoint, coapData, strlen(coapData));
485 printf("Sent Unicast data \n");
486 free(endpoint.resourceUri);
488 else if (CA_LE == connType)
491 CARemoteEndpoint_t endpoint;
493 //Get the device address from user
494 char deviceaddress[100] = {0};
495 printf("Enter the device address: \n");
496 scanf("%s", deviceaddress);
498 if (strlen(deviceaddress) == 0)
500 printf("Invlaid device address\n");
504 //Get the service uuid from user
505 char uuid[100] = {0};
506 printf("Enter the service uuid: \n");
509 if (strlen(uuid) == 0)
511 printf("Invlaid service uuid\n");
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);
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");
527 int16_t interfaceSendMulticastData(CAConnectivityType_t connType)
529 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
530 if (NULL == tempConnectivityHandlers)
532 printf("None of the interface is initialized\n");
536 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
538 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
541 if (NULL == tempConnectivityHandlers)
543 printf( "No interface handler for type %d", connType);
547 if (connType == CA_WIFI)
549 tempConnectivityHandlers->handler.sendDataToAll(coapData,
550 strlen(coapData) + 1);
552 else if (connType == CA_EDR || connType == CA_LE)
554 tempConnectivityHandlers->handler.sendDataToAll(coapData, strlen(coapData));
558 void interfaceReadData(CAConnectivityType_t connType)
560 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
561 if (NULL == tempConnectivityHandlers)
563 printf("None of the interface is initialized\n");
567 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
569 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
572 if (NULL == tempConnectivityHandlers)
574 printf( "No interface handler for type %d", connType);
578 if (CA_STATUS_OK != tempConnectivityHandlers->handler.readData())
580 printf("Failed to Read Data\n");
584 printf("Read Data is successfull\n");
588 void interfaceGetNetworkInfo(CAConnectivityType_t connType)
592 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
593 if (NULL == tempConnectivityHandlers)
595 printf("None of the interface is initialized\n");
599 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
601 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
604 if (NULL == tempConnectivityHandlers)
606 printf( "No interface handler for type %d", connType);
610 //Get the network interface info
611 CALocalConnectivity_t *info = NULL;
613 if (CA_STATUS_OK != tempConnectivityHandlers->handler.GetnetInfo(&info, &size))
615 printf("Failed to get network info\n");
619 if (0 >= size || info == NULL)
621 printf("No network found !!!\n");
625 printf("Network Information: \n");
626 for (; i < size; i++)
628 if (connType == CA_WIFI)
630 printf("Type : %s\n", (connType == CA_ETHERNET) ? "CA_ETHERNET" : "CA_WIFI");
631 printf("Address : %s\n", info[i].addressInfo.IP.ipAddress);
635 printf("Type : %s\n", (connType == CA_EDR) ? "CA_EDR" : "CA_LE");
636 printf("Address : %s\n\n", info[i].addressInfo.BT.btMacAddress);
641 int16_t interfaceStopAdapter(CAConnectivityType_t connType)
643 ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
644 if (NULL == tempConnectivityHandlers)
646 printf("None of the interface is initialized\n");
650 while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
652 tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
655 if (NULL == tempConnectivityHandlers)
657 printf( "No interface handler for type %d", connType);
661 if (CA_STATUS_OK != tempConnectivityHandlers->handler.stopAdapter())
663 printf("Failed to Stop adapter\n");
667 printf("Stopped the adapter\n");
671 void testInitializeInterface()
673 printf("testInitializeInterface Entry\n");
675 int16_t type = selectConnectivityType();
676 if (0 >= type || 3 < type)
678 printf("Invalid selection...\n");
684 #ifdef WIFI_ADAPTER_TEST
687 testInitializeWIFIInterface();
691 #ifdef BT_ADAPTER_TEST
694 testInitializeBTInterface();
698 #ifdef BLE_ADAPTER_TEST
701 testInitializeBLEInterface();
706 printf("Feature is not enabled or not implemented\n");
710 void testTerminateInterface()
712 int16_t type = selectConnectivityType();
713 if (0 >= type || 3 < type)
715 printf("Invalid selection...\n");
719 #ifdef WIFI_ADAPTER_TEST
720 if (1 == type) /* WIFI */
722 testTerminateWIFIInterface();
725 #ifdef BT_ADAPTER_TEST
726 if (2 == type) /*BT*/
728 testTerminateBTInterface();
731 #ifdef BLE_ADAPTER_TEST
732 if (3 == type) /*BLE*/
734 testTerminateBLEInterface();
738 ConnectivityHandlerList *currentConnectivityHandler = gConnectivityHandlers;
739 ConnectivityHandlerList *prevConnectivityHandler = NULL;
741 printf("Linked list delete start\n");
742 while (currentConnectivityHandler != NULL)
744 printf("Iterating through the list to find the matching interface\n");
745 if (currentConnectivityHandler->type == type)
747 printf("Matching interface found\n");
748 if (prevConnectivityHandler == NULL)
750 currentConnectivityHandler = currentConnectivityHandler->nextHandler;
751 freeData(gConnectivityHandlers);
752 gConnectivityHandlers = NULL;
753 printf( "Node deleted with interface type %d", type);
757 prevConnectivityHandler->nextHandler = currentConnectivityHandler->nextHandler;
758 freeData(currentConnectivityHandler);
759 currentConnectivityHandler = prevConnectivityHandler->nextHandler;
760 printf( "Node deleted with interface type %d from linked list", type);
765 prevConnectivityHandler = currentConnectivityHandler;
766 currentConnectivityHandler = currentConnectivityHandler->nextHandler;
769 gConnectivityHandlers = NULL;
773 void testStartAdapter()
775 int type = selectConnectivityType();
776 if (0 >= type || 3 < type)
778 printf("Invalid selection...\n");
784 #ifdef WIFI_ADAPTER_TEST
787 interfaceStartAdapter(CA_WIFI);
791 #ifdef BT_ADAPTER_TEST
794 interfaceStartAdapter(CA_EDR);
798 #ifdef BLE_ADAPTER_TEST
801 interfaceStartAdapter(CA_LE);
806 printf("Feature is not enabled or not implemented\n");
810 void testStartServer(int serverType)
812 int type = selectConnectivityType();
813 if (0 >= type || 3 < type)
815 printf("Invalid selection...\n");
821 #ifdef WIFI_ADAPTER_TEST
824 interfaceMulticastStartServer(CA_WIFI, serverType);
828 #ifdef BT_ADAPTER_TEST
831 interfaceMulticastStartServer(CA_EDR, serverType);
835 #ifdef BLE_ADAPTER_TEST
838 interfaceMulticastStartServer(CA_LE, serverType);
843 printf("Feature is not enabled or not implemented\n");
847 void testSendUnicastData()
849 int16_t type = selectConnectivityType();
850 if (0 >= type || 3 < type)
852 printf("Invalid selection...\n");
858 #ifdef WIFI_ADAPTER_TEST
861 interfaceSendUnicastData(CA_WIFI);
865 #ifdef BT_ADAPTER_TEST
868 interfaceSendUnicastData(CA_EDR);
872 #ifdef BLE_ADAPTER_TEST
875 interfaceSendUnicastData(CA_LE);
880 printf("Feature is not enabled or not implemented\n");
884 void testSendNotification()
886 int16_t type = selectConnectivityType();
887 if (0 >= type || 3 < type)
889 printf("Invalid selection...\n");
895 #ifdef WIFI_ADAPTER_TEST
898 interfaceSendNotification(CA_WIFI);
902 #ifdef BT_ADAPTER_TEST
905 interfaceSendNotification(CA_EDR);
909 #ifdef BLE_ADAPTER_TEST
912 interfaceSendNotification(CA_LE);
917 printf("Feature is not enabled or not implemented\n");
921 void testSendMulticastData()
923 int16_t type = selectConnectivityType();
924 if (0 >= type || 3 < type)
926 printf("Invalid selection...\n");
932 #ifdef WIFI_ADAPTER_TEST
935 interfaceSendMulticastData(CA_WIFI);
939 #ifdef BT_ADAPTER_TEST
942 interfaceSendMulticastData(CA_EDR);
946 #ifdef BLE_ADAPTER_TEST
949 interfaceSendMulticastData(CA_LE);
954 printf("Feature is not enabled or not implemented\n");
958 void testReadData(void)
960 int16_t type = selectConnectivityType();
961 if (0 >= type || 3 < type)
963 printf("Invalid selection...\n");
969 #ifdef WIFI_ADAPTER_TEST
972 interfaceReadData(CA_WIFI);
976 #ifdef BT_ADAPTER_TEST
979 interfaceReadData(CA_EDR);
983 #ifdef BLE_ADAPTER_TEST
986 interfaceReadData(CA_LE);
991 printf("Feature is not enabled or not implemented\n");
995 void testGetNetworkInfo(void)
997 int16_t type = selectConnectivityType();
998 if (0 >= type || 3 < type)
1000 printf("Invalid selection...\n");
1006 #ifdef WIFI_ADAPTER_TEST
1009 interfaceGetNetworkInfo(CA_WIFI);
1013 #ifdef BT_ADAPTER_TEST
1016 interfaceGetNetworkInfo(CA_EDR);
1020 #ifdef BLE_ADAPTER_TEST
1023 interfaceGetNetworkInfo(CA_LE);
1028 printf("Feature is not enabled or not implemented\n");
1032 void testStopAdapter()
1034 int16_t type = selectConnectivityType();
1035 if (0 >= type || 3 < type)
1037 printf("Invalid selection...\n");
1043 #ifdef WIFI_ADAPTER_TEST
1046 interfaceStopAdapter(CA_WIFI);
1050 #ifdef BT_ADAPTER_TEST
1053 interfaceStopAdapter(CA_EDR);
1057 #ifdef BLE_ADAPTER_TEST
1060 interfaceStopAdapter(CA_LE);
1065 printf("Feature is not enabled or not implemented\n");
1069 #ifdef BT_ADAPTER_TEST
1070 void testInitializeBTInterface(void)
1072 printf("Initiazing EDR\n");
1074 printf("Initializing BT Adapter threadpool\n");
1075 initializeThreadPool(CA_EDR);
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)
1082 printf("Failed to initialize bluetooth communication adapter!\n");
1086 void testTerminateBTInterface(void)
1088 printf("Terminating EDR\n");
1090 //Terminate the Bluetooth communication adapter
1093 printf( "Terminating BT Adapter thread pool");
1094 u_thread_pool_free(gBTThreadPool);
1096 #endif //BT_ADAPTER_TEST
1098 #ifdef WIFI_ADAPTER_TEST
1099 void testInitializeWIFIInterface(void)
1101 printf("testInitializeWIFIInterface IN\n");
1103 printf("Initializing WIFI adapter threadpool\n");
1104 initializeThreadPool(CA_WIFI);
1106 //Start Wifi communication adapter
1107 if (0 != CAInitializeWifi(interfaceRegisterCallback, networkPacketHandler,
1108 networkInterfaceCallback, gWiFiThreadPool))
1110 printf("testInitializeWIFIInterface Failed to initialize bluetooth communication adapter\n");
1114 printf("testInitializeWIFIInterface OUT\n");
1117 void testTerminateWIFIInterface(void)
1119 printf("testTerminateWIFIInterface IN\n");
1121 // Stop if Wifi communication adapter is running
1122 interfaceStopAdapter(CA_WIFI);
1124 // Freeing threadpool for wifi communication adapter
1125 printf( "Terminating WIFI Adapter thread pool");
1126 u_thread_pool_free(gWiFiThreadPool);
1128 //Terminate the Wifi communication adapter
1132 printf("testTerminateWIFIInterface OUT\n");
1134 #endif //WIFI_ADAPTER_TEST
1136 #ifdef BLE_ADAPTER_TEST
1137 void testInitializeBLEInterface(void)
1139 printf("testInitializeBLEInterface IN\n");
1141 printf("Initializing BLE adapter threadpool\n");
1142 initializeThreadPool(CA_LE);
1144 //Start bluetooth communication adapter
1145 if (0 != CAInitializeLE(interfaceRegisterCallback, networkPacketHandler,
1146 networkInterfaceCallback, gLEThreadPool))
1148 printf("testInitializeBLEInterface Failed due to CAInitializeLE\n");
1152 printf("testInitializeBLEInterface OUT\n");
1155 void testTerminateBLEInterface(void)
1157 printf("testTerminateBLEInterface IN\n");
1159 //Terminate the BLE server & Client
1162 printf( "Terminating BLE Adapter thread pool");
1163 u_thread_pool_free(gLEThreadPool);
1165 printf("testTerminateBLEInterface OUT\n");
1167 #endif //BLE_ADAPTER_TEST
1169 static void testPrintHelp(void)
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");
1193 static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer data)
1195 gchar buf[10] = {'\0'};
1199 if (g_io_channel_read(channel, buf, 10, &read) != G_IO_ERROR_NONE)
1201 printf("g_io_channel_read error!!!\n");
1207 /*if ((!has_register) && (((buf[0]!= 'i') && (buf[0]!= 'h') && (buf[0]!= 'q')) || (read != 2))) {
1209 printf("***Warning***: You should Register firstly!\n");
1215 testInitializeInterface();
1218 testTerminateInterface();
1219 if (g_source_remove(g_test_io_watch_id))
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);
1228 testTerminateInterface();
1254 testSendUnicastData();
1257 testSendMulticastData();
1260 testSendNotification();
1266 testGetNetworkInfo();
1274 int main(int argc, char *argv[])
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,
1282 printf("CM Test Thread created...\n");
1284 g_main_loop_run(mainloop);