ofono: Add cdma-netreg relative functions
[platform/upstream/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  *  Copyright (C) 2011  BWM Car IT GmbH. All rights reserved.
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <errno.h>
29 #include <stdlib.h>
30
31 #include <gdbus.h>
32 #include <string.h>
33 #include <stdint.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/network.h>
39 #include <connman/inet.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
42
43 #define uninitialized_var(x) x = x
44
45 #define OFONO_SERVICE                   "org.ofono"
46
47 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
49 #define OFONO_SIM_INTERFACE             OFONO_SERVICE ".SimManager"
50 #define OFONO_NETREG_INTERFACE          OFONO_SERVICE ".NetworkRegistration"
51 #define OFONO_CM_INTERFACE              OFONO_SERVICE ".ConnectionManager"
52 #define OFONO_CONTEXT_INTERFACE         OFONO_SERVICE ".ConnectionContext"
53 #define OFONO_CDMA_CM_INTERFACE         OFONO_SERVICE ".cdma.ConnectionManager"
54 #define OFONO_CDMA_NETREG_INTERFACE     OFONO_SERVICE ".cdma.NetworkRegistration"
55
56 #define MODEM_ADDED                     "ModemAdded"
57 #define MODEM_REMOVED                   "ModemRemoved"
58 #define PROPERTY_CHANGED                "PropertyChanged"
59 #define CONTEXT_ADDED                   "ContextAdded"
60 #define CONTEXT_REMOVED                 "ContextRemoved"
61
62 #define GET_PROPERTIES                  "GetProperties"
63 #define SET_PROPERTY                    "SetProperty"
64 #define GET_MODEMS                      "GetModems"
65 #define GET_CONTEXTS                    "GetContexts"
66
67 #define TIMEOUT 40000
68
69 enum ofono_api {
70         OFONO_API_SIM =         0x1,
71         OFONO_API_NETREG =      0x2,
72         OFONO_API_CM =          0x4,
73         OFONO_API_CDMA_NETREG = 0x8,
74         OFONO_API_CDMA_CM =     0x10,
75 };
76
77 /*
78  * The way this plugin works is following:
79  *
80  *   powered -> SubscriberIdentity or Online = True -> gprs, context ->
81  *     attached -> netreg -> ready
82  *
83  * Enabling and disabling modems are steered through the rfkill
84  * interface. That means when ConnMan toggles the rfkill bit oFono
85  * will add or remove the modems.
86  *
87  * ConnMan will always power up (set Powered and Online) the
88  * modems. No need to power them down because this will be done
89  * through the rfkill inteface.
90  */
91
92 static DBusConnection *connection;
93
94 static GHashTable *modem_hash;
95 static GHashTable *context_hash;
96
97 struct network_context {
98         char *path;
99         int index;
100
101         enum connman_ipconfig_method ipv4_method;
102         struct connman_ipaddress *ipv4_address;
103         char *ipv4_nameservers;
104
105         enum connman_ipconfig_method ipv6_method;
106         struct connman_ipaddress *ipv6_address;
107         char *ipv6_nameservers;
108 };
109
110 struct modem_data {
111         char *path;
112
113         struct connman_device *device;
114         struct connman_network *network;
115
116         struct network_context *context;
117
118         /* Modem Interface */
119         char *serial;
120         connman_bool_t powered;
121         connman_bool_t online;
122         uint8_t interfaces;
123         connman_bool_t ignore;
124
125         connman_bool_t set_powered;
126         connman_bool_t set_online;
127
128         /* CDMA ConnectionManager Interface */
129         connman_bool_t cdma_cm_powered;
130
131         /* ConnectionManager Interface */
132         connman_bool_t attached;
133         connman_bool_t cm_powered;
134
135         connman_bool_t set_cm_powered;
136
137         /* ConnectionContext Interface */
138         connman_bool_t active;
139         connman_bool_t set_active;
140
141         /* SimManager Interface */
142         char *imsi;
143
144         /* Netreg Interface */
145         char *name;
146         uint8_t strength;
147         uint8_t data_strength; /* 1xEVDO signal strength */
148         connman_bool_t roaming;
149
150         /* pending calls */
151         DBusPendingCall *call_set_property;
152         DBusPendingCall *call_get_properties;
153         DBusPendingCall *call_get_contexts;
154 };
155
156 static char *get_ident(const char *path)
157 {
158         char *pos;
159
160         if (*path != '/')
161                 return NULL;
162
163         pos = strrchr(path, '/');
164         if (pos == NULL)
165                 return NULL;
166
167         return pos + 1;
168 }
169
170 static struct network_context *network_context_alloc(const char *path)
171 {
172         struct network_context *context;
173
174         context = g_try_new0(struct network_context, 1);
175         if (context == NULL)
176                 return NULL;
177
178         context->path = g_strdup(path);
179         context->index = -1;
180
181         context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
182         context->ipv4_address = NULL;
183         context->ipv4_nameservers = NULL;
184
185         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
186         context->ipv6_address = NULL;
187         context->ipv6_nameservers = NULL;
188
189         return context;
190 }
191
192 static void network_context_free(struct network_context *context)
193 {
194         g_free(context->path);
195
196         connman_ipaddress_free(context->ipv4_address);
197         g_free(context->ipv4_nameservers);
198
199         connman_ipaddress_free(context->ipv6_address);
200         g_free(context->ipv6_nameservers);
201
202         free(context);
203 }
204
205 static void set_connected(struct modem_data *modem)
206 {
207         connman_bool_t setip = FALSE;
208
209         DBG("%s", modem->path);
210
211         connman_network_set_index(modem->network, modem->context->index);
212
213         switch (modem->context->ipv4_method) {
214         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
215         case CONNMAN_IPCONFIG_METHOD_OFF:
216         case CONNMAN_IPCONFIG_METHOD_MANUAL:
217         case CONNMAN_IPCONFIG_METHOD_AUTO:
218                 break;
219
220         case CONNMAN_IPCONFIG_METHOD_FIXED:
221                 connman_network_set_ipv4_method(modem->network,
222                                                 modem->context->ipv4_method);
223                 connman_network_set_ipaddress(modem->network,
224                                                 modem->context->ipv4_address);
225                 connman_network_set_nameservers(modem->network,
226                                         modem->context->ipv4_nameservers);
227                 setip = TRUE;
228                 break;
229
230         case CONNMAN_IPCONFIG_METHOD_DHCP:
231                 connman_network_set_ipv4_method(modem->network,
232                                                 modem->context->ipv4_method);
233                 setip = TRUE;
234                 break;
235         }
236
237         switch (modem->context->ipv6_method) {
238         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
239         case CONNMAN_IPCONFIG_METHOD_OFF:
240         case CONNMAN_IPCONFIG_METHOD_MANUAL:
241         case CONNMAN_IPCONFIG_METHOD_DHCP:
242         case CONNMAN_IPCONFIG_METHOD_AUTO:
243                 break;
244
245         case CONNMAN_IPCONFIG_METHOD_FIXED:
246                 connman_network_set_ipv6_method(modem->network,
247                                                         modem->context->ipv6_method);
248                 connman_network_set_ipaddress(modem->network,
249                                                         modem->context->ipv6_address);
250                 setip = TRUE;
251                 break;
252         }
253
254         if (setip == TRUE)
255                 connman_network_set_connected(modem->network, TRUE);
256 }
257
258 static void set_disconnected(struct modem_data *modem)
259 {
260         DBG("%s", modem->path);
261
262         connman_network_set_connected(modem->network, FALSE);
263 }
264
265 typedef void (*set_property_cb)(struct modem_data *data,
266                                 connman_bool_t success);
267 typedef void (*get_properties_cb)(struct modem_data *data,
268                                 DBusMessageIter *dict);
269
270 struct property_info {
271         struct modem_data *modem;
272         const char *path;
273         const char *interface;
274         const char *property;
275         set_property_cb set_property_cb;
276         get_properties_cb get_properties_cb;
277 };
278
279 static void set_property_reply(DBusPendingCall *call, void *user_data)
280 {
281         struct property_info *info = user_data;
282         DBusMessage *reply;
283         DBusError error;
284         connman_bool_t success = TRUE;
285
286         DBG("%s path %s %s.%s", info->modem->path,
287                 info->path, info->interface, info->property);
288
289         info->modem->call_set_property = NULL;
290
291         dbus_error_init(&error);
292
293         reply = dbus_pending_call_steal_reply(call);
294
295         if (dbus_set_error_from_message(&error, reply)) {
296                 connman_error("Failed to change property: %s %s.%s: %s %s",
297                                 info->path, info->interface, info->property,
298                                 error.name, error.message);
299                 dbus_error_free(&error);
300                 success = FALSE;
301         }
302
303         if (info->set_property_cb != NULL)
304                 (*info->set_property_cb)(info->modem, success);
305
306         dbus_message_unref(reply);
307
308         dbus_pending_call_unref(call);
309 }
310
311 static int set_property(struct modem_data *modem,
312                         const char *path, const char *interface,
313                         const char *property, int type, void *value,
314                         set_property_cb notify)
315 {
316         DBusMessage *message;
317         DBusMessageIter iter;
318         struct property_info *info;
319
320         DBG("%s path %s %s.%s", modem->path, path, interface, property);
321
322         if (modem->call_set_property != NULL) {
323                 connman_error("Pending SetProperty");
324                 return -EBUSY;
325         }
326
327         message = dbus_message_new_method_call(OFONO_SERVICE, path,
328                                         interface, SET_PROPERTY);
329         if (message == NULL)
330                 return -ENOMEM;
331
332         dbus_message_iter_init_append(message, &iter);
333         connman_dbus_property_append_basic(&iter, property, type, value);
334
335         if (dbus_connection_send_with_reply(connection, message,
336                         &modem->call_set_property, TIMEOUT) == FALSE) {
337                 connman_error("Failed to change property: %s %s.%s",
338                                 path, interface, property);
339                 dbus_message_unref(message);
340                 return -EINVAL;
341         }
342
343         if (modem->call_set_property == NULL) {
344                 connman_error("D-Bus connection not available");
345                 dbus_message_unref(message);
346                 return -EINVAL;
347         }
348
349         info = g_try_new0(struct property_info, 1);
350         if (info == NULL) {
351                 dbus_message_unref(message);
352                 return -ENOMEM;
353         }
354
355         info->modem = modem;
356         info->path = path;
357         info->interface = interface;
358         info->property = property;
359         info->set_property_cb = notify;
360
361         dbus_pending_call_set_notify(modem->call_set_property,
362                                         set_property_reply, info, g_free);
363
364         dbus_message_unref(message);
365
366         return -EINPROGRESS;
367 }
368
369 static void get_properties_reply(DBusPendingCall *call, void *user_data)
370 {
371         struct property_info *info = user_data;
372         DBusMessageIter array, dict;
373         DBusMessage *reply;
374         DBusError error;
375
376         DBG("%s path %s %s", info->modem->path, info->path, info->interface);
377
378         info->modem->call_get_properties = NULL;
379
380         dbus_error_init(&error);
381
382         reply = dbus_pending_call_steal_reply(call);
383
384         if (dbus_set_error_from_message(&error, reply)) {
385                 connman_error("Failed to get properties: %s %s: %s %s",
386                                 info->path, info->interface,
387                                 error.name, error.message);
388                 dbus_error_free(&error);
389
390                 goto done;
391         }
392
393         if (dbus_message_iter_init(reply, &array) == FALSE)
394                 goto done;
395
396         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
397                 goto done;
398
399         dbus_message_iter_recurse(&array, &dict);
400
401         if (info->get_properties_cb != NULL)
402                 (*info->get_properties_cb)(info->modem, &dict);
403
404 done:
405
406         dbus_message_unref(reply);
407
408         dbus_pending_call_unref(call);
409 }
410
411 static int get_properties(const char *path, const char *interface,
412                                 get_properties_cb notify,
413                                 struct modem_data *modem)
414 {
415         DBusMessage *message;
416         struct property_info *info;
417
418         DBG("%s path %s %s", modem->path, path, interface);
419
420         if (modem->call_get_properties != NULL) {
421                 connman_error("Pending GetProperties");
422                 return -EBUSY;
423         }
424
425         message = dbus_message_new_method_call(OFONO_SERVICE, path,
426                                         interface, GET_PROPERTIES);
427         if (message == NULL)
428                 return -ENOMEM;
429
430         if (dbus_connection_send_with_reply(connection, message,
431                         &modem->call_get_properties, TIMEOUT) == FALSE) {
432                 connman_error("Failed to call %s.GetProperties()", interface);
433                 dbus_message_unref(message);
434                 return -EINVAL;
435         }
436
437         if (modem->call_get_properties == NULL) {
438                 connman_error("D-Bus connection not available");
439                 dbus_message_unref(message);
440                 return -EINVAL;
441         }
442
443         info = g_try_new0(struct property_info, 1);
444         if (info == NULL) {
445                 dbus_message_unref(message);
446                 return -ENOMEM;
447         }
448
449         info->modem = modem;
450         info->path = path;
451         info->interface = interface;
452         info->get_properties_cb = notify;
453
454         dbus_pending_call_set_notify(modem->call_get_properties,
455                                         get_properties_reply, info, g_free);
456
457         dbus_message_unref(message);
458
459         return -EINPROGRESS;
460 }
461
462 static void context_set_active_reply(struct modem_data *modem,
463                                         connman_bool_t success)
464 {
465         DBG("%s", modem->path);
466
467         if (success == TRUE) {
468                 /*
469                  * Don't handle do anything on success here. oFono will send
470                  * the change via PropertyChanged singal.
471                  */
472                 return;
473         }
474
475         /*
476          * Active = True might fail due a timeout. That means oFono
477          * still tries to go online. If we retry to set Active = True,
478          * we just get a InProgress error message. Should we power
479          * cycle the modem in such cases?
480          */
481
482         connman_network_set_error(modem->network,
483                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
484 }
485
486 static int context_set_active(struct modem_data *modem)
487 {
488         dbus_bool_t active = TRUE;
489
490         DBG("%s", modem->path);
491
492         return set_property(modem, modem->context->path,
493                                 OFONO_CONTEXT_INTERFACE,
494                                 "Active", DBUS_TYPE_BOOLEAN,
495                                 &active,
496                                 context_set_active_reply);
497 }
498
499 static int context_set_inactive(struct modem_data *modem)
500 {
501         dbus_bool_t active = FALSE;
502         int err;
503
504         DBG("%s", modem->path);
505
506         err = set_property(modem, modem->context->path,
507                                 OFONO_CONTEXT_INTERFACE,
508                                 "Active", DBUS_TYPE_BOOLEAN,
509                                 &active,
510                                 NULL);
511         if (err == -EINPROGRESS)
512                 return 0;
513
514         return err;
515 }
516
517 static void cdma_cm_set_powered_reply(struct modem_data *modem,
518                                         connman_bool_t success)
519 {
520         DBG("%s", modem->path);
521
522         if (success == TRUE) {
523                 /*
524                  * Don't handle do anything on success here. oFono will send
525                  * the change via PropertyChanged singal.
526                  */
527                 return;
528         }
529
530         /*
531          * Powered = True might fail due a timeout. That means oFono
532          * still tries to go online. If we retry to set Powered = True,
533          * we just get a InProgress error message. Should we power
534          * cycle the modem in such cases?
535          */
536
537         connman_network_set_error(modem->network,
538                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
539 }
540
541 static int cdma_cm_set_powered(struct modem_data *modem)
542 {
543         dbus_bool_t powered = TRUE;
544
545         DBG("%s", modem->path);
546
547         return set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
548                                 "Powered", DBUS_TYPE_BOOLEAN, &powered,
549                                 cdma_cm_set_powered_reply);
550 }
551
552 static int cdma_cm_shutdown(struct modem_data *modem)
553 {
554         dbus_bool_t powered = FALSE;
555         int err;
556
557         DBG("%s", modem->path);
558
559         err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
560                                 "Powered", DBUS_TYPE_BOOLEAN, &powered, NULL);
561         if (err == -EINPROGRESS)
562                 return 0;
563
564         return err;
565 }
566
567 static void modem_set_online_reply(struct modem_data *modem,
568                                         connman_bool_t success)
569 {
570         DBG("%s", modem->path);
571
572         if (success == TRUE) {
573                 /*
574                  * Don't handle do anything on success here. oFono will send
575                  * the change via PropertyChanged singal.
576                  */
577                 return;
578         }
579
580         modem->set_online = FALSE;
581 }
582
583 static int modem_set_online(struct modem_data *modem)
584 {
585         DBG("%s", modem->path);
586
587         modem->set_online = TRUE;
588
589         return set_property(modem, modem->path,
590                                 OFONO_MODEM_INTERFACE,
591                                 "Online", DBUS_TYPE_BOOLEAN,
592                                 &modem->set_online,
593                                 modem_set_online_reply);
594 }
595
596 static void cm_set_powered_reply(struct modem_data *modem,
597                                         connman_bool_t success)
598 {
599         DBG("%s", modem->path);
600
601         if (success == TRUE) {
602                 /*
603                  * Don't handle do anything on success here. oFono will send
604                  * the change via PropertyChanged singal.
605                  */
606                 return;
607         }
608
609         modem->set_cm_powered = FALSE;
610 }
611
612 static int cm_set_powered(struct modem_data *modem)
613 {
614         DBG("%s", modem->path);
615
616         modem->set_cm_powered = TRUE;
617
618         return set_property(modem, modem->path,
619                                 OFONO_CM_INTERFACE,
620                                 "Powered", DBUS_TYPE_BOOLEAN,
621                                 &modem->set_cm_powered,
622                                 cm_set_powered_reply);
623 }
624
625 static int modem_set_powered(struct modem_data *modem)
626 {
627         DBG("%s", modem->path);
628
629         modem->set_powered = TRUE;
630
631         return set_property(modem, modem->path,
632                                 OFONO_MODEM_INTERFACE,
633                                 "Powered", DBUS_TYPE_BOOLEAN,
634                                 &modem->set_powered,
635                                 NULL);
636 }
637
638 static int modem_set_unpowered(struct modem_data *modem)
639 {
640         DBG("%s", modem->path);
641
642         modem->set_powered = FALSE;
643
644         return set_property(modem, modem->path,
645                                 OFONO_MODEM_INTERFACE,
646                                 "Powered", DBUS_TYPE_BOOLEAN,
647                                 &modem->set_powered,
648                                 NULL);
649 }
650
651 static connman_bool_t has_interface(uint8_t interfaces,
652                                         enum ofono_api api)
653 {
654         if ((interfaces & api) == api)
655                 return TRUE;
656
657         return FALSE;
658 }
659
660 static uint8_t extract_interfaces(DBusMessageIter *array)
661 {
662         DBusMessageIter entry;
663         uint8_t interfaces = 0;
664
665         dbus_message_iter_recurse(array, &entry);
666
667         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
668                 const char *name;
669
670                 dbus_message_iter_get_basic(&entry, &name);
671
672                 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
673                         interfaces |= OFONO_API_SIM;
674                 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
675                         interfaces |= OFONO_API_NETREG;
676                 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
677                         interfaces |= OFONO_API_CM;
678                 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
679                         interfaces |= OFONO_API_CDMA_CM;
680                 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
681                         interfaces |= OFONO_API_CDMA_NETREG;
682
683                 dbus_message_iter_next(&entry);
684         }
685
686         return interfaces;
687 }
688
689 static char *extract_nameservers(DBusMessageIter *array)
690 {
691         DBusMessageIter entry;
692         char *nameservers = NULL;
693         char *tmp;
694
695         dbus_message_iter_recurse(array, &entry);
696
697         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
698                 const char *nameserver;
699
700                 dbus_message_iter_get_basic(&entry, &nameserver);
701
702                 if (nameservers == NULL) {
703                         nameservers = g_strdup(nameserver);
704                 } else {
705                         tmp = nameservers;
706                         nameservers = g_strdup_printf("%s %s", tmp, nameserver);
707                         g_free(tmp);
708                 }
709
710                 dbus_message_iter_next(&entry);
711         }
712
713         return nameservers;
714 }
715
716 static void extract_ipv4_settings(DBusMessageIter *array,
717                                 struct network_context *context)
718 {
719         DBusMessageIter dict;
720         char *address = NULL, *netmask = NULL, *gateway = NULL;
721         char *nameservers = NULL;
722         const char *interface = NULL;
723         int index = -1;
724
725         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
726                 return;
727
728         dbus_message_iter_recurse(array, &dict);
729
730         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
731                 DBusMessageIter entry, value;
732                 const char *key, *val;
733
734                 dbus_message_iter_recurse(&dict, &entry);
735                 dbus_message_iter_get_basic(&entry, &key);
736
737                 dbus_message_iter_next(&entry);
738                 dbus_message_iter_recurse(&entry, &value);
739
740                 if (g_str_equal(key, "Interface") == TRUE) {
741                         dbus_message_iter_get_basic(&value, &interface);
742
743                         DBG("Interface %s", interface);
744
745                         index = connman_inet_ifindex(interface);
746
747                         DBG("index %d", index);
748                 } else if (g_str_equal(key, "Method") == TRUE) {
749                         dbus_message_iter_get_basic(&value, &val);
750
751                         DBG("Method %s", val);
752
753                         if (g_strcmp0(val, "static") == 0) {
754                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
755                         } else if (g_strcmp0(val, "dhcp") == 0) {
756                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
757                                 break;
758                         }
759                 } else if (g_str_equal(key, "Address") == TRUE) {
760                         dbus_message_iter_get_basic(&value, &val);
761
762                         address = g_strdup(val);
763
764                         DBG("Address %s", address);
765                 } else if (g_str_equal(key, "Netmask") == TRUE) {
766                         dbus_message_iter_get_basic(&value, &val);
767
768                         netmask = g_strdup(val);
769
770                         DBG("Netmask %s", netmask);
771                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
772                         nameservers = extract_nameservers(&value);
773
774                         DBG("Nameservers %s", nameservers);
775                 } else if (g_str_equal(key, "Gateway") == TRUE) {
776                         dbus_message_iter_get_basic(&value, &val);
777
778                         gateway = g_strdup(val);
779
780                         DBG("Gateway %s", gateway);
781                 }
782
783                 dbus_message_iter_next(&dict);
784         }
785
786         if (index < 0)
787                 goto out;
788
789         if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
790                 goto out;
791
792         context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
793         if (context->ipv4_address == NULL)
794                 goto out;
795
796         context->index = index;
797         connman_ipaddress_set_ipv4(context->ipv4_address, address,
798                                 netmask, gateway);
799
800         context->ipv4_nameservers = nameservers;
801
802 out:
803         if (context->ipv4_nameservers != nameservers)
804                 g_free(nameservers);
805
806         g_free(address);
807         g_free(netmask);
808         g_free(gateway);
809 }
810
811 static void extract_ipv6_settings(DBusMessageIter *array,
812                                 struct network_context *context)
813 {
814         DBusMessageIter dict;
815         char *address = NULL, *gateway = NULL;
816         unsigned char prefix_length;
817         char *nameservers = NULL;
818         const char *interface = NULL;
819         int index = -1;
820
821         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
822                 return;
823
824         dbus_message_iter_recurse(array, &dict);
825
826         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
827                 DBusMessageIter entry, value;
828                 const char *key, *val;
829
830                 dbus_message_iter_recurse(&dict, &entry);
831                 dbus_message_iter_get_basic(&entry, &key);
832
833                 dbus_message_iter_next(&entry);
834                 dbus_message_iter_recurse(&entry, &value);
835
836                 if (g_str_equal(key, "Interface") == TRUE) {
837                         dbus_message_iter_get_basic(&value, &interface);
838
839                         DBG("Interface %s", interface);
840
841                         index = connman_inet_ifindex(interface);
842
843                         DBG("index %d", index);
844                 } else if (g_str_equal(key, "Address") == TRUE) {
845                         dbus_message_iter_get_basic(&value, &val);
846
847                         address = g_strdup(val);
848
849                         DBG("Address %s", address);
850                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
851                         dbus_message_iter_get_basic(&value, &prefix_length);
852
853                         DBG("prefix length %d", prefix_length);
854                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
855                         nameservers = extract_nameservers(&value);
856
857                         DBG("Nameservers %s", nameservers);
858                 } else if (g_str_equal(key, "Gateway") == TRUE) {
859                         dbus_message_iter_get_basic(&value, &val);
860
861                         gateway = g_strdup(val);
862
863                         DBG("Gateway %s", gateway);
864                 }
865
866                 dbus_message_iter_next(&dict);
867         }
868
869         if (index < 0)
870                 goto out;
871
872         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
873
874         context->ipv6_address =
875                 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
876         if (context->ipv6_address == NULL)
877                 goto out;
878
879         context->index = index;
880         connman_ipaddress_set_ipv6(context->ipv6_address, address,
881                                 prefix_length, gateway);
882
883         context->ipv6_nameservers = nameservers;
884
885 out:
886         if (context->ipv6_nameservers != nameservers)
887                 g_free(nameservers);
888
889         g_free(address);
890         g_free(gateway);
891 }
892
893 static connman_bool_t ready_to_create_device(struct modem_data *modem)
894 {
895         /*
896          * There are three different modem types which behave slightly
897          * different:
898          * - GSM modems will expose the SIM interface then the
899          *   CM interface.
900          * - DUN modems will expose first a unique serial number (BDADDR)
901          *   and then the CM interface.
902          * - CDMA modems will expose CM first and sometime later
903          *   a unique serial number.
904          *
905          * This functions tests if we have the necessary information gathered
906          * before we are able to create a device.
907          */
908
909         if (modem->device != NULL)
910                 return FALSE;
911
912         if (modem->imsi != NULL || modem->serial != NULL)
913                 return TRUE;
914
915         return FALSE;
916 }
917
918 static void create_device(struct modem_data *modem)
919 {
920         struct connman_device *device;
921         char *uninitialized_var(ident);
922
923         DBG("%s", modem->path);
924
925         if (modem->imsi != NULL)
926                 ident = modem->imsi;
927         else if (modem->serial != NULL)
928                 ident = modem->serial;
929
930         if (connman_dbus_validate_ident(ident) == FALSE)
931                 ident = connman_dbus_encode_string(ident);
932         else
933                 ident = g_strdup(ident);
934
935         device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
936         if (device == NULL)
937                 goto out;
938
939         DBG("device %p", device);
940
941         connman_device_set_ident(device, ident);
942
943         connman_device_set_string(device, "Path", modem->path);
944
945         connman_device_set_data(device, modem);
946
947         if (connman_device_register(device) < 0) {
948                 connman_error("Failed to register cellular device");
949                 connman_device_unref(device);
950                 goto out;
951         }
952
953         modem->device = device;
954
955 out:
956         g_free(ident);
957 }
958
959 static void destroy_device(struct modem_data *modem)
960 {
961         DBG("%s", modem->path);
962
963         connman_device_set_powered(modem->device, FALSE);
964
965         if (modem->network != NULL) {
966                 connman_device_remove_network(modem->device, modem->network);
967                 connman_network_unref(modem->network);
968                 modem->network = NULL;
969         }
970
971         connman_device_unregister(modem->device);
972         connman_device_unref(modem->device);
973
974         modem->device = NULL;
975 }
976
977 static void add_network(struct modem_data *modem)
978 {
979         const char *group;
980
981         DBG("%s", modem->path);
982
983         if (modem->network != NULL)
984                 return;
985
986         modem->network = connman_network_create(modem->context->path,
987                                                 CONNMAN_NETWORK_TYPE_CELLULAR);
988         if (modem->network == NULL)
989                 return;
990
991         DBG("network %p", modem->network);
992
993         connman_network_set_data(modem->network, modem);
994
995         connman_network_set_string(modem->network, "Path",
996                                         modem->context->path);
997
998         connman_network_set_index(modem->network, modem->context->index);
999
1000         if (modem->name != NULL)
1001                 connman_network_set_name(modem->network, modem->name);
1002         else
1003                 connman_network_set_name(modem->network, "");
1004
1005         connman_network_set_strength(modem->network, modem->strength);
1006
1007         group = get_ident(modem->context->path);
1008         connman_network_set_group(modem->network, group);
1009
1010         connman_network_set_available(modem->network, TRUE);
1011
1012         connman_network_set_bool(modem->network, "Roaming",
1013                                         modem->roaming);
1014
1015         if (connman_device_add_network(modem->device, modem->network) < 0) {
1016                 connman_network_unref(modem->network);
1017                 modem->network = NULL;
1018                 return;
1019         }
1020 }
1021
1022 static int add_cm_context(struct modem_data *modem, const char *context_path,
1023                                 DBusMessageIter *dict)
1024 {
1025         const char *context_type;
1026         struct network_context *context = NULL;
1027         connman_bool_t active = FALSE;
1028
1029         DBG("%s context path %s", modem->path, context_path);
1030
1031         if (modem->context != NULL) {
1032                 /*
1033                  * We have already assigned a context to this modem
1034                  * and we do only support one Internet context.
1035                  */
1036                 return -EALREADY;
1037         }
1038
1039         context = network_context_alloc(context_path);
1040         if (context == NULL)
1041                 return -ENOMEM;
1042
1043         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1044                 DBusMessageIter entry, value;
1045                 const char *key;
1046
1047                 dbus_message_iter_recurse(dict, &entry);
1048                 dbus_message_iter_get_basic(&entry, &key);
1049
1050                 dbus_message_iter_next(&entry);
1051                 dbus_message_iter_recurse(&entry, &value);
1052
1053                 if (g_str_equal(key, "Type") == TRUE) {
1054                         dbus_message_iter_get_basic(&value, &context_type);
1055
1056                         DBG("%s context %s type %s", modem->path,
1057                                 context_path, context_type);
1058                 } else if (g_str_equal(key, "Settings") == TRUE) {
1059                         DBG("%s Settings", modem->path);
1060
1061                         extract_ipv4_settings(&value, context);
1062                 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1063                         DBG("%s IPv6.Settings", modem->path);
1064
1065                         extract_ipv6_settings(&value, context);
1066                 } else if (g_str_equal(key, "Active") == TRUE) {
1067                         dbus_message_iter_get_basic(&value, &active);
1068
1069                         DBG("%s Active %d", modem->path, active);
1070                 }
1071
1072                 dbus_message_iter_next(dict);
1073         }
1074
1075         if (g_strcmp0(context_type, "internet") != 0) {
1076                 network_context_free(context);
1077                 return -EINVAL;
1078         }
1079
1080         modem->context = context;
1081         modem->active = active;
1082
1083         g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1084
1085         return 0;
1086 }
1087
1088 static void remove_cm_context(struct modem_data *modem,
1089                                 const char *context_path)
1090 {
1091         if (modem->context == NULL)
1092                 return;
1093
1094         g_hash_table_remove(context_hash, context_path);
1095
1096         network_context_free(modem->context);
1097         modem->context = NULL;
1098 }
1099
1100 static gboolean context_changed(DBusConnection *connection,
1101                                 DBusMessage *message,
1102                                 void *user_data)
1103 {
1104         const char *context_path = dbus_message_get_path(message);
1105         struct modem_data *modem = NULL;
1106         DBusMessageIter iter, value;
1107         const char *key;
1108
1109         DBG("context_path %s", context_path);
1110
1111         modem = g_hash_table_lookup(context_hash, context_path);
1112         if (modem == NULL)
1113                 return TRUE;
1114
1115         if (dbus_message_iter_init(message, &iter) == FALSE)
1116                 return TRUE;
1117
1118         dbus_message_iter_get_basic(&iter, &key);
1119
1120         dbus_message_iter_next(&iter);
1121         dbus_message_iter_recurse(&iter, &value);
1122
1123         /*
1124          * oFono guarantees the ordering of Settings and
1125          * Active. Settings will always be send before Active = True.
1126          * That means we don't have to order here.
1127          */
1128         if (g_str_equal(key, "Settings") == TRUE) {
1129                 DBG("%s Settings", modem->path);
1130
1131                 extract_ipv4_settings(&value, modem->context);
1132         } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1133                 DBG("%s IPv6.Settings", modem->path);
1134
1135                 extract_ipv6_settings(&value, modem->context);
1136         } else if (g_str_equal(key, "Active") == TRUE) {
1137                 dbus_message_iter_get_basic(&value, &modem->active);
1138
1139                 DBG("%s Active %d", modem->path, modem->active);
1140
1141                 if (modem->active == TRUE)
1142                         set_connected(modem);
1143                 else
1144                         set_disconnected(modem);
1145         }
1146
1147         return TRUE;
1148 }
1149
1150 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1151 {
1152         struct modem_data *modem = user_data;
1153         DBusMessageIter array, dict, entry, value;
1154         DBusMessage *reply;
1155         DBusError error;
1156
1157         DBG("%s", modem->path);
1158
1159         modem->call_get_contexts = NULL;
1160
1161         reply = dbus_pending_call_steal_reply(call);
1162
1163         dbus_error_init(&error);
1164
1165         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1166                 connman_error("%s", error.message);
1167                 dbus_error_free(&error);
1168                 goto done;
1169         }
1170
1171         if (dbus_message_iter_init(reply, &array) == FALSE)
1172                 goto done;
1173
1174         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1175                 goto done;
1176
1177         dbus_message_iter_recurse(&array, &dict);
1178
1179         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1180                 const char *context_path;
1181
1182                 dbus_message_iter_recurse(&dict, &entry);
1183                 dbus_message_iter_get_basic(&entry, &context_path);
1184
1185                 dbus_message_iter_next(&entry);
1186                 dbus_message_iter_recurse(&entry, &value);
1187
1188                 if (add_cm_context(modem, context_path, &value) == 0)
1189                         break;
1190
1191                 dbus_message_iter_next(&dict);
1192         }
1193
1194 done:
1195         dbus_message_unref(reply);
1196
1197         dbus_pending_call_unref(call);
1198 }
1199
1200 static int cm_get_contexts(struct modem_data *modem)
1201 {
1202         DBusMessage *message;
1203
1204         DBG("%s", modem->path);
1205
1206         if (modem->call_get_contexts != NULL)
1207                 return -EBUSY;
1208
1209         message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1210                                         OFONO_CM_INTERFACE, GET_CONTEXTS);
1211         if (message == NULL)
1212                 return -ENOMEM;
1213
1214         if (dbus_connection_send_with_reply(connection, message,
1215                         &modem->call_get_contexts, TIMEOUT) == FALSE) {
1216                 connman_error("Failed to call GetContexts()");
1217                 dbus_message_unref(message);
1218                 return -EINVAL;
1219         }
1220
1221         if (modem->call_get_contexts == NULL) {
1222                 connman_error("D-Bus connection not available");
1223                 dbus_message_unref(message);
1224                 return -EINVAL;
1225         }
1226
1227         dbus_pending_call_set_notify(modem->call_get_contexts,
1228                                         cm_get_contexts_reply,
1229                                         modem, NULL);
1230
1231         dbus_message_unref(message);
1232
1233         return -EINPROGRESS;
1234 }
1235
1236 static gboolean cm_context_added(DBusConnection *connection,
1237                                         DBusMessage *message,
1238                                         void *user_data)
1239 {
1240         const char *path = dbus_message_get_path(message);
1241         char *context_path;
1242         struct modem_data *modem;
1243         DBusMessageIter iter, properties;
1244
1245         DBG("%s", path);
1246
1247         modem = g_hash_table_lookup(modem_hash, context_path);
1248         if (modem == NULL)
1249                 return TRUE;
1250
1251         if (dbus_message_iter_init(message, &iter) == FALSE)
1252                 return TRUE;
1253
1254         dbus_message_iter_get_basic(&iter, &context_path);
1255
1256         dbus_message_iter_next(&iter);
1257         dbus_message_iter_recurse(&iter, &properties);
1258
1259         if (add_cm_context(modem, context_path, &properties) != 0)
1260                 return TRUE;
1261
1262         return TRUE;
1263 }
1264
1265 static gboolean cm_context_removed(DBusConnection *connection,
1266                                         DBusMessage *message,
1267                                         void *user_data)
1268 {
1269         const char *path = dbus_message_get_path(message);
1270         const char *context_path;
1271         struct modem_data *modem;
1272         DBusMessageIter iter;
1273
1274         DBG("context path %s", path);
1275
1276         if (dbus_message_iter_init(message, &iter) == FALSE)
1277                 return TRUE;
1278
1279         dbus_message_iter_get_basic(&iter, &context_path);
1280
1281         modem = g_hash_table_lookup(context_hash, context_path);
1282         if (modem == NULL)
1283                 return TRUE;
1284
1285         remove_cm_context(modem, context_path);
1286
1287         return TRUE;
1288 }
1289
1290 static void netreg_update_name(struct modem_data *modem,
1291                                 DBusMessageIter* value)
1292 {
1293         char *name;
1294
1295         dbus_message_iter_get_basic(value, &name);
1296
1297         DBG("%s Name %s", modem->path, name);
1298
1299         g_free(modem->name);
1300         modem->name = g_strdup(name);
1301
1302         if (modem->network == NULL)
1303                 return;
1304
1305         connman_network_set_name(modem->network, modem->name);
1306         connman_network_update(modem->network);
1307 }
1308
1309 static void netreg_update_strength(struct modem_data *modem,
1310                                         DBusMessageIter *value)
1311 {
1312         dbus_message_iter_get_basic(value, &modem->strength);
1313
1314         DBG("%s Strength %d", modem->path, modem->strength);
1315
1316         if (modem->network == NULL)
1317                 return;
1318
1319         /*
1320          * GSM:
1321          * We don't have 2 signal notifications we always report the strength
1322          * signal. data_strength is always equal to 0.
1323          *
1324          * CDMA:
1325          * In the case we have a data_strength signal (from 1xEVDO network)
1326          * we don't need to update the value with strength signal (from 1xCDMA)
1327          * because the modem is registered to 1xEVDO network for data call.
1328          * In case we have no data_strength signal (not registered to 1xEVDO
1329          * network), we must report the strength signal (registered to 1xCDMA
1330          * network e.g slow mode).
1331          */
1332         if (modem->data_strength != 0)
1333                 return;
1334
1335         connman_network_set_strength(modem->network, modem->strength);
1336         connman_network_update(modem->network);
1337 }
1338
1339 /* Retrieve 1xEVDO Data Strength signal */
1340 static void netreg_update_datastrength(struct modem_data *modem,
1341                                         DBusMessageIter *value)
1342 {
1343         dbus_message_iter_get_basic(value, &modem->data_strength);
1344
1345         DBG("%s Data Strength %d", modem->path, modem->data_strength);
1346
1347         if (modem->network == NULL)
1348                 return;
1349
1350         /*
1351          * CDMA modem is not registered to 1xEVDO network, let
1352          * update_signal_strength() reporting the value on the Strength signal
1353          * notification.
1354          */
1355         if (modem->data_strength == 0)
1356                 return;
1357
1358         connman_network_set_strength(modem->network, modem->data_strength);
1359         connman_network_update(modem->network);
1360 }
1361
1362 static void netreg_update_roaming(struct modem_data *modem,
1363                                         DBusMessageIter *value)
1364 {
1365         char *status;
1366         connman_bool_t roaming;
1367
1368         dbus_message_iter_get_basic(value, &status);
1369
1370         if (g_str_equal(status, "roaming") == TRUE)
1371                 roaming = TRUE;
1372         else
1373                 roaming = FALSE;
1374
1375         if (roaming == modem->roaming)
1376                 return;
1377
1378         modem->roaming = roaming;
1379
1380         if (modem->network == NULL)
1381                 return;
1382
1383         connman_network_set_bool(modem->network,
1384                                 "Roaming", modem->roaming);
1385         connman_network_update(modem->network);
1386 }
1387
1388 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1389                                 void *user_data)
1390 {
1391         const char *path = dbus_message_get_path(message);
1392         struct modem_data *modem;
1393         DBusMessageIter iter, value;
1394         const char *key;
1395
1396         modem = g_hash_table_lookup(modem_hash, path);
1397         if (modem == NULL)
1398                 return TRUE;
1399
1400         if (modem->ignore == TRUE)
1401                 return TRUE;
1402
1403         if (dbus_message_iter_init(message, &iter) == FALSE)
1404                 return TRUE;
1405
1406         dbus_message_iter_get_basic(&iter, &key);
1407
1408         dbus_message_iter_next(&iter);
1409         dbus_message_iter_recurse(&iter, &value);
1410
1411         if (g_str_equal(key, "Name") == TRUE)
1412                 netreg_update_name(modem, &value);
1413         else if (g_str_equal(key, "Strength") == TRUE)
1414                 netreg_update_strength(modem, &value);
1415         else if (g_str_equal(key, "Status") == TRUE)
1416                 netreg_update_roaming(modem, &value);
1417
1418         return TRUE;
1419 }
1420
1421 static void netreg_properties_reply(struct modem_data *modem,
1422                                         DBusMessageIter *dict)
1423 {
1424         DBG("%s", modem->path);
1425
1426         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1427                 DBusMessageIter entry, value;
1428                 const char *key;
1429
1430                 dbus_message_iter_recurse(dict, &entry);
1431                 dbus_message_iter_get_basic(&entry, &key);
1432
1433                 dbus_message_iter_next(&entry);
1434                 dbus_message_iter_recurse(&entry, &value);
1435
1436                 if (g_str_equal(key, "Name") == TRUE)
1437                         netreg_update_name(modem, &value);
1438                 else if (g_str_equal(key, "Strength") == TRUE)
1439                         netreg_update_strength(modem, &value);
1440                 else if (g_str_equal(key, "Status") == TRUE)
1441                         netreg_update_roaming(modem, &value);
1442
1443                 dbus_message_iter_next(dict);
1444         }
1445
1446         if (modem->context == NULL) {
1447                 /*
1448                  * netgreg_get_properties() was issued after we got
1449                  * cm_get_contexts_reply() where we create the
1450                  * context. Though before we got the
1451                  * netreg_properties_reply the context was removed
1452                  * again. Therefore we have to skip the network
1453                  * creation.
1454                  */
1455                 return;
1456         }
1457
1458         add_network(modem);
1459
1460         if (modem->active == TRUE)
1461                 set_connected(modem);
1462 }
1463
1464 static int netreg_get_properties(struct modem_data *modem)
1465 {
1466         return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1467                         netreg_properties_reply, modem);
1468 }
1469
1470 static void add_cdma_network(struct modem_data *modem)
1471 {
1472         /* Be sure that device is created before adding CDMA network */
1473         if (modem->device == NULL)
1474                 return;
1475
1476         /*
1477          * CDMA modems don't need contexts for data call, however the current
1478          * add_network() logic needs one, so we create one to proceed.
1479          */
1480         if (modem->context == NULL)
1481                 modem->context = network_context_alloc(modem->path);
1482
1483         add_network(modem);
1484
1485         if (modem->cdma_cm_powered == TRUE)
1486                 set_connected(modem);
1487 }
1488
1489 static gboolean cdma_netreg_changed(DBusConnection *connection,
1490                                         DBusMessage *message,
1491                                         void *user_data)
1492 {
1493         const char *path = dbus_message_get_path(message);
1494         struct modem_data *modem;
1495         DBusMessageIter iter, value;
1496         const char *key;
1497
1498         DBG("");
1499
1500         modem = g_hash_table_lookup(modem_hash, path);
1501         if (modem == NULL)
1502                 return TRUE;
1503
1504         if (modem->ignore == TRUE)
1505                 return TRUE;
1506
1507         if (dbus_message_iter_init(message, &iter) == FALSE)
1508                 return TRUE;
1509
1510         dbus_message_iter_get_basic(&iter, &key);
1511
1512         dbus_message_iter_next(&iter);
1513         dbus_message_iter_recurse(&iter, &value);
1514
1515         if (g_str_equal(key, "Name") == TRUE)
1516                 netreg_update_name(modem, &value);
1517         else if (g_str_equal(key, "Strength") == TRUE)
1518                 netreg_update_strength(modem, &value);
1519         else if (g_str_equal(key, "DataStrength") == TRUE)
1520                 netreg_update_datastrength(modem, &value);
1521         else if (g_str_equal(key, "Status") == TRUE)
1522                 netreg_update_roaming(modem, &value);
1523
1524         add_cdma_network(modem);
1525
1526         return TRUE;
1527 }
1528
1529 static void cdma_netreg_properties_reply(struct modem_data *modem,
1530                                         DBusMessageIter *dict)
1531 {
1532         DBG("%s", modem->path);
1533
1534         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1535                 DBusMessageIter entry, value;
1536                 const char *key;
1537
1538                 dbus_message_iter_recurse(dict, &entry);
1539                 dbus_message_iter_get_basic(&entry, &key);
1540
1541                 dbus_message_iter_next(&entry);
1542                 dbus_message_iter_recurse(&entry, &value);
1543
1544                 if (g_str_equal(key, "Name") == TRUE)
1545                         netreg_update_name(modem, &value);
1546                 else if (g_str_equal(key, "Strength") == TRUE)
1547                         netreg_update_strength(modem, &value);
1548                 else if (g_str_equal(key, "DataStrength") == TRUE)
1549                         netreg_update_datastrength(modem, &value);
1550                 else if (g_str_equal(key, "Status") == TRUE)
1551                         netreg_update_roaming(modem, &value);
1552
1553                 dbus_message_iter_next(dict);
1554         }
1555
1556         add_cdma_network(modem);
1557 }
1558
1559 static int cdma_netreg_get_properties(struct modem_data *modem)
1560 {
1561         return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1562                         cdma_netreg_properties_reply, modem);
1563 }
1564
1565 static void cm_update_attached(struct modem_data *modem,
1566                                 DBusMessageIter *value)
1567 {
1568         dbus_message_iter_get_basic(value, &modem->attached);
1569
1570         DBG("%s Attached %d", modem->path, modem->attached);
1571
1572         if (modem->attached == FALSE)
1573                 return;
1574
1575         if (has_interface(modem->interfaces,
1576                                 OFONO_API_NETREG) == FALSE) {
1577                 return;
1578         }
1579
1580         netreg_get_properties(modem);
1581 }
1582
1583 static void cm_update_powered(struct modem_data *modem,
1584                                 DBusMessageIter *value)
1585 {
1586         dbus_message_iter_get_basic(value, &modem->cm_powered);
1587
1588         DBG("%s ConnnectionManager Powered %d", modem->path,
1589                 modem->cm_powered);
1590
1591         if (modem->cm_powered == TRUE)
1592                 return;
1593
1594         cm_set_powered(modem);
1595 }
1596
1597 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1598                                 void *user_data)
1599 {
1600         const char *path = dbus_message_get_path(message);
1601         struct modem_data *modem;
1602         DBusMessageIter iter, value;
1603         const char *key;
1604
1605         modem = g_hash_table_lookup(modem_hash, path);
1606         if (modem == NULL)
1607                 return TRUE;
1608
1609         if (modem->ignore == TRUE)
1610                 return TRUE;
1611
1612         if (dbus_message_iter_init(message, &iter) == FALSE)
1613                 return TRUE;
1614
1615         dbus_message_iter_get_basic(&iter, &key);
1616
1617         dbus_message_iter_next(&iter);
1618         dbus_message_iter_recurse(&iter, &value);
1619
1620         if (g_str_equal(key, "Attached") == TRUE)
1621                 cm_update_attached(modem, &value);
1622         else if (g_str_equal(key, "Powered") == TRUE)
1623                 cm_update_powered(modem, &value);
1624
1625         return TRUE;
1626 }
1627
1628 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1629 {
1630         DBG("%s", modem->path);
1631
1632         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1633                 DBusMessageIter entry, value;
1634                 const char *key;
1635
1636                 dbus_message_iter_recurse(dict, &entry);
1637                 dbus_message_iter_get_basic(&entry, &key);
1638
1639                 dbus_message_iter_next(&entry);
1640                 dbus_message_iter_recurse(&entry, &value);
1641
1642                 if (g_str_equal(key, "Attached") == TRUE)
1643                         cm_update_attached(modem, &value);
1644                 else if (g_str_equal(key, "Powered") == TRUE)
1645                         cm_update_powered(modem, &value);
1646
1647                 dbus_message_iter_next(dict);
1648         }
1649 }
1650
1651 static int cm_get_properties(struct modem_data *modem)
1652 {
1653         return get_properties(modem->path, OFONO_CM_INTERFACE,
1654                                 cm_properties_reply, modem);
1655 }
1656
1657 static int cdma_cm_get_properties(struct modem_data *modem)
1658 {
1659         return -EINVAL;
1660 }
1661
1662 static int connection_manager_init(struct modem_data *modem)
1663 {
1664         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1665                 if (ready_to_create_device(modem) == TRUE)
1666                         create_device(modem);
1667
1668                 if (modem->device != NULL) {
1669                         cm_get_properties(modem);
1670                         cm_get_contexts(modem);
1671                 }
1672
1673                 return 0;
1674         }
1675
1676         if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE) {
1677                 if (ready_to_create_device(modem) == TRUE)
1678                         create_device(modem);
1679
1680                 if (modem->device != NULL)
1681                         cdma_cm_get_properties(modem);
1682
1683                 return 0;
1684         }
1685
1686         return 0;
1687 }
1688
1689 static void update_sim_imsi(struct modem_data *modem,
1690                                 const char *imsi)
1691 {
1692         DBG("%s imsi %s", modem->path, imsi);
1693
1694         if (g_strcmp0(modem->imsi, imsi) == 0)
1695                 return;
1696
1697         g_free(modem->imsi);
1698         modem->imsi = g_strdup(imsi);
1699 }
1700
1701 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1702                                 void *user_data)
1703 {
1704         const char *path = dbus_message_get_path(message);
1705         struct modem_data *modem;
1706         DBusMessageIter iter, value;
1707         const char *key;
1708
1709         modem = g_hash_table_lookup(modem_hash, path);
1710         if (modem == NULL)
1711                 return TRUE;
1712
1713         if (modem->ignore == TRUE)
1714                 return TRUE;
1715
1716         if (dbus_message_iter_init(message, &iter) == FALSE)
1717                 return TRUE;
1718
1719         dbus_message_iter_get_basic(&iter, &key);
1720
1721         dbus_message_iter_next(&iter);
1722         dbus_message_iter_recurse(&iter, &value);
1723
1724         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1725                 char *imsi;
1726
1727                 dbus_message_iter_get_basic(&value, &imsi);
1728
1729                 update_sim_imsi(modem, imsi);
1730
1731                 if (modem->online == FALSE) {
1732                         modem_set_online(modem);
1733                 } else if (has_interface(modem->interfaces,
1734                                                 OFONO_API_CM) == TRUE) {
1735                         if (ready_to_create_device(modem) == TRUE)
1736                                 create_device(modem);
1737                 } else if (has_interface(modem->interfaces,
1738                                                 OFONO_API_CDMA_CM) == TRUE) {
1739                         if (ready_to_create_device(modem) == TRUE)
1740                                 create_device(modem);
1741                 }
1742         }
1743
1744         return TRUE;
1745 }
1746
1747 static void sim_properties_reply(struct modem_data *modem,
1748                                         DBusMessageIter *dict)
1749 {
1750         DBG("%s", modem->path);
1751
1752         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1753                 DBusMessageIter entry, value;
1754                 const char *key;
1755
1756                 dbus_message_iter_recurse(dict, &entry);
1757                 dbus_message_iter_get_basic(&entry, &key);
1758
1759                 dbus_message_iter_next(&entry);
1760                 dbus_message_iter_recurse(&entry, &value);
1761
1762                 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1763                         char *imsi;
1764
1765                         dbus_message_iter_get_basic(&value, &imsi);
1766
1767                         update_sim_imsi(modem, imsi);
1768
1769                         if (modem->online == FALSE) {
1770                                 modem_set_online(modem);
1771                                 break;
1772                         }
1773
1774                         connection_manager_init(modem);
1775
1776                         return;
1777                 }
1778
1779                 dbus_message_iter_next(dict);
1780         }
1781 }
1782
1783 static int sim_get_properties(struct modem_data *modem)
1784 {
1785         return get_properties(modem->path, OFONO_SIM_INTERFACE,
1786                                 sim_properties_reply, modem);
1787 }
1788
1789 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1790                                 void *user_data)
1791 {
1792         const char *path = dbus_message_get_path(message);
1793         struct modem_data *modem;
1794         DBusMessageIter iter, value;
1795         const char *key;
1796
1797         modem = g_hash_table_lookup(modem_hash, path);
1798         if (modem == NULL)
1799                 return TRUE;
1800
1801         if (modem->ignore == TRUE)
1802                 return TRUE;
1803
1804         if (dbus_message_iter_init(message, &iter) == FALSE)
1805                 return TRUE;
1806
1807         dbus_message_iter_get_basic(&iter, &key);
1808
1809         dbus_message_iter_next(&iter);
1810         dbus_message_iter_recurse(&iter, &value);
1811
1812         if (g_str_equal(key, "Powered") == TRUE) {
1813                 dbus_message_iter_get_basic(&value, &modem->powered);
1814
1815                 DBG("%s Powered %d", modem->path, modem->powered);
1816
1817                 if (modem->powered == FALSE)
1818                         modem_set_powered(modem);
1819         } else if (g_str_equal(key, "Online") == TRUE) {
1820                 dbus_message_iter_get_basic(&value, &modem->online);
1821
1822                 DBG("%s Online %d", modem->path, modem->online);
1823
1824                 if (modem->online == FALSE)
1825                         return TRUE;
1826
1827                 connection_manager_init(modem);
1828         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1829                 modem->interfaces = extract_interfaces(&value);
1830
1831                 DBG("%s Interfaces 0x%02x", modem->path,
1832                         modem->interfaces);
1833
1834                 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1835                         if (modem->imsi == NULL &&
1836                                         modem->set_powered == FALSE) {
1837                                 /*
1838                                  * Only use do GetProperties() when
1839                                  * device has not been powered up.
1840                                  */
1841                                 sim_get_properties(modem);
1842                                 return TRUE;
1843                         }
1844                 }
1845
1846                 if (connection_manager_init(modem) == FALSE) {
1847                         if (modem->context != NULL) {
1848                                 remove_cm_context(modem,
1849                                                 modem->context->path);
1850                         }
1851
1852                         if (modem->device != NULL)
1853                                 destroy_device(modem);
1854
1855                         return TRUE;
1856                 }
1857
1858                 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1859                         if (modem->attached == TRUE)
1860                                 netreg_get_properties(modem);
1861                 } else if (has_interface(modem->interfaces,
1862                                 OFONO_API_CDMA_NETREG) == TRUE)
1863                         cdma_netreg_get_properties(modem);
1864         } else if (g_str_equal(key, "Serial") == TRUE) {
1865                 char *serial;
1866
1867                 dbus_message_iter_get_basic(&value, &serial);
1868
1869                 g_free(modem->serial);
1870                 modem->serial = g_strdup(serial);
1871
1872                 DBG("%s Serial %s", modem->path, modem->serial);
1873
1874                 connection_manager_init(modem);
1875         }
1876
1877         return TRUE;
1878 }
1879
1880 static void add_modem(const char *path, DBusMessageIter *prop)
1881 {
1882         struct modem_data *modem;
1883
1884         DBG("%s", path);
1885
1886         modem = g_hash_table_lookup(modem_hash, path);
1887         if (modem != NULL) {
1888                 /*
1889                  * When oFono powers up we ask for the modems and oFono is
1890                  * reporting with modem_added signal the modems. Only
1891                  * handle them once.
1892                  */
1893                 return;
1894         }
1895
1896         modem = g_try_new0(struct modem_data, 1);
1897         if (modem == NULL)
1898                 return;
1899
1900         modem->path = g_strdup(path);
1901
1902         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1903
1904         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1905                 DBusMessageIter entry, value;
1906                 const char *key;
1907
1908                 dbus_message_iter_recurse(prop, &entry);
1909                 dbus_message_iter_get_basic(&entry, &key);
1910
1911                 dbus_message_iter_next(&entry);
1912                 dbus_message_iter_recurse(&entry, &value);
1913
1914                 if (g_str_equal(key, "Powered") == TRUE) {
1915                         dbus_message_iter_get_basic(&value, &modem->powered);
1916
1917                         DBG("%s Powered %d", modem->path, modem->powered);
1918                 } else if (g_str_equal(key, "Online") == TRUE) {
1919                         dbus_message_iter_get_basic(&value, &modem->online);
1920
1921                         DBG("%s Online %d", modem->path, modem->online);
1922                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1923                         modem->interfaces = extract_interfaces(&value);
1924
1925                         DBG("%s Interfaces 0x%02x", modem->path,
1926                                 modem->interfaces);
1927                 } else if (g_str_equal(key, "Serial") == TRUE) {
1928                         char *serial;
1929
1930                         dbus_message_iter_get_basic(&value, &serial);
1931                         modem->serial = g_strdup(serial);
1932
1933                         DBG("%s Serial %s", modem->path, modem->serial);
1934                 } else if (g_str_equal(key, "Type") == TRUE) {
1935                         char *type;
1936
1937                         dbus_message_iter_get_basic(&value, &type);
1938
1939                         DBG("%s Type %s", modem->path, type);
1940                         if (g_strcmp0(type, "hardware") != 0) {
1941                                 DBG("%s Ignore this modem", modem->path);
1942                                 modem->ignore = TRUE;
1943                         }
1944                 }
1945
1946                 dbus_message_iter_next(prop);
1947         }
1948
1949         if (modem->ignore == TRUE)
1950                 return;
1951
1952         if (modem->powered == FALSE) {
1953                 modem_set_powered(modem);
1954         } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1955                 sim_get_properties(modem);
1956         } else
1957                 connection_manager_init(modem);
1958 }
1959
1960 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
1961 {
1962         struct modem_data *modem = value;
1963
1964         DBG("%s", modem->path);
1965
1966         if (modem->ignore ==  TRUE)
1967                 return;
1968
1969         modem_set_unpowered(modem);
1970 }
1971
1972 static void remove_modem(gpointer data)
1973 {
1974         struct modem_data *modem = data;
1975
1976         DBG("%s", modem->path);
1977
1978         if (modem->call_set_property != NULL)
1979                 dbus_pending_call_cancel(modem->call_set_property);
1980
1981         if (modem->call_get_properties != NULL)
1982                 dbus_pending_call_cancel(modem->call_get_properties);
1983
1984         if (modem->call_get_contexts != NULL)
1985                 dbus_pending_call_cancel(modem->call_get_contexts);
1986
1987         if (modem->device != NULL)
1988                 destroy_device(modem);
1989
1990         if (modem->context != NULL)
1991                 remove_cm_context(modem, modem->context->path);
1992
1993         g_free(modem->serial);
1994         g_free(modem->name);
1995         g_free(modem->imsi);
1996         g_free(modem->path);
1997
1998         g_free(modem);
1999 }
2000
2001 static gboolean modem_added(DBusConnection *connection,
2002                                 DBusMessage *message, void *user_data)
2003 {
2004         DBusMessageIter iter, properties;
2005         const char *path;
2006
2007         DBG("");
2008
2009         if (dbus_message_iter_init(message, &iter) == FALSE)
2010                 return TRUE;
2011
2012         dbus_message_iter_get_basic(&iter, &path);
2013
2014         dbus_message_iter_next(&iter);
2015         dbus_message_iter_recurse(&iter, &properties);
2016
2017         add_modem(path, &properties);
2018
2019         return TRUE;
2020 }
2021
2022 static gboolean modem_removed(DBusConnection *connection,
2023                                 DBusMessage *message, void *user_data)
2024 {
2025         DBusMessageIter iter;
2026         const char *path;
2027
2028         DBG("");
2029
2030         if (dbus_message_iter_init(message, &iter) == FALSE)
2031                 return TRUE;
2032
2033         dbus_message_iter_get_basic(&iter, &path);
2034
2035         g_hash_table_remove(modem_hash, path);
2036
2037         return TRUE;
2038 }
2039
2040 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2041 {
2042         DBusMessage *reply;
2043         DBusError error;
2044         DBusMessageIter array, dict;
2045
2046         DBG("");
2047
2048         reply = dbus_pending_call_steal_reply(call);
2049
2050         dbus_error_init(&error);
2051
2052         if (dbus_set_error_from_message(&error, reply) == TRUE) {
2053                 connman_error("%s", error.message);
2054                 dbus_error_free(&error);
2055                 goto done;
2056         }
2057
2058         if (dbus_message_iter_init(reply, &array) == FALSE)
2059                 goto done;
2060
2061         dbus_message_iter_recurse(&array, &dict);
2062
2063         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2064                 DBusMessageIter value, properties;
2065                 const char *path;
2066
2067                 dbus_message_iter_recurse(&dict, &value);
2068                 dbus_message_iter_get_basic(&value, &path);
2069
2070                 dbus_message_iter_next(&value);
2071                 dbus_message_iter_recurse(&value, &properties);
2072
2073                 add_modem(path, &properties);
2074
2075                 dbus_message_iter_next(&dict);
2076         }
2077
2078 done:
2079         dbus_message_unref(reply);
2080
2081         dbus_pending_call_unref(call);
2082 }
2083
2084 static int manager_get_modems(void)
2085 {
2086         DBusMessage *message;
2087         DBusPendingCall *call;
2088
2089         DBG("");
2090
2091         message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2092                                         OFONO_MANAGER_INTERFACE, GET_MODEMS);
2093         if (message == NULL)
2094                 return -ENOMEM;
2095
2096         if (dbus_connection_send_with_reply(connection, message,
2097                                                &call, TIMEOUT) == FALSE) {
2098                 connman_error("Failed to call GetModems()");
2099                 dbus_message_unref(message);
2100                 return -EINVAL;
2101         }
2102
2103         if (call == NULL) {
2104                 connman_error("D-Bus connection not available");
2105                 dbus_message_unref(message);
2106                 return -EINVAL;
2107         }
2108
2109         dbus_pending_call_set_notify(call, manager_get_modems_reply,
2110                                         NULL, NULL);
2111
2112         dbus_message_unref(message);
2113
2114         return -EINPROGRESS;
2115 }
2116
2117 static void ofono_connect(DBusConnection *conn, void *user_data)
2118 {
2119         DBG("");
2120
2121         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2122                                                 g_free, remove_modem);
2123         if (modem_hash == NULL)
2124                 return;
2125
2126         context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2127                                                 g_free, NULL);
2128         if (context_hash == NULL) {
2129                 g_hash_table_destroy(modem_hash);
2130                 return;
2131         }
2132
2133         manager_get_modems();
2134 }
2135
2136 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2137 {
2138         DBG("");
2139
2140         if (modem_hash == NULL || context_hash == NULL)
2141                 return;
2142
2143         g_hash_table_destroy(modem_hash);
2144         modem_hash = NULL;
2145
2146         g_hash_table_destroy(context_hash);
2147         context_hash = NULL;
2148 }
2149
2150 static int network_probe(struct connman_network *network)
2151 {
2152         struct modem_data *modem = connman_network_get_data(network);
2153
2154         DBG("%s network %p", modem->path, network);
2155
2156         return 0;
2157 }
2158
2159 static void network_remove(struct connman_network *network)
2160 {
2161         struct modem_data *modem = connman_network_get_data(network);
2162
2163         DBG("%s network %p", modem->path, network);
2164 }
2165
2166 static int network_connect(struct connman_network *network)
2167 {
2168         struct modem_data *modem = connman_network_get_data(network);
2169
2170         DBG("%s network %p", modem->path, network);
2171
2172         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2173                 return context_set_active(modem);
2174         else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2175                 return cdma_cm_set_powered(modem);
2176
2177         connman_error("Connection manager interface not available");
2178
2179         return -ENOSYS;
2180 }
2181
2182 static int network_disconnect(struct connman_network *network)
2183 {
2184         struct modem_data *modem = connman_network_get_data(network);
2185
2186         DBG("%s network %p", modem->path, network);
2187
2188         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2189                 return context_set_inactive(modem);
2190         else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2191                 return cdma_cm_shutdown(modem);
2192
2193         connman_error("Connection manager interface not available");
2194
2195         return -ENOSYS;
2196 }
2197
2198 static struct connman_network_driver network_driver = {
2199         .name           = "network",
2200         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
2201         .probe          = network_probe,
2202         .remove         = network_remove,
2203         .connect        = network_connect,
2204         .disconnect     = network_disconnect,
2205 };
2206
2207 static int modem_probe(struct connman_device *device)
2208 {
2209         struct modem_data *modem = connman_device_get_data(device);
2210
2211         DBG("%s device %p", modem->path, device);
2212
2213         return 0;
2214 }
2215
2216 static void modem_remove(struct connman_device *device)
2217 {
2218         struct modem_data *modem = connman_device_get_data(device);
2219
2220         DBG("%s device %p", modem->path, device);
2221 }
2222
2223 static int modem_enable(struct connman_device *device)
2224 {
2225         struct modem_data *modem = connman_device_get_data(device);
2226
2227         DBG("%s device %p", modem->path, device);
2228
2229         return 0;
2230 }
2231
2232 static int modem_disable(struct connman_device *device)
2233 {
2234         struct modem_data *modem = connman_device_get_data(device);
2235
2236         DBG("%s device %p", modem->path, device);
2237
2238         return 0;
2239 }
2240
2241 static struct connman_device_driver modem_driver = {
2242         .name           = "modem",
2243         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
2244         .probe          = modem_probe,
2245         .remove         = modem_remove,
2246         .enable         = modem_enable,
2247         .disable        = modem_disable,
2248 };
2249
2250 static guint watch;
2251 static guint modem_added_watch;
2252 static guint modem_removed_watch;
2253 static guint modem_watch;
2254 static guint cm_watch;
2255 static guint sim_watch;
2256 static guint context_added_watch;
2257 static guint context_removed_watch;
2258 static guint netreg_watch;
2259 static guint context_watch;
2260 static guint cdma_netreg_watch;
2261
2262 static int ofono_init(void)
2263 {
2264         int err;
2265
2266         DBG("");
2267
2268         connection = connman_dbus_get_connection();
2269         if (connection == NULL)
2270                 return -EIO;
2271
2272         watch = g_dbus_add_service_watch(connection,
2273                                         OFONO_SERVICE, ofono_connect,
2274                                         ofono_disconnect, NULL, NULL);
2275
2276         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2277                                                 OFONO_MANAGER_INTERFACE,
2278                                                 MODEM_ADDED,
2279                                                 modem_added,
2280                                                 NULL, NULL);
2281
2282         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2283                                                 OFONO_MANAGER_INTERFACE,
2284                                                 MODEM_REMOVED,
2285                                                 modem_removed,
2286                                                 NULL, NULL);
2287
2288         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2289                                                 OFONO_MODEM_INTERFACE,
2290                                                 PROPERTY_CHANGED,
2291                                                 modem_changed,
2292                                                 NULL, NULL);
2293
2294         cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2295                                                 OFONO_CM_INTERFACE,
2296                                                 PROPERTY_CHANGED,
2297                                                 cm_changed,
2298                                                 NULL, NULL);
2299
2300         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2301                                                 OFONO_SIM_INTERFACE,
2302                                                 PROPERTY_CHANGED,
2303                                                 sim_changed,
2304                                                 NULL, NULL);
2305
2306         context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2307                                                 OFONO_CM_INTERFACE,
2308                                                 CONTEXT_ADDED,
2309                                                 cm_context_added,
2310                                                 NULL, NULL);
2311
2312         context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2313                                                 OFONO_CM_INTERFACE,
2314                                                 CONTEXT_REMOVED,
2315                                                 cm_context_removed,
2316                                                 NULL, NULL);
2317
2318         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2319                                                 OFONO_CONTEXT_INTERFACE,
2320                                                 PROPERTY_CHANGED,
2321                                                 context_changed,
2322                                                 NULL, NULL);
2323
2324         netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2325                                                 OFONO_NETREG_INTERFACE,
2326                                                 PROPERTY_CHANGED,
2327                                                 netreg_changed,
2328                                                 NULL, NULL);
2329
2330         cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2331                                                 OFONO_CDMA_NETREG_INTERFACE,
2332                                                 PROPERTY_CHANGED,
2333                                                 cdma_netreg_changed,
2334                                                 NULL, NULL);
2335
2336
2337         if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2338                         modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2339                         context_added_watch == 0 ||
2340                         context_removed_watch == 0 ||
2341                         context_watch == 0 || netreg_watch == 0 ||
2342                         cdma_netreg_watch == 0) {
2343                 err = -EIO;
2344                 goto remove;
2345         }
2346
2347         err = connman_network_driver_register(&network_driver);
2348         if (err < 0)
2349                 goto remove;
2350
2351         err = connman_device_driver_register(&modem_driver);
2352         if (err < 0) {
2353                 connman_network_driver_unregister(&network_driver);
2354                 goto remove;
2355         }
2356
2357         return 0;
2358
2359 remove:
2360         g_dbus_remove_watch(connection, cdma_netreg_watch);
2361         g_dbus_remove_watch(connection, netreg_watch);
2362         g_dbus_remove_watch(connection, context_watch);
2363         g_dbus_remove_watch(connection, context_removed_watch);
2364         g_dbus_remove_watch(connection, context_added_watch);
2365         g_dbus_remove_watch(connection, sim_watch);
2366         g_dbus_remove_watch(connection, cm_watch);
2367         g_dbus_remove_watch(connection, modem_watch);
2368         g_dbus_remove_watch(connection, modem_removed_watch);
2369         g_dbus_remove_watch(connection, modem_added_watch);
2370         g_dbus_remove_watch(connection, watch);
2371         dbus_connection_unref(connection);
2372
2373         return err;
2374 }
2375
2376 static void ofono_exit(void)
2377 {
2378         DBG("");
2379
2380         if (modem_hash != NULL) {
2381                 /*
2382                  * We should propably wait for the SetProperty() reply
2383                  * message, because ...
2384                  */
2385                 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2386
2387                 /*
2388                  * ... here we will cancel the call.
2389                  */
2390                 g_hash_table_destroy(modem_hash);
2391                 modem_hash = NULL;
2392         }
2393
2394         if (context_hash != NULL) {
2395                 g_hash_table_destroy(context_hash);
2396                 context_hash = NULL;
2397         }
2398
2399         connman_device_driver_unregister(&modem_driver);
2400         connman_network_driver_unregister(&network_driver);
2401
2402         g_dbus_remove_watch(connection, cdma_netreg_watch);
2403         g_dbus_remove_watch(connection, netreg_watch);
2404         g_dbus_remove_watch(connection, context_watch);
2405         g_dbus_remove_watch(connection, context_removed_watch);
2406         g_dbus_remove_watch(connection, context_added_watch);
2407         g_dbus_remove_watch(connection, sim_watch);
2408         g_dbus_remove_watch(connection, cm_watch);
2409         g_dbus_remove_watch(connection, modem_watch);
2410         g_dbus_remove_watch(connection, modem_added_watch);
2411         g_dbus_remove_watch(connection, modem_removed_watch);
2412         g_dbus_remove_watch(connection, watch);
2413
2414         dbus_connection_unref(connection);
2415 }
2416
2417 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2418                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)