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