Home | History | Annotate | Download | only in plugins
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel (at) holtmann.org>
      6  *
      7  *
      8  *  This program is free software; you can redistribute it and/or modify
      9  *  it under the terms of the GNU General Public License as published by
     10  *  the Free Software Foundation; either version 2 of the License, or
     11  *  (at your option) any later version.
     12  *
     13  *  This program is distributed in the hope that it will be useful,
     14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  *  GNU General Public License for more details.
     17  *
     18  *  You should have received a copy of the GNU General Public License
     19  *  along with this program; if not, write to the Free Software
     20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     21  *
     22  */
     23 
     24 #ifdef HAVE_CONFIG_H
     25 #include <config.h>
     26 #endif
     27 
     28 #include <errno.h>
     29 #include <unistd.h>
     30 #include <sys/socket.h>
     31 
     32 #include <bluetooth/bluetooth.h>
     33 #include <bluetooth/rfcomm.h>
     34 
     35 #include <glib.h>
     36 
     37 #include <gdbus.h>
     38 
     39 #include "plugin.h"
     40 #include "adapter.h"
     41 #include "log.h"
     42 
     43 static gboolean session_event(GIOChannel *chan,
     44 					GIOCondition cond, gpointer data)
     45 {
     46 	unsigned char buf[672];
     47 	gsize len, written;
     48 	GIOError err;
     49 
     50 	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
     51 		return FALSE;
     52 
     53 	err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
     54 	if (err == G_IO_ERROR_AGAIN)
     55 		return TRUE;
     56 
     57 	g_io_channel_write(chan, (const gchar *) buf, len, &written);
     58 
     59 	return TRUE;
     60 }
     61 
     62 static gboolean connect_event(GIOChannel *chan,
     63 					GIOCondition cond, gpointer data)
     64 {
     65 	GIOChannel *io;
     66 	struct sockaddr_rc addr;
     67 	socklen_t optlen;
     68 	char address[18];
     69 	int sk, nsk;
     70 
     71 	sk = g_io_channel_unix_get_fd(chan);
     72 
     73 	memset(&addr, 0, sizeof(addr));
     74 	optlen = sizeof(addr);
     75 
     76 	nsk = accept(sk, (struct sockaddr *) &addr, &optlen);
     77 	if (nsk < 0)
     78 		return TRUE;
     79 
     80 	io = g_io_channel_unix_new(nsk);
     81 	g_io_channel_set_close_on_unref(io, TRUE);
     82 
     83 	ba2str(&addr.rc_bdaddr, address);
     84 
     85 	g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
     86 							session_event, NULL);
     87 
     88 	return TRUE;
     89 }
     90 
     91 static GIOChannel *setup_rfcomm(uint8_t channel)
     92 {
     93 	GIOChannel *io;
     94 	struct sockaddr_rc addr;
     95 	int sk;
     96 
     97 	sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
     98 	if (sk < 0)
     99 		return NULL;
    100 
    101 	memset(&addr, 0, sizeof(addr));
    102 	addr.rc_family = AF_BLUETOOTH;
    103 	bacpy(&addr.rc_bdaddr, BDADDR_ANY);
    104 	addr.rc_channel = channel;
    105 
    106 	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    107 		close(sk);
    108 		return NULL;
    109 	}
    110 
    111 	if (listen(sk, 10) < 0) {
    112 		close(sk);
    113 		return NULL;
    114 	}
    115 
    116 	io = g_io_channel_unix_new(sk);
    117 	g_io_channel_set_close_on_unref(io, TRUE);
    118 
    119 	g_io_add_watch(io, G_IO_IN, connect_event, NULL);
    120 
    121 	return io;
    122 }
    123 
    124 static GIOChannel *chan = NULL;
    125 
    126 static int echo_probe(struct btd_adapter *adapter)
    127 {
    128 	const char *path = adapter_get_path(adapter);
    129 
    130 	DBG("path %s", path);
    131 
    132 	chan = setup_rfcomm(23);
    133 
    134 	return 0;
    135 }
    136 
    137 static void echo_remove(struct btd_adapter *adapter)
    138 {
    139 	const char *path = adapter_get_path(adapter);
    140 
    141 	DBG("path %s", path);
    142 
    143 	g_io_channel_unref(chan);
    144 }
    145 
    146 static struct btd_adapter_driver echo_server = {
    147 	.name	= "echo-server",
    148 	.probe	= echo_probe,
    149 	.remove	= echo_remove,
    150 };
    151 
    152 static int echo_init(void)
    153 {
    154 	DBG("Setup echo plugin");
    155 
    156 	return btd_register_adapter_driver(&echo_server);
    157 }
    158 
    159 static void echo_exit(void)
    160 {
    161 	DBG("Cleanup echo plugin");
    162 
    163 	btd_unregister_adapter_driver(&echo_server);
    164 }
    165 
    166 BLUETOOTH_PLUGIN_DEFINE(echo, VERSION,
    167 		BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, echo_init, echo_exit)
    168