1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@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 <ctype.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <termios.h>
36 #include <unistd.h>
37 #include <arpa/inet.h>
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/un.h>
43
44 #include <bluetooth/bluetooth.h>
45 #include <bluetooth/hci.h>
46 #include <bluetooth/hci_lib.h>
47 #include <bluetooth/sdp.h>
48 #include <bluetooth/sdp_lib.h>
49 #include <bluetooth/rfcomm.h>
50
51 #include <glib.h>
52 #include <gdbus.h>
53
54 #include "../src/dbus-common.h"
55 #include "adapter.h"
56 #include "device.h"
57
58 #include "logging.h"
59 #include "textfile.h"
60
61 #include "error.h"
62 #include "port.h"
63 #include "proxy.h"
64 #include "storage.h"
65 #include "manager.h"
66 #include "sdpd.h"
67 #include "glib-helper.h"
68
69 #define SERIAL_PORT_UUID "00001101-0000-1000-8000-00805F9B34FB"
70 #define DIALUP_NET_UUID "00001103-0000-1000-8000-00805F9B34FB"
71 #define OBJECT_PUSH_UUID "00001105-0000-1000-8000-00805F9B34FB"
72 #define FILE_TRANSFER_UUID "00001106-0000-1000-8000-00805F9B34FB"
73 #define RFCOMM_UUID_STR "00000003-0000-1000-8000-00805F9B34FB"
74
75 static DBusConnection *connection = NULL;
76 GSList *adapters = NULL;
77
serial_probe(struct btd_device * device,const char * uuid)78 static int serial_probe(struct btd_device *device, const char *uuid)
79 {
80 struct btd_adapter *adapter = device_get_adapter(device);
81 const gchar *path = device_get_path(device);
82 sdp_list_t *protos;
83 int ch;
84 bdaddr_t src, dst;
85 const sdp_record_t *rec;
86
87 DBG("path %s: %s", path, uuid);
88
89 rec = btd_device_get_record(device, uuid);
90 if (!rec)
91 return -EINVAL;
92
93 if (sdp_get_access_protos(rec, &protos) < 0)
94 return -EINVAL;
95
96 ch = sdp_get_proto_port(protos, RFCOMM_UUID);
97 sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
98 sdp_list_free(protos, NULL);
99
100 if (ch < 1 || ch > 30) {
101 error("Channel out of range: %d", ch);
102 return -EINVAL;
103 }
104
105 adapter_get_address(adapter, &src);
106 device_get_address(device, &dst);
107
108 return port_register(connection, path, &src, &dst, uuid, ch);
109 }
110
serial_remove(struct btd_device * device)111 static void serial_remove(struct btd_device *device)
112 {
113 const gchar *path = device_get_path(device);
114
115 DBG("path %s", path);
116
117 port_unregister(path);
118 }
119
120
port_probe(struct btd_device * device,GSList * uuids)121 static int port_probe(struct btd_device *device, GSList *uuids)
122 {
123 while (uuids) {
124 serial_probe(device, uuids->data);
125 uuids = uuids->next;
126 }
127
128 return 0;
129 }
130
port_remove(struct btd_device * device)131 static void port_remove(struct btd_device *device)
132 {
133 return serial_remove(device);
134 }
135
136 static struct btd_device_driver serial_port_driver = {
137 .name = "serial-port",
138 .uuids = BTD_UUIDS(RFCOMM_UUID_STR),
139 .probe = port_probe,
140 .remove = port_remove,
141 };
142
proxy_probe(struct btd_adapter * adapter)143 static int proxy_probe(struct btd_adapter *adapter)
144 {
145 const char *path = adapter_get_path(adapter);
146
147 DBG("path %s", path);
148
149 return proxy_register(connection, adapter);
150 }
151
proxy_remove(struct btd_adapter * adapter)152 static void proxy_remove(struct btd_adapter *adapter)
153 {
154 const char *path = adapter_get_path(adapter);
155
156 DBG("path %s", path);
157
158 proxy_unregister(adapter);
159 }
160
161 static struct btd_adapter_driver serial_proxy_driver = {
162 .name = "serial-proxy",
163 .probe = proxy_probe,
164 .remove = proxy_remove,
165 };
166
serial_manager_init(DBusConnection * conn)167 int serial_manager_init(DBusConnection *conn)
168 {
169 connection = dbus_connection_ref(conn);
170
171 btd_register_adapter_driver(&serial_proxy_driver);
172 btd_register_device_driver(&serial_port_driver);
173
174 return 0;
175 }
176
serial_manager_exit(void)177 void serial_manager_exit(void)
178 {
179 btd_unregister_device_driver(&serial_port_driver);
180
181 dbus_connection_unref(connection);
182 connection = NULL;
183 }
184