Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / src / machine.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2014  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 <errno.h>
27 #include <gdbus.h>
28
29 #include "connman.h"
30
31 #define DEFAULT_MACHINE_TYPE    "laptop"
32
33 #define HOSTNAMED_SERVICE       "org.freedesktop.hostname1"
34 #define HOSTNAMED_INTERFACE     HOSTNAMED_SERVICE
35 #define HOSTNAMED_PATH          "/org/freedesktop/hostname1"
36
37 static GDBusClient *hostnamed_client = NULL;
38 static GDBusProxy *hostnamed_proxy = NULL;
39 static char *machine_type = NULL;
40
41 const char *connman_machine_get_type(void)
42 {
43         if (machine_type)
44                 return machine_type;
45
46         return DEFAULT_MACHINE_TYPE;
47 }
48
49 static void machine_property_changed(GDBusProxy *proxy, const char *name,
50                                         DBusMessageIter *iter, void *user_data)
51 {
52         DBG("Property %s", name);
53
54         if (g_str_equal(name, "Chassis")) {
55                 const char *str;
56
57                 if (!iter) {
58                         g_dbus_proxy_refresh_property(proxy, name);
59                         return;
60                 }
61
62                 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
63                         return;
64
65                 dbus_message_iter_get_basic(iter, &str);
66                 g_free(machine_type);
67                 machine_type = g_strdup(str);
68
69                 DBG("Machine type set to %s", machine_type);
70         }
71 }
72
73 int __connman_machine_init(void)
74 {
75         DBusConnection *connection;
76         int err = -EIO;
77
78         DBG("");
79
80         connection = connman_dbus_get_connection();
81
82         hostnamed_client = g_dbus_client_new(connection, HOSTNAMED_SERVICE,
83                                                         HOSTNAMED_PATH);
84         if (!hostnamed_client)
85                 goto error;
86
87         hostnamed_proxy = g_dbus_proxy_new(hostnamed_client, HOSTNAMED_PATH,
88                                                         HOSTNAMED_INTERFACE);
89         if (!hostnamed_proxy)
90                 goto error;
91
92         g_dbus_proxy_set_property_watch(hostnamed_proxy,
93                                         machine_property_changed, NULL);
94
95         dbus_connection_unref(connection);
96
97         return 0;
98 error:
99         if (hostnamed_client) {
100                 g_dbus_client_unref(hostnamed_client);
101                 hostnamed_client = NULL;
102         }
103
104         dbus_connection_unref(connection);
105
106         return err;
107 }
108
109 void __connman_machine_cleanup(void)
110 {
111         DBG("");
112
113         if (hostnamed_proxy) {
114                 g_dbus_proxy_unref(hostnamed_proxy);
115                 hostnamed_proxy = NULL;
116         }
117
118         if (hostnamed_client) {
119                 g_dbus_client_unref(hostnamed_client);
120                 hostnamed_client = NULL;
121         }
122
123         g_free(machine_type);
124         machine_type = NULL;
125 }