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