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