RDK Documentation (Open Sourced RDK Components)
btrMgr_testFcgi.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 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include "btmgr.h"
23 #include <fcgi_stdio.h>
24 
25 const char* pSplChar = "%20";
26 const char *pChangePower = "ChangePower";
27 const char *pSetPower = "SetPower=";
28 const char *pSaveName = "SaveName=";
29 const char *pPairTo = "PairTo=";
30 const char *pUnPairTo = "UnpairTo=";
31 const char *pGetName = "GetName";
32 const char *pStartDiscovery = "StartDiscovery";
33 const char *pShowDiscovered = "ShowDiscovered";
34 const char *pShowAllPaired = "ShowAllPaired";
35 const char *pStartPlaying = "StartPlaying=";
36 const char *pStopPlaying = "StopPlaying";
37 
38 const char *pMainPage = ""
39  "<tr> "
40  "<td> Power On/Off Adapter </td> "
41  "<td> <button id=\"power\" class=\"button\" onclick=\"ChangePower()\">....Go....</button> </td> "
42  "</tr> "
43  " "
44  "<tr> "
45  "<td> Get Name Of the Adapter </td> "
46  "<td> <button class=\"button\" onclick=\"GetName()\">....Go....</button> </td> "
47  "</tr> "
48  " "
49  "<tr> "
50  "<td> Show Paired Devices </td> "
51  "<td> <button class=\"button\" onclick=\"ShowAllPaired()\">....Go....</button> </td> "
52  "</tr> "
53  " "
54  "<tr> "
55  "<td> Start Discovery </td> "
56  "<td> <button class=\"button\" onclick=\"StartDiscovery()\">....Go....</button> </td> "
57  "</tr> "
58  " "
59  "<tr> "
60  "<td> Show Discovered Devices </td> "
61  "<td> <button class=\"button\" onclick=\"ShowDiscovered()\">....Go....</button> </td> "
62  "</tr> "
63  " "
64  "<tr> "
65  "<td> Stop Playing </td> "
66  "<td> <button class=\"button\" onclick=\"StopPlaying()\">....Go....</button> </td> "
67  "</tr> "
68  "</table> "
69  " "
70  "<script type=\"text/javascript\"> "
71  " "
72  "function ChangePower() { "
73  " location.href = \"ChangePower\" "
74  "} "
75  " "
76  "function GetName() { "
77  " location.href = \"GetName\" "
78  "} "
79  " "
80  "function StartDiscovery() { "
81  " location.href = \"StartDiscovery\" "
82  "} "
83  " "
84  "function ShowDiscovered() { "
85  " location.href = \"ShowDiscovered\" "
86  "} "
87  " "
88  "function ShowAllPaired() { "
89  " location.href = \"ShowAllPaired\" "
90  "} "
91  " "
92  "function StopPlaying() { "
93  " location.href = \"StopPlaying\" "
94  "} "
95  " "
96  "</script> "
97  " "
98  "</body> "
99  "</html> "
100  " ";
101 
102 // "font-family: helvetica; "
103 // " background-color: black; "
104 // " color: white; "
105 // " border-radius: 4px 4px 0 0; "
106 
107 const char* pStylePage = ""
108  "<html> "
109  "<head> "
110  " "
111  "<meta http-equiv=\"pragma\" content=\"no-cache\" />"
112  "<meta http-equiv=\"expires\" content=\"0\" />"
113  " "
114  "<style> "
115  ".button { "
116  " padding: 5px 15px; "
117  " text-decoration: none; "
118  " text-align: center; "
119  " display: inline-block; "
120  " font-size: 14px; "
121  " float: right "
122  "} "
123  " "
124  ".button:hover "
125  "{ "
126  " background-color: orange; "
127  "} "
128  " "
129  ".button:active {"
130  " background-color: #A0B0C0; "
131  " transform: translateY(4px); "
132  "}"
133  " "
134  "table, th, td { "
135  "color: black;"
136  "} "
137  " "
138  "</style> "
139  "</head> "
140  " "
141  "<body bgcolor=\"#E0F48E\"> "
142  " "
143  "<table align=\"center\"> "
144  "";
145 
146 const char* pGoHome = ""
147  "<hr> "
148  "<p> </p> "
149  "<button onclick=\"location.href='home';\" class=\"button\">Back</button> "
150  " ";
151 
152 const char* pGetNamePage = ""
153  "<script type=\"text/javascript\"> "
154  " "
155  "function ChangeName() { "
156  " location.href = \"SaveName=\" + document.getElementById(\"name1\").value; "
157  "} "
158  " "
159  "</script> "
160  " "
161  "</body> "
162  "</html> "
163  " ";
164 
165 void findAndReplaceSplCharWithSpace (char* ptr)
166 {
167  if (ptr)
168  {
169  int l1 = 0;
170  int l2 = 0;
171  int l3 = 0;
172  char* p = NULL;
173  char* t = ptr;
174  char temp[1024];
175 
176  memset(temp, '\0', sizeof(temp));
177 
178  l1 = strlen(pSplChar);
179  while (1)
180  {
181  p = strstr(ptr, pSplChar);
182  if (p)
183  {
184  l2 = strlen(ptr);
185  l3 = strlen(p);
186 
187  strncat (temp, ptr, (l2 - l3));
188  strcat (temp, " ");
189  ptr = p+l1;
190  }
191  else
192  {
193  strcat(temp, ptr);
194  break;
195  }
196  }
197  strcpy (t, temp);
198  }
199 }
200 
201 unsigned long long int gLastPlaying = 0;
202 
203 void startStreaming (unsigned long long int handle )
204 {
205  BTRMGR_Result_t rc = BTRMGR_RESULT_SUCCESS;
206 
207  /* Connect to a device first */
208  rc = BTRMGR_ConnectToDevice(0, handle, BTRMGR_DEVICE_OP_TYPE_AUDIO_OUTPUT);
209  if (BTRMGR_RESULT_SUCCESS != rc)
210  printf ("<tr>" "<td> Connection establishment failed\n </td>" "</tr>" "</table> ");
211  else
212  {
213  sleep(5);
214  rc = BTRMGR_StartAudioStreamingOut(0, handle, BTRMGR_DEVICE_OP_TYPE_AUDIO_OUTPUT);
215  if (BTRMGR_RESULT_SUCCESS != rc)
216  printf ("<tr>" "<td> Failed to Stream out to this device\n </td>" "</tr>" "</table> ");
217  else
218  {
219  gLastPlaying = handle;
220  printf ("<tr>" "<td> Enjoy the show...\t\t :-)\n </td>" "</tr>" "</table> ");
221  }
222  }
223 }
224 
225 void stopStreaming ()
226 {
227  BTRMGR_Result_t rc = BTRMGR_RESULT_SUCCESS;
228 
229  rc = BTRMGR_StopAudioStreamingOut(0, gLastPlaying);
230  if (BTRMGR_RESULT_SUCCESS != rc)
231  printf ("<tr>" "<td> Failed to Stop Streaming... :( </td>" "</tr>" "</table> ");
232  else
233  {
234  gLastPlaying = 0;
235  printf ("<tr>" "<td> Successfully Stopped... </td>" "</tr>" "</table> ");
236  }
237 }
238 
239 void GetNumberOfAdapters ()
240 {
241  unsigned char numOfAdapters = 0;
242  if (BTRMGR_RESULT_SUCCESS != BTRMGR_GetNumberOfAdapters(&numOfAdapters))
243  printf ("<tr>" "<td> Failed to get the count\n\n\n</td>" "</tr>" "</table> ");
244  else
245  printf ("<tr>" "<td> We found %d Bluetooth adapters in this Platform\n\n\n</td>" "</tr>" "</table> ", numOfAdapters);
246 
247  return;
248 }
249 
250 int main ()
251 {
252  int initOnce = 0;
253  char *pInput = NULL;
254  char array[32] = "";
255  BTRMGR_Result_t rc = BTRMGR_RESULT_SUCCESS;
256 
257  while (FCGI_Accept() >= 0)
258  {
259  const char* func = getenv("REQUEST_URI");
260 
261  if (!func)
262  {
263  fprintf(stderr, "No No No.. This is totally not good..! :(\n");
264  }
265  else
266  {
267  fprintf (stderr, "The func is .. %s\n", func);
268  if (NULL != strstr (func, "/btmgr/"))
269  {
270  if (0 == initOnce)
271  {
272  rc = BTRMGR_Init();
273 
274  if (BTRMGR_RESULT_SUCCESS != rc)
275  {
276  fprintf (stderr, "Failed to init BTMgr.. Quiting.. \n");
277  return 0;
278  }
279  initOnce = 1;
280  }
281 
282  /* Reset the input pointer to NULL; as it is being reused */
283  pInput = NULL;
284 
285  /* Begin Rendeing the html page */
286  printf("Content-type: text/html\r\n\r\n");
287  printf ("%s", pStylePage);
288 
289  if (NULL != strstr (func, "home"))
290  {
291  printf ("%s", pMainPage);
292  }
293  else
294  {
295  if (NULL != (pInput = strstr (func, pChangePower)))
296  {
297  unsigned char power_status = 1;
298  char temp1[32] = "ON";
299  char temp2[32] = "OFF";
300 
301  rc = BTRMGR_GetAdapterPowerStatus (0, &power_status);
302  if (BTRMGR_RESULT_SUCCESS != rc)
303  power_status = 1;
304 
305  if (0 == power_status)
306  {
307  strcpy (temp1, "OFF");
308  strcpy (temp2, "ON");
309  }
310 
311 
312  printf ("<tr> "
313  "<td>The Device is Currently %s; Power %s Adapter </td> "
314  "<td> <button onclick=\"location.href='SetPower=%s';\" class=\"button\"> %s</button> </td>"
315  "</tr> "
316  "</table> "
317  " ", temp1, temp2, temp2, temp2);
318  }
319  else if (NULL != (pInput = strstr (func, pSetPower)))
320  {
321  int length = strlen(pSetPower);
322  unsigned char power = 1;
323  if (0 == strcmp("OFF", (pInput+length)))
324  power = 0;
325 
326  rc = BTRMGR_SetAdapterPowerStatus(0, power);
327 
328  if (BTRMGR_RESULT_SUCCESS != rc)
329  printf ("<tr>" "<td> Failed tp Set the Power... </td>" "</tr>" "</table> ");
330  else
331  printf ("<tr>" "<td> Successfully Set the power... </td>" "</tr>" "</table> ");
332  }
333  else if (NULL != (pInput = strstr (func, pSaveName)))
334  {
335  int length = strlen(pSaveName);
336 
337  memset (array, '\0', sizeof(array));
338  strncpy (array, (pInput+length), 30);
339  findAndReplaceSplCharWithSpace(array);
340 
341  rc = BTRMGR_SetAdapterName(0, array);
342  if (BTRMGR_RESULT_SUCCESS != rc)
343  printf ("<tr>" "<td> Failed to Set the name as %s... </td>" "</tr>" "</table> ", array);
344  else
345  printf ("<tr>" "<td> Successfully Set the name to %s... </td>" "</tr>" "</table> ", array);
346  }
347  else if (NULL != (pInput = strstr (func, pPairTo)))
348  {
349  int length = strlen(pPairTo);
350  unsigned long long int handle = 0;
351 
352  memset (array, '\0', sizeof(array));
353  strncpy (array, (pInput+length), 30);
354  handle = strtoll(array, NULL, 0);
355 
356  rc = BTRMGR_PairDevice(0, handle);
357  if (BTRMGR_RESULT_SUCCESS != rc)
358  printf ("<tr>" "<td> Failed to Pair to %s </td>" "</tr>" "</table> ", array);
359  else
360  printf ("<tr>" "<td> Successfully Pair to %s </td>" "</tr>" "</table> ", array);
361 
362  }
363  else if (NULL != (pInput = strstr (func, pUnPairTo)))
364  {
365  int length = strlen(pUnPairTo);
366  unsigned long long int handle = 0;
367 
368  memset (array, '\0', sizeof(array));
369  strncpy (array, (pInput+length), 30);
370  handle = strtoll(array, NULL, 0);
371 
372  rc = BTRMGR_UnpairDevice(0, handle);
373 
374  if (BTRMGR_RESULT_SUCCESS != rc)
375  printf ("<tr>" "<td> Failed to UnPair to %s </td>" "</tr>" "</table> ", array);
376  else
377  printf ("<tr>" "<td> Successfully UnPair to %s </td>" "</tr>" "</table> ", array);
378  }
379  else if (NULL != (pInput = strstr (func, pGetName)))
380  {
381  memset (array, '\0', sizeof(array));
382 
383  rc = BTRMGR_GetAdapterName(0, array);
384  if (BTRMGR_RESULT_SUCCESS != rc)
385  {
386  printf ("<tr>" "<td> Failed to Get the name of this Device </td>" "</tr>" "</table> ");
387  }
388  else
389  {
390  printf ("<tr> "
391  "<td>Name of this Adapter is </td> "
392  "<td><input type=\"text\" id=\"name1\" style=\"background-color:#CBD5E4;color:black\" value=%s></td> "
393  "<td> <button id=\"nameChange\" class=\"button\" onclick=\"ChangeName()\">Change Name</button> </td> "
394  "</tr> "
395  "</table> "
396  " ", array);
397 
398  }
399  }
400  else if (NULL != (pInput = strstr (func, pStartDiscovery)))
401  {
402  rc = BTRMGR_StartDeviceDiscovery(0, BTRMGR_DEVICE_OP_TYPE_AUDIO_OUTPUT);
403  if (BTRMGR_RESULT_SUCCESS != rc)
404  printf ("<tr>" "<td> Failed to Start Discovery... </td>" "</tr>" "</table> ");
405  else
406  printf ("<tr>" "<td> Discovery Start Successfully... </td>" "</tr>" "</table> ");
407  }
408  else if (NULL != (pInput = strstr (func, pShowDiscovered)))
409  {
410  BTRMGR_DiscoveredDevicesList_t discoveredDevices;
411  rc = BTRMGR_StopDeviceDiscovery(0, BTRMGR_DEVICE_OP_TYPE_AUDIO_OUTPUT);
412  if (BTRMGR_RESULT_SUCCESS != rc)
413  printf ("<tr>" "<td> Stopping Discovery Failed... </td>" "</tr>" "</table> ");
414  else
415  {
416  memset (&discoveredDevices, 0, sizeof(discoveredDevices));
417  rc = BTRMGR_GetDiscoveredDevices(0, &discoveredDevices);
418  if (BTRMGR_RESULT_SUCCESS != rc)
419  {
420  printf ("<tr>" "<td> Failed to Get the Discovered Devices List... </td>" "</tr>" "</table> ");
421  }
422  else
423  {
424  int j = 0;
425  for (; j< discoveredDevices.m_numOfDevices; j++)
426  {
427  memset (array, '\0', sizeof(array));
428  printf ("<tr>");
429  printf ("<td> %s </td>", discoveredDevices.m_deviceProperty[j].m_name );
430  printf ("<td> %s </td>", discoveredDevices.m_deviceProperty[j].m_deviceAddress);
431  sprintf (array, "%llu", discoveredDevices.m_deviceProperty[j].m_deviceHandle);
432  printf ("<td> <button onclick=\"location.href='PairTo=%s';\" class=\"button\"> Pair This </button> </td>", array);
433  printf ("</tr>");
434  }
435  printf ("</table>\n");
436  printf ("<p> We have %d devices discovered </p>\n", discoveredDevices.m_numOfDevices);
437  }
438  }
439  }
440  else if (NULL != (pInput = strstr (func, pShowAllPaired)))
441  {
442  BTRMGR_PairedDevicesList_t pairedDevices;
443  memset (&pairedDevices, 0, sizeof(pairedDevices));
444 
445  rc = BTRMGR_GetPairedDevices(0, &pairedDevices);
446  if (BTRMGR_RESULT_SUCCESS != rc)
447  printf ("<tr>" "<td> Failed to Get the Paired Devices List... </td>" "</tr>" "</table> ");
448  else
449  {
450  int j = 0;
451  for (; j< pairedDevices.m_numOfDevices; j++)
452  {
453  memset (array, '\0', sizeof(array));
454  printf ("<tr>");
455  printf ("<td> %s </td>", pairedDevices.m_deviceProperty[j].m_name );
456  sprintf (array, "%llu" , pairedDevices.m_deviceProperty[j].m_deviceHandle);
457  printf ("<td> <button onclick=\"location.href='StartPlaying=%s';\" class=\"button\"> StartPlaying </button> </td>", array);
458  printf ("<td> <button onclick=\"location.href='UnpairTo=%s';\" class=\"button\"> UnPair </button> </td>", array);
459  printf ("</tr>");
460  }
461  printf ("</table>\n");
462  printf ("<p> We have %d devices paired </p>\n", pairedDevices.m_numOfDevices);
463  }
464  }
465  else if (NULL != (pInput = strstr (func, pStartPlaying)))
466  {
467  int length = strlen(pStartPlaying);
468  unsigned long long int handle = 0;
469 
470  memset (array, '\0', sizeof(array));
471  strncpy (array, (pInput+length), 30);
472  handle = strtoll(array, NULL, 0);
473 
474  startStreaming(handle);
475  }
476  else if (NULL != (pInput = strstr (func, pStopPlaying)))
477  {
478  stopStreaming();
479 
480  }
481  else if (NULL != strstr (func, "GetNumberOfAdapters"))
482  {
483  GetNumberOfAdapters();
484  }
485  else
486  {
487  printf ("<tr>" "<td> Seems new to me.. Let me handle this soon.. %s \n</td>" "</tr>" "</table> ", func);
488  }
489 
490  /* Close the HTML Page */
491  printf ("%s", pGoHome);
492  printf ("</body> " "</html> ");
493  }
494  }
495  else
496  {
497  fprintf (stderr, "how come it landed here!?!?!\n");
498  }
499  }
500  FCGI_Finish();
501  }
502 
503  return 0;
504 }
505 
506 
507 /* End of File */
BTRMGR_SetAdapterName
BTRMGR_Result_t BTRMGR_SetAdapterName(unsigned char aui8AdapterIdx, const char *pNameOfAdapter)
This API is used to set the new name to the bluetooth adapter.
Definition: btrMgr.c:2645
BTRMGR_UnpairDevice
BTRMGR_Result_t BTRMGR_UnpairDevice(unsigned char aui8AdapterIdx, BTRMgrDeviceHandle ahBTRMgrDevHdl)
This API is used to remove the pairing information of the device selected.
Definition: btrMgr.c:3440
BTRMGR_PairDevice
BTRMGR_Result_t BTRMGR_PairDevice(unsigned char aui8AdapterIdx, BTRMgrDeviceHandle ahBTRMgrDevHdl)
This API is used to pair the device that you wish to pair.
Definition: btrMgr.c:3313
BTRMGR_Result_t
enum _BTRMGR_Result_t BTRMGR_Result_t
Represents the status of the operation.
btmgr.h
BTRMGR_GetAdapterPowerStatus
BTRMGR_Result_t BTRMGR_GetAdapterPowerStatus(unsigned char aui8AdapterIdx, unsigned char *pPowerStatus)
This API fetches the power status, either 0 or 1.
Definition: btrMgr.c:2790
BTRMGR_StartAudioStreamingOut
BTRMGR_Result_t BTRMGR_StartAudioStreamingOut(unsigned char aui8AdapterIdx, BTRMgrDeviceHandle ahBTRMgrDevHdl, BTRMGR_DeviceOperationType_t connectAs)
This API initates the streaming from the device with the selected operation type.
Definition: btrMgr.c:4179
BTRMGR_StartDeviceDiscovery
BTRMGR_Result_t BTRMGR_StartDeviceDiscovery(unsigned char aui8AdapterIdx, BTRMGR_DeviceOperationType_t aenBTRMgrDevOpT)
This API initiates the scanning process.
Definition: btrMgr.c:3141
_BTRMGR_PairedDevicesList_t
Represents the list of paired devices.
Definition: btmgr.h:476
BTRMGR_GetNumberOfAdapters
BTRMGR_Result_t BTRMGR_GetNumberOfAdapters(unsigned char *pNumOfAdapters)
This API returns the number of bluetooth adapters available.
Definition: btrMgr.c:2550
BTRMGR_GetAdapterName
BTRMGR_Result_t BTRMGR_GetAdapterName(unsigned char aui8AdapterIdx, char *pNameOfAdapter)
This API fetches the bluetooth adapter name.
Definition: btrMgr.c:2685
BTRMGR_StopAudioStreamingOut
BTRMGR_Result_t BTRMGR_StopAudioStreamingOut(unsigned char aui8AdapterIdx, BTRMgrDeviceHandle ahBTRMgrDevHdl)
This API terminates the streaming from the device.
Definition: btrMgr.c:4205
_BTRMGR_DiscoveredDevicesList_t
Represents the list of scanned devices.
Definition: btmgr.h:484
BTRMGR_SetAdapterPowerStatus
BTRMGR_Result_t BTRMGR_SetAdapterPowerStatus(unsigned char aui8AdapterIdx, unsigned char power_status)
This API sets the bluetooth adapter power to ON/OFF.
Definition: btrMgr.c:2728
BTRMGR_ConnectToDevice
BTRMGR_Result_t BTRMGR_ConnectToDevice(unsigned char aui8AdapterIdx, BTRMgrDeviceHandle ahBTRMgrDevHdl, BTRMGR_DeviceOperationType_t connectAs)
This API connects the device as audio sink/headset/audio src based on the device type specified.
Definition: btrMgr.c:3635
BTRMGR_GetDiscoveredDevices
BTRMGR_Result_t BTRMGR_GetDiscoveredDevices(unsigned char aui8AdapterIdx, BTRMGR_DiscoveredDevicesList_t *pDiscoveredDevices)
This API fetches the list of devices scanned.
Definition: btrMgr.c:3226
BTRMGR_GetPairedDevices
BTRMGR_Result_t BTRMGR_GetPairedDevices(unsigned char aui8AdapterIdx, BTRMGR_PairedDevicesList_t *pPairedDevices)
This API returns the list of devices paired.
Definition: btrMgr.c:3556
BTRMGR_StopDeviceDiscovery
BTRMGR_Result_t BTRMGR_StopDeviceDiscovery(unsigned char aui8AdapterIdx, BTRMGR_DeviceOperationType_t aenBTRMgrDevOpT)
This API terminates the scanning process.
Definition: btrMgr.c:3169
BTRMGR_Init
BTRMGR_Result_t BTRMGR_Init(void)
This API initializes the bluetooth manager.
Definition: btrMgr.c:2295