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
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         return TRUE;
1318 }
1319
1320 static void netreg_properties_reply(struct modem_data *modem,
1321                                         DBusMessageIter *dict)
1322 {
1323         DBG("%s", modem->path);
1324
1325         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1326                 DBusMessageIter entry, value;
1327                 const char *key;
1328
1329                 dbus_message_iter_recurse(dict, &entry);
1330                 dbus_message_iter_get_basic(&entry, &key);
1331
1332                 dbus_message_iter_next(&entry);
1333                 dbus_message_iter_recurse(&entry, &value);
1334
1335                 if (g_str_equal(key, "Name") == TRUE)
1336                         netreg_update_name(modem, &value);
1337                 else if (g_str_equal(key, "Strength") == TRUE)
1338                         netreg_update_strength(modem, &value);
1339                 else if (g_str_equal(key, "Status") == TRUE)
1340                         netreg_update_roaming(modem, &value);
1341
1342                 dbus_message_iter_next(dict);
1343         }
1344
1345         if (modem->context == NULL) {
1346                 /*
1347                  * netgreg_get_properties() was issued after we got
1348                  * cm_get_contexts_reply() where we create the
1349                  * context. Though before we got the
1350                  * netreg_properties_reply the context was removed
1351                  * again. Therefore we have to skip the network
1352                  * creation.
1353                  */
1354                 return;
1355         }
1356
1357         add_network(modem);
1358
1359         if (modem->active == TRUE)
1360                 set_connected(modem);
1361 }
1362
1363 static int netreg_get_properties(struct modem_data *modem)
1364 {
1365         return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1366                         netreg_properties_reply, modem);
1367 }
1368
1369 static void cm_update_attached(struct modem_data *modem,
1370                                 DBusMessageIter *value)
1371 {
1372         dbus_message_iter_get_basic(value, &modem->attached);
1373
1374         DBG("%s Attached %d", modem->path, modem->attached);
1375
1376         if (modem->attached == FALSE)
1377                 return;
1378
1379         if (has_interface(modem->interfaces,
1380                                 OFONO_API_NETREG) == FALSE) {
1381                 return;
1382         }
1383
1384         netreg_get_properties(modem);
1385 }
1386
1387 static void cm_update_powered(struct modem_data *modem,
1388                                 DBusMessageIter *value)
1389 {
1390         dbus_message_iter_get_basic(value, &modem->cm_powered);
1391
1392         DBG("%s ConnnectionManager Powered %d", modem->path,
1393                 modem->cm_powered);
1394
1395         if (modem->cm_powered == TRUE)
1396                 return;
1397
1398         cm_set_powered(modem);
1399 }
1400
1401 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1402                                 void *user_data)
1403 {
1404         const char *path = dbus_message_get_path(message);
1405         struct modem_data *modem;
1406         DBusMessageIter iter, value;
1407         const char *key;
1408
1409         modem = g_hash_table_lookup(modem_hash, path);
1410         if (modem == NULL)
1411                 return TRUE;
1412
1413         if (modem->ignore == TRUE)
1414                 return TRUE;
1415
1416         if (dbus_message_iter_init(message, &iter) == FALSE)
1417                 return TRUE;
1418
1419         dbus_message_iter_get_basic(&iter, &key);
1420
1421         dbus_message_iter_next(&iter);
1422         dbus_message_iter_recurse(&iter, &value);
1423
1424         if (g_str_equal(key, "Attached") == TRUE)
1425                 cm_update_attached(modem, &value);
1426         else if (g_str_equal(key, "Powered") == TRUE)
1427                 cm_update_powered(modem, &value);
1428
1429         return TRUE;
1430 }
1431
1432 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1433 {
1434         DBG("%s", modem->path);
1435
1436         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1437                 DBusMessageIter entry, value;
1438                 const char *key;
1439
1440                 dbus_message_iter_recurse(dict, &entry);
1441                 dbus_message_iter_get_basic(&entry, &key);
1442
1443                 dbus_message_iter_next(&entry);
1444                 dbus_message_iter_recurse(&entry, &value);
1445
1446                 if (g_str_equal(key, "Attached") == TRUE)
1447                         cm_update_attached(modem, &value);
1448                 else if (g_str_equal(key, "Powered") == TRUE)
1449                         cm_update_powered(modem, &value);
1450
1451                 dbus_message_iter_next(dict);
1452         }
1453 }
1454
1455 static int cm_get_properties(struct modem_data *modem)
1456 {
1457         return get_properties(modem->path, OFONO_CM_INTERFACE,
1458                                 cm_properties_reply, modem);
1459 }
1460
1461 static void update_sim_imsi(struct modem_data *modem,
1462                                 const char *imsi)
1463 {
1464         DBG("%s imsi %s", modem->path, imsi);
1465
1466         if (g_strcmp0(modem->imsi, imsi) == 0)
1467                 return;
1468
1469         g_free(modem->imsi);
1470         modem->imsi = g_strdup(imsi);
1471 }
1472
1473 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1474                                 void *user_data)
1475 {
1476         const char *path = dbus_message_get_path(message);
1477         struct modem_data *modem;
1478         DBusMessageIter iter, value;
1479         const char *key;
1480
1481         modem = g_hash_table_lookup(modem_hash, path);
1482         if (modem == NULL)
1483                 return TRUE;
1484
1485         if (modem->ignore == TRUE)
1486                 return TRUE;
1487
1488         if (dbus_message_iter_init(message, &iter) == FALSE)
1489                 return TRUE;
1490
1491         dbus_message_iter_get_basic(&iter, &key);
1492
1493         dbus_message_iter_next(&iter);
1494         dbus_message_iter_recurse(&iter, &value);
1495
1496         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1497                 char *imsi;
1498
1499                 dbus_message_iter_get_basic(&value, &imsi);
1500
1501                 update_sim_imsi(modem, imsi);
1502
1503                 if (modem->online == FALSE) {
1504                         modem_set_online(modem);
1505                 } else if (has_interface(modem->interfaces,
1506                                                 OFONO_API_CM) == TRUE) {
1507                         if (ready_to_create_device(modem) == TRUE)
1508                                 create_device(modem);
1509                 }
1510         }
1511
1512         return TRUE;
1513 }
1514
1515 static void sim_properties_reply(struct modem_data *modem,
1516                                         DBusMessageIter *dict)
1517 {
1518         DBG("%s", modem->path);
1519
1520         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1521                 DBusMessageIter entry, value;
1522                 const char *key;
1523
1524                 dbus_message_iter_recurse(dict, &entry);
1525                 dbus_message_iter_get_basic(&entry, &key);
1526
1527                 dbus_message_iter_next(&entry);
1528                 dbus_message_iter_recurse(&entry, &value);
1529
1530                 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1531                         char *imsi;
1532
1533                         dbus_message_iter_get_basic(&value, &imsi);
1534
1535                         update_sim_imsi(modem, imsi);
1536
1537                         if (modem->online == FALSE) {
1538                                 modem_set_online(modem);
1539                                 break;
1540                         }
1541
1542                         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1543                                 if (ready_to_create_device(modem) == TRUE)
1544                                         create_device(modem);
1545                                 if (modem->device != NULL) {
1546                                         cm_get_properties(modem);
1547                                         cm_get_contexts(modem);
1548                                 }
1549                         }
1550                         return;
1551                 }
1552
1553                 dbus_message_iter_next(dict);
1554         }
1555 }
1556
1557 static int sim_get_properties(struct modem_data *modem)
1558 {
1559         return get_properties(modem->path, OFONO_SIM_INTERFACE,
1560                         sim_properties_reply, modem);
1561 }
1562
1563 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1564                                 void *user_data)
1565 {
1566         const char *path = dbus_message_get_path(message);
1567         struct modem_data *modem;
1568         DBusMessageIter iter, value;
1569         const char *key;
1570
1571         modem = g_hash_table_lookup(modem_hash, path);
1572         if (modem == NULL)
1573                 return TRUE;
1574
1575         if (modem->ignore == TRUE)
1576                 return TRUE;
1577
1578         if (dbus_message_iter_init(message, &iter) == FALSE)
1579                 return TRUE;
1580
1581         dbus_message_iter_get_basic(&iter, &key);
1582
1583         dbus_message_iter_next(&iter);
1584         dbus_message_iter_recurse(&iter, &value);
1585
1586         if (g_str_equal(key, "Powered") == TRUE) {
1587                 dbus_message_iter_get_basic(&value, &modem->powered);
1588
1589                 DBG("%s Powered %d", modem->path, modem->powered);
1590
1591                 if (modem->powered == FALSE)
1592                         modem_set_powered(modem);
1593         } else if (g_str_equal(key, "Online") == TRUE) {
1594                 dbus_message_iter_get_basic(&value, &modem->online);
1595
1596                 DBG("%s Online %d", modem->path, modem->online);
1597
1598                 if (modem->online == FALSE)
1599                         return TRUE;
1600
1601                 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
1602                         return TRUE;
1603                 if (ready_to_create_device(modem) == TRUE)
1604                         create_device(modem);
1605                 if (modem->device != NULL) {
1606                         cm_get_properties(modem);
1607                         cm_get_contexts(modem);
1608                 }
1609         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1610                 modem->interfaces = extract_interfaces(&value);
1611
1612                 DBG("%s Interfaces 0x%02x", modem->path,
1613                         modem->interfaces);
1614
1615                 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1616                         if (modem->imsi == NULL &&
1617                                         modem->set_powered == FALSE) {
1618                                 /*
1619                                  * Only use do GetProperties() when
1620                                  * device has not been powered up.
1621                                  */
1622                                 sim_get_properties(modem);
1623                                 return TRUE;
1624                         }
1625                 }
1626
1627                 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1628                         if (ready_to_create_device(modem) == TRUE)
1629                                 create_device(modem);
1630                         if (modem->device != NULL) {
1631                                 cm_get_properties(modem);
1632                                 cm_get_contexts(modem);
1633                                 return TRUE;
1634                         }
1635                 } else {
1636                         if (modem->context != NULL) {
1637                                 remove_cm_context(modem,
1638                                                 modem->context->path);
1639                         }
1640
1641                         if (modem->device != NULL)
1642                                 destroy_device(modem);
1643
1644                         return TRUE;
1645                 }
1646
1647                 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1648                         if (modem->attached == TRUE)
1649                                 netreg_get_properties(modem);
1650                 }
1651         } else if (g_str_equal(key, "Serial") == TRUE) {
1652                 char *serial;
1653
1654                 dbus_message_iter_get_basic(&value, &serial);
1655
1656                 g_free(modem->serial);
1657                 modem->serial = g_strdup(serial);
1658
1659                 DBG("%s Serial %s", modem->path, modem->serial);
1660
1661                 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1662                         if (ready_to_create_device(modem) == TRUE)
1663                                 create_device(modem);
1664                         if (modem->device != NULL) {
1665                                 cm_get_properties(modem);
1666                                 cm_get_contexts(modem);
1667                         }
1668                 }
1669         }
1670
1671         return TRUE;
1672 }
1673
1674 static void add_modem(const char *path, DBusMessageIter *prop)
1675 {
1676         struct modem_data *modem;
1677
1678         DBG("%s", path);
1679
1680         modem = g_hash_table_lookup(modem_hash, path);
1681         if (modem != NULL) {
1682                 /*
1683                  * When oFono powers up we ask for the modems and oFono is
1684                  * reporting with modem_added signal the modems. Only
1685                  * handle them once.
1686                  */
1687                 return;
1688         }
1689
1690         modem = g_try_new0(struct modem_data, 1);
1691         if (modem == NULL)
1692                 return;
1693
1694         modem->path = g_strdup(path);
1695
1696         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1697
1698         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1699                 DBusMessageIter entry, value;
1700                 const char *key;
1701
1702                 dbus_message_iter_recurse(prop, &entry);
1703                 dbus_message_iter_get_basic(&entry, &key);
1704
1705                 dbus_message_iter_next(&entry);
1706                 dbus_message_iter_recurse(&entry, &value);
1707
1708                 if (g_str_equal(key, "Powered") == TRUE) {
1709                         dbus_message_iter_get_basic(&value, &modem->powered);
1710
1711                         DBG("%s Powered %d", modem->path, modem->powered);
1712                 } else if (g_str_equal(key, "Online") == TRUE) {
1713                         dbus_message_iter_get_basic(&value, &modem->online);
1714
1715                         DBG("%s Online %d", modem->path, modem->online);
1716                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1717                         modem->interfaces = extract_interfaces(&value);
1718
1719                         DBG("%s Interfaces 0x%02x", modem->path,
1720                                 modem->interfaces);
1721                 } else if (g_str_equal(key, "Serial") == TRUE) {
1722                         char *serial;
1723
1724                         dbus_message_iter_get_basic(&value, &serial);
1725                         modem->serial = g_strdup(serial);
1726
1727                         DBG("%s Serial %s", modem->path, modem->serial);
1728                 } else if (g_str_equal(key, "Type") == TRUE) {
1729                         char *type;
1730
1731                         dbus_message_iter_get_basic(&value, &type);
1732
1733                         DBG("%s Type %s", modem->path, type);
1734                         if (g_strcmp0(type, "hardware") != 0) {
1735                                 DBG("%s Ignore this modem", modem->path);
1736                                 modem->ignore = TRUE;
1737                         }
1738                 }
1739
1740                 dbus_message_iter_next(prop);
1741         }
1742
1743         if (modem->ignore == TRUE)
1744                 return;
1745
1746         if (modem->powered == FALSE) {
1747                 modem_set_powered(modem);
1748         } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1749                 sim_get_properties(modem);
1750         } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1751                 if (ready_to_create_device(modem) == TRUE)
1752                         create_device(modem);
1753                 if (modem->device != NULL) {
1754                         cm_get_properties(modem);
1755                         cm_get_contexts(modem);
1756                 }
1757         }
1758 }
1759
1760 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
1761 {
1762         struct modem_data *modem = value;
1763
1764         DBG("%s", modem->path);
1765
1766         if (modem->ignore ==  TRUE)
1767                 return;
1768
1769         modem_set_unpowered(modem);
1770 }
1771
1772 static void remove_modem(gpointer data)
1773 {
1774         struct modem_data *modem = data;
1775
1776         DBG("%s", modem->path);
1777
1778         if (modem->call_set_property != NULL)
1779                 dbus_pending_call_cancel(modem->call_set_property);
1780
1781         if (modem->call_get_properties != NULL)
1782                 dbus_pending_call_cancel(modem->call_get_properties);
1783
1784         if (modem->call_get_contexts != NULL)
1785                 dbus_pending_call_cancel(modem->call_get_contexts);
1786
1787         if (modem->device != NULL)
1788                 destroy_device(modem);
1789
1790         if (modem->context != NULL)
1791                 remove_cm_context(modem, modem->context->path);
1792
1793         g_free(modem->serial);
1794         g_free(modem->name);
1795         g_free(modem->imsi);
1796         g_free(modem->path);
1797
1798         g_free(modem);
1799 }
1800
1801 static gboolean modem_added(DBusConnection *connection,
1802                                 DBusMessage *message, void *user_data)
1803 {
1804         DBusMessageIter iter, properties;
1805         const char *path;
1806
1807         DBG("");
1808
1809         if (dbus_message_iter_init(message, &iter) == FALSE)
1810                 return TRUE;
1811
1812         dbus_message_iter_get_basic(&iter, &path);
1813
1814         dbus_message_iter_next(&iter);
1815         dbus_message_iter_recurse(&iter, &properties);
1816
1817         add_modem(path, &properties);
1818
1819         return TRUE;
1820 }
1821
1822 static gboolean modem_removed(DBusConnection *connection,
1823                                 DBusMessage *message, void *user_data)
1824 {
1825         DBusMessageIter iter;
1826         const char *path;
1827
1828         DBG("");
1829
1830         if (dbus_message_iter_init(message, &iter) == FALSE)
1831                 return TRUE;
1832
1833         dbus_message_iter_get_basic(&iter, &path);
1834
1835         g_hash_table_remove(modem_hash, path);
1836
1837         return TRUE;
1838 }
1839
1840 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1841 {
1842         DBusMessage *reply;
1843         DBusError error;
1844         DBusMessageIter array, dict;
1845
1846         DBG("");
1847
1848         reply = dbus_pending_call_steal_reply(call);
1849
1850         dbus_error_init(&error);
1851
1852         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1853                 connman_error("%s", error.message);
1854                 dbus_error_free(&error);
1855                 goto done;
1856         }
1857
1858         if (dbus_message_iter_init(reply, &array) == FALSE)
1859                 goto done;
1860
1861         dbus_message_iter_recurse(&array, &dict);
1862
1863         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1864                 DBusMessageIter value, properties;
1865                 const char *path;
1866
1867                 dbus_message_iter_recurse(&dict, &value);
1868                 dbus_message_iter_get_basic(&value, &path);
1869
1870                 dbus_message_iter_next(&value);
1871                 dbus_message_iter_recurse(&value, &properties);
1872
1873                 add_modem(path, &properties);
1874
1875                 dbus_message_iter_next(&dict);
1876         }
1877
1878 done:
1879         dbus_message_unref(reply);
1880
1881         dbus_pending_call_unref(call);
1882 }
1883
1884 static int manager_get_modems(void)
1885 {
1886         DBusMessage *message;
1887         DBusPendingCall *call;
1888
1889         DBG("");
1890
1891         message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1892                                         OFONO_MANAGER_INTERFACE, GET_MODEMS);
1893         if (message == NULL)
1894                 return -ENOMEM;
1895
1896         if (dbus_connection_send_with_reply(connection, message,
1897                                                &call, TIMEOUT) == FALSE) {
1898                 connman_error("Failed to call GetModems()");
1899                 dbus_message_unref(message);
1900                 return -EINVAL;
1901         }
1902
1903         if (call == NULL) {
1904                 connman_error("D-Bus connection not available");
1905                 dbus_message_unref(message);
1906                 return -EINVAL;
1907         }
1908
1909         dbus_pending_call_set_notify(call, manager_get_modems_reply,
1910                                         NULL, NULL);
1911
1912         dbus_message_unref(message);
1913
1914         return -EINPROGRESS;
1915 }
1916
1917 static void ofono_connect(DBusConnection *conn, void *user_data)
1918 {
1919         DBG("");
1920
1921         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1922                                                 g_free, remove_modem);
1923         if (modem_hash == NULL)
1924                 return;
1925
1926         context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1927                                                 g_free, NULL);
1928         if (context_hash == NULL) {
1929                 g_hash_table_destroy(modem_hash);
1930                 return;
1931         }
1932
1933         manager_get_modems();
1934 }
1935
1936 static void ofono_disconnect(DBusConnection *conn, void *user_data)
1937 {
1938         DBG("");
1939
1940         if (modem_hash == NULL || context_hash == NULL)
1941                 return;
1942
1943         g_hash_table_destroy(modem_hash);
1944         modem_hash = NULL;
1945
1946         g_hash_table_destroy(context_hash);
1947         context_hash = NULL;
1948 }
1949
1950 static int network_probe(struct connman_network *network)
1951 {
1952         struct modem_data *modem = connman_network_get_data(network);
1953
1954         DBG("%s network %p", modem->path, network);
1955
1956         return 0;
1957 }
1958
1959 static void network_remove(struct connman_network *network)
1960 {
1961         struct modem_data *modem = connman_network_get_data(network);
1962
1963         DBG("%s network %p", modem->path, network);
1964 }
1965
1966 static int network_connect(struct connman_network *network)
1967 {
1968         struct modem_data *modem = connman_network_get_data(network);
1969
1970         DBG("%s network %p", modem->path, network);
1971
1972         return context_set_active(modem);
1973 }
1974
1975 static int network_disconnect(struct connman_network *network)
1976 {
1977         struct modem_data *modem = connman_network_get_data(network);
1978
1979         DBG("%s network %p", modem->path, network);
1980
1981         return context_set_inactive(modem);
1982 }
1983
1984 static struct connman_network_driver network_driver = {
1985         .name           = "network",
1986         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
1987         .probe          = network_probe,
1988         .remove         = network_remove,
1989         .connect        = network_connect,
1990         .disconnect     = network_disconnect,
1991 };
1992
1993 static int modem_probe(struct connman_device *device)
1994 {
1995         struct modem_data *modem = connman_device_get_data(device);
1996
1997         DBG("%s device %p", modem->path, device);
1998
1999         return 0;
2000 }
2001
2002 static void modem_remove(struct connman_device *device)
2003 {
2004         struct modem_data *modem = connman_device_get_data(device);
2005
2006         DBG("%s device %p", modem->path, device);
2007 }
2008
2009 static int modem_enable(struct connman_device *device)
2010 {
2011         struct modem_data *modem = connman_device_get_data(device);
2012
2013         DBG("%s device %p", modem->path, device);
2014
2015         return 0;
2016 }
2017
2018 static int modem_disable(struct connman_device *device)
2019 {
2020         struct modem_data *modem = connman_device_get_data(device);
2021
2022         DBG("%s device %p", modem->path, device);
2023
2024         return 0;
2025 }
2026
2027 static struct connman_device_driver modem_driver = {
2028         .name           = "modem",
2029         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
2030         .probe          = modem_probe,
2031         .remove         = modem_remove,
2032         .enable         = modem_enable,
2033         .disable        = modem_disable,
2034 };
2035
2036 static guint watch;
2037 static guint modem_added_watch;
2038 static guint modem_removed_watch;
2039 static guint modem_watch;
2040 static guint cm_watch;
2041 static guint sim_watch;
2042 static guint context_added_watch;
2043 static guint context_removed_watch;
2044 static guint netreg_watch;
2045 static guint context_watch;
2046
2047 static int ofono_init(void)
2048 {
2049         int err;
2050
2051         DBG("");
2052
2053         connection = connman_dbus_get_connection();
2054         if (connection == NULL)
2055                 return -EIO;
2056
2057         watch = g_dbus_add_service_watch(connection,
2058                                         OFONO_SERVICE, ofono_connect,
2059                                         ofono_disconnect, NULL, NULL);
2060
2061         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2062                                                 OFONO_MANAGER_INTERFACE,
2063                                                 MODEM_ADDED,
2064                                                 modem_added,
2065                                                 NULL, NULL);
2066
2067         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2068                                                 OFONO_MANAGER_INTERFACE,
2069                                                 MODEM_REMOVED,
2070                                                 modem_removed,
2071                                                 NULL, NULL);
2072
2073         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2074                                                 OFONO_MODEM_INTERFACE,
2075                                                 PROPERTY_CHANGED,
2076                                                 modem_changed,
2077                                                 NULL, NULL);
2078
2079         cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2080                                                 OFONO_CM_INTERFACE,
2081                                                 PROPERTY_CHANGED,
2082                                                 cm_changed,
2083                                                 NULL, NULL);
2084
2085         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2086                                                 OFONO_SIM_INTERFACE,
2087                                                 PROPERTY_CHANGED,
2088                                                 sim_changed,
2089                                                 NULL, NULL);
2090
2091         context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2092                                                 OFONO_CM_INTERFACE,
2093                                                 CONTEXT_ADDED,
2094                                                 cm_context_added,
2095                                                 NULL, NULL);
2096
2097         context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2098                                                 OFONO_CM_INTERFACE,
2099                                                 CONTEXT_REMOVED,
2100                                                 cm_context_removed,
2101                                                 NULL, NULL);
2102
2103         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2104                                                 OFONO_CONTEXT_INTERFACE,
2105                                                 PROPERTY_CHANGED,
2106                                                 context_changed,
2107                                                 NULL, NULL);
2108
2109         netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2110                                                 OFONO_NETREG_INTERFACE,
2111                                                 PROPERTY_CHANGED,
2112                                                 netreg_changed,
2113                                                 NULL, NULL);
2114
2115
2116         if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2117                         modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2118                         context_added_watch == 0 ||
2119                         context_removed_watch == 0 ||
2120                         context_watch == 0 || netreg_watch == 0) {
2121                 err = -EIO;
2122                 goto remove;
2123         }
2124
2125         err = connman_network_driver_register(&network_driver);
2126         if (err < 0)
2127                 goto remove;
2128
2129         err = connman_device_driver_register(&modem_driver);
2130         if (err < 0) {
2131                 connman_network_driver_unregister(&network_driver);
2132                 goto remove;
2133         }
2134
2135         return 0;
2136
2137 remove:
2138         g_dbus_remove_watch(connection, netreg_watch);
2139         g_dbus_remove_watch(connection, context_watch);
2140         g_dbus_remove_watch(connection, context_removed_watch);
2141         g_dbus_remove_watch(connection, context_added_watch);
2142         g_dbus_remove_watch(connection, sim_watch);
2143         g_dbus_remove_watch(connection, cm_watch);
2144         g_dbus_remove_watch(connection, modem_watch);
2145         g_dbus_remove_watch(connection, modem_removed_watch);
2146         g_dbus_remove_watch(connection, modem_added_watch);
2147         g_dbus_remove_watch(connection, watch);
2148         dbus_connection_unref(connection);
2149
2150         return err;
2151 }
2152
2153 static void ofono_exit(void)
2154 {
2155         DBG("");
2156
2157         if (modem_hash != NULL) {
2158                 /*
2159                  * We should propably wait for the SetProperty() reply
2160                  * message, because ...
2161                  */
2162                 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2163
2164                 /*
2165                  * ... here we will cancel the call.
2166                  */
2167                 g_hash_table_destroy(modem_hash);
2168                 modem_hash = NULL;
2169         }
2170
2171         if (context_hash != NULL) {
2172                 g_hash_table_destroy(context_hash);
2173                 context_hash = NULL;
2174         }
2175
2176         connman_device_driver_unregister(&modem_driver);
2177         connman_network_driver_unregister(&network_driver);
2178
2179         g_dbus_remove_watch(connection, netreg_watch);
2180         g_dbus_remove_watch(connection, context_watch);
2181         g_dbus_remove_watch(connection, context_removed_watch);
2182         g_dbus_remove_watch(connection, context_added_watch);
2183         g_dbus_remove_watch(connection, sim_watch);
2184         g_dbus_remove_watch(connection, cm_watch);
2185         g_dbus_remove_watch(connection, modem_watch);
2186         g_dbus_remove_watch(connection, modem_added_watch);
2187         g_dbus_remove_watch(connection, modem_removed_watch);
2188         g_dbus_remove_watch(connection, watch);
2189
2190         dbus_connection_unref(connection);
2191 }
2192
2193 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2194                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)