2009-05-28 11:39:20 +02:00
|
|
|
/*
|
|
|
|
* rfkill userspace tool
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2009-06-04 20:28:39 +02:00
|
|
|
#include <errno.h>
|
2009-05-28 11:39:20 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2009-06-04 20:28:39 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-07-23 10:53:47 -06:00
|
|
|
#include <ctype.h>
|
2009-07-23 19:11:21 +02:00
|
|
|
#include <stdbool.h>
|
2009-06-04 20:28:39 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/poll.h>
|
2009-12-03 14:28:03 +01:00
|
|
|
#include <sys/time.h>
|
2009-05-28 11:39:20 +02:00
|
|
|
|
|
|
|
#include "rfkill.h"
|
|
|
|
#include "core.h"
|
|
|
|
|
2009-06-04 20:28:39 +02:00
|
|
|
static void rfkill_event(void)
|
|
|
|
{
|
2009-07-05 14:33:25 +02:00
|
|
|
struct rfkill_event event;
|
2009-12-03 14:28:03 +01:00
|
|
|
struct timeval tv;
|
2009-06-04 20:28:39 +02:00
|
|
|
struct pollfd p;
|
|
|
|
ssize_t len;
|
|
|
|
int fd, n;
|
|
|
|
|
2009-06-05 12:31:10 +02:00
|
|
|
fd = open("/dev/rfkill", O_RDONLY);
|
2009-06-04 20:28:39 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("Can't open RFKILL control device");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p.fd = fd;
|
|
|
|
p.events = POLLIN | POLLHUP;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = poll(&p, 1, -1);
|
|
|
|
if (n < 0) {
|
|
|
|
perror("Failed to poll RFKILL control device");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-07-05 14:33:25 +02:00
|
|
|
len = read(fd, &event, sizeof(event));
|
2009-06-04 20:28:39 +02:00
|
|
|
if (len < 0) {
|
|
|
|
perror("Reading of RFKILL events failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-05 14:37:40 +02:00
|
|
|
if (len != RFKILL_EVENT_SIZE_V1) {
|
2009-06-04 20:28:39 +02:00
|
|
|
fprintf(stderr, "Wrong size of RFKILL event\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-03 14:28:03 +01:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
printf("%ld.%06u: idx %u type %u op %u soft %u hard %u\n",
|
|
|
|
(long) tv.tv_sec, (unsigned int) tv.tv_usec,
|
|
|
|
event.idx, event.type, event.op, event.soft, event.hard);
|
2009-10-29 13:57:27 +01:00
|
|
|
fflush(stdout);
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *get_name(__u32 idx)
|
|
|
|
{
|
|
|
|
static char name[128];
|
|
|
|
ssize_t len;
|
|
|
|
char *pos, filename[64];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename) - 1,
|
|
|
|
"/sys/class/rfkill/rfkill%u/name", idx);
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(name, 0, sizeof(name));
|
|
|
|
len = read(fd, name, sizeof(name) - 1);
|
|
|
|
|
|
|
|
pos = strchr(name, '\n');
|
|
|
|
if (pos)
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *type2string(enum rfkill_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case RFKILL_TYPE_ALL:
|
|
|
|
return "All";
|
|
|
|
case RFKILL_TYPE_WLAN:
|
|
|
|
return "Wireless LAN";
|
|
|
|
case RFKILL_TYPE_BLUETOOTH:
|
|
|
|
return "Bluetooth";
|
|
|
|
case RFKILL_TYPE_UWB:
|
|
|
|
return "Ultra-Wideband";
|
|
|
|
case RFKILL_TYPE_WIMAX:
|
|
|
|
return "WiMAX";
|
|
|
|
case RFKILL_TYPE_WWAN:
|
|
|
|
return "Wireless WAN";
|
2009-08-25 11:52:29 +02:00
|
|
|
case RFKILL_TYPE_GPS:
|
|
|
|
return "GPS";
|
2010-01-19 09:05:21 +02:00
|
|
|
case RFKILL_TYPE_FM:
|
|
|
|
return "FM";
|
2009-06-04 20:28:39 +02:00
|
|
|
case NUM_RFKILL_TYPES:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:35:29 +00:00
|
|
|
struct rfkill_type_str {
|
|
|
|
enum rfkill_type type;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
static const struct rfkill_type_str rfkill_type_strings[] = {
|
|
|
|
{ .type = RFKILL_TYPE_ALL, .name = "all" },
|
|
|
|
{ .type = RFKILL_TYPE_WLAN, .name = "wifi" },
|
|
|
|
{ .type = RFKILL_TYPE_WLAN, .name = "wlan" }, /* alias */
|
|
|
|
{ .type = RFKILL_TYPE_BLUETOOTH, .name = "bluetooth" },
|
|
|
|
{ .type = RFKILL_TYPE_UWB, .name = "uwb" },
|
|
|
|
{ .type = RFKILL_TYPE_UWB, .name = "ultrawideband" }, /* alias */
|
|
|
|
{ .type = RFKILL_TYPE_WIMAX, .name = "wimax" },
|
|
|
|
{ .type = RFKILL_TYPE_WWAN, .name = "wwan" },
|
|
|
|
{ .type = RFKILL_TYPE_GPS, .name = "gps" },
|
|
|
|
{ .type = RFKILL_TYPE_FM, .name = "fm" },
|
|
|
|
{ .name = NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rfkill_id {
|
|
|
|
union {
|
|
|
|
enum rfkill_type type;
|
|
|
|
__u32 index;
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
RFKILL_IS_INVALID,
|
|
|
|
RFKILL_IS_TYPE,
|
|
|
|
RFKILL_IS_INDEX,
|
|
|
|
} result;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct rfkill_id rfkill_id_to_type(const char *s)
|
|
|
|
{
|
|
|
|
const struct rfkill_type_str *p;
|
|
|
|
struct rfkill_id ret;
|
|
|
|
|
|
|
|
if (islower(*s)) {
|
|
|
|
for (p = rfkill_type_strings; p->name != NULL; p++) {
|
|
|
|
if ((strlen(s) == strlen(p->name)) && (!strcmp(s,p->name))) {
|
|
|
|
ret.type = p->type;
|
|
|
|
ret.result = RFKILL_IS_TYPE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isdigit(*s)) {
|
|
|
|
/* assume a numeric character implies an index. */
|
|
|
|
ret.index = atoi(s);
|
|
|
|
ret.result = RFKILL_IS_INDEX;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.result = RFKILL_IS_INVALID;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:35:30 +00:00
|
|
|
static int rfkill_list(const char *param)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2010-01-22 23:35:30 +00:00
|
|
|
struct rfkill_id id = { .result = RFKILL_IS_INVALID };
|
2009-07-05 14:33:25 +02:00
|
|
|
struct rfkill_event event;
|
2009-07-23 19:11:21 +02:00
|
|
|
const char *name;
|
2009-06-04 20:28:39 +02:00
|
|
|
ssize_t len;
|
|
|
|
int fd;
|
|
|
|
|
2010-01-22 23:35:30 +00:00
|
|
|
if (param) {
|
|
|
|
id = rfkill_id_to_type(param);
|
|
|
|
if (id.result == RFKILL_IS_INVALID) {
|
|
|
|
fprintf(stderr, "Bogus %s argument '%s'.\n", "list", param);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
/* don't filter "all" */
|
|
|
|
if (id.result == RFKILL_IS_TYPE && id.type == RFKILL_TYPE_ALL)
|
|
|
|
id.result = RFKILL_IS_INVALID;
|
|
|
|
}
|
|
|
|
|
2009-06-05 12:31:10 +02:00
|
|
|
fd = open("/dev/rfkill", O_RDONLY);
|
2009-06-04 20:28:39 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("Can't open RFKILL control device");
|
2010-01-22 23:35:28 +00:00
|
|
|
return 1;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
|
|
|
|
perror("Can't set RFKILL control device to non-blocking");
|
|
|
|
close(fd);
|
2010-01-22 23:35:28 +00:00
|
|
|
return 1;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2009-07-05 14:33:25 +02:00
|
|
|
len = read(fd, &event, sizeof(event));
|
2009-06-04 20:28:39 +02:00
|
|
|
if (len < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
perror("Reading of RFKILL events failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-05 14:37:40 +02:00
|
|
|
if (len != RFKILL_EVENT_SIZE_V1) {
|
2009-06-04 20:28:39 +02:00
|
|
|
fprintf(stderr, "Wrong size of RFKILL event\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-05 14:33:25 +02:00
|
|
|
if (event.op != RFKILL_OP_ADD)
|
2009-06-04 20:28:39 +02:00
|
|
|
continue;
|
|
|
|
|
2010-01-22 23:35:30 +00:00
|
|
|
/* filter out unwanted results */
|
|
|
|
switch (id.result)
|
|
|
|
{
|
|
|
|
case RFKILL_IS_TYPE:
|
|
|
|
if (event.type != id.type)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case RFKILL_IS_INDEX:
|
|
|
|
if (event.idx != id.index)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case RFKILL_IS_INVALID:; /* must be last */
|
|
|
|
}
|
|
|
|
|
2009-07-23 19:11:21 +02:00
|
|
|
name = get_name(event.idx);
|
2009-06-04 20:28:39 +02:00
|
|
|
|
2009-07-23 19:11:21 +02:00
|
|
|
printf("%u: %s: %s\n", event.idx, name,
|
|
|
|
type2string(event.type));
|
|
|
|
printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
|
|
|
|
printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2010-01-22 23:35:28 +00:00
|
|
|
return 0;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2010-01-22 23:35:29 +00:00
|
|
|
static int rfkill_block(__u8 block, const char *param)
|
2009-06-04 20:28:39 +02:00
|
|
|
{
|
2010-01-22 23:35:29 +00:00
|
|
|
struct rfkill_id id;
|
2009-06-04 20:28:39 +02:00
|
|
|
struct rfkill_event event;
|
|
|
|
ssize_t len;
|
|
|
|
int fd;
|
|
|
|
|
2010-01-22 23:35:29 +00:00
|
|
|
id = rfkill_id_to_type(param);
|
|
|
|
if (id.result == RFKILL_IS_INVALID) {
|
|
|
|
fprintf(stderr, "Bogus %s argument '%s'.\n", block ? "block" : "unblock", param);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2009-06-04 20:28:39 +02:00
|
|
|
fd = open("/dev/rfkill", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("Can't open RFKILL control device");
|
2010-01-22 23:35:28 +00:00
|
|
|
return 1;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&event, 0, sizeof(event));
|
2010-01-22 23:35:29 +00:00
|
|
|
switch (id.result) {
|
|
|
|
case RFKILL_IS_TYPE:
|
2009-07-23 19:11:21 +02:00
|
|
|
event.op = RFKILL_OP_CHANGE_ALL;
|
2010-01-22 23:35:29 +00:00
|
|
|
event.type = id.type;
|
|
|
|
break;
|
|
|
|
case RFKILL_IS_INDEX:
|
|
|
|
event.op = RFKILL_OP_CHANGE;
|
|
|
|
event.idx = id.index;
|
|
|
|
break;
|
|
|
|
case RFKILL_IS_INVALID:; /* must be last */
|
2009-07-23 19:11:21 +02:00
|
|
|
}
|
2009-06-04 20:28:39 +02:00
|
|
|
event.soft = block;
|
|
|
|
|
|
|
|
len = write(fd, &event, sizeof(event));
|
|
|
|
if (len < 0)
|
|
|
|
perror("Failed to change RFKILL state");
|
|
|
|
|
|
|
|
close(fd);
|
2010-01-22 23:35:28 +00:00
|
|
|
return 0;
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *argv0;
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
|
|
|
|
fprintf(stderr, "Options:\n");
|
|
|
|
fprintf(stderr, "\t--version\tshow version (%s)\n", rfkill_version);
|
|
|
|
fprintf(stderr, "Commands:\n");
|
|
|
|
fprintf(stderr, "\thelp\n");
|
|
|
|
fprintf(stderr, "\tevent\n");
|
2010-01-22 23:35:30 +00:00
|
|
|
fprintf(stderr, "\tlist [IDENTIFIER]\n");
|
2010-01-22 23:35:26 +00:00
|
|
|
fprintf(stderr, "\tblock IDENTIFIER\n");
|
|
|
|
fprintf(stderr, "\tunblock IDENTIFIER\n");
|
|
|
|
fprintf(stderr, "where IDENTIFIER is the index no. of an rfkill switch or one of:\n");
|
|
|
|
fprintf(stderr, "\t<idx> all wifi wlan bluetooth uwb ultrawideband wimax wwan gps\n");
|
2009-06-04 20:28:39 +02:00
|
|
|
}
|
|
|
|
|
2009-05-28 11:39:20 +02:00
|
|
|
static void version(void)
|
|
|
|
{
|
|
|
|
printf("rfkill %s\n", rfkill_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-01-22 23:35:28 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2009-06-04 20:28:39 +02:00
|
|
|
/* strip off self */
|
|
|
|
argc--;
|
|
|
|
argv0 = *argv++;
|
|
|
|
|
2009-05-28 11:39:20 +02:00
|
|
|
if (argc > 0 && strcmp(*argv, "--version") == 0) {
|
|
|
|
version();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-04 20:28:39 +02:00
|
|
|
if (argc == 0 || strcmp(*argv, "help") == 0) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(*argv, "event") == 0) {
|
|
|
|
rfkill_event();
|
|
|
|
} else if (strcmp(*argv, "list") == 0) {
|
2010-01-22 23:35:30 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
rfkill_list(*argv); /* NULL is acceptable */
|
2009-06-04 20:28:39 +02:00
|
|
|
} else if (strcmp(*argv, "block") == 0 && argc > 1) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2010-01-22 23:35:29 +00:00
|
|
|
ret = rfkill_block(1,*argv);
|
2009-06-04 20:28:39 +02:00
|
|
|
} else if (strcmp(*argv, "unblock") == 0 && argc > 1) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2010-01-22 23:35:29 +00:00
|
|
|
ret = rfkill_block(0,*argv);
|
2009-06-04 20:28:39 +02:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:35:28 +00:00
|
|
|
return ret;
|
2009-05-28 11:39:20 +02:00
|
|
|
}
|