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