session: Add configuration plugin
[platform/upstream/connman.git] / plugins / ofono.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <connman/technology.h>
43
44 #include "mcc.h"
45
46 #define OFONO_SERVICE                   "org.ofono"
47
48 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
49 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
50 #define OFONO_SIM_INTERFACE             OFONO_SERVICE ".SimManager"
51 #define OFONO_NETREG_INTERFACE          OFONO_SERVICE ".NetworkRegistration"
52 #define OFONO_CM_INTERFACE              OFONO_SERVICE ".ConnectionManager"
53 #define OFONO_CONTEXT_INTERFACE         OFONO_SERVICE ".ConnectionContext"
54 #define OFONO_CDMA_CM_INTERFACE         OFONO_SERVICE ".cdma.ConnectionManager"
55 #define OFONO_CDMA_NETREG_INTERFACE     OFONO_SERVICE ".cdma.NetworkRegistration"
56
57 #define MODEM_ADDED                     "ModemAdded"
58 #define MODEM_REMOVED                   "ModemRemoved"
59 #define PROPERTY_CHANGED                "PropertyChanged"
60 #define CONTEXT_ADDED                   "ContextAdded"
61 #define CONTEXT_REMOVED                 "ContextRemoved"
62
63 #define GET_PROPERTIES                  "GetProperties"
64 #define SET_PROPERTY                    "SetProperty"
65 #define GET_MODEMS                      "GetModems"
66 #define GET_CONTEXTS                    "GetContexts"
67
68 #define TIMEOUT 40000
69
70 enum ofono_api {
71         OFONO_API_SIM =         0x1,
72         OFONO_API_NETREG =      0x2,
73         OFONO_API_CM =          0x4,
74         OFONO_API_CDMA_NETREG = 0x8,
75         OFONO_API_CDMA_CM =     0x10,
76 };
77
78 /*
79  * The way this plugin works is following:
80  *
81  *   powered -> SubscriberIdentity or Online = True -> gprs, context ->
82  *     attached -> netreg -> ready
83  *
84  * Depending on the modem type, this plugin will behave differently.
85  *
86  * GSM working flow:
87  *
88  * When a new modem appears, the plugin always powers it up. This
89  * allows the plugin to create a connman_device. The core will call
90  * modem_enable() if the technology is enabled. modem_enable() will
91  * then set the modem online. If the technology is disabled then
92  * modem_disable() will just set the modem offline. The modem is
93  * always kept powered all the time.
94  *
95  * After setting the modem online the plugin waits for the
96  * ConnectionManager and ConnectionContext to appear. When the context
97  * signals that it is attached and the NetworkRegistration interface
98  * appears, a new Service will be created and registered at the core.
99  *
100  * When asked to connect to the network (network_connect()) the plugin
101  * will set the Active property on the context. If this operation is
102  * successful the modem is connected to the network. oFono will inform
103  * the plugin about IP configuration through the updating the context's
104  * properties.
105  *
106  * CDMA working flow:
107  *
108  * When a new modem appears, the plugin always powers it up. This
109  * allows the plugin to create connman_device either using IMSI either
110  * using modem Serial if the modem got a SIM interface or not.
111  *
112  * As for GSM, the core will call modem_enable() if the technology
113  * is enabled. modem_enable() will then set the modem online.
114  * If the technology is disabled then modem_disable() will just set the
115  * modem offline. The modem is always kept powered all the time.
116  *
117  * After setting the modem online the plugin waits for CdmaConnectionManager
118  * interface to appear. Then, once CdmaNetworkRegistration appears, a new
119  * Service will be created and registered at the core.
120  *
121  * When asked to connect to the network (network_connect()) the plugin
122  * will power up the CdmaConnectionManager interface.
123  * If the operation is successful the modem is connected to the network.
124  * oFono will inform the plugin about IP configuration through the
125  * updating CdmaConnectionManager settings properties.
126  */
127
128 static DBusConnection *connection;
129
130 static GHashTable *modem_hash;
131 static GHashTable *context_hash;
132
133 struct network_context {
134         char *path;
135         int index;
136
137         enum connman_ipconfig_method ipv4_method;
138         struct connman_ipaddress *ipv4_address;
139         char *ipv4_nameservers;
140
141         enum connman_ipconfig_method ipv6_method;
142         struct connman_ipaddress *ipv6_address;
143         char *ipv6_nameservers;
144 };
145
146 struct modem_data {
147         char *path;
148
149         struct connman_device *device;
150         struct connman_network *network;
151
152         struct network_context *context;
153
154         /* Modem Interface */
155         char *serial;
156         connman_bool_t powered;
157         connman_bool_t online;
158         uint8_t interfaces;
159         connman_bool_t ignore;
160
161         connman_bool_t set_powered;
162
163         /* CDMA ConnectionManager Interface */
164         connman_bool_t cdma_cm_powered;
165
166         /* ConnectionManager Interface */
167         connman_bool_t attached;
168         connman_bool_t cm_powered;
169
170         /* ConnectionContext Interface */
171         connman_bool_t active;
172         connman_bool_t set_active;
173         connman_bool_t valid_apn; /* APN is 'valid' if length > 0 */
174
175         /* SimManager Interface */
176         char *imsi;
177
178         /* Netreg Interface */
179         char *name;
180         uint8_t strength;
181         uint8_t data_strength; /* 1xEVDO signal strength */
182         connman_bool_t roaming;
183
184         /* pending calls */
185         DBusPendingCall *call_set_property;
186         DBusPendingCall *call_get_properties;
187         DBusPendingCall *call_get_contexts;
188 };
189
190 static const char *api2string(enum ofono_api api)
191 {
192         switch (api) {
193         case OFONO_API_SIM:
194                 return "sim";
195         case OFONO_API_NETREG:
196                 return "netreg";
197         case OFONO_API_CM:
198                 return "cm";
199         case OFONO_API_CDMA_NETREG:
200                 return "cdma-netreg";
201         case OFONO_API_CDMA_CM:
202                 return "cmda-cm";
203         }
204
205         return "unknown";
206 }
207
208 static char *get_ident(const char *path)
209 {
210         char *pos;
211
212         if (*path != '/')
213                 return NULL;
214
215         pos = strrchr(path, '/');
216         if (pos == NULL)
217                 return NULL;
218
219         return pos + 1;
220 }
221
222 static struct network_context *network_context_alloc(const char *path)
223 {
224         struct network_context *context;
225
226         context = g_try_new0(struct network_context, 1);
227         if (context == NULL)
228                 return NULL;
229
230         context->path = g_strdup(path);
231         context->index = -1;
232
233         context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
234         context->ipv4_address = NULL;
235         context->ipv4_nameservers = NULL;
236
237         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
238         context->ipv6_address = NULL;
239         context->ipv6_nameservers = NULL;
240
241         return context;
242 }
243
244 static void network_context_free(struct network_context *context)
245 {
246         g_free(context->path);
247
248         connman_ipaddress_free(context->ipv4_address);
249         g_free(context->ipv4_nameservers);
250
251         connman_ipaddress_free(context->ipv6_address);
252         g_free(context->ipv6_nameservers);
253
254         free(context);
255 }
256
257 static void set_connected(struct modem_data *modem)
258 {
259         struct connman_service *service;
260         connman_bool_t setip = FALSE;
261         enum connman_ipconfig_method method;
262         char *nameservers;
263         int index;
264
265         DBG("%s", modem->path);
266
267         index = modem->context->index;
268
269         if (index < 0 || modem->context->ipv4_address == NULL) {
270                 connman_error("Invalid index and/or address");
271                 return;
272         }
273
274         service = connman_service_lookup_from_network(modem->network);
275         if (service == NULL)
276                 return;
277
278         method = modem->context->ipv4_method;
279         if (method == CONNMAN_IPCONFIG_METHOD_FIXED ||
280                         method == CONNMAN_IPCONFIG_METHOD_DHCP)
281         {
282                 connman_service_create_ip4config(service, index);
283                 connman_network_set_index(modem->network, index);
284
285                 connman_network_set_ipv4_method(modem->network, method);
286
287                 setip = TRUE;
288         }
289
290         if (method == CONNMAN_IPCONFIG_METHOD_FIXED) {
291                 connman_network_set_ipaddress(modem->network,
292                                                 modem->context->ipv4_address);
293         }
294
295         method = modem->context->ipv6_method;
296         if (method == CONNMAN_IPCONFIG_METHOD_FIXED) {
297                 connman_service_create_ip6config(service, index);
298                 connman_network_set_ipv6_method(modem->network, method);
299                 connman_network_set_ipaddress(modem->network,
300                                                 modem->context->ipv6_address);
301                 setip = TRUE;
302         }
303
304         /* Set the nameservers */
305         if (modem->context->ipv4_nameservers != NULL &&
306                         modem->context->ipv6_nameservers != NULL) {
307                 nameservers = g_strdup_printf("%s %s",
308                                         modem->context->ipv4_nameservers,
309                                         modem->context->ipv6_nameservers);
310                 connman_network_set_nameservers(modem->network, nameservers);
311                 g_free(nameservers);
312         } else if (modem->context->ipv4_nameservers != NULL) {
313                 connman_network_set_nameservers(modem->network,
314                                         modem->context->ipv4_nameservers);
315         } else if (modem->context->ipv6_nameservers != NULL) {
316                 connman_network_set_nameservers(modem->network,
317                                         modem->context->ipv6_nameservers);
318         }
319
320         if (setip == TRUE)
321                 connman_network_set_connected(modem->network, TRUE);
322 }
323
324 static void set_disconnected(struct modem_data *modem)
325 {
326         DBG("%s", modem->path);
327
328         if (modem->network == NULL)
329                 return;
330
331         connman_network_set_connected(modem->network, FALSE);
332 }
333
334 typedef void (*set_property_cb)(struct modem_data *data,
335                                 connman_bool_t success);
336 typedef void (*get_properties_cb)(struct modem_data *data,
337                                 DBusMessageIter *dict);
338
339 struct property_info {
340         struct modem_data *modem;
341         const char *path;
342         const char *interface;
343         const char *property;
344         set_property_cb set_property_cb;
345         get_properties_cb get_properties_cb;
346 };
347
348 static void set_property_reply(DBusPendingCall *call, void *user_data)
349 {
350         struct property_info *info = user_data;
351         DBusMessage *reply;
352         DBusError error;
353         connman_bool_t success = TRUE;
354
355         DBG("%s path %s %s.%s", info->modem->path,
356                 info->path, info->interface, info->property);
357
358         info->modem->call_set_property = NULL;
359
360         dbus_error_init(&error);
361
362         reply = dbus_pending_call_steal_reply(call);
363
364         if (dbus_set_error_from_message(&error, reply)) {
365                 connman_error("Failed to change property: %s %s.%s: %s %s",
366                                 info->path, info->interface, info->property,
367                                 error.name, error.message);
368                 dbus_error_free(&error);
369                 success = FALSE;
370         }
371
372         if (info->set_property_cb != NULL)
373                 (*info->set_property_cb)(info->modem, success);
374
375         dbus_message_unref(reply);
376
377         dbus_pending_call_unref(call);
378 }
379
380 static int set_property(struct modem_data *modem,
381                         const char *path, const char *interface,
382                         const char *property, int type, void *value,
383                         set_property_cb notify)
384 {
385         DBusMessage *message;
386         DBusMessageIter iter;
387         struct property_info *info;
388
389         DBG("%s path %s %s.%s", modem->path, path, interface, property);
390
391         if (modem->call_set_property != NULL) {
392                 DBG("Cancel pending SetProperty");
393
394                 dbus_pending_call_cancel(modem->call_set_property);
395                 modem->call_set_property = NULL;
396         }
397
398         message = dbus_message_new_method_call(OFONO_SERVICE, path,
399                                         interface, SET_PROPERTY);
400         if (message == NULL)
401                 return -ENOMEM;
402
403         dbus_message_iter_init_append(message, &iter);
404         connman_dbus_property_append_basic(&iter, property, type, value);
405
406         if (dbus_connection_send_with_reply(connection, message,
407                         &modem->call_set_property, TIMEOUT) == FALSE) {
408                 connman_error("Failed to change property: %s %s.%s",
409                                 path, interface, property);
410                 dbus_message_unref(message);
411                 return -EINVAL;
412         }
413
414         if (modem->call_set_property == NULL) {
415                 connman_error("D-Bus connection not available");
416                 dbus_message_unref(message);
417                 return -EINVAL;
418         }
419
420         info = g_try_new0(struct property_info, 1);
421         if (info == NULL) {
422                 dbus_message_unref(message);
423                 return -ENOMEM;
424         }
425
426         info->modem = modem;
427         info->path = path;
428         info->interface = interface;
429         info->property = property;
430         info->set_property_cb = notify;
431
432         dbus_pending_call_set_notify(modem->call_set_property,
433                                         set_property_reply, info, g_free);
434
435         dbus_message_unref(message);
436
437         return -EINPROGRESS;
438 }
439
440 static void get_properties_reply(DBusPendingCall *call, void *user_data)
441 {
442         struct property_info *info = user_data;
443         DBusMessageIter array, dict;
444         DBusMessage *reply;
445         DBusError error;
446
447         DBG("%s path %s %s", info->modem->path, info->path, info->interface);
448
449         info->modem->call_get_properties = NULL;
450
451         dbus_error_init(&error);
452
453         reply = dbus_pending_call_steal_reply(call);
454
455         if (dbus_set_error_from_message(&error, reply)) {
456                 connman_error("Failed to get properties: %s %s: %s %s",
457                                 info->path, info->interface,
458                                 error.name, error.message);
459                 dbus_error_free(&error);
460
461                 goto done;
462         }
463
464         if (dbus_message_iter_init(reply, &array) == FALSE)
465                 goto done;
466
467         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
468                 goto done;
469
470         dbus_message_iter_recurse(&array, &dict);
471
472         if (info->get_properties_cb != NULL)
473                 (*info->get_properties_cb)(info->modem, &dict);
474
475 done:
476
477         dbus_message_unref(reply);
478
479         dbus_pending_call_unref(call);
480 }
481
482 static int get_properties(const char *path, const char *interface,
483                                 get_properties_cb notify,
484                                 struct modem_data *modem)
485 {
486         DBusMessage *message;
487         struct property_info *info;
488
489         DBG("%s path %s %s", modem->path, path, interface);
490
491         if (modem->call_get_properties != NULL) {
492                 connman_error("Pending GetProperties");
493                 return -EBUSY;
494         }
495
496         message = dbus_message_new_method_call(OFONO_SERVICE, path,
497                                         interface, GET_PROPERTIES);
498         if (message == NULL)
499                 return -ENOMEM;
500
501         if (dbus_connection_send_with_reply(connection, message,
502                         &modem->call_get_properties, TIMEOUT) == FALSE) {
503                 connman_error("Failed to call %s.GetProperties()", interface);
504                 dbus_message_unref(message);
505                 return -EINVAL;
506         }
507
508         if (modem->call_get_properties == NULL) {
509                 connman_error("D-Bus connection not available");
510                 dbus_message_unref(message);
511                 return -EINVAL;
512         }
513
514         info = g_try_new0(struct property_info, 1);
515         if (info == NULL) {
516                 dbus_message_unref(message);
517                 return -ENOMEM;
518         }
519
520         info->modem = modem;
521         info->path = path;
522         info->interface = interface;
523         info->get_properties_cb = notify;
524
525         dbus_pending_call_set_notify(modem->call_get_properties,
526                                         get_properties_reply, info, g_free);
527
528         dbus_message_unref(message);
529
530         return -EINPROGRESS;
531 }
532
533 static void context_set_active_reply(struct modem_data *modem,
534                                         connman_bool_t success)
535 {
536         DBG("%s", modem->path);
537
538         if (success == TRUE) {
539                 /*
540                  * Don't handle do anything on success here. oFono will send
541                  * the change via PropertyChanged singal.
542                  */
543                 return;
544         }
545
546         /*
547          * Active = True might fail due a timeout. That means oFono
548          * still tries to go online. If we retry to set Active = True,
549          * we just get a InProgress error message. Should we power
550          * cycle the modem in such cases?
551          */
552
553         if (modem->network == NULL) {
554                 /*
555                  * In the case where we power down the device
556                  * we don't wait for the reply, therefore the network
557                  * might already be gone.
558                  */
559                 return;
560         }
561
562         connman_network_set_error(modem->network,
563                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
564 }
565
566 static int context_set_active(struct modem_data *modem,
567                                 connman_bool_t active)
568 {
569         int err;
570
571         DBG("%s active %d", modem->path, active);
572
573         err = set_property(modem, modem->context->path,
574                                 OFONO_CONTEXT_INTERFACE,
575                                 "Active", DBUS_TYPE_BOOLEAN,
576                                 &active,
577                                 context_set_active_reply);
578
579         if (active == FALSE && err == -EINPROGRESS)
580                 return 0;
581
582         return err;
583 }
584
585 static void cdma_cm_set_powered_reply(struct modem_data *modem,
586                                         connman_bool_t success)
587 {
588         DBG("%s", modem->path);
589
590         if (success == TRUE) {
591                 /*
592                  * Don't handle do anything on success here. oFono will send
593                  * the change via PropertyChanged singal.
594                  */
595                 return;
596         }
597
598         /*
599          * Powered = True might fail due a timeout. That means oFono
600          * still tries to go online. If we retry to set Powered = True,
601          * we just get a InProgress error message. Should we power
602          * cycle the modem in such cases?
603          */
604
605         if (modem->network == NULL) {
606                 /*
607                  * In the case where we power down the device
608                  * we don't wait for the reply, therefore the network
609                  * might already be gone.
610                  */
611                 return;
612         }
613
614         connman_network_set_error(modem->network,
615                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
616 }
617
618 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
619 {
620         int err;
621
622         DBG("%s powered %d", modem->path, powered);
623
624         err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
625                                 "Powered", DBUS_TYPE_BOOLEAN,
626                                 &powered,
627                                 cdma_cm_set_powered_reply);
628
629         if (powered == FALSE && err == -EINPROGRESS)
630                 return 0;
631
632         return err;
633 }
634
635 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
636 {
637         DBG("%s online %d", modem->path, online);
638
639         return set_property(modem, modem->path,
640                                 OFONO_MODEM_INTERFACE,
641                                 "Online", DBUS_TYPE_BOOLEAN,
642                                 &online,
643                                 NULL);
644 }
645
646 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
647 {
648         int err;
649
650         DBG("%s powered %d", modem->path, powered);
651
652         err = set_property(modem, modem->path,
653                                 OFONO_CM_INTERFACE,
654                                 "Powered", DBUS_TYPE_BOOLEAN,
655                                 &powered,
656                                 NULL);
657
658         if (powered == FALSE && err == -EINPROGRESS)
659                 return 0;
660
661         return err;
662 }
663
664 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
665 {
666         int err;
667
668         DBG("%s powered %d", modem->path, powered);
669
670         modem->set_powered = powered;
671
672         err = set_property(modem, modem->path,
673                                 OFONO_MODEM_INTERFACE,
674                                 "Powered", DBUS_TYPE_BOOLEAN,
675                                 &powered,
676                                 NULL);
677
678         if (powered == FALSE && err == -EINPROGRESS)
679                 return 0;
680
681         return err;
682 }
683
684 static connman_bool_t has_interface(uint8_t interfaces,
685                                         enum ofono_api api)
686 {
687         if ((interfaces & api) == api)
688                 return TRUE;
689
690         return FALSE;
691 }
692
693 static uint8_t extract_interfaces(DBusMessageIter *array)
694 {
695         DBusMessageIter entry;
696         uint8_t interfaces = 0;
697
698         dbus_message_iter_recurse(array, &entry);
699
700         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
701                 const char *name;
702
703                 dbus_message_iter_get_basic(&entry, &name);
704
705                 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
706                         interfaces |= OFONO_API_SIM;
707                 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
708                         interfaces |= OFONO_API_NETREG;
709                 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
710                         interfaces |= OFONO_API_CM;
711                 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
712                         interfaces |= OFONO_API_CDMA_CM;
713                 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
714                         interfaces |= OFONO_API_CDMA_NETREG;
715
716                 dbus_message_iter_next(&entry);
717         }
718
719         return interfaces;
720 }
721
722 static char *extract_nameservers(DBusMessageIter *array)
723 {
724         DBusMessageIter entry;
725         char *nameservers = NULL;
726         char *tmp;
727
728         dbus_message_iter_recurse(array, &entry);
729
730         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
731                 const char *nameserver;
732
733                 dbus_message_iter_get_basic(&entry, &nameserver);
734
735                 if (nameservers == NULL) {
736                         nameservers = g_strdup(nameserver);
737                 } else {
738                         tmp = nameservers;
739                         nameservers = g_strdup_printf("%s %s", tmp, nameserver);
740                         g_free(tmp);
741                 }
742
743                 dbus_message_iter_next(&entry);
744         }
745
746         return nameservers;
747 }
748
749 static void extract_ipv4_settings(DBusMessageIter *array,
750                                 struct network_context *context)
751 {
752         DBusMessageIter dict;
753         char *address = NULL, *netmask = NULL, *gateway = NULL;
754         char *nameservers = NULL;
755         const char *interface = NULL;
756         int index = -1;
757
758         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
759                 return;
760
761         dbus_message_iter_recurse(array, &dict);
762
763         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
764                 DBusMessageIter entry, value;
765                 const char *key, *val;
766
767                 dbus_message_iter_recurse(&dict, &entry);
768                 dbus_message_iter_get_basic(&entry, &key);
769
770                 dbus_message_iter_next(&entry);
771                 dbus_message_iter_recurse(&entry, &value);
772
773                 if (g_str_equal(key, "Interface") == TRUE) {
774                         dbus_message_iter_get_basic(&value, &interface);
775
776                         DBG("Interface %s", interface);
777
778                         index = connman_inet_ifindex(interface);
779
780                         DBG("index %d", index);
781                 } else if (g_str_equal(key, "Method") == TRUE) {
782                         dbus_message_iter_get_basic(&value, &val);
783
784                         DBG("Method %s", val);
785
786                         if (g_strcmp0(val, "static") == 0) {
787                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
788                         } else if (g_strcmp0(val, "dhcp") == 0) {
789                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
790                                 break;
791                         }
792                 } else if (g_str_equal(key, "Address") == TRUE) {
793                         dbus_message_iter_get_basic(&value, &val);
794
795                         address = g_strdup(val);
796
797                         DBG("Address %s", address);
798                 } else if (g_str_equal(key, "Netmask") == TRUE) {
799                         dbus_message_iter_get_basic(&value, &val);
800
801                         netmask = g_strdup(val);
802
803                         DBG("Netmask %s", netmask);
804                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
805                         nameservers = extract_nameservers(&value);
806
807                         DBG("Nameservers %s", nameservers);
808                 } else if (g_str_equal(key, "Gateway") == TRUE) {
809                         dbus_message_iter_get_basic(&value, &val);
810
811                         gateway = g_strdup(val);
812
813                         DBG("Gateway %s", gateway);
814                 }
815
816                 dbus_message_iter_next(&dict);
817         }
818
819         if (index < 0)
820                 goto out;
821
822         if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
823                 goto out;
824
825         context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
826         if (context->ipv4_address == NULL)
827                 goto out;
828
829         context->index = index;
830         connman_ipaddress_set_ipv4(context->ipv4_address, address,
831                                 netmask, gateway);
832
833         context->ipv4_nameservers = nameservers;
834
835 out:
836         if (context->ipv4_nameservers != nameservers)
837                 g_free(nameservers);
838
839         g_free(address);
840         g_free(netmask);
841         g_free(gateway);
842 }
843
844 static void extract_ipv6_settings(DBusMessageIter *array,
845                                 struct network_context *context)
846 {
847         DBusMessageIter dict;
848         char *address = NULL, *gateway = NULL;
849         unsigned char prefix_length = 0;
850         char *nameservers = NULL;
851         const char *interface = NULL;
852         int index = -1;
853
854         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
855                 return;
856
857         dbus_message_iter_recurse(array, &dict);
858
859         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
860                 DBusMessageIter entry, value;
861                 const char *key, *val;
862
863                 dbus_message_iter_recurse(&dict, &entry);
864                 dbus_message_iter_get_basic(&entry, &key);
865
866                 dbus_message_iter_next(&entry);
867                 dbus_message_iter_recurse(&entry, &value);
868
869                 if (g_str_equal(key, "Interface") == TRUE) {
870                         dbus_message_iter_get_basic(&value, &interface);
871
872                         DBG("Interface %s", interface);
873
874                         index = connman_inet_ifindex(interface);
875
876                         DBG("index %d", index);
877                 } else if (g_str_equal(key, "Address") == TRUE) {
878                         dbus_message_iter_get_basic(&value, &val);
879
880                         address = g_strdup(val);
881
882                         DBG("Address %s", address);
883                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
884                         dbus_message_iter_get_basic(&value, &prefix_length);
885
886                         DBG("prefix length %d", prefix_length);
887                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
888                         nameservers = extract_nameservers(&value);
889
890                         DBG("Nameservers %s", nameservers);
891                 } else if (g_str_equal(key, "Gateway") == TRUE) {
892                         dbus_message_iter_get_basic(&value, &val);
893
894                         gateway = g_strdup(val);
895
896                         DBG("Gateway %s", gateway);
897                 }
898
899                 dbus_message_iter_next(&dict);
900         }
901
902         if (index < 0)
903                 goto out;
904
905         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
906
907         context->ipv6_address =
908                 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
909         if (context->ipv6_address == NULL)
910                 goto out;
911
912         context->index = index;
913         connman_ipaddress_set_ipv6(context->ipv6_address, address,
914                                 prefix_length, gateway);
915
916         context->ipv6_nameservers = nameservers;
917
918 out:
919         if (context->ipv6_nameservers != nameservers)
920                 g_free(nameservers);
921
922         g_free(address);
923         g_free(gateway);
924 }
925
926 static connman_bool_t ready_to_create_device(struct modem_data *modem)
927 {
928         /*
929          * There are three different modem types which behave slightly
930          * different:
931          * - GSM modems will expose the SIM interface then the
932          *   CM interface.
933          * - CDMA modems will expose CM first and sometime later
934          *   a unique serial number.
935          *
936          * This functions tests if we have the necessary information gathered
937          * before we are able to create a device.
938          */
939
940         if (modem->device != NULL)
941                 return FALSE;
942
943         if (modem->imsi != NULL || modem->serial != NULL)
944                 return TRUE;
945
946         return FALSE;
947 }
948
949 static void create_device(struct modem_data *modem)
950 {
951         struct connman_device *device;
952         char *ident = NULL;
953
954         DBG("%s", modem->path);
955
956         if (modem->imsi != NULL)
957                 ident = modem->imsi;
958         else if (modem->serial != NULL)
959                 ident = modem->serial;
960
961         if (connman_dbus_validate_ident(ident) == FALSE)
962                 ident = connman_dbus_encode_string(ident);
963         else
964                 ident = g_strdup(ident);
965
966         device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
967         if (device == NULL)
968                 goto out;
969
970         DBG("device %p", device);
971
972         connman_device_set_ident(device, ident);
973
974         connman_device_set_string(device, "Path", modem->path);
975
976         connman_device_set_data(device, modem);
977
978         if (connman_device_register(device) < 0) {
979                 connman_error("Failed to register cellular device");
980                 connman_device_unref(device);
981                 goto out;
982         }
983
984         modem->device = device;
985
986         connman_device_set_powered(modem->device, modem->online);
987 out:
988         g_free(ident);
989 }
990
991 static void destroy_device(struct modem_data *modem)
992 {
993         DBG("%s", modem->path);
994
995         connman_device_set_powered(modem->device, FALSE);
996
997         if (modem->network != NULL) {
998                 connman_device_remove_network(modem->device, modem->network);
999                 connman_network_unref(modem->network);
1000                 modem->network = NULL;
1001         }
1002
1003         connman_device_unregister(modem->device);
1004         connman_device_unref(modem->device);
1005
1006         modem->device = NULL;
1007 }
1008
1009 static void add_network(struct modem_data *modem)
1010 {
1011         const char *group;
1012
1013         DBG("%s", modem->path);
1014
1015         if (modem->network != NULL)
1016                 return;
1017
1018         modem->network = connman_network_create(modem->context->path,
1019                                                 CONNMAN_NETWORK_TYPE_CELLULAR);
1020         if (modem->network == NULL)
1021                 return;
1022
1023         DBG("network %p", modem->network);
1024
1025         connman_network_set_data(modem->network, modem);
1026
1027         connman_network_set_string(modem->network, "Path",
1028                                         modem->context->path);
1029
1030         if (modem->name != NULL)
1031                 connman_network_set_name(modem->network, modem->name);
1032         else
1033                 connman_network_set_name(modem->network, "");
1034
1035         connman_network_set_strength(modem->network, modem->strength);
1036
1037         group = get_ident(modem->context->path);
1038         connman_network_set_group(modem->network, group);
1039
1040         connman_network_set_available(modem->network, TRUE);
1041
1042         connman_network_set_bool(modem->network, "Roaming",
1043                                         modem->roaming);
1044
1045         if (connman_device_add_network(modem->device, modem->network) < 0) {
1046                 connman_network_unref(modem->network);
1047                 modem->network = NULL;
1048                 return;
1049         }
1050 }
1051
1052 static void remove_network(struct modem_data *modem)
1053 {
1054         DBG("%s", modem->path);
1055
1056         if (modem->network == NULL)
1057                 return;
1058
1059         DBG("network %p", modem->network);
1060
1061         connman_device_remove_network(modem->device, modem->network);
1062         connman_network_unref(modem->network);
1063         modem->network = NULL;
1064 }
1065
1066 static int add_cm_context(struct modem_data *modem, const char *context_path,
1067                                 DBusMessageIter *dict)
1068 {
1069         const char *context_type = NULL;
1070         struct network_context *context = NULL;
1071         connman_bool_t active = FALSE;
1072
1073         DBG("%s context path %s", modem->path, context_path);
1074
1075         if (modem->context != NULL) {
1076                 /*
1077                  * We have already assigned a context to this modem
1078                  * and we do only support one Internet context.
1079                  */
1080                 return -EALREADY;
1081         }
1082
1083         context = network_context_alloc(context_path);
1084         if (context == NULL)
1085                 return -ENOMEM;
1086
1087         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1088                 DBusMessageIter entry, value;
1089                 const char *key;
1090
1091                 dbus_message_iter_recurse(dict, &entry);
1092                 dbus_message_iter_get_basic(&entry, &key);
1093
1094                 dbus_message_iter_next(&entry);
1095                 dbus_message_iter_recurse(&entry, &value);
1096
1097                 if (g_str_equal(key, "Type") == TRUE) {
1098                         dbus_message_iter_get_basic(&value, &context_type);
1099
1100                         DBG("%s context %s type %s", modem->path,
1101                                 context_path, context_type);
1102                 } else if (g_str_equal(key, "Settings") == TRUE) {
1103                         DBG("%s Settings", modem->path);
1104
1105                         extract_ipv4_settings(&value, context);
1106                 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1107                         DBG("%s IPv6.Settings", modem->path);
1108
1109                         extract_ipv6_settings(&value, context);
1110                 } else if (g_str_equal(key, "Active") == TRUE) {
1111                         dbus_message_iter_get_basic(&value, &active);
1112
1113                         DBG("%s Active %d", modem->path, active);
1114                 } else if (g_str_equal(key, "AccessPointName") == TRUE) {
1115                         const char *apn;
1116
1117                         dbus_message_iter_get_basic(&value, &apn);
1118                         if (apn != NULL && strlen(apn) > 0)
1119                                 modem->valid_apn = TRUE;
1120                         else
1121                                 modem->valid_apn = FALSE;
1122
1123                         DBG("%s AccessPointName '%s'", modem->path, apn);
1124                 }
1125                 dbus_message_iter_next(dict);
1126         }
1127
1128         if (g_strcmp0(context_type, "internet") != 0) {
1129                 network_context_free(context);
1130                 return -EINVAL;
1131         }
1132
1133         modem->context = context;
1134         modem->active = active;
1135
1136         g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1137
1138         if (modem->valid_apn == TRUE && modem->attached == TRUE &&
1139                         has_interface(modem->interfaces,
1140                                 OFONO_API_NETREG) == TRUE) {
1141                 add_network(modem);
1142         }
1143
1144         return 0;
1145 }
1146
1147 static void remove_cm_context(struct modem_data *modem,
1148                                 const char *context_path)
1149 {
1150         if (modem->context == NULL)
1151                 return;
1152
1153         if (modem->network != NULL)
1154                 remove_network(modem);
1155
1156         g_hash_table_remove(context_hash, context_path);
1157
1158         network_context_free(modem->context);
1159         modem->context = NULL;
1160
1161         modem->valid_apn = FALSE;
1162
1163         if (modem->network != NULL)
1164                 remove_network(modem);
1165 }
1166
1167 static gboolean context_changed(DBusConnection *conn,
1168                                 DBusMessage *message,
1169                                 void *user_data)
1170 {
1171         const char *context_path = dbus_message_get_path(message);
1172         struct modem_data *modem = NULL;
1173         DBusMessageIter iter, value;
1174         const char *key;
1175
1176         DBG("context_path %s", context_path);
1177
1178         modem = g_hash_table_lookup(context_hash, context_path);
1179         if (modem == NULL)
1180                 return TRUE;
1181
1182         if (dbus_message_iter_init(message, &iter) == FALSE)
1183                 return TRUE;
1184
1185         dbus_message_iter_get_basic(&iter, &key);
1186
1187         dbus_message_iter_next(&iter);
1188         dbus_message_iter_recurse(&iter, &value);
1189
1190         /*
1191          * oFono guarantees the ordering of Settings and
1192          * Active. Settings will always be send before Active = True.
1193          * That means we don't have to order here.
1194          */
1195         if (g_str_equal(key, "Settings") == TRUE) {
1196                 DBG("%s Settings", modem->path);
1197
1198                 extract_ipv4_settings(&value, modem->context);
1199         } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1200                 DBG("%s IPv6.Settings", modem->path);
1201
1202                 extract_ipv6_settings(&value, modem->context);
1203         } else if (g_str_equal(key, "Active") == TRUE) {
1204                 dbus_message_iter_get_basic(&value, &modem->active);
1205
1206                 DBG("%s Active %d", modem->path, modem->active);
1207
1208                 if (modem->active == TRUE)
1209                         set_connected(modem);
1210                 else
1211                         set_disconnected(modem);
1212         } else if (g_str_equal(key, "AccessPointName") == TRUE) {
1213                 const char *apn;
1214
1215                 dbus_message_iter_get_basic(&value, &apn);
1216
1217                 DBG("%s AccessPointName %s", modem->path, apn);
1218
1219                 if (apn != NULL && strlen(apn) > 0) {
1220                         modem->valid_apn = TRUE;
1221
1222                         if (modem->network != NULL)
1223                                 return TRUE;
1224
1225                         if (modem->attached == FALSE)
1226                                 return TRUE;
1227
1228                         if (has_interface(modem->interfaces,
1229                                         OFONO_API_NETREG) == FALSE) {
1230                                 return TRUE;
1231                         }
1232
1233                         add_network(modem);
1234
1235                         if (modem->active == TRUE)
1236                                 set_connected(modem);
1237                 } else {
1238                         modem->valid_apn = FALSE;
1239
1240                         if (modem->network == NULL)
1241                                 return TRUE;
1242
1243                         remove_network(modem);
1244                 }
1245         }
1246
1247         return TRUE;
1248 }
1249
1250 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1251 {
1252         struct modem_data *modem = user_data;
1253         DBusMessageIter array, dict, entry, value;
1254         DBusMessage *reply;
1255         DBusError error;
1256
1257         DBG("%s", modem->path);
1258
1259         modem->call_get_contexts = NULL;
1260
1261         reply = dbus_pending_call_steal_reply(call);
1262
1263         dbus_error_init(&error);
1264
1265         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1266                 connman_error("%s", error.message);
1267                 dbus_error_free(&error);
1268                 goto done;
1269         }
1270
1271         if (dbus_message_iter_init(reply, &array) == FALSE)
1272                 goto done;
1273
1274         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1275                 goto done;
1276
1277         dbus_message_iter_recurse(&array, &dict);
1278
1279         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1280                 const char *context_path;
1281
1282                 dbus_message_iter_recurse(&dict, &entry);
1283                 dbus_message_iter_get_basic(&entry, &context_path);
1284
1285                 dbus_message_iter_next(&entry);
1286                 dbus_message_iter_recurse(&entry, &value);
1287
1288                 if (add_cm_context(modem, context_path, &value) == 0)
1289                         break;
1290
1291                 dbus_message_iter_next(&dict);
1292         }
1293
1294 done:
1295         dbus_message_unref(reply);
1296
1297         dbus_pending_call_unref(call);
1298 }
1299
1300 static int cm_get_contexts(struct modem_data *modem)
1301 {
1302         DBusMessage *message;
1303
1304         DBG("%s", modem->path);
1305
1306         if (modem->call_get_contexts != NULL)
1307                 return -EBUSY;
1308
1309         message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1310                                         OFONO_CM_INTERFACE, GET_CONTEXTS);
1311         if (message == NULL)
1312                 return -ENOMEM;
1313
1314         if (dbus_connection_send_with_reply(connection, message,
1315                         &modem->call_get_contexts, TIMEOUT) == FALSE) {
1316                 connman_error("Failed to call GetContexts()");
1317                 dbus_message_unref(message);
1318                 return -EINVAL;
1319         }
1320
1321         if (modem->call_get_contexts == NULL) {
1322                 connman_error("D-Bus connection not available");
1323                 dbus_message_unref(message);
1324                 return -EINVAL;
1325         }
1326
1327         dbus_pending_call_set_notify(modem->call_get_contexts,
1328                                         cm_get_contexts_reply,
1329                                         modem, NULL);
1330
1331         dbus_message_unref(message);
1332
1333         return -EINPROGRESS;
1334 }
1335
1336 static gboolean cm_context_added(DBusConnection *conn,
1337                                         DBusMessage *message,
1338                                         void *user_data)
1339 {
1340         const char *path = dbus_message_get_path(message);
1341         char *context_path;
1342         struct modem_data *modem;
1343         DBusMessageIter iter, properties;
1344
1345         DBG("%s", path);
1346
1347         modem = g_hash_table_lookup(modem_hash, path);
1348         if (modem == NULL)
1349                 return TRUE;
1350
1351         if (dbus_message_iter_init(message, &iter) == FALSE)
1352                 return TRUE;
1353
1354         dbus_message_iter_get_basic(&iter, &context_path);
1355
1356         dbus_message_iter_next(&iter);
1357         dbus_message_iter_recurse(&iter, &properties);
1358
1359         if (add_cm_context(modem, context_path, &properties) != 0)
1360                 return TRUE;
1361
1362         return TRUE;
1363 }
1364
1365 static gboolean cm_context_removed(DBusConnection *conn,
1366                                         DBusMessage *message,
1367                                         void *user_data)
1368 {
1369         const char *path = dbus_message_get_path(message);
1370         const char *context_path;
1371         struct modem_data *modem;
1372         DBusMessageIter iter;
1373
1374         DBG("context path %s", path);
1375
1376         if (dbus_message_iter_init(message, &iter) == FALSE)
1377                 return TRUE;
1378
1379         dbus_message_iter_get_basic(&iter, &context_path);
1380
1381         modem = g_hash_table_lookup(context_hash, context_path);
1382         if (modem == NULL)
1383                 return TRUE;
1384
1385         remove_cm_context(modem, context_path);
1386
1387         return TRUE;
1388 }
1389
1390 static void netreg_update_name(struct modem_data *modem,
1391                                 DBusMessageIter* value)
1392 {
1393         char *name;
1394
1395         dbus_message_iter_get_basic(value, &name);
1396
1397         DBG("%s Name %s", modem->path, name);
1398
1399         g_free(modem->name);
1400         modem->name = g_strdup(name);
1401
1402         if (modem->network == NULL)
1403                 return;
1404
1405         connman_network_set_name(modem->network, modem->name);
1406         connman_network_update(modem->network);
1407 }
1408
1409 static void netreg_update_strength(struct modem_data *modem,
1410                                         DBusMessageIter *value)
1411 {
1412         dbus_message_iter_get_basic(value, &modem->strength);
1413
1414         DBG("%s Strength %d", modem->path, modem->strength);
1415
1416         if (modem->network == NULL)
1417                 return;
1418
1419         /*
1420          * GSM:
1421          * We don't have 2 signal notifications we always report the strength
1422          * signal. data_strength is always equal to 0.
1423          *
1424          * CDMA:
1425          * In the case we have a data_strength signal (from 1xEVDO network)
1426          * we don't need to update the value with strength signal (from 1xCDMA)
1427          * because the modem is registered to 1xEVDO network for data call.
1428          * In case we have no data_strength signal (not registered to 1xEVDO
1429          * network), we must report the strength signal (registered to 1xCDMA
1430          * network e.g slow mode).
1431          */
1432         if (modem->data_strength != 0)
1433                 return;
1434
1435         connman_network_set_strength(modem->network, modem->strength);
1436         connman_network_update(modem->network);
1437 }
1438
1439 /* Retrieve 1xEVDO Data Strength signal */
1440 static void netreg_update_datastrength(struct modem_data *modem,
1441                                         DBusMessageIter *value)
1442 {
1443         dbus_message_iter_get_basic(value, &modem->data_strength);
1444
1445         DBG("%s Data Strength %d", modem->path, modem->data_strength);
1446
1447         if (modem->network == NULL)
1448                 return;
1449
1450         /*
1451          * CDMA modem is not registered to 1xEVDO network, let
1452          * update_signal_strength() reporting the value on the Strength signal
1453          * notification.
1454          */
1455         if (modem->data_strength == 0)
1456                 return;
1457
1458         connman_network_set_strength(modem->network, modem->data_strength);
1459         connman_network_update(modem->network);
1460 }
1461
1462 static void netreg_update_roaming(struct modem_data *modem,
1463                                         DBusMessageIter *value)
1464 {
1465         char *status;
1466         connman_bool_t roaming;
1467
1468         dbus_message_iter_get_basic(value, &status);
1469
1470         if (g_str_equal(status, "roaming") == TRUE)
1471                 roaming = TRUE;
1472         else
1473                 roaming = FALSE;
1474
1475         if (roaming == modem->roaming)
1476                 return;
1477
1478         modem->roaming = roaming;
1479
1480         if (modem->network == NULL)
1481                 return;
1482
1483         connman_network_set_bool(modem->network,
1484                                 "Roaming", modem->roaming);
1485         connman_network_update(modem->network);
1486 }
1487
1488 static void netreg_update_regdom(struct modem_data *modem,
1489                                 DBusMessageIter *value)
1490 {
1491         char *mobile_country_code;
1492         char *alpha2;
1493         int mcc;
1494
1495         dbus_message_iter_get_basic(value, &mobile_country_code);
1496
1497         DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1498
1499
1500         mcc = atoi(mobile_country_code);
1501         if (mcc > 799 || mcc < 200)
1502                 return;
1503
1504         alpha2 = mcc_country_codes[mcc - 200];
1505         if (alpha2 != NULL)
1506                 connman_technology_set_regdom(alpha2);
1507 }
1508
1509 static gboolean netreg_changed(DBusConnection *conn, DBusMessage *message,
1510                                 void *user_data)
1511 {
1512         const char *path = dbus_message_get_path(message);
1513         struct modem_data *modem;
1514         DBusMessageIter iter, value;
1515         const char *key;
1516
1517         modem = g_hash_table_lookup(modem_hash, path);
1518         if (modem == NULL)
1519                 return TRUE;
1520
1521         if (modem->ignore == TRUE)
1522                 return TRUE;
1523
1524         if (dbus_message_iter_init(message, &iter) == FALSE)
1525                 return TRUE;
1526
1527         dbus_message_iter_get_basic(&iter, &key);
1528
1529         dbus_message_iter_next(&iter);
1530         dbus_message_iter_recurse(&iter, &value);
1531
1532         if (g_str_equal(key, "Name") == TRUE)
1533                 netreg_update_name(modem, &value);
1534         else if (g_str_equal(key, "Strength") == TRUE)
1535                 netreg_update_strength(modem, &value);
1536         else if (g_str_equal(key, "Status") == TRUE)
1537                 netreg_update_roaming(modem, &value);
1538         else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1539                 netreg_update_regdom(modem, &value);
1540
1541         return TRUE;
1542 }
1543
1544 static void netreg_properties_reply(struct modem_data *modem,
1545                                         DBusMessageIter *dict)
1546 {
1547         DBG("%s", modem->path);
1548
1549         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1550                 DBusMessageIter entry, value;
1551                 const char *key;
1552
1553                 dbus_message_iter_recurse(dict, &entry);
1554                 dbus_message_iter_get_basic(&entry, &key);
1555
1556                 dbus_message_iter_next(&entry);
1557                 dbus_message_iter_recurse(&entry, &value);
1558
1559                 if (g_str_equal(key, "Name") == TRUE)
1560                         netreg_update_name(modem, &value);
1561                 else if (g_str_equal(key, "Strength") == TRUE)
1562                         netreg_update_strength(modem, &value);
1563                 else if (g_str_equal(key, "Status") == TRUE)
1564                         netreg_update_roaming(modem, &value);
1565                 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1566                         netreg_update_regdom(modem, &value);
1567
1568                 dbus_message_iter_next(dict);
1569         }
1570
1571         if (modem->context == NULL) {
1572                 /*
1573                  * netgreg_get_properties() was issued after we got
1574                  * cm_get_contexts_reply() where we create the
1575                  * context. Though before we got the
1576                  * netreg_properties_reply the context was removed
1577                  * again. Therefore we have to skip the network
1578                  * creation.
1579                  */
1580                 return;
1581         }
1582
1583         if (modem->valid_apn == TRUE)
1584                 add_network(modem);
1585
1586         if (modem->active == TRUE)
1587                 set_connected(modem);
1588 }
1589
1590 static int netreg_get_properties(struct modem_data *modem)
1591 {
1592         return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1593                         netreg_properties_reply, modem);
1594 }
1595
1596 static void add_cdma_network(struct modem_data *modem)
1597 {
1598         /* Be sure that device is created before adding CDMA network */
1599         if (modem->device == NULL)
1600                 return;
1601
1602         /*
1603          * CDMA modems don't need contexts for data call, however the current
1604          * add_network() logic needs one, so we create one to proceed.
1605          */
1606         if (modem->context == NULL)
1607                 modem->context = network_context_alloc(modem->path);
1608
1609         if (modem->name == NULL)
1610                 modem->name = g_strdup("CDMA Network");
1611
1612         add_network(modem);
1613
1614         if (modem->cdma_cm_powered == TRUE)
1615                 set_connected(modem);
1616 }
1617
1618 static gboolean cdma_netreg_changed(DBusConnection *conn,
1619                                         DBusMessage *message,
1620                                         void *user_data)
1621 {
1622         const char *path = dbus_message_get_path(message);
1623         struct modem_data *modem;
1624         DBusMessageIter iter, value;
1625         const char *key;
1626
1627         DBG("");
1628
1629         modem = g_hash_table_lookup(modem_hash, path);
1630         if (modem == NULL)
1631                 return TRUE;
1632
1633         if (modem->ignore == TRUE)
1634                 return TRUE;
1635
1636         if (dbus_message_iter_init(message, &iter) == FALSE)
1637                 return TRUE;
1638
1639         dbus_message_iter_get_basic(&iter, &key);
1640
1641         dbus_message_iter_next(&iter);
1642         dbus_message_iter_recurse(&iter, &value);
1643
1644         if (g_str_equal(key, "Name") == TRUE)
1645                 netreg_update_name(modem, &value);
1646         else if (g_str_equal(key, "Strength") == TRUE)
1647                 netreg_update_strength(modem, &value);
1648         else if (g_str_equal(key, "DataStrength") == TRUE)
1649                 netreg_update_datastrength(modem, &value);
1650         else if (g_str_equal(key, "Status") == TRUE)
1651                 netreg_update_roaming(modem, &value);
1652
1653         add_cdma_network(modem);
1654
1655         return TRUE;
1656 }
1657
1658 static void cdma_netreg_properties_reply(struct modem_data *modem,
1659                                         DBusMessageIter *dict)
1660 {
1661         DBG("%s", modem->path);
1662
1663         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1664                 DBusMessageIter entry, value;
1665                 const char *key;
1666
1667                 dbus_message_iter_recurse(dict, &entry);
1668                 dbus_message_iter_get_basic(&entry, &key);
1669
1670                 dbus_message_iter_next(&entry);
1671                 dbus_message_iter_recurse(&entry, &value);
1672
1673                 if (g_str_equal(key, "Name") == TRUE)
1674                         netreg_update_name(modem, &value);
1675                 else if (g_str_equal(key, "Strength") == TRUE)
1676                         netreg_update_strength(modem, &value);
1677                 else if (g_str_equal(key, "DataStrength") == TRUE)
1678                         netreg_update_datastrength(modem, &value);
1679                 else if (g_str_equal(key, "Status") == TRUE)
1680                         netreg_update_roaming(modem, &value);
1681
1682                 dbus_message_iter_next(dict);
1683         }
1684
1685         add_cdma_network(modem);
1686 }
1687
1688 static int cdma_netreg_get_properties(struct modem_data *modem)
1689 {
1690         return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1691                         cdma_netreg_properties_reply, modem);
1692 }
1693
1694 static void cm_update_attached(struct modem_data *modem,
1695                                 DBusMessageIter *value)
1696 {
1697         dbus_message_iter_get_basic(value, &modem->attached);
1698
1699         DBG("%s Attached %d", modem->path, modem->attached);
1700
1701         if (modem->attached == FALSE)
1702                 return;
1703
1704         if (has_interface(modem->interfaces,
1705                                 OFONO_API_NETREG) == FALSE) {
1706                 return;
1707         }
1708
1709         netreg_get_properties(modem);
1710 }
1711
1712 static void cm_update_powered(struct modem_data *modem,
1713                                 DBusMessageIter *value)
1714 {
1715         dbus_message_iter_get_basic(value, &modem->cm_powered);
1716
1717         DBG("%s ConnnectionManager Powered %d", modem->path,
1718                 modem->cm_powered);
1719
1720         if (modem->cm_powered == TRUE)
1721                 return;
1722
1723         cm_set_powered(modem, TRUE);
1724 }
1725
1726 static gboolean cm_changed(DBusConnection *conn, DBusMessage *message,
1727                                 void *user_data)
1728 {
1729         const char *path = dbus_message_get_path(message);
1730         struct modem_data *modem;
1731         DBusMessageIter iter, value;
1732         const char *key;
1733
1734         modem = g_hash_table_lookup(modem_hash, path);
1735         if (modem == NULL)
1736                 return TRUE;
1737
1738         if (modem->ignore == TRUE)
1739                 return TRUE;
1740
1741         if (dbus_message_iter_init(message, &iter) == FALSE)
1742                 return TRUE;
1743
1744         dbus_message_iter_get_basic(&iter, &key);
1745
1746         dbus_message_iter_next(&iter);
1747         dbus_message_iter_recurse(&iter, &value);
1748
1749         if (g_str_equal(key, "Attached") == TRUE)
1750                 cm_update_attached(modem, &value);
1751         else if (g_str_equal(key, "Powered") == TRUE)
1752                 cm_update_powered(modem, &value);
1753
1754         return TRUE;
1755 }
1756
1757 static void cdma_cm_update_powered(struct modem_data *modem,
1758                                         DBusMessageIter *value)
1759 {
1760         dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1761
1762         DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1763
1764         if (modem->network == NULL)
1765                 return;
1766
1767         if (modem->cdma_cm_powered == TRUE)
1768                 set_connected(modem);
1769         else
1770                 set_disconnected(modem);
1771 }
1772
1773 static void cdma_cm_update_settings(struct modem_data *modem,
1774                                         DBusMessageIter *value)
1775 {
1776         DBG("%s Settings", modem->path);
1777
1778         extract_ipv4_settings(value, modem->context);
1779 }
1780
1781 static gboolean cdma_cm_changed(DBusConnection *conn,
1782                                 DBusMessage *message, void *user_data)
1783 {
1784         const char *path = dbus_message_get_path(message);
1785         struct modem_data *modem;
1786         DBusMessageIter iter, value;
1787         const char *key;
1788
1789         modem = g_hash_table_lookup(modem_hash, path);
1790         if (modem == NULL)
1791                 return TRUE;
1792
1793         if (modem->online == TRUE && modem->network == NULL)
1794                 cdma_netreg_get_properties(modem);
1795
1796         if (dbus_message_iter_init(message, &iter) == FALSE)
1797                 return TRUE;
1798
1799         dbus_message_iter_get_basic(&iter, &key);
1800
1801         dbus_message_iter_next(&iter);
1802         dbus_message_iter_recurse(&iter, &value);
1803
1804         if (g_str_equal(key, "Powered") == TRUE)
1805                 cdma_cm_update_powered(modem, &value);
1806         if (g_str_equal(key, "Settings") == TRUE)
1807                 cdma_cm_update_settings(modem, &value);
1808
1809         return TRUE;
1810 }
1811
1812 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1813 {
1814         DBG("%s", modem->path);
1815
1816         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1817                 DBusMessageIter entry, value;
1818                 const char *key;
1819
1820                 dbus_message_iter_recurse(dict, &entry);
1821                 dbus_message_iter_get_basic(&entry, &key);
1822
1823                 dbus_message_iter_next(&entry);
1824                 dbus_message_iter_recurse(&entry, &value);
1825
1826                 if (g_str_equal(key, "Attached") == TRUE)
1827                         cm_update_attached(modem, &value);
1828                 else if (g_str_equal(key, "Powered") == TRUE)
1829                         cm_update_powered(modem, &value);
1830
1831                 dbus_message_iter_next(dict);
1832         }
1833 }
1834
1835 static int cm_get_properties(struct modem_data *modem)
1836 {
1837         return get_properties(modem->path, OFONO_CM_INTERFACE,
1838                                 cm_properties_reply, modem);
1839 }
1840
1841 static void cdma_cm_properties_reply(struct modem_data *modem,
1842                                         DBusMessageIter *dict)
1843 {
1844         DBG("%s", modem->path);
1845
1846         if (modem->online == TRUE)
1847                 cdma_netreg_get_properties(modem);
1848
1849         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1850                 DBusMessageIter entry, value;
1851                 const char *key;
1852
1853                 dbus_message_iter_recurse(dict, &entry);
1854                 dbus_message_iter_get_basic(&entry, &key);
1855
1856                 dbus_message_iter_next(&entry);
1857                 dbus_message_iter_recurse(&entry, &value);
1858
1859                 if (g_str_equal(key, "Powered") == TRUE)
1860                         cdma_cm_update_powered(modem, &value);
1861                 if (g_str_equal(key, "Settings") == TRUE)
1862                         cdma_cm_update_settings(modem, &value);
1863
1864                 dbus_message_iter_next(dict);
1865         }
1866 }
1867
1868 static int cdma_cm_get_properties(struct modem_data *modem)
1869 {
1870         return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1871                                 cdma_cm_properties_reply, modem);
1872 }
1873
1874 static void sim_update_imsi(struct modem_data *modem,
1875                                 DBusMessageIter* value)
1876 {
1877         char *imsi;
1878
1879         dbus_message_iter_get_basic(value, &imsi);
1880
1881         DBG("%s imsi %s", modem->path, imsi);
1882
1883         g_free(modem->imsi);
1884         modem->imsi = g_strdup(imsi);
1885 }
1886
1887 static gboolean sim_changed(DBusConnection *conn, DBusMessage *message,
1888                                 void *user_data)
1889 {
1890         const char *path = dbus_message_get_path(message);
1891         struct modem_data *modem;
1892         DBusMessageIter iter, value;
1893         const char *key;
1894
1895         modem = g_hash_table_lookup(modem_hash, path);
1896         if (modem == NULL)
1897                 return TRUE;
1898
1899         if (modem->ignore == TRUE)
1900                 return TRUE;
1901
1902         if (dbus_message_iter_init(message, &iter) == FALSE)
1903                 return TRUE;
1904
1905         dbus_message_iter_get_basic(&iter, &key);
1906
1907         dbus_message_iter_next(&iter);
1908         dbus_message_iter_recurse(&iter, &value);
1909
1910         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1911                 sim_update_imsi(modem, &value);
1912
1913                 if (ready_to_create_device(modem) == FALSE)
1914                         return TRUE;
1915
1916                 /*
1917                  * This is a GSM modem. Create the device and
1918                  * register it at the core. Enabling (setting
1919                  * it online is done through the
1920                  * modem_enable() callback.
1921                  */
1922                 create_device(modem);
1923         }
1924
1925         return TRUE;
1926 }
1927
1928 static void sim_properties_reply(struct modem_data *modem,
1929                                         DBusMessageIter *dict)
1930 {
1931         DBG("%s", modem->path);
1932
1933         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1934                 DBusMessageIter entry, value;
1935                 const char *key;
1936
1937                 dbus_message_iter_recurse(dict, &entry);
1938                 dbus_message_iter_get_basic(&entry, &key);
1939
1940                 dbus_message_iter_next(&entry);
1941                 dbus_message_iter_recurse(&entry, &value);
1942
1943                 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1944                         sim_update_imsi(modem, &value);
1945
1946                         if (ready_to_create_device(modem) == FALSE)
1947                                 return;
1948
1949                         /*
1950                          * This is a GSM modem. Create the device and
1951                          * register it at the core. Enabling (setting
1952                          * it online is done through the
1953                          * modem_enable() callback.
1954                          */
1955                         create_device(modem);
1956
1957                         if (modem->online == FALSE)
1958                                 return;
1959
1960                         /*
1961                          * The modem is already online and we have the CM interface.
1962                          * There will be no interface update and therefore our
1963                          * state machine will not go to next step. We have to
1964                          * trigger it from here.
1965                          */
1966                         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1967                                 cm_get_properties(modem);
1968                                 cm_get_contexts(modem);
1969                         }
1970                         return;
1971                 }
1972
1973                 dbus_message_iter_next(dict);
1974         }
1975 }
1976
1977 static int sim_get_properties(struct modem_data *modem)
1978 {
1979         return get_properties(modem->path, OFONO_SIM_INTERFACE,
1980                                 sim_properties_reply, modem);
1981 }
1982
1983 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1984                                 enum ofono_api api)
1985 {
1986         if (has_interface(old_iface, api) == FALSE &&
1987                         has_interface(new_iface, api) == TRUE) {
1988                 DBG("%s added", api2string(api));
1989                 return TRUE;
1990         }
1991
1992         return FALSE;
1993 }
1994
1995 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1996                                 enum ofono_api api)
1997 {
1998         if (has_interface(old_iface, api) == TRUE &&
1999                         has_interface(new_iface, api) == FALSE) {
2000                 DBG("%s removed", api2string(api));
2001                 return TRUE;
2002         }
2003
2004         return FALSE;
2005 }
2006
2007 static void modem_update_interfaces(struct modem_data *modem,
2008                                 uint8_t old_ifaces,
2009                                 uint8_t new_ifaces)
2010 {
2011         DBG("%s", modem->path);
2012
2013         if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
2014                 if (modem->imsi == NULL &&
2015                                 modem->set_powered == FALSE) {
2016                         /*
2017                          * Only use do GetProperties() when
2018                          * device has not been powered up.
2019                          */
2020                         sim_get_properties(modem);
2021                 }
2022         }
2023
2024         if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2025                 if (modem->device != NULL) {
2026                         cm_get_properties(modem);
2027                         cm_get_contexts(modem);
2028                 }
2029         }
2030
2031         if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2032                 if (ready_to_create_device(modem) == TRUE)
2033                         create_device(modem);
2034
2035                 if (modem->device != NULL)
2036                         cdma_cm_get_properties(modem);
2037         }
2038
2039         if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2040                 if (modem->attached == TRUE)
2041                         netreg_get_properties(modem);
2042         }
2043
2044         if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
2045                 cdma_netreg_get_properties(modem);
2046         }
2047
2048         if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2049                 remove_cm_context(modem, modem->context->path);
2050         }
2051
2052         if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2053                 remove_cm_context(modem, modem->context->path);
2054         }
2055
2056         if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2057                 remove_network(modem);
2058         }
2059
2060         if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
2061                 remove_network(modem);
2062         }
2063 }
2064
2065 static gboolean modem_changed(DBusConnection *conn, DBusMessage *message,
2066                                 void *user_data)
2067 {
2068         const char *path = dbus_message_get_path(message);
2069         struct modem_data *modem;
2070         DBusMessageIter iter, value;
2071         const char *key;
2072
2073         modem = g_hash_table_lookup(modem_hash, path);
2074         if (modem == NULL)
2075                 return TRUE;
2076
2077         if (modem->ignore == TRUE)
2078                 return TRUE;
2079
2080         if (dbus_message_iter_init(message, &iter) == FALSE)
2081                 return TRUE;
2082
2083         dbus_message_iter_get_basic(&iter, &key);
2084
2085         dbus_message_iter_next(&iter);
2086         dbus_message_iter_recurse(&iter, &value);
2087
2088         if (g_str_equal(key, "Powered") == TRUE) {
2089                 dbus_message_iter_get_basic(&value, &modem->powered);
2090
2091                 DBG("%s Powered %d", modem->path, modem->powered);
2092
2093                 if (modem->powered == FALSE)
2094                         modem_set_powered(modem, TRUE);
2095         } else if (g_str_equal(key, "Online") == TRUE) {
2096                 dbus_message_iter_get_basic(&value, &modem->online);
2097
2098                 DBG("%s Online %d", modem->path, modem->online);
2099
2100                 if (modem->device == NULL)
2101                         return TRUE;
2102
2103                 connman_device_set_powered(modem->device, modem->online);
2104         } else if (g_str_equal(key, "Interfaces") == TRUE) {
2105                 uint8_t interfaces;
2106
2107                 interfaces = extract_interfaces(&value);
2108
2109                 if (interfaces == modem->interfaces)
2110                         return TRUE;
2111
2112                 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2113
2114                 modem_update_interfaces(modem, modem->interfaces, interfaces);
2115
2116                 modem->interfaces = interfaces;
2117         } else if (g_str_equal(key, "Serial") == TRUE) {
2118                 char *serial;
2119
2120                 dbus_message_iter_get_basic(&value, &serial);
2121
2122                 g_free(modem->serial);
2123                 modem->serial = g_strdup(serial);
2124
2125                 DBG("%s Serial %s", modem->path, modem->serial);
2126
2127                 if (has_interface(modem->interfaces,
2128                                          OFONO_API_CDMA_CM) == TRUE) {
2129                         if (ready_to_create_device(modem) == TRUE)
2130                                 create_device(modem);
2131                 }
2132         }
2133
2134         return TRUE;
2135 }
2136
2137 static void add_modem(const char *path, DBusMessageIter *prop)
2138 {
2139         struct modem_data *modem;
2140
2141         DBG("%s", path);
2142
2143         modem = g_hash_table_lookup(modem_hash, path);
2144         if (modem != NULL) {
2145                 /*
2146                  * When oFono powers up we ask for the modems and oFono is
2147                  * reporting with modem_added signal the modems. Only
2148                  * handle them once.
2149                  */
2150                 return;
2151         }
2152
2153         modem = g_try_new0(struct modem_data, 1);
2154         if (modem == NULL)
2155                 return;
2156
2157         modem->path = g_strdup(path);
2158
2159         g_hash_table_insert(modem_hash, g_strdup(path), modem);
2160
2161         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2162                 DBusMessageIter entry, value;
2163                 const char *key;
2164
2165                 dbus_message_iter_recurse(prop, &entry);
2166                 dbus_message_iter_get_basic(&entry, &key);
2167
2168                 dbus_message_iter_next(&entry);
2169                 dbus_message_iter_recurse(&entry, &value);
2170
2171                 if (g_str_equal(key, "Powered") == TRUE) {
2172                         dbus_message_iter_get_basic(&value, &modem->powered);
2173
2174                         DBG("%s Powered %d", modem->path, modem->powered);
2175                 } else if (g_str_equal(key, "Online") == TRUE) {
2176                         dbus_message_iter_get_basic(&value, &modem->online);
2177
2178                         DBG("%s Online %d", modem->path, modem->online);
2179                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2180                         modem->interfaces = extract_interfaces(&value);
2181
2182                         DBG("%s Interfaces 0x%02x", modem->path,
2183                                 modem->interfaces);
2184                 } else if (g_str_equal(key, "Serial") == TRUE) {
2185                         char *serial;
2186
2187                         dbus_message_iter_get_basic(&value, &serial);
2188                         modem->serial = g_strdup(serial);
2189
2190                         DBG("%s Serial %s", modem->path, modem->serial);
2191                 } else if (g_str_equal(key, "Type") == TRUE) {
2192                         char *type;
2193
2194                         dbus_message_iter_get_basic(&value, &type);
2195
2196                         DBG("%s Type %s", modem->path, type);
2197                         if (g_strcmp0(type, "hardware") != 0) {
2198                                 DBG("%s Ignore this modem", modem->path);
2199                                 modem->ignore = TRUE;
2200                         }
2201                 }
2202
2203                 dbus_message_iter_next(prop);
2204         }
2205
2206         if (modem->ignore == TRUE)
2207                 return;
2208
2209         if (modem->powered == FALSE) {
2210                 modem_set_powered(modem, TRUE);
2211                 return;
2212         }
2213
2214         modem_update_interfaces(modem, 0, modem->interfaces);
2215 }
2216
2217 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2218 {
2219         struct modem_data *modem = value;
2220
2221         DBG("%s", modem->path);
2222
2223         if (modem->ignore ==  TRUE)
2224                 return;
2225
2226         modem_set_powered(modem, FALSE);
2227 }
2228
2229 static void remove_modem(gpointer data)
2230 {
2231         struct modem_data *modem = data;
2232
2233         DBG("%s", modem->path);
2234
2235         if (modem->call_set_property != NULL)
2236                 dbus_pending_call_cancel(modem->call_set_property);
2237
2238         if (modem->call_get_properties != NULL)
2239                 dbus_pending_call_cancel(modem->call_get_properties);
2240
2241         if (modem->call_get_contexts != NULL)
2242                 dbus_pending_call_cancel(modem->call_get_contexts);
2243
2244         if (modem->device != NULL)
2245                 destroy_device(modem);
2246
2247         if (modem->context != NULL)
2248                 remove_cm_context(modem, modem->context->path);
2249
2250         g_free(modem->serial);
2251         g_free(modem->name);
2252         g_free(modem->imsi);
2253         g_free(modem->path);
2254
2255         g_free(modem);
2256 }
2257
2258 static gboolean modem_added(DBusConnection *conn,
2259                                 DBusMessage *message, void *user_data)
2260 {
2261         DBusMessageIter iter, properties;
2262         const char *path;
2263
2264         DBG("");
2265
2266         if (dbus_message_iter_init(message, &iter) == FALSE)
2267                 return TRUE;
2268
2269         dbus_message_iter_get_basic(&iter, &path);
2270
2271         dbus_message_iter_next(&iter);
2272         dbus_message_iter_recurse(&iter, &properties);
2273
2274         add_modem(path, &properties);
2275
2276         return TRUE;
2277 }
2278
2279 static gboolean modem_removed(DBusConnection *conn,
2280                                 DBusMessage *message, void *user_data)
2281 {
2282         DBusMessageIter iter;
2283         const char *path;
2284
2285         DBG("");
2286
2287         if (dbus_message_iter_init(message, &iter) == FALSE)
2288                 return TRUE;
2289
2290         dbus_message_iter_get_basic(&iter, &path);
2291
2292         g_hash_table_remove(modem_hash, path);
2293
2294         return TRUE;
2295 }
2296
2297 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2298 {
2299         DBusMessage *reply;
2300         DBusError error;
2301         DBusMessageIter array, dict;
2302
2303         DBG("");
2304
2305         reply = dbus_pending_call_steal_reply(call);
2306
2307         dbus_error_init(&error);
2308
2309         if (dbus_set_error_from_message(&error, reply) == TRUE) {
2310                 connman_error("%s", error.message);
2311                 dbus_error_free(&error);
2312                 goto done;
2313         }
2314
2315         if (dbus_message_iter_init(reply, &array) == FALSE)
2316                 goto done;
2317
2318         dbus_message_iter_recurse(&array, &dict);
2319
2320         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2321                 DBusMessageIter value, properties;
2322                 const char *path;
2323
2324                 dbus_message_iter_recurse(&dict, &value);
2325                 dbus_message_iter_get_basic(&value, &path);
2326
2327                 dbus_message_iter_next(&value);
2328                 dbus_message_iter_recurse(&value, &properties);
2329
2330                 add_modem(path, &properties);
2331
2332                 dbus_message_iter_next(&dict);
2333         }
2334
2335 done:
2336         dbus_message_unref(reply);
2337
2338         dbus_pending_call_unref(call);
2339 }
2340
2341 static int manager_get_modems(void)
2342 {
2343         DBusMessage *message;
2344         DBusPendingCall *call;
2345
2346         DBG("");
2347
2348         message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2349                                         OFONO_MANAGER_INTERFACE, GET_MODEMS);
2350         if (message == NULL)
2351                 return -ENOMEM;
2352
2353         if (dbus_connection_send_with_reply(connection, message,
2354                                                &call, TIMEOUT) == FALSE) {
2355                 connman_error("Failed to call GetModems()");
2356                 dbus_message_unref(message);
2357                 return -EINVAL;
2358         }
2359
2360         if (call == NULL) {
2361                 connman_error("D-Bus connection not available");
2362                 dbus_message_unref(message);
2363                 return -EINVAL;
2364         }
2365
2366         dbus_pending_call_set_notify(call, manager_get_modems_reply,
2367                                         NULL, NULL);
2368
2369         dbus_message_unref(message);
2370
2371         return -EINPROGRESS;
2372 }
2373
2374 static void ofono_connect(DBusConnection *conn, void *user_data)
2375 {
2376         DBG("");
2377
2378         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2379                                                 g_free, remove_modem);
2380         if (modem_hash == NULL)
2381                 return;
2382
2383         context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2384                                                 g_free, NULL);
2385         if (context_hash == NULL) {
2386                 g_hash_table_destroy(modem_hash);
2387                 return;
2388         }
2389
2390         manager_get_modems();
2391 }
2392
2393 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2394 {
2395         DBG("");
2396
2397         if (modem_hash == NULL || context_hash == NULL)
2398                 return;
2399
2400         g_hash_table_destroy(modem_hash);
2401         modem_hash = NULL;
2402
2403         g_hash_table_destroy(context_hash);
2404         context_hash = NULL;
2405 }
2406
2407 static int network_probe(struct connman_network *network)
2408 {
2409         struct modem_data *modem = connman_network_get_data(network);
2410
2411         DBG("%s network %p", modem->path, network);
2412
2413         return 0;
2414 }
2415
2416 static void network_remove(struct connman_network *network)
2417 {
2418         struct modem_data *modem = connman_network_get_data(network);
2419
2420         DBG("%s network %p", modem->path, network);
2421 }
2422
2423 static int network_connect(struct connman_network *network)
2424 {
2425         struct modem_data *modem = connman_network_get_data(network);
2426
2427         DBG("%s network %p", modem->path, network);
2428
2429         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2430                 return context_set_active(modem, TRUE);
2431         else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2432                 return cdma_cm_set_powered(modem, TRUE);
2433
2434         connman_error("Connection manager interface not available");
2435
2436         return -ENOSYS;
2437 }
2438
2439 static int network_disconnect(struct connman_network *network)
2440 {
2441         struct modem_data *modem = connman_network_get_data(network);
2442
2443         DBG("%s network %p", modem->path, network);
2444
2445         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2446                 return context_set_active(modem, FALSE);
2447         else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2448                 return cdma_cm_set_powered(modem, FALSE);
2449
2450         connman_error("Connection manager interface not available");
2451
2452         return -ENOSYS;
2453 }
2454
2455 static struct connman_network_driver network_driver = {
2456         .name           = "cellular",
2457         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
2458         .probe          = network_probe,
2459         .remove         = network_remove,
2460         .connect        = network_connect,
2461         .disconnect     = network_disconnect,
2462 };
2463
2464 static int modem_probe(struct connman_device *device)
2465 {
2466         struct modem_data *modem = connman_device_get_data(device);
2467
2468         DBG("%s device %p", modem->path, device);
2469
2470         return 0;
2471 }
2472
2473 static void modem_remove(struct connman_device *device)
2474 {
2475         struct modem_data *modem = connman_device_get_data(device);
2476
2477         DBG("%s device %p", modem->path, device);
2478 }
2479
2480 static int modem_enable(struct connman_device *device)
2481 {
2482         struct modem_data *modem = connman_device_get_data(device);
2483
2484         DBG("%s device %p", modem->path, device);
2485
2486         if (modem->online == TRUE)
2487                 return 0;
2488
2489         return modem_set_online(modem, TRUE);
2490 }
2491
2492 static int modem_disable(struct connman_device *device)
2493 {
2494         struct modem_data *modem = connman_device_get_data(device);
2495
2496         DBG("%s device %p", modem->path, device);
2497
2498         if (modem->online == FALSE)
2499                 return 0;
2500
2501         return modem_set_online(modem, FALSE);
2502 }
2503
2504 static struct connman_device_driver modem_driver = {
2505         .name           = "modem",
2506         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
2507         .probe          = modem_probe,
2508         .remove         = modem_remove,
2509         .enable         = modem_enable,
2510         .disable        = modem_disable,
2511 };
2512
2513 static int tech_probe(struct connman_technology *technology)
2514 {
2515         return 0;
2516 }
2517
2518 static void tech_remove(struct connman_technology *technology)
2519 {
2520 }
2521
2522 static struct connman_technology_driver tech_driver = {
2523         .name           = "cellular",
2524         .type           = CONNMAN_SERVICE_TYPE_CELLULAR,
2525         .probe          = tech_probe,
2526         .remove         = tech_remove,
2527 };
2528
2529 static guint watch;
2530 static guint modem_added_watch;
2531 static guint modem_removed_watch;
2532 static guint modem_watch;
2533 static guint cm_watch;
2534 static guint sim_watch;
2535 static guint context_added_watch;
2536 static guint context_removed_watch;
2537 static guint netreg_watch;
2538 static guint context_watch;
2539 static guint cdma_cm_watch;
2540 static guint cdma_netreg_watch;
2541
2542 static int ofono_init(void)
2543 {
2544         int err;
2545
2546         DBG("");
2547
2548         connection = connman_dbus_get_connection();
2549         if (connection == NULL)
2550                 return -EIO;
2551
2552         watch = g_dbus_add_service_watch(connection,
2553                                         OFONO_SERVICE, ofono_connect,
2554                                         ofono_disconnect, NULL, NULL);
2555
2556         modem_added_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2557                                                 NULL, OFONO_MANAGER_INTERFACE,
2558                                                 MODEM_ADDED,
2559                                                 modem_added,
2560                                                 NULL, NULL);
2561
2562         modem_removed_watch = g_dbus_add_signal_watch(connection,
2563                                                 OFONO_SERVICE, NULL,
2564                                                 OFONO_MANAGER_INTERFACE,
2565                                                 MODEM_REMOVED,
2566                                                 modem_removed,
2567                                                 NULL, NULL);
2568
2569         modem_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2570                                                 OFONO_MODEM_INTERFACE,
2571                                                 PROPERTY_CHANGED,
2572                                                 modem_changed,
2573                                                 NULL, NULL);
2574
2575         cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2576                                                 OFONO_CM_INTERFACE,
2577                                                 PROPERTY_CHANGED,
2578                                                 cm_changed,
2579                                                 NULL, NULL);
2580
2581         sim_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2582                                                 OFONO_SIM_INTERFACE,
2583                                                 PROPERTY_CHANGED,
2584                                                 sim_changed,
2585                                                 NULL, NULL);
2586
2587         context_added_watch = g_dbus_add_signal_watch(connection,
2588                                                 OFONO_SERVICE, NULL,
2589                                                 OFONO_CM_INTERFACE,
2590                                                 CONTEXT_ADDED,
2591                                                 cm_context_added,
2592                                                 NULL, NULL);
2593
2594         context_removed_watch = g_dbus_add_signal_watch(connection,
2595                                                 OFONO_SERVICE, NULL,
2596                                                 OFONO_CM_INTERFACE,
2597                                                 CONTEXT_REMOVED,
2598                                                 cm_context_removed,
2599                                                 NULL, NULL);
2600
2601         context_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2602                                                 NULL, OFONO_CONTEXT_INTERFACE,
2603                                                 PROPERTY_CHANGED,
2604                                                 context_changed,
2605                                                 NULL, NULL);
2606
2607         netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2608                                                 OFONO_NETREG_INTERFACE,
2609                                                 PROPERTY_CHANGED,
2610                                                 netreg_changed,
2611                                                 NULL, NULL);
2612
2613         cdma_cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2614                                                 NULL, OFONO_CDMA_CM_INTERFACE,
2615                                                 PROPERTY_CHANGED,
2616                                                 cdma_cm_changed,
2617                                                 NULL, NULL);
2618
2619         cdma_netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2620                                                 NULL, OFONO_CDMA_NETREG_INTERFACE,
2621                                                 PROPERTY_CHANGED,
2622                                                 cdma_netreg_changed,
2623                                                 NULL, NULL);
2624
2625
2626         if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2627                         modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2628                         context_added_watch == 0 ||
2629                         context_removed_watch == 0 ||
2630                         context_watch == 0 || netreg_watch == 0 ||
2631                         cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2632                 err = -EIO;
2633                 goto remove;
2634         }
2635
2636         err = connman_network_driver_register(&network_driver);
2637         if (err < 0)
2638                 goto remove;
2639
2640         err = connman_device_driver_register(&modem_driver);
2641         if (err < 0) {
2642                 connman_network_driver_unregister(&network_driver);
2643                 goto remove;
2644         }
2645
2646         err = connman_technology_driver_register(&tech_driver);
2647         if (err < 0) {
2648                 connman_device_driver_unregister(&modem_driver);
2649                 connman_network_driver_unregister(&network_driver);
2650                 goto remove;
2651         }
2652
2653         return 0;
2654
2655 remove:
2656         g_dbus_remove_watch(connection, cdma_netreg_watch);
2657         g_dbus_remove_watch(connection, cdma_cm_watch);
2658         g_dbus_remove_watch(connection, netreg_watch);
2659         g_dbus_remove_watch(connection, context_watch);
2660         g_dbus_remove_watch(connection, context_removed_watch);
2661         g_dbus_remove_watch(connection, context_added_watch);
2662         g_dbus_remove_watch(connection, sim_watch);
2663         g_dbus_remove_watch(connection, cm_watch);
2664         g_dbus_remove_watch(connection, modem_watch);
2665         g_dbus_remove_watch(connection, modem_removed_watch);
2666         g_dbus_remove_watch(connection, modem_added_watch);
2667         g_dbus_remove_watch(connection, watch);
2668         dbus_connection_unref(connection);
2669
2670         return err;
2671 }
2672
2673 static void ofono_exit(void)
2674 {
2675         DBG("");
2676
2677         if (modem_hash != NULL) {
2678                 /*
2679                  * We should propably wait for the SetProperty() reply
2680                  * message, because ...
2681                  */
2682                 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2683
2684                 /*
2685                  * ... here we will cancel the call.
2686                  */
2687                 g_hash_table_destroy(modem_hash);
2688                 modem_hash = NULL;
2689         }
2690
2691         if (context_hash != NULL) {
2692                 g_hash_table_destroy(context_hash);
2693                 context_hash = NULL;
2694         }
2695
2696         connman_technology_driver_unregister(&tech_driver);
2697         connman_device_driver_unregister(&modem_driver);
2698         connman_network_driver_unregister(&network_driver);
2699
2700         g_dbus_remove_watch(connection, cdma_netreg_watch);
2701         g_dbus_remove_watch(connection, cdma_cm_watch);
2702         g_dbus_remove_watch(connection, netreg_watch);
2703         g_dbus_remove_watch(connection, context_watch);
2704         g_dbus_remove_watch(connection, context_removed_watch);
2705         g_dbus_remove_watch(connection, context_added_watch);
2706         g_dbus_remove_watch(connection, sim_watch);
2707         g_dbus_remove_watch(connection, cm_watch);
2708         g_dbus_remove_watch(connection, modem_watch);
2709         g_dbus_remove_watch(connection, modem_added_watch);
2710         g_dbus_remove_watch(connection, modem_removed_watch);
2711         g_dbus_remove_watch(connection, watch);
2712
2713         dbus_connection_unref(connection);
2714 }
2715
2716 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2717                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)