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