Update WISPr command line client to use GWeb
[framework/connectivity/connman.git] / plugins / ofono.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28
29 #include <gdbus.h>
30 #include <string.h>
31
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/element.h>
35 #include <connman/device.h>
36 #include <connman/network.h>
37 #include <connman/dbus.h>
38 #include <connman/inet.h>
39 #include <connman/log.h>
40
41 #define OFONO_SERVICE                   "org.ofono"
42
43 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
44 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
45 #define OFONO_GPRS_INTERFACE            OFONO_SERVICE ".ConnectionManager"
46 #define OFONO_CONTEXT_INTERFACE         OFONO_SERVICE ".ConnectionContext"
47 #define OFONO_SIM_INTERFACE             OFONO_SERVICE ".SimManager"
48 #define OFONO_REGISTRATION_INTERFACE    OFONO_SERVICE ".NetworkRegistration"
49
50 #define PROPERTY_CHANGED                "PropertyChanged"
51 #define GET_PROPERTIES                  "GetProperties"
52 #define SET_PROPERTY                    "SetProperty"
53 #define CONTEXT_ADDED                   "ContextAdded"
54 #define CONTEXT_REMOVED                 "ContextRemoved"
55 #define ADD_CONTEXT                     "AddContext"
56 #define GET_MODEMS                      "GetModems"
57 #define MODEM_ADDED                     "ModemAdded"
58 #define MODEM_REMOVED                   "ModemRemoved"
59
60
61 #define TIMEOUT 40000
62
63 static DBusConnection *connection;
64
65 static GHashTable *modem_hash = NULL;
66
67 static GHashTable *network_hash;
68
69 struct modem_data {
70         char *path;
71         struct connman_device *device;
72         gboolean has_sim;
73         gboolean has_reg;
74         gboolean has_gprs;
75         gboolean available;
76         gboolean pending_online;
77         gboolean requested_online;
78         gboolean online;
79
80         uint8_t strength, has_strength;
81 };
82
83 static int modem_probe(struct connman_device *device)
84 {
85         DBG("device %p", device);
86
87         return 0;
88 }
89
90 static void modem_remove(struct connman_device *device)
91 {
92         DBG("device %p", device);
93 }
94
95 static int call_ofono(const char *path,
96                         const char *interface, const char *method,
97                         DBusPendingCallNotifyFunction notify, void *user_data,
98                         DBusFreeFunction free_function,
99                         int type, ...)
100 {
101         DBusMessage *message;
102         DBusPendingCall *call;
103         dbus_bool_t ok;
104         va_list va;
105
106         DBG("path %s %s.%s", path, interface, method);
107
108         if (path == NULL)
109                 return -EINVAL;
110
111         message = dbus_message_new_method_call(OFONO_SERVICE, path,
112                                         interface, method);
113         if (message == NULL)
114                 return -ENOMEM;
115
116         dbus_message_set_auto_start(message, FALSE);
117
118         va_start(va, type);
119         ok = dbus_message_append_args_valist(message, type, va);
120         va_end(va);
121
122         if (!ok)
123                 return -ENOMEM;
124
125         if (dbus_connection_send_with_reply(connection, message,
126                                                 &call, TIMEOUT) == FALSE) {
127                 connman_error("Failed to call %s.%s", interface, method);
128                 dbus_message_unref(message);
129                 return -EINVAL;
130         }
131
132         if (call == NULL) {
133                 connman_error("D-Bus connection not available");
134                 dbus_message_unref(message);
135                 return -EINVAL;
136         }
137
138         dbus_pending_call_set_notify(call, notify, user_data, free_function);
139
140         dbus_message_unref(message);
141
142         return -EINPROGRESS;
143 }
144
145 static void set_property_reply(DBusPendingCall *call, void *user_data)
146 {
147         DBusMessage *reply;
148         DBusError error;
149         char const *name = user_data;
150
151         DBG("");
152
153         dbus_error_init(&error);
154
155         reply = dbus_pending_call_steal_reply(call);
156
157         if (dbus_set_error_from_message(&error, reply)) {
158                 connman_error("SetProperty(%s) %s %s", name,
159                                 error.name, error.message);
160                 dbus_error_free(&error);
161         }
162
163         dbus_message_unref(reply);
164
165         dbus_pending_call_unref(call);
166 }
167
168 static int set_property(const char *path, const char *interface,
169                         const char *property, int type, void *value,
170                         DBusPendingCallNotifyFunction notify, void *user_data,
171                         DBusFreeFunction free_function)
172 {
173         DBusMessage *message;
174         DBusMessageIter iter;
175         DBusPendingCall *call;
176
177         DBG("path %s %s.%s", path, interface, property);
178
179         g_assert(notify == NULL ? free_function == NULL : 1);
180
181         if (path == NULL)
182                 return -EINVAL;
183
184         message = dbus_message_new_method_call(OFONO_SERVICE, path,
185                                         interface, SET_PROPERTY);
186         if (message == NULL)
187                 return -ENOMEM;
188
189         dbus_message_set_auto_start(message, FALSE);
190
191         dbus_message_iter_init_append(message, &iter);
192         connman_dbus_property_append_basic(&iter, property, type, value);
193
194         if (dbus_connection_send_with_reply(connection, message,
195                                                 &call, TIMEOUT) == FALSE) {
196                 connman_error("Failed to change \"%s\" property on %s",
197                                 property, interface);
198                 dbus_message_unref(message);
199                 return -EINVAL;
200         }
201
202         if (call == NULL) {
203                 connman_error("D-Bus connection not available");
204                 dbus_message_unref(message);
205                 return -EINVAL;
206         }
207
208         if (notify == NULL) {
209                 notify = set_property_reply;
210                 user_data = (void *)property;
211                 free_function = NULL;
212         }
213
214         dbus_pending_call_set_notify(call, notify, user_data, free_function);
215
216         dbus_message_unref(message);
217
218         return -EINPROGRESS;
219 }
220
221 static void update_modem_online(struct modem_data *modem,
222                                 connman_bool_t online)
223 {
224         DBG("modem %p path %s online %d", modem, modem->path, online);
225
226         modem->online = online;
227         modem->requested_online = online;
228         modem->pending_online = FALSE;
229
230         if (modem->device)
231                 connman_device_set_powered(modem->device, online);
232 }
233
234 static void set_online_reply(DBusPendingCall *call, void *user_data)
235 {
236         struct modem_data *modem;
237         DBusMessage *reply;
238         DBusError error;
239         gboolean result;
240
241         DBG("path %s", (char *)user_data);
242
243         if (modem_hash == NULL)
244                 return;
245
246         modem = g_hash_table_lookup(modem_hash, user_data);
247         if (modem == NULL)
248                 return;
249
250         reply = dbus_pending_call_steal_reply(call);
251
252         dbus_error_init(&error);
253
254         if (dbus_set_error_from_message(&error, reply)) {
255                 connman_error("SetProperty(Online) %s %s",
256                                 error.name, error.message);
257                 dbus_error_free(&error);
258
259                 result = modem->online;
260         } else
261                 result = modem->requested_online;
262
263         if (modem->pending_online)
264                 update_modem_online(modem, result);
265
266         dbus_message_unref(reply);
267
268         dbus_pending_call_unref(call);
269 }
270
271 static int modem_change_online(char const *path, dbus_bool_t online)
272 {
273         return set_property(path, OFONO_MODEM_INTERFACE, "Online",
274                                 DBUS_TYPE_BOOLEAN, &online,
275                                 set_online_reply,
276                                 (void *)g_strdup(path), g_free);
277 }
278
279 static int modem_enable(struct connman_device *device)
280 {
281         const char *path = connman_device_get_string(device, "Path");
282
283         DBG("device %p, path, %s", device, path);
284
285         return modem_change_online(path, TRUE);
286 }
287
288 static int modem_disable(struct connman_device *device)
289 {
290         const char *path = connman_device_get_string(device, "Path");
291
292         DBG("device %p path %s", device, path);
293
294         return modem_change_online(path, FALSE);
295 }
296
297 static struct connman_device_driver modem_driver = {
298         .name           = "modem",
299         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
300         .probe          = modem_probe,
301         .remove         = modem_remove,
302         .enable         = modem_enable,
303         .disable        = modem_disable,
304 };
305
306 static void modem_remove_device(struct modem_data *modem)
307 {
308         DBG("modem %p path %s device %p", modem, modem->path, modem->device);
309
310         if (modem->device == NULL)
311                 return;
312
313         connman_device_remove_all_networks(modem->device);
314         connman_device_unregister(modem->device);
315         connman_device_unref(modem->device);
316
317         modem->device = NULL;
318 }
319
320 static void remove_modem(gpointer data)
321 {
322         struct modem_data *modem = data;
323
324         modem_remove_device(modem);
325
326         g_free(modem->path);
327
328         g_free(modem);
329 }
330
331 static void remove_network(gpointer data)
332 {
333         connman_network_unref(data);
334 }
335
336 static char *get_ident(const char *path)
337 {
338         char *pos;
339
340         if (*path != '/')
341                 return NULL;
342
343         pos = strrchr(path, '/');
344         if (pos == NULL)
345                 return NULL;
346
347         return pos + 1;
348 }
349
350 static void create_service(struct connman_network *network)
351 {
352         const char *path;
353         char *group;
354
355         DBG("");
356
357         path = connman_network_get_string(network, "Path");
358
359         group = get_ident(path);
360
361         connman_network_set_group(network, group);
362 }
363
364 static int network_probe(struct connman_network *network)
365 {
366         create_service(network);
367         return 0;
368 }
369
370 static gboolean pending_network_is_available(struct connman_network *network)
371 {
372         /* Modem or network may be removed */
373         if (network == NULL || connman_network_get_device(network) == NULL) {
374                 DBG("Modem or network was removed");
375                 return FALSE;
376         }
377
378         return TRUE;
379 }
380
381 static void set_active_reply(DBusPendingCall *call, void *user_data)
382 {
383         char const *path = user_data;
384         DBusMessage *reply;
385         DBusError error;
386         struct connman_network *network;
387
388         network = g_hash_table_lookup(network_hash, path);
389
390         DBG("path %s network %p", path, network);
391
392         reply = dbus_pending_call_steal_reply(call);
393
394         if (!pending_network_is_available(network))
395                 goto done;
396
397         dbus_error_init(&error);
398
399         if (dbus_set_error_from_message(&error, reply)) {
400                 connman_error("SetProperty(Active) %s %s",
401                                 error.name, error.message);
402
403                 if (connman_network_get_index(network) < 0)
404                         connman_network_set_error(network,
405                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
406
407                 dbus_error_free(&error);
408         }
409
410 done:
411         dbus_message_unref(reply);
412
413         dbus_pending_call_unref(call);
414 }
415
416 static int set_network_active(struct connman_network *network,
417                                                 dbus_bool_t active)
418 {
419         int error;
420
421         const char *path = connman_network_get_string(network, "Path");
422
423         DBG("network %p, path %s, active %d", network, path, active);
424
425         error = set_property(path, OFONO_CONTEXT_INTERFACE,
426                                 "Active", DBUS_TYPE_BOOLEAN, &active,
427                                 set_active_reply, g_strdup(path), NULL);
428
429         if (active == FALSE && error == -EINPROGRESS)
430                 error = 0;
431
432         return error;
433 }
434
435 static void set_apn(struct connman_network *network)
436 {
437         const char *apn, *path;
438
439         apn = connman_network_get_string(network, "Cellular.APN");
440         if (apn == NULL)
441                 return;
442
443         path = connman_network_get_string(network, "Path");
444         if (path == NULL)
445                 return;
446
447         DBG("path %s, apn %s", path, apn);
448
449         set_property(path, OFONO_CONTEXT_INTERFACE,
450                         "AccessPointName", DBUS_TYPE_STRING, &apn,
451                         NULL, NULL, NULL);
452 }
453
454 static int network_connect(struct connman_network *network)
455 {
456         DBG("network %p", network);
457
458         if (connman_network_get_index(network) >= 0)
459                 return -EISCONN;
460
461         return set_network_active(network, TRUE);
462 }
463
464 static int network_disconnect(struct connman_network *network)
465 {
466         DBG("network %p", network);
467
468         if (connman_network_get_index(network) < 0)
469                 return -ENOTCONN;
470
471         return set_network_active(network, FALSE);
472 }
473
474 static void network_remove(struct connman_network *network)
475 {
476         char const *path = connman_network_get_string(network, "Path");
477
478         DBG("network %p path %s", network, path);
479
480         g_hash_table_remove(network_hash, path);
481 }
482
483 static int network_setup(struct connman_network *network, const char *key)
484 {
485         if (g_strcmp0(key, "Cellular.APN") == 0)
486                 set_apn(network);
487
488         return 0;
489 }
490
491 static struct connman_network_driver network_driver = {
492         .name           = "network",
493         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
494         .probe          = network_probe,
495         .remove         = network_remove,
496         .connect        = network_connect,
497         .disconnect     = network_disconnect,
498         .setup          = network_setup,
499 };
500
501 static void update_settings(DBusMessageIter *array,
502                                 struct connman_network *network);
503
504 static void set_connected(struct connman_network *network,
505                                 connman_bool_t connected);
506
507 static int add_network(struct connman_device *device,
508                         const char *path, DBusMessageIter *dict)
509 {
510         struct modem_data *modem = connman_device_get_data(device);
511         struct connman_network *network;
512         char *ident;
513         const char *hash_path;
514         char const *operator;
515         char const *reg_status;
516         dbus_bool_t active = FALSE;
517
518         DBG("modem %p device %p path %s", modem, device, path);
519
520         ident = get_ident(path);
521
522         network = connman_device_get_network(device, ident);
523         if (network != NULL)
524                 return -EALREADY;
525
526         network = g_hash_table_lookup(network_hash, path);
527         if (network != NULL) {
528                 DBG("path %p already exists with device %p", path,
529                         connman_network_get_device(network));
530                 if (connman_network_get_device(network))
531                         return -EALREADY;
532                 g_hash_table_remove(network_hash, path);
533         }
534
535         network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
536         if (network == NULL)
537                 return -ENOMEM;
538
539         connman_network_set_string(network, "Path", path);
540         hash_path = connman_network_get_string(network, "Path");
541         if (hash_path == NULL)
542                 goto error;
543         connman_network_ref(network);
544         g_hash_table_insert(network_hash, (char *)hash_path, network);
545
546         connman_network_set_available(network, TRUE);
547         connman_network_set_index(network, -1);
548
549         operator = connman_device_get_string(device, "Operator");
550         if (operator)
551                 connman_network_set_name(network, operator);
552
553         if (modem->has_strength)
554                 connman_network_set_strength(network, modem->strength);
555
556         reg_status = connman_device_get_string(device, "RegistrationStatus");
557
558         if (!g_strcmp0(reg_status, "roaming"))
559                 connman_network_set_roaming(network, TRUE);
560         else if (!g_strcmp0(reg_status, "registered"))
561                 connman_network_set_roaming(network, FALSE);
562
563         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
564                 DBusMessageIter entry, value;
565                 const char *key;
566
567                 dbus_message_iter_recurse(dict, &entry);
568                 dbus_message_iter_get_basic(&entry, &key);
569
570                 dbus_message_iter_next(&entry);
571                 dbus_message_iter_recurse(&entry, &value);
572
573                 if (g_str_equal(key, "Type")) {
574                         const char *type;
575
576                         dbus_message_iter_get_basic(&value, &type);
577                         if (g_strcmp0(type, "internet") == 0) {
578                                 connman_network_set_protocol(network,
579                                                 CONNMAN_NETWORK_PROTOCOL_IP);
580                         } else {
581                                 DBG("path %p type %s", path, type);
582                                 goto error;
583                         }
584                 } else if (g_str_equal(key, "AccessPointName")) {
585                         const char *ap;
586
587                         dbus_message_iter_get_basic(&value, &ap);
588
589                         connman_network_set_string(network, "Cellular.APN", ap);
590                 } else if (g_str_equal(key, "Settings"))
591                         update_settings(&value, network);
592                 else if (g_str_equal(key, "Active") == TRUE)
593                         dbus_message_iter_get_basic(&value, &active);
594
595                 dbus_message_iter_next(dict);
596         }
597
598         if (connman_device_add_network(device, network) != 0)
599                 goto error;
600
601         if (active)
602                 set_connected(network, active);
603
604         return 0;
605
606 error:
607         connman_network_unref(network);
608         g_hash_table_remove(network_hash, hash_path);
609         return -EIO;
610 }
611
612 static void check_networks_reply(DBusPendingCall *call, void *user_data)
613 {
614         char *path = user_data;
615         struct modem_data *modem;
616         DBusMessage *reply;
617         DBusMessageIter array, entry, value, properties;
618
619         DBG("path %s", path);
620
621         modem = g_hash_table_lookup(modem_hash, path);
622         if (modem == NULL)
623                 return;
624         if (modem->device == NULL)
625                 return;
626
627         reply = dbus_pending_call_steal_reply(call);
628
629         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
630                 goto done;
631
632         dbus_message_iter_init(reply, &array);
633
634         dbus_message_iter_recurse(&array, &entry);
635
636         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
637                 char const *network_path;
638
639                 dbus_message_iter_recurse(&entry, &value);
640                 dbus_message_iter_get_basic(&value, &network_path);
641
642                 dbus_message_iter_next(&value);
643                 dbus_message_iter_recurse(&value, &properties);
644
645                 add_network(modem->device, network_path, &properties);
646
647                 dbus_message_iter_next(&entry);
648         }
649
650 done:
651         dbus_message_unref(reply);
652
653         dbus_pending_call_unref(call);
654 }
655
656 static void check_networks(struct modem_data *modem)
657 {
658         char const *path = modem->path;
659
660         DBG("modem %p path %s", modem, path);
661
662         call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
663                         check_networks_reply, g_strdup(path), g_free,
664                         DBUS_TYPE_INVALID);
665 }
666
667 static void modem_operator_name_changed(struct modem_data *modem,
668                                         char const *name)
669 {
670         struct connman_device *device = modem->device;
671         GHashTableIter i;
672         gpointer value;
673
674         if (device == NULL)
675                 return;
676
677         connman_device_set_string(device, "Operator", name);
678
679         for (g_hash_table_iter_init(&i, network_hash);
680              g_hash_table_iter_next(&i, NULL, &value);) {
681                 struct connman_network *network = value;
682
683                 if (connman_network_get_device(network) == device) {
684                         connman_network_set_name(network, name);
685                         connman_network_update(network);
686                 }
687         }
688 }
689
690 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
691 {
692         struct connman_device *device = modem->device;
693         GHashTableIter i;
694         gpointer value;
695
696         modem->strength = strength;
697         modem->has_strength = TRUE;
698
699         if (device == NULL)
700                 return;
701
702         for (g_hash_table_iter_init(&i, network_hash);
703              g_hash_table_iter_next(&i, NULL, &value);) {
704                 struct connman_network *network = value;
705
706                 if (connman_network_get_device(network) == device) {
707                         connman_network_set_strength(network, strength);
708                         connman_network_update(network);
709                 }
710         }
711 }
712
713 static void modem_roaming_changed(struct modem_data *modem,
714                                         char const *status)
715 {
716         struct connman_device *device = modem->device;
717         connman_bool_t roaming;
718         GHashTableIter i;
719         gpointer value;
720
721         if (device == NULL)
722                 return;
723
724         connman_device_set_string(device, "RegistrationStatus", status);
725
726         if (g_str_equal(status, "roaming"))
727                 roaming = TRUE;
728         else if (g_str_equal(status, "registered"))
729                 roaming = FALSE;
730         else
731                 return;
732
733         for (g_hash_table_iter_init(&i, network_hash);
734              g_hash_table_iter_next(&i, NULL, &value);) {
735                 struct connman_network *network = value;
736
737                 if (connman_network_get_device(network) == device) {
738                         connman_network_set_roaming(network, roaming);
739                         connman_network_update(network);
740                 }
741         }
742 }
743
744 static void modem_registration_changed(struct modem_data *modem,
745                                         DBusMessageIter *entry)
746 {
747         DBusMessageIter iter;
748         const char *key;
749         int type;
750         struct {
751                 char const *str;
752                 connman_uint8_t byte;
753         } value;
754
755         dbus_message_iter_get_basic(entry, &key);
756
757         DBG("key %s", key);
758
759         dbus_message_iter_next(entry);
760
761         dbus_message_iter_recurse(entry, &iter);
762
763         type = dbus_message_iter_get_arg_type(&iter);
764         if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
765                 return;
766
767         dbus_message_iter_get_basic(&iter, &value);
768
769         if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING)
770                 modem_operator_name_changed(modem, value.str);
771         else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE)
772                 modem_strength_changed(modem, value.byte);
773         else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING)
774                 modem_roaming_changed(modem, value.str);
775
776 }
777
778 static gboolean reg_changed(DBusConnection *connection,
779                                 DBusMessage *message, void *user_data)
780 {
781         const char *path = dbus_message_get_path(message);
782         struct modem_data *modem;
783         DBusMessageIter iter;
784
785         DBG("path %s", path);
786
787         modem = g_hash_table_lookup(modem_hash, path);
788         if (modem == NULL)
789                 return TRUE;
790
791         if (dbus_message_iter_init(message, &iter))
792                 modem_registration_changed(modem, &iter);
793
794         return TRUE;
795 }
796
797 static void check_registration_reply(DBusPendingCall *call, void *user_data)
798 {
799         char const *path = user_data;
800         struct modem_data *modem;
801         DBusMessage *reply;
802         DBusMessageIter array, dict, entry;
803
804         DBG("path %s", path);
805
806         modem = g_hash_table_lookup(modem_hash, path);
807         if (modem == NULL)
808                 return;
809
810         reply = dbus_pending_call_steal_reply(call);
811
812         if (dbus_message_iter_init(reply, &array) == FALSE)
813                 goto done;
814
815         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
816                 goto done;
817
818         dbus_message_iter_recurse(&array, &dict);
819         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
820                 dbus_message_iter_recurse(&dict, &entry);
821                 modem_registration_changed(modem, &entry);
822                 dbus_message_iter_next(&dict);
823         }
824
825 done:
826         dbus_message_unref(reply);
827
828         dbus_pending_call_unref(call);
829 }
830
831 static void check_registration(struct modem_data *modem)
832 {
833         char const *path = modem->path;
834
835         DBG("modem %p path %s", modem, path);
836
837         call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
838                         check_registration_reply, g_strdup(path), g_free,
839                         DBUS_TYPE_INVALID);
840 }
841
842 static void add_device(const char *path, const char *imsi)
843 {
844         struct modem_data *modem;
845         struct connman_device *device;
846
847         DBG("path %s imsi %s", path, imsi);
848
849         if (path == NULL)
850                 return;
851
852         if (imsi == NULL)
853                 return;
854
855         modem = g_hash_table_lookup(modem_hash, path);
856         if (modem == NULL)
857                 return;
858
859         if (modem->device) {
860                 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
861                         return;
862
863                 modem_remove_device(modem);
864         }
865
866         if (strlen(imsi) == 0)
867                 return;
868
869         device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
870         if (device == NULL)
871                 return;
872
873         connman_device_set_ident(device, imsi);
874
875         connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
876
877         connman_device_set_string(device, "Path", path);
878
879         connman_device_set_data(device, modem);
880
881         if (connman_device_register(device) < 0) {
882                 connman_device_unref(device);
883                 return;
884         }
885
886         modem->device = device;
887
888         if (modem->has_reg)
889                 check_registration(modem);
890         if (modem->has_gprs)
891                 check_networks(modem);
892 }
893
894 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
895 {
896         const char *path = user_data;
897         const char *imsi = NULL;
898         DBusMessage *reply;
899         DBusMessageIter array, dict;
900
901         DBG("path %s", path);
902
903         reply = dbus_pending_call_steal_reply(call);
904
905         if (dbus_message_iter_init(reply, &array) == FALSE)
906                 goto done;
907
908         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
909                 goto done;
910
911         dbus_message_iter_recurse(&array, &dict);
912
913         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
914                 DBusMessageIter entry, value;
915                 const char *key;
916
917                 dbus_message_iter_recurse(&dict, &entry);
918                 dbus_message_iter_get_basic(&entry, &key);
919
920                 dbus_message_iter_next(&entry);
921                 dbus_message_iter_recurse(&entry, &value);
922
923                 if (g_str_equal(key, "SubscriberIdentity")) {
924                         dbus_message_iter_get_basic(&value, &imsi);
925                         add_device(path, imsi);
926                 }
927
928                 dbus_message_iter_next(&dict);
929         }
930
931 done:
932         dbus_message_unref(reply);
933
934         dbus_pending_call_unref(call);
935 }
936
937 static void get_imsi(const char *path)
938 {
939         DBG("path %s", path);
940
941         call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
942                         sim_properties_reply, g_strdup(path), g_free,
943                         DBUS_TYPE_INVALID);
944 }
945
946 static int gprs_change_powered(const char *path, dbus_bool_t powered)
947 {
948         DBG("path %s powered %d", path, powered);
949
950         return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
951                                 DBUS_TYPE_BOOLEAN, &powered,
952                                 NULL, NULL, NULL);
953 }
954
955 static int modem_change_powered(const char *path, dbus_bool_t powered)
956 {
957         DBG("path %s powered %d", path, powered);
958
959         return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
960                                 DBUS_TYPE_BOOLEAN, &powered,
961                                 NULL, NULL, NULL);
962 }
963
964
965 static gboolean modem_has_interface(DBusMessageIter *array,
966                                         char const *interface)
967 {
968         DBusMessageIter entry;
969
970         dbus_message_iter_recurse(array, &entry);
971
972         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
973                 const char *element;
974
975                 dbus_message_iter_get_basic(&entry, &element);
976
977                 if (g_strcmp0(interface, element) == 0)
978                         return TRUE;
979
980                 dbus_message_iter_next(&entry);
981         }
982
983         return FALSE;
984 }
985
986 static gboolean modem_has_sim(DBusMessageIter *array)
987 {
988         return modem_has_interface(array, OFONO_SIM_INTERFACE);
989 }
990
991 static gboolean modem_has_reg(DBusMessageIter *array)
992 {
993         return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
994 }
995
996 static gboolean modem_has_gprs(DBusMessageIter *array)
997 {
998         return modem_has_interface(array, OFONO_GPRS_INTERFACE);
999 }
1000
1001 static void add_modem(const char *path, DBusMessageIter *properties)
1002 {
1003         struct modem_data *modem;
1004         DBusMessageIter dict;
1005         dbus_bool_t powered = FALSE;
1006         dbus_bool_t online = FALSE;
1007         dbus_bool_t has_online = FALSE;
1008         gboolean has_sim = FALSE;
1009         gboolean has_reg = FALSE;
1010         gboolean has_gprs = FALSE;
1011
1012         modem = g_hash_table_lookup(modem_hash, path);
1013
1014         if (modem != NULL)
1015                 return;
1016
1017         modem = g_try_new0(struct modem_data, 1);
1018         if (modem == NULL)
1019                 return;
1020
1021         modem->path = g_strdup(path);
1022         modem->device = NULL;
1023         modem->available = TRUE;
1024
1025         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1026
1027         dbus_message_iter_recurse(properties, &dict);
1028
1029         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1030                 DBusMessageIter entry, value;
1031                 const char *key;
1032
1033                 dbus_message_iter_recurse(&dict, &entry);
1034                 dbus_message_iter_get_basic(&entry, &key);
1035
1036                 dbus_message_iter_next(&entry);
1037                 dbus_message_iter_recurse(&entry, &value);
1038
1039                 if (g_str_equal(key, "Powered") == TRUE)
1040                         dbus_message_iter_get_basic(&value, &powered);
1041                 else if (g_str_equal(key, "Online") == TRUE) {
1042                         has_online = TRUE;
1043                         dbus_message_iter_get_basic(&value, &online);
1044                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1045                         has_sim = modem_has_sim(&value);
1046                         has_reg = modem_has_reg(&value);
1047                         has_gprs = modem_has_gprs(&value);
1048                 }
1049
1050                 dbus_message_iter_next(&dict);
1051         }
1052
1053         if (!powered)
1054                 modem_change_powered(path, TRUE);
1055
1056         modem->has_sim = has_sim;
1057         modem->has_reg = has_reg;
1058         modem->has_gprs = has_gprs;
1059
1060         update_modem_online(modem, online);
1061
1062         if (has_sim)
1063                 get_imsi(path);
1064 }
1065
1066 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1067 {
1068         DBusMessage *reply;
1069         DBusError error;
1070         DBusMessageIter array, dict;
1071
1072         DBG("");
1073
1074         reply = dbus_pending_call_steal_reply(call);
1075
1076         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1077                 goto done;
1078
1079         dbus_error_init(&error);
1080
1081         if (dbus_set_error_from_message(&error, reply)) {
1082                 connman_error("ModemManager.GetModems() %s %s",
1083                                 error.name, error.message);
1084                 dbus_error_free(&error);
1085                 goto done;
1086         }
1087
1088         if (dbus_message_iter_init(reply, &array) == FALSE)
1089                 goto done;
1090
1091         dbus_message_iter_recurse(&array, &dict);
1092
1093         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1094                 DBusMessageIter value, properties;
1095                 const char *modem_path;
1096
1097                 dbus_message_iter_recurse(&dict, &value);
1098                 dbus_message_iter_get_basic(&value, &modem_path);
1099
1100                 dbus_message_iter_next(&value);
1101                 dbus_message_iter_recurse(&value, &properties);
1102
1103                 /* Add modem */
1104                 add_modem(modem_path, &properties);
1105
1106                 dbus_message_iter_next(&dict);
1107         }
1108
1109 done:
1110         dbus_message_unref(reply);
1111
1112         dbus_pending_call_unref(call);
1113 }
1114
1115 static void ofono_connect(DBusConnection *connection, void *user_data)
1116 {
1117         DBG("connection %p", connection);
1118
1119         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1120                                                 g_free, remove_modem);
1121
1122         network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1123                                                 NULL, remove_network);
1124
1125         call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1126                         manager_modems_reply, NULL, NULL,
1127                         DBUS_TYPE_INVALID);
1128 }
1129
1130 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1131 {
1132         DBG("connection %p", connection);
1133
1134         if (modem_hash == NULL)
1135                 return;
1136
1137         g_hash_table_destroy(modem_hash);
1138
1139         modem_hash = NULL;
1140 }
1141
1142 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1143                                 void *user_data)
1144 {
1145         const char *path = dbus_message_get_path(message);
1146         struct modem_data *modem;
1147         DBusMessageIter iter, value;
1148         const char *key;
1149
1150         DBG("path %s", path);
1151
1152         modem = g_hash_table_lookup(modem_hash, path);
1153         if (modem == NULL)
1154                 return TRUE;
1155
1156         if (dbus_message_iter_init(message, &iter) == FALSE)
1157                 return TRUE;
1158
1159         dbus_message_iter_get_basic(&iter, &key);
1160
1161         dbus_message_iter_next(&iter);
1162         dbus_message_iter_recurse(&iter, &value);
1163
1164         if (g_str_equal(key, "Powered") == TRUE) {
1165                 dbus_bool_t powered;
1166
1167                 dbus_message_iter_get_basic(&value, &powered);
1168                 if (powered == TRUE)
1169                         return TRUE;
1170
1171                 modem->has_sim = FALSE;
1172                 modem->has_reg = FALSE;
1173                 modem->has_gprs = FALSE;
1174
1175                 modem_remove_device(modem);
1176         } else if (g_str_equal(key, "Online") == TRUE) {
1177                 dbus_bool_t online;
1178
1179                 dbus_message_iter_get_basic(&value, &online);
1180
1181                 update_modem_online(modem, online);
1182         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1183                 gboolean has_sim = modem_has_sim(&value);
1184                 gboolean has_reg = modem_has_reg(&value);
1185                 gboolean added_reg = has_reg && !modem->has_reg;
1186                 gboolean has_gprs = modem_has_gprs(&value);
1187                 gboolean added_gprs = has_gprs && !modem->has_gprs;
1188
1189                 modem->has_sim = has_sim;
1190                 modem->has_reg = has_reg;
1191                 modem->has_gprs = has_gprs;
1192
1193                 if (modem->device == NULL) {
1194                         if (has_sim)
1195                                 get_imsi(modem->path);
1196                 } else if (!has_sim) {
1197                         modem_remove_device(modem);
1198                 } else {
1199                         if (added_reg)
1200                                 check_registration(modem);
1201                         if (added_gprs)
1202                                 gprs_change_powered(modem->path, TRUE);
1203                 }
1204         }
1205
1206         return TRUE;
1207 }
1208
1209 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1210                                 void *user_data)
1211 {
1212         const char *path = dbus_message_get_path(message);
1213         struct modem_data *modem;
1214         DBusMessageIter iter, value;
1215         const char *key;
1216
1217         DBG("path %s", path);
1218
1219         modem = g_hash_table_lookup(modem_hash, path);
1220         if (modem == NULL)
1221                 return TRUE;
1222
1223         if (dbus_message_iter_init(message, &iter) == FALSE)
1224                 return TRUE;
1225
1226         dbus_message_iter_get_basic(&iter, &key);
1227
1228         dbus_message_iter_next(&iter);
1229         dbus_message_iter_recurse(&iter, &value);
1230
1231         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1232                 char *imsi;
1233
1234                 dbus_message_iter_get_basic(&value, &imsi);
1235
1236                 add_device(path, imsi);
1237         } else if (g_str_equal(key, "Present") == TRUE) {
1238                 dbus_bool_t present;
1239
1240                 dbus_message_iter_get_basic(&value, &present);
1241
1242                 if (present)
1243                         return TRUE;
1244
1245                 if (modem->device != NULL)
1246                         modem_remove_device(modem);
1247
1248                 modem->has_gprs = FALSE;
1249                 modem->has_reg = FALSE;
1250         }
1251
1252         return TRUE;
1253 }
1254
1255 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1256                                 void *user_data)
1257 {
1258         const char *path = dbus_message_get_path(message);
1259         struct modem_data *modem;
1260         DBusMessageIter iter, value;
1261         const char *key;
1262
1263         DBG("path %s", path);
1264
1265         modem = g_hash_table_lookup(modem_hash, path);
1266         if (modem == NULL)
1267                 return TRUE;
1268
1269         if (dbus_message_iter_init(message, &iter) == FALSE)
1270                 return TRUE;
1271
1272         dbus_message_iter_get_basic(&iter, &key);
1273
1274         dbus_message_iter_next(&iter);
1275         dbus_message_iter_recurse(&iter, &value);
1276
1277         if (g_str_equal(key, "Attached") == TRUE) {
1278                 dbus_bool_t attached;
1279
1280                 dbus_message_iter_get_basic(&value, &attached);
1281
1282                 DBG("Attached %d", attached);
1283
1284                 if (attached == TRUE)
1285                         check_networks(modem);
1286                 else if (modem->device != NULL)
1287                         connman_device_remove_all_networks(modem->device);
1288
1289         }
1290
1291         return TRUE;
1292 }
1293
1294 static gboolean modem_added(DBusConnection *connection,
1295                                 DBusMessage *message, void *user_data)
1296 {
1297         DBusMessageIter iter, properties;
1298         const char *modem_path;
1299
1300         DBG("");
1301
1302         if (dbus_message_iter_init(message, &iter) == FALSE)
1303                 return TRUE;
1304
1305         dbus_message_iter_get_basic(&iter, &modem_path);
1306
1307         dbus_message_iter_next(&iter);
1308         dbus_message_iter_recurse(&iter, &properties);
1309
1310         add_modem(modem_path, &properties);
1311
1312         return TRUE;
1313 }
1314
1315 static gboolean modem_removed(DBusConnection *connection,
1316                                 DBusMessage *message, void *user_data)
1317 {
1318         DBusMessageIter iter;
1319         const char *modem_path;
1320
1321         DBG("");
1322
1323         if (dbus_message_iter_init(message, &iter) == FALSE)
1324                 return TRUE;
1325
1326         dbus_message_iter_get_basic(&iter, &modem_path);
1327
1328         g_hash_table_remove(modem_hash, modem_path);
1329
1330         return TRUE;
1331 }
1332
1333
1334 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1335 {
1336         DBusMessageIter entry;
1337         gchar *nameserver = NULL, *nameserver_old = NULL;
1338
1339         DBG("");
1340
1341         dbus_message_iter_recurse(array, &entry);
1342
1343         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1344                 const char *dns;
1345
1346                 dbus_message_iter_get_basic(&entry, &dns);
1347
1348                 DBG("dns %s", dns);
1349
1350                 if (nameserver == NULL) {
1351
1352                         nameserver = g_strdup(dns);
1353                 } else {
1354
1355                         nameserver_old = nameserver;
1356                         nameserver = g_strdup_printf("%s %s",
1357                                                 nameserver_old, dns);
1358                         g_free(nameserver_old);
1359                 }
1360
1361                 dbus_message_iter_next(&entry);
1362         }
1363
1364         parent->ipv4.nameserver = nameserver;
1365 }
1366
1367 static void update_settings(DBusMessageIter *array,
1368                                 struct connman_network *network)
1369 {
1370         struct connman_element *parent = connman_network_get_element(network);
1371         DBusMessageIter dict;
1372         const char *interface = NULL;
1373
1374         DBG("network %p", network);
1375
1376         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1377                 return;
1378
1379         dbus_message_iter_recurse(array, &dict);
1380
1381         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1382                 DBusMessageIter entry, value;
1383                 const char *key;
1384
1385                 dbus_message_iter_recurse(&dict, &entry);
1386                 dbus_message_iter_get_basic(&entry, &key);
1387
1388                 DBG("key %s", key);
1389
1390                 dbus_message_iter_next(&entry);
1391                 dbus_message_iter_recurse(&entry, &value);
1392
1393                 if (g_str_equal(key, "Interface") == TRUE) {
1394                         int index;
1395
1396                         dbus_message_iter_get_basic(&value, &interface);
1397
1398                         DBG("interface %s", interface);
1399
1400                         index = connman_inet_ifindex(interface);
1401                         if (index >= 0) {
1402                                 connman_network_set_index(network, index);
1403                         } else {
1404                                 connman_error("Can not find interface %s",
1405                                                                 interface);
1406                                 break;
1407                         }
1408                 } else if (g_str_equal(key, "Method") == TRUE) {
1409                         const char *method;
1410
1411                         dbus_message_iter_get_basic(&value, &method);
1412                         if (g_strcmp0(method, "static") == 0) {
1413
1414                                 parent->ipv4.method =
1415                                         CONNMAN_IPCONFIG_METHOD_FIXED;
1416                         } else if (g_strcmp0(method, "dhcp") == 0) {
1417
1418                                 parent->ipv4.method =
1419                                         CONNMAN_IPCONFIG_METHOD_DHCP;
1420                                 break;
1421                         }
1422                 } else if (g_str_equal(key, "Address") == TRUE) {
1423                         const char *address;
1424
1425                         dbus_message_iter_get_basic(&value, &address);
1426
1427                         DBG("address %s", address);
1428
1429                         parent->ipv4.address = g_strdup(address);
1430                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1431                         const char *netmask;
1432
1433                         dbus_message_iter_get_basic(&value, &netmask);
1434
1435                         DBG("netmask %s", netmask);
1436
1437                         parent->ipv4.netmask = g_strdup(netmask);
1438                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1439
1440                         get_dns(&value, parent);
1441                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1442                         const char *gateway;
1443
1444                         dbus_message_iter_get_basic(&value, &gateway);
1445
1446                         DBG("gateway %s", gateway);
1447
1448                         parent->ipv4.gateway = g_strdup(gateway);
1449                 }
1450
1451                 dbus_message_iter_next(&dict);
1452         }
1453
1454         /* deactive, oFono send NULL inteface before deactive signal */
1455         if (interface == NULL)
1456                 connman_network_set_index(network, -1);
1457 }
1458
1459 static void cleanup_ipconfig(struct connman_network *network)
1460 {
1461         struct connman_element *parent = connman_network_get_element(network);
1462
1463         g_free(parent->ipv4.address);
1464         parent->ipv4.address = NULL;
1465
1466         g_free(parent->ipv4.netmask);
1467         parent->ipv4.netmask = NULL;
1468
1469         g_free(parent->ipv4.nameserver);
1470         parent->ipv4.nameserver = NULL;
1471
1472         g_free(parent->ipv4.gateway);
1473         parent->ipv4.gateway = NULL;
1474
1475         parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1476 }
1477
1478
1479 static void set_connected(struct connman_network *network,
1480                                 connman_bool_t connected)
1481 {
1482         struct connman_element *parent = connman_network_get_element(network);
1483         enum connman_ipconfig_method method = parent->ipv4.method;
1484
1485         switch (method) {
1486         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1487         case CONNMAN_IPCONFIG_METHOD_OFF:
1488         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1489                 return;
1490
1491         case CONNMAN_IPCONFIG_METHOD_FIXED:
1492                 connman_network_set_method(network, method);
1493
1494                 if (connected == FALSE)
1495                         cleanup_ipconfig(network);
1496
1497                 connman_network_set_connected(network, connected);
1498                 break;
1499
1500         case CONNMAN_IPCONFIG_METHOD_DHCP:
1501                 connman_network_set_method(network, method);
1502
1503                 connman_network_set_connected(network, connected);
1504                 break;
1505         }
1506 }
1507
1508 static gboolean context_changed(DBusConnection *connection,
1509                                         DBusMessage *message, void *user_data)
1510 {
1511         const char *path = dbus_message_get_path(message);
1512         struct connman_network *network;
1513         DBusMessageIter iter, value;
1514         const char *key;
1515
1516         DBG("path %s", path);
1517
1518         network = g_hash_table_lookup(network_hash, path);
1519         if (network == NULL)
1520                 return TRUE;
1521
1522         if (!pending_network_is_available(network)) {
1523                 remove_network(network);
1524                 return TRUE;
1525         }
1526
1527         if (dbus_message_iter_init(message, &iter) == FALSE)
1528                 return TRUE;
1529
1530         dbus_message_iter_get_basic(&iter, &key);
1531
1532         dbus_message_iter_next(&iter);
1533         dbus_message_iter_recurse(&iter, &value);
1534
1535         if (g_str_equal(key, "Settings") == TRUE)
1536                 update_settings(&value, network);
1537         else if (g_str_equal(key, "Active") == TRUE) {
1538                 dbus_bool_t active;
1539
1540                 dbus_message_iter_get_basic(&value, &active);
1541
1542                 set_connected(network, active);
1543         }
1544
1545         return TRUE;
1546 }
1547
1548 static guint watch;
1549 static guint reg_watch;
1550 static guint sim_watch;
1551 static guint gprs_watch;
1552 static guint modem_watch;
1553 static guint modem_added_watch;
1554 static guint modem_removed_watch;
1555 static guint context_watch;
1556
1557 static int ofono_init(void)
1558 {
1559         int err;
1560
1561         connection = connman_dbus_get_connection();
1562         if (connection == NULL)
1563                 return -EIO;
1564
1565         watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1566                         ofono_connect, ofono_disconnect, NULL, NULL);
1567
1568         reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1569                                                 OFONO_REGISTRATION_INTERFACE,
1570                                                 PROPERTY_CHANGED,
1571                                                 reg_changed,
1572                                                 NULL, NULL);
1573
1574         gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1575                                                 OFONO_GPRS_INTERFACE,
1576                                                 PROPERTY_CHANGED,
1577                                                 gprs_changed,
1578                                                 NULL, NULL);
1579
1580         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1581                                                 OFONO_MODEM_INTERFACE,
1582                                                 PROPERTY_CHANGED,
1583                                                 modem_changed,
1584                                                 NULL, NULL);
1585
1586         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1587                                                 OFONO_SIM_INTERFACE,
1588                                                 PROPERTY_CHANGED,
1589                                                 sim_changed,
1590                                                 NULL, NULL);
1591
1592         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1593                                                 OFONO_MANAGER_INTERFACE,
1594                                                 MODEM_ADDED,
1595                                                 modem_added,
1596                                                 NULL, NULL);
1597
1598         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1599                                                 OFONO_MANAGER_INTERFACE,
1600                                                 MODEM_REMOVED,
1601                                                 modem_removed,
1602                                                 NULL, NULL);
1603
1604         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1605                                                 OFONO_CONTEXT_INTERFACE,
1606                                                 PROPERTY_CHANGED,
1607                                                 context_changed,
1608                                                 NULL, NULL);
1609
1610         if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1611                         reg_watch == 0 || sim_watch == 0 ||
1612                         modem_added_watch == 0 || modem_removed_watch == 0 ||
1613                                 context_watch == 0) {
1614                 err = -EIO;
1615                 goto remove;
1616         }
1617
1618         err = connman_network_driver_register(&network_driver);
1619         if (err < 0)
1620                 goto remove;
1621
1622         err = connman_device_driver_register(&modem_driver);
1623         if (err < 0) {
1624                 connman_network_driver_unregister(&network_driver);
1625                 goto remove;
1626         }
1627
1628         return 0;
1629
1630 remove:
1631         g_dbus_remove_watch(connection, watch);
1632         g_dbus_remove_watch(connection, sim_watch);
1633         g_dbus_remove_watch(connection, reg_watch);
1634         g_dbus_remove_watch(connection, gprs_watch);
1635         g_dbus_remove_watch(connection, modem_watch);
1636         g_dbus_remove_watch(connection, modem_added_watch);
1637         g_dbus_remove_watch(connection, modem_removed_watch);
1638         g_dbus_remove_watch(connection, context_watch);
1639
1640         dbus_connection_unref(connection);
1641
1642         return err;
1643 }
1644
1645 static void ofono_exit(void)
1646 {
1647         g_dbus_remove_watch(connection, watch);
1648         g_dbus_remove_watch(connection, sim_watch);
1649         g_dbus_remove_watch(connection, reg_watch);
1650         g_dbus_remove_watch(connection, gprs_watch);
1651         g_dbus_remove_watch(connection, modem_watch);
1652         g_dbus_remove_watch(connection, modem_added_watch);
1653         g_dbus_remove_watch(connection, modem_removed_watch);
1654         g_dbus_remove_watch(connection, context_watch);
1655
1656         ofono_disconnect(connection, NULL);
1657
1658         connman_device_driver_unregister(&modem_driver);
1659         connman_network_driver_unregister(&network_driver);
1660
1661         dbus_connection_unref(connection);
1662 }
1663
1664 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1665                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)