RetroArch/wifi/drivers/connmanctl.c

134 lines
3.0 KiB
C
Raw Normal View History

2016-09-21 16:06:14 +02:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2015 - Michael Lelli
*
* 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/>.
*/
2016-09-23 01:38:19 +02:00
#include <compat/strl.h>
#include <file/file_path.h>
2016-09-21 16:06:14 +02:00
#include "../wifi_driver.h"
2016-09-22 16:44:51 +02:00
#include "../../runloop.h"
2016-09-21 16:06:14 +02:00
static struct string_list* lines;
2016-09-23 01:38:19 +02:00
static void *connmanctl_init(void)
2016-09-21 16:06:14 +02:00
{
return (void*)-1;
}
static void connmanctl_free(void *data)
{
(void)data;
}
static bool connmanctl_start(void *data)
{
(void)data;
return true;
}
static void connmanctl_stop(void *data)
{
(void)data;
}
2016-09-23 01:38:19 +02:00
static void connmanctl_scan(void)
2016-09-21 19:46:02 +02:00
{
2016-09-23 01:38:19 +02:00
char line[512];
2016-09-21 19:46:02 +02:00
union string_list_elem_attr attr;
2016-09-23 01:38:19 +02:00
FILE *serv_file = NULL;
2016-09-21 19:46:02 +02:00
attr.i = RARCH_FILETYPE_UNSET;
if (lines)
free(lines);
lines = string_list_new();
2016-09-21 19:46:02 +02:00
pclose(popen("connmanctl scan wifi", "r"));
2016-09-22 00:03:01 +02:00
2016-09-23 01:38:19 +02:00
serv_file = popen("connmanctl services", "r");
while (fgets (line, 512, serv_file) != NULL)
{
2016-09-22 16:44:51 +02:00
size_t len = strlen(line);
2016-09-23 01:38:19 +02:00
if (len > 0 && line[len-1] == '\n')
2016-09-22 16:44:51 +02:00
line[--len] = '\0';
string_list_append(lines, line, attr);
}
pclose(serv_file);
}
static void connmanctl_get_ssids(struct string_list* ssids)
{
unsigned i;
union string_list_elem_attr attr;
attr.i = RARCH_FILETYPE_UNSET;
for (i = 0; i < lines->size; i++)
{
char ssid[20];
2016-09-23 01:38:19 +02:00
const char *line = lines->elems[i].data;
strlcpy(ssid, line+4, sizeof(ssid));
string_list_append(ssids, ssid, attr);
}
}
static bool connmanctl_ssid_is_online(unsigned i)
{
const char *line = lines->elems[i].data;
2016-09-23 01:38:19 +02:00
if (!line)
return false;
return line[2] == 'O';
2016-09-21 19:46:02 +02:00
}
2016-09-23 13:16:27 +02:00
static bool connmanctl_connect_ssid(unsigned i, const char* passphrase)
2016-09-22 16:44:51 +02:00
{
2016-09-23 01:38:19 +02:00
char ln[512];
2016-09-22 16:44:51 +02:00
char service[128];
char command[256];
2016-09-23 01:38:19 +02:00
FILE *file = NULL;
const char *line = lines->elems[i].data;
2016-09-22 16:44:51 +02:00
strlcpy(service, line+25, sizeof(service));
2016-09-23 13:16:27 +02:00
command[0] = '\0';
2016-09-22 16:44:51 +02:00
strlcat(command, "connmanctl connect ", sizeof(command));
strlcat(command, service, sizeof(command));
strlcat(command, " 2>&1", sizeof(command));
printf("%s\n", command);
2016-09-23 01:38:19 +02:00
file = popen(command, "r");
2016-09-22 16:44:51 +02:00
while (fgets (ln, 512, file) != NULL)
{
printf("%s\n", ln);
runloop_msg_queue_push(ln, 1, 180, true);
}
pclose(file);
return true;
}
2016-09-21 16:06:14 +02:00
wifi_driver_t wifi_connmanctl = {
connmanctl_init,
connmanctl_free,
connmanctl_start,
connmanctl_stop,
2016-09-21 19:46:02 +02:00
connmanctl_scan,
connmanctl_get_ssids,
connmanctl_ssid_is_online,
2016-09-22 16:44:51 +02:00
connmanctl_connect_ssid,
2016-09-21 16:06:14 +02:00
"connmanctl",
};