RDK Documentation (Open Sourced RDK Components)
btrCore_test.c
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
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 /*BT.c file*/
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h> //for malloc
24 #include <unistd.h> //for close?
25 #include <errno.h> //for errno handling
26 #include <poll.h>
27 
28 #include <sys/stat.h> //for mkfifo
29 #include <fcntl.h> //for open
30 
31 /* Ext lib Headers */
32 #include <glib.h>
33 
34 /* Interface lib Headers */
35 #include "btrCore.h" //basic RDK BT functions
36 #include "btrCore_service.h" //service UUIDs, use for service discovery
37 
38 
39 //for BT audio input testing
40 static GThread* fileWriteThread = NULL;
41 static int writeSBC = 0;
42 unsigned int BT_loop = 0;
43 
44 
45 typedef struct appDataStruct{
46  tBTRCoreHandle hBTRCore;
47  stBTRCoreDevMediaInfo stBtrCoreDevMediaInfo;
48  int iDataPath;
49  int iDataReadMTU;
50  int iDataWriteMTU;
51  unsigned int ui32DevDelay;
53 
54 
55 #define NO_ADAPTER 1234
56 
57 //for connection callback testing
58 static int acceptConnection = 0;
59 static int connectedDeviceIndex=0;
60 
61 void *
62 DoSBCwrite (
63  void* ptr
64 ) {
65  char *message = "SBC Write Thread Started";
66  char * myfifo = "/tmp/myfifo";
67  int fd;
68  appDataStruct* pstAppData = (appDataStruct*)ptr;
69 
70  /* create the FIFO (named pipe) */
71  mkfifo(myfifo, 0666);
72  fprintf(stderr, "%d\t: %s - Thread starting: %s \n", __LINE__, __FUNCTION__, message);
73  fd = open(myfifo, O_WRONLY );//not sure if I need nonblock or not the first time I tried it it failed. then I tried nonblock, no audio
74  fprintf(stderr, "%d\t: %s - BT data flowing...\n", __LINE__, __FUNCTION__);
75 
76  do {
77  sleep (1);
78  } while (writeSBC == 0);
79 
80  {
81  int tmp;
82  unsigned char sigByte1 = 0;
83  unsigned char bitpool = 0;
84  int blocks;
85  int computed_len;
86  int frame_length = 0;
87  //unsigned char channel_mode;
88  //unsigned char alloc_method;
89  int sub_bands;
90 
91  unsigned char bit_blocks;
92  unsigned char bit_channel_mode;
93  //unsigned char bit_alloc_method;
94  unsigned char bit_sub_bands;
95  int k;
96  int z;
97  int y;//for first time processing
98  unsigned char x;
99  int first_time_thru;
100  int sbcFrameState = 0;
101  int bytes2write=0;
102  unsigned char temp;
103  int read_return;
104  //FILE *w_ptr;
105  //w_ptr=fopen("/tmp/myfifo", "wb");
106  char buffy[1024];//hope MTU is not bigger than this, or we could get hurt...
107  k=0;
108  first_time_thru = 1;
109 
110  while (k < BT_loop) {
111  //I guess the in is the out, so try 5 as our reading fd
112  read_return = read(pstAppData->iDataPath, buffy, pstAppData->iDataReadMTU);
113  /////////insert FIRST TIME CODE
114  y=0;
115 
116  while (first_time_thru) {
117  x = buffy[y];
118  y++;
119 
120  if (x == 0x9C) {
121  fprintf(stderr, "%d\t: %s - first sbc frame detected\n", __LINE__, __FUNCTION__);
122  //get another byte
123  x = buffy[y];
124  y++;
125  sigByte1 = x;//we will use this later for parsing
126  bit_blocks = (x & 0x30) >> 4;
127  bit_channel_mode = (x & 0x0C) >> 2;
128  // bit_alloc_method = (x & 0x02) >> 1;
129  bit_sub_bands = (x & 0x01);
130  sub_bands = (bit_sub_bands + 1) * 4;
131  blocks = (bit_blocks + 1) * 4;
132  //get another byte
133  x = buffy[y];
134  y++;
135  bitpool = x;
136  //get the length
137  fprintf(stderr, "%d\t: %s - channel mode 0x%x\n", __LINE__, __FUNCTION__, bit_channel_mode);
138  fprintf(stderr, "%d\t: %s - nrof_subbands 0x%x\n", __LINE__, __FUNCTION__, sub_bands);
139  fprintf(stderr, "%d\t: %s - nrof_blocks 0x%x\n", __LINE__, __FUNCTION__, blocks);
140  fprintf(stderr, "%d\t: %s - bitpool 0x%x\n", __LINE__, __FUNCTION__, bitpool );
141 
142  switch (bit_channel_mode) {
143  case 0x00:
144  bit_blocks /= 2;
145  tmp = blocks * bitpool;
146  break;
147  case 0x01:
148  tmp = blocks * bitpool * 2;
149  break;
150  case 0x02:
151  tmp = blocks * bitpool;
152  break;
153  case 0x03:
154  tmp = blocks * bitpool + sub_bands;
155  break;
156  default:
157  return 0;
158  }
159 
160  computed_len = sub_bands + ((tmp + 7) / 8);
161  frame_length = computed_len + 4;
162  //fprintf(stderr, "%d\t: %s - tmp = %d\n", __LINE__, __FUNCTION__tmp);
163  //fprintf(stderr, "%d\t: %s - computed length is %d\n", __LINE__, __FUNCTION__computed_len);
164  fprintf(stderr, "%d\t: %s - frame length is %d\n", __LINE__, __FUNCTION__, frame_length);
165  first_time_thru = 0;
166  //we now know how long the frames are. and what the two signature bytes after the sync are
167  //lets not bother with this frame, instead start searching now, based on what we know.
168  }
169  }
170 
171  //////END FIRST TIME CODE now that we know the details, lets parse
172  if (read_return > 0) {
173  if ( (k % 1000) == 0) {
174  fprintf(stderr, "%d\t: %s - reading %d - %d - %d\n", __LINE__, __FUNCTION__, read_return,k,BT_loop);
175  }
176 
177  k++;
178  //fwrite(buffy, 1 , read_return, fp);
179  //remove the sbc data from rtp
180 
181  for(z=0;z<read_return;z++) {
182  x=buffy[z];
183  if ((sbcFrameState == 0) && (x == 0x9C)) {
184  sbcFrameState = 1;
185  }
186 
187  if ((sbcFrameState == 1) && (x == sigByte1)) {
188  sbcFrameState = 2;
189  }
190 
191  if ((sbcFrameState == 2) && (x == bitpool)) {
192  sbcFrameState = 3;
193 
194  sbcFrameState = 4;
195  bytes2write=frame_length-2;///113 + 2 sig bytes
196  //write 0x9C plus header info
197  temp = 0x9C;
198  // fwrite(&temp,1,1,w_ptr);
199  if (write(fd,&temp,1)) {
200  }
201  temp = sigByte1;
202  // fwrite(&temp,1,1,w_ptr);
203  if (write(fd,&temp,1)) {
204  }
205  }
206 
207  if (bytes2write) {
208  bytes2write--;
209  //write x
210  //fwrite(&x,1,1,w_ptr);
211  if (write(fd,&x,1)) {
212  }
213 
214  if (bytes2write == 0)
215  sbcFrameState = 0;//reset the state machine
216  }
217  }//end for
218  }
219 
220  usleep(50);//chandresh did this, so lets try?
221  }
222 
223  // fclose(w_ptr);
224  close(fd);
225  unlink(myfifo);
226  }
227 
228  return NULL;
229 }
230 
231 
232 
233 static void
234 GetTransport (
235  appDataStruct* pstAppData
236 ) {
237 
238  if (!pstAppData)
239  return;
240 
241  BTRCore_GetDeviceMediaInfo ( pstAppData->hBTRCore, connectedDeviceIndex, enBTRCoreMobileAudioIn, &pstAppData->stBtrCoreDevMediaInfo);
242 
243  pstAppData->iDataPath = 0;
244  pstAppData->iDataReadMTU = 0;
245  pstAppData->iDataWriteMTU = 0;
246  pstAppData->ui32DevDelay = 0;
247 
248  BTRCore_AcquireDeviceDataPath ( pstAppData->hBTRCore,
249  connectedDeviceIndex,
250  enBTRCoreMobileAudioIn,
251  &pstAppData->iDataPath,
252  &pstAppData->iDataReadMTU,
253  &pstAppData->iDataWriteMTU,
254  &pstAppData->ui32DevDelay);
255 
256  fprintf(stderr, "%d\t: %s - Device Data Path = %d \n", __LINE__, __FUNCTION__, pstAppData->iDataPath);
257  fprintf(stderr, "%d\t: %s - Device Data Read MTU = %d \n", __LINE__, __FUNCTION__, pstAppData->iDataReadMTU);
258  fprintf(stderr, "%d\t: %s - Device Data Write MTU= %d \n", __LINE__, __FUNCTION__, pstAppData->iDataWriteMTU);
259  fprintf(stderr, "%d\t: %s - Device Delay = %d \n", __LINE__, __FUNCTION__, pstAppData->ui32DevDelay);
260 
261  if (pstAppData->stBtrCoreDevMediaInfo.eBtrCoreDevMType == eBTRCoreDevMediaTypeSBC) {
262  if (pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo) {
263  fprintf(stderr, "%d\t: %s - Device Media Info SFreq = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui32DevMSFreq);
264  fprintf(stderr, "%d\t: %s - Device Media Info AChan = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->eDevMAChan);
265  fprintf(stderr, "%d\t: %s - Device Media Info SbcAllocMethod= %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcAllocMethod);
266  fprintf(stderr, "%d\t: %s - Device Media Info SbcSubbands = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcSubbands);
267  fprintf(stderr, "%d\t: %s - Device Media Info SbcBlockLength= %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcBlockLength);
268  fprintf(stderr, "%d\t: %s - Device Media Info SbcMinBitpool = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcMinBitpool);
269  fprintf(stderr, "%d\t: %s - Device Media Info SbcMaxBitpool = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcMaxBitpool);
270  fprintf(stderr, "%d\t: %s - Device Media Info SbcFrameLen = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui16DevMSbcFrameLen);
271  fprintf(stderr, "%d\t: %s - Device Media Info SbcBitrate = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(pstAppData->stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui16DevMSbcBitrate);
272  }
273  }
274 }
275 
276 
277 static int
278 getChoice (
279  void
280 ) {
281  int mychoice;
282  fprintf(stderr, "\nEnter a choice...\n");
283  if (scanf("%d", &mychoice)) {
284  }
285  getchar();//suck up a newline?
286  return mychoice;
287 }
288 
289 static char*
290 getEncodedSBCFile (
291  void
292 ) {
293  char sbcEncodedFile[1024];
294  fprintf(stderr, "%d\t: %s - Enter SBC File location...\n", __LINE__, __FUNCTION__);
295  if (scanf("%s", sbcEncodedFile)) {
296  }
297  getchar();//suck up a newline?
298  return strdup(sbcEncodedFile);
299 }
300 
301 static char*
302 getLeUuidString (
303  void
304 ) {
305  char leUuidString[64];
306  fprintf(stderr, "%d\t: %s - Enter the UUID for Le device...\n", __LINE__, __FUNCTION__);
307  if (scanf("%s",leUuidString )) {
308  }
309  getchar();//suck up a newline?
310  return strdup(leUuidString);
311 }
312 
313 static int
314 getBitsToString (
315  unsigned short flagBits
316 ) {
317  unsigned char i = 0;
318  for (; i<16; i++) {
319  fprintf(stderr, "%d", (flagBits >> i) & 1);
320  }
321 
322  return 0;
323 }
324 
325 static void
326 sendSBCFileOverBT (
327  char* fileLocation,
328  int fd,
329  int mtuSize
330 ) {
331  FILE* sbcFilePtr = fopen(fileLocation, "rb");
332  int bytesLeft = 0;
333  void *encoded_buf = NULL;
334  int bytesToSend = mtuSize;
335  struct pollfd pollout = { fd, POLLOUT, 0 };
336  int timeout;
337 
338 
339  if (!sbcFilePtr)
340  return;
341 
342  fprintf(stderr, "%d\t: %s - fileLocation %s", __LINE__, __FUNCTION__, fileLocation);
343 
344  fseek(sbcFilePtr, 0, SEEK_END);
345  bytesLeft = ftell(sbcFilePtr);
346  fseek(sbcFilePtr, 0, SEEK_SET);
347 
348  fprintf(stderr, "%d\t: %s - File size: %d bytes\n", __LINE__, __FUNCTION__, (int)bytesLeft);
349 
350  encoded_buf = malloc (mtuSize);
351 
352  while (bytesLeft) {
353 
354  if (bytesLeft < mtuSize)
355  bytesToSend = bytesLeft;
356 
357  timeout = poll (&pollout, 1, 1000); //delay 1s to allow others to update our state
358 
359  if (timeout == 0)
360  continue;
361  if (timeout < 0)
362  fprintf (stderr, "%d\t: %s - Bluetooth Write Error : %d\n", __LINE__, __FUNCTION__, errno);
363 
364  // write bluetooth
365  if (timeout > 0) {
366  if (fread (encoded_buf, 1, bytesToSend, sbcFilePtr)) {
367  }
368  if (write(fd, encoded_buf, bytesToSend)) {
369  }
370  bytesLeft -= bytesToSend;
371  }
372 
373  usleep(26000); //1ms delay //12.5 ms can hear words
374  }
375 
376  free(encoded_buf);
377  fclose(sbcFilePtr);
378 }
379 
380 
381 enBTRCoreRet
382 cb_connection_intimation (
383  stBTRCoreConnCBInfo* apstConnCbInfo,
384  int* api32ConnInIntimResp,
385  void* apvUserData
386 ) {
387  fprintf(stderr, "%d\t: %s - Choose 35 to verify pin-passcode or 36 to discard pin-passcode\n\n", __LINE__, __FUNCTION__);
388 
389  if (apstConnCbInfo->ui32devPassKey) {
390  fprintf(stderr, "%d\t: %s - Incoming Connection passkey = %6d\n", __LINE__, __FUNCTION__, apstConnCbInfo->ui32devPassKey);
391  }
392 
393  do {
394  usleep(20000);
395  } while (acceptConnection == 0);
396 
397  fprintf(stderr, "%d\t: %s - you picked %d\n", __LINE__, __FUNCTION__, acceptConnection);
398  if (acceptConnection == 1) {
399  fprintf(stderr, "%d\t: %s - Pin-Passcode accepted\n", __LINE__, __FUNCTION__);
400  acceptConnection = 0;//reset variabhle for the next connection
401  *api32ConnInIntimResp = 1;
402  }
403  else {
404  fprintf(stderr, "%d\t: %s - Pin-Passcode denied\n", __LINE__, __FUNCTION__);
405  acceptConnection = 0;//reset variabhle for the next connection
406  *api32ConnInIntimResp = 0;
407  }
408 
409  return enBTRCoreSuccess;
410 }
411 
412 
413 enBTRCoreRet
414 cb_connection_authentication (
415  stBTRCoreConnCBInfo* apstConnCbInfo,
416  int* api32ConnInAuthResp,
417  void* apvUserData
418 ) {
419  fprintf(stderr, "%d\t: %s - Choose 35 to accept the connection or 36 to deny the connection\n\n", __LINE__, __FUNCTION__);
420 
421  do {
422  usleep(20000);
423  } while (acceptConnection == 0);
424 
425  fprintf(stderr, "%d\t: %s - you picked %d\n", __LINE__, __FUNCTION__, acceptConnection);
426  if (acceptConnection == 1) {
427  fprintf(stderr, "%d\t: %s - connection accepted\n", __LINE__, __FUNCTION__);
428  acceptConnection = 0;//reset variabhle for the next connection
429  *api32ConnInAuthResp = 1;
430  }
431  else {
432  fprintf(stderr, "%d\t: %s - connection denied\n", __LINE__, __FUNCTION__);
433  acceptConnection = 0;//reset variabhle for the next connection
434  *api32ConnInAuthResp = 0;
435  }
436 
437  return enBTRCoreSuccess;
438 }
439 
440 
441 enBTRCoreRet
442 cb_unsolicited_bluetooth_status (
443  stBTRCoreDevStatusCBInfo* p_StatusCB,
444  void* apvUserData
445 ) {
446  //fprintf(stderr, "%d\t: %s - device status change: %d\n", __LINE__, __FUNCTION__p_StatusCB->eDeviceType);
447  fprintf(stderr, "%d\t: %s - app level cb device status change: new state is %d\n", __LINE__, __FUNCTION__, p_StatusCB->eDeviceCurrState);
448  if ((p_StatusCB->eDevicePrevState == enBTRCoreDevStConnected) && (p_StatusCB->eDeviceCurrState == enBTRCoreDevStPlaying)) {
449  if (p_StatusCB->eDeviceType == enBTRCoreMobileAudioIn) {
450  fprintf(stderr, "%d\t: %s - transition to playing, get the transport info...\n", __LINE__, __FUNCTION__);
451  GetTransport((appDataStruct*)apvUserData);
452  }
453  }
454 
455  return enBTRCoreSuccess;
456 }
457 
458 static void
459 printMenu (
460  void
461 ) {
462  fprintf( stderr, "Bluetooth Test Menu\n\n");
463  fprintf( stderr, "1. Get Current Adapter\n");
464  fprintf( stderr, "2. Scan\n");
465  fprintf( stderr, "3. Show found devices\n");
466  fprintf( stderr, "4. Pair\n");
467  fprintf( stderr, "5. UnPair/Forget a device\n");
468  fprintf( stderr, "6. Show known devices\n");
469  fprintf( stderr, "7. Connect to Headset/Speakers\n");
470  fprintf( stderr, "8. Disconnect to Headset/Speakers\n");
471  fprintf( stderr, "9. Connect as Headset/Speakerst\n");
472  fprintf( stderr, "10. Disconnect as Headset/Speakerst\n");
473  fprintf( stderr, "11. Show all Bluetooth Adapters\n");
474  fprintf( stderr, "12. Enable Bluetooth Adapter\n");
475  fprintf( stderr, "13. Disable Bluetooth Adapter\n");
476  fprintf( stderr, "14. Set Discoverable Timeout\n");
477  fprintf( stderr, "15. Set Discoverable \n");
478  fprintf( stderr, "16. Set friendly name \n");
479  fprintf( stderr, "17. Check for audio sink capability\n");
480  fprintf( stderr, "18. Check for existance of a service\n");
481  fprintf( stderr, "19. Find service details\n");
482  fprintf( stderr, "20. Check if Device Paired\n");
483  fprintf( stderr, "21. Get Connected Dev Data path\n");
484  fprintf( stderr, "22. Release Connected Dev Data path\n");
485  fprintf( stderr, "23. Send SBC data to BT Headset/Speakers\n");
486  fprintf( stderr, "29. BT audio input test\n");
487  fprintf( stderr, "30. install agent for accepting connections NoInputNoOutput\n");
488  fprintf( stderr, "31. install agent for accepting connections DisplayYesNo\n");
489  fprintf( stderr, "32. Uninstall agent - allows device-initiated pairing\n");
490  fprintf( stderr, "33. Register connection-in intimation callback.\n");
491  fprintf( stderr, "34. Register connection authentication callback to allow accepting or rejection of connections.\n");
492  fprintf( stderr, "35. Accept a connection request\n");
493  fprintf( stderr, "36. Deny a connection request\n");
494  fprintf( stderr, "37. Check if Device is Connectable\n");
495  fprintf( stderr, "38. Scan for LE Devices\n");
496  fprintf( stderr, "39. Connect to LE Device\n");
497  fprintf( stderr, "40. Disconnect to LE Device\n");
498  fprintf( stderr, "41. Get Gatt Properties of connected LE device.\n");
499  fprintf( stderr, "42. Perform Operation on connected LE device.\n");
500  fprintf( stderr, "43. Connect to HID/Unknown\n");
501  fprintf( stderr, "44. Disconnect to HID/Unknown\n");
502 
503  fprintf( stderr, "88. debug test\n");
504  fprintf( stderr, "99. Exit\n");
505 }
506 
507 
508 int
509 main (
510  void
511 ) {
512  tBTRCoreHandle lhBTRCore = NULL;
513 
514  int choice;
515  int devnum;
516  int default_adapter = NO_ADAPTER;
517  stBTRCoreGetAdapters GetAdapters;
518  stBTRCoreAdapter lstBTRCoreAdapter;
519 
520  char default_path[128];
521  char* agent_path = NULL;
522  char myData[2048];
523  int myadapter = 0;
524  int bfound;
525  int i;
526 
527  char *sbcEncodedFileName = NULL;
528 
529  char myService[16];//for testing findService API
530 
531  appDataStruct stAppData;
532 
533  memset(&stAppData, 0, sizeof(stAppData));
534 
535  snprintf(default_path, sizeof(default_path), "/org/bluez/agent_%d", getpid());
536 
537  if (!agent_path)
538  agent_path = strdup(default_path);
539 
540  //call the BTRCore_init...eventually everything goes after this...
541  BTRCore_Init(&lhBTRCore);
542 
543  //Init the adapter
544  lstBTRCoreAdapter.bFirstAvailable = TRUE;
545  if (enBTRCoreSuccess == BTRCore_GetAdapter(lhBTRCore, &lstBTRCoreAdapter)) {
546  default_adapter = lstBTRCoreAdapter.adapter_number;
547  fprintf(stderr, "%d\t: %s - GetAdapter Returns Adapter number %d\n", __LINE__, __FUNCTION__, default_adapter);
548  }
549  else {
550  fprintf(stderr, "%d\t: %s - No bluetooth adapter found!\n", __LINE__, __FUNCTION__);
551  return -1;
552  }
553 
554  stAppData.hBTRCore = lhBTRCore;
555  //register callback for unsolicted events, such as powering off a bluetooth device
556  BTRCore_RegisterStatusCb(lhBTRCore, cb_unsolicited_bluetooth_status, &stAppData);
557 
558  stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo = (void*)malloc((sizeof(stBTRCoreDevMediaSbcInfo) > sizeof(stBTRCoreDevMediaMpegInfo)) ? sizeof(stBTRCoreDevMediaSbcInfo) : sizeof(stBTRCoreDevMediaMpegInfo));
559 
560  //display a menu of choices
561  printMenu();
562  //start Bluetooth input data writing thread - supports BT in audio test
563  fileWriteThread = g_thread_new("DoSBCwrite", DoSBCwrite, (gpointer)&stAppData);
564  do {
565  fprintf(stderr, "Enter a choice...\n");
566  if(scanf("%d", &choice)) {
567  }
568  getchar();//suck up a newline?
569  switch (choice) {
570  case 1:
571  fprintf(stderr, "%d\t: %s - Adapter is %s\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.pcAdapterPath);
572  break;
573  case 2:
574  if (default_adapter != NO_ADAPTER) {
575  fprintf(stderr, "%d\t: %s - Looking for devices on BT adapter %s\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.pcAdapterPath);
576  fprintf(stderr, "%d\t: %s - Performing device scan for 15 seconds . Please wait...\n", __LINE__, __FUNCTION__);
577  BTRCore_StartDiscovery(lhBTRCore, lstBTRCoreAdapter.pcAdapterPath, enBTRCoreUnknown, 15);
578  fprintf(stderr, "%d\t: %s - scan complete\n", __LINE__, __FUNCTION__);
579  }
580  else {
581  fprintf(stderr, "%d\t: %s - Error, no default_adapter set\n", __LINE__, __FUNCTION__);
582  }
583  break;
584  case 3:
585  {
586  stBTRCoreScannedDevicesCount lstBTRCoreScannedDevList;
587  fprintf(stderr, "%d\t: %s - Show Found Devices\n", __LINE__, __FUNCTION__);
588  lstBTRCoreAdapter.adapter_number = myadapter;
589  BTRCore_GetListOfScannedDevices(lhBTRCore, &lstBTRCoreScannedDevList);
590 
591  fprintf(stderr, "%d\t: %s - Display Ad Service data for scanned devices\n", __LINE__, __FUNCTION__);
592  fprintf(stderr, "%d\t: %s - *******************************************\n", __LINE__, __FUNCTION__);
593  fprintf(stderr, "%d\t: %s - numberOfDevices : %d \n", __LINE__, __FUNCTION__, lstBTRCoreScannedDevList.numberOfDevices);
594 
595  for(int i = 0; i < lstBTRCoreScannedDevList.numberOfDevices; i++)
596  {
597  fprintf(stderr, "%d\t: %s - ServiceData from Scanned Device : %d \n", __LINE__, __FUNCTION__, i);
598 
599  for(int count = 0; count < lstBTRCoreScannedDevList.devices[i].stDeviceProfile.numberOfService; count++)
600  {
601  fprintf(stderr, "%d\t: %s - ServiceData for UUID : %s \n", __LINE__, __FUNCTION__, lstBTRCoreScannedDevList.devices[i].stAdServiceData[count].pcUUIDs);
602  if(0 != lstBTRCoreScannedDevList.devices[i].stAdServiceData[count].len)
603  {
604  for (int j =0; j < lstBTRCoreScannedDevList.devices[i].stAdServiceData[count].len; j++){
605  fprintf(stderr, "%d\t: %s - ServiceData[%d] = [%x]\n ", __LINE__, __FUNCTION__, j, lstBTRCoreScannedDevList.devices[i].stAdServiceData[count].pcData[j]);
606  }
607  }
608  }
609 
610  }
611  }
612  break;
613  case 4:
614  {
615  stBTRCoreScannedDevicesCount lstBTRCoreScannedDevList;
616  fprintf(stderr, "%d\t: %s - Pick a Device to Pair...\n", __LINE__, __FUNCTION__);
617  lstBTRCoreAdapter.adapter_number = myadapter;
618  BTRCore_GetListOfScannedDevices(lhBTRCore, &lstBTRCoreScannedDevList);
619  devnum = getChoice();
620 
621  fprintf(stderr, "%d\t: %s - adapter_path %s\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.pcAdapterPath);
622  fprintf(stderr, "%d\t: %s - agent_path %s\n", __LINE__, __FUNCTION__, agent_path);
623  if ( BTRCore_PairDevice(lhBTRCore, devnum) == enBTRCoreSuccess)
624  fprintf(stderr, "%d\t: %s - device pairing successful.\n", __LINE__, __FUNCTION__);
625  else
626  fprintf(stderr, "%d\t: %s - device pairing FAILED.\n", __LINE__, __FUNCTION__);
627  }
628  break;
629  case 5:
630  {
631  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
632  fprintf(stderr, "%d\t: %s - UnPair/Forget a device\n", __LINE__, __FUNCTION__);
633  fprintf(stderr, "%d\t: %s - Pick a Device to Remove...\n", __LINE__, __FUNCTION__);
634  lstBTRCoreAdapter.adapter_number = myadapter;
635  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
636  devnum = getChoice();
637  BTRCore_UnPairDevice(lhBTRCore, devnum);
638  }
639  break;
640  case 6:
641  {
642  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
643  fprintf(stderr, "%d\t: %s - Show Known Devices...using BTRCore_GetListOfPairedDevices\n", __LINE__, __FUNCTION__);
644  lstBTRCoreAdapter.adapter_number = myadapter;
645  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList); //TODO pass in a different structure for each adapter
646  }
647  break;
648  case 7:
649  {
650  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
651  fprintf(stderr, "%d\t: %s - Pick a Device to Connect...\n", __LINE__, __FUNCTION__);
652  lstBTRCoreAdapter.adapter_number = myadapter;
653  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
654  devnum = getChoice();
655  BTRCore_ConnectDevice(lhBTRCore, devnum, enBTRCoreSpeakers);
656  connectedDeviceIndex = devnum; //TODO update this if remote device initiates connection.
657  fprintf(stderr, "%d\t: %s - device connect process completed.\n", __LINE__, __FUNCTION__);
658  }
659  break;
660  case 8:
661  {
662  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
663  fprintf(stderr, "%d\t: %s - Pick a Device to Disconnect...\n", __LINE__, __FUNCTION__);
664  lstBTRCoreAdapter.adapter_number = myadapter;
665  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
666  devnum = getChoice();
667  BTRCore_DisconnectDevice(lhBTRCore, devnum, enBTRCoreSpeakers);
668  fprintf(stderr, "%d\t: %s - device disconnect process completed.\n", __LINE__, __FUNCTION__);
669  }
670  break;
671  case 9:
672  {
673  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
674  fprintf(stderr, "%d\t: %s - Pick a Device to Connect...\n", __LINE__, __FUNCTION__);
675  lstBTRCoreAdapter.adapter_number = myadapter;
676  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
677  devnum = getChoice();
678  BTRCore_ConnectDevice(lhBTRCore, devnum, enBTRCoreMobileAudioIn);
679  connectedDeviceIndex = devnum; //TODO update this if remote device initiates connection.
680  fprintf(stderr, "%d\t: %s - device connect process completed.\n", __LINE__, __FUNCTION__);
681  }
682  break;
683  case 10:
684  {
685  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
686  fprintf(stderr, "%d\t: %s - Pick a Device to Disonnect...\n", __LINE__, __FUNCTION__);
687  lstBTRCoreAdapter.adapter_number = myadapter;
688  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
689  devnum = getChoice();
690  BTRCore_DisconnectDevice(lhBTRCore, devnum, enBTRCoreMobileAudioIn);
691  fprintf(stderr, "%d\t: %s - device disconnect process completed.\n", __LINE__, __FUNCTION__);
692  }
693  break;
694  case 11:
695  fprintf(stderr, "%d\t: %s - Getting all available adapters\n", __LINE__, __FUNCTION__);
696  //START - adapter selection: if there is more than one adapter, offer choice of which adapter to use for pairing
697  BTRCore_GetAdapters(lhBTRCore, &GetAdapters);
698  if ( GetAdapters.number_of_adapters > 1) {
699  fprintf(stderr, "%d\t: %s - There are %d Bluetooth adapters\n", __LINE__, __FUNCTION__, GetAdapters.number_of_adapters);
700  fprintf(stderr, "%d\t: %s - current adatper is %s\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.pcAdapterPath);
701  fprintf(stderr, "%d\t: %s - Which adapter would you like to use (0 = default)?\n", __LINE__, __FUNCTION__);
702  myadapter = getChoice();
703 
704  BTRCore_SetAdapter(lhBTRCore, myadapter);
705  }
706  //END adapter selection
707  break;
708  case 12:
709  lstBTRCoreAdapter.adapter_number = myadapter;
710  fprintf(stderr, "%d\t: %s - Enabling adapter %d\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.adapter_number);
711  BTRCore_EnableAdapter(lhBTRCore, &lstBTRCoreAdapter);
712  break;
713  case 13:
714  lstBTRCoreAdapter.adapter_number = myadapter;
715  fprintf(stderr, "%d\t: %s - Disabling adapter %d\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.adapter_number);
716  BTRCore_DisableAdapter(lhBTRCore, &lstBTRCoreAdapter);
717  break;
718  case 14:
719  fprintf(stderr, "%d\t: %s - Enter discoverable timeout in seconds. Zero seconds = FOREVER \n", __LINE__, __FUNCTION__);
720  lstBTRCoreAdapter.DiscoverableTimeout = getChoice();
721  fprintf(stderr, "%d\t: %s - setting DiscoverableTimeout to %d\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.DiscoverableTimeout);
722  BTRCore_SetAdapterDiscoverableTimeout(lhBTRCore, lstBTRCoreAdapter.pcAdapterPath, lstBTRCoreAdapter.DiscoverableTimeout);
723  break;
724  case 15:
725  fprintf(stderr, "%d\t: %s - Set discoverable. Zero = Not Discoverable, One = Discoverable \n", __LINE__, __FUNCTION__);
726  lstBTRCoreAdapter.discoverable = getChoice();
727  fprintf(stderr, "%d\t: %s - setting discoverable to %d\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.discoverable);
728  BTRCore_SetAdapterDiscoverable(lhBTRCore, lstBTRCoreAdapter.pcAdapterPath, lstBTRCoreAdapter.discoverable);
729  break;
730  case 16:
731  {
732  char lcAdapterName[64] = {'\0'};
733  fprintf(stderr, "%d\t: %s - Set friendly name (up to 64 characters): \n", __LINE__, __FUNCTION__);
734  if (fgets(lcAdapterName, 63 , stdin)) {
735  }
736  fprintf(stderr, "%d\t: %s - setting name to %s\n", __LINE__, __FUNCTION__, lcAdapterName);
737  BTRCore_SetAdapterDeviceName(lhBTRCore, &lstBTRCoreAdapter, lcAdapterName);
738  }
739  break;
740  case 17:
741  {
742  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
743  fprintf(stderr, "%d\t: %s - Check for Audio Sink capability\n", __LINE__, __FUNCTION__);
744  fprintf(stderr, "%d\t: %s - Pick a Device to Check for Audio Sink...\n", __LINE__, __FUNCTION__);
745  lstBTRCoreAdapter.adapter_number = myadapter;
746  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
747  devnum = getChoice();
748  if (BTRCore_FindService(lhBTRCore, devnum, BTR_CORE_A2SNK,NULL,&bfound) == enBTRCoreSuccess) {
749  if (bfound) {
750  fprintf(stderr, "%d\t: %s - Service UUID BTRCore_A2SNK is found\n", __LINE__, __FUNCTION__);
751  }
752  else {
753  fprintf(stderr, "%d\t: %s - Service UUID BTRCore_A2SNK is NOT found\n", __LINE__, __FUNCTION__);
754  }
755  }
756  else {
757  fprintf(stderr, "%d\t: %s - Error on BTRCore_FindService\n", __LINE__, __FUNCTION__);
758  }
759  }
760  break;
761  case 18:
762  {
763  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
764  fprintf(stderr, "%d\t: %s - Find a Service\n", __LINE__, __FUNCTION__);
765  fprintf(stderr, "%d\t: %s - Pick a Device to Check for Services...\n", __LINE__, __FUNCTION__);
766  lstBTRCoreAdapter.adapter_number = myadapter;
767  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
768  devnum = getChoice();
769  fprintf(stderr, "%d\t: %s - enter UUID of desired service... e.g. 0x110b for Audio Sink\n", __LINE__, __FUNCTION__);
770  if (fgets(myService,sizeof(myService),stdin)) {
771  }
772  for (i=0;i<sizeof(myService);i++)//you need to remove the final newline from the string
773  {
774  if(myService[i] == '\n')
775  myService[i] = '\0';
776  }
777  bfound=0;//assume not found
778  if (BTRCore_FindService(lhBTRCore, devnum, myService,NULL,&bfound) == enBTRCoreSuccess) {
779  if (bfound) {
780  fprintf(stderr, "%d\t: %s - Service UUID %s is found\n", __LINE__, __FUNCTION__, myService);
781  }
782  else {
783  fprintf(stderr, "%d\t: %s - Service UUID %s is NOT found\n", __LINE__, __FUNCTION__, myService);
784  }
785  }
786  else {
787  fprintf(stderr, "%d\t: %s - Error on BTRCore_FindService\n", __LINE__, __FUNCTION__);
788  }
789  }
790  break;
791  case 19:
792  {
793  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
794  fprintf(stderr, "%d\t: %s - Find a Service and get details\n", __LINE__, __FUNCTION__);
795  fprintf(stderr, "%d\t: %s - Pick a Device to Check for Services...\n", __LINE__, __FUNCTION__);
796  lstBTRCoreAdapter.adapter_number = myadapter;
797  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
798  devnum = getChoice();
799  fprintf(stderr, "%d\t: %s - enter UUID of desired service... e.g. 0x110b for Audio Sink\n", __LINE__, __FUNCTION__);
800  if (fgets(myService,sizeof(myService),stdin)) {
801  }
802  for (i=0;i<sizeof(myService);i++)//you need to remove the final newline from the string
803  {
804  if(myService[i] == '\n')
805  myService[i] = '\0';
806  }
807  bfound=0;//assume not found
808  /*CAUTION! This usage is intended for development purposes.
809  myData needs to be allocated large enough to hold the returned device data
810  for development purposes it may be helpful for an app to gain access to this data,
811  so this usage can provide that capability.
812  In most cases, simply knowing if the service exists may suffice, in which case you can use
813  the simplified option where the data pointer is NULL, and no data is copied*/
814  if (BTRCore_FindService(lhBTRCore, devnum,myService,myData,&bfound) == enBTRCoreSuccess) {
815  if (bfound) {
816  fprintf(stderr, "%d\t: %s - Service UUID %s is found\n", __LINE__, __FUNCTION__, myService);
817  fprintf(stderr, "%d\t: %s - Data is:\n %s \n", __LINE__, __FUNCTION__, myData);
818  }
819  else {
820  fprintf(stderr, "%d\t: %s - Service UUID %s is NOT found\n", __LINE__, __FUNCTION__, myService);
821  }
822  }
823  else {
824  fprintf(stderr, "%d\t: %s - Error on BTRCore_FindService\n", __LINE__, __FUNCTION__);
825  }
826  }
827  break;
828  case 20:
829  {
830  stBTRCoreScannedDevicesCount lstBTRCoreScannedDevList;
831  fprintf(stderr, "%d\t: %s - Pick a Device to Find (see if it is already paired)...\n", __LINE__, __FUNCTION__);
832  lstBTRCoreAdapter.adapter_number = myadapter;
833  BTRCore_GetListOfScannedDevices(lhBTRCore, &lstBTRCoreScannedDevList);
834  devnum = getChoice();
835  if ( BTRCore_FindDevice(lhBTRCore, devnum) == enBTRCoreSuccess)
836  fprintf(stderr, "%d\t: %s - device FOUND successful.\n", __LINE__, __FUNCTION__);
837  else
838  fprintf(stderr, "%d\t: %s - device was NOT found.\n", __LINE__, __FUNCTION__);
839  }
840  break;
841  case 21:
842  {
843  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
844  fprintf(stderr, "%d\t: %s - Pick a Device to Get Data tranport parameters...\n", __LINE__, __FUNCTION__);
845  lstBTRCoreAdapter.adapter_number = myadapter;
846  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
847  devnum = getChoice();
848 
849  BTRCore_GetDeviceMediaInfo(lhBTRCore, devnum, enBTRCoreSpeakers, &stAppData.stBtrCoreDevMediaInfo);
850 
851  stAppData.iDataPath = 0;
852  stAppData.iDataReadMTU = 0;
853  stAppData.iDataWriteMTU = 0;
854  stAppData.ui32DevDelay = 0;
855 
856  BTRCore_AcquireDeviceDataPath(lhBTRCore, devnum, enBTRCoreSpeakers, &stAppData.iDataPath, &stAppData.iDataReadMTU, &stAppData.iDataWriteMTU, &stAppData.ui32DevDelay);
857 
858  fprintf(stderr, "%d\t: %s - Device Data Path = %d \n", __LINE__, __FUNCTION__, stAppData.iDataPath);
859  fprintf(stderr, "%d\t: %s - Device Data Read MTU = %d \n", __LINE__, __FUNCTION__, stAppData.iDataReadMTU);
860  fprintf(stderr, "%d\t: %s - Device Data Write MTU= %d \n", __LINE__, __FUNCTION__, stAppData.iDataWriteMTU);
861  fprintf(stderr, "%d\t: %s - Device Delay = %d \n", __LINE__, __FUNCTION__, stAppData.ui32DevDelay);
862 
863  if (stAppData.stBtrCoreDevMediaInfo.eBtrCoreDevMType == eBTRCoreDevMediaTypeSBC) {
864  if (stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo) {
865  fprintf(stderr, "%d\t: %s - Device Media Info SFreq = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui32DevMSFreq);
866  fprintf(stderr, "%d\t: %s - Device Media Info AChan = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->eDevMAChan);
867  fprintf(stderr, "%d\t: %s - Device Media Info SbcAllocMethod= %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcAllocMethod);
868  fprintf(stderr, "%d\t: %s - Device Media Info SbcSubbands = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcSubbands);
869  fprintf(stderr, "%d\t: %s - Device Media Info SbcBlockLength= %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcBlockLength);
870  fprintf(stderr, "%d\t: %s - Device Media Info SbcMinBitpool = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcMinBitpool);
871  fprintf(stderr, "%d\t: %s - Device Media Info SbcMaxBitpool = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui8DevMSbcMaxBitpool);
872  fprintf(stderr, "%d\t: %s - Device Media Info SbcFrameLen = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui16DevMSbcFrameLen);
873  fprintf(stderr, "%d\t: %s - Device Media Info SbcBitrate = %d\n", __LINE__, __FUNCTION__, ((stBTRCoreDevMediaSbcInfo*)(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo))->ui16DevMSbcBitrate);
874  }
875  }
876  }
877  break;
878  case 22:
879  {
880  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
881  fprintf(stderr, "%d\t: %s - Pick a Device to ReleaseData tranport...\n", __LINE__, __FUNCTION__);
882  lstBTRCoreAdapter.adapter_number = myadapter;
883  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
884  devnum = getChoice();
885  BTRCore_ReleaseDeviceDataPath(lhBTRCore, devnum, enBTRCoreSpeakers);
886 
887  stAppData.iDataPath = 0;
888  stAppData.iDataReadMTU = 0;
889  stAppData.iDataWriteMTU = 0;
890  }
891  break;
892  case 23:
893  fprintf(stderr, "%d\t: %s - Enter Encoded SBC file location to send to BT Headset/Speakers...\n", __LINE__, __FUNCTION__);
894  sbcEncodedFileName = getEncodedSBCFile();
895  if (sbcEncodedFileName) {
896  fprintf(stderr, "%d\t: %s - We will send %s to BT FD %d \n", __LINE__, __FUNCTION__, sbcEncodedFileName, stAppData.iDataPath);
897  sendSBCFileOverBT(sbcEncodedFileName, stAppData.iDataPath, stAppData.iDataWriteMTU);
898  free(sbcEncodedFileName);
899  sbcEncodedFileName = NULL;
900  }
901  else {
902  fprintf(stderr, "%d\t: %s - Invalid file location\n", __LINE__, __FUNCTION__);
903  }
904  break;
905  case 29:
906  fprintf(stderr, "%d\t: %s - rtp deplayload and play some music over BT\n", __LINE__, __FUNCTION__);
907  fprintf(stderr, "%d\t: %s - about how many minutes to play?\n", __LINE__, __FUNCTION__);
908  choice = getChoice();
909  BT_loop = 6000 * choice;//6000 equates to roughly one minute
910  writeSBC = 1;
911  sleep(2);
912  if (system("gst-launch-1.0 filesrc location=/tmp/myfifo ! sbcparse ! sbcdec ! brcmpcmsink")) {
913  }
914  break;
915  case 30:
916  fprintf(stderr, "%d\t: %s - install agent - NoInputNoOutput\n", __LINE__, __FUNCTION__);
917  BTRCore_RegisterAgent(lhBTRCore, 0);// 2nd arg controls the mode, 0 = NoInputNoOutput, 1 = DisplayYesNo
918  break;
919  case 31:
920  fprintf(stderr, "%d\t: %s - install agent - DisplayYesNo\n", __LINE__, __FUNCTION__);
921  BTRCore_RegisterAgent(lhBTRCore, 1);// 2nd arg controls the mode, 0 = NoInputNoOutput, 1 = DisplayYesNo
922  break;
923  case 32:
924  fprintf(stderr, "%d\t: %s - uninstall agent - DisplayYesNo\n", __LINE__, __FUNCTION__);
925  BTRCore_UnregisterAgent(lhBTRCore);
926  break;
927  case 33:
928  fprintf(stderr, "%d\t: %s - register connection-in Intimation CB\n", __LINE__, __FUNCTION__);
929  BTRCore_RegisterConnectionIntimationCb(lhBTRCore, cb_connection_intimation, NULL);
930  break;
931  case 34:
932  fprintf(stderr, "%d\t: %s - register authentication CB\n", __LINE__, __FUNCTION__);
933  BTRCore_RegisterConnectionAuthenticationCb(lhBTRCore, cb_connection_authentication, NULL);
934  break;
935  case 35:
936  fprintf(stderr, "%d\t: %s - accept the connection\n", __LINE__, __FUNCTION__);
937  acceptConnection = 1;
938  break;
939  case 36:
940  fprintf(stderr, "%d\t: %s - deny the connection\n", __LINE__, __FUNCTION__);
941  acceptConnection = 2;//anything but 1 means do not connect
942  break;
943  case 37:
944  fprintf(stderr, "%d\t: %s - Pick a Device to Check if Connectable...\n", __LINE__, __FUNCTION__);
945  devnum = getChoice();
946  BTRCore_IsDeviceConnectable(lhBTRCore, devnum);
947  break;
948  case 38:
949  if (default_adapter != NO_ADAPTER) {
950  fprintf(stderr, "%d\t: %s - Looking for LE devices on BT adapter %s\n", __LINE__, __FUNCTION__, lstBTRCoreAdapter.pcAdapterPath);
951  fprintf(stderr, "%d\t: %s - Performing LE scan for 30 seconds . Please wait...\n", __LINE__, __FUNCTION__);
952  BTRCore_StartDiscovery(lhBTRCore, lstBTRCoreAdapter.pcAdapterPath, enBTRCoreLE, 30);
953  fprintf(stderr, "%d\t: %s - scan complete\n", __LINE__, __FUNCTION__);
954  }
955  else {
956  fprintf(stderr, "%d\t: %s - Error, no default_adapter set\n", __LINE__, __FUNCTION__);
957  }
958  break;
959  case 39:
960  {
961  stBTRCoreScannedDevicesCount lBTRCoreScannedDevList;
962  fprintf(stderr, "%d\t: %s - Pick a LE Device to Connect...\n", __LINE__, __FUNCTION__);
963  lstBTRCoreAdapter.adapter_number = myadapter;
964  BTRCore_GetListOfScannedDevices(lhBTRCore, &lBTRCoreScannedDevList);
965  devnum = getChoice();
966  BTRCore_ConnectDevice(lhBTRCore, devnum, enBTRCoreLE);
967  connectedDeviceIndex = devnum; //TODO update this if remote device initiates connection.
968  fprintf(stderr, "%d\t: %s - LE device connect process completed.\n", __LINE__, __FUNCTION__);
969  }
970 
971  break;
972  case 40:
973  {
974  stBTRCoreScannedDevicesCount lstBTRCoreScannedDevList;
975  fprintf(stderr, "%d\t: %s - Pick a LE Device to Disconnect...\n", __LINE__, __FUNCTION__);
976  lstBTRCoreAdapter.adapter_number = myadapter;
977  BTRCore_GetListOfScannedDevices(lhBTRCore, &lstBTRCoreScannedDevList);
978  devnum = getChoice();
979  BTRCore_DisconnectDevice(lhBTRCore, devnum,enBTRCoreLE);
980  fprintf(stderr, "%d\t: %s - LE device disconnect process completed.\n", __LINE__, __FUNCTION__);
981  }
982  break;
983  case 41:
984  {
985  fprintf(stderr, "%d\t: %s - Pick a connected LE Device to Show Gatt Properties.\n", __LINE__, __FUNCTION__);
986  lstBTRCoreAdapter.adapter_number = myadapter;
987  devnum = getChoice();
988  char *leUuidString = getLeUuidString();
989 
990  if (leUuidString) {
991 
992  fprintf(stderr, "%d\t: %s - select the property you want to query for.\n", __LINE__, __FUNCTION__);
993  fprintf(stderr, "[0 - Uuid | 1 - Primary | 2 - Device | 3 - Service | 4 - Value |"
994  " 5 - Notifying | 6 - Flags | 7 -Character]\n");
995  int propSelection = getChoice();
996 
997  if (!propSelection) {
998  stBTRCoreUUIDList lstBTRCoreUUIDList;
999  unsigned char i = 0;
1000  BTRCore_GetLEProperty(lhBTRCore, devnum, leUuidString, propSelection, (void*)&lstBTRCoreUUIDList);
1001  if (lstBTRCoreUUIDList.numberOfUUID) {
1002  fprintf(stderr, "\n\nObtained 'Flag' indices are based on the below mapping..\n"
1003  "0 - read\n"
1004  "1 - write\n"
1005  "2 - encrypt-read\n"
1006  "3 - encrypt-write\n"
1007  "4 - encrypt-authenticated-read\n"
1008  "5 - encrypt-authenticated-write\n"
1009  "6 - secure-read (Server only)\n"
1010  "7 - secure-write (Server only)\n"
1011  "8 - notify\n"
1012  "9 - indicate\n"
1013  "10 - broadcast\n"
1014  "11 - write-without-response\n"
1015  "12 - authenticated-signed-writes\n"
1016  "13 - reliable-write\n"
1017  "14 - writable-auxiliaries\n");
1018  fprintf(stderr, "\n\t%-40s Flag\n", "UUID List");
1019  for (; i<lstBTRCoreUUIDList.numberOfUUID; i++) {
1020  fprintf(stderr, "\n\t%-40s ", lstBTRCoreUUIDList.uuidList[i].uuid);
1021  getBitsToString(lstBTRCoreUUIDList.uuidList[i].flags);
1022  }
1023  } else {
1024  fprintf (stderr, "\n\n\tNo UUIDs Found...\n");
1025  }
1026  } else
1027  if (propSelection == 1 || propSelection == 5) {
1028  unsigned char val = 0;
1029  BTRCore_GetLEProperty(lhBTRCore, devnum, leUuidString, propSelection, (void*)&val);
1030  fprintf(stderr, "\n\tResult : %d\n", val);
1031  } else
1032  if (propSelection == 4 || propSelection == 2 ||
1033  propSelection == 3 || propSelection == 7 ){
1034  char val[BTRCORE_MAX_STR_LEN] = "\0";
1035  BTRCore_GetLEProperty(lhBTRCore, devnum, leUuidString, propSelection, (void*)&val);
1036  fprintf(stderr, "\n\tResult : %s\n", val);
1037  } else
1038  if (propSelection == 6) {
1039  char val[15][64]; int i=0;
1040  memset (val, 0, sizeof(val));
1041  BTRCore_GetLEProperty(lhBTRCore, devnum, leUuidString, propSelection, (void*)&val);
1042  fprintf(stderr, "\n\tResult :\n");
1043  for (; i < 15 && val[i][0]; i++) {
1044  fprintf(stderr, "\t- %s\n", val[i]);
1045  }
1046  }
1047  free(leUuidString);
1048  leUuidString = NULL;
1049  }
1050  }
1051  break;
1052  case 42:
1053  {
1054  fprintf(stderr, "%d\t: %s - Pick a connected LE Device to Perform Operation.\n", __LINE__, __FUNCTION__);
1055  lstBTRCoreAdapter.adapter_number = myadapter;
1056  devnum = getChoice();
1057  char *leUuidString = getLeUuidString();
1058  char writeArg[BTRCORE_MAX_STR_LEN] = "\0";
1059 
1060  fprintf(stderr, "%d\t: %s - Pick a option to perform method operation LE.\n", __LINE__, __FUNCTION__);
1061  fprintf(stderr, "\t[1 - ReadValue | 2 - WriteValue | 3 - StartNotify | 4 - StopNotify]\n");
1062 
1063  enBTRCoreLeOp aenBTRCoreLeOp = getChoice();
1064 
1065  if (aenBTRCoreLeOp == enBTRCoreLeOpGWriteValue) {
1066  fprintf(stderr, "\tEnter the Value to Write : ");
1067  if (scanf("%s", writeArg)) {
1068  }
1069  }
1070 
1071  if (leUuidString) {
1072  char val[BTRCORE_MAX_STR_LEN] = "\0";
1073  BTRCore_PerformLEOp (lhBTRCore, devnum, leUuidString, aenBTRCoreLeOp, writeArg, val);
1074  free(leUuidString);
1075  leUuidString = NULL;
1076  if (aenBTRCoreLeOp == 1) {
1077  fprintf(stderr, "%d\t: %s - Obtained Value [%s]\n", __LINE__, __FUNCTION__, val );
1078  }
1079  }
1080  }
1081  break;
1082  case 43:
1083  {
1084  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
1085  fprintf(stderr, "%d\t: %s - Pick a Device to Connect...\n", __LINE__, __FUNCTION__);
1086  lstBTRCoreAdapter.adapter_number = myadapter;
1087  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
1088  devnum = getChoice();
1089  BTRCore_ConnectDevice(lhBTRCore, devnum, enBTRCoreUnknown);
1090  connectedDeviceIndex = devnum; //TODO update this if remote device initiates connection.
1091  fprintf(stderr, "%d\t: %s - device connect process completed.\n", __LINE__, __FUNCTION__);
1092  }
1093  break;
1094  case 44:
1095  {
1096  stBTRCorePairedDevicesCount lstBTRCorePairedDevList;
1097  fprintf(stderr, "%d\t: %s - Pick a Device to Disconnect...\n", __LINE__, __FUNCTION__);
1098  lstBTRCoreAdapter.adapter_number = myadapter;
1099  BTRCore_GetListOfPairedDevices(lhBTRCore, &lstBTRCorePairedDevList);
1100  devnum = getChoice();
1101  BTRCore_DisconnectDevice(lhBTRCore, devnum, enBTRCoreUnknown);
1102  fprintf(stderr, "%d\t: %s - device disconnect process completed.\n", __LINE__, __FUNCTION__);
1103  }
1104  break;
1105 
1106  case 99:
1107  fprintf(stderr, "%d\t: %s - Quitting program!\n", __LINE__, __FUNCTION__);
1108  BTRCore_DeInit(lhBTRCore);
1109  exit(0);
1110  break;
1111  default:
1112  fprintf(stderr, "%d\t: %s - Available options are:\n", __LINE__, __FUNCTION__);
1113  printMenu();
1114  break;
1115  }
1116  } while (1);
1117 
1118 
1119  if (stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo)
1120  free(stAppData.stBtrCoreDevMediaInfo.pstBtrCoreDevMCodecInfo);
1121 
1122  return 0;
1123 }
1124 
BTRCore_Init
enBTRCoreRet BTRCore_Init(tBTRCoreHandle *phBTRCore)
This API connects to a bus daemon and registers the client with it.
Definition: btrCore.c:2494
BTRCore_GetAdapters
enBTRCoreRet BTRCore_GetAdapters(tBTRCoreHandle hBTRCore, stBTRCoreGetAdapters *pstGetAdapters)
This API returns the value of org.bluez.Manager.Getadapters .
Definition: btrCore.c:2899
BTRCore_DisableAdapter
enBTRCoreRet BTRCore_DisableAdapter(tBTRCoreHandle hBTRCore, stBTRCoreAdapter *apstBTRCoreAdapter)
This API disables specific adapter.
Definition: btrCore.c:3036
BTRCore_SetAdapterDeviceName
enBTRCoreRet BTRCore_SetAdapterDeviceName(tBTRCoreHandle hBTRCore, stBTRCoreAdapter *apstBTRCoreAdapter, char *apcAdapterDeviceName)
This API sets a friendly name to BT adapter device.
Definition: btrCore.c:3194
BTRCore_GetLEProperty
enBTRCoreRet BTRCore_GetLEProperty(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, const char *apcBTRCoreLEUuid, enBTRCoreLeProp aenBTRCoreLeProp, void *apvBTRCorePropVal)
This API returns the Low energy profile device name and address.
Definition: btrCore.c:5173
BTRCore_AcquireDeviceDataPath
enBTRCoreRet BTRCore_AcquireDeviceDataPath(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, enBTRCoreDeviceType aenBTRCoreDevType, int *aiDataPath, int *aidataReadMTU, int *aidataWriteMTU, unsigned int *apui32Delay)
This API returns the bluetooth device address.
Definition: btrCore.c:4415
BTRCore_GetListOfScannedDevices
enBTRCoreRet BTRCore_GetListOfScannedDevices(tBTRCoreHandle hBTRCore, stBTRCoreScannedDevicesCount *pListOfScannedDevices)
This API returns the number of devices scanned.
Definition: btrCore.c:3500
BTRCore_StartDiscovery
enBTRCoreRet BTRCore_StartDiscovery(tBTRCoreHandle hBTRCore, const char *pAdapterPath, enBTRCoreDeviceType aenBTRCoreDevType, unsigned int aui32DiscDuration)
This method starts the device discovery session.
Definition: btrCore.c:3392
BTRCore_IsDeviceConnectable
enBTRCoreRet BTRCore_IsDeviceConnectable(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId)
This API checks the device is connectable.
Definition: btrCore.c:3864
BTRCore_EnableAdapter
enBTRCoreRet BTRCore_EnableAdapter(tBTRCoreHandle hBTRCore, stBTRCoreAdapter *apstBTRCoreAdapter)
This API enables specific adapter.
Definition: btrCore.c:3005
BTRCore_FindService
enBTRCoreRet BTRCore_FindService(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, const char *UUID, char *XMLdata, int *found)
This API is used to confirm if a given service exists on a device.
Definition: btrCore.c:3762
_stBTRCoreGetAdapters
Definition: btrCore.h:257
_stBTRCoreUUIDList
Definition: btrCore.h:454
BTRCore_FindDevice
enBTRCoreRet BTRCore_FindDevice(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId)
This API checks the device entry in the scanned device list.
Definition: btrCore.c:3727
BTRCore_GetDeviceMediaInfo
enBTRCoreRet BTRCore_GetDeviceMediaInfo(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, enBTRCoreDeviceType aenBTRCoreDevType, stBTRCoreDevMediaInfo *apstBTRCoreDevMediaInfo)
This API returns current media info that includes the codec info, channel modes, subbands etc.
Definition: btrCore.c:4232
BTRCore_ReleaseDeviceDataPath
enBTRCoreRet BTRCore_ReleaseDeviceDataPath(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, enBTRCoreDeviceType enDeviceType)
This API release the bluetooth device address.
Definition: btrCore.c:4483
_stBTRCoreDevMediaSbcInfo
Definition: btrCore.h:359
BTRCore_UnregisterAgent
enBTRCoreRet BTRCore_UnregisterAgent(tBTRCoreHandle hBTRCore)
This unregisters the agent that has been previously registered.
Definition: btrCore.c:2836
BTRCore_GetListOfPairedDevices
enBTRCoreRet BTRCore_GetListOfPairedDevices(tBTRCoreHandle hBTRCore, stBTRCorePairedDevicesCount *pListOfDevices)
Gets the paired devices list.
Definition: btrCore.c:3695
BTRCore_GetAdapter
enBTRCoreRet BTRCore_GetAdapter(tBTRCoreHandle hBTRCore, stBTRCoreAdapter *apstBTRCoreAdapter)
This API returns the bluetooth adapter path.
Definition: btrCore.c:2926
appDataStruct
Definition: btrMgr_streamInOutCoreTest.c:43
BTRCore_PairDevice
enBTRCoreRet BTRCore_PairDevice(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId)
This API initiates the pairing of the device.
Definition: btrCore.c:3539
BTRCore_DeInit
enBTRCoreRet BTRCore_DeInit(tBTRCoreHandle hBTRCore)
This APi deinitialzes and free BTRCore.
Definition: btrCore.c:2657
BTRCore_DisconnectDevice
enBTRCoreRet BTRCore_DisconnectDevice(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, enBTRCoreDeviceType aenBTRCoreDevType)
This method gracefully disconnects all connected profiles and then terminates connection.
Definition: btrCore.c:3993
_stBTRCoreDevMediaInfo
Definition: btrCore.h:382
BTRCore_SetAdapterDiscoverable
enBTRCoreRet BTRCore_SetAdapterDiscoverable(tBTRCoreHandle hBTRCore, const char *pAdapterPath, unsigned char discoverable)
This API sets adapter as discoverable.
Definition: btrCore.c:3098
_stBTRCorePairedDevicesCount
Definition: btrCore.h:338
BTRCore_RegisterAgent
enBTRCoreRet BTRCore_RegisterAgent(tBTRCoreHandle hBTRCore, int iBTRCapMode)
This API registers an agent handler.
Definition: btrCore.c:2803
BTRCore_SetAdapter
enBTRCoreRet BTRCore_SetAdapter(tBTRCoreHandle hBTRCore, int adapter_number)
This API sets Current Bluetooth Adapter to use.
Definition: btrCore.c:2958
BTRCore_UnPairDevice
enBTRCoreRet BTRCore_UnPairDevice(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId)
This API removes the remote device object at the given path.
Definition: btrCore.c:3630
BTRCore_PerformLEOp
enBTRCoreRet BTRCore_PerformLEOp(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, const char *apcBTRCoreLEUuid, enBTRCoreLeOp aenBTRCoreLeOp, char *apLeOpArg, char *rpLeOpRes)
This API is used to perform read, write, notify operations on LE devices.
Definition: btrCore.c:5262
_stBTRCoreDevStatusCBInfo
Definition: btrCore.h:273
_stBTRCoreScannedDevicesCount
Definition: btrCore.h:333
BTRCore_SetAdapterDiscoverableTimeout
enBTRCoreRet BTRCore_SetAdapterDiscoverableTimeout(tBTRCoreHandle hBTRCore, const char *pAdapterPath, unsigned short timeout)
This API sets how long the adapter is discoverable.
Definition: btrCore.c:3130
_stBTRCoreConnCBInfo
Definition: btrCore.h:343
TRUE
#define TRUE
Defines for TRUE/FALSE/ENABLE flags.
Definition: wifi_common_hal.h:199
BTRCore_ConnectDevice
enBTRCoreRet BTRCore_ConnectDevice(tBTRCoreHandle hBTRCore, tBTRCoreDevId aBTRCoreDevId, enBTRCoreDeviceType aenBTRCoreDevType)
This method connect any profiles the remote device supports.
Definition: btrCore.c:3921
_stBTRCoreDevMediaMpegInfo
Definition: btrCore.h:371
_stBTRCoreAdapter
Definition: btrCore.h:306