summaryrefslogtreecommitdiff
path: root/src/machine.c
blob: 14ea366708c0b642add3fd3ded5de0340af0d795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2014  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <gdbus.h>

#include "connman.h"

#define DEFAULT_MACHINE_TYPE	"laptop"

#define HOSTNAMED_SERVICE	"org.freedesktop.hostname1"
#define HOSTNAMED_INTERFACE	HOSTNAMED_SERVICE
#define HOSTNAMED_PATH		"/org/freedesktop/hostname1"

static GDBusClient *hostnamed_client = NULL;
static GDBusProxy *hostnamed_proxy = NULL;
static char *machine_type = NULL;

const char *connman_machine_get_type(void)
{
	if (machine_type)
		return machine_type;

	return DEFAULT_MACHINE_TYPE;
}

static void machine_property_changed(GDBusProxy *proxy, const char *name,
					DBusMessageIter *iter, void *user_data)
{
	DBG("Property %s", name);

	if (g_str_equal(name, "Chassis")) {
		const char *str;

		if (!iter) {
			g_dbus_proxy_refresh_property(proxy, name);
			return;
		}

		if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
			return;

		dbus_message_iter_get_basic(iter, &str);
		g_free(machine_type);
		machine_type = g_strdup(str);

		DBG("Machine type set to %s", machine_type);
	}
}

int __connman_machine_init(void)
{
	DBusConnection *connection;
	int err = -EIO;

	DBG("");

	connection = connman_dbus_get_connection();

	hostnamed_client = g_dbus_client_new(connection, HOSTNAMED_SERVICE,
							HOSTNAMED_PATH);
	if (!hostnamed_client)
		goto error;

	hostnamed_proxy = g_dbus_proxy_new(hostnamed_client, HOSTNAMED_PATH,
							HOSTNAMED_INTERFACE);
	if (!hostnamed_proxy)
		goto error;

	g_dbus_proxy_set_property_watch(hostnamed_proxy,
					machine_property_changed, NULL);

	dbus_connection_unref(connection);

	return 0;
error:
	if (hostnamed_client) {
		g_dbus_client_unref(hostnamed_client);
		hostnamed_client = NULL;
	}

	dbus_connection_unref(connection);

	return err;
}

void __connman_machine_cleanup(void)
{
	DBG("");

	if (hostnamed_proxy) {
		g_dbus_proxy_unref(hostnamed_proxy);
		hostnamed_proxy = NULL;
	}

	if (hostnamed_client) {
		g_dbus_client_unref(hostnamed_client);
		hostnamed_client = NULL;
	}

	g_free(machine_type);
	machine_type = NULL;
}