Open Source Telephony
 help / color / mirror / Atom feed
* [PATCH 1/3] plugins: Implementation of Network Time plugin
@ 2010-12-03  9:47 Antti Paila
  2010-12-03  9:47 ` [PATCH 2/3] plugins: Enabling nettime plugin in Makefile.am Antti Paila
                   ` (3 more replies)
  0 siblings, 4 replies; 21+ messages in thread
From: Antti Paila @ 2010-12-03  9:47 UTC (permalink / raw)
  To: ofono

[-- Attachment #1: Type: text/plain, Size: 8465 bytes --]

---
 include/dbus.h    |    1 +
 plugins/nettime.c |  293 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 294 insertions(+), 0 deletions(-)
 create mode 100644 plugins/nettime.c

diff --git a/include/dbus.h b/include/dbus.h
index 9e29afb..0c48f83 100644
--- a/include/dbus.h
+++ b/include/dbus.h
@@ -45,6 +45,7 @@ extern "C" {
 #define OFONO_MESSAGE_WAITING_INTERFACE "org.ofono.MessageWaiting"
 #define OFONO_NETWORK_REGISTRATION_INTERFACE "org.ofono.NetworkRegistration"
 #define OFONO_NETWORK_OPERATOR_INTERFACE "org.ofono.NetworkOperator"
+#define OFONO_NETWORK_TIME_INTERFACE "org.ofono.NetworkTime"
 #define OFONO_PHONEBOOK_INTERFACE "org.ofono.Phonebook"
 #define OFONO_RADIO_SETTINGS_INTERFACE "org.ofono.RadioSettings"
 #define OFONO_AUDIO_SETTINGS_INTERFACE "org.ofono.AudioSettings"
diff --git a/plugins/nettime.c b/plugins/nettime.c
new file mode 100644
index 0000000..0f99bb1
--- /dev/null
+++ b/plugins/nettime.c
@@ -0,0 +1,293 @@
+/*
+ *
+ *  oFono - Open Source Telephony
+ *
+ *  Copyright (C) 2008-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 <glib.h>
+
+#define OFONO_API_SUBJECT_TO_CHANGE
+#include <ofono/plugin.h>
+#include <ofono/log.h>
+#include <ofono/nettime.h>
+#include <ofono/types.h>
+#include <gdbus.h>
+#include "ofono.h"
+
+#include "common.h"
+
+#define MAX_TIME_STR_LEN 128
+#define TIME_FORMAT "%FT%TZ"
+
+
+struct nettime_data {
+	struct ofono_network_time nw_time;
+	time_t received;
+};
+
+static void nettime_register(struct ofono_nettime_context *);
+
+static gboolean encode_time_format(struct ofono_network_time *time,
+				 struct tm *tm)
+{
+	if (time->year < 0)
+		return FALSE;
+
+	tm->tm_year = time->year - 1900;
+	tm->tm_mon = time->mon - 1;
+	tm->tm_mday = time->mday;
+	tm->tm_hour = time->hour;
+	tm->tm_min = time->min;
+	tm->tm_sec = time->sec;
+	tm->tm_gmtoff = time->utcoff;
+	tm->tm_isdst = time->dst;
+
+	return TRUE;
+}
+
+static time_t get_monotonic_time()
+{
+	struct timespec ts;
+	clock_gettime(CLOCK_MONOTONIC, &ts);
+	return ts.tv_sec;
+}
+
+static struct tm *refresh_nw_time(struct tm *nw_time,
+				time_t received)
+{
+	time_t now, new_nw_time;
+
+	now = get_monotonic_time();
+	new_nw_time = timegm(nw_time) + now - received;
+	return gmtime(&new_nw_time);
+}
+
+static int fill_time_changed_signal(DBusMessage *signal,
+		struct nettime_data *ntd)
+{
+	DBusMessageIter iter, iter_array;
+	int time_available;
+	char buf[MAX_TIME_STR_LEN];
+	struct tm time_tm, *new_time_tm;
+	const char *str = buf;
+
+	dbus_message_iter_init_append(signal, &iter);
+	dbus_message_iter_open_container(&iter,
+					DBUS_TYPE_ARRAY,
+					"{sv}",
+					&iter_array);
+
+	time_available = encode_time_format(&ntd->nw_time, &time_tm);
+	if (time_available != 0) {
+		new_time_tm = refresh_nw_time(&time_tm, ntd->received);
+		strftime(buf, MAX_TIME_STR_LEN, TIME_FORMAT, new_time_tm);
+
+		ofono_dbus_dict_append(&iter_array,
+					"DateAndTime",
+					DBUS_TYPE_STRING,
+					&str);
+	}
+
+	ofono_dbus_dict_append(&iter_array,
+				"Timezone",
+				DBUS_TYPE_INT32,
+				&ntd->nw_time.utcoff);
+	ofono_dbus_dict_append(&iter_array,
+				"DST",
+				DBUS_TYPE_UINT32,
+				&ntd->nw_time.dst);
+	dbus_message_iter_close_container(&iter, &iter_array);
+	return 0;
+}
+
+static DBusMessage *create_time_changed_signal(
+			struct ofono_nettime_context *context)
+{
+	DBusMessage *signal;
+	struct nettime_data *ntd = context->data;
+	const char *path = ofono_modem_get_path(context->modem);
+
+	if (path == NULL) {
+		ofono_error("Fetching path for modem failed");
+		return NULL;
+	}
+
+	signal = dbus_message_new_signal(path, OFONO_NETWORK_TIME_INTERFACE,
+					"NetworkTimeChanged");
+	fill_time_changed_signal(signal, ntd);
+
+	return signal;
+}
+
+static void init_time(struct ofono_nettime_context *context)
+{
+	struct nettime_data *nettime_data;
+	context->data = g_try_new0(struct nettime_data, 1);
+
+	nettime_data = context->data;
+	nettime_data->nw_time.year = -1;
+
+}
+
+static int nettime_probe(struct ofono_nettime_context *context)
+{
+	ofono_debug("Network Time Probe for modem: %p", context->modem);
+
+	init_time(context);
+
+	nettime_register(context);
+	return 0;
+}
+
+static void nettime_remove(struct ofono_nettime_context *context)
+{
+	DBusConnection *conn;
+	const char *path;
+
+	ofono_debug("Network Time Remove for modem: %p", context->modem);
+
+	g_free(context->data);
+
+	conn = ofono_dbus_get_connection();
+	path = ofono_modem_get_path(context->modem);
+
+	if (!g_dbus_unregister_interface(conn,
+					path,
+					OFONO_NETWORK_TIME_INTERFACE))
+		ofono_error("Could not unregister %s interface",
+				OFONO_NETWORK_TIME_INTERFACE);
+
+	ofono_modem_remove_interface(context->modem,
+			OFONO_NETWORK_TIME_INTERFACE);
+}
+
+static void nettime_info_received(struct ofono_nettime_context *context,
+				struct ofono_network_time *info)
+{
+	DBusMessage *nt_signal;
+	struct nettime_data *ntd = context->data;
+
+	if (info == NULL)
+		return;
+
+	ofono_debug("Received a network time notification on modem: %p",
+			context->modem);
+
+	ntd->nw_time = *info;
+	ntd->received = get_monotonic_time();
+
+	nt_signal = create_time_changed_signal(context);
+	if (nt_signal == NULL) {
+		ofono_error("Failed to create NetworkTimeChanged signal");
+		return;
+	}
+
+	g_dbus_send_message(ofono_dbus_get_connection(), nt_signal);
+}
+
+static struct ofono_nettime_driver nettime_driver = {
+	.name		= "Network Time",
+	.probe		= nettime_probe,
+	.remove		= nettime_remove,
+	.info_received	= nettime_info_received,
+};
+
+static int nettime_init(void)
+{
+	return ofono_nettime_driver_register(&nettime_driver);
+}
+
+static void nettime_exit(void)
+{
+	ofono_nettime_driver_unregister(&nettime_driver);
+}
+
+static DBusMessage *nettime_get_time(DBusConnection *conn,
+						DBusMessage *msg, void *data)
+{
+	DBusMessage *reply;
+	int status;
+	struct ofono_nettime_context *context = data;
+
+
+	ofono_debug("Time requested from modem %p", context->modem);
+
+	reply = dbus_message_new_method_return(msg);
+	if (reply == NULL) {
+		ofono_error("Message allocation failed");
+		return NULL;
+	}
+
+	status = fill_time_changed_signal(reply, context->data);
+	if (status != 0) {
+		ofono_error("NetworkTimeChaged signal not filled, status: %d",
+			status);
+		return NULL;
+	}
+
+	return reply;
+}
+
+static GDBusMethodTable nettime_methods[] = {
+	{ "GetNetworkTime", "", "a{sv}", nettime_get_time },
+	{ }
+};
+
+static GDBusSignalTable nettime_signals[] = {
+	{ "NetworkTimeChanged", "a{sv}" },
+	{ }
+};
+
+static void nettime_register(struct ofono_nettime_context *context)
+{
+	DBusConnection *conn;
+	const char *path;
+
+	ofono_debug("Registering Network time for modem %s" ,
+		ofono_modem_get_path(context->modem));
+
+	conn = ofono_dbus_get_connection();
+
+	path = ofono_modem_get_path(context->modem);
+	if (path == NULL) {
+		ofono_error("No path for modem found");
+		return;
+	}
+
+	if (!g_dbus_register_interface(conn, path,
+				OFONO_NETWORK_TIME_INTERFACE,
+				nettime_methods,
+				nettime_signals,
+				NULL, context, NULL)) {
+		ofono_error("Could not create %s interface",
+				OFONO_NETWORK_TIME_INTERFACE);
+		return;
+	}
+
+	ofono_modem_add_interface(context->modem, OFONO_NETWORK_TIME_INTERFACE);
+}
+
+OFONO_PLUGIN_DEFINE(nettime, "Network Time Plugin",
+		VERSION, OFONO_PLUGIN_PRIORITY_DEFAULT,
+		nettime_init, nettime_exit)
+
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2010-12-08 16:49 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-03  9:47 [PATCH 1/3] plugins: Implementation of Network Time plugin Antti Paila
2010-12-03  9:47 ` [PATCH 2/3] plugins: Enabling nettime plugin in Makefile.am Antti Paila
2010-12-03  9:47 ` [PATCH 3/3] plugins: Test scripts for nettime plugin Antti Paila
2010-12-03 14:07 ` [PATCH 1/3] plugins: Implementation of Network Time plugin Aki Niemi
2010-12-04  0:02   ` Marcel Holtmann
2010-12-04 12:03     ` Aki Niemi
2010-12-05 12:40       ` Marcel Holtmann
2010-12-08  8:44         ` Marcel Holtmann
2010-12-08  9:19           ` Aki Niemi
2010-12-06 16:33     ` =?unknown-8bit?q?R=C3=A9mi?= Denis-Courmont
2010-12-06 23:29       ` Marcel Holtmann
2010-12-06 23:56         ` =?unknown-8bit?q?R=C3=A9mi?= Denis-Courmont
2010-12-07  8:58           ` Marcel Holtmann
2010-12-07 11:00             ` Antti Paila
2010-12-07 16:05             ` =?unknown-8bit?q?R=C3=A9mi?= Denis-Courmont
2010-12-06 17:00 ` =?unknown-8bit?q?R=C3=A9mi?= Denis-Courmont
2010-12-07  7:51   ` Aki Niemi
2010-12-07  9:12     ` Antti Paila
2010-12-07 16:12     ` =?unknown-8bit?q?R=C3=A9mi?= Denis-Courmont
2010-12-08  8:13       ` Aki Niemi
2010-12-08 16:49         ` =?unknown-8bit?q?R=C3=A9mi?= Denis-Courmont

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox