Git init
[framework/connectivity/bluez.git] / attrib / manager.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2010  Nokia Corporation
6  *  Copyright (C) 2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <bluetooth/bluetooth.h>
30 #include <bluetooth/sdp.h>
31 #include <bluetooth/sdp_lib.h>
32
33 #include "../src/adapter.h"
34 #include "../src/device.h"
35 #include "hcid.h"
36
37 #include "manager.h"
38 #include "client.h"
39 #include "example.h"
40
41 #define GATT_UUID       "00001801-0000-1000-8000-00805f9b34fb"
42
43 static DBusConnection *connection;
44
45 static int client_probe(struct btd_device *device, GSList *uuids)
46 {
47         const sdp_record_t *rec;
48         int psm = -1;
49
50         rec = btd_device_get_record(device, GATT_UUID);
51         if (rec) {
52                 sdp_list_t *list;
53                 if (sdp_get_access_protos(rec, &list) < 0)
54                         return -1;
55
56                 psm = sdp_get_proto_port(list, L2CAP_UUID);
57
58                 sdp_list_foreach(list, (sdp_list_func_t) sdp_list_free, NULL);
59                 sdp_list_free(list, NULL);
60
61                 if (psm < 0)
62                         return -1;
63         }
64
65         return attrib_client_register(device, psm);
66 }
67
68 static void client_remove(struct btd_device *device)
69 {
70         attrib_client_unregister(device);
71 }
72
73 static struct btd_device_driver client_driver = {
74         .name = "gatt-client",
75         .uuids = BTD_UUIDS(GATT_UUID),
76         .probe = client_probe,
77         .remove = client_remove,
78 };
79
80 int attrib_manager_init(DBusConnection *conn)
81 {
82         connection = dbus_connection_ref(conn);
83
84         attrib_client_init(connection);
85
86         btd_register_device_driver(&client_driver);
87
88
89         if (main_opts.attrib_server)
90                 return server_example_init();
91
92         return 0;
93 }
94
95 void attrib_manager_exit(void)
96 {
97         btd_unregister_device_driver(&client_driver);
98
99         if (main_opts.attrib_server)
100                 server_example_exit();
101
102         attrib_client_exit();
103
104         dbus_connection_unref(connection);
105 }