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