RetroArch/bluetooth/drivers/bluetoothctl.c

221 lines
5.3 KiB
C
Raw Normal View History

2020-06-17 14:56:44 +03:00
/* RetroArch - A frontend for libretro.
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <compat/strl.h>
#include <configuration.h>
#include "../bluetooth_driver.h"
#include "../../retroarch.h"
2020-06-28 19:40:37 +02:00
/* TODO/FIXME - static globals - should go into userdata
* struct for driver */
static bool bluetoothctl_cache[256] = {0};
static unsigned bluetoothctl_counter[256] = {0};
static struct string_list* lines = NULL;
static char command[256] = {0};
2020-06-17 14:56:44 +03:00
static void *bluetoothctl_init(void)
{
2020-06-28 19:40:37 +02:00
return (void*)-1;
2020-06-17 14:56:44 +03:00
}
static void bluetoothctl_free(void *data)
{
2020-06-28 19:40:37 +02:00
(void)data;
}
static bool bluetoothctl_start(void *data)
{
(void)data;
return true;
}
static void bluetoothctl_stop(void *data)
{
(void)data;
2020-06-17 14:56:44 +03:00
}
2020-06-28 19:40:37 +02:00
static void bluetoothctl_scan(void)
2020-06-17 14:56:44 +03:00
{
char line[512];
union string_list_elem_attr attr;
FILE *dev_file = NULL;
attr.i = 0;
2020-06-28 19:40:37 +02:00
if (lines)
free(lines);
lines = string_list_new();
2020-06-17 14:56:44 +03:00
pclose(popen("bluetoothctl -- power on", "r"));
2020-06-28 19:40:37 +02:00
pclose(popen("bluetoothctl --timeout 15 scan on", "r"));
2020-06-17 14:56:44 +03:00
runloop_msg_queue_push(msg_hash_to_str(MSG_BLUETOOTH_SCAN_COMPLETE),
1, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT,
MESSAGE_QUEUE_CATEGORY_INFO);
dev_file = popen("bluetoothctl -- devices", "r");
2020-06-17 14:56:44 +03:00
while (fgets(line, 512, dev_file))
{
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n')
line[--len] = '\0';
2020-06-28 19:40:37 +02:00
string_list_append(lines, line, attr);
2020-06-17 14:56:44 +03:00
}
2020-06-17 14:56:44 +03:00
pclose(dev_file);
}
2020-06-28 19:40:37 +02:00
static void bluetoothctl_get_devices(struct string_list* devices)
2020-06-17 14:56:44 +03:00
{
unsigned i;
union string_list_elem_attr attr;
2020-06-17 14:56:44 +03:00
attr.i = 0;
2020-06-28 19:40:37 +02:00
if (!lines)
2020-06-17 14:56:44 +03:00
return;
2020-06-28 19:40:37 +02:00
for (i = 0; i < lines->size; i++)
2020-06-17 14:56:44 +03:00
{
char device[64];
2020-06-28 19:40:37 +02:00
const char *line = lines->elems[i].data;
2020-06-17 14:56:44 +03:00
/* bluetoothctl devices outputs lines of the format:
* $ bluetoothctl devices
* 'Device (mac address) (device name)'
*/
strlcpy(device, line+24, sizeof(device));
2020-06-17 14:56:44 +03:00
string_list_append(devices, device, attr);
}
}
2020-06-28 19:40:37 +02:00
static bool bluetoothctl_device_is_connected(unsigned i)
2020-06-17 14:56:44 +03:00
{
char ln[512] = {0};
char device[18] = {0};
2020-06-28 19:40:37 +02:00
const char *line = lines->elems[i].data;
2020-06-17 14:56:44 +03:00
FILE *command_file = NULL;
2020-06-28 19:40:37 +02:00
if (bluetoothctl_counter[i] == 60)
2020-06-17 14:56:44 +03:00
{
static struct string_list* list = NULL;
2020-06-28 19:40:37 +02:00
bluetoothctl_counter[i] = 0;
2020-06-17 14:56:44 +03:00
list = string_split(line, " ");
if (!list)
return false;
if (list->size == 0)
{
string_list_free(list);
return false;
}
strlcpy(device, list->elems[1].data, sizeof(device));
string_list_free(list);
2020-06-28 19:40:37 +02:00
snprintf(command, sizeof(command), "\
2020-06-17 14:56:44 +03:00
bluetoothctl -- info %s | grep 'Connected: yes'",
device);
2020-06-28 19:40:37 +02:00
command_file = popen(command, "r");
2020-06-17 14:56:44 +03:00
while (fgets(ln, 512, command_file))
{
2020-06-28 19:40:37 +02:00
bluetoothctl_cache[i] = true;
2020-06-17 14:56:44 +03:00
return true;
}
pclose(command_file);
2020-06-28 19:40:37 +02:00
bluetoothctl_cache[i] = false;
2020-06-17 14:56:44 +03:00
}
else
{
2020-06-28 19:40:37 +02:00
bluetoothctl_counter[i]++;
return bluetoothctl_cache[i];
2020-06-17 14:56:44 +03:00
}
return false;
}
2020-06-28 19:40:37 +02:00
static bool bluetoothctl_connect_device(unsigned idx)
2020-06-17 14:56:44 +03:00
{
unsigned i;
char device[18] = {0};
2020-06-28 19:40:37 +02:00
const char *line = lines->elems[idx].data;
2020-06-17 14:56:44 +03:00
static struct string_list* list = NULL;
2020-06-17 14:56:44 +03:00
/* bluetoothctl devices outputs lines of the format:
* $ bluetoothctl devices
* 'Device (mac address) (device name)'
*/
list = string_split(line, " ");
if (!list)
return false;
if (list->size == 0)
{
string_list_free(list);
return false;
}
strlcpy(device, list->elems[1].data, sizeof(device));
string_list_free(list);
2020-06-28 19:40:37 +02:00
snprintf(command, sizeof(command), "\
2020-06-17 14:56:44 +03:00
bluetoothctl -- trust %s",
device);
2020-06-28 19:40:37 +02:00
pclose(popen(command, "r"));
2020-06-17 14:56:44 +03:00
2020-06-28 19:40:37 +02:00
snprintf(command, sizeof(command), "\
2020-06-17 14:56:44 +03:00
bluetoothctl -- pair %s",
device);
2020-06-28 19:40:37 +02:00
pclose(popen(command, "r"));
2020-06-17 14:56:44 +03:00
2020-06-28 19:40:37 +02:00
snprintf(command, sizeof(command), "\
2020-06-17 14:56:44 +03:00
bluetoothctl -- connect %s",
device);
2020-06-28 19:40:37 +02:00
pclose(popen(command, "r"));
2020-06-17 14:56:44 +03:00
2020-06-28 19:40:37 +02:00
bluetoothctl_counter[idx] = 0;
2020-06-17 14:56:44 +03:00
return true;
}
2020-06-28 19:40:37 +02:00
void bluetoothctl_device_get_sublabel (char *s, unsigned i, size_t len)
{
/* bluetoothctl devices outputs lines of the format:
* $ bluetoothctl devices
* 'Device (mac address) (device name)'
*/
2020-06-28 19:40:37 +02:00
const char *line = lines->elems[i].data;
strlcpy(s, line+7, 18);
}
2020-06-17 14:56:44 +03:00
bluetooth_driver_t bluetooth_bluetoothctl = {
bluetoothctl_init,
bluetoothctl_free,
2020-06-28 19:40:37 +02:00
bluetoothctl_start,
bluetoothctl_stop,
2020-06-17 14:56:44 +03:00
bluetoothctl_scan,
bluetoothctl_get_devices,
bluetoothctl_device_is_connected,
bluetoothctl_device_get_sublabel,
2020-06-17 14:56:44 +03:00
bluetoothctl_connect_device,
"bluetoothctl",
};