ofono: Move cm powered update into a function
[platform/upstream/connman.git] / plugins / ofono.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7  *  Copyright (C) 2011  BWM Car IT GmbH. All rights reserved.
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <errno.h>
29 #include <stdlib.h>
30
31 #include <gdbus.h>
32 #include <string.h>
33 #include <stdint.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/network.h>
39 #include <connman/inet.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
42
43 #define uninitialized_var(x) x = x
44
45 #define OFONO_SERVICE                   "org.ofono"
46
47 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
49 #define OFONO_SIM_INTERFACE             OFONO_SERVICE ".SimManager"
50 #define OFONO_NETREG_INTERFACE          OFONO_SERVICE ".NetworkRegistration"
51 #define OFONO_CM_INTERFACE              OFONO_SERVICE ".ConnectionManager"
52 #define OFONO_CONTEXT_INTERFACE         OFONO_SERVICE ".ConnectionContext"
53
54 #define MODEM_ADDED                     "ModemAdded"
55 #define MODEM_REMOVED                   "ModemRemoved"
56 #define PROPERTY_CHANGED                "PropertyChanged"
57 #define CONTEXT_ADDED                   "ContextAdded"
58 #define CONTEXT_REMOVED                 "ContextRemoved"
59
60 #define GET_PROPERTIES                  "GetProperties"
61 #define SET_PROPERTY                    "SetProperty"
62 #define GET_MODEMS                      "GetModems"
63 #define GET_CONTEXTS                    "GetContexts"
64
65 #define TIMEOUT 40000
66
67 enum ofono_api {
68         OFONO_API_SIM =         0x1,
69         OFONO_API_NETREG =      0x2,
70         OFONO_API_CM =          0x4,
71 };
72
73 /*
74  * The way this plugin works is following:
75  *
76  *   powered -> SubscriberIdentity or Online = True -> gprs, context ->
77  *     attached -> netreg -> ready
78  *
79  * Enabling and disabling modems are steered through the rfkill
80  * interface. That means when ConnMan toggles the rfkill bit oFono
81  * will add or remove the modems.
82  *
83  * ConnMan will always power up (set Powered and Online) the
84  * modems. No need to power them down because this will be done
85  * through the rfkill inteface.
86  */
87
88 static DBusConnection *connection;
89
90 static GHashTable *modem_hash;
91 static GHashTable *context_hash;
92
93 struct network_context {
94         char *path;
95         int index;
96
97         enum connman_ipconfig_method ipv4_method;
98         struct connman_ipaddress *ipv4_address;
99         char *ipv4_nameservers;
100
101         enum connman_ipconfig_method ipv6_method;
102         struct connman_ipaddress *ipv6_address;
103         char *ipv6_nameservers;
104 };
105
106 struct modem_data {
107         char *path;
108
109         struct connman_device *device;
110         struct connman_network *network;
111
112         struct network_context *context;
113
114         /* Modem Interface */
115         char *serial;
116         connman_bool_t powered;
117         connman_bool_t online;
118         uint8_t interfaces;
119         connman_bool_t ignore;
120
121         connman_bool_t set_powered;
122         connman_bool_t set_online;
123
124         /* ConnectionManager Interface */
125         connman_bool_t attached;
126         connman_bool_t cm_powered;
127
128         connman_bool_t set_cm_powered;
129
130         /* ConnectionContext Interface */
131         connman_bool_t active;
132         connman_bool_t set_active;
133
134         /* SimManager Interface */
135         char *imsi;
136
137         /* Netreg Interface */
138         char *name;
139         uint8_t strength;
140         connman_bool_t roaming;
141
142         /* pending calls */
143         DBusPendingCall *call_set_property;
144         DBusPendingCall *call_get_properties;
145         DBusPendingCall *call_get_contexts;
146 };
147
148 static char *get_ident(const char *path)
149 {
150         char *pos;
151
152         if (*path != '/')
153                 return NULL;
154
155         pos = strrchr(path, '/');
156         if (pos == NULL)
157                 return NULL;
158
159         return pos + 1;
160 }
161
162 static struct network_context *network_context_alloc(const char *path)
163 {
164         struct network_context *context;
165
166         context = g_try_new0(struct network_context, 1);
167         if (context == NULL)
168                 return NULL;
169
170         context->path = g_strdup(path);
171         context->index = -1;
172
173         context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
174         context->ipv4_address = NULL;
175         context->ipv4_nameservers = NULL;
176
177         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
178         context->ipv6_address = NULL;
179         context->ipv6_nameservers = NULL;
180
181         return context;
182 }
183
184 static void network_context_free(struct network_context *context)
185 {
186         g_free(context->path);
187
188         connman_ipaddress_free(context->ipv4_address);
189         g_free(context->ipv4_nameservers);
190
191         connman_ipaddress_free(context->ipv6_address);
192         g_free(context->ipv6_nameservers);
193
194         free(context);
195 }
196
197 static void set_connected(struct modem_data *modem)
198 {
199         connman_bool_t setip = FALSE;
200
201         DBG("%s", modem->path);
202
203         connman_network_set_index(modem->network, modem->context->index);
204
205         switch (modem->context->ipv4_method) {
206         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
207         case CONNMAN_IPCONFIG_METHOD_OFF:
208         case CONNMAN_IPCONFIG_METHOD_MANUAL:
209         case CONNMAN_IPCONFIG_METHOD_AUTO:
210                 break;
211
212         case CONNMAN_IPCONFIG_METHOD_FIXED:
213                 connman_network_set_ipv4_method(modem->network,
214                                                 modem->context->ipv4_method);
215                 connman_network_set_ipaddress(modem->network,
216                                                 modem->context->ipv4_address);
217                 connman_network_set_nameservers(modem->network,
218                                         modem->context->ipv4_nameservers);
219                 setip = TRUE;
220                 break;
221
222         case CONNMAN_IPCONFIG_METHOD_DHCP:
223                 connman_network_set_ipv4_method(modem->network,
224                                                 modem->context->ipv4_method);
225                 setip = TRUE;
226                 break;
227         }
228
229         switch (modem->context->ipv6_method) {
230         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
231         case CONNMAN_IPCONFIG_METHOD_OFF:
232         case CONNMAN_IPCONFIG_METHOD_MANUAL:
233         case CONNMAN_IPCONFIG_METHOD_DHCP:
234         case CONNMAN_IPCONFIG_METHOD_AUTO:
235                 break;
236
237         case CONNMAN_IPCONFIG_METHOD_FIXED:
238                 connman_network_set_ipv6_method(modem->network,
239                                                         modem->context->ipv6_method);
240                 connman_network_set_ipaddress(modem->network,
241                                                         modem->context->ipv6_address);
242                 setip = TRUE;
243                 break;
244         }
245
246         if (setip == TRUE)
247                 connman_network_set_connected(modem->network, TRUE);
248 }
249
250 static void set_disconnected(struct modem_data *modem)
251 {
252         DBG("%s", modem->path);
253
254         connman_network_set_connected(modem->network, FALSE);
255 }
256
257 typedef void (*set_property_cb)(struct modem_data *data,
258                                 connman_bool_t success);
259 typedef void (*get_properties_cb)(struct modem_data *data,
260                                 DBusMessageIter *dict);
261
262 struct property_info {
263         struct modem_data *modem;
264         const char *path;
265         const char *interface;
266         const char *property;
267         set_property_cb set_property_cb;
268         get_properties_cb get_properties_cb;
269 };
270
271 static void set_property_reply(DBusPendingCall *call, void *user_data)
272 {
273         struct property_info *info = user_data;
274         DBusMessage *reply;
275         DBusError error;
276         connman_bool_t success = TRUE;
277
278         DBG("%s path %s %s.%s", info->modem->path,
279                 info->path, info->interface, info->property);
280
281         info->modem->call_set_property = NULL;
282
283         dbus_error_init(&error);
284
285         reply = dbus_pending_call_steal_reply(call);
286
287         if (dbus_set_error_from_message(&error, reply)) {
288                 connman_error("Failed to change property: %s %s.%s: %s %s",
289                                 info->path, info->interface, info->property,
290                                 error.name, error.message);
291                 dbus_error_free(&error);
292                 success = FALSE;
293         }
294
295         if (info->set_property_cb != NULL)
296                 (*info->set_property_cb)(info->modem, success);
297
298         dbus_message_unref(reply);
299
300         dbus_pending_call_unref(call);
301 }
302
303 static int set_property(struct modem_data *modem,
304                         const char *path, const char *interface,
305                         const char *property, int type, void *value,
306                         set_property_cb notify)
307 {
308         DBusMessage *message;
309         DBusMessageIter iter;
310         struct property_info *info;
311
312         DBG("%s path %s %s.%s", modem->path, path, interface, property);
313
314         if (modem->call_set_property != NULL) {
315                 connman_error("Pending SetProperty");
316                 return -EBUSY;
317         }
318
319         message = dbus_message_new_method_call(OFONO_SERVICE, path,
320                                         interface, SET_PROPERTY);
321         if (message == NULL)
322                 return -ENOMEM;
323
324         dbus_message_iter_init_append(message, &iter);
325         connman_dbus_property_append_basic(&iter, property, type, value);
326
327         if (dbus_connection_send_with_reply(connection, message,
328                         &modem->call_set_property, TIMEOUT) == FALSE) {
329                 connman_error("Failed to change property: %s %s.%s",
330                                 path, interface, property);
331                 dbus_message_unref(message);
332                 return -EINVAL;
333         }
334
335         if (modem->call_set_property == NULL) {
336                 connman_error("D-Bus connection not available");
337                 dbus_message_unref(message);
338                 return -EINVAL;
339         }
340
341         info = g_try_new0(struct property_info, 1);
342         if (info == NULL) {
343                 dbus_message_unref(message);
344                 return -ENOMEM;
345         }
346
347         info->modem = modem;
348         info->path = path;
349         info->interface = interface;
350         info->property = property;
351         info->set_property_cb = notify;
352
353         dbus_pending_call_set_notify(modem->call_set_property,
354                                         set_property_reply, info, g_free);
355
356         dbus_message_unref(message);
357
358         return -EINPROGRESS;
359 }
360
361 static void get_properties_reply(DBusPendingCall *call, void *user_data)
362 {
363         struct property_info *info = user_data;
364         DBusMessageIter array, dict;
365         DBusMessage *reply;
366         DBusError error;
367
368         DBG("%s path %s %s", info->modem->path, info->path, info->interface);
369
370         info->modem->call_get_properties = NULL;
371
372         dbus_error_init(&error);
373
374         reply = dbus_pending_call_steal_reply(call);
375
376         if (dbus_set_error_from_message(&error, reply)) {
377                 connman_error("Failed to get properties: %s %s: %s %s",
378                                 info->path, info->interface,
379                                 error.name, error.message);
380                 dbus_error_free(&error);
381
382                 goto done;
383         }
384
385         if (dbus_message_iter_init(reply, &array) == FALSE)
386                 goto done;
387
388         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
389                 goto done;
390
391         dbus_message_iter_recurse(&array, &dict);
392
393         if (info->get_properties_cb != NULL)
394                 (*info->get_properties_cb)(info->modem, &dict);
395
396 done:
397
398         dbus_message_unref(reply);
399
400         dbus_pending_call_unref(call);
401 }
402
403 static int get_properties(const char *path, const char *interface,
404                                 get_properties_cb notify,
405                                 struct modem_data *modem)
406 {
407         DBusMessage *message;
408         struct property_info *info;
409
410         DBG("%s path %s %s", modem->path, path, interface);
411
412         if (modem->call_get_properties != NULL) {
413                 connman_error("Pending GetProperties");
414                 return -EBUSY;
415         }
416
417         message = dbus_message_new_method_call(OFONO_SERVICE, path,
418                                         interface, GET_PROPERTIES);
419         if (message == NULL)
420                 return -ENOMEM;
421
422         if (dbus_connection_send_with_reply(connection, message,
423                         &modem->call_get_properties, TIMEOUT) == FALSE) {
424                 connman_error("Failed to call %s.GetProperties()", interface);
425                 dbus_message_unref(message);
426                 return -EINVAL;
427         }
428
429         if (modem->call_get_properties == NULL) {
430                 connman_error("D-Bus connection not available");
431                 dbus_message_unref(message);
432                 return -EINVAL;
433         }
434
435         info = g_try_new0(struct property_info, 1);
436         if (info == NULL) {
437                 dbus_message_unref(message);
438                 return -ENOMEM;
439         }
440
441         info->modem = modem;
442         info->path = path;
443         info->interface = interface;
444         info->get_properties_cb = notify;
445
446         dbus_pending_call_set_notify(modem->call_get_properties,
447                                         get_properties_reply, info, g_free);
448
449         dbus_message_unref(message);
450
451         return -EINPROGRESS;
452 }
453
454 static void context_set_active_reply(struct modem_data *modem,
455                                         connman_bool_t success)
456 {
457         DBG("%s", modem->path);
458
459         if (success == TRUE) {
460                 /*
461                  * Don't handle do anything on success here. oFono will send
462                  * the change via PropertyChanged singal.
463                  */
464                 return;
465         }
466
467         /*
468          * Active = True might fail due a timeout. That means oFono
469          * still tries to go online. If we retry to set Active = True,
470          * we just get a InProgress error message. Should we power
471          * cycle the modem in such cases?
472          */
473
474         connman_network_set_error(modem->network,
475                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
476 }
477
478 static int context_set_active(struct modem_data *modem)
479 {
480         dbus_bool_t active = TRUE;
481
482         DBG("%s", modem->path);
483
484         return set_property(modem, modem->context->path,
485                                 OFONO_CONTEXT_INTERFACE,
486                                 "Active", DBUS_TYPE_BOOLEAN,
487                                 &active,
488                                 context_set_active_reply);
489 }
490
491 static int context_set_inactive(struct modem_data *modem)
492 {
493         dbus_bool_t active = FALSE;
494         int err;
495
496         DBG("%s", modem->path);
497
498         err = set_property(modem, modem->context->path,
499                                 OFONO_CONTEXT_INTERFACE,
500                                 "Active", DBUS_TYPE_BOOLEAN,
501                                 &active,
502                                 NULL);
503         if (err == -EINPROGRESS)
504                 return 0;
505
506         return err;
507 }
508
509 static void modem_set_online_reply(struct modem_data *modem,
510                                         connman_bool_t success)
511 {
512         DBG("%s", modem->path);
513
514         if (success == TRUE) {
515                 /*
516                  * Don't handle do anything on success here. oFono will send
517                  * the change via PropertyChanged singal.
518                  */
519                 return;
520         }
521
522         modem->set_online = FALSE;
523 }
524
525 static int modem_set_online(struct modem_data *modem)
526 {
527         DBG("%s", modem->path);
528
529         modem->set_online = TRUE;
530
531         return set_property(modem, modem->path,
532                                 OFONO_MODEM_INTERFACE,
533                                 "Online", DBUS_TYPE_BOOLEAN,
534                                 &modem->set_online,
535                                 modem_set_online_reply);
536 }
537
538 static void cm_set_powered_reply(struct modem_data *modem,
539                                         connman_bool_t success)
540 {
541         DBG("%s", modem->path);
542
543         if (success == TRUE) {
544                 /*
545                  * Don't handle do anything on success here. oFono will send
546                  * the change via PropertyChanged singal.
547                  */
548                 return;
549         }
550
551         modem->set_cm_powered = FALSE;
552 }
553
554 static int cm_set_powered(struct modem_data *modem)
555 {
556         DBG("%s", modem->path);
557
558         modem->set_cm_powered = TRUE;
559
560         return set_property(modem, modem->path,
561                                 OFONO_CM_INTERFACE,
562                                 "Powered", DBUS_TYPE_BOOLEAN,
563                                 &modem->set_cm_powered,
564                                 cm_set_powered_reply);
565 }
566
567 static int modem_set_powered(struct modem_data *modem)
568 {
569         DBG("%s", modem->path);
570
571         modem->set_powered = TRUE;
572
573         return set_property(modem, modem->path,
574                                 OFONO_MODEM_INTERFACE,
575                                 "Powered", DBUS_TYPE_BOOLEAN,
576                                 &modem->set_powered,
577                                 NULL);
578 }
579
580 static int modem_set_unpowered(struct modem_data *modem)
581 {
582         DBG("%s", modem->path);
583
584         modem->set_powered = FALSE;
585
586         return set_property(modem, modem->path,
587                                 OFONO_MODEM_INTERFACE,
588                                 "Powered", DBUS_TYPE_BOOLEAN,
589                                 &modem->set_powered,
590                                 NULL);
591 }
592
593 static connman_bool_t has_interface(uint8_t interfaces,
594                                         enum ofono_api api)
595 {
596         if ((interfaces & api) == api)
597                 return TRUE;
598
599         return FALSE;
600 }
601
602 static uint8_t extract_interfaces(DBusMessageIter *array)
603 {
604         DBusMessageIter entry;
605         uint8_t interfaces = 0;
606
607         dbus_message_iter_recurse(array, &entry);
608
609         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
610                 const char *name;
611
612                 dbus_message_iter_get_basic(&entry, &name);
613
614                 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
615                         interfaces |= OFONO_API_SIM;
616                 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
617                         interfaces |= OFONO_API_NETREG;
618                 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
619                         interfaces |= OFONO_API_CM;
620
621                 dbus_message_iter_next(&entry);
622         }
623
624         return interfaces;
625 }
626
627 static char *extract_nameservers(DBusMessageIter *array)
628 {
629         DBusMessageIter entry;
630         char *nameservers = NULL;
631         char *tmp;
632
633         dbus_message_iter_recurse(array, &entry);
634
635         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
636                 const char *nameserver;
637
638                 dbus_message_iter_get_basic(&entry, &nameserver);
639
640                 if (nameservers == NULL) {
641                         nameservers = g_strdup(nameserver);
642                 } else {
643                         tmp = nameservers;
644                         nameservers = g_strdup_printf("%s %s", tmp, nameserver);
645                         g_free(tmp);
646                 }
647
648                 dbus_message_iter_next(&entry);
649         }
650
651         return nameservers;
652 }
653
654 static void extract_ipv4_settings(DBusMessageIter *array,
655                                 struct network_context *context)
656 {
657         DBusMessageIter dict;
658         char *address = NULL, *netmask = NULL, *gateway = NULL;
659         char *nameservers = NULL;
660         const char *interface = NULL;
661         int index = -1;
662
663         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
664                 return;
665
666         dbus_message_iter_recurse(array, &dict);
667
668         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
669                 DBusMessageIter entry, value;
670                 const char *key, *val;
671
672                 dbus_message_iter_recurse(&dict, &entry);
673                 dbus_message_iter_get_basic(&entry, &key);
674
675                 dbus_message_iter_next(&entry);
676                 dbus_message_iter_recurse(&entry, &value);
677
678                 if (g_str_equal(key, "Interface") == TRUE) {
679                         dbus_message_iter_get_basic(&value, &interface);
680
681                         DBG("Interface %s", interface);
682
683                         index = connman_inet_ifindex(interface);
684
685                         DBG("index %d", index);
686                 } else if (g_str_equal(key, "Method") == TRUE) {
687                         dbus_message_iter_get_basic(&value, &val);
688
689                         DBG("Method %s", val);
690
691                         if (g_strcmp0(val, "static") == 0) {
692                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
693                         } else if (g_strcmp0(val, "dhcp") == 0) {
694                                 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
695                                 break;
696                         }
697                 } else if (g_str_equal(key, "Address") == TRUE) {
698                         dbus_message_iter_get_basic(&value, &val);
699
700                         address = g_strdup(val);
701
702                         DBG("Address %s", address);
703                 } else if (g_str_equal(key, "Netmask") == TRUE) {
704                         dbus_message_iter_get_basic(&value, &val);
705
706                         netmask = g_strdup(val);
707
708                         DBG("Netmask %s", netmask);
709                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
710                         nameservers = extract_nameservers(&value);
711
712                         DBG("Nameservers %s", nameservers);
713                 } else if (g_str_equal(key, "Gateway") == TRUE) {
714                         dbus_message_iter_get_basic(&value, &val);
715
716                         gateway = g_strdup(val);
717
718                         DBG("Gateway %s", gateway);
719                 }
720
721                 dbus_message_iter_next(&dict);
722         }
723
724         if (index < 0)
725                 goto out;
726
727         if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
728                 goto out;
729
730         context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
731         if (context->ipv4_address == NULL)
732                 goto out;
733
734         context->index = index;
735         connman_ipaddress_set_ipv4(context->ipv4_address, address,
736                                 netmask, gateway);
737
738         context->ipv4_nameservers = nameservers;
739
740 out:
741         if (context->ipv4_nameservers != nameservers)
742                 g_free(nameservers);
743
744         g_free(address);
745         g_free(netmask);
746         g_free(gateway);
747 }
748
749 static void extract_ipv6_settings(DBusMessageIter *array,
750                                 struct network_context *context)
751 {
752         DBusMessageIter dict;
753         char *address = NULL, *gateway = NULL;
754         unsigned char prefix_length;
755         char *nameservers = NULL;
756         const char *interface = NULL;
757         int index = -1;
758
759         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
760                 return;
761
762         dbus_message_iter_recurse(array, &dict);
763
764         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
765                 DBusMessageIter entry, value;
766                 const char *key, *val;
767
768                 dbus_message_iter_recurse(&dict, &entry);
769                 dbus_message_iter_get_basic(&entry, &key);
770
771                 dbus_message_iter_next(&entry);
772                 dbus_message_iter_recurse(&entry, &value);
773
774                 if (g_str_equal(key, "Interface") == TRUE) {
775                         dbus_message_iter_get_basic(&value, &interface);
776
777                         DBG("Interface %s", interface);
778
779                         index = connman_inet_ifindex(interface);
780
781                         DBG("index %d", index);
782                 } else if (g_str_equal(key, "Address") == TRUE) {
783                         dbus_message_iter_get_basic(&value, &val);
784
785                         address = g_strdup(val);
786
787                         DBG("Address %s", address);
788                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
789                         dbus_message_iter_get_basic(&value, &prefix_length);
790
791                         DBG("prefix length %d", prefix_length);
792                 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
793                         nameservers = extract_nameservers(&value);
794
795                         DBG("Nameservers %s", nameservers);
796                 } else if (g_str_equal(key, "Gateway") == TRUE) {
797                         dbus_message_iter_get_basic(&value, &val);
798
799                         gateway = g_strdup(val);
800
801                         DBG("Gateway %s", gateway);
802                 }
803
804                 dbus_message_iter_next(&dict);
805         }
806
807         if (index < 0)
808                 goto out;
809
810         context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
811
812         context->ipv6_address =
813                 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
814         if (context->ipv6_address == NULL)
815                 goto out;
816
817         context->index = index;
818         connman_ipaddress_set_ipv6(context->ipv6_address, address,
819                                 prefix_length, gateway);
820
821         context->ipv6_nameservers = nameservers;
822
823 out:
824         if (context->ipv6_nameservers != nameservers)
825                 g_free(nameservers);
826
827         g_free(address);
828         g_free(gateway);
829 }
830
831 static connman_bool_t ready_to_create_device(struct modem_data *modem)
832 {
833         /*
834          * There are three different modem types which behave slightly
835          * different:
836          * - GSM modems will expose the SIM interface then the
837          *   CM interface.
838          * - DUN modems will expose first a unique serial number (BDADDR)
839          *   and then the CM interface.
840          * - CDMA modems will expose CM first and sometime later
841          *   a unique serial number.
842          *
843          * This functions tests if we have the necessary information gathered
844          * before we are able to create a device.
845          */
846
847         if (modem->device != NULL)
848                 return FALSE;
849
850         if (modem->imsi != NULL || modem->serial != NULL)
851                 return TRUE;
852
853         return FALSE;
854 }
855
856 static void create_device(struct modem_data *modem)
857 {
858         struct connman_device *device;
859         char *uninitialized_var(ident);
860
861         DBG("%s", modem->path);
862
863         if (modem->imsi != NULL)
864                 ident = modem->imsi;
865         else if (modem->serial != NULL)
866                 ident = modem->serial;
867
868         if (connman_dbus_validate_ident(ident) == FALSE)
869                 ident = connman_dbus_encode_string(ident);
870         else
871                 ident = g_strdup(ident);
872
873         device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
874         if (device == NULL)
875                 goto out;
876
877         DBG("device %p", device);
878
879         connman_device_set_ident(device, ident);
880
881         connman_device_set_string(device, "Path", modem->path);
882
883         connman_device_set_data(device, modem);
884
885         if (connman_device_register(device) < 0) {
886                 connman_error("Failed to register cellular device");
887                 connman_device_unref(device);
888                 goto out;
889         }
890
891         modem->device = device;
892
893 out:
894         g_free(ident);
895 }
896
897 static void destroy_device(struct modem_data *modem)
898 {
899         DBG("%s", modem->path);
900
901         connman_device_set_powered(modem->device, FALSE);
902
903         if (modem->network != NULL) {
904                 connman_device_remove_network(modem->device, modem->network);
905                 connman_network_unref(modem->network);
906                 modem->network = NULL;
907         }
908
909         connman_device_unregister(modem->device);
910         connman_device_unref(modem->device);
911
912         modem->device = NULL;
913 }
914
915 static void add_network(struct modem_data *modem)
916 {
917         const char *group;
918
919         DBG("%s", modem->path);
920
921         if (modem->network != NULL)
922                 return;
923
924         modem->network = connman_network_create(modem->context->path,
925                                                 CONNMAN_NETWORK_TYPE_CELLULAR);
926         if (modem->network == NULL)
927                 return;
928
929         DBG("network %p", modem->network);
930
931         connman_network_set_data(modem->network, modem);
932
933         connman_network_set_string(modem->network, "Path",
934                                         modem->context->path);
935
936         connman_network_set_index(modem->network, modem->context->index);
937
938         if (modem->name != NULL)
939                 connman_network_set_name(modem->network, modem->name);
940         else
941                 connman_network_set_name(modem->network, "");
942
943         connman_network_set_strength(modem->network, modem->strength);
944
945         group = get_ident(modem->context->path);
946         connman_network_set_group(modem->network, group);
947
948         connman_network_set_available(modem->network, TRUE);
949
950         connman_network_set_bool(modem->network, "Roaming",
951                                         modem->roaming);
952
953         if (connman_device_add_network(modem->device, modem->network) < 0) {
954                 connman_network_unref(modem->network);
955                 modem->network = NULL;
956                 return;
957         }
958 }
959
960 static int add_cm_context(struct modem_data *modem, const char *context_path,
961                                 DBusMessageIter *dict)
962 {
963         const char *context_type;
964         struct network_context *context = NULL;
965         connman_bool_t active = FALSE;
966
967         DBG("%s context path %s", modem->path, context_path);
968
969         if (modem->context != NULL) {
970                 /*
971                  * We have already assigned a context to this modem
972                  * and we do only support one Internet context.
973                  */
974                 return -EALREADY;
975         }
976
977         context = network_context_alloc(context_path);
978         if (context == NULL)
979                 return -ENOMEM;
980
981         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
982                 DBusMessageIter entry, value;
983                 const char *key;
984
985                 dbus_message_iter_recurse(dict, &entry);
986                 dbus_message_iter_get_basic(&entry, &key);
987
988                 dbus_message_iter_next(&entry);
989                 dbus_message_iter_recurse(&entry, &value);
990
991                 if (g_str_equal(key, "Type") == TRUE) {
992                         dbus_message_iter_get_basic(&value, &context_type);
993
994                         DBG("%s context %s type %s", modem->path,
995                                 context_path, context_type);
996                 } else if (g_str_equal(key, "Settings") == TRUE) {
997                         DBG("%s Settings", modem->path);
998
999                         extract_ipv4_settings(&value, context);
1000                 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1001                         DBG("%s IPv6.Settings", modem->path);
1002
1003                         extract_ipv6_settings(&value, context);
1004                 } else if (g_str_equal(key, "Active") == TRUE) {
1005                         dbus_message_iter_get_basic(&value, &active);
1006
1007                         DBG("%s Active %d", modem->path, active);
1008                 }
1009
1010                 dbus_message_iter_next(dict);
1011         }
1012
1013         if (g_strcmp0(context_type, "internet") != 0) {
1014                 network_context_free(context);
1015                 return -EINVAL;
1016         }
1017
1018         modem->context = context;
1019         modem->active = active;
1020
1021         g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1022
1023         return 0;
1024 }
1025
1026 static void remove_cm_context(struct modem_data *modem,
1027                                 const char *context_path)
1028 {
1029         if (modem->context == NULL)
1030                 return;
1031
1032         g_hash_table_remove(context_hash, context_path);
1033
1034         network_context_free(modem->context);
1035         modem->context = NULL;
1036 }
1037
1038 static gboolean context_changed(DBusConnection *connection,
1039                                 DBusMessage *message,
1040                                 void *user_data)
1041 {
1042         const char *context_path = dbus_message_get_path(message);
1043         struct modem_data *modem = NULL;
1044         DBusMessageIter iter, value;
1045         const char *key;
1046
1047         DBG("context_path %s", context_path);
1048
1049         modem = g_hash_table_lookup(context_hash, context_path);
1050         if (modem == NULL)
1051                 return TRUE;
1052
1053         if (dbus_message_iter_init(message, &iter) == FALSE)
1054                 return TRUE;
1055
1056         dbus_message_iter_get_basic(&iter, &key);
1057
1058         dbus_message_iter_next(&iter);
1059         dbus_message_iter_recurse(&iter, &value);
1060
1061         /*
1062          * oFono guarantees the ordering of Settings and
1063          * Active. Settings will always be send before Active = True.
1064          * That means we don't have to order here.
1065          */
1066         if (g_str_equal(key, "Settings") == TRUE) {
1067                 DBG("%s Settings", modem->path);
1068
1069                 extract_ipv4_settings(&value, modem->context);
1070         } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1071                 DBG("%s IPv6.Settings", modem->path);
1072
1073                 extract_ipv6_settings(&value, modem->context);
1074         } else if (g_str_equal(key, "Active") == TRUE) {
1075                 dbus_message_iter_get_basic(&value, &modem->active);
1076
1077                 DBG("%s Active %d", modem->path, modem->active);
1078
1079                 if (modem->active == TRUE)
1080                         set_connected(modem);
1081                 else
1082                         set_disconnected(modem);
1083         }
1084
1085         return TRUE;
1086 }
1087
1088 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1089 {
1090         struct modem_data *modem = user_data;
1091         DBusMessageIter array, dict, entry, value;
1092         DBusMessage *reply;
1093         DBusError error;
1094
1095         DBG("%s", modem->path);
1096
1097         modem->call_get_contexts = NULL;
1098
1099         reply = dbus_pending_call_steal_reply(call);
1100
1101         dbus_error_init(&error);
1102
1103         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1104                 connman_error("%s", error.message);
1105                 dbus_error_free(&error);
1106                 goto done;
1107         }
1108
1109         if (dbus_message_iter_init(reply, &array) == FALSE)
1110                 goto done;
1111
1112         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1113                 goto done;
1114
1115         dbus_message_iter_recurse(&array, &dict);
1116
1117         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1118                 const char *context_path;
1119
1120                 dbus_message_iter_recurse(&dict, &entry);
1121                 dbus_message_iter_get_basic(&entry, &context_path);
1122
1123                 dbus_message_iter_next(&entry);
1124                 dbus_message_iter_recurse(&entry, &value);
1125
1126                 if (add_cm_context(modem, context_path, &value) == 0)
1127                         break;
1128
1129                 dbus_message_iter_next(&dict);
1130         }
1131
1132 done:
1133         dbus_message_unref(reply);
1134
1135         dbus_pending_call_unref(call);
1136 }
1137
1138 static int cm_get_contexts(struct modem_data *modem)
1139 {
1140         DBusMessage *message;
1141
1142         DBG("%s", modem->path);
1143
1144         if (modem->call_get_contexts != NULL)
1145                 return -EBUSY;
1146
1147         message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1148                                         OFONO_CM_INTERFACE, GET_CONTEXTS);
1149         if (message == NULL)
1150                 return -ENOMEM;
1151
1152         if (dbus_connection_send_with_reply(connection, message,
1153                         &modem->call_get_contexts, TIMEOUT) == FALSE) {
1154                 connman_error("Failed to call GetContexts()");
1155                 dbus_message_unref(message);
1156                 return -EINVAL;
1157         }
1158
1159         if (modem->call_get_contexts == NULL) {
1160                 connman_error("D-Bus connection not available");
1161                 dbus_message_unref(message);
1162                 return -EINVAL;
1163         }
1164
1165         dbus_pending_call_set_notify(modem->call_get_contexts,
1166                                         cm_get_contexts_reply,
1167                                         modem, NULL);
1168
1169         dbus_message_unref(message);
1170
1171         return -EINPROGRESS;
1172 }
1173
1174 static gboolean cm_context_added(DBusConnection *connection,
1175                                         DBusMessage *message,
1176                                         void *user_data)
1177 {
1178         const char *path = dbus_message_get_path(message);
1179         char *context_path;
1180         struct modem_data *modem;
1181         DBusMessageIter iter, properties;
1182
1183         DBG("%s", path);
1184
1185         modem = g_hash_table_lookup(modem_hash, context_path);
1186         if (modem == NULL)
1187                 return TRUE;
1188
1189         if (dbus_message_iter_init(message, &iter) == FALSE)
1190                 return TRUE;
1191
1192         dbus_message_iter_get_basic(&iter, &context_path);
1193
1194         dbus_message_iter_next(&iter);
1195         dbus_message_iter_recurse(&iter, &properties);
1196
1197         if (add_cm_context(modem, context_path, &properties) != 0)
1198                 return TRUE;
1199
1200         return TRUE;
1201 }
1202
1203 static gboolean cm_context_removed(DBusConnection *connection,
1204                                         DBusMessage *message,
1205                                         void *user_data)
1206 {
1207         const char *path = dbus_message_get_path(message);
1208         const char *context_path;
1209         struct modem_data *modem;
1210         DBusMessageIter iter;
1211
1212         DBG("context path %s", path);
1213
1214         if (dbus_message_iter_init(message, &iter) == FALSE)
1215                 return TRUE;
1216
1217         dbus_message_iter_get_basic(&iter, &context_path);
1218
1219         modem = g_hash_table_lookup(context_hash, context_path);
1220         if (modem == NULL)
1221                 return TRUE;
1222
1223         remove_cm_context(modem, context_path);
1224
1225         return TRUE;
1226 }
1227
1228 static void netreg_update_name(struct modem_data *modem,
1229                                 DBusMessageIter* value)
1230 {
1231         char *name;
1232
1233         dbus_message_iter_get_basic(value, &name);
1234
1235         DBG("%s Name %s", modem->path, name);
1236
1237         g_free(modem->name);
1238         modem->name = g_strdup(name);
1239
1240         if (modem->network == NULL)
1241                 return;
1242
1243         connman_network_set_name(modem->network, modem->name);
1244         connman_network_update(modem->network);
1245 }
1246
1247 static void netreg_update_strength(struct modem_data *modem,
1248                                         DBusMessageIter *value)
1249 {
1250         dbus_message_iter_get_basic(value, &modem->strength);
1251
1252         DBG("%s Strength %d", modem->path, modem->strength);
1253
1254         if (modem->network == NULL)
1255                 return;
1256
1257         connman_network_set_strength(modem->network, modem->strength);
1258         connman_network_update(modem->network);
1259 }
1260
1261 static void netreg_update_roaming(struct modem_data *modem,
1262                                         DBusMessageIter *value)
1263 {
1264         char *status;
1265         connman_bool_t roaming;
1266
1267         dbus_message_iter_get_basic(value, &status);
1268
1269         if (g_str_equal(status, "roaming") == TRUE)
1270                 roaming = TRUE;
1271         else
1272                 roaming = FALSE;
1273
1274         if (roaming == modem->roaming)
1275                 return;
1276
1277         modem->roaming = roaming;
1278
1279         if (modem->network == NULL)
1280                 return;
1281
1282         connman_network_set_bool(modem->network,
1283                                 "Roaming", modem->roaming);
1284         connman_network_update(modem->network);
1285 }
1286
1287 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1288                                 void *user_data)
1289 {
1290         const char *path = dbus_message_get_path(message);
1291         struct modem_data *modem;
1292         DBusMessageIter iter, value;
1293         const char *key;
1294
1295         modem = g_hash_table_lookup(modem_hash, path);
1296         if (modem == NULL)
1297                 return TRUE;
1298
1299         if (modem->ignore == TRUE)
1300                 return TRUE;
1301
1302         if (dbus_message_iter_init(message, &iter) == FALSE)
1303                 return TRUE;
1304
1305         dbus_message_iter_get_basic(&iter, &key);
1306
1307         dbus_message_iter_next(&iter);
1308         dbus_message_iter_recurse(&iter, &value);
1309
1310         if (g_str_equal(key, "Name") == TRUE) {
1311                 netreg_update_name(modem, &value);
1312         } else if (g_str_equal(key, "Strength") == TRUE) {
1313                 netreg_update_strength(modem, &value);
1314         } else if (g_str_equal(key, "Status") == TRUE) {
1315                 netreg_update_roaming(modem, &value);
1316         }
1317
1318         return TRUE;
1319 }
1320
1321 static void netreg_properties_reply(struct modem_data *modem,
1322                                         DBusMessageIter *dict)
1323 {
1324         DBG("%s", modem->path);
1325
1326         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1327                 DBusMessageIter entry, value;
1328                 const char *key;
1329
1330                 dbus_message_iter_recurse(dict, &entry);
1331                 dbus_message_iter_get_basic(&entry, &key);
1332
1333                 dbus_message_iter_next(&entry);
1334                 dbus_message_iter_recurse(&entry, &value);
1335
1336                 if (g_str_equal(key, "Name") == TRUE) {
1337                         netreg_update_name(modem, &value);
1338                 } else if (g_str_equal(key, "Strength") == TRUE) {
1339                         netreg_update_strength(modem, &value);
1340                 } else if (g_str_equal(key, "Status") == TRUE) {
1341                         netreg_update_roaming(modem, &value);
1342                 }
1343
1344                 dbus_message_iter_next(dict);
1345         }
1346
1347         if (modem->context == NULL) {
1348                 /*
1349                  * netgreg_get_properties() was issued after we got
1350                  * cm_get_contexts_reply() where we create the
1351                  * context. Though before we got the
1352                  * netreg_properties_reply the context was removed
1353                  * again. Therefore we have to skip the network
1354                  * creation.
1355                  */
1356                 return;
1357         }
1358
1359         add_network(modem);
1360
1361         if (modem->active == TRUE)
1362                 set_connected(modem);
1363 }
1364
1365 static int netreg_get_properties(struct modem_data *modem)
1366 {
1367         return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1368                         netreg_properties_reply, modem);
1369 }
1370
1371 static void cm_update_attached(struct modem_data *modem,
1372                                 DBusMessageIter *value)
1373 {
1374         dbus_message_iter_get_basic(value, &modem->attached);
1375
1376         DBG("%s Attached %d", modem->path, modem->attached);
1377
1378         if (modem->attached == FALSE)
1379                 return;
1380
1381         if (has_interface(modem->interfaces,
1382                                 OFONO_API_NETREG) == FALSE) {
1383                 return;
1384         }
1385
1386         netreg_get_properties(modem);
1387 }
1388
1389 static void cm_update_powered(struct modem_data *modem,
1390                                 DBusMessageIter *value)
1391 {
1392         dbus_message_iter_get_basic(value, &modem->cm_powered);
1393
1394         DBG("%s ConnnectionManager Powered %d", modem->path,
1395                 modem->cm_powered);
1396
1397         if (modem->cm_powered == TRUE)
1398                 return;
1399
1400         cm_set_powered(modem);
1401 }
1402
1403 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1404                                 void *user_data)
1405 {
1406         const char *path = dbus_message_get_path(message);
1407         struct modem_data *modem;
1408         DBusMessageIter iter, value;
1409         const char *key;
1410
1411         modem = g_hash_table_lookup(modem_hash, path);
1412         if (modem == NULL)
1413                 return TRUE;
1414
1415         if (modem->ignore == TRUE)
1416                 return TRUE;
1417
1418         if (dbus_message_iter_init(message, &iter) == FALSE)
1419                 return TRUE;
1420
1421         dbus_message_iter_get_basic(&iter, &key);
1422
1423         dbus_message_iter_next(&iter);
1424         dbus_message_iter_recurse(&iter, &value);
1425
1426         if (g_str_equal(key, "Attached") == TRUE) {
1427                 cm_update_attached(modem, &value);
1428         } else if (g_str_equal(key, "Powered") == TRUE) {
1429                 cm_update_powered(modem, &value);
1430         }
1431
1432         return TRUE;
1433 }
1434
1435 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1436 {
1437         DBG("%s", modem->path);
1438
1439         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1440                 DBusMessageIter entry, value;
1441                 const char *key;
1442
1443                 dbus_message_iter_recurse(dict, &entry);
1444                 dbus_message_iter_get_basic(&entry, &key);
1445
1446                 dbus_message_iter_next(&entry);
1447                 dbus_message_iter_recurse(&entry, &value);
1448
1449                 if (g_str_equal(key, "Attached") == TRUE) {
1450                         cm_update_attached(modem, &value);
1451                 } else if (g_str_equal(key, "Powered") == TRUE) {
1452                         cm_update_powered(modem, &value);
1453                 }
1454
1455                 dbus_message_iter_next(dict);
1456         }
1457 }
1458
1459 static int cm_get_properties(struct modem_data *modem)
1460 {
1461         return get_properties(modem->path, OFONO_CM_INTERFACE,
1462                                 cm_properties_reply, modem);
1463 }
1464
1465 static void update_sim_imsi(struct modem_data *modem,
1466                                 const char *imsi)
1467 {
1468         DBG("%s imsi %s", modem->path, imsi);
1469
1470         if (g_strcmp0(modem->imsi, imsi) == 0)
1471                 return;
1472
1473         g_free(modem->imsi);
1474         modem->imsi = g_strdup(imsi);
1475 }
1476
1477 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1478                                 void *user_data)
1479 {
1480         const char *path = dbus_message_get_path(message);
1481         struct modem_data *modem;
1482         DBusMessageIter iter, value;
1483         const char *key;
1484
1485         modem = g_hash_table_lookup(modem_hash, path);
1486         if (modem == NULL)
1487                 return TRUE;
1488
1489         if (modem->ignore == TRUE)
1490                 return TRUE;
1491
1492         if (dbus_message_iter_init(message, &iter) == FALSE)
1493                 return TRUE;
1494
1495         dbus_message_iter_get_basic(&iter, &key);
1496
1497         dbus_message_iter_next(&iter);
1498         dbus_message_iter_recurse(&iter, &value);
1499
1500         if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1501                 char *imsi;
1502
1503                 dbus_message_iter_get_basic(&value, &imsi);
1504
1505                 update_sim_imsi(modem, imsi);
1506
1507                 if (modem->online == FALSE) {
1508                         modem_set_online(modem);
1509                 } else if (has_interface(modem->interfaces,
1510                                                 OFONO_API_CM) == TRUE) {
1511                         if (ready_to_create_device(modem) == TRUE)
1512                                 create_device(modem);
1513                 }
1514         }
1515
1516         return TRUE;
1517 }
1518
1519 static void sim_properties_reply(struct modem_data *modem,
1520                                         DBusMessageIter *dict)
1521 {
1522         DBG("%s", modem->path);
1523
1524         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1525                 DBusMessageIter entry, value;
1526                 const char *key;
1527
1528                 dbus_message_iter_recurse(dict, &entry);
1529                 dbus_message_iter_get_basic(&entry, &key);
1530
1531                 dbus_message_iter_next(&entry);
1532                 dbus_message_iter_recurse(&entry, &value);
1533
1534                 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1535                         char *imsi;
1536
1537                         dbus_message_iter_get_basic(&value, &imsi);
1538
1539                         update_sim_imsi(modem, imsi);
1540
1541                         if (modem->online == FALSE) {
1542                                 modem_set_online(modem);
1543                                 break;
1544                         }
1545
1546                         if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1547                                 if (ready_to_create_device(modem) == TRUE)
1548                                         create_device(modem);
1549                                 if (modem->device != NULL) {
1550                                         cm_get_properties(modem);
1551                                         cm_get_contexts(modem);
1552                                 }
1553                         }
1554                         return;
1555                 }
1556
1557                 dbus_message_iter_next(dict);
1558         }
1559 }
1560
1561 static int sim_get_properties(struct modem_data *modem)
1562 {
1563         return get_properties(modem->path, OFONO_SIM_INTERFACE,
1564                         sim_properties_reply, modem);
1565 }
1566
1567 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1568                                 void *user_data)
1569 {
1570         const char *path = dbus_message_get_path(message);
1571         struct modem_data *modem;
1572         DBusMessageIter iter, value;
1573         const char *key;
1574
1575         modem = g_hash_table_lookup(modem_hash, path);
1576         if (modem == NULL)
1577                 return TRUE;
1578
1579         if (modem->ignore == TRUE)
1580                 return TRUE;
1581
1582         if (dbus_message_iter_init(message, &iter) == FALSE)
1583                 return TRUE;
1584
1585         dbus_message_iter_get_basic(&iter, &key);
1586
1587         dbus_message_iter_next(&iter);
1588         dbus_message_iter_recurse(&iter, &value);
1589
1590         if (g_str_equal(key, "Powered") == TRUE) {
1591                 dbus_message_iter_get_basic(&value, &modem->powered);
1592
1593                 DBG("%s Powered %d", modem->path, modem->powered);
1594
1595                 if (modem->powered == FALSE)
1596                         modem_set_powered(modem);
1597         } else if (g_str_equal(key, "Online") == TRUE) {
1598                 dbus_message_iter_get_basic(&value, &modem->online);
1599
1600                 DBG("%s Online %d", modem->path, modem->online);
1601
1602                 if (modem->online == FALSE)
1603                         return TRUE;
1604
1605                 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
1606                         return TRUE;
1607                 if (ready_to_create_device(modem) == TRUE)
1608                         create_device(modem);
1609                 if (modem->device != NULL) {
1610                         cm_get_properties(modem);
1611                         cm_get_contexts(modem);
1612                 }
1613         } else if (g_str_equal(key, "Interfaces") == TRUE) {
1614                 modem->interfaces = extract_interfaces(&value);
1615
1616                 DBG("%s Interfaces 0x%02x", modem->path,
1617                         modem->interfaces);
1618
1619                 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1620                         if (modem->imsi == NULL &&
1621                                         modem->set_powered == FALSE) {
1622                                 /*
1623                                  * Only use do GetProperties() when
1624                                  * device has not been powered up.
1625                                  */
1626                                 sim_get_properties(modem);
1627                                 return TRUE;
1628                         }
1629                 }
1630
1631                 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1632                         if (ready_to_create_device(modem) == TRUE)
1633                                 create_device(modem);
1634                         if (modem->device != NULL) {
1635                                 cm_get_properties(modem);
1636                                 cm_get_contexts(modem);
1637                                 return TRUE;
1638                         }
1639                 } else {
1640                         if (modem->context != NULL) {
1641                                 remove_cm_context(modem,
1642                                                 modem->context->path);
1643                         }
1644
1645                         if (modem->device != NULL)
1646                                 destroy_device(modem);
1647
1648                         return TRUE;
1649                 }
1650
1651                 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1652                         if (modem->attached == TRUE)
1653                                 netreg_get_properties(modem);
1654                 }
1655         } else if (g_str_equal(key, "Serial") == TRUE) {
1656                 char *serial;
1657
1658                 dbus_message_iter_get_basic(&value, &serial);
1659
1660                 g_free(modem->serial);
1661                 modem->serial = g_strdup(serial);
1662
1663                 DBG("%s Serial %s", modem->path, modem->serial);
1664
1665                 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1666                         if (ready_to_create_device(modem) == TRUE)
1667                                 create_device(modem);
1668                         if (modem->device != NULL) {
1669                                 cm_get_properties(modem);
1670                                 cm_get_contexts(modem);
1671                         }
1672                 }
1673         }
1674
1675         return TRUE;
1676 }
1677
1678 static void add_modem(const char *path, DBusMessageIter *prop)
1679 {
1680         struct modem_data *modem;
1681
1682         DBG("%s", path);
1683
1684         modem = g_hash_table_lookup(modem_hash, path);
1685         if (modem != NULL) {
1686                 /*
1687                  * When oFono powers up we ask for the modems and oFono is
1688                  * reporting with modem_added signal the modems. Only
1689                  * handle them once.
1690                  */
1691                 return;
1692         }
1693
1694         modem = g_try_new0(struct modem_data, 1);
1695         if (modem == NULL)
1696                 return;
1697
1698         modem->path = g_strdup(path);
1699
1700         g_hash_table_insert(modem_hash, g_strdup(path), modem);
1701
1702         while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1703                 DBusMessageIter entry, value;
1704                 const char *key;
1705
1706                 dbus_message_iter_recurse(prop, &entry);
1707                 dbus_message_iter_get_basic(&entry, &key);
1708
1709                 dbus_message_iter_next(&entry);
1710                 dbus_message_iter_recurse(&entry, &value);
1711
1712                 if (g_str_equal(key, "Powered") == TRUE) {
1713                         dbus_message_iter_get_basic(&value, &modem->powered);
1714
1715                         DBG("%s Powered %d", modem->path, modem->powered);
1716                 } else if (g_str_equal(key, "Online") == TRUE) {
1717                         dbus_message_iter_get_basic(&value, &modem->online);
1718
1719                         DBG("%s Online %d", modem->path, modem->online);
1720                 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1721                         modem->interfaces = extract_interfaces(&value);
1722
1723                         DBG("%s Interfaces 0x%02x", modem->path,
1724                                 modem->interfaces);
1725                 } else if (g_str_equal(key, "Serial") == TRUE) {
1726                         char *serial;
1727
1728                         dbus_message_iter_get_basic(&value, &serial);
1729                         modem->serial = g_strdup(serial);
1730
1731                         DBG("%s Serial %s", modem->path, modem->serial);
1732                 } else if (g_str_equal(key, "Type") == TRUE) {
1733                         char *type;
1734
1735                         dbus_message_iter_get_basic(&value, &type);
1736
1737                         DBG("%s Type %s", modem->path, type);
1738                         if (g_strcmp0(type, "hardware") != 0) {
1739                                 DBG("%s Ignore this modem", modem->path);
1740                                 modem->ignore = TRUE;
1741                         }
1742                 }
1743
1744                 dbus_message_iter_next(prop);
1745         }
1746
1747         if (modem->ignore == TRUE)
1748                 return;
1749
1750         if (modem->powered == FALSE) {
1751                 modem_set_powered(modem);
1752         } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1753                 sim_get_properties(modem);
1754         } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1755                 if (ready_to_create_device(modem) == TRUE)
1756                         create_device(modem);
1757                 if (modem->device != NULL) {
1758                         cm_get_properties(modem);
1759                         cm_get_contexts(modem);
1760                 }
1761         }
1762 }
1763
1764 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
1765 {
1766         struct modem_data *modem = value;
1767
1768         DBG("%s", modem->path);
1769
1770         if (modem->ignore ==  TRUE)
1771                 return;
1772
1773         modem_set_unpowered(modem);
1774 }
1775
1776 static void remove_modem(gpointer data)
1777 {
1778         struct modem_data *modem = data;
1779
1780         DBG("%s", modem->path);
1781
1782         if (modem->call_set_property != NULL)
1783                 dbus_pending_call_cancel(modem->call_set_property);
1784
1785         if (modem->call_get_properties != NULL)
1786                 dbus_pending_call_cancel(modem->call_get_properties);
1787
1788         if (modem->call_get_contexts != NULL)
1789                 dbus_pending_call_cancel(modem->call_get_contexts);
1790
1791         if (modem->device != NULL)
1792                 destroy_device(modem);
1793
1794         if (modem->context != NULL)
1795                 remove_cm_context(modem, modem->context->path);
1796
1797         g_free(modem->serial);
1798         g_free(modem->name);
1799         g_free(modem->imsi);
1800         g_free(modem->path);
1801
1802         g_free(modem);
1803 }
1804
1805 static gboolean modem_added(DBusConnection *connection,
1806                                 DBusMessage *message, void *user_data)
1807 {
1808         DBusMessageIter iter, properties;
1809         const char *path;
1810
1811         DBG("");
1812
1813         if (dbus_message_iter_init(message, &iter) == FALSE)
1814                 return TRUE;
1815
1816         dbus_message_iter_get_basic(&iter, &path);
1817
1818         dbus_message_iter_next(&iter);
1819         dbus_message_iter_recurse(&iter, &properties);
1820
1821         add_modem(path, &properties);
1822
1823         return TRUE;
1824 }
1825
1826 static gboolean modem_removed(DBusConnection *connection,
1827                                 DBusMessage *message, void *user_data)
1828 {
1829         DBusMessageIter iter;
1830         const char *path;
1831
1832         DBG("");
1833
1834         if (dbus_message_iter_init(message, &iter) == FALSE)
1835                 return TRUE;
1836
1837         dbus_message_iter_get_basic(&iter, &path);
1838
1839         g_hash_table_remove(modem_hash, path);
1840
1841         return TRUE;
1842 }
1843
1844 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1845 {
1846         DBusMessage *reply;
1847         DBusError error;
1848         DBusMessageIter array, dict;
1849
1850         DBG("");
1851
1852         reply = dbus_pending_call_steal_reply(call);
1853
1854         dbus_error_init(&error);
1855
1856         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1857                 connman_error("%s", error.message);
1858                 dbus_error_free(&error);
1859                 goto done;
1860         }
1861
1862         if (dbus_message_iter_init(reply, &array) == FALSE)
1863                 goto done;
1864
1865         dbus_message_iter_recurse(&array, &dict);
1866
1867         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1868                 DBusMessageIter value, properties;
1869                 const char *path;
1870
1871                 dbus_message_iter_recurse(&dict, &value);
1872                 dbus_message_iter_get_basic(&value, &path);
1873
1874                 dbus_message_iter_next(&value);
1875                 dbus_message_iter_recurse(&value, &properties);
1876
1877                 add_modem(path, &properties);
1878
1879                 dbus_message_iter_next(&dict);
1880         }
1881
1882 done:
1883         dbus_message_unref(reply);
1884
1885         dbus_pending_call_unref(call);
1886 }
1887
1888 static int manager_get_modems(void)
1889 {
1890         DBusMessage *message;
1891         DBusPendingCall *call;
1892
1893         DBG("");
1894
1895         message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1896                                         OFONO_MANAGER_INTERFACE, GET_MODEMS);
1897         if (message == NULL)
1898                 return -ENOMEM;
1899
1900         if (dbus_connection_send_with_reply(connection, message,
1901                                                &call, TIMEOUT) == FALSE) {
1902                 connman_error("Failed to call GetModems()");
1903                 dbus_message_unref(message);
1904                 return -EINVAL;
1905         }
1906
1907         if (call == NULL) {
1908                 connman_error("D-Bus connection not available");
1909                 dbus_message_unref(message);
1910                 return -EINVAL;
1911         }
1912
1913         dbus_pending_call_set_notify(call, manager_get_modems_reply,
1914                                         NULL, NULL);
1915
1916         dbus_message_unref(message);
1917
1918         return -EINPROGRESS;
1919 }
1920
1921 static void ofono_connect(DBusConnection *conn, void *user_data)
1922 {
1923         DBG("");
1924
1925         modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1926                                                 g_free, remove_modem);
1927         if (modem_hash == NULL)
1928                 return;
1929
1930         context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1931                                                 g_free, NULL);
1932         if (context_hash == NULL) {
1933                 g_hash_table_destroy(modem_hash);
1934                 return;
1935         }
1936
1937         manager_get_modems();
1938 }
1939
1940 static void ofono_disconnect(DBusConnection *conn, void *user_data)
1941 {
1942         DBG("");
1943
1944         if (modem_hash == NULL || context_hash == NULL)
1945                 return;
1946
1947         g_hash_table_destroy(modem_hash);
1948         modem_hash = NULL;
1949
1950         g_hash_table_destroy(context_hash);
1951         context_hash = NULL;
1952 }
1953
1954 static int network_probe(struct connman_network *network)
1955 {
1956         struct modem_data *modem = connman_network_get_data(network);
1957
1958         DBG("%s network %p", modem->path, network);
1959
1960         return 0;
1961 }
1962
1963 static void network_remove(struct connman_network *network)
1964 {
1965         struct modem_data *modem = connman_network_get_data(network);
1966
1967         DBG("%s network %p", modem->path, network);
1968 }
1969
1970 static int network_connect(struct connman_network *network)
1971 {
1972         struct modem_data *modem = connman_network_get_data(network);
1973
1974         DBG("%s network %p", modem->path, network);
1975
1976         return context_set_active(modem);
1977 }
1978
1979 static int network_disconnect(struct connman_network *network)
1980 {
1981         struct modem_data *modem = connman_network_get_data(network);
1982
1983         DBG("%s network %p", modem->path, network);
1984
1985         return context_set_inactive(modem);
1986 }
1987
1988 static struct connman_network_driver network_driver = {
1989         .name           = "network",
1990         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
1991         .probe          = network_probe,
1992         .remove         = network_remove,
1993         .connect        = network_connect,
1994         .disconnect     = network_disconnect,
1995 };
1996
1997 static int modem_probe(struct connman_device *device)
1998 {
1999         struct modem_data *modem = connman_device_get_data(device);
2000
2001         DBG("%s device %p", modem->path, device);
2002
2003         return 0;
2004 }
2005
2006 static void modem_remove(struct connman_device *device)
2007 {
2008         struct modem_data *modem = connman_device_get_data(device);
2009
2010         DBG("%s device %p", modem->path, device);
2011 }
2012
2013 static int modem_enable(struct connman_device *device)
2014 {
2015         struct modem_data *modem = connman_device_get_data(device);
2016
2017         DBG("%s device %p", modem->path, device);
2018
2019         return 0;
2020 }
2021
2022 static int modem_disable(struct connman_device *device)
2023 {
2024         struct modem_data *modem = connman_device_get_data(device);
2025
2026         DBG("%s device %p", modem->path, device);
2027
2028         return 0;
2029 }
2030
2031 static struct connman_device_driver modem_driver = {
2032         .name           = "modem",
2033         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
2034         .probe          = modem_probe,
2035         .remove         = modem_remove,
2036         .enable         = modem_enable,
2037         .disable        = modem_disable,
2038 };
2039
2040 static guint watch;
2041 static guint modem_added_watch;
2042 static guint modem_removed_watch;
2043 static guint modem_watch;
2044 static guint cm_watch;
2045 static guint sim_watch;
2046 static guint context_added_watch;
2047 static guint context_removed_watch;
2048 static guint netreg_watch;
2049 static guint context_watch;
2050
2051 static int ofono_init(void)
2052 {
2053         int err;
2054
2055         DBG("");
2056
2057         connection = connman_dbus_get_connection();
2058         if (connection == NULL)
2059                 return -EIO;
2060
2061         watch = g_dbus_add_service_watch(connection,
2062                                         OFONO_SERVICE, ofono_connect,
2063                                         ofono_disconnect, NULL, NULL);
2064
2065         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2066                                                 OFONO_MANAGER_INTERFACE,
2067                                                 MODEM_ADDED,
2068                                                 modem_added,
2069                                                 NULL, NULL);
2070
2071         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2072                                                 OFONO_MANAGER_INTERFACE,
2073                                                 MODEM_REMOVED,
2074                                                 modem_removed,
2075                                                 NULL, NULL);
2076
2077         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2078                                                 OFONO_MODEM_INTERFACE,
2079                                                 PROPERTY_CHANGED,
2080                                                 modem_changed,
2081                                                 NULL, NULL);
2082
2083         cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2084                                                 OFONO_CM_INTERFACE,
2085                                                 PROPERTY_CHANGED,
2086                                                 cm_changed,
2087                                                 NULL, NULL);
2088
2089         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2090                                                 OFONO_SIM_INTERFACE,
2091                                                 PROPERTY_CHANGED,
2092                                                 sim_changed,
2093                                                 NULL, NULL);
2094
2095         context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2096                                                 OFONO_CM_INTERFACE,
2097                                                 CONTEXT_ADDED,
2098                                                 cm_context_added,
2099                                                 NULL, NULL);
2100
2101         context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2102                                                 OFONO_CM_INTERFACE,
2103                                                 CONTEXT_REMOVED,
2104                                                 cm_context_removed,
2105                                                 NULL, NULL);
2106
2107         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2108                                                 OFONO_CONTEXT_INTERFACE,
2109                                                 PROPERTY_CHANGED,
2110                                                 context_changed,
2111                                                 NULL, NULL);
2112
2113         netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2114                                                 OFONO_NETREG_INTERFACE,
2115                                                 PROPERTY_CHANGED,
2116                                                 netreg_changed,
2117                                                 NULL, NULL);
2118
2119
2120         if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2121                         modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2122                         context_added_watch == 0 ||
2123                         context_removed_watch == 0 ||
2124                         context_watch == 0 || netreg_watch == 0) {
2125                 err = -EIO;
2126                 goto remove;
2127         }
2128
2129         err = connman_network_driver_register(&network_driver);
2130         if (err < 0)
2131                 goto remove;
2132
2133         err = connman_device_driver_register(&modem_driver);
2134         if (err < 0) {
2135                 connman_network_driver_unregister(&network_driver);
2136                 goto remove;
2137         }
2138
2139         return 0;
2140
2141 remove:
2142         g_dbus_remove_watch(connection, netreg_watch);
2143         g_dbus_remove_watch(connection, context_watch);
2144         g_dbus_remove_watch(connection, context_removed_watch);
2145         g_dbus_remove_watch(connection, context_added_watch);
2146         g_dbus_remove_watch(connection, sim_watch);
2147         g_dbus_remove_watch(connection, cm_watch);
2148         g_dbus_remove_watch(connection, modem_watch);
2149         g_dbus_remove_watch(connection, modem_removed_watch);
2150         g_dbus_remove_watch(connection, modem_added_watch);
2151         g_dbus_remove_watch(connection, watch);
2152         dbus_connection_unref(connection);
2153
2154         return err;
2155 }
2156
2157 static void ofono_exit(void)
2158 {
2159         DBG("");
2160
2161         if (modem_hash != NULL) {
2162                 /*
2163                  * We should propably wait for the SetProperty() reply
2164                  * message, because ...
2165                  */
2166                 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2167
2168                 /*
2169                  * ... here we will cancel the call.
2170                  */
2171                 g_hash_table_destroy(modem_hash);
2172                 modem_hash = NULL;
2173         }
2174
2175         if (context_hash != NULL) {
2176                 g_hash_table_destroy(context_hash);
2177                 context_hash = NULL;
2178         }
2179
2180         connman_device_driver_unregister(&modem_driver);
2181         connman_network_driver_unregister(&network_driver);
2182
2183         g_dbus_remove_watch(connection, netreg_watch);
2184         g_dbus_remove_watch(connection, context_watch);
2185         g_dbus_remove_watch(connection, context_removed_watch);
2186         g_dbus_remove_watch(connection, context_added_watch);
2187         g_dbus_remove_watch(connection, sim_watch);
2188         g_dbus_remove_watch(connection, cm_watch);
2189         g_dbus_remove_watch(connection, modem_watch);
2190         g_dbus_remove_watch(connection, modem_added_watch);
2191         g_dbus_remove_watch(connection, modem_removed_watch);
2192         g_dbus_remove_watch(connection, watch);
2193
2194         dbus_connection_unref(connection);
2195 }
2196
2197 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2198                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)