Fix bug:TIVI-374[2.0] no hidd binary, which blocks bluetooth keyboard connection
[profile/ivi/bluez.git] / proximity / main.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 <errno.h>
30 #include <stdint.h>
31 #include <glib.h>
32 #include <gdbus.h>
33
34 #include "log.h"
35 #include "plugin.h"
36 #include "manager.h"
37 #include "hcid.h"
38
39 static DBusConnection *connection = NULL;
40 static GKeyFile *config = NULL;
41
42 static GKeyFile *open_config_file(const char *file)
43 {
44         GError *gerr = NULL;
45         GKeyFile *keyfile;
46
47         keyfile = g_key_file_new();
48
49         g_key_file_set_list_separator(keyfile, ',');
50
51         if (!g_key_file_load_from_file(keyfile, file, 0, &gerr)) {
52                 error("Parsing %s failed: %s", file, gerr->message);
53                 g_error_free(gerr);
54                 g_key_file_free(keyfile);
55                 return NULL;
56         }
57
58         return keyfile;
59 }
60
61 static int proximity_init(void)
62 {
63         if (!main_opts.gatt_enabled) {
64                 DBG("GATT is disabled");
65                 return -ENOTSUP;
66         }
67
68         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
69         if (connection == NULL)
70                 return -EIO;
71
72         config = open_config_file(CONFIGDIR "/proximity.conf");
73
74         if (proximity_manager_init(connection, config) < 0) {
75                 dbus_connection_unref(connection);
76                 return -EIO;
77         }
78
79         return 0;
80 }
81
82 static void proximity_exit(void)
83 {
84         if (!main_opts.gatt_enabled)
85                 return;
86
87         if (config)
88                 g_key_file_free(config);
89
90         proximity_manager_exit();
91         dbus_connection_unref(connection);
92 }
93
94 BLUETOOTH_PLUGIN_DEFINE(proximity, VERSION,
95                         BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
96                         proximity_init, proximity_exit)