ofono: Retry autoconnect when gprs gets attached
[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  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include <gdbus.h>
31 #include <string.h>
32
33 #define CONNMAN_API_SUBJECT_TO_CHANGE
34 #include <connman/plugin.h>
35 #include <connman/element.h>
36 #include <connman/device.h>
37 #include <connman/network.h>
38 #include <connman/dbus.h>
39 #include <connman/inet.h>
40 #include <connman/technology.h>
41 #include <connman/log.h>
42
43 #include "mcc.h"
44
45 #define OFONO_SERVICE                   "org.ofono"
46
47 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
49 #define OFONO_GPRS_INTERFACE            OFONO_SERVICE ".ConnectionManager"
50 #define OFONO_CONTEXT_INTERFACE         OFONO_SERVICE ".ConnectionContext"
51 #define OFONO_SIM_INTERFACE             OFONO_SERVICE ".SimManager"
52 #define OFONO_REGISTRATION_INTERFACE    OFONO_SERVICE ".NetworkRegistration"
53
54 #define PROPERTY_CHANGED                "PropertyChanged"
55 #define GET_PROPERTIES                  "GetProperties"
56 #define SET_PROPERTY                    "SetProperty"
57 #define CONTEXT_ADDED                   "ContextAdded"
58 #define CONTEXT_REMOVED                 "ContextRemoved"
59 #define ADD_CONTEXT                     "AddContext"
60 #define GET_MODEMS                      "GetModems"
61 #define MODEM_ADDED                     "ModemAdded"
62 #define MODEM_REMOVED                   "ModemRemoved"
63
64
65 #define TIMEOUT 40000
66
67 static DBusConnection *connection;
68
69 static GHashTable *modem_hash = NULL;
70
71 static GHashTable *network_hash;
72
73 struct modem_data {
74         char *path;
75         struct connman_device *device;
76         gboolean has_sim;
77         gboolean has_reg;
78         gboolean has_gprs;
79         gboolean available;
80         gboolean pending_online;
81         dbus_bool_t requested_online;
82         dbus_bool_t online;
83
84         /* org.ofono.ConnectionManager properties */
85         dbus_bool_t powered;
86         dbus_bool_t attached;
87         dbus_bool_t roaming_allowed;
88
89         uint8_t strength, has_strength;
90 };
91
92 static int modem_probe(struct connman_device *device)
93 {
94         DBG("device %p", device);
95
96         return 0;
97 }
98
99 static void modem_remove(struct connman_device *device)
100 {
101         DBG("device %p", device);
102 }
103
104 static int call_ofono(const char *path,
105                         const char *interface, const char *method,
106                         DBusPendingCallNotifyFunction notify, void *user_data,
107                         DBusFreeFunction free_function,
108                         int type, ...)
109 {
110         DBusMessage *message;
111         DBusPendingCall *call;
112         dbus_bool_t ok;
113         va_list va;
114
115         DBG("path %s %s.%s", path, interface, method);
116
117         if (path == NULL)
118                 return -EINVAL;
119
120         message = dbus_message_new_method_call(OFONO_SERVICE, path,
121                                         interface, method);
122         if (message == NULL)
123                 return -ENOMEM;
124
125         dbus_message_set_auto_start(message, FALSE);
126
127         va_start(va, type);
128         ok = dbus_message_append_args_valist(message, type, va);
129         va_end(va);
130
131         if (!ok)
132                 return -ENOMEM;
133
134         if (dbus_connection_send_with_reply(connection, message,
135                                                 &call, TIMEOUT) == FALSE) {
136                 connman_error("Failed to call %s.%s", interface, method);
137                 dbus_message_unref(message);
138                 return -EINVAL;
139         }
140
141         if (call == NULL) {
142                 connman_error("D-Bus connection not available");
143                 dbus_message_unref(message);
144                 return -EINVAL;
145         }
146
147         dbus_pending_call_set_notify(call, notify, user_data, free_function);
148
149         dbus_message_unref(message);
150
151         return -EINPROGRESS;
152 }
153
154 static void set_property_reply(DBusPendingCall *call, void *user_data)
155 {
156         DBusMessage *reply;
157         DBusError error;
158         char const *name = user_data;
159
160         DBG("");
161
162         dbus_error_init(&error);
163
164         reply = dbus_pending_call_steal_reply(call);
165
166         if (dbus_set_error_from_message(&error, reply)) {
167                 connman_error("SetProperty(%s) %s %s", name,
168                                 error.name, error.message);
169                 dbus_error_free(&error);
170         }
171
172         dbus_message_unref(reply);
173
174         dbus_pending_call_unref(call);
175 }
176
177 static int set_property(const char *path, const char *interface,
178                         const char *property, int type, void *value,
179                         DBusPendingCallNotifyFunction notify, void *user_data,
180                         DBusFreeFunction free_function)
181 {
182         DBusMessage *message;
183         DBusMessageIter iter;
184         DBusPendingCall *call;
185
186         DBG("path %s %s.%s", path, interface, property);
187
188         g_assert(notify == NULL ? free_function == NULL : 1);
189
190         if (path == NULL)
191                 return -EINVAL;
192
193         message = dbus_message_new_method_call(OFONO_SERVICE, path,
194                                         interface, SET_PROPERTY);
195         if (message == NULL)
196                 return -ENOMEM;
197
198         dbus_message_set_auto_start(message, FALSE);
199
200         dbus_message_iter_init_append(message, &iter);
201         connman_dbus_property_append_basic(&iter, property, type, value);
202
203         if (dbus_connection_send_with_reply(connection, message,
204                                                 &call, TIMEOUT) == FALSE) {
205                 connman_error("Failed to change \"%s\" property on %s",
206                                 property, interface);
207                 dbus_message_unref(message);
208                 return -EINVAL;
209         }
210
211         if (call == NULL) {
212                 connman_error("D-Bus connection not available");
213                 dbus_message_unref(message);
214                 return -EINVAL;
215         }
216
217         if (notify == NULL) {
218                 notify = set_property_reply;
219                 user_data = (void *)property;
220                 free_function = NULL;
221         }
222
223         dbus_pending_call_set_notify(call, notify, user_data, free_function);
224
225         dbus_message_unref(message);
226
227         return -EINPROGRESS;
228 }
229
230 static void update_modem_online(struct modem_data *modem,
231                                 connman_bool_t online)
232 {
233         DBG("modem %p path %s online %d", modem, modem->path, online);
234
235         modem->online = online;
236         modem->requested_online = online;
237         modem->pending_online = FALSE;
238
239         if (modem->device)
240                 connman_device_set_powered(modem->device, online);
241 }
242
243 static void set_online_reply(DBusPendingCall *call, void *user_data)
244 {
245         struct modem_data *modem;
246         DBusMessage *reply;
247         DBusError error;
248         gboolean result;
249
250         DBG("path %s", (char *)user_data);
251
252         if (modem_hash == NULL)
253                 return;
254
255         modem = g_hash_table_lookup(modem_hash, user_data);
256         if (modem == NULL)
257                 return;
258
259         reply = dbus_pending_call_steal_reply(call);
260
261         dbus_error_init(&error);
262
263         if (dbus_set_error_from_message(&error, reply)) {
264                 connman_error("SetProperty(Online) %s %s",
265                                 error.name, error.message);
266                 dbus_error_free(&error);
267
268                 result = modem->online;
269         } else
270                 result = modem->requested_online;
271
272         if (modem->pending_online)
273                 update_modem_online(modem, result);
274
275         dbus_message_unref(reply);
276
277         dbus_pending_call_unref(call);
278 }
279
280 static int modem_change_online(char const *path, dbus_bool_t online)
281 {
282         struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
283
284         if (modem == NULL)
285                 return -ENODEV;
286
287         if (modem->online == online)
288                 return -EALREADY;
289
290         modem->requested_online = online;
291
292         return set_property(path, OFONO_MODEM_INTERFACE, "Online",
293                                 DBUS_TYPE_BOOLEAN, &online,
294                                 set_online_reply,
295                                 (void *)g_strdup(path), g_free);
296 }
297
298 static int modem_enable(struct connman_device *device)
299 {
300         const char *path = connman_device_get_string(device, "Path");
301
302         DBG("device %p, path, %s", device, path);
303
304         return modem_change_online(path, TRUE);
305 }
306
307 static int modem_disable(struct connman_device *device)
308 {
309         const char *path = connman_device_get_string(device, "Path");
310
311         DBG("device %p path %s", device, path);
312
313         return modem_change_online(path, FALSE);
314 }
315
316 static struct connman_device_driver modem_driver = {
317         .name           = "modem",
318         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
319         .probe          = modem_probe,
320         .remove         = modem_remove,
321         .enable         = modem_enable,
322         .disable        = modem_disable,
323 };
324
325 static void modem_remove_device(struct modem_data *modem)
326 {
327         DBG("modem %p path %s device %p", modem, modem->path, modem->device);
328
329         if (modem->device == NULL)
330                 return;
331
332         connman_device_remove_all_networks(modem->device);
333         connman_device_unregister(modem->device);
334         connman_device_unref(modem->device);
335
336         modem->device = NULL;
337 }
338
339 static void remove_modem(gpointer data)
340 {
341         struct modem_data *modem = data;
342
343         modem_remove_device(modem);
344
345         g_free(modem->path);
346
347         g_free(modem);
348 }
349
350 static void remove_network(gpointer data)
351 {
352         connman_network_unref(data);
353 }
354
355 static char *get_ident(const char *path)
356 {
357         char *pos;
358
359         if (*path != '/')
360                 return NULL;
361
362         pos = strrchr(path, '/');
363         if (pos == NULL)
364                 return NULL;
365
366         return pos + 1;
367 }
368
369 static void create_service(struct connman_network *network)
370 {
371         const char *path;
372         char *group;
373
374         DBG("");
375
376         path = connman_network_get_string(network, "Path");
377
378         group = get_ident(path);
379
380         connman_network_set_group(network, group);
381 }
382
383 static int network_probe(struct connman_network *network)
384 {
385         return 0;
386 }
387
388 static gboolean pending_network_is_available(struct connman_network *network)
389 {
390         /* Modem or network may be removed */
391         if (network == NULL || connman_network_get_device(network) == NULL) {
392                 DBG("Modem or network was removed");
393                 return FALSE;
394         }
395
396         return TRUE;
397 }
398
399 static void set_active_reply(DBusPendingCall *call, void *user_data)
400 {
401         char const *path = user_data;
402         DBusMessage *reply;
403         DBusError error;
404         struct connman_network *network;
405
406         network = g_hash_table_lookup(network_hash, path);
407
408         DBG("path %s network %p", path, network);
409
410         reply = dbus_pending_call_steal_reply(call);
411
412         if (!pending_network_is_available(network))
413                 goto done;
414
415         dbus_error_init(&error);
416
417         if (dbus_set_error_from_message(&error, reply)) {
418                 connman_error("SetProperty(Active) %s %s",
419                                 error.name, error.message);
420
421                 if (connman_network_get_index(network) < 0)
422                         connman_network_set_error(network,
423                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
424
425                 dbus_error_free(&error);
426         }
427
428 done:
429         dbus_message_unref(reply);
430
431         dbus_pending_call_unref(call);
432 }
433
434 static int set_network_active(struct connman_network *network,
435                                                 dbus_bool_t active)
436 {
437         int error;
438
439         const char *path = connman_network_get_string(network, "Path");
440
441         DBG("network %p, path %s, active %d", network, path, active);
442
443         error = set_property(path, OFONO_CONTEXT_INTERFACE,
444                                 "Active", DBUS_TYPE_BOOLEAN, &active,
445                                 set_active_reply, g_strdup(path), NULL);
446
447         if (active == FALSE && error == -EINPROGRESS)
448                 error = 0;
449
450         return error;
451 }
452
453 static void set_apn(struct connman_network *network)
454 {
455         const char *apn, *path;
456
457         apn = connman_network_get_string(network, "Cellular.APN");
458         if (apn == NULL)
459                 return;
460
461         path = connman_network_get_string(network, "Path");
462         if (path == NULL)
463                 return;
464
465         DBG("path %s, apn %s", path, apn);
466
467         set_property(path, OFONO_CONTEXT_INTERFACE,
468                         "AccessPointName", DBUS_TYPE_STRING, &apn,
469                         NULL, NULL, NULL);
470 }
471
472 static int network_connect(struct connman_network *network)
473 {
474         DBG("network %p", network);
475
476         if (connman_network_get_index(network) >= 0)
477                 return -EISCONN;
478
479         return set_network_active(network, TRUE);
480 }
481
482 static int network_disconnect(struct connman_network *network)
483 {
484         DBG("network %p", network);
485
486         if (connman_network_get_index(network) < 0)
487                 return -ENOTCONN;
488
489         return set_network_active(network, FALSE);
490 }
491
492 static void network_remove(struct connman_network *network)
493 {
494         char const *path = connman_network_get_string(network, "Path");
495
496         DBG("network %p path %s", network, path);
497
498         g_hash_table_remove(network_hash, path);
499 }
500
501 static int network_setup(struct connman_network *network, const char *key)
502 {
503         if (g_strcmp0(key, "Cellular.APN") == 0)
504                 set_apn(network);
505
506         return 0;
507 }
508
509 static struct connman_network_driver network_driver = {
510         .name           = "network",
511         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
512         .probe          = network_probe,
513         .remove         = network_remove,
514         .connect        = network_connect,
515         .disconnect     = network_disconnect,
516         .setup          = network_setup,
517 };
518
519 static void update_settings(DBusMessageIter *array,
520                                 struct connman_network *network);
521
522 static void set_connected(struct connman_network *network,
523                                 connman_bool_t connected);
524
525 static int add_network(struct connman_device *device,
526                         const char *path, DBusMessageIter *dict)
527 {
528         struct modem_data *modem = connman_device_get_data(device);
529         struct connman_network *network;
530         char *ident;
531         const char *hash_path;
532         char const *operator;
533         char const *reg_status;
534         dbus_bool_t active = FALSE;
535
536         DBG("modem %p device %p path %s", modem, device, path);
537
538         ident = get_ident(path);
539
540         network = connman_device_get_network(device, ident);
541         if (network != NULL)
542                 return -EALREADY;
543
544         network = g_hash_table_lookup(network_hash, path);
545         if (network != NULL) {
546                 DBG("path %p already exists with device %p", path,
547                         connman_network_get_device(network));
548                 if (connman_network_get_device(network))
549                         return -EALREADY;
550                 g_hash_table_remove(network_hash, path);
551         }
552
553         network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
554         if (network == NULL)
555                 return -ENOMEM;
556
557         connman_network_set_string(network, "Path", path);
558         hash_path = connman_network_get_string(network, "Path");
559         if (hash_path == NULL)
560                 goto error;
561
562         create_service(network);
563
564         connman_network_ref(network);
565         g_hash_table_insert(network_hash, (char *)hash_path, network);
566
567         connman_network_set_available(network, TRUE);
568         connman_network_set_index(network, -1);
569
570         operator = connman_device_get_string(device, "Operator");
571         if (operator)
572                 connman_network_set_name(network, operator);
573
574         if (modem->has_strength)
575                 connman_network_set_strength(network, modem->strength);
576
577         reg_status = connman_device_get_string(device, "RegistrationStatus");
578
579         if (!g_strcmp0(reg_status, "roaming"))
580                 connman_network_set_roaming(network, TRUE);
581         else if (!g_strcmp0(reg_status, "registered"))
582                 connman_network_set_roaming(network, FALSE);
583
584         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
585                 DBusMessageIter entry, value;
586                 const char *key;
587
588                 dbus_message_iter_recurse(dict, &entry);
589                 dbus_message_iter_get_basic(&entry, &key);
590
591                 dbus_message_iter_next(&entry);
592                 dbus_message_iter_recurse(&entry, &value);
593
594                 if (g_str_equal(key, "Type")) {
595                         const char *type;
596
597                         dbus_message_iter_get_basic(&value, &type);
598                         if (g_strcmp0(type, "internet") != 0) {
599                                 DBG("path %p type %s", path, type);
600                                 goto error;
601                         }
602                 } else if (g_str_equal(key, "AccessPointName")) {
603                         const char *ap;
604
605                         dbus_message_iter_get_basic(&value, &ap);
606
607                         connman_network_set_string(network, "Cellular.APN", ap);
608                 } else if (g_str_equal(key, "Settings"))
609                         update_settings(&value, network);
610                 else if (g_str_equal(key, "Active") == TRUE)
611                         dbus_message_iter_get_basic(&value, &active);
612
613                 dbus_message_iter_next(dict);
614         }
615
616         if (connman_device_add_network(device, network) != 0)
617                 goto error;
618
619         if (active)
620                 set_connected(network, active);
621
622         return 0;
623
624 error:
625         connman_network_unref(network);
626         g_hash_table_remove(network_hash, hash_path);
627         return -EIO;
628 }
629
630 static void check_networks_reply(DBusPendingCall *call, void *user_data)
631 {
632         char *path = user_data;
633         struct modem_data *modem;
634         DBusMessage *reply;
635         DBusMessageIter array, entry, value, properties;
636
637         DBG("path %s", path);
638
639         modem = g_hash_table_lookup(modem_hash, path);
640         if (modem == NULL)
641                 return;
642         if (modem->device == NULL)
643                 return;
644
645         reply = dbus_pending_call_steal_reply(call);
646
647         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
648                 goto done;
649
650         dbus_message_iter_init(reply, &array);
651
652         dbus_message_iter_recurse(&array, &entry);
653
654         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
655                 char const *network_path;
656
657                 dbus_message_iter_recurse(&entry, &value);
658                 dbus_message_iter_get_basic(&value, &network_path);
659
660                 dbus_message_iter_next(&value);
661                 dbus_message_iter_recurse(&value, &properties);
662
663                 add_network(modem->device, network_path, &properties);
664
665                 dbus_message_iter_next(&entry);
666         }
667
668 done:
669         dbus_message_unref(reply);
670
671         dbus_pending_call_unref(call);
672 }
673
674 static void check_networks(struct modem_data *modem)
675 {
676         char const *path = modem->path;
677
678         DBG("modem %p path %s", modem, path);
679
680         call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
681                         check_networks_reply, g_strdup(path), g_free,
682                         DBUS_TYPE_INVALID);
683 }
684
685 static void modem_clear_network_errors(struct modem_data *modem)
686 {
687         struct connman_device *device = modem->device;
688         GHashTableIter i;
689         gpointer value;
690
691         if (device == NULL)
692                 return;
693
694         g_hash_table_iter_init(&i, network_hash);
695
696         while (g_hash_table_iter_next(&i, NULL, &value)) {
697                 struct connman_network *network = value;
698
699                 if (connman_network_get_device(network) == device)
700                         connman_network_clear_error(network);
701         }
702 }
703
704 static void modem_operator_name_changed(struct modem_data *modem,
705                                         char const *name)
706 {
707         struct connman_device *device = modem->device;
708         GHashTableIter i;
709         gpointer value;
710
711         if (device == NULL)
712                 return;
713
714         connman_device_set_string(device, "Operator", name);
715
716         for (g_hash_table_iter_init(&i, network_hash);
717              g_hash_table_iter_next(&i, NULL, &value);) {
718                 struct connman_network *network = value;
719
720                 if (connman_network_get_device(network) == device) {
721                         connman_network_set_name(network, name);
722                         connman_network_update(network);
723                 }
724         }
725 }
726
727 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
728 {
729         struct connman_device *device = modem->device;
730         GHashTableIter i;
731         gpointer value;
732
733         modem->strength = strength;
734         modem->has_strength = TRUE;
735
736         if (device == NULL)
737                 return;
738
739         for (g_hash_table_iter_init(&i, network_hash);
740              g_hash_table_iter_next(&i, NULL, &value);) {
741                 struct connman_network *network = value;
742
743                 if (connman_network_get_device(network) == device) {
744                         connman_network_set_strength(network, strength);
745                         connman_network_update(network);
746                 }
747         }
748 }
749
750 static void modem_roaming_changed(struct modem_data *modem,
751                                         char const *status)
752 {
753         struct connman_device *device = modem->device;
754         connman_bool_t roaming;
755         GHashTableIter i;
756         gpointer value;
757
758         if (device == NULL)
759                 return;
760
761         connman_device_set_string(device, "RegistrationStatus", status);
762
763         if (g_str_equal(status, "roaming"))
764                 roaming = TRUE;
765         else if (g_str_equal(status, "registered"))
766                 roaming = FALSE;
767         else
768                 return;
769
770         for (g_hash_table_iter_init(&i, network_hash);
771              g_hash_table_iter_next(&i, NULL, &value);) {
772                 struct connman_network *network = value;
773
774                 if (connman_network_get_device(network) == device) {
775                         connman_network_set_roaming(network, roaming);
776                         connman_network_update(network);
777                 }
778         }
779 }
780
781 static void modem_registration_changed(struct modem_data *modem,
782                                         DBusMessageIter *entry)
783 {
784         DBusMessageIter iter;
785         const char *key;
786         int type;
787         connman_uint8_t strength;
788         char const *name, *status, *mcc_s;
789
790         dbus_message_iter_get_basic(entry, &key);
791
792         DBG("key %s", key);
793
794         dbus_message_iter_next(entry);
795
796         dbus_message_iter_recurse(entry, &iter);
797
798         type = dbus_message_iter_get_arg_type(&iter);
799         if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
800                 return;
801
802         if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
803                 dbus_message_iter_get_basic(&iter, &name);
804                 modem_operator_name_changed(modem, name);
805         } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
806                 dbus_message_iter_get_basic(&iter, &strength);
807                 modem_strength_changed(modem, strength);
808         } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
809                 dbus_message_iter_get_basic(&iter, &status);
810                 modem_roaming_changed(modem, status);
811         } else if (g_str_equal(key, "MobileCountryCode") &&
812                                         type == DBUS_TYPE_STRING) {
813                 int mcc;
814                 char *alpha2;
815
816                 dbus_message_iter_get_basic(&iter, &mcc_s);
817
818                 mcc = atoi(mcc_s);
819                 if (mcc > 799)
820                         return;
821
822                 alpha2 = mcc_country_codes[mcc - 200];
823                 connman_technology_set_regdom(alpha2);
824         }
825
826 }
827
828 static gboolean reg_changed(DBusConnection *connection,
829                                 DBusMessage *message, void *user_data)
830 {
831         const char *path = dbus_message_get_path(message);
832         struct modem_data *modem;
833         DBusMessageIter iter;
834
835         DBG("path %s", path);
836
837         modem = g_hash_table_lookup(modem_hash, path);
838         if (modem == NULL)
839                 return TRUE;
840
841         if (dbus_message_iter_init(message, &iter))
842                 modem_registration_changed(modem, &iter);
843
844         return TRUE;
845 }
846
847 static void check_registration_reply(DBusPendingCall *call, void *user_data)
848 {
849         char const *path = user_data;
850         struct modem_data *modem;
851         DBusMessage *reply;
852         DBusMessageIter array, dict, entry;
853
854         DBG("path %s", path);
855
856         modem = g_hash_table_lookup(modem_hash, path);
857         if (modem == NULL)
858                 return;
859
860         reply = dbus_pending_call_steal_reply(call);
861
862         if (dbus_message_iter_init(reply, &array) == FALSE)
863                 goto done;
864
865         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
866                 goto done;
867
868         dbus_message_iter_recurse(&array, &dict);
869         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
870                 dbus_message_iter_recurse(&dict, &entry);
871                 modem_registration_changed(modem, &entry);
872                 dbus_message_iter_next(&dict);
873         }
874
875 done:
876         dbus_message_unref(reply);
877
878         dbus_pending_call_unref(call);
879 }
880
881 static void check_registration(struct modem_data *modem)
882 {
883         char const *path = modem->path;
884
885         DBG("modem %p path %s", modem, path);
886
887         call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
888                         check_registration_reply, g_strdup(path), g_free,
889                         DBUS_TYPE_INVALID);
890 }
891
892 static void modem_gprs_changed(struct modem_data *modem,
893                                         DBusMessageIter *entry)
894 {
895         DBusMessageIter iter;
896         const char *key;
897         int type;
898         dbus_bool_t value;
899
900         dbus_message_iter_get_basic(entry, &key);
901
902         DBG("key %s", key);
903
904         dbus_message_iter_next(entry);
905
906         dbus_message_iter_recurse(entry, &iter);
907
908         type = dbus_message_iter_get_arg_type(&iter);
909
910         if (type != DBUS_TYPE_BOOLEAN)
911                 return;
912
913         dbus_message_iter_get_basic(&iter, &value);
914
915         if (g_str_equal(key, "Attached") == TRUE) {
916                 DBG("Attached %d", value);
917
918                 modem->attached = value;
919
920                 if (value)
921                         modem_clear_network_errors(modem);
922         } else if (g_str_equal(key, "Powered") == TRUE) {
923                 DBG("Powered %d", value);
924
925                 modem->powered = value;
926         } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
927                 DBG("RoamingAllowed %d", value);
928
929                 modem->roaming_allowed = value;
930         }
931 }
932
933 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
934 {
935         char const *path = user_data;
936         struct modem_data *modem;
937         DBusMessage *reply;
938         DBusMessageIter array, dict, entry;
939
940         DBG("path %s", path);
941
942         modem = g_hash_table_lookup(modem_hash, path);
943         if (modem == NULL)
944                 return;
945
946         reply = dbus_pending_call_steal_reply(call);
947
948         if (dbus_message_iter_init(reply, &array) == FALSE)
949                 goto done;
950
951         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
952                 goto done;
953
954         dbus_message_iter_recurse(&array, &dict);
955         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
956                 dbus_message_iter_recurse(&dict, &entry);
957                 modem_gprs_changed(modem, &entry);
958                 dbus_message_iter_next(&dict);
959         }
960
961 done:
962         dbus_message_unref(reply);
963
964         dbus_pending_call_unref(call);
965
966         check_networks(modem);
967 }
968
969 static void check_gprs(struct modem_data *modem)
970 {
971         char const *path = modem->path;
972
973         DBG("modem %p path %s", modem, path);
974
975         call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
976                         check_gprs_reply, g_strdup(path), g_free,
977                         DBUS_TYPE_INVALID);
978 }
979
980 static void add_device(const char *path, const char *imsi)
981 {
982         struct modem_data *modem;
983         struct connman_device *device;
984
985         DBG("path %s imsi %s", path, imsi);
986
987         if (path == NULL)
988                 return;
989
990         if (imsi == NULL)
991                 return;
992
993         modem = g_hash_table_lookup(modem_hash, path);
994         if (modem == NULL)
995                 return;
996
997         if (modem->device) {
998                 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
999                         return;
1000
1001                 modem_remove_device(modem);
1002         }
1003
1004         if (strlen(imsi) == 0)
1005                 return;
1006
1007         device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1008         if (device == NULL)
1009                 return;
1010
1011         connman_device_set_ident(device, imsi);
1012
1013         connman_device_set_string(device, "Path", path);
1014
1015         connman_device_set_data(device, modem);
1016
1017         if (connman_device_register(device) < 0) {
1018                 connman_device_unref(device);
1019                 return;
1020         }
1021
1022         modem->device = device;
1023
1024         if (modem->has_reg)
1025                 check_registration(modem);
1026
1027         if (modem->has_gprs)
1028                 check_gprs(modem);
1029 }
1030
1031 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1032 {
1033         const char *path = user_data;
1034         const char *imsi = NULL;
1035         DBusMessage *reply;
1036         DBusMessageIter array, dict;
1037
1038         DBG("path %s", path);
1039
1040         reply = dbus_pending_call_steal_reply(call);
1041
1042         if (dbus_message_iter_init(reply, &array) == FALSE)
1043                 goto done;
1044
1045         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1046                 goto done;
1047
1048         dbus_message_iter_recurse(&array, &dict);
1049
1050         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1051                 DBusMessageIter entry, value;
1052                 const char *key;
1053
1054                 dbus_message_iter_recurse(&dict, &entry);
1055                 dbus_message_iter_get_basic(&entry, &key);
1056
1057                 dbus_message_iter_next(&entry);
1058                 dbus_message_iter_recurse(&entry, &value);
1059
1060                 if (g_str_equal(key, "SubscriberIdentity")) {
1061                         dbus_message_iter_get_basic(&value, &imsi);
1062                         add_device(path, imsi);
1063                 }
1064
1065                 dbus_message_iter_next(&dict);
1066         }
1067
1068 done:
1069         dbus_message_unref(reply);
1070
1071         dbus_pending_call_unref(call);
1072 }
1073
1074 static void get_imsi(const char *path)
1075 {
1076         DBG("path %s", path);
1077
1078         call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1079                         sim_properties_reply, g_strdup(path), g_free,
1080                         DBUS_TYPE_INVALID);
1081 }
1082
1083 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1084 {
1085         DBG("path %s powered %d", path, powered);
1086
1087         return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1088                                 DBUS_TYPE_BOOLEAN, &powered,
1089                                 NULL, NULL, NULL);
1090 }
1091
1092 static int modem_change_powered(const char *path, dbus_bool_t powered)
1093 {
1094         DBG("path %s powered %d", path, powered);
1095
1096         return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1097                                 DBUS_TYPE_BOOLEAN, &powered,
1098                                 NULL, NULL, NULL);
1099 }
1100
1101
1102 static gboolean modem_has_interface(DBusMessageIter *array,
1103                                         char const *interface)
1104 {
1105         DBusMessageIter entry;
1106
1107         dbus_message_iter_recurse(array, &entry);
1108
1109         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1110                 const char *element;
1111
1112                 dbus_message_iter_get_basic(&entry, &element);
1113
1114                 if (g_strcmp0(interface, element) == 0)
1115                         return TRUE;
1116
1117                 dbus_message_iter_next(&entry);
1118         }
1119
1120         return FALSE;
1121 }
1122
1123 static gboolean modem_has_sim(DBusMessageIter *array)
1124 {
1125         return modem_has_interface(array, OFONO_SIM_INTERFACE);
1126 }
1127
1128 static gboolean modem_has_reg(DBusMessageIter *array)
1129 {
1130         return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1131 }
1132
1133 static gboolean modem_has_gprs(DBusMessageIter *array)
1134 {
1135         return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1136 }
1137
1138 static void add_modem(const char *path, DBusMessageIter *prop)
1139 {
1140         struct modem_data *modem;
1141         dbus_bool_t powered = FALSE;
1142         dbus_bool_t online = FALSE;
1143         dbus_bool_t has_online = FALSE;
1144         dbus_bool_t locked = FALSE;
1145         gboolean has_sim = FALSE;
1146         gboolean has_reg = FALSE;
1147         gboolean has_gprs = FALSE;
1148
1149         modem = g_hash_table_lookup(modem_hash, path);
1150
1151         if (modem != NULL)
1152                 return;
1153
1154         modem = g_try_new0(struct modem_data, 1);
1155         if (modem == NULL)
1156                 return;
1157
1158         modem->path = g_strdup(path);
1159         modem->device = NULL;
1160         modem->available = TRUE;
1161
1162         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1163
1164         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1165                 DBusMessageIter entry, value;
1166                 const char *key;
1167
1168                 dbus_message_iter_recurse(prop, &entry);
1169                 dbus_message_iter_get_basic(&entry, &key);
1170
1171                 dbus_message_iter_next(&entry);
1172                 dbus_message_iter_recurse(&entry, &value);
1173
1174                 if (g_str_equal(key, "Powered") == TRUE)
1175                         dbus_message_iter_get_basic(&value, &powered);
1176                 else if (g_str_equal(key, "Lockdown") == TRUE)
1177                         dbus_message_iter_get_basic(&value, &locked);
1178                 else if (g_str_equal(key, "Online") == TRUE) {
1179                         has_online = TRUE;
1180                         dbus_message_iter_get_basic(&value, &online);
1181                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1182                         has_sim = modem_has_sim(&value);
1183                         has_reg = modem_has_reg(&value);
1184                         has_gprs = modem_has_gprs(&value);
1185                 }
1186
1187                 dbus_message_iter_next(prop);
1188         }
1189
1190         if (locked)
1191                 return;
1192
1193         if (!powered)
1194                 modem_change_powered(path, TRUE);
1195
1196         modem->has_sim = has_sim;
1197         modem->has_reg = has_reg;
1198         modem->has_gprs = has_gprs;
1199
1200         update_modem_online(modem, online);
1201
1202         if (has_sim)
1203                 get_imsi(path);
1204 }
1205
1206 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1207 {
1208         DBusMessage *reply;
1209         DBusError error;
1210         DBusMessageIter array, dict;
1211
1212         DBG("");
1213
1214         reply = dbus_pending_call_steal_reply(call);
1215
1216         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1217                 goto done;
1218
1219         dbus_error_init(&error);
1220
1221         if (dbus_set_error_from_message(&error, reply)) {
1222                 connman_error("ModemManager.GetModems() %s %s",
1223                                 error.name, error.message);
1224                 dbus_error_free(&error);
1225                 goto done;
1226         }
1227
1228         if (dbus_message_iter_init(reply, &array) == FALSE)
1229                 goto done;
1230
1231         dbus_message_iter_recurse(&array, &dict);
1232
1233         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1234                 DBusMessageIter value, properties;
1235                 const char *modem_path;
1236
1237                 dbus_message_iter_recurse(&dict, &value);
1238                 dbus_message_iter_get_basic(&value, &modem_path);
1239
1240                 dbus_message_iter_next(&value);
1241                 dbus_message_iter_recurse(&value, &properties);
1242
1243                 /* Add modem */
1244                 add_modem(modem_path, &properties);
1245
1246                 dbus_message_iter_next(&dict);
1247         }
1248
1249 done:
1250         dbus_message_unref(reply);
1251
1252         dbus_pending_call_unref(call);
1253 }
1254
1255 static void ofono_connect(DBusConnection *connection, void *user_data)
1256 {
1257         DBG("connection %p", connection);
1258
1259         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1260                                                 g_free, remove_modem);
1261
1262         network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1263                                                 NULL, remove_network);
1264
1265         call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1266                         manager_modems_reply, NULL, NULL,
1267                         DBUS_TYPE_INVALID);
1268 }
1269
1270 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1271 {
1272         DBG("connection %p", connection);
1273
1274         if (modem_hash == NULL)
1275                 return;
1276
1277         g_hash_table_destroy(modem_hash);
1278
1279         modem_hash = NULL;
1280 }
1281
1282 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1283                                 void *user_data)
1284 {
1285         const char *path = dbus_message_get_path(message);
1286         struct modem_data *modem;
1287         DBusMessageIter iter, value;
1288         const char *key;
1289
1290         DBG("path %s", path);
1291
1292         modem = g_hash_table_lookup(modem_hash, path);
1293         if (modem == NULL)
1294                 return TRUE;
1295
1296         if (dbus_message_iter_init(message, &iter) == FALSE)
1297                 return TRUE;
1298
1299         dbus_message_iter_get_basic(&iter, &key);
1300
1301         dbus_message_iter_next(&iter);
1302         dbus_message_iter_recurse(&iter, &value);
1303
1304         if (g_str_equal(key, "Powered") == TRUE) {
1305                 dbus_bool_t powered;
1306
1307                 dbus_message_iter_get_basic(&value, &powered);
1308                 if (powered == TRUE)
1309                         return TRUE;
1310
1311                 modem->has_sim = FALSE;
1312                 modem->has_reg = FALSE;
1313                 modem->has_gprs = FALSE;
1314
1315                 modem_remove_device(modem);
1316         } else if (g_str_equal(key, "Online") == TRUE) {
1317                 dbus_bool_t online;
1318
1319                 dbus_message_iter_get_basic(&value, &online);
1320
1321                 update_modem_online(modem, online);
1322         } else if (g_str_equal(key, "Lockdown") == TRUE) {
1323                 dbus_bool_t locked;
1324
1325                 dbus_message_iter_get_basic(&value, &locked);
1326
1327                 if (!locked)
1328                         modem_change_powered(path, TRUE);
1329
1330         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1331                 gboolean has_sim = modem_has_sim(&value);
1332                 gboolean has_reg = modem_has_reg(&value);
1333                 gboolean added_reg = has_reg && !modem->has_reg;
1334                 gboolean has_gprs = modem_has_gprs(&value);
1335                 gboolean added_gprs = has_gprs && !modem->has_gprs;
1336
1337                 modem->has_sim = has_sim;
1338                 modem->has_reg = has_reg;
1339                 modem->has_gprs = has_gprs;
1340
1341                 if (modem->device == NULL) {
1342                         if (has_sim)
1343                                 get_imsi(modem->path);
1344                 } else if (!has_sim) {
1345                         modem_remove_device(modem);
1346                 } else {
1347                         if (added_reg)
1348                                 check_registration(modem);
1349
1350                         if (added_gprs) {
1351                                 gprs_change_powered(modem->path, TRUE);
1352                                 check_gprs(modem);
1353                         }
1354                 }
1355         }
1356
1357         return TRUE;
1358 }
1359
1360 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1361                                 void *user_data)
1362 {
1363         const char *path = dbus_message_get_path(message);
1364         struct modem_data *modem;
1365         DBusMessageIter iter, value;
1366         const char *key;
1367
1368         DBG("path %s", path);
1369
1370         modem = g_hash_table_lookup(modem_hash, path);
1371         if (modem == NULL)
1372                 return TRUE;
1373
1374         if (dbus_message_iter_init(message, &iter) == FALSE)
1375                 return TRUE;
1376
1377         dbus_message_iter_get_basic(&iter, &key);
1378
1379         dbus_message_iter_next(&iter);
1380         dbus_message_iter_recurse(&iter, &value);
1381
1382         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1383                 char *imsi;
1384
1385                 dbus_message_iter_get_basic(&value, &imsi);
1386
1387                 add_device(path, imsi);
1388         } else if (g_str_equal(key, "Present") == TRUE) {
1389                 dbus_bool_t present;
1390
1391                 dbus_message_iter_get_basic(&value, &present);
1392
1393                 if (present)
1394                         return TRUE;
1395
1396                 if (modem->device != NULL)
1397                         modem_remove_device(modem);
1398
1399                 modem->has_gprs = FALSE;
1400                 modem->has_reg = FALSE;
1401         }
1402
1403         return TRUE;
1404 }
1405
1406 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1407                                 void *user_data)
1408 {
1409         const char *path = dbus_message_get_path(message);
1410         struct modem_data *modem;
1411         DBusMessageIter iter;
1412
1413         DBG("path %s", path);
1414
1415         modem = g_hash_table_lookup(modem_hash, path);
1416         if (modem == NULL)
1417                 return TRUE;
1418
1419         if (dbus_message_iter_init(message, &iter))
1420                 modem_gprs_changed(modem, &iter);
1421
1422         return TRUE;
1423 }
1424
1425 static gboolean modem_added(DBusConnection *connection,
1426                                 DBusMessage *message, void *user_data)
1427 {
1428         DBusMessageIter iter, properties;
1429         const char *modem_path;
1430
1431         DBG("");
1432
1433         if (dbus_message_iter_init(message, &iter) == FALSE)
1434                 return TRUE;
1435
1436         dbus_message_iter_get_basic(&iter, &modem_path);
1437
1438         dbus_message_iter_next(&iter);
1439         dbus_message_iter_recurse(&iter, &properties);
1440
1441         add_modem(modem_path, &properties);
1442
1443         return TRUE;
1444 }
1445
1446 static gboolean modem_removed(DBusConnection *connection,
1447                                 DBusMessage *message, void *user_data)
1448 {
1449         DBusMessageIter iter;
1450         const char *modem_path;
1451
1452         DBG("");
1453
1454         if (dbus_message_iter_init(message, &iter) == FALSE)
1455                 return TRUE;
1456
1457         dbus_message_iter_get_basic(&iter, &modem_path);
1458
1459         g_hash_table_remove(modem_hash, modem_path);
1460
1461         return TRUE;
1462 }
1463
1464
1465 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1466 {
1467         DBusMessageIter entry;
1468         gchar *nameserver = NULL, *nameserver_old = NULL;
1469
1470         DBG("");
1471
1472         dbus_message_iter_recurse(array, &entry);
1473
1474         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1475                 const char *dns;
1476
1477                 dbus_message_iter_get_basic(&entry, &dns);
1478
1479                 DBG("dns %s", dns);
1480
1481                 if (nameserver == NULL) {
1482
1483                         nameserver = g_strdup(dns);
1484                 } else {
1485
1486                         nameserver_old = nameserver;
1487                         nameserver = g_strdup_printf("%s %s",
1488                                                 nameserver_old, dns);
1489                         g_free(nameserver_old);
1490                 }
1491
1492                 dbus_message_iter_next(&entry);
1493         }
1494
1495         parent->ipv4.nameserver = nameserver;
1496 }
1497
1498 static void update_settings(DBusMessageIter *array,
1499                                 struct connman_network *network)
1500 {
1501         struct connman_element *parent = connman_network_get_element(network);
1502         DBusMessageIter dict;
1503         const char *interface = NULL;
1504
1505         DBG("network %p", network);
1506
1507         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1508                 return;
1509
1510         dbus_message_iter_recurse(array, &dict);
1511
1512         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1513                 DBusMessageIter entry, value;
1514                 const char *key;
1515
1516                 dbus_message_iter_recurse(&dict, &entry);
1517                 dbus_message_iter_get_basic(&entry, &key);
1518
1519                 DBG("key %s", key);
1520
1521                 dbus_message_iter_next(&entry);
1522                 dbus_message_iter_recurse(&entry, &value);
1523
1524                 if (g_str_equal(key, "Interface") == TRUE) {
1525                         int index;
1526
1527                         dbus_message_iter_get_basic(&value, &interface);
1528
1529                         DBG("interface %s", interface);
1530
1531                         index = connman_inet_ifindex(interface);
1532                         if (index >= 0) {
1533                                 connman_network_set_index(network, index);
1534                         } else {
1535                                 connman_error("Can not find interface %s",
1536                                                                 interface);
1537                                 break;
1538                         }
1539                 } else if (g_str_equal(key, "Method") == TRUE) {
1540                         const char *method;
1541
1542                         dbus_message_iter_get_basic(&value, &method);
1543                         if (g_strcmp0(method, "static") == 0) {
1544
1545                                 parent->ipv4.method =
1546                                         CONNMAN_IPCONFIG_METHOD_FIXED;
1547                         } else if (g_strcmp0(method, "dhcp") == 0) {
1548
1549                                 parent->ipv4.method =
1550                                         CONNMAN_IPCONFIG_METHOD_DHCP;
1551                                 break;
1552                         }
1553                 } else if (g_str_equal(key, "Address") == TRUE) {
1554                         const char *address;
1555
1556                         dbus_message_iter_get_basic(&value, &address);
1557
1558                         DBG("address %s", address);
1559
1560                         parent->ipv4.address = g_strdup(address);
1561                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1562                         const char *netmask;
1563
1564                         dbus_message_iter_get_basic(&value, &netmask);
1565
1566                         DBG("netmask %s", netmask);
1567
1568                         parent->ipv4.netmask = g_strdup(netmask);
1569                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1570
1571                         get_dns(&value, parent);
1572                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1573                         const char *gateway;
1574
1575                         dbus_message_iter_get_basic(&value, &gateway);
1576
1577                         DBG("gateway %s", gateway);
1578
1579                         parent->ipv4.gateway = g_strdup(gateway);
1580                 }
1581
1582                 dbus_message_iter_next(&dict);
1583         }
1584
1585         /* deactive, oFono send NULL inteface before deactive signal */
1586         if (interface == NULL)
1587                 connman_network_set_index(network, -1);
1588 }
1589
1590 static void cleanup_ipconfig(struct connman_network *network)
1591 {
1592         struct connman_element *parent = connman_network_get_element(network);
1593
1594         g_free(parent->ipv4.address);
1595         parent->ipv4.address = NULL;
1596
1597         g_free(parent->ipv4.netmask);
1598         parent->ipv4.netmask = NULL;
1599
1600         g_free(parent->ipv4.nameserver);
1601         parent->ipv4.nameserver = NULL;
1602
1603         g_free(parent->ipv4.gateway);
1604         parent->ipv4.gateway = NULL;
1605
1606         parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1607 }
1608
1609
1610 static void set_connected(struct connman_network *network,
1611                                 connman_bool_t connected)
1612 {
1613         struct connman_element *parent = connman_network_get_element(network);
1614         enum connman_ipconfig_method method = parent->ipv4.method;
1615
1616         switch (method) {
1617         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1618         case CONNMAN_IPCONFIG_METHOD_OFF:
1619         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1620         case CONNMAN_IPCONFIG_METHOD_AUTO:
1621                 return;
1622
1623         case CONNMAN_IPCONFIG_METHOD_FIXED:
1624                 connman_network_set_ipv4_method(network, method);
1625
1626                 connman_network_set_connected(network, connected);
1627
1628                 if (connected == FALSE)
1629                         cleanup_ipconfig(network);
1630
1631                 break;
1632
1633         case CONNMAN_IPCONFIG_METHOD_DHCP:
1634                 connman_network_set_ipv4_method(network, method);
1635
1636                 connman_network_set_connected(network, connected);
1637                 break;
1638         }
1639 }
1640
1641 static gboolean context_changed(DBusConnection *connection,
1642                                         DBusMessage *message, void *user_data)
1643 {
1644         const char *path = dbus_message_get_path(message);
1645         struct connman_network *network;
1646         DBusMessageIter iter, value;
1647         const char *key;
1648
1649         DBG("path %s", path);
1650
1651         network = g_hash_table_lookup(network_hash, path);
1652         if (network == NULL)
1653                 return TRUE;
1654
1655         if (!pending_network_is_available(network)) {
1656                 remove_network(network);
1657                 return TRUE;
1658         }
1659
1660         if (dbus_message_iter_init(message, &iter) == FALSE)
1661                 return TRUE;
1662
1663         dbus_message_iter_get_basic(&iter, &key);
1664
1665         dbus_message_iter_next(&iter);
1666         dbus_message_iter_recurse(&iter, &value);
1667
1668         if (g_str_equal(key, "Settings") == TRUE)
1669                 update_settings(&value, network);
1670         else if (g_str_equal(key, "Active") == TRUE) {
1671                 dbus_bool_t active;
1672
1673                 dbus_message_iter_get_basic(&value, &active);
1674
1675                 set_connected(network, active);
1676         }
1677
1678         return TRUE;
1679 }
1680
1681 static guint watch;
1682 static guint reg_watch;
1683 static guint sim_watch;
1684 static guint gprs_watch;
1685 static guint modem_watch;
1686 static guint modem_added_watch;
1687 static guint modem_removed_watch;
1688 static guint context_watch;
1689
1690 static int ofono_init(void)
1691 {
1692         int err;
1693
1694         connection = connman_dbus_get_connection();
1695         if (connection == NULL)
1696                 return -EIO;
1697
1698         watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1699                         ofono_connect, ofono_disconnect, NULL, NULL);
1700
1701         reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1702                                                 OFONO_REGISTRATION_INTERFACE,
1703                                                 PROPERTY_CHANGED,
1704                                                 reg_changed,
1705                                                 NULL, NULL);
1706
1707         gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1708                                                 OFONO_GPRS_INTERFACE,
1709                                                 PROPERTY_CHANGED,
1710                                                 gprs_changed,
1711                                                 NULL, NULL);
1712
1713         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1714                                                 OFONO_MODEM_INTERFACE,
1715                                                 PROPERTY_CHANGED,
1716                                                 modem_changed,
1717                                                 NULL, NULL);
1718
1719         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1720                                                 OFONO_SIM_INTERFACE,
1721                                                 PROPERTY_CHANGED,
1722                                                 sim_changed,
1723                                                 NULL, NULL);
1724
1725         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1726                                                 OFONO_MANAGER_INTERFACE,
1727                                                 MODEM_ADDED,
1728                                                 modem_added,
1729                                                 NULL, NULL);
1730
1731         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1732                                                 OFONO_MANAGER_INTERFACE,
1733                                                 MODEM_REMOVED,
1734                                                 modem_removed,
1735                                                 NULL, NULL);
1736
1737         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1738                                                 OFONO_CONTEXT_INTERFACE,
1739                                                 PROPERTY_CHANGED,
1740                                                 context_changed,
1741                                                 NULL, NULL);
1742
1743         if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1744                         reg_watch == 0 || sim_watch == 0 ||
1745                         modem_added_watch == 0 || modem_removed_watch == 0 ||
1746                                 context_watch == 0) {
1747                 err = -EIO;
1748                 goto remove;
1749         }
1750
1751         err = connman_network_driver_register(&network_driver);
1752         if (err < 0)
1753                 goto remove;
1754
1755         err = connman_device_driver_register(&modem_driver);
1756         if (err < 0) {
1757                 connman_network_driver_unregister(&network_driver);
1758                 goto remove;
1759         }
1760
1761         return 0;
1762
1763 remove:
1764         g_dbus_remove_watch(connection, watch);
1765         g_dbus_remove_watch(connection, sim_watch);
1766         g_dbus_remove_watch(connection, reg_watch);
1767         g_dbus_remove_watch(connection, gprs_watch);
1768         g_dbus_remove_watch(connection, modem_watch);
1769         g_dbus_remove_watch(connection, modem_added_watch);
1770         g_dbus_remove_watch(connection, modem_removed_watch);
1771         g_dbus_remove_watch(connection, context_watch);
1772
1773         dbus_connection_unref(connection);
1774
1775         return err;
1776 }
1777
1778 static void ofono_exit(void)
1779 {
1780         g_dbus_remove_watch(connection, watch);
1781         g_dbus_remove_watch(connection, sim_watch);
1782         g_dbus_remove_watch(connection, reg_watch);
1783         g_dbus_remove_watch(connection, gprs_watch);
1784         g_dbus_remove_watch(connection, modem_watch);
1785         g_dbus_remove_watch(connection, modem_added_watch);
1786         g_dbus_remove_watch(connection, modem_removed_watch);
1787         g_dbus_remove_watch(connection, context_watch);
1788
1789         ofono_disconnect(connection, NULL);
1790
1791         connman_device_driver_unregister(&modem_driver);
1792         connman_network_driver_unregister(&network_driver);
1793
1794         dbus_connection_unref(connection);
1795 }
1796
1797 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1798                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)