summaryrefslogtreecommitdiffstats
path: root/src/nettime.c
blob: 06242596b1b5c9a76f009bf287a9ccc0e94e5963 (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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2010  Nokia Corporation and/or its subsidiary(-ies).
 *
 *  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 <string.h>
#include <stdio.h>

#include <glib.h>

#include "ofono.h"

static GSList *nettime_drivers = NULL;

static struct ofono_nettime_context *nettime_context_create(
					struct ofono_modem *modem,
					struct ofono_nettime_driver *driver)
{
	struct ofono_nettime_context *context;

	if (driver->probe == NULL)
		return NULL;

	context = g_try_new0(struct ofono_nettime_context, 1);

	if (context == NULL)
		return NULL;

	context->driver = driver;
	context->modem = modem;

	if (driver->probe(context) < 0) {
		g_free(context);
		return NULL;
	}

	return context;
}

static void context_remove(struct ofono_atom *atom)
{
	struct ofono_nettime_context *context = __ofono_atom_get_data(atom);

	if (context->driver->remove)
		context->driver->remove(context);

	g_free(context);
}

void __ofono_nettime_probe_drivers(struct ofono_modem *modem)
{
	struct ofono_nettime_driver *driver;
	struct ofono_nettime_context *context;
	GSList *l;

	for (l = nettime_drivers; l; l = l->next) {
		driver = l->data;

		context = nettime_context_create(modem, driver);
		if (context == NULL)
			continue;

		__ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_NETTIME,
						context_remove, context);
	}
}

static void nettime_info_received(struct ofono_atom *atom, void *data)
{
	struct ofono_nettime_context *context = __ofono_atom_get_data(atom);
	struct ofono_network_time *info = data;

	if (context->driver->info_received == NULL)
		return;

	context->driver->info_received(context, info);
}

void __ofono_nettime_info_received(struct ofono_modem *modem,
					struct ofono_network_time *info)
{
	__ofono_modem_foreach_atom(modem, OFONO_ATOM_TYPE_NETTIME,
					nettime_info_received, info);
}

int ofono_nettime_driver_register(const struct ofono_nettime_driver *driver)
{
	DBG("driver: %p name: %s", driver, driver->name);

	nettime_drivers = g_slist_prepend(nettime_drivers, (void *) driver);

	return 0;
}

void ofono_nettime_driver_unregister(const struct ofono_nettime_driver *driver)
{
	DBG("driver: %p name: %s", driver, driver->name);

	nettime_drivers = g_slist_remove(nettime_drivers, driver);
}