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