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