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