Tizen 2.0 Release
[framework/connectivity/bluez.git] / serial / manager.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  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/sdp.h>
46 #include <bluetooth/sdp_lib.h>
47 #include <bluetooth/rfcomm.h>
48 #include <bluetooth/uuid.h>
49
50 #include <glib.h>
51 #include <gdbus.h>
52
53 #include "../src/dbus-common.h"
54 #include "adapter.h"
55 #include "device.h"
56
57 #include "log.h"
58
59 #include "error.h"
60 #include "port.h"
61 #include "proxy.h"
62 #include "storage.h"
63 #include "manager.h"
64 #include "sdpd.h"
65 #include "glib-helper.h"
66
67 static DBusConnection *connection = NULL;
68
69 static int serial_probe(struct btd_device *device, const char *uuid)
70 {
71         struct btd_adapter *adapter = device_get_adapter(device);
72         const gchar *path = device_get_path(device);
73         sdp_list_t *protos;
74         int ch;
75         bdaddr_t src, dst;
76         const sdp_record_t *rec;
77
78         DBG("path %s: %s", path, uuid);
79
80         rec = btd_device_get_record(device, uuid);
81         if (!rec)
82                 return -EINVAL;
83
84         if (sdp_get_access_protos(rec, &protos) < 0)
85                 return -EINVAL;
86
87         ch = sdp_get_proto_port(protos, RFCOMM_UUID);
88         sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
89         sdp_list_free(protos, NULL);
90
91         if (ch < 1 || ch > 30) {
92                 error("Channel out of range: %d", ch);
93                 return -EINVAL;
94         }
95
96         adapter_get_address(adapter, &src);
97         device_get_address(device, &dst, NULL);
98
99         return port_register(connection, path, &src, &dst, uuid, ch);
100 }
101
102 static void serial_remove(struct btd_device *device)
103 {
104         const gchar *path = device_get_path(device);
105
106         DBG("path %s", path);
107
108         port_unregister(path);
109 }
110
111
112 static int port_probe(struct btd_device *device, GSList *uuids)
113 {
114         while (uuids) {
115                 serial_probe(device, uuids->data);
116                 uuids = uuids->next;
117         }
118
119         return 0;
120 }
121
122 static void port_remove(struct btd_device *device)
123 {
124         return serial_remove(device);
125 }
126
127 static struct btd_device_driver serial_port_driver = {
128         .name   = "serial-port",
129         .uuids  = BTD_UUIDS(RFCOMM_UUID_STR),
130         .probe  = port_probe,
131         .remove = port_remove,
132 };
133
134 static int proxy_probe(struct btd_adapter *adapter)
135 {
136         const char *path = adapter_get_path(adapter);
137
138         DBG("path %s", path);
139
140         return proxy_register(connection, adapter);
141 }
142
143 static void proxy_remove(struct btd_adapter *adapter)
144 {
145         const char *path = adapter_get_path(adapter);
146
147         DBG("path %s", path);
148
149         proxy_unregister(adapter);
150 }
151
152 static struct btd_adapter_driver serial_proxy_driver = {
153         .name   = "serial-proxy",
154         .probe  = proxy_probe,
155         .remove = proxy_remove,
156 };
157
158 int serial_manager_init(DBusConnection *conn)
159 {
160         connection = dbus_connection_ref(conn);
161
162         btd_register_adapter_driver(&serial_proxy_driver);
163         btd_register_device_driver(&serial_port_driver);
164
165         return 0;
166 }
167
168 void serial_manager_exit(void)
169 {
170         btd_unregister_device_driver(&serial_port_driver);
171
172         dbus_connection_unref(connection);
173         connection = NULL;
174 }