Initialize Tizen 2.3
[framework/connectivity/bluez.git] / wearable / proximity / manager.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011  Nokia Corporation
6  *  Copyright (C) 2011  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 <glib.h>
30 #include <gdbus.h>
31 #include <bluetooth/uuid.h>
32
33 #include "adapter.h"
34 #include "device.h"
35 #include "att.h"
36 #include "gattrib.h"
37 #include "gatt.h"
38 #include "monitor.h"
39 #include "reporter.h"
40 #include "manager.h"
41
42 static DBusConnection *connection = NULL;
43
44 static struct enabled enabled  = {
45         .linkloss = TRUE,
46         .pathloss = TRUE,
47         .findme = TRUE,
48 };
49
50 static gint primary_uuid_cmp(gconstpointer a, gconstpointer b)
51 {
52         const struct gatt_primary *prim = a;
53         const char *uuid = b;
54
55         return g_strcmp0(prim->uuid, uuid);
56 }
57
58 static int attio_device_probe(struct btd_device *device, GSList *uuids)
59 {
60         struct gatt_primary *linkloss, *txpower, *immediate;
61         GSList *l, *primaries;
62
63         primaries = btd_device_get_primaries(device);
64
65         l = g_slist_find_custom(primaries, IMMEDIATE_ALERT_UUID,
66                         primary_uuid_cmp);
67         immediate = (l ? l->data : NULL);
68
69         l = g_slist_find_custom(primaries, TX_POWER_UUID, primary_uuid_cmp);
70         txpower = (l ? l->data : NULL);
71
72         l = g_slist_find_custom(primaries, LINK_LOSS_UUID, primary_uuid_cmp);
73         linkloss = (l ? l->data : NULL);
74
75         return monitor_register(connection, device, linkloss, txpower,
76                                                         immediate, &enabled);
77 }
78
79 static void attio_device_remove(struct btd_device *device)
80 {
81         monitor_unregister(connection, device);
82 }
83
84 static struct btd_device_driver monitor_driver = {
85         .name = "Proximity GATT Monitor Driver",
86         .uuids = BTD_UUIDS(IMMEDIATE_ALERT_UUID, LINK_LOSS_UUID, TX_POWER_UUID),
87         .probe = attio_device_probe,
88         .remove = attio_device_remove,
89 };
90
91 static struct btd_adapter_driver reporter_server_driver = {
92         .name = "Proximity GATT Reporter Driver",
93         .probe = reporter_init,
94         .remove = reporter_exit,
95 };
96
97 static void load_config_file(GKeyFile *config)
98 {
99         char **list;
100         int i;
101
102         if (config == NULL)
103                 return;
104
105         list = g_key_file_get_string_list(config, "General", "Disable",
106                                                                 NULL, NULL);
107         for (i = 0; list && list[i] != NULL; i++) {
108                 if (g_str_equal(list[i], "FindMe"))
109                         enabled.findme = FALSE;
110                 else if (g_str_equal(list[i], "LinkLoss"))
111                         enabled.linkloss = FALSE;
112                 else if (g_str_equal(list[i], "PathLoss"))
113                         enabled.pathloss = FALSE;
114         }
115
116         g_strfreev(list);
117 }
118
119 int proximity_manager_init(DBusConnection *conn, GKeyFile *config)
120 {
121         int ret;
122
123         load_config_file(config);
124
125         connection = dbus_connection_ref(conn);
126
127         ret = btd_register_device_driver(&monitor_driver);
128         if (ret < 0)
129                 goto fail_monitor;
130
131         ret = btd_register_adapter_driver(&reporter_server_driver);
132         if (ret < 0)
133                 goto fail_reporter;
134
135         return 0;
136
137 fail_reporter:
138         btd_unregister_device_driver(&monitor_driver);
139
140 fail_monitor:
141         dbus_connection_unref(connection);
142         return ret;
143 }
144
145 void proximity_manager_exit(void)
146 {
147         btd_unregister_device_driver(&monitor_driver);
148         btd_unregister_adapter_driver(&reporter_server_driver);
149         dbus_connection_unref(connection);
150 }