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