summaryrefslogtreecommitdiffstats
path: root/binding/bluetooth-rfkill.c
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2018-10-30 19:41:53 -0700
committerMatt Ranostay <matt.ranostay@konsulko.com>2018-11-23 08:49:14 -0800
commitfb433aa5407795e864c2ff638d9e3d85fb6c297b (patch)
tree1224fed6864778137b12f51e9af9a7f7660695bd /binding/bluetooth-rfkill.c
parentbc1fe8673e46f073e31bed137e532e380fcfd85f (diff)
binding: bluetooth: initial commit of binding rewrite
Bug-AGL: SPEC-1630 Change-Id: I33cfec70283fa29f47b76f9e6be3e8e6cd6a2f54 Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding/bluetooth-rfkill.c')
-rw-r--r--binding/bluetooth-rfkill.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/binding/bluetooth-rfkill.c b/binding/bluetooth-rfkill.c
new file mode 100644
index 0000000..5419e8f
--- /dev/null
+++ b/binding/bluetooth-rfkill.c
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2018 Konsulko Group
+ * Author: Matt Ranostay <matt.ranostay@konsulko.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include <linux/rfkill.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gio/gio.h>
+
+#define AFB_BINDING_VERSION 3
+#include <afb/afb-binding.h>
+
+#define HCIDEVUP _IOW('H', 201, int)
+#define BTPROTO_HCI 1
+
+
+
+static int hci_interface_enable(int hdev)
+{
+ int ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+ int ret;
+
+ if (ctl < 0)
+ return ctl;
+
+ ret = ioctl(ctl, HCIDEVUP, hdev);
+
+ close(ctl);
+
+ return ret;
+}
+
+static gboolean bluetooth_rfkill_event(GIOChannel *chan, GIOCondition cond, gpointer ptr)
+{
+ struct rfkill_event event;
+ gchar *name, buf[8];
+ ssize_t len;
+ int fd;
+
+ fd = g_io_channel_unix_get_fd(chan);
+ len = read(fd, &event, sizeof(struct rfkill_event));
+
+ if (len != sizeof(struct rfkill_event))
+ return TRUE;
+
+ if (event.type != RFKILL_TYPE_BLUETOOTH)
+ return TRUE;
+
+ if (event.op == RFKILL_OP_DEL)
+ return TRUE;
+
+ name = g_strdup_printf("/sys/class/rfkill/rfkill%u/soft", event.idx);
+
+ fd = g_open(name, O_WRONLY);
+ len = write(fd, "0", 1);
+ g_close(fd, NULL);
+
+ g_free(name);
+
+ memset(&buf, 0, sizeof(buf));
+
+ name = g_strdup_printf("/sys/class/rfkill/rfkill%u/name", event.idx);
+ fd = g_open(name, O_RDONLY);
+ len = read(fd, &buf, sizeof(buf) - 1);
+
+ if (len > 0)
+ {
+ int idx = 0;
+ sscanf(buf, "hci%d", &idx);
+
+ /*
+ * 50 millisecond delay to allow time for rfkill to unblock before
+ * attempting to bring up HCI interface
+ */
+ g_usleep(50000);
+
+ hci_interface_enable(idx);
+ }
+
+ g_free(name);
+
+ return TRUE;
+}
+
+int bluetooth_monitor_init(void)
+{
+ int fd = g_open("/dev/rfkill", O_RDWR);
+ GIOChannel *chan = NULL;
+ int ret = -EINVAL;
+
+ if (fd < 0)
+ {
+ AFB_ERROR("Cannot open /dev/rfkill");
+ return ret;
+ }
+
+ chan = g_io_channel_unix_new(fd);
+ g_io_channel_set_close_on_unref(chan, TRUE);
+
+ ret = g_io_add_watch(chan, G_IO_IN, bluetooth_rfkill_event, NULL);
+
+ g_io_channel_unref(chan);
+
+ return 0;
+}