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