2
* oFono - Open Source Telephony
4
* Copyright (C) 2008-2012 Intel Corporation. All rights reserved.
5
* Copyright (C) 2012 BMW Car IT GmbH. All rights reserved.
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.
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.
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
29
#include <netinet/ether.h>
38
#define PPP_TIMEOUT 15
40
static int next_device_id = 0;
41
static GHashTable *device_hash;
43
static const char *none_prefix[] = { NULL };
45
struct ipv4_settings {
51
struct dundee_device {
53
struct dundee_device_driver *driver;
61
struct ipv4_settings settings;
64
guint connect_timeout;
68
const char *__dundee_device_get_path(struct dundee_device *device)
73
static void settings_append(struct dundee_device *device,
74
DBusMessageIter *iter)
76
DBusMessageIter variant;
77
DBusMessageIter array;
81
arraysig[0] = DBUS_TYPE_ARRAY;
82
arraysig[1] = typesig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
83
arraysig[2] = typesig[1] = DBUS_TYPE_STRING;
84
arraysig[3] = typesig[2] = DBUS_TYPE_VARIANT;
85
arraysig[4] = typesig[3] = DBUS_DICT_ENTRY_END_CHAR;
86
arraysig[5] = typesig[4] = '\0';
88
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
91
dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
94
if (device->active == FALSE)
97
if (device->settings.interface)
98
ofono_dbus_dict_append(&array, "Interface",
99
DBUS_TYPE_STRING, &device->settings.interface);
101
if (device->settings.ip)
102
ofono_dbus_dict_append(&array, "Address", DBUS_TYPE_STRING,
103
&device->settings.ip);
105
if (device->settings.nameservers)
106
ofono_dbus_dict_append_array(&array, "DomainNameServers",
108
&device->settings.nameservers);
111
dbus_message_iter_close_container(&variant, &array);
113
dbus_message_iter_close_container(iter, &variant);
116
static void settings_append_dict(struct dundee_device *device,
117
DBusMessageIter *dict)
119
DBusMessageIter entry;
120
const char *key = "Settings";
122
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
125
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
127
settings_append(device, &entry);
129
dbus_message_iter_close_container(dict, &entry);
132
void __dundee_device_append_properties(struct dundee_device *device,
133
DBusMessageIter *dict)
135
settings_append_dict(device, dict);
137
ofono_dbus_dict_append(dict, "Name", DBUS_TYPE_STRING,
140
ofono_dbus_dict_append(dict, "Active", DBUS_TYPE_BOOLEAN,
144
void __dundee_device_foreach(dundee_device_foreach_func func, void *userdata)
151
g_hash_table_iter_init(&iter, device_hash);
153
while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
154
struct dundee_device *device = value;
156
func(device, userdata);
160
static void settings_changed(struct dundee_device *device)
162
DBusConnection *conn = ofono_dbus_get_connection();
164
DBusMessageIter iter;
165
const char *key = "Settings";
167
signal = dbus_message_new_signal(device->path,
168
DUNDEE_DEVICE_INTERFACE,
173
dbus_message_iter_init_append(signal, &iter);
174
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
176
settings_append(device, &iter);
178
g_dbus_send_message(conn, signal);
181
static DBusMessage *device_get_properties(DBusConnection *conn,
182
DBusMessage *msg, void *data)
184
struct dundee_device *device = data;
186
DBusMessageIter iter;
187
DBusMessageIter dict;
189
reply = dbus_message_new_method_return(msg);
193
dbus_message_iter_init_append(reply, &iter);
195
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
196
OFONO_PROPERTIES_ARRAY_SIGNATURE,
199
__dundee_device_append_properties(device, &dict);
201
dbus_message_iter_close_container(&iter, &dict);
207
static void debug(const char *str, void *data)
209
DBG("%s: %s\n", (const char *) data, str);
212
static void ppp_connect(const char *iface, const char *local, const char *peer,
213
const char *dns1, const char *dns2,
216
DBusConnection *conn = ofono_dbus_get_connection();
217
struct dundee_device *device = user_data;
218
const char *dns[3] = { dns1, dns2, 0 };
221
DBG("Network Device: %s\n", iface);
222
DBG("IP Address: %s\n", local);
223
DBG("Peer IP Address: %s\n", peer);
224
DBG("Primary DNS Server: %s\n", dns1);
225
DBG("Secondary DNS Server: %s\n", dns2);
227
if (device->connect_timeout > 0) {
228
g_source_remove(device->connect_timeout);
229
device->connect_timeout = 0;
232
g_free(device->settings.interface);
233
device->settings.interface = g_strdup(iface);
234
if (device->settings.interface == NULL)
237
g_free(device->settings.ip);
238
device->settings.ip = g_strdup(local);
239
if (device->settings.ip == NULL)
242
g_strfreev(device->settings.nameservers);
243
device->settings.nameservers = g_strdupv((gchar **)dns);
244
if (device->settings.nameservers == NULL)
247
__ofono_dbus_pending_reply(&device->pending,
248
dbus_message_new_method_return(device->pending));
249
device->pending = NULL;
251
device->active = TRUE;
253
settings_changed(device);
254
ofono_dbus_signal_property_changed(conn, device->path,
255
DUNDEE_DEVICE_INTERFACE, "Active",
256
DBUS_TYPE_BOOLEAN, &device->active);
261
g_free(device->settings.interface);
262
g_free(device->settings.ip);
263
g_strfreev(device->settings.nameservers);
264
device->settings.interface = NULL;
265
device->settings.ip = NULL;
266
device->settings.nameservers = NULL;
268
__ofono_dbus_pending_reply(&device->pending,
269
__dundee_error_failed(device->pending));
270
device->pending = NULL;
273
static void disconnect_callback(const struct dundee_error *error, void *data)
275
struct dundee_device *device = data;
279
g_at_chat_unref(device->chat);
282
if (device->pending == NULL)
285
if (error->type != DUNDEE_ERROR_TYPE_NO_ERROR) {
286
__ofono_dbus_pending_reply(&device->pending,
287
__dundee_error_failed(device->pending));
291
__ofono_dbus_pending_reply(&device->pending,
292
dbus_message_new_method_return(device->pending));
295
device->pending = NULL;
298
static gboolean ppp_connect_timeout(gpointer user_data)
300
struct dundee_device *device = user_data;
302
if (device->pending != NULL) {
303
__ofono_dbus_pending_reply(&device->pending,
304
__dundee_error_timed_out(device->pending));
305
device->pending = NULL;
308
device->driver->disconnect(device, disconnect_callback, device);
310
device->connect_timeout = 0;
315
static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
317
DBusConnection *conn = ofono_dbus_get_connection();
318
struct dundee_device *device = user_data;
321
DBG("PPP Link down: %d\n", reason);
323
g_at_ppp_unref(device->ppp);
326
g_at_chat_resume(device->chat);
328
g_free(device->settings.interface);
329
g_free(device->settings.ip);
330
g_strfreev(device->settings.nameservers);
331
device->settings.interface = NULL;
332
device->settings.ip = NULL;
333
device->settings.nameservers = NULL;
335
device->active = FALSE;
337
settings_changed(device);
338
ofono_dbus_signal_property_changed(conn, device->path,
339
DUNDEE_DEVICE_INTERFACE, "Active",
340
DBUS_TYPE_BOOLEAN, &device->active);
342
device->driver->disconnect(device, disconnect_callback, device);
345
static void dial_cb(gboolean ok, GAtResult *result, gpointer user_data)
347
struct dundee_device *device = user_data;
351
DBG("Unable to define context\n");
355
/* get the data IO channel */
356
io = g_at_chat_get_io(device->chat);
359
* shutdown gatchat or else it tries to take all the input
360
* from the modem and does not let PPP get it.
362
g_at_chat_suspend(device->chat);
365
device->ppp = g_at_ppp_new();
366
if (device->ppp == NULL) {
367
DBG("Unable to create PPP object\n");
370
g_at_ppp_set_debug(device->ppp, debug, "PPP");
372
device->connect_timeout = g_timeout_add_seconds(PPP_TIMEOUT,
373
ppp_connect_timeout, device);
375
/* set connect and disconnect callbacks */
376
g_at_ppp_set_connect_function(device->ppp, ppp_connect, device);
377
g_at_ppp_set_disconnect_function(device->ppp, ppp_disconnect, device);
379
/* open the ppp connection */
380
g_at_ppp_open(device->ppp, io);
385
__ofono_dbus_pending_reply(&device->pending,
386
__dundee_error_failed(device->pending));
387
device->pending = NULL;
389
device->driver->disconnect(device, disconnect_callback, device);
392
static int device_dial_setup(struct dundee_device *device, int fd)
397
io = g_io_channel_unix_new(fd);
401
syntax = g_at_syntax_new_gsm_permissive();
402
device->chat = g_at_chat_new(io, syntax);
403
g_io_channel_unref(io);
404
g_at_syntax_unref(syntax);
406
if (device->chat == NULL)
409
g_at_chat_set_debug(device->chat, debug, "Control");
411
g_at_chat_send(device->chat, "ATD*99#", none_prefix, dial_cb,
417
static void connect_callback(const struct dundee_error *error,
420
struct dundee_device *device = data;
425
if (error->type != DUNDEE_ERROR_TYPE_NO_ERROR)
428
err = device_dial_setup(device, fd);
435
__ofono_dbus_pending_reply(&device->pending,
436
__dundee_error_failed(device->pending));
437
device->pending = NULL;
440
static DBusMessage *set_property_active(struct dundee_device *device,
442
DBusMessageIter *var)
446
DBG("%p path %s", device, device->path);
448
if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_BOOLEAN)
449
return __dundee_error_invalid_args(msg);
451
dbus_message_iter_get_basic(var, &active);
453
device->pending = dbus_message_ref(msg);
456
device->driver->connect(device, connect_callback, device);
457
else if (device->ppp)
458
g_at_ppp_shutdown(device->ppp);
463
static DBusMessage *device_set_property(DBusConnection *conn,
464
DBusMessage *msg, void *data)
466
struct dundee_device *device = data;
467
DBusMessageIter iter, var;
470
if (dbus_message_iter_init(msg, &iter) == FALSE)
471
return __dundee_error_invalid_args(msg);
473
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
474
return __dundee_error_invalid_args(msg);
476
dbus_message_iter_get_basic(&iter, &name);
477
dbus_message_iter_next(&iter);
479
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
480
return __dundee_error_invalid_args(msg);
482
dbus_message_iter_recurse(&iter, &var);
484
if (g_str_equal(name, "Active"))
485
return set_property_active(device, msg, &var);
487
return __dundee_error_invalid_args(msg);
490
static const GDBusMethodTable device_methods[] = {
491
{ GDBUS_METHOD("GetProperties",
492
NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
493
device_get_properties) },
494
{ GDBUS_ASYNC_METHOD("SetProperty",
495
GDBUS_ARGS({ "property", "s" }, { "value", "v" }),
496
NULL, device_set_property) },
500
static const GDBusSignalTable device_signals[] = {
501
{ GDBUS_SIGNAL("PropertyChanged",
502
GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
506
static int register_device(struct dundee_device *device)
508
DBusConnection *conn = ofono_dbus_get_connection();
510
DBusMessageIter iter;
511
DBusMessageIter dict;
513
DBG("%p path %s", device, device->path);
515
if (!g_dbus_register_interface(conn, device->path,
516
DUNDEE_DEVICE_INTERFACE,
517
device_methods, device_signals,
518
NULL, device, NULL)) {
519
ofono_error("Could not register Device %s", device->path);
523
signal = dbus_message_new_signal(DUNDEE_MANAGER_PATH,
524
DUNDEE_MANAGER_INTERFACE,
530
dbus_message_iter_init_append(signal, &iter);
532
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
534
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
535
OFONO_PROPERTIES_ARRAY_SIGNATURE,
537
__dundee_device_append_properties(device, &dict);
538
dbus_message_iter_close_container(&iter, &dict);
540
g_dbus_send_message(conn, signal);
545
static int unregister_device(struct dundee_device *device)
547
DBusConnection *conn = ofono_dbus_get_connection();
549
DBG("%p path %s", device, device->path);
551
g_dbus_unregister_interface(conn, device->path,
552
DUNDEE_DEVICE_INTERFACE);
554
g_dbus_emit_signal(conn, DUNDEE_MANAGER_PATH,
555
DUNDEE_MANAGER_INTERFACE, "DeviceRemoved",
556
DBUS_TYPE_OBJECT_PATH, &device->path,
562
static void destroy_device(gpointer user)
564
struct dundee_device *device = user;
566
if (device->chat != NULL)
567
g_at_chat_unref(device->chat);
569
if (device->ppp != NULL)
570
g_at_ppp_unref(device->ppp);
573
dbus_message_unref(device->pending);
575
g_free(device->settings.interface);
576
g_free(device->settings.ip);
577
g_strfreev(device->settings.nameservers);
579
g_free(device->path);
580
g_free(device->name);
585
struct dundee_device *dundee_device_create(struct dundee_device_driver *d)
587
struct dundee_device *device;
589
device = g_try_new0(struct dundee_device, 1);
595
device->path = g_strdup_printf("/device%d", next_device_id);
596
if (device->path == NULL) {
606
int dundee_device_register(struct dundee_device *device)
610
err = register_device(device);
614
device->registered = TRUE;
616
g_hash_table_insert(device_hash, g_strdup(device->path), device);
621
void dundee_device_unregister(struct dundee_device *device)
625
unregister_device(device);
627
device->registered = FALSE;
629
g_hash_table_remove(device_hash, device->path);
632
void dundee_device_set_data(struct dundee_device *device, void *data)
637
void *dundee_device_get_data(struct dundee_device *device)
642
int dundee_device_set_name(struct dundee_device *device, const char *name)
644
DBusConnection *conn = ofono_dbus_get_connection();
646
DBG("%p name %s", device, name);
648
g_free(device->name);
649
device->name = g_strdup(name);
651
if (device->registered == FALSE)
654
ofono_dbus_signal_property_changed(conn, device->path,
655
DUNDEE_DEVICE_INTERFACE, "Name",
656
DBUS_TYPE_STRING, &device->name);
661
static void device_shutdown(gpointer key, gpointer value, gpointer user_data)
663
struct dundee_device *device = value;
665
unregister_device(device);
668
void __dundee_device_shutdown(void)
670
g_hash_table_foreach(device_hash, device_shutdown, NULL);
675
int __dundee_device_init(void)
679
device_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
680
g_free, destroy_device);
685
void __dundee_device_cleanup(void)
689
g_hash_table_destroy(device_hash);