summaryrefslogtreecommitdiffstats
path: root/demo/dbus-simple-service/dbus-simple-service.c
blob: e6ed3f60d6c9e3d7d50dabf6d894c88d49e3acf7 (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
/*
 *   json dbus bridge
 *
 *   Copyright (c) 2009 by Michael Olbrich <m.olbrich@pengutronix.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>

#include <systeminfo_skel.h>


int system_info_date(system_info_t *self, unsigned int *date)
{
	(void)self;
	struct timeval t;
	gettimeofday(&t, 0);
	*date = t.tv_sec;
	return 0;
}

int system_info_noop(system_info_t *self)
{
	(void)self;
	return 0;
}

int system_info_ping(system_info_t *self, const char *ping, const char **pong)
{
	(void)self;
	*pong = ping;
	return 0;
}

static int quit;

void sighandler(int signal)
{
	(void)signal;
	quit = 1;
}

int main(void)
{
	int signals[] = { SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, 0 };
	struct sigaction action = {
		.sa_handler = sighandler
	};
	int *signal;
	DBusConnection *connection;
	DBusError err;
	system_info_t info;

	quit = 0;
	sigemptyset(&action.sa_mask);
	for (signal = signals; *signal; ++signal) {
		sigaddset(&action.sa_mask, *signal);
	}
	for (signal = signals; *signal; ++signal) {
		if (sigaction(*signal, &action, NULL) != 0)
			return 1;
	}

	dbus_error_init(&err);
	if ((connection = dbus_bus_get(DBUS_BUS_SESSION, &err)) == 0) {
		fprintf(stderr, "couldn't connect to dbus: %s: %s\n",
			err.name, err.message);
		dbus_error_free(&err);
		exit(1);
	}
	if (dbus_bus_request_name(connection, "com.pengutronix.jdb.simpleinfo",
			 0, &err) < 0) {
		fprintf(stderr, "couldn't connect to dbus: %s: %s\n",
			err.name, err.message);
		dbus_error_free(&err);
		exit(1);
	}
	dbus_error_free(&err);

	system_info_init(&info);
	info.Date = system_info_date;
	info.Noop = system_info_noop;
	info.Ping = system_info_ping;
	if (system_info_register(&info, connection, "/") != 0) {
		fprintf(stderr, "system_info_register failed.\n");
		exit(1);
	}

	while (!quit) {
		dbus_connection_read_write_dispatch(connection, 1000);
	}
	dbus_connection_unref(connection);
	return 0;
}