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