nmcompat: D-Bus configuration file
[framework/connectivity/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), NULL);
448
449         if (active == FALSE && error == -EINPROGRESS)
450                 error = 0;
451
452         return error;
453 }
454
455 static void set_apn(struct connman_network *network)
456 {
457         const char *apn, *path;
458
459         apn = connman_network_get_string(network, "Cellular.APN");
460         if (apn == NULL)
461                 return;
462
463         path = connman_network_get_string(network, "Path");
464         if (path == NULL)
465                 return;
466
467         DBG("path %s, apn %s", path, apn);
468
469         set_property(path, OFONO_CONTEXT_INTERFACE,
470                         "AccessPointName", DBUS_TYPE_STRING, &apn,
471                         NULL, NULL, NULL);
472 }
473
474 static int network_connect(struct connman_network *network)
475 {
476         struct connman_device *device;
477         struct modem_data *modem;
478
479         DBG("network %p", network);
480
481         if (connman_network_get_index(network) >= 0)
482                 return -EISCONN;
483
484         device = connman_network_get_device(network);
485         if (device == NULL)
486                 return -ENODEV;
487
488         modem = connman_device_get_data(device);
489         if (modem == NULL)
490                 return -ENODEV;
491
492         if (modem->registered == FALSE)
493                 return -ENOLINK;
494
495         if (modem->powered == FALSE)
496                 return -ENOLINK;
497
498         if (modem->roaming_allowed == FALSE && modem->roaming == TRUE)
499                 return -ENOLINK;
500
501         return set_network_active(network, TRUE);
502 }
503
504 static int network_disconnect(struct connman_network *network)
505 {
506         DBG("network %p", network);
507
508         if (connman_network_get_index(network) < 0)
509                 return -ENOTCONN;
510
511         return set_network_active(network, FALSE);
512 }
513
514 static void network_remove(struct connman_network *network)
515 {
516         char const *path = connman_network_get_string(network, "Path");
517
518         DBG("network %p path %s", network, path);
519
520         g_hash_table_remove(network_hash, path);
521 }
522
523 static int network_setup(struct connman_network *network, const char *key)
524 {
525         if (g_strcmp0(key, "Cellular.APN") == 0)
526                 set_apn(network);
527
528         return 0;
529 }
530
531 static struct connman_network_driver network_driver = {
532         .name           = "network",
533         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
534         .probe          = network_probe,
535         .remove         = network_remove,
536         .connect        = network_connect,
537         .disconnect     = network_disconnect,
538         .setup          = network_setup,
539 };
540
541 static void update_settings(DBusMessageIter *array,
542                                 struct connman_network *network);
543
544 static void set_connected(struct connman_network *network,
545                                 connman_bool_t connected);
546
547 static int add_network(struct connman_device *device,
548                         const char *path, DBusMessageIter *dict)
549 {
550         struct modem_data *modem = connman_device_get_data(device);
551         struct connman_network *network;
552         char *ident;
553         const char *hash_path;
554         char const *operator;
555         dbus_bool_t active = FALSE;
556
557         DBG("modem %p device %p path %s", modem, device, path);
558
559         ident = get_ident(path);
560
561         network = connman_device_get_network(device, ident);
562         if (network != NULL)
563                 return -EALREADY;
564
565         network = g_hash_table_lookup(network_hash, path);
566         if (network != NULL) {
567                 DBG("path %p already exists with device %p", path,
568                         connman_network_get_device(network));
569                 if (connman_network_get_device(network))
570                         return -EALREADY;
571                 g_hash_table_remove(network_hash, path);
572         }
573
574         network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
575         if (network == NULL)
576                 return -ENOMEM;
577
578         connman_network_set_string(network, "Path", path);
579         hash_path = connman_network_get_string(network, "Path");
580         if (hash_path == NULL)
581                 goto error;
582
583         create_service(network);
584
585         connman_network_ref(network);
586         g_hash_table_insert(network_hash, (char *)hash_path, network);
587
588         connman_network_set_available(network, TRUE);
589         connman_network_set_index(network, -1);
590
591         operator = connman_device_get_string(device, "Operator");
592         if (operator)
593                 connman_network_set_name(network, operator);
594
595         if (modem->has_strength)
596                 connman_network_set_strength(network, modem->strength);
597
598         connman_network_set_roaming(network, modem->roaming);
599
600         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
601                 DBusMessageIter entry, value;
602                 const char *key;
603
604                 dbus_message_iter_recurse(dict, &entry);
605                 dbus_message_iter_get_basic(&entry, &key);
606
607                 dbus_message_iter_next(&entry);
608                 dbus_message_iter_recurse(&entry, &value);
609
610                 if (g_str_equal(key, "Type")) {
611                         const char *type;
612
613                         dbus_message_iter_get_basic(&value, &type);
614                         if (g_strcmp0(type, "internet") != 0) {
615                                 DBG("path %p type %s", path, type);
616                                 goto error;
617                         }
618                 } else if (g_str_equal(key, "AccessPointName")) {
619                         const char *ap;
620
621                         dbus_message_iter_get_basic(&value, &ap);
622
623                         connman_network_set_string(network, "Cellular.APN", ap);
624                 } else if (g_str_equal(key, "Settings"))
625                         update_settings(&value, network);
626                 else if (g_str_equal(key, "Active") == TRUE)
627                         dbus_message_iter_get_basic(&value, &active);
628
629                 dbus_message_iter_next(dict);
630         }
631
632         if (connman_device_add_network(device, network) != 0)
633                 goto error;
634
635         if (active)
636                 set_connected(network, active);
637
638         return 0;
639
640 error:
641         connman_network_unref(network);
642         g_hash_table_remove(network_hash, hash_path);
643         return -EIO;
644 }
645
646 static void check_networks_reply(DBusPendingCall *call, void *user_data)
647 {
648         char *path = user_data;
649         struct modem_data *modem;
650         DBusMessage *reply;
651         DBusMessageIter array, entry, value, properties;
652
653         DBG("path %s", path);
654
655         modem = g_hash_table_lookup(modem_hash, path);
656         if (modem == NULL)
657                 return;
658         if (modem->device == NULL)
659                 return;
660
661         reply = dbus_pending_call_steal_reply(call);
662
663         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
664                 goto done;
665
666         dbus_message_iter_init(reply, &array);
667
668         dbus_message_iter_recurse(&array, &entry);
669
670         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
671                 char const *network_path;
672
673                 dbus_message_iter_recurse(&entry, &value);
674                 dbus_message_iter_get_basic(&value, &network_path);
675
676                 dbus_message_iter_next(&value);
677                 dbus_message_iter_recurse(&value, &properties);
678
679                 add_network(modem->device, network_path, &properties);
680
681                 dbus_message_iter_next(&entry);
682         }
683
684 done:
685         dbus_message_unref(reply);
686
687         dbus_pending_call_unref(call);
688 }
689
690 static void check_networks(struct modem_data *modem)
691 {
692         char const *path = modem->path;
693
694         DBG("modem %p path %s", modem, path);
695
696         call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
697                         check_networks_reply, g_strdup(path), g_free,
698                         DBUS_TYPE_INVALID);
699 }
700
701 static void modem_clear_network_errors(struct modem_data *modem)
702 {
703         struct connman_device *device = modem->device;
704         GHashTableIter i;
705         gpointer value;
706
707         if (device == NULL)
708                 return;
709
710         g_hash_table_iter_init(&i, network_hash);
711
712         while (g_hash_table_iter_next(&i, NULL, &value)) {
713                 struct connman_network *network = value;
714
715                 if (connman_network_get_device(network) == device)
716                         connman_network_clear_error(network);
717         }
718 }
719
720 static void modem_operator_name_changed(struct modem_data *modem,
721                                         char const *name)
722 {
723         struct connman_device *device = modem->device;
724         GHashTableIter i;
725         gpointer value;
726
727         if (device == NULL)
728                 return;
729
730         connman_device_set_string(device, "Operator", name);
731
732         for (g_hash_table_iter_init(&i, network_hash);
733              g_hash_table_iter_next(&i, NULL, &value);) {
734                 struct connman_network *network = value;
735
736                 if (connman_network_get_device(network) == device) {
737                         connman_network_set_name(network, name);
738                         connman_network_update(network);
739                 }
740         }
741 }
742
743 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
744 {
745         struct connman_device *device = modem->device;
746         GHashTableIter i;
747         gpointer value;
748
749         modem->strength = strength;
750         modem->has_strength = TRUE;
751
752         if (device == NULL)
753                 return;
754
755         for (g_hash_table_iter_init(&i, network_hash);
756              g_hash_table_iter_next(&i, NULL, &value);) {
757                 struct connman_network *network = value;
758
759                 if (connman_network_get_device(network) == device) {
760                         connman_network_set_strength(network, strength);
761                         connman_network_update(network);
762                 }
763         }
764 }
765
766 static void modem_roaming_changed(struct modem_data *modem,
767                                         char const *status)
768 {
769         struct connman_device *device = modem->device;
770         connman_bool_t roaming = FALSE;
771         connman_bool_t registered = FALSE;
772         connman_bool_t was_roaming = modem->roaming;
773         GHashTableIter i;
774         gpointer value;
775
776         if (g_str_equal(status, "roaming"))
777                 roaming = TRUE;
778         else if (g_str_equal(status, "registered"))
779                 registered = TRUE;
780
781         registered = registered || roaming;
782
783         if (modem->roaming == roaming && modem->registered == registered)
784                 return;
785
786         modem->registered = registered;
787         modem->roaming = roaming;
788
789         if (roaming == was_roaming)
790                 return;
791
792         if (device == NULL)
793                 return;
794
795         g_hash_table_iter_init(&i, network_hash);
796
797         while (g_hash_table_iter_next(&i, NULL, &value)) {
798                 struct connman_network *network = value;
799
800                 if (connman_network_get_device(network) == device) {
801                         connman_network_set_roaming(network, roaming);
802                         connman_network_update(network);
803                 }
804         }
805 }
806
807 static void modem_registration_removed(struct modem_data *modem)
808 {
809         modem->registered = FALSE;
810         modem->roaming = FALSE;
811 }
812
813 static void modem_registration_changed(struct modem_data *modem,
814                                         DBusMessageIter *entry)
815 {
816         DBusMessageIter iter;
817         const char *key;
818         int type;
819         connman_uint8_t strength;
820         char const *name, *status, *mcc_s;
821
822         dbus_message_iter_get_basic(entry, &key);
823
824         DBG("key %s", key);
825
826         dbus_message_iter_next(entry);
827
828         dbus_message_iter_recurse(entry, &iter);
829
830         type = dbus_message_iter_get_arg_type(&iter);
831         if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
832                 return;
833
834         if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
835                 dbus_message_iter_get_basic(&iter, &name);
836                 modem_operator_name_changed(modem, name);
837         } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
838                 dbus_message_iter_get_basic(&iter, &strength);
839                 modem_strength_changed(modem, strength);
840         } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
841                 dbus_message_iter_get_basic(&iter, &status);
842                 modem_roaming_changed(modem, status);
843         } else if (g_str_equal(key, "MobileCountryCode") &&
844                                         type == DBUS_TYPE_STRING) {
845                 int mcc;
846                 char *alpha2;
847
848                 dbus_message_iter_get_basic(&iter, &mcc_s);
849
850                 mcc = atoi(mcc_s);
851                 if (mcc > 799)
852                         return;
853
854                 alpha2 = mcc_country_codes[mcc - 200];
855                 connman_technology_set_regdom(alpha2);
856         }
857
858 }
859
860 static gboolean reg_changed(DBusConnection *connection,
861                                 DBusMessage *message, void *user_data)
862 {
863         const char *path = dbus_message_get_path(message);
864         struct modem_data *modem;
865         DBusMessageIter iter;
866
867         DBG("path %s", path);
868
869         modem = g_hash_table_lookup(modem_hash, path);
870         if (modem == NULL)
871                 return TRUE;
872
873         if (dbus_message_iter_init(message, &iter))
874                 modem_registration_changed(modem, &iter);
875
876         return TRUE;
877 }
878
879 static void check_registration_reply(DBusPendingCall *call, void *user_data)
880 {
881         char const *path = user_data;
882         struct modem_data *modem;
883         DBusMessage *reply;
884         DBusMessageIter array, dict, entry;
885
886         DBG("path %s", path);
887
888         modem = g_hash_table_lookup(modem_hash, path);
889         if (modem == NULL)
890                 return;
891
892         reply = dbus_pending_call_steal_reply(call);
893
894         if (dbus_message_iter_init(reply, &array) == FALSE)
895                 goto done;
896
897         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
898                 goto done;
899
900         dbus_message_iter_recurse(&array, &dict);
901         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
902                 dbus_message_iter_recurse(&dict, &entry);
903                 modem_registration_changed(modem, &entry);
904                 dbus_message_iter_next(&dict);
905         }
906
907 done:
908         dbus_message_unref(reply);
909
910         dbus_pending_call_unref(call);
911 }
912
913 static void check_registration(struct modem_data *modem)
914 {
915         char const *path = modem->path;
916
917         DBG("modem %p path %s", modem, path);
918
919         call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
920                         check_registration_reply, g_strdup(path), g_free,
921                         DBUS_TYPE_INVALID);
922 }
923
924 static void modem_gprs_changed(struct modem_data *modem,
925                                         DBusMessageIter *entry)
926 {
927         DBusMessageIter iter;
928         const char *key;
929         int type;
930         dbus_bool_t value;
931
932         dbus_message_iter_get_basic(entry, &key);
933
934         DBG("key %s", key);
935
936         dbus_message_iter_next(entry);
937
938         dbus_message_iter_recurse(entry, &iter);
939
940         type = dbus_message_iter_get_arg_type(&iter);
941
942         if (type != DBUS_TYPE_BOOLEAN)
943                 return;
944
945         dbus_message_iter_get_basic(&iter, &value);
946
947         if (g_str_equal(key, "Attached") == TRUE) {
948                 DBG("Attached %d", value);
949
950                 modem->attached = value;
951
952                 if (value)
953                         modem_clear_network_errors(modem);
954         } else if (g_str_equal(key, "Powered") == TRUE) {
955                 DBG("Powered %d", value);
956
957                 modem->powered = value;
958         } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
959                 DBG("RoamingAllowed %d", value);
960
961                 modem->roaming_allowed = value;
962         }
963 }
964
965 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
966 {
967         char const *path = user_data;
968         struct modem_data *modem;
969         DBusMessage *reply;
970         DBusMessageIter array, dict, entry;
971
972         DBG("path %s", path);
973
974         modem = g_hash_table_lookup(modem_hash, path);
975         if (modem == NULL)
976                 return;
977
978         reply = dbus_pending_call_steal_reply(call);
979
980         if (dbus_message_iter_init(reply, &array) == FALSE)
981                 goto done;
982
983         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
984                 goto done;
985
986         dbus_message_iter_recurse(&array, &dict);
987         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
988                 dbus_message_iter_recurse(&dict, &entry);
989                 modem_gprs_changed(modem, &entry);
990                 dbus_message_iter_next(&dict);
991         }
992
993 done:
994         dbus_message_unref(reply);
995
996         dbus_pending_call_unref(call);
997
998         check_networks(modem);
999 }
1000
1001 static void check_gprs(struct modem_data *modem)
1002 {
1003         char const *path = modem->path;
1004
1005         DBG("modem %p path %s", modem, path);
1006
1007         call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
1008                         check_gprs_reply, g_strdup(path), g_free,
1009                         DBUS_TYPE_INVALID);
1010 }
1011
1012 static void add_device(const char *path, const char *imsi)
1013 {
1014         struct modem_data *modem;
1015         struct connman_device *device;
1016
1017         DBG("path %s imsi %s", path, imsi);
1018
1019         if (path == NULL)
1020                 return;
1021
1022         if (imsi == NULL)
1023                 return;
1024
1025         modem = g_hash_table_lookup(modem_hash, path);
1026         if (modem == NULL)
1027                 return;
1028
1029         if (modem->device) {
1030                 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1031                         return;
1032
1033                 modem_remove_device(modem);
1034         }
1035
1036         if (strlen(imsi) == 0)
1037                 return;
1038
1039         device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1040         if (device == NULL)
1041                 return;
1042
1043         connman_device_set_ident(device, imsi);
1044
1045         connman_device_set_string(device, "Path", path);
1046
1047         connman_device_set_data(device, modem);
1048
1049         if (connman_device_register(device) < 0) {
1050                 connman_device_unref(device);
1051                 return;
1052         }
1053
1054         modem->device = device;
1055
1056         if (modem->has_reg)
1057                 check_registration(modem);
1058
1059         if (modem->has_gprs)
1060                 check_gprs(modem);
1061 }
1062
1063 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1064 {
1065         const char *path = user_data;
1066         const char *imsi = NULL;
1067         DBusMessage *reply;
1068         DBusMessageIter array, dict;
1069
1070         DBG("path %s", path);
1071
1072         reply = dbus_pending_call_steal_reply(call);
1073
1074         if (dbus_message_iter_init(reply, &array) == FALSE)
1075                 goto done;
1076
1077         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1078                 goto done;
1079
1080         dbus_message_iter_recurse(&array, &dict);
1081
1082         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1083                 DBusMessageIter entry, value;
1084                 const char *key;
1085
1086                 dbus_message_iter_recurse(&dict, &entry);
1087                 dbus_message_iter_get_basic(&entry, &key);
1088
1089                 dbus_message_iter_next(&entry);
1090                 dbus_message_iter_recurse(&entry, &value);
1091
1092                 if (g_str_equal(key, "SubscriberIdentity")) {
1093                         dbus_message_iter_get_basic(&value, &imsi);
1094                         add_device(path, imsi);
1095                 }
1096
1097                 dbus_message_iter_next(&dict);
1098         }
1099
1100 done:
1101         dbus_message_unref(reply);
1102
1103         dbus_pending_call_unref(call);
1104 }
1105
1106 static void get_imsi(const char *path)
1107 {
1108         DBG("path %s", path);
1109
1110         call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1111                         sim_properties_reply, g_strdup(path), g_free,
1112                         DBUS_TYPE_INVALID);
1113 }
1114
1115 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1116 {
1117         DBG("path %s powered %d", path, powered);
1118
1119         return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1120                                 DBUS_TYPE_BOOLEAN, &powered,
1121                                 NULL, NULL, NULL);
1122 }
1123
1124 static int modem_change_powered(const char *path, dbus_bool_t powered)
1125 {
1126         DBG("path %s powered %d", path, powered);
1127
1128         return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1129                                 DBUS_TYPE_BOOLEAN, &powered,
1130                                 NULL, NULL, NULL);
1131 }
1132
1133
1134 static gboolean modem_has_interface(DBusMessageIter *array,
1135                                         char const *interface)
1136 {
1137         DBusMessageIter entry;
1138
1139         dbus_message_iter_recurse(array, &entry);
1140
1141         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1142                 const char *element;
1143
1144                 dbus_message_iter_get_basic(&entry, &element);
1145
1146                 if (g_strcmp0(interface, element) == 0)
1147                         return TRUE;
1148
1149                 dbus_message_iter_next(&entry);
1150         }
1151
1152         return FALSE;
1153 }
1154
1155 static gboolean modem_has_sim(DBusMessageIter *array)
1156 {
1157         return modem_has_interface(array, OFONO_SIM_INTERFACE);
1158 }
1159
1160 static gboolean modem_has_reg(DBusMessageIter *array)
1161 {
1162         return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1163 }
1164
1165 static gboolean modem_has_gprs(DBusMessageIter *array)
1166 {
1167         return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1168 }
1169
1170 static void add_modem(const char *path, DBusMessageIter *prop)
1171 {
1172         struct modem_data *modem;
1173         dbus_bool_t powered = FALSE;
1174         dbus_bool_t online = FALSE;
1175         dbus_bool_t has_online = FALSE;
1176         dbus_bool_t locked = FALSE;
1177         gboolean has_sim = FALSE;
1178         gboolean has_reg = FALSE;
1179         gboolean has_gprs = FALSE;
1180
1181         modem = g_hash_table_lookup(modem_hash, path);
1182
1183         if (modem != NULL)
1184                 return;
1185
1186         modem = g_try_new0(struct modem_data, 1);
1187         if (modem == NULL)
1188                 return;
1189
1190         modem->path = g_strdup(path);
1191         modem->device = NULL;
1192         modem->available = TRUE;
1193
1194         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1195
1196         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1197                 DBusMessageIter entry, value;
1198                 const char *key;
1199
1200                 dbus_message_iter_recurse(prop, &entry);
1201                 dbus_message_iter_get_basic(&entry, &key);
1202
1203                 dbus_message_iter_next(&entry);
1204                 dbus_message_iter_recurse(&entry, &value);
1205
1206                 if (g_str_equal(key, "Powered") == TRUE)
1207                         dbus_message_iter_get_basic(&value, &powered);
1208                 else if (g_str_equal(key, "Lockdown") == TRUE)
1209                         dbus_message_iter_get_basic(&value, &locked);
1210                 else if (g_str_equal(key, "Online") == TRUE) {
1211                         has_online = TRUE;
1212                         dbus_message_iter_get_basic(&value, &online);
1213                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1214                         has_sim = modem_has_sim(&value);
1215                         has_reg = modem_has_reg(&value);
1216                         has_gprs = modem_has_gprs(&value);
1217                 }
1218
1219                 dbus_message_iter_next(prop);
1220         }
1221
1222         if (locked)
1223                 return;
1224
1225         if (!powered)
1226                 modem_change_powered(path, TRUE);
1227
1228         modem->has_sim = has_sim;
1229         modem->has_reg = has_reg;
1230         modem->has_gprs = has_gprs;
1231
1232         update_modem_online(modem, online);
1233
1234         if (has_sim)
1235                 get_imsi(path);
1236 }
1237
1238 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1239 {
1240         DBusMessage *reply;
1241         DBusError error;
1242         DBusMessageIter array, dict;
1243
1244         DBG("");
1245
1246         reply = dbus_pending_call_steal_reply(call);
1247
1248         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1249                 goto done;
1250
1251         dbus_error_init(&error);
1252
1253         if (dbus_set_error_from_message(&error, reply)) {
1254                 connman_error("ModemManager.GetModems() %s %s",
1255                                 error.name, error.message);
1256                 dbus_error_free(&error);
1257                 goto done;
1258         }
1259
1260         if (dbus_message_iter_init(reply, &array) == FALSE)
1261                 goto done;
1262
1263         dbus_message_iter_recurse(&array, &dict);
1264
1265         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1266                 DBusMessageIter value, properties;
1267                 const char *modem_path;
1268
1269                 dbus_message_iter_recurse(&dict, &value);
1270                 dbus_message_iter_get_basic(&value, &modem_path);
1271
1272                 dbus_message_iter_next(&value);
1273                 dbus_message_iter_recurse(&value, &properties);
1274
1275                 /* Add modem */
1276                 add_modem(modem_path, &properties);
1277
1278                 dbus_message_iter_next(&dict);
1279         }
1280
1281 done:
1282         dbus_message_unref(reply);
1283
1284         dbus_pending_call_unref(call);
1285 }
1286
1287 static void ofono_connect(DBusConnection *connection, void *user_data)
1288 {
1289         DBG("connection %p", connection);
1290
1291         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1292                                                 g_free, remove_modem);
1293
1294         network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1295                                                 NULL, remove_network);
1296
1297         call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1298                         manager_modems_reply, NULL, NULL,
1299                         DBUS_TYPE_INVALID);
1300 }
1301
1302 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1303 {
1304         DBG("connection %p", connection);
1305
1306         if (modem_hash == NULL)
1307                 return;
1308
1309         g_hash_table_destroy(modem_hash);
1310
1311         modem_hash = NULL;
1312 }
1313
1314 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1315                                 void *user_data)
1316 {
1317         const char *path = dbus_message_get_path(message);
1318         struct modem_data *modem;
1319         DBusMessageIter iter, value;
1320         const char *key;
1321
1322         DBG("path %s", path);
1323
1324         modem = g_hash_table_lookup(modem_hash, path);
1325         if (modem == NULL)
1326                 return TRUE;
1327
1328         if (dbus_message_iter_init(message, &iter) == FALSE)
1329                 return TRUE;
1330
1331         dbus_message_iter_get_basic(&iter, &key);
1332
1333         dbus_message_iter_next(&iter);
1334         dbus_message_iter_recurse(&iter, &value);
1335
1336         if (g_str_equal(key, "Powered") == TRUE) {
1337                 dbus_bool_t powered;
1338
1339                 dbus_message_iter_get_basic(&value, &powered);
1340                 if (powered == TRUE)
1341                         return TRUE;
1342
1343                 modem->has_sim = FALSE;
1344                 modem->has_reg = FALSE;
1345                 modem->has_gprs = FALSE;
1346
1347                 modem_remove_device(modem);
1348         } else if (g_str_equal(key, "Online") == TRUE) {
1349                 dbus_bool_t online;
1350
1351                 dbus_message_iter_get_basic(&value, &online);
1352
1353                 update_modem_online(modem, online);
1354         } else if (g_str_equal(key, "Lockdown") == TRUE) {
1355                 dbus_bool_t locked;
1356
1357                 dbus_message_iter_get_basic(&value, &locked);
1358
1359                 if (!locked)
1360                         modem_change_powered(path, TRUE);
1361
1362         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1363                 gboolean has_sim = modem_has_sim(&value);
1364                 gboolean has_reg = modem_has_reg(&value);
1365                 gboolean had_reg = modem->has_reg;
1366                 gboolean has_gprs = modem_has_gprs(&value);
1367                 gboolean had_gprs = modem->has_gprs;
1368
1369                 modem->has_sim = has_sim;
1370                 modem->has_reg = has_reg;
1371                 modem->has_gprs = has_gprs;
1372
1373                 if (modem->device == NULL) {
1374                         if (has_sim)
1375                                 get_imsi(modem->path);
1376                 } else if (!has_sim) {
1377                         modem_remove_device(modem);
1378                 } else {
1379                         if (has_reg && !had_reg)
1380                                 check_registration(modem);
1381                         else if (had_reg && !has_reg)
1382                                 modem_registration_removed(modem);
1383
1384                         if (has_gprs && !had_gprs) {
1385                                 gprs_change_powered(modem->path, TRUE);
1386                                 check_gprs(modem);
1387                         }
1388                 }
1389         }
1390
1391         return TRUE;
1392 }
1393
1394 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1395                                 void *user_data)
1396 {
1397         const char *path = dbus_message_get_path(message);
1398         struct modem_data *modem;
1399         DBusMessageIter iter, value;
1400         const char *key;
1401
1402         DBG("path %s", path);
1403
1404         modem = g_hash_table_lookup(modem_hash, path);
1405         if (modem == NULL)
1406                 return TRUE;
1407
1408         if (dbus_message_iter_init(message, &iter) == FALSE)
1409                 return TRUE;
1410
1411         dbus_message_iter_get_basic(&iter, &key);
1412
1413         dbus_message_iter_next(&iter);
1414         dbus_message_iter_recurse(&iter, &value);
1415
1416         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1417                 char *imsi;
1418
1419                 dbus_message_iter_get_basic(&value, &imsi);
1420
1421                 add_device(path, imsi);
1422         } else if (g_str_equal(key, "Present") == TRUE) {
1423                 dbus_bool_t present;
1424
1425                 dbus_message_iter_get_basic(&value, &present);
1426
1427                 if (present)
1428                         return TRUE;
1429
1430                 if (modem->device != NULL)
1431                         modem_remove_device(modem);
1432
1433                 modem->has_gprs = FALSE;
1434                 modem->has_reg = FALSE;
1435         }
1436
1437         return TRUE;
1438 }
1439
1440 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1441                                 void *user_data)
1442 {
1443         const char *path = dbus_message_get_path(message);
1444         struct modem_data *modem;
1445         DBusMessageIter iter;
1446
1447         DBG("path %s", path);
1448
1449         modem = g_hash_table_lookup(modem_hash, path);
1450         if (modem == NULL)
1451                 return TRUE;
1452
1453         if (dbus_message_iter_init(message, &iter))
1454                 modem_gprs_changed(modem, &iter);
1455
1456         return TRUE;
1457 }
1458
1459 static gboolean modem_added(DBusConnection *connection,
1460                                 DBusMessage *message, void *user_data)
1461 {
1462         DBusMessageIter iter, properties;
1463         const char *modem_path;
1464
1465         DBG("");
1466
1467         if (dbus_message_iter_init(message, &iter) == FALSE)
1468                 return TRUE;
1469
1470         dbus_message_iter_get_basic(&iter, &modem_path);
1471
1472         dbus_message_iter_next(&iter);
1473         dbus_message_iter_recurse(&iter, &properties);
1474
1475         add_modem(modem_path, &properties);
1476
1477         return TRUE;
1478 }
1479
1480 static gboolean modem_removed(DBusConnection *connection,
1481                                 DBusMessage *message, void *user_data)
1482 {
1483         DBusMessageIter iter;
1484         const char *modem_path;
1485
1486         DBG("");
1487
1488         if (dbus_message_iter_init(message, &iter) == FALSE)
1489                 return TRUE;
1490
1491         dbus_message_iter_get_basic(&iter, &modem_path);
1492
1493         g_hash_table_remove(modem_hash, modem_path);
1494
1495         return TRUE;
1496 }
1497
1498
1499 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1500 {
1501         DBusMessageIter entry;
1502         gchar *nameserver = NULL, *nameserver_old = NULL;
1503
1504         DBG("");
1505
1506         dbus_message_iter_recurse(array, &entry);
1507
1508         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1509                 const char *dns;
1510
1511                 dbus_message_iter_get_basic(&entry, &dns);
1512
1513                 DBG("dns %s", dns);
1514
1515                 if (nameserver == NULL) {
1516
1517                         nameserver = g_strdup(dns);
1518                 } else {
1519
1520                         nameserver_old = nameserver;
1521                         nameserver = g_strdup_printf("%s %s",
1522                                                 nameserver_old, dns);
1523                         g_free(nameserver_old);
1524                 }
1525
1526                 dbus_message_iter_next(&entry);
1527         }
1528
1529         parent->ipv4.nameserver = nameserver;
1530 }
1531
1532 static void update_settings(DBusMessageIter *array,
1533                                 struct connman_network *network)
1534 {
1535         struct connman_element *parent = connman_network_get_element(network);
1536         DBusMessageIter dict;
1537         const char *interface = NULL;
1538
1539         DBG("network %p", network);
1540
1541         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1542                 return;
1543
1544         dbus_message_iter_recurse(array, &dict);
1545
1546         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1547                 DBusMessageIter entry, value;
1548                 const char *key;
1549
1550                 dbus_message_iter_recurse(&dict, &entry);
1551                 dbus_message_iter_get_basic(&entry, &key);
1552
1553                 DBG("key %s", key);
1554
1555                 dbus_message_iter_next(&entry);
1556                 dbus_message_iter_recurse(&entry, &value);
1557
1558                 if (g_str_equal(key, "Interface") == TRUE) {
1559                         int index;
1560
1561                         dbus_message_iter_get_basic(&value, &interface);
1562
1563                         DBG("interface %s", interface);
1564
1565                         index = connman_inet_ifindex(interface);
1566                         if (index >= 0) {
1567                                 connman_network_set_index(network, index);
1568                         } else {
1569                                 connman_error("Can not find interface %s",
1570                                                                 interface);
1571                                 break;
1572                         }
1573                 } else if (g_str_equal(key, "Method") == TRUE) {
1574                         const char *method;
1575
1576                         dbus_message_iter_get_basic(&value, &method);
1577                         if (g_strcmp0(method, "static") == 0) {
1578
1579                                 parent->ipv4.method =
1580                                         CONNMAN_IPCONFIG_METHOD_FIXED;
1581                         } else if (g_strcmp0(method, "dhcp") == 0) {
1582
1583                                 parent->ipv4.method =
1584                                         CONNMAN_IPCONFIG_METHOD_DHCP;
1585                                 break;
1586                         }
1587                 } else if (g_str_equal(key, "Address") == TRUE) {
1588                         const char *address;
1589
1590                         dbus_message_iter_get_basic(&value, &address);
1591
1592                         DBG("address %s", address);
1593
1594                         parent->ipv4.address = g_strdup(address);
1595                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1596                         const char *netmask;
1597
1598                         dbus_message_iter_get_basic(&value, &netmask);
1599
1600                         DBG("netmask %s", netmask);
1601
1602                         parent->ipv4.netmask = g_strdup(netmask);
1603                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1604
1605                         get_dns(&value, parent);
1606                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1607                         const char *gateway;
1608
1609                         dbus_message_iter_get_basic(&value, &gateway);
1610
1611                         DBG("gateway %s", gateway);
1612
1613                         parent->ipv4.gateway = g_strdup(gateway);
1614                 }
1615
1616                 dbus_message_iter_next(&dict);
1617         }
1618
1619         /* deactive, oFono send NULL inteface before deactive signal */
1620         if (interface == NULL)
1621                 connman_network_set_index(network, -1);
1622 }
1623
1624 static void cleanup_ipconfig(struct connman_network *network)
1625 {
1626         struct connman_element *parent = connman_network_get_element(network);
1627
1628         g_free(parent->ipv4.address);
1629         parent->ipv4.address = NULL;
1630
1631         g_free(parent->ipv4.netmask);
1632         parent->ipv4.netmask = NULL;
1633
1634         g_free(parent->ipv4.nameserver);
1635         parent->ipv4.nameserver = NULL;
1636
1637         g_free(parent->ipv4.gateway);
1638         parent->ipv4.gateway = NULL;
1639
1640         parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1641 }
1642
1643
1644 static void set_connected(struct connman_network *network,
1645                                 connman_bool_t connected)
1646 {
1647         struct connman_element *parent = connman_network_get_element(network);
1648         enum connman_ipconfig_method method = parent->ipv4.method;
1649
1650         switch (method) {
1651         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1652         case CONNMAN_IPCONFIG_METHOD_OFF:
1653         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1654         case CONNMAN_IPCONFIG_METHOD_AUTO:
1655                 return;
1656
1657         case CONNMAN_IPCONFIG_METHOD_FIXED:
1658                 connman_network_set_ipv4_method(network, method);
1659
1660                 connman_network_set_connected(network, connected);
1661
1662                 if (connected == FALSE)
1663                         cleanup_ipconfig(network);
1664
1665                 break;
1666
1667         case CONNMAN_IPCONFIG_METHOD_DHCP:
1668                 connman_network_set_ipv4_method(network, method);
1669
1670                 connman_network_set_connected(network, connected);
1671                 break;
1672         }
1673 }
1674
1675 static gboolean context_changed(DBusConnection *connection,
1676                                         DBusMessage *message, void *user_data)
1677 {
1678         const char *path = dbus_message_get_path(message);
1679         struct connman_network *network;
1680         DBusMessageIter iter, value;
1681         const char *key;
1682
1683         DBG("path %s", path);
1684
1685         network = g_hash_table_lookup(network_hash, path);
1686         if (network == NULL)
1687                 return TRUE;
1688
1689         if (!pending_network_is_available(network)) {
1690                 remove_network(network);
1691                 return TRUE;
1692         }
1693
1694         if (dbus_message_iter_init(message, &iter) == FALSE)
1695                 return TRUE;
1696
1697         dbus_message_iter_get_basic(&iter, &key);
1698
1699         dbus_message_iter_next(&iter);
1700         dbus_message_iter_recurse(&iter, &value);
1701
1702         if (g_str_equal(key, "Settings") == TRUE)
1703                 update_settings(&value, network);
1704         else if (g_str_equal(key, "Active") == TRUE) {
1705                 dbus_bool_t active;
1706
1707                 dbus_message_iter_get_basic(&value, &active);
1708
1709                 set_connected(network, active);
1710         }
1711
1712         return TRUE;
1713 }
1714
1715 static guint watch;
1716 static guint reg_watch;
1717 static guint sim_watch;
1718 static guint gprs_watch;
1719 static guint modem_watch;
1720 static guint modem_added_watch;
1721 static guint modem_removed_watch;
1722 static guint context_watch;
1723
1724 static int ofono_init(void)
1725 {
1726         int err;
1727
1728         connection = connman_dbus_get_connection();
1729         if (connection == NULL)
1730                 return -EIO;
1731
1732         watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1733                         ofono_connect, ofono_disconnect, NULL, NULL);
1734
1735         reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1736                                                 OFONO_REGISTRATION_INTERFACE,
1737                                                 PROPERTY_CHANGED,
1738                                                 reg_changed,
1739                                                 NULL, NULL);
1740
1741         gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1742                                                 OFONO_GPRS_INTERFACE,
1743                                                 PROPERTY_CHANGED,
1744                                                 gprs_changed,
1745                                                 NULL, NULL);
1746
1747         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1748                                                 OFONO_MODEM_INTERFACE,
1749                                                 PROPERTY_CHANGED,
1750                                                 modem_changed,
1751                                                 NULL, NULL);
1752
1753         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1754                                                 OFONO_SIM_INTERFACE,
1755                                                 PROPERTY_CHANGED,
1756                                                 sim_changed,
1757                                                 NULL, NULL);
1758
1759         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1760                                                 OFONO_MANAGER_INTERFACE,
1761                                                 MODEM_ADDED,
1762                                                 modem_added,
1763                                                 NULL, NULL);
1764
1765         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1766                                                 OFONO_MANAGER_INTERFACE,
1767                                                 MODEM_REMOVED,
1768                                                 modem_removed,
1769                                                 NULL, NULL);
1770
1771         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1772                                                 OFONO_CONTEXT_INTERFACE,
1773                                                 PROPERTY_CHANGED,
1774                                                 context_changed,
1775                                                 NULL, NULL);
1776
1777         if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1778                         reg_watch == 0 || sim_watch == 0 ||
1779                         modem_added_watch == 0 || modem_removed_watch == 0 ||
1780                                 context_watch == 0) {
1781                 err = -EIO;
1782                 goto remove;
1783         }
1784
1785         err = connman_network_driver_register(&network_driver);
1786         if (err < 0)
1787                 goto remove;
1788
1789         err = connman_device_driver_register(&modem_driver);
1790         if (err < 0) {
1791                 connman_network_driver_unregister(&network_driver);
1792                 goto remove;
1793         }
1794
1795         return 0;
1796
1797 remove:
1798         g_dbus_remove_watch(connection, watch);
1799         g_dbus_remove_watch(connection, sim_watch);
1800         g_dbus_remove_watch(connection, reg_watch);
1801         g_dbus_remove_watch(connection, gprs_watch);
1802         g_dbus_remove_watch(connection, modem_watch);
1803         g_dbus_remove_watch(connection, modem_added_watch);
1804         g_dbus_remove_watch(connection, modem_removed_watch);
1805         g_dbus_remove_watch(connection, context_watch);
1806
1807         dbus_connection_unref(connection);
1808
1809         return err;
1810 }
1811
1812 static void ofono_exit(void)
1813 {
1814         g_dbus_remove_watch(connection, watch);
1815         g_dbus_remove_watch(connection, sim_watch);
1816         g_dbus_remove_watch(connection, reg_watch);
1817         g_dbus_remove_watch(connection, gprs_watch);
1818         g_dbus_remove_watch(connection, modem_watch);
1819         g_dbus_remove_watch(connection, modem_added_watch);
1820         g_dbus_remove_watch(connection, modem_removed_watch);
1821         g_dbus_remove_watch(connection, context_watch);
1822
1823         ofono_disconnect(connection, NULL);
1824
1825         connman_device_driver_unregister(&modem_driver);
1826         connman_network_driver_unregister(&network_driver);
1827
1828         dbus_connection_unref(connection);
1829 }
1830
1831 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1832                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)