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