Add packaging directory
[platform/upstream/neard.git] / src / manager.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29
30 #include <glib.h>
31
32 #include <gdbus.h>
33
34 #include "near.h"
35
36 static DBusConnection *connection;
37
38 int __near_manager_adapter_add(uint32_t idx, const char *name,
39                                 uint32_t protocols, bool powered)
40 {
41         struct near_adapter *adapter;
42         const char *path;
43         int err;
44
45         DBG("idx %d", idx);
46
47         adapter = __near_adapter_create(idx, name, protocols, powered);
48         if (!adapter)
49                 return -ENOMEM;
50
51         path = __near_adapter_get_path(adapter);
52         if (!path) {
53                 __near_adapter_destroy(adapter);
54                 return -EINVAL;
55         }
56
57         err = __near_adapter_add(adapter);
58         if (err < 0)
59                 __near_adapter_destroy(adapter);
60
61         return err;
62 }
63
64 void __near_manager_adapter_remove(uint32_t idx)
65 {
66         struct near_adapter *adapter;
67         const char *path;
68
69         DBG("idx %d", idx);
70
71         adapter = __near_adapter_get(idx);
72         if (!adapter)
73                 return;
74
75         path = __near_adapter_get_path(adapter);
76         if (!path)
77                 return;
78
79         __near_adapter_remove(adapter);
80 }
81
82 int __near_manager_init(DBusConnection *conn)
83 {
84         DBG("");
85
86         connection = dbus_connection_ref(conn);
87
88         DBG("connection %p", connection);
89
90         g_dbus_attach_object_manager(connection);
91
92         return __near_netlink_get_adapters();
93 }
94
95 void __near_manager_cleanup(void)
96 {
97         DBG("");
98
99         g_dbus_detach_object_manager(connection);
100
101         dbus_connection_unref(connection);
102 }