device: Turn off all running interfaces at startup
[framework/connectivity/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         connman_bool_t setip = FALSE;
260
261         DBG("%s", modem->path);
262
263         if (modem->context->index < 0 ||
264                         modem->context->ipv4_address == NULL) {
265                 connman_error("Invalid index and/or address");
266                 return;
267         }
268
269         connman_network_set_index(modem->network, modem->context->index);
270
271         switch (modem->context->ipv4_method) {
272         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
273         case CONNMAN_IPCONFIG_METHOD_OFF:
274         case CONNMAN_IPCONFIG_METHOD_MANUAL:
275         case CONNMAN_IPCONFIG_METHOD_AUTO:
276                 break;
277
278         case CONNMAN_IPCONFIG_METHOD_FIXED:
279                 connman_network_set_ipv4_method(modem->network,
280                                                 modem->context->ipv4_method);
281                 connman_network_set_ipaddress(modem->network,
282                                                 modem->context->ipv4_address);
283                 connman_network_set_nameservers(modem->network,
284                                         modem->context->ipv4_nameservers);
285                 setip = TRUE;
286                 break;
287
288         case CONNMAN_IPCONFIG_METHOD_DHCP:
289                 connman_network_set_ipv4_method(modem->network,
290                                                 modem->context->ipv4_method);
291                 setip = TRUE;
292                 break;
293         }
294
295         switch (modem->context->ipv6_method) {
296         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
297         case CONNMAN_IPCONFIG_METHOD_OFF:
298         case CONNMAN_IPCONFIG_METHOD_MANUAL:
299         case CONNMAN_IPCONFIG_METHOD_DHCP:
300         case CONNMAN_IPCONFIG_METHOD_AUTO:
301                 break;
302
303         case CONNMAN_IPCONFIG_METHOD_FIXED:
304                 connman_network_set_ipv6_method(modem->network,
305                                                         modem->context->ipv6_method);
306                 connman_network_set_ipaddress(modem->network,
307                                                         modem->context->ipv6_address);
308                 setip = TRUE;
309                 break;
310         }
311
312         if (setip == TRUE)
313                 connman_network_set_connected(modem->network, TRUE);
314 }
315
316 static void set_disconnected(struct modem_data *modem)
317 {
318         DBG("%s", modem->path);
319
320         if (modem->network == NULL)
321                 return;
322
323         connman_network_set_connected(modem->network, FALSE);
324 }
325
326 typedef void (*set_property_cb)(struct modem_data *data,
327                                 connman_bool_t success);
328 typedef void (*get_properties_cb)(struct modem_data *data,
329                                 DBusMessageIter *dict);
330
331 struct property_info {
332         struct modem_data *modem;
333         const char *path;
334         const char *interface;
335         const char *property;
336         set_property_cb set_property_cb;
337         get_properties_cb get_properties_cb;
338 };
339
340 static void set_property_reply(DBusPendingCall *call, void *user_data)
341 {
342         struct property_info *info = user_data;
343         DBusMessage *reply;
344         DBusError error;
345         connman_bool_t success = TRUE;
346
347         DBG("%s path %s %s.%s", info->modem->path,
348                 info->path, info->interface, info->property);
349
350         info->modem->call_set_property = NULL;
351
352         dbus_error_init(&error);
353
354         reply = dbus_pending_call_steal_reply(call);
355
356         if (dbus_set_error_from_message(&error, reply)) {
357                 connman_error("Failed to change property: %s %s.%s: %s %s",
358                                 info->path, info->interface, info->property,
359                                 error.name, error.message);
360                 dbus_error_free(&error);
361                 success = FALSE;
362         }
363
364         if (info->set_property_cb != NULL)
365                 (*info->set_property_cb)(info->modem, success);
366
367         dbus_message_unref(reply);
368
369         dbus_pending_call_unref(call);
370 }
371
372 static int set_property(struct modem_data *modem,
373                         const char *path, const char *interface,
374                         const char *property, int type, void *value,
375                         set_property_cb notify)
376 {
377         DBusMessage *message;
378         DBusMessageIter iter;
379         struct property_info *info;
380
381         DBG("%s path %s %s.%s", modem->path, path, interface, property);
382
383         if (modem->call_set_property != NULL) {
384                 DBG("Cancel pending SetProperty");
385
386                 dbus_pending_call_cancel(modem->call_set_property);
387                 modem->call_set_property = NULL;
388         }
389
390         message = dbus_message_new_method_call(OFONO_SERVICE, path,
391                                         interface, SET_PROPERTY);
392         if (message == NULL)
393                 return -ENOMEM;
394
395         dbus_message_iter_init_append(message, &iter);
396         connman_dbus_property_append_basic(&iter, property, type, value);
397
398         if (dbus_connection_send_with_reply(connection, message,
399                         &modem->call_set_property, TIMEOUT) == FALSE) {
400                 connman_error("Failed to change property: %s %s.%s",
401                                 path, interface, property);
402                 dbus_message_unref(message);
403                 return -EINVAL;
404         }
405
406         if (modem->call_set_property == NULL) {
407                 connman_error("D-Bus connection not available");
408                 dbus_message_unref(message);
409                 return -EINVAL;
410         }
411
412         info = g_try_new0(struct property_info, 1);
413         if (info == NULL) {
414                 dbus_message_unref(message);
415                 return -ENOMEM;
416         }
417
418         info->modem = modem;
419         info->path = path;
420         info->interface = interface;
421         info->property = property;
422         info->set_property_cb = notify;
423
424         dbus_pending_call_set_notify(modem->call_set_property,
425                                         set_property_reply, info, g_free);
426
427         dbus_message_unref(message);
428
429         return -EINPROGRESS;
430 }
431
432 static void get_properties_reply(DBusPendingCall *call, void *user_data)
433 {
434         struct property_info *info = user_data;
435         DBusMessageIter array, dict;
436         DBusMessage *reply;
437         DBusError error;
438
439         DBG("%s path %s %s", info->modem->path, info->path, info->interface);
440
441         info->modem->call_get_properties = NULL;
442
443         dbus_error_init(&error);
444
445         reply = dbus_pending_call_steal_reply(call);
446
447         if (dbus_set_error_from_message(&error, reply)) {
448                 connman_error("Failed to get properties: %s %s: %s %s",
449                                 info->path, info->interface,
450                                 error.name, error.message);
451                 dbus_error_free(&error);
452
453                 goto done;
454         }
455
456         if (dbus_message_iter_init(reply, &array) == FALSE)
457                 goto done;
458
459         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
460                 goto done;
461
462         dbus_message_iter_recurse(&array, &dict);
463
464         if (info->get_properties_cb != NULL)
465                 (*info->get_properties_cb)(info->modem, &dict);
466
467 done:
468
469         dbus_message_unref(reply);
470
471         dbus_pending_call_unref(call);
472 }
473
474 static int get_properties(const char *path, const char *interface,
475                                 get_properties_cb notify,
476                                 struct modem_data *modem)
477 {
478         DBusMessage *message;
479         struct property_info *info;
480
481         DBG("%s path %s %s", modem->path, path, interface);
482
483         if (modem->call_get_properties != NULL) {
484                 connman_error("Pending GetProperties");
485                 return -EBUSY;
486         }
487
488         message = dbus_message_new_method_call(OFONO_SERVICE, path,
489                                         interface, GET_PROPERTIES);
490         if (message == NULL)
491                 return -ENOMEM;
492
493         if (dbus_connection_send_with_reply(connection, message,
494                         &modem->call_get_properties, TIMEOUT) == FALSE) {
495                 connman_error("Failed to call %s.GetProperties()", interface);
496                 dbus_message_unref(message);
497                 return -EINVAL;
498         }
499
500         if (modem->call_get_properties == NULL) {
501                 connman_error("D-Bus connection not available");
502                 dbus_message_unref(message);
503                 return -EINVAL;
504         }
505
506         info = g_try_new0(struct property_info, 1);
507         if (info == NULL) {
508                 dbus_message_unref(message);
509                 return -ENOMEM;
510         }
511
512         info->modem = modem;
513         info->path = path;
514         info->interface = interface;
515         info->get_properties_cb = notify;
516
517         dbus_pending_call_set_notify(modem->call_get_properties,
518                                         get_properties_reply, info, g_free);
519
520         dbus_message_unref(message);
521
522         return -EINPROGRESS;
523 }
524
525 static void context_set_active_reply(struct modem_data *modem,
526                                         connman_bool_t success)
527 {
528         DBG("%s", modem->path);
529
530         if (success == TRUE) {
531                 /*
532                  * Don't handle do anything on success here. oFono will send
533                  * the change via PropertyChanged singal.
534                  */
535                 return;
536         }
537
538         /*
539          * Active = True might fail due a timeout. That means oFono
540          * still tries to go online. If we retry to set Active = True,
541          * we just get a InProgress error message. Should we power
542          * cycle the modem in such cases?
543          */
544
545         if (modem->network == NULL) {
546                 /*
547                  * In the case where we power down the device
548                  * we don't wait for the reply, therefore the network
549                  * might already be gone.
550                  */
551                 return;
552         }
553
554         connman_network_set_error(modem->network,
555                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
556 }
557
558 static int context_set_active(struct modem_data *modem,
559                                 connman_bool_t active)
560 {
561         int err;
562
563         DBG("%s active %d", modem->path, active);
564
565         err = set_property(modem, modem->context->path,
566                                 OFONO_CONTEXT_INTERFACE,
567                                 "Active", DBUS_TYPE_BOOLEAN,
568                                 &active,
569                                 context_set_active_reply);
570
571         if (active == FALSE && err == -EINPROGRESS)
572                 return 0;
573
574         return err;
575 }
576
577 static void cdma_cm_set_powered_reply(struct modem_data *modem,
578                                         connman_bool_t success)
579 {
580         DBG("%s", modem->path);
581
582         if (success == TRUE) {
583                 /*
584                  * Don't handle do anything on success here. oFono will send
585                  * the change via PropertyChanged singal.
586                  */
587                 return;
588         }
589
590         /*
591          * Powered = True might fail due a timeout. That means oFono
592          * still tries to go online. If we retry to set Powered = True,
593          * we just get a InProgress error message. Should we power
594          * cycle the modem in such cases?
595          */
596
597         if (modem->network == NULL) {
598                 /*
599                  * In the case where we power down the device
600                  * we don't wait for the reply, therefore the network
601                  * might already be gone.
602                  */
603                 return;
604         }
605
606         connman_network_set_error(modem->network,
607                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
608 }
609
610 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
611 {
612         int err;
613
614         DBG("%s powered %d", modem->path, powered);
615
616         err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
617                                 "Powered", DBUS_TYPE_BOOLEAN,
618                                 &powered,
619                                 cdma_cm_set_powered_reply);
620
621         if (powered == FALSE && err == -EINPROGRESS)
622                 return 0;
623
624         return err;
625 }
626
627 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
628 {
629         DBG("%s online %d", modem->path, online);
630
631         return set_property(modem, modem->path,
632                                 OFONO_MODEM_INTERFACE,
633                                 "Online", DBUS_TYPE_BOOLEAN,
634                                 &online,
635                                 NULL);
636 }
637
638 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
639 {
640         int err;
641
642         DBG("%s powered %d", modem->path, powered);
643
644         err = set_property(modem, modem->path,
645                                 OFONO_CM_INTERFACE,
646                                 "Powered", DBUS_TYPE_BOOLEAN,
647                                 &powered,
648                                 NULL);
649
650         if (powered == FALSE && err == -EINPROGRESS)
651                 return 0;
652
653         return err;
654 }
655
656 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
657 {
658         int err;
659
660         DBG("%s powered %d", modem->path, powered);
661
662         modem->set_powered = powered;
663
664         err = set_property(modem, modem->path,
665                                 OFONO_MODEM_INTERFACE,
666                                 "Powered", DBUS_TYPE_BOOLEAN,
667                                 &powered,
668                                 NULL);
669
670         if (powered == FALSE && err == -EINPROGRESS)
671                 return 0;
672
673         return err;
674 }
675
676 static connman_bool_t has_interface(uint8_t interfaces,
677                                         enum ofono_api api)
678 {
679         if ((interfaces & api) == api)
680                 return TRUE;
681
682         return FALSE;
683 }
684
685 static uint8_t extract_interfaces(DBusMessageIter *array)
686 {
687         DBusMessageIter entry;
688         uint8_t interfaces = 0;
689
690         dbus_message_iter_recurse(array, &entry);
691
692         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
693                 const char *name;
694
695                 dbus_message_iter_get_basic(&entry, &name);
696
697                 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
698                         interfaces |= OFONO_API_SIM;
699                 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
700                         interfaces |= OFONO_API_NETREG;
701                 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
702                         interfaces |= OFONO_API_CM;
703                 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
704                         interfaces |= OFONO_API_CDMA_CM;
705                 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
706                         interfaces |= OFONO_API_CDMA_NETREG;
707
708                 dbus_message_iter_next(&entry);
709         }
710
711         return interfaces;
712 }
713
714 static char *extract_nameservers(DBusMessageIter *array)
715 {
716         DBusMessageIter entry;
717         char *nameservers = NULL;
718         char *tmp;
719
720         dbus_message_iter_recurse(array, &entry);
721
722         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
723                 const char *nameserver;
724
725                 dbus_message_iter_get_basic(&entry, &nameserver);
726
727                 if (nameservers == NULL) {
728                         nameservers = g_strdup(nameserver);
729                 } else {
730                         tmp = nameservers;
731                         nameservers = g_strdup_printf("%s %s", tmp, nameserver);
732                         g_free(tmp);
733                 }
734
735                 dbus_message_iter_next(&entry);
736         }
737
738         return nameservers;
739 }
740
741 static void extract_ipv4_settings(DBusMessageIter *array,
742                                 struct network_context *context)
743 {
744         DBusMessageIter dict;
745         char *address = NULL, *netmask = NULL, *gateway = NULL;
746         char *nameservers = NULL;
747         const char *interface = NULL;
748         int index = -1;
749
750         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
751                 return;
752
753         dbus_message_iter_recurse(array, &dict);
754
755         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
756                 DBusMessageIter entry, value;
757                 const char *key, *val;
758
759                 dbus_message_iter_recurse(&dict, &entry);
760                 dbus_message_iter_get_basic(&entry, &key);
761
762                 dbus_message_iter_next(&entry);
763                 dbus_message_iter_recurse(&entry, &value);
764
765                 if (g_str_equal(key, "Interface") == TRUE) {
766                         dbus_message_iter_get_basic(&value, &interface);
767
768                         DBG("Interface %s", interface);
769
770                         index = connman_inet_ifindex(interface);
771
772                         DBG("index %d", index);
773                 } else if (g_str_equal(key, "Method") == TRUE) {
774                         dbus_message_iter_get_basic(&value, &val);
775
776                         DBG("Method %s", val);
777
778                         if (g_strcmp0(val, "static") == 0) {
779                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
780                         } else if (g_strcmp0(val, "dhcp") == 0) {
781                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
782                                 break;
783                         }
784                 } else if (g_str_equal(key, "Address") == TRUE) {
785                         dbus_message_iter_get_basic(&value, &val);
786
787                         address = g_strdup(val);
788
789                         DBG("Address %s", address);
790                 } else if (g_str_equal(key, "Netmask") == TRUE) {
791                         dbus_message_iter_get_basic(&value, &val);
792
793                         netmask = g_strdup(val);
794
795                         DBG("Netmask %s", netmask);
796                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
797                         nameservers = extract_nameservers(&value);
798
799                         DBG("Nameservers %s", nameservers);
800                 } else if (g_str_equal(key, "Gateway") == TRUE) {
801                         dbus_message_iter_get_basic(&value, &val);
802
803                         gateway = g_strdup(val);
804
805                         DBG("Gateway %s", gateway);
806                 }
807
808                 dbus_message_iter_next(&dict);
809         }
810
811         if (index < 0)
812                 goto out;
813
814         if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
815                 goto out;
816
817         context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
818         if (context->ipv4_address == NULL)
819                 goto out;
820
821         context->index = index;
822         connman_ipaddress_set_ipv4(context->ipv4_address, address,
823                                 netmask, gateway);
824
825         context->ipv4_nameservers = nameservers;
826
827 out:
828         if (context->ipv4_nameservers != nameservers)
829                 g_free(nameservers);
830
831         g_free(address);
832         g_free(netmask);
833         g_free(gateway);
834 }
835
836 static void extract_ipv6_settings(DBusMessageIter *array,
837                                 struct network_context *context)
838 {
839         DBusMessageIter dict;
840         char *address = NULL, *gateway = NULL;
841         unsigned char prefix_length = 0;
842         char *nameservers = NULL;
843         const char *interface = NULL;
844         int index = -1;
845
846         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
847                 return;
848
849         dbus_message_iter_recurse(array, &dict);
850
851         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
852                 DBusMessageIter entry, value;
853                 const char *key, *val;
854
855                 dbus_message_iter_recurse(&dict, &entry);
856                 dbus_message_iter_get_basic(&entry, &key);
857
858                 dbus_message_iter_next(&entry);
859                 dbus_message_iter_recurse(&entry, &value);
860
861                 if (g_str_equal(key, "Interface") == TRUE) {
862                         dbus_message_iter_get_basic(&value, &interface);
863
864                         DBG("Interface %s", interface);
865
866                         index = connman_inet_ifindex(interface);
867
868                         DBG("index %d", index);
869                 } else if (g_str_equal(key, "Address") == TRUE) {
870                         dbus_message_iter_get_basic(&value, &val);
871
872                         address = g_strdup(val);
873
874                         DBG("Address %s", address);
875                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
876                         dbus_message_iter_get_basic(&value, &prefix_length);
877
878                         DBG("prefix length %d", prefix_length);
879                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
880                         nameservers = extract_nameservers(&value);
881
882                         DBG("Nameservers %s", nameservers);
883                 } else if (g_str_equal(key, "Gateway") == TRUE) {
884                         dbus_message_iter_get_basic(&value, &val);
885
886                         gateway = g_strdup(val);
887
888                         DBG("Gateway %s", gateway);
889                 }
890
891                 dbus_message_iter_next(&dict);
892         }
893
894         if (index < 0)
895                 goto out;
896
897         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
898
899         context->ipv6_address =
900                 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
901         if (context->ipv6_address == NULL)
902                 goto out;
903
904         context->index = index;
905         connman_ipaddress_set_ipv6(context->ipv6_address, address,
906                                 prefix_length, gateway);
907
908         context->ipv6_nameservers = nameservers;
909
910 out:
911         if (context->ipv6_nameservers != nameservers)
912                 g_free(nameservers);
913
914         g_free(address);
915         g_free(gateway);
916 }
917
918 static connman_bool_t ready_to_create_device(struct modem_data *modem)
919 {
920         /*
921          * There are three different modem types which behave slightly
922          * different:
923          * - GSM modems will expose the SIM interface then the
924          *   CM interface.
925          * - CDMA modems will expose CM first and sometime later
926          *   a unique serial number.
927          *
928          * This functions tests if we have the necessary information gathered
929          * before we are able to create a device.
930          */
931
932         if (modem->device != NULL)
933                 return FALSE;
934
935         if (modem->imsi != NULL || modem->serial != NULL)
936                 return TRUE;
937
938         return FALSE;
939 }
940
941 static void create_device(struct modem_data *modem)
942 {
943         struct connman_device *device;
944         char *ident = NULL;
945
946         DBG("%s", modem->path);
947
948         if (modem->imsi != NULL)
949                 ident = modem->imsi;
950         else if (modem->serial != NULL)
951                 ident = modem->serial;
952
953         if (connman_dbus_validate_ident(ident) == FALSE)
954                 ident = connman_dbus_encode_string(ident);
955         else
956                 ident = g_strdup(ident);
957
958         device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
959         if (device == NULL)
960                 goto out;
961
962         DBG("device %p", device);
963
964         connman_device_set_ident(device, ident);
965
966         connman_device_set_string(device, "Path", modem->path);
967
968         connman_device_set_data(device, modem);
969
970         if (connman_device_register(device) < 0) {
971                 connman_error("Failed to register cellular device");
972                 connman_device_unref(device);
973                 goto out;
974         }
975
976         modem->device = device;
977
978         connman_device_set_powered(modem->device, modem->online);
979 out:
980         g_free(ident);
981 }
982
983 static void destroy_device(struct modem_data *modem)
984 {
985         DBG("%s", modem->path);
986
987         connman_device_set_powered(modem->device, FALSE);
988
989         if (modem->network != NULL) {
990                 connman_device_remove_network(modem->device, modem->network);
991                 connman_network_unref(modem->network);
992                 modem->network = NULL;
993         }
994
995         connman_device_unregister(modem->device);
996         connman_device_unref(modem->device);
997
998         modem->device = NULL;
999 }
1000
1001 static void add_network(struct modem_data *modem)
1002 {
1003         const char *group;
1004
1005         DBG("%s", modem->path);
1006
1007         if (modem->network != NULL)
1008                 return;
1009
1010         modem->network = connman_network_create(modem->context->path,
1011                                                 CONNMAN_NETWORK_TYPE_CELLULAR);
1012         if (modem->network == NULL)
1013                 return;
1014
1015         DBG("network %p", modem->network);
1016
1017         connman_network_set_data(modem->network, modem);
1018
1019         connman_network_set_string(modem->network, "Path",
1020                                         modem->context->path);
1021
1022         connman_network_set_index(modem->network, modem->context->index);
1023
1024         if (modem->name != NULL)
1025                 connman_network_set_name(modem->network, modem->name);
1026         else
1027                 connman_network_set_name(modem->network, "");
1028
1029         connman_network_set_strength(modem->network, modem->strength);
1030
1031         group = get_ident(modem->context->path);
1032         connman_network_set_group(modem->network, group);
1033
1034         connman_network_set_available(modem->network, TRUE);
1035
1036         connman_network_set_bool(modem->network, "Roaming",
1037                                         modem->roaming);
1038
1039         if (connman_device_add_network(modem->device, modem->network) < 0) {
1040                 connman_network_unref(modem->network);
1041                 modem->network = NULL;
1042                 return;
1043         }
1044
1045         /*
1046          * Create the ipconfig layer before trying to connect. Withouth
1047          * the ipconfig layer the core is not ready to process errors.
1048          */
1049         connman_network_set_index(modem->network, -1);
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 *connection,
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 *connection,
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 *connection,
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 *connection, 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 *connection,
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 *connection, 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 *connection,
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 *connection, 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 *connection, 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 *connection,
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 *connection,
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)