ofono: Remove unnecessary brackets
[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 cdma_cm_update_powered(struct modem_data *modem,
1629                                         DBusMessageIter *value)
1630 {
1631         dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1632
1633         DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1634
1635         if (modem->network == NULL)
1636                 return;
1637
1638         if (modem->cdma_cm_powered == TRUE)
1639                 set_connected(modem);
1640         else
1641                 set_disconnected(modem);
1642 }
1643
1644 static void cdma_cm_update_settings(struct modem_data *modem,
1645                                         DBusMessageIter *value)
1646 {
1647         DBG("%s Settings", modem->path);
1648
1649         if (modem->context != NULL)
1650                 return;
1651
1652         extract_ipv4_settings(value, modem->context);
1653 }
1654
1655 static gboolean cdma_cm_changed(DBusConnection *connection,
1656                                 DBusMessage *message, void *user_data)
1657 {
1658         const char *path = dbus_message_get_path(message);
1659         struct modem_data *modem;
1660         DBusMessageIter iter, value;
1661         const char *key;
1662
1663         modem = g_hash_table_lookup(modem_hash, path);
1664         if (modem == NULL)
1665                 return TRUE;
1666
1667         if (modem->online == TRUE && modem->network == NULL)
1668                 cdma_netreg_get_properties(modem);
1669
1670         if (dbus_message_iter_init(message, &iter) == FALSE)
1671                 return TRUE;
1672
1673         dbus_message_iter_get_basic(&iter, &key);
1674
1675         dbus_message_iter_next(&iter);
1676         dbus_message_iter_recurse(&iter, &value);
1677
1678         if (g_str_equal(key, "Powered") == TRUE)
1679                 cdma_cm_update_powered(modem, &value);
1680         if (g_str_equal(key, "Settings") == TRUE)
1681                 cdma_cm_update_settings(modem, &value);
1682
1683         return TRUE;
1684 }
1685
1686 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1687 {
1688         DBG("%s", modem->path);
1689
1690         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1691                 DBusMessageIter entry, value;
1692                 const char *key;
1693
1694                 dbus_message_iter_recurse(dict, &entry);
1695                 dbus_message_iter_get_basic(&entry, &key);
1696
1697                 dbus_message_iter_next(&entry);
1698                 dbus_message_iter_recurse(&entry, &value);
1699
1700                 if (g_str_equal(key, "Attached") == TRUE)
1701                         cm_update_attached(modem, &value);
1702                 else if (g_str_equal(key, "Powered") == TRUE)
1703                         cm_update_powered(modem, &value);
1704
1705                 dbus_message_iter_next(dict);
1706         }
1707 }
1708
1709 static int cm_get_properties(struct modem_data *modem)
1710 {
1711         return get_properties(modem->path, OFONO_CM_INTERFACE,
1712                                 cm_properties_reply, modem);
1713 }
1714
1715 static void cdma_cm_properties_reply(struct modem_data *modem,
1716                                         DBusMessageIter *dict)
1717 {
1718         DBG("%s", modem->path);
1719
1720         if (modem->online == TRUE)
1721                 cdma_netreg_get_properties(modem);
1722
1723         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1724                 DBusMessageIter entry, value;
1725                 const char *key;
1726
1727                 dbus_message_iter_recurse(dict, &entry);
1728                 dbus_message_iter_get_basic(&entry, &key);
1729
1730                 dbus_message_iter_next(&entry);
1731                 dbus_message_iter_recurse(&entry, &value);
1732
1733                 if (g_str_equal(key, "Powered") == TRUE)
1734                         cdma_cm_update_powered(modem, &value);
1735                 if (g_str_equal(key, "Settings") == TRUE)
1736                         cdma_cm_update_settings(modem, &value);
1737
1738                 dbus_message_iter_next(dict);
1739         }
1740 }
1741
1742 static int cdma_cm_get_properties(struct modem_data *modem)
1743 {
1744         return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1745                                 cdma_cm_properties_reply, modem);
1746 }
1747
1748 static int connection_manager_init(struct modem_data *modem)
1749 {
1750         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1751                 if (ready_to_create_device(modem) == TRUE)
1752                         create_device(modem);
1753
1754                 if (modem->device != NULL) {
1755                         cm_get_properties(modem);
1756                         cm_get_contexts(modem);
1757                 }
1758
1759                 return 0;
1760         }
1761
1762         if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE) {
1763                 if (ready_to_create_device(modem) == TRUE)
1764                         create_device(modem);
1765
1766                 if (modem->device != NULL)
1767                         cdma_cm_get_properties(modem);
1768
1769                 return 0;
1770         }
1771
1772         return 0;
1773 }
1774
1775 static void update_sim_imsi(struct modem_data *modem,
1776                                 const char *imsi)
1777 {
1778         DBG("%s imsi %s", modem->path, imsi);
1779
1780         if (g_strcmp0(modem->imsi, imsi) == 0)
1781                 return;
1782
1783         g_free(modem->imsi);
1784         modem->imsi = g_strdup(imsi);
1785 }
1786
1787 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1788                                 void *user_data)
1789 {
1790         const char *path = dbus_message_get_path(message);
1791         struct modem_data *modem;
1792         DBusMessageIter iter, value;
1793         const char *key;
1794
1795         modem = g_hash_table_lookup(modem_hash, path);
1796         if (modem == NULL)
1797                 return TRUE;
1798
1799         if (modem->ignore == TRUE)
1800                 return TRUE;
1801
1802         if (dbus_message_iter_init(message, &iter) == FALSE)
1803                 return TRUE;
1804
1805         dbus_message_iter_get_basic(&iter, &key);
1806
1807         dbus_message_iter_next(&iter);
1808         dbus_message_iter_recurse(&iter, &value);
1809
1810         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1811                 char *imsi;
1812
1813                 dbus_message_iter_get_basic(&value, &imsi);
1814
1815                 update_sim_imsi(modem, imsi);
1816
1817                 if (modem->online == FALSE) {
1818                         modem_set_online(modem);
1819                 } else if (has_interface(modem->interfaces,
1820                                                 OFONO_API_CM) == TRUE) {
1821                         if (ready_to_create_device(modem) == TRUE)
1822                                 create_device(modem);
1823                 } else if (has_interface(modem->interfaces,
1824                                                 OFONO_API_CDMA_CM) == TRUE) {
1825                         if (ready_to_create_device(modem) == TRUE)
1826                                 create_device(modem);
1827                 }
1828         }
1829
1830         return TRUE;
1831 }
1832
1833 static void sim_properties_reply(struct modem_data *modem,
1834                                         DBusMessageIter *dict)
1835 {
1836         DBG("%s", modem->path);
1837
1838         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1839                 DBusMessageIter entry, value;
1840                 const char *key;
1841
1842                 dbus_message_iter_recurse(dict, &entry);
1843                 dbus_message_iter_get_basic(&entry, &key);
1844
1845                 dbus_message_iter_next(&entry);
1846                 dbus_message_iter_recurse(&entry, &value);
1847
1848                 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1849                         char *imsi;
1850
1851                         dbus_message_iter_get_basic(&value, &imsi);
1852
1853                         update_sim_imsi(modem, imsi);
1854
1855                         if (modem->online == FALSE) {
1856                                 modem_set_online(modem);
1857                                 break;
1858                         }
1859
1860                         connection_manager_init(modem);
1861
1862                         return;
1863                 }
1864
1865                 dbus_message_iter_next(dict);
1866         }
1867 }
1868
1869 static int sim_get_properties(struct modem_data *modem)
1870 {
1871         return get_properties(modem->path, OFONO_SIM_INTERFACE,
1872                                 sim_properties_reply, modem);
1873 }
1874
1875 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1876                                 void *user_data)
1877 {
1878         const char *path = dbus_message_get_path(message);
1879         struct modem_data *modem;
1880         DBusMessageIter iter, value;
1881         const char *key;
1882
1883         modem = g_hash_table_lookup(modem_hash, path);
1884         if (modem == NULL)
1885                 return TRUE;
1886
1887         if (modem->ignore == TRUE)
1888                 return TRUE;
1889
1890         if (dbus_message_iter_init(message, &iter) == FALSE)
1891                 return TRUE;
1892
1893         dbus_message_iter_get_basic(&iter, &key);
1894
1895         dbus_message_iter_next(&iter);
1896         dbus_message_iter_recurse(&iter, &value);
1897
1898         if (g_str_equal(key, "Powered") == TRUE) {
1899                 dbus_message_iter_get_basic(&value, &modem->powered);
1900
1901                 DBG("%s Powered %d", modem->path, modem->powered);
1902
1903                 if (modem->powered == FALSE)
1904                         modem_set_powered(modem);
1905         } else if (g_str_equal(key, "Online") == TRUE) {
1906                 dbus_message_iter_get_basic(&value, &modem->online);
1907
1908                 DBG("%s Online %d", modem->path, modem->online);
1909
1910                 if (modem->online == FALSE)
1911                         return TRUE;
1912
1913                 connection_manager_init(modem);
1914         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1915                 modem->interfaces = extract_interfaces(&value);
1916
1917                 DBG("%s Interfaces 0x%02x", modem->path,
1918                         modem->interfaces);
1919
1920                 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1921                         if (modem->imsi == NULL &&
1922                                         modem->set_powered == FALSE) {
1923                                 /*
1924                                  * Only use do GetProperties() when
1925                                  * device has not been powered up.
1926                                  */
1927                                 sim_get_properties(modem);
1928                                 return TRUE;
1929                         }
1930                 }
1931
1932                 if (connection_manager_init(modem) == FALSE) {
1933                         if (modem->context != NULL) {
1934                                 remove_cm_context(modem,
1935                                                 modem->context->path);
1936                         }
1937
1938                         if (modem->device != NULL)
1939                                 destroy_device(modem);
1940
1941                         return TRUE;
1942                 }
1943
1944                 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1945                         if (modem->attached == TRUE)
1946                                 netreg_get_properties(modem);
1947                 } else if (has_interface(modem->interfaces,
1948                                 OFONO_API_CDMA_NETREG) == TRUE)
1949                         cdma_netreg_get_properties(modem);
1950         } else if (g_str_equal(key, "Serial") == TRUE) {
1951                 char *serial;
1952
1953                 dbus_message_iter_get_basic(&value, &serial);
1954
1955                 g_free(modem->serial);
1956                 modem->serial = g_strdup(serial);
1957
1958                 DBG("%s Serial %s", modem->path, modem->serial);
1959
1960                 connection_manager_init(modem);
1961         }
1962
1963         return TRUE;
1964 }
1965
1966 static void add_modem(const char *path, DBusMessageIter *prop)
1967 {
1968         struct modem_data *modem;
1969
1970         DBG("%s", path);
1971
1972         modem = g_hash_table_lookup(modem_hash, path);
1973         if (modem != NULL) {
1974                 /*
1975                  * When oFono powers up we ask for the modems and oFono is
1976                  * reporting with modem_added signal the modems. Only
1977                  * handle them once.
1978                  */
1979                 return;
1980         }
1981
1982         modem = g_try_new0(struct modem_data, 1);
1983         if (modem == NULL)
1984                 return;
1985
1986         modem->path = g_strdup(path);
1987
1988         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1989
1990         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1991                 DBusMessageIter entry, value;
1992                 const char *key;
1993
1994                 dbus_message_iter_recurse(prop, &entry);
1995                 dbus_message_iter_get_basic(&entry, &key);
1996
1997                 dbus_message_iter_next(&entry);
1998                 dbus_message_iter_recurse(&entry, &value);
1999
2000                 if (g_str_equal(key, "Powered") == TRUE) {
2001                         dbus_message_iter_get_basic(&value, &modem->powered);
2002
2003                         DBG("%s Powered %d", modem->path, modem->powered);
2004                 } else if (g_str_equal(key, "Online") == TRUE) {
2005                         dbus_message_iter_get_basic(&value, &modem->online);
2006
2007                         DBG("%s Online %d", modem->path, modem->online);
2008                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2009                         modem->interfaces = extract_interfaces(&value);
2010
2011                         DBG("%s Interfaces 0x%02x", modem->path,
2012                                 modem->interfaces);
2013                 } else if (g_str_equal(key, "Serial") == TRUE) {
2014                         char *serial;
2015
2016                         dbus_message_iter_get_basic(&value, &serial);
2017                         modem->serial = g_strdup(serial);
2018
2019                         DBG("%s Serial %s", modem->path, modem->serial);
2020                 } else if (g_str_equal(key, "Type") == TRUE) {
2021                         char *type;
2022
2023                         dbus_message_iter_get_basic(&value, &type);
2024
2025                         DBG("%s Type %s", modem->path, type);
2026                         if (g_strcmp0(type, "hardware") != 0) {
2027                                 DBG("%s Ignore this modem", modem->path);
2028                                 modem->ignore = TRUE;
2029                         }
2030                 }
2031
2032                 dbus_message_iter_next(prop);
2033         }
2034
2035         if (modem->ignore == TRUE)
2036                 return;
2037
2038         if (modem->powered == FALSE)
2039                 modem_set_powered(modem);
2040         else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE)
2041                 sim_get_properties(modem);
2042         else
2043                 connection_manager_init(modem);
2044 }
2045
2046 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2047 {
2048         struct modem_data *modem = value;
2049
2050         DBG("%s", modem->path);
2051
2052         if (modem->ignore ==  TRUE)
2053                 return;
2054
2055         modem_set_unpowered(modem);
2056 }
2057
2058 static void remove_modem(gpointer data)
2059 {
2060         struct modem_data *modem = data;
2061
2062         DBG("%s", modem->path);
2063
2064         if (modem->call_set_property != NULL)
2065                 dbus_pending_call_cancel(modem->call_set_property);
2066
2067         if (modem->call_get_properties != NULL)
2068                 dbus_pending_call_cancel(modem->call_get_properties);
2069
2070         if (modem->call_get_contexts != NULL)
2071                 dbus_pending_call_cancel(modem->call_get_contexts);
2072
2073         if (modem->device != NULL)
2074                 destroy_device(modem);
2075
2076         if (modem->context != NULL)
2077                 remove_cm_context(modem, modem->context->path);
2078
2079         g_free(modem->serial);
2080         g_free(modem->name);
2081         g_free(modem->imsi);
2082         g_free(modem->path);
2083
2084         g_free(modem);
2085 }
2086
2087 static gboolean modem_added(DBusConnection *connection,
2088                                 DBusMessage *message, void *user_data)
2089 {
2090         DBusMessageIter iter, properties;
2091         const char *path;
2092
2093         DBG("");
2094
2095         if (dbus_message_iter_init(message, &iter) == FALSE)
2096                 return TRUE;
2097
2098         dbus_message_iter_get_basic(&iter, &path);
2099
2100         dbus_message_iter_next(&iter);
2101         dbus_message_iter_recurse(&iter, &properties);
2102
2103         add_modem(path, &properties);
2104
2105         return TRUE;
2106 }
2107
2108 static gboolean modem_removed(DBusConnection *connection,
2109                                 DBusMessage *message, void *user_data)
2110 {
2111         DBusMessageIter iter;
2112         const char *path;
2113
2114         DBG("");
2115
2116         if (dbus_message_iter_init(message, &iter) == FALSE)
2117                 return TRUE;
2118
2119         dbus_message_iter_get_basic(&iter, &path);
2120
2121         g_hash_table_remove(modem_hash, path);
2122
2123         return TRUE;
2124 }
2125
2126 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2127 {
2128         DBusMessage *reply;
2129         DBusError error;
2130         DBusMessageIter array, dict;
2131
2132         DBG("");
2133
2134         reply = dbus_pending_call_steal_reply(call);
2135
2136         dbus_error_init(&error);
2137
2138         if (dbus_set_error_from_message(&error, reply) == TRUE) {
2139                 connman_error("%s", error.message);
2140                 dbus_error_free(&error);
2141                 goto done;
2142         }
2143
2144         if (dbus_message_iter_init(reply, &array) == FALSE)
2145                 goto done;
2146
2147         dbus_message_iter_recurse(&array, &dict);
2148
2149         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2150                 DBusMessageIter value, properties;
2151                 const char *path;
2152
2153                 dbus_message_iter_recurse(&dict, &value);
2154                 dbus_message_iter_get_basic(&value, &path);
2155
2156                 dbus_message_iter_next(&value);
2157                 dbus_message_iter_recurse(&value, &properties);
2158
2159                 add_modem(path, &properties);
2160
2161                 dbus_message_iter_next(&dict);
2162         }
2163
2164 done:
2165         dbus_message_unref(reply);
2166
2167         dbus_pending_call_unref(call);
2168 }
2169
2170 static int manager_get_modems(void)
2171 {
2172         DBusMessage *message;
2173         DBusPendingCall *call;
2174
2175         DBG("");
2176
2177         message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2178                                         OFONO_MANAGER_INTERFACE, GET_MODEMS);
2179         if (message == NULL)
2180                 return -ENOMEM;
2181
2182         if (dbus_connection_send_with_reply(connection, message,
2183                                                &call, TIMEOUT) == FALSE) {
2184                 connman_error("Failed to call GetModems()");
2185                 dbus_message_unref(message);
2186                 return -EINVAL;
2187         }
2188
2189         if (call == NULL) {
2190                 connman_error("D-Bus connection not available");
2191                 dbus_message_unref(message);
2192                 return -EINVAL;
2193         }
2194
2195         dbus_pending_call_set_notify(call, manager_get_modems_reply,
2196                                         NULL, NULL);
2197
2198         dbus_message_unref(message);
2199
2200         return -EINPROGRESS;
2201 }
2202
2203 static void ofono_connect(DBusConnection *conn, void *user_data)
2204 {
2205         DBG("");
2206
2207         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2208                                                 g_free, remove_modem);
2209         if (modem_hash == NULL)
2210                 return;
2211
2212         context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2213                                                 g_free, NULL);
2214         if (context_hash == NULL) {
2215                 g_hash_table_destroy(modem_hash);
2216                 return;
2217         }
2218
2219         manager_get_modems();
2220 }
2221
2222 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2223 {
2224         DBG("");
2225
2226         if (modem_hash == NULL || context_hash == NULL)
2227                 return;
2228
2229         g_hash_table_destroy(modem_hash);
2230         modem_hash = NULL;
2231
2232         g_hash_table_destroy(context_hash);
2233         context_hash = NULL;
2234 }
2235
2236 static int network_probe(struct connman_network *network)
2237 {
2238         struct modem_data *modem = connman_network_get_data(network);
2239
2240         DBG("%s network %p", modem->path, network);
2241
2242         return 0;
2243 }
2244
2245 static void network_remove(struct connman_network *network)
2246 {
2247         struct modem_data *modem = connman_network_get_data(network);
2248
2249         DBG("%s network %p", modem->path, network);
2250 }
2251
2252 static int network_connect(struct connman_network *network)
2253 {
2254         struct modem_data *modem = connman_network_get_data(network);
2255
2256         DBG("%s network %p", modem->path, network);
2257
2258         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2259                 return context_set_active(modem);
2260         else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2261                 return cdma_cm_set_powered(modem);
2262
2263         connman_error("Connection manager interface not available");
2264
2265         return -ENOSYS;
2266 }
2267
2268 static int network_disconnect(struct connman_network *network)
2269 {
2270         struct modem_data *modem = connman_network_get_data(network);
2271
2272         DBG("%s network %p", modem->path, network);
2273
2274         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2275                 return context_set_inactive(modem);
2276         else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2277                 return cdma_cm_shutdown(modem);
2278
2279         connman_error("Connection manager interface not available");
2280
2281         return -ENOSYS;
2282 }
2283
2284 static struct connman_network_driver network_driver = {
2285         .name           = "network",
2286         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
2287         .probe          = network_probe,
2288         .remove         = network_remove,
2289         .connect        = network_connect,
2290         .disconnect     = network_disconnect,
2291 };
2292
2293 static int modem_probe(struct connman_device *device)
2294 {
2295         struct modem_data *modem = connman_device_get_data(device);
2296
2297         DBG("%s device %p", modem->path, device);
2298
2299         return 0;
2300 }
2301
2302 static void modem_remove(struct connman_device *device)
2303 {
2304         struct modem_data *modem = connman_device_get_data(device);
2305
2306         DBG("%s device %p", modem->path, device);
2307 }
2308
2309 static int modem_enable(struct connman_device *device)
2310 {
2311         struct modem_data *modem = connman_device_get_data(device);
2312
2313         DBG("%s device %p", modem->path, device);
2314
2315         return 0;
2316 }
2317
2318 static int modem_disable(struct connman_device *device)
2319 {
2320         struct modem_data *modem = connman_device_get_data(device);
2321
2322         DBG("%s device %p", modem->path, device);
2323
2324         return 0;
2325 }
2326
2327 static struct connman_device_driver modem_driver = {
2328         .name           = "modem",
2329         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
2330         .probe          = modem_probe,
2331         .remove         = modem_remove,
2332         .enable         = modem_enable,
2333         .disable        = modem_disable,
2334 };
2335
2336 static guint watch;
2337 static guint modem_added_watch;
2338 static guint modem_removed_watch;
2339 static guint modem_watch;
2340 static guint cm_watch;
2341 static guint sim_watch;
2342 static guint context_added_watch;
2343 static guint context_removed_watch;
2344 static guint netreg_watch;
2345 static guint context_watch;
2346 static guint cdma_cm_watch;
2347 static guint cdma_netreg_watch;
2348
2349 static int ofono_init(void)
2350 {
2351         int err;
2352
2353         DBG("");
2354
2355         connection = connman_dbus_get_connection();
2356         if (connection == NULL)
2357                 return -EIO;
2358
2359         watch = g_dbus_add_service_watch(connection,
2360                                         OFONO_SERVICE, ofono_connect,
2361                                         ofono_disconnect, NULL, NULL);
2362
2363         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2364                                                 OFONO_MANAGER_INTERFACE,
2365                                                 MODEM_ADDED,
2366                                                 modem_added,
2367                                                 NULL, NULL);
2368
2369         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2370                                                 OFONO_MANAGER_INTERFACE,
2371                                                 MODEM_REMOVED,
2372                                                 modem_removed,
2373                                                 NULL, NULL);
2374
2375         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2376                                                 OFONO_MODEM_INTERFACE,
2377                                                 PROPERTY_CHANGED,
2378                                                 modem_changed,
2379                                                 NULL, NULL);
2380
2381         cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2382                                                 OFONO_CM_INTERFACE,
2383                                                 PROPERTY_CHANGED,
2384                                                 cm_changed,
2385                                                 NULL, NULL);
2386
2387         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2388                                                 OFONO_SIM_INTERFACE,
2389                                                 PROPERTY_CHANGED,
2390                                                 sim_changed,
2391                                                 NULL, NULL);
2392
2393         context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2394                                                 OFONO_CM_INTERFACE,
2395                                                 CONTEXT_ADDED,
2396                                                 cm_context_added,
2397                                                 NULL, NULL);
2398
2399         context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2400                                                 OFONO_CM_INTERFACE,
2401                                                 CONTEXT_REMOVED,
2402                                                 cm_context_removed,
2403                                                 NULL, NULL);
2404
2405         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2406                                                 OFONO_CONTEXT_INTERFACE,
2407                                                 PROPERTY_CHANGED,
2408                                                 context_changed,
2409                                                 NULL, NULL);
2410
2411         netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2412                                                 OFONO_NETREG_INTERFACE,
2413                                                 PROPERTY_CHANGED,
2414                                                 netreg_changed,
2415                                                 NULL, NULL);
2416
2417         cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2418                                                 OFONO_CDMA_CM_INTERFACE,
2419                                                 PROPERTY_CHANGED,
2420                                                 cdma_cm_changed,
2421                                                 NULL, NULL);
2422
2423         cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2424                                                 OFONO_CDMA_NETREG_INTERFACE,
2425                                                 PROPERTY_CHANGED,
2426                                                 cdma_netreg_changed,
2427                                                 NULL, NULL);
2428
2429
2430         if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2431                         modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2432                         context_added_watch == 0 ||
2433                         context_removed_watch == 0 ||
2434                         context_watch == 0 || netreg_watch == 0 ||
2435                         cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2436                 err = -EIO;
2437                 goto remove;
2438         }
2439
2440         err = connman_network_driver_register(&network_driver);
2441         if (err < 0)
2442                 goto remove;
2443
2444         err = connman_device_driver_register(&modem_driver);
2445         if (err < 0) {
2446                 connman_network_driver_unregister(&network_driver);
2447                 goto remove;
2448         }
2449
2450         return 0;
2451
2452 remove:
2453         g_dbus_remove_watch(connection, cdma_netreg_watch);
2454         g_dbus_remove_watch(connection, cdma_cm_watch);
2455         g_dbus_remove_watch(connection, netreg_watch);
2456         g_dbus_remove_watch(connection, context_watch);
2457         g_dbus_remove_watch(connection, context_removed_watch);
2458         g_dbus_remove_watch(connection, context_added_watch);
2459         g_dbus_remove_watch(connection, sim_watch);
2460         g_dbus_remove_watch(connection, cm_watch);
2461         g_dbus_remove_watch(connection, modem_watch);
2462         g_dbus_remove_watch(connection, modem_removed_watch);
2463         g_dbus_remove_watch(connection, modem_added_watch);
2464         g_dbus_remove_watch(connection, watch);
2465         dbus_connection_unref(connection);
2466
2467         return err;
2468 }
2469
2470 static void ofono_exit(void)
2471 {
2472         DBG("");
2473
2474         if (modem_hash != NULL) {
2475                 /*
2476                  * We should propably wait for the SetProperty() reply
2477                  * message, because ...
2478                  */
2479                 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2480
2481                 /*
2482                  * ... here we will cancel the call.
2483                  */
2484                 g_hash_table_destroy(modem_hash);
2485                 modem_hash = NULL;
2486         }
2487
2488         if (context_hash != NULL) {
2489                 g_hash_table_destroy(context_hash);
2490                 context_hash = NULL;
2491         }
2492
2493         connman_device_driver_unregister(&modem_driver);
2494         connman_network_driver_unregister(&network_driver);
2495
2496         g_dbus_remove_watch(connection, cdma_netreg_watch);
2497         g_dbus_remove_watch(connection, cdma_cm_watch);
2498         g_dbus_remove_watch(connection, netreg_watch);
2499         g_dbus_remove_watch(connection, context_watch);
2500         g_dbus_remove_watch(connection, context_removed_watch);
2501         g_dbus_remove_watch(connection, context_added_watch);
2502         g_dbus_remove_watch(connection, sim_watch);
2503         g_dbus_remove_watch(connection, cm_watch);
2504         g_dbus_remove_watch(connection, modem_watch);
2505         g_dbus_remove_watch(connection, modem_added_watch);
2506         g_dbus_remove_watch(connection, modem_removed_watch);
2507         g_dbus_remove_watch(connection, watch);
2508
2509         dbus_connection_unref(connection);
2510 }
2511
2512 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2513                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)