technology: Add helpers for (un)registering a technology in D-Bus
[platform/upstream/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection;
34
35 static GSList *technology_list = NULL;
36
37 /*
38  * List of devices with no technology associated with them either because of
39  * no compiled in support or the driver is not yet loaded.
40 */
41 static GSList *techless_device_list = NULL;
42 static GHashTable *rfkill_list;
43
44 static connman_bool_t global_offlinemode;
45
46 struct connman_rfkill {
47         unsigned int index;
48         enum connman_service_type type;
49         connman_bool_t softblock;
50         connman_bool_t hardblock;
51 };
52
53 struct connman_technology {
54         int refcount;
55         enum connman_service_type type;
56         char *path;
57         GSList *device_list;
58         int enabled;
59         char *regdom;
60         connman_bool_t connected;
61
62         connman_bool_t tethering;
63         char *tethering_ident;
64         char *tethering_passphrase;
65
66         connman_bool_t enable_persistent; /* Save the tech state */
67
68         struct connman_technology_driver *driver;
69         void *driver_data;
70
71         DBusMessage *pending_reply;
72         guint pending_timeout;
73
74         GSList *scan_pending;
75
76         connman_bool_t hardblocked;
77         connman_bool_t dbus_registered;
78 };
79
80 static GSList *driver_list = NULL;
81
82 static gint compare_priority(gconstpointer a, gconstpointer b)
83 {
84         const struct connman_technology_driver *driver1 = a;
85         const struct connman_technology_driver *driver2 = b;
86
87         return driver2->priority - driver1->priority;
88 }
89
90 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
91 {
92         struct connman_rfkill *rfkill = value;
93         enum connman_service_type type = GPOINTER_TO_INT(user_data);
94
95         /* Calling _technology_rfkill_add will update the tech. */
96         if (rfkill->type == type)
97                 __connman_technology_add_rfkill(rfkill->index, type,
98                                 rfkill->softblock, rfkill->hardblock);
99 }
100
101 /**
102  * connman_technology_driver_register:
103  * @driver: Technology driver definition
104  *
105  * Register a new technology driver
106  *
107  * Returns: %0 on success
108  */
109 int connman_technology_driver_register(struct connman_technology_driver *driver)
110 {
111         GSList *list;
112         struct connman_device *device;
113         enum connman_service_type type;
114
115         DBG("Registering %s driver", driver->name);
116
117         driver_list = g_slist_insert_sorted(driver_list, driver,
118                                                         compare_priority);
119
120         if (techless_device_list == NULL)
121                 goto check_rfkill;
122
123         /*
124          * Check for technology less devices if this driver
125          * can service any of them.
126         */
127         for (list = techless_device_list; list; list = list->next) {
128                 device = list->data;
129
130                 type = __connman_device_get_service_type(device);
131                 if (type != driver->type)
132                         continue;
133
134                 techless_device_list = g_slist_remove(techless_device_list,
135                                                                 device);
136
137                 __connman_technology_add_device(device);
138         }
139
140 check_rfkill:
141         /* Check for orphaned rfkill switches. */
142         g_hash_table_foreach(rfkill_list, rfkill_check,
143                                         GINT_TO_POINTER(driver->type));
144
145         return 0;
146 }
147
148 /**
149  * connman_technology_driver_unregister:
150  * @driver: Technology driver definition
151  *
152  * Remove a previously registered technology driver
153  */
154 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
155 {
156         GSList *list;
157         struct connman_technology *technology;
158
159         DBG("Unregistering driver %p name %s", driver, driver->name);
160
161         for (list = technology_list; list; list = list->next) {
162                 technology = list->data;
163
164                 if (technology->driver == NULL)
165                         continue;
166
167                 if (technology->type == driver->type) {
168                         technology->driver->remove(technology);
169                         technology->driver = NULL;
170                 }
171         }
172
173         driver_list = g_slist_remove(driver_list, driver);
174 }
175
176 static void tethering_changed(struct connman_technology *technology)
177 {
178         connman_bool_t tethering = technology->tethering;
179
180         connman_dbus_property_changed_basic(technology->path,
181                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
182                                                 DBUS_TYPE_BOOLEAN, &tethering);
183 }
184
185 void connman_technology_tethering_notify(struct connman_technology *technology,
186                                                         connman_bool_t enabled)
187 {
188         GSList *list;
189
190         DBG("technology %p enabled %u", technology, enabled);
191
192         if (technology->tethering == enabled)
193                 return;
194
195         technology->tethering = enabled;
196
197         tethering_changed(technology);
198
199         if (enabled == TRUE)
200                 __connman_tethering_set_enabled();
201         else {
202                 for (list = technology_list; list; list = list->next) {
203                         struct connman_technology *other_tech = list->data;
204                         if (other_tech->tethering == TRUE)
205                                 break;
206                 }
207                 if (list == NULL)
208                         __connman_tethering_set_disabled();
209         }
210 }
211
212 static int set_tethering(struct connman_technology *technology,
213                                 connman_bool_t enabled)
214 {
215         const char *ident, *passphrase, *bridge;
216
217         ident = technology->tethering_ident;
218         passphrase = technology->tethering_passphrase;
219
220         if (technology->driver == NULL ||
221                         technology->driver->set_tethering == NULL)
222                 return -EOPNOTSUPP;
223
224         bridge = __connman_tethering_get_bridge();
225         if (bridge == NULL)
226                 return -EOPNOTSUPP;
227
228         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
229             (ident == NULL || passphrase == NULL))
230                 return -EINVAL;
231
232         return technology->driver->set_tethering(technology, ident, passphrase,
233                                                         bridge, enabled);
234 }
235
236 void connman_technology_regdom_notify(struct connman_technology *technology,
237                                                         const char *alpha2)
238 {
239         DBG("");
240
241         if (alpha2 == NULL)
242                 connman_error("Failed to set regulatory domain");
243         else
244                 DBG("Regulatory domain set to %s", alpha2);
245
246         g_free(technology->regdom);
247         technology->regdom = g_strdup(alpha2);
248 }
249
250 static int set_regdom_by_device(struct connman_technology *technology,
251                                                         const char *alpha2)
252 {
253         GSList *list;
254
255         for (list = technology->device_list; list; list = list->next) {
256                 struct connman_device *device = list->data;
257
258                 if (connman_device_set_regdom(device, alpha2) != 0)
259                         return -ENOTSUP;
260         }
261
262         return 0;
263 }
264
265 int connman_technology_set_regdom(const char *alpha2)
266 {
267         GSList *list;
268
269         for (list = technology_list; list; list = list->next) {
270                 struct connman_technology *technology = list->data;
271
272                 if (set_regdom_by_device(technology, alpha2) != 0) {
273                         if (technology->driver == NULL)
274                                 continue;
275
276                         if (technology->driver->set_regdom != NULL)
277                                 technology->driver->set_regdom(technology,
278                                                                 alpha2);
279                 }
280         }
281
282         return 0;
283 }
284
285 static void free_rfkill(gpointer data)
286 {
287         struct connman_rfkill *rfkill = data;
288
289         g_free(rfkill);
290 }
291
292 static const char *get_name(enum connman_service_type type)
293 {
294         switch (type) {
295         case CONNMAN_SERVICE_TYPE_UNKNOWN:
296         case CONNMAN_SERVICE_TYPE_SYSTEM:
297         case CONNMAN_SERVICE_TYPE_GPS:
298         case CONNMAN_SERVICE_TYPE_VPN:
299         case CONNMAN_SERVICE_TYPE_GADGET:
300                 break;
301         case CONNMAN_SERVICE_TYPE_ETHERNET:
302                 return "Wired";
303         case CONNMAN_SERVICE_TYPE_WIFI:
304                 return "WiFi";
305         case CONNMAN_SERVICE_TYPE_WIMAX:
306                 return "WiMAX";
307         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
308                 return "Bluetooth";
309         case CONNMAN_SERVICE_TYPE_CELLULAR:
310                 return "Cellular";
311         }
312
313         return NULL;
314 }
315
316 static void technology_save(struct connman_technology *technology)
317 {
318         GKeyFile *keyfile;
319         gchar *identifier;
320
321         DBG("technology %p", technology);
322
323         keyfile = __connman_storage_load_global();
324         if (keyfile == NULL)
325                 keyfile = g_key_file_new();
326
327         identifier = g_strdup_printf("%s", get_name(technology->type));
328         if (identifier == NULL)
329                 goto done;
330
331         g_key_file_set_boolean(keyfile, identifier, "Enable",
332                                 technology->enable_persistent);
333
334         if (technology->tethering_ident != NULL)
335                 g_key_file_set_string(keyfile, identifier,
336                                         "Tethering.Identifier",
337                                         technology->tethering_ident);
338
339         if (technology->tethering_passphrase != NULL)
340                 g_key_file_set_string(keyfile, identifier,
341                                         "Tethering.Passphrase",
342                                         technology->tethering_passphrase);
343
344 done:
345         g_free(identifier);
346
347         __connman_storage_save_global(keyfile);
348
349         g_key_file_free(keyfile);
350
351         return;
352 }
353
354 static void technology_load(struct connman_technology *technology)
355 {
356         GKeyFile *keyfile;
357         gchar *identifier;
358         GError *error = NULL;
359         connman_bool_t enable;
360
361         DBG("technology %p", technology);
362
363         keyfile = __connman_storage_load_global();
364         /* Fallback on disabling technology if file not found. */
365         if (keyfile == NULL) {
366                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
367                         /* We enable ethernet by default */
368                         technology->enable_persistent = TRUE;
369                 else
370                         technology->enable_persistent = FALSE;
371                 return;
372         }
373
374         identifier = g_strdup_printf("%s", get_name(technology->type));
375         if (identifier == NULL)
376                 goto done;
377
378         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
379         if (error == NULL)
380                 technology->enable_persistent = enable;
381         else {
382                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
383                         technology->enable_persistent = TRUE;
384                 else
385                         technology->enable_persistent = FALSE;
386
387                 technology_save(technology);
388                 g_clear_error(&error);
389         }
390
391         technology->tethering_ident = g_key_file_get_string(keyfile,
392                                 identifier, "Tethering.Identifier", NULL);
393
394         technology->tethering_passphrase = g_key_file_get_string(keyfile,
395                                 identifier, "Tethering.Passphrase", NULL);
396 done:
397         g_free(identifier);
398
399         g_key_file_free(keyfile);
400
401         return;
402 }
403
404 connman_bool_t __connman_technology_get_offlinemode(void)
405 {
406         return global_offlinemode;
407 }
408
409 static void connman_technology_save_offlinemode()
410 {
411         GKeyFile *keyfile;
412
413         keyfile = __connman_storage_load_global();
414         if (keyfile == NULL)
415                 keyfile = g_key_file_new();
416
417         g_key_file_set_boolean(keyfile, "global",
418                                         "OfflineMode", global_offlinemode);
419
420         __connman_storage_save_global(keyfile);
421
422         g_key_file_free(keyfile);
423
424         return;
425 }
426
427 static connman_bool_t connman_technology_load_offlinemode()
428 {
429         GKeyFile *keyfile;
430         GError *error = NULL;
431         connman_bool_t offlinemode;
432
433         /* If there is a error, we enable offlinemode */
434         keyfile = __connman_storage_load_global();
435         if (keyfile == NULL)
436                 return FALSE;
437
438         offlinemode = g_key_file_get_boolean(keyfile, "global",
439                                                 "OfflineMode", &error);
440         if (error != NULL) {
441                 offlinemode = FALSE;
442                 g_clear_error(&error);
443         }
444
445         g_key_file_free(keyfile);
446
447         return offlinemode;
448 }
449
450 static void append_properties(DBusMessageIter *iter,
451                 struct connman_technology *technology)
452 {
453         DBusMessageIter dict;
454         const char *str;
455         connman_bool_t powered;
456
457         connman_dbus_dict_open(iter, &dict);
458
459         str = get_name(technology->type);
460         if (str != NULL)
461                 connman_dbus_dict_append_basic(&dict, "Name",
462                                                 DBUS_TYPE_STRING, &str);
463
464         str = __connman_service_type2string(technology->type);
465         if (str != NULL)
466                 connman_dbus_dict_append_basic(&dict, "Type",
467                                                 DBUS_TYPE_STRING, &str);
468
469         __sync_synchronize();
470         if (technology->enabled > 0)
471                 powered = TRUE;
472         else
473                 powered = FALSE;
474         connman_dbus_dict_append_basic(&dict, "Powered",
475                                         DBUS_TYPE_BOOLEAN, &powered);
476
477         connman_dbus_dict_append_basic(&dict, "Connected",
478                                         DBUS_TYPE_BOOLEAN,
479                                         &technology->connected);
480
481         connman_dbus_dict_append_basic(&dict, "Tethering",
482                                         DBUS_TYPE_BOOLEAN,
483                                         &technology->tethering);
484
485         if (technology->tethering_ident != NULL)
486                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
487                                                 DBUS_TYPE_STRING,
488                                                 &technology->tethering_ident);
489
490         if (technology->tethering_passphrase != NULL)
491                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
492                                                 DBUS_TYPE_STRING,
493                                                 &technology->tethering_passphrase);
494
495         connman_dbus_dict_close(iter, &dict);
496 }
497
498 static void technology_added_signal(struct connman_technology *technology)
499 {
500         DBusMessage *signal;
501         DBusMessageIter iter;
502
503         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
504                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
505         if (signal == NULL)
506                 return;
507
508         dbus_message_iter_init_append(signal, &iter);
509         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
510                                                         &technology->path);
511         append_properties(&iter, technology);
512
513         dbus_connection_send(connection, signal, NULL);
514         dbus_message_unref(signal);
515 }
516
517 static void technology_removed_signal(struct connman_technology *technology)
518 {
519         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
520                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
521                         DBUS_TYPE_OBJECT_PATH, &technology->path,
522                         DBUS_TYPE_INVALID);
523 }
524
525 static DBusMessage *get_properties(DBusConnection *conn,
526                                         DBusMessage *message, void *user_data)
527 {
528         struct connman_technology *technology = user_data;
529         DBusMessage *reply;
530         DBusMessageIter iter;
531
532         reply = dbus_message_new_method_return(message);
533         if (reply == NULL)
534                 return NULL;
535
536         dbus_message_iter_init_append(reply, &iter);
537         append_properties(&iter, technology);
538
539         return reply;
540 }
541
542 void __connman_technology_list_struct(DBusMessageIter *array)
543 {
544         GSList *list;
545         DBusMessageIter entry;
546
547         for (list = technology_list; list; list = list->next) {
548                 struct connman_technology *technology = list->data;
549
550                 if (technology->path == NULL)
551                         continue;
552
553                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
554                                 NULL, &entry);
555                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
556                                 &technology->path);
557                 append_properties(&entry, technology);
558                 dbus_message_iter_close_container(array, &entry);
559         }
560 }
561
562 static gboolean technology_pending_reply(gpointer user_data)
563 {
564         struct connman_technology *technology = user_data;
565         DBusMessage *reply;
566
567         /* Power request timedout, send ETIMEDOUT. */
568         if (technology->pending_reply != NULL) {
569                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
570                 if (reply != NULL)
571                         g_dbus_send_message(connection, reply);
572
573                 dbus_message_unref(technology->pending_reply);
574                 technology->pending_reply = NULL;
575                 technology->pending_timeout = 0;
576         }
577
578         return FALSE;
579 }
580
581 static int technology_enable(struct connman_technology *technology,
582                                                 connman_bool_t hardblock)
583 {
584         GSList *list;
585         int err = 0;
586
587         DBG("technology %p enable", technology);
588
589         __sync_synchronize();
590         if (technology->enabled > 0) {
591                 err = -EALREADY;
592                 goto done;
593         }
594
595         if (technology->pending_reply != NULL) {
596                 err = -EBUSY;
597                 goto done;
598         }
599
600         if (hardblock == TRUE && technology->enable_persistent == FALSE)
601                 goto done;
602
603         __connman_rfkill_block(technology->type, FALSE);
604
605         for (list = technology->device_list; list; list = list->next) {
606                 struct connman_device *device = list->data;
607
608                 err = __connman_device_enable(device);
609         }
610
611 done:
612         return err;
613 }
614
615 static int technology_disable(struct connman_technology *technology,
616                                                 connman_bool_t hardblock)
617 {
618         GSList *list;
619         int err = 0;
620
621         DBG("technology %p disable", technology);
622
623         __sync_synchronize();
624         if (technology->enabled == 0) {
625                 err = -EALREADY;
626                 goto done;
627         }
628
629         if (technology->pending_reply != NULL) {
630                 err = -EBUSY;
631                 goto done;
632         }
633
634         if (technology->tethering == TRUE)
635                 set_tethering(technology, FALSE);
636
637         if (hardblock == FALSE)
638                 __connman_rfkill_block(technology->type, TRUE);
639
640         for (list = technology->device_list; list; list = list->next) {
641                 struct connman_device *device = list->data;
642
643                 err = __connman_device_disable(device);
644         }
645
646 done:
647         return err;
648 }
649
650 static DBusMessage *set_powered(struct connman_technology *technology,
651                                 DBusMessage *msg, connman_bool_t powered)
652 {
653         DBusMessage *reply = NULL;
654         int err = 0;
655
656         if (technology->hardblocked == TRUE) {
657                 err = -EACCES;
658                 goto make_reply;
659         }
660
661         if (powered == TRUE)
662                 err = technology_enable(technology, FALSE);
663         else
664                 err = technology_disable(technology, FALSE);
665
666         if (err != -EBUSY) {
667                 technology->enable_persistent = powered;
668                 technology_save(technology);
669         }
670
671 make_reply:
672         if (err == -EINPROGRESS) {
673                 technology->pending_reply = dbus_message_ref(msg);
674                 technology->pending_timeout = g_timeout_add_seconds(10,
675                                         technology_pending_reply, technology);
676         } else if (err == -EALREADY) {
677                 if (powered == TRUE)
678                         reply = __connman_error_already_enabled(msg);
679                 else
680                         reply = __connman_error_already_disabled(msg);
681         } else if (err < 0)
682                 reply = __connman_error_failed(msg, -err);
683         else
684                 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
685
686         return reply;
687 }
688
689 static DBusMessage *set_property(DBusConnection *conn,
690                                         DBusMessage *msg, void *data)
691 {
692         struct connman_technology *technology = data;
693         DBusMessageIter iter, value;
694         const char *name;
695         int type;
696
697         DBG("conn %p", conn);
698
699         if (dbus_message_iter_init(msg, &iter) == FALSE)
700                 return __connman_error_invalid_arguments(msg);
701
702         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
703                 return __connman_error_invalid_arguments(msg);
704
705         dbus_message_iter_get_basic(&iter, &name);
706         dbus_message_iter_next(&iter);
707
708         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
709                 return __connman_error_invalid_arguments(msg);
710
711         dbus_message_iter_recurse(&iter, &value);
712
713         type = dbus_message_iter_get_arg_type(&value);
714
715         DBG("property %s", name);
716
717         if (g_str_equal(name, "Tethering") == TRUE) {
718                 int err;
719                 connman_bool_t tethering;
720
721                 if (type != DBUS_TYPE_BOOLEAN)
722                         return __connman_error_invalid_arguments(msg);
723
724                 dbus_message_iter_get_basic(&value, &tethering);
725
726                 if (technology->tethering == tethering) {
727                         if (tethering == FALSE)
728                                 return __connman_error_already_disabled(msg);
729                         else
730                                 return __connman_error_already_enabled(msg);
731                 }
732
733                 err = set_tethering(technology, tethering);
734                 if (err < 0)
735                         return __connman_error_failed(msg, -err);
736
737         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
738                 const char *str;
739
740                 dbus_message_iter_get_basic(&value, &str);
741
742                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
743                         return __connman_error_not_supported(msg);
744
745                 if (strlen(str) < 1 || strlen(str) > 32)
746                         return __connman_error_invalid_arguments(msg);
747
748                 if (g_strcmp0(technology->tethering_ident, str) != 0) {
749                         g_free(technology->tethering_ident);
750                         technology->tethering_ident = g_strdup(str);
751                         technology_save(technology);
752
753                         connman_dbus_property_changed_basic(technology->path,
754                                                 CONNMAN_TECHNOLOGY_INTERFACE,
755                                                 "TetheringIdentifier",
756                                                 DBUS_TYPE_STRING,
757                                                 &technology->tethering_ident);
758                 }
759         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
760                 const char *str;
761
762                 dbus_message_iter_get_basic(&value, &str);
763
764                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
765                         return __connman_error_not_supported(msg);
766
767                 if (strlen(str) < 8 || strlen(str) > 63)
768                         return __connman_error_passphrase_required(msg);
769
770                 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
771                         g_free(technology->tethering_passphrase);
772                         technology->tethering_passphrase = g_strdup(str);
773                         technology_save(technology);
774
775                         connman_dbus_property_changed_basic(technology->path,
776                                         CONNMAN_TECHNOLOGY_INTERFACE,
777                                         "TetheringPassphrase",
778                                         DBUS_TYPE_STRING,
779                                         &technology->tethering_passphrase);
780                 }
781         } else if (g_str_equal(name, "Powered") == TRUE) {
782                 connman_bool_t enable;
783
784                 if (type != DBUS_TYPE_BOOLEAN)
785                         return __connman_error_invalid_arguments(msg);
786
787                 dbus_message_iter_get_basic(&value, &enable);
788
789                 return set_powered(technology, msg, enable);
790         } else
791                 return __connman_error_invalid_property(msg);
792
793         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
794 }
795
796 static struct connman_technology *technology_find(enum connman_service_type type)
797 {
798         GSList *list;
799
800         DBG("type %d", type);
801
802         for (list = technology_list; list; list = list->next) {
803                 struct connman_technology *technology = list->data;
804
805                 if (technology->type == type)
806                         return technology;
807         }
808
809         return NULL;
810 }
811
812 static void reply_scan_pending(struct connman_technology *technology, int err)
813 {
814         DBusMessage *reply;
815
816         DBG("technology %p err %d", technology, err);
817
818         while (technology->scan_pending != NULL) {
819                 DBusMessage *msg = technology->scan_pending->data;
820
821                 DBG("reply to %s", dbus_message_get_sender(msg));
822
823                 if (err == 0)
824                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
825                 else
826                         reply = __connman_error_failed(msg, -err);
827                 g_dbus_send_message(connection, reply);
828                 dbus_message_unref(msg);
829
830                 technology->scan_pending =
831                         g_slist_delete_link(technology->scan_pending,
832                                         technology->scan_pending);
833         }
834 }
835
836 void __connman_technology_scan_started(struct connman_device *device)
837 {
838         DBG("device %p", device);
839 }
840
841 void __connman_technology_scan_stopped(struct connman_device *device)
842 {
843         int count = 0;
844         struct connman_technology *technology;
845         enum connman_service_type type;
846         GSList *list;
847
848         type = __connman_device_get_service_type(device);
849         technology = technology_find(type);
850
851         DBG("technology %p device %p", technology, device);
852
853         if (technology == NULL)
854                 return;
855
856         for (list = technology->device_list; list != NULL; list = list->next) {
857                 struct connman_device *other_device = list->data;
858
859                 if (device == other_device)
860                         continue;
861
862                 if (__connman_device_get_service_type(other_device) != type)
863                         continue;
864
865                 if (connman_device_get_scanning(other_device) == TRUE)
866                         count += 1;
867         }
868
869         if (count == 0)
870                 reply_scan_pending(technology, 0);
871 }
872
873 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
874                                                 int result, const char *alpha2)
875 {
876         struct connman_technology *technology;
877         enum connman_service_type type;
878
879         type = __connman_device_get_service_type(device);
880         technology = technology_find(type);
881
882         if (technology == NULL)
883                 return;
884
885         if (result < 0) {
886                 if (technology->driver != NULL &&
887                                 technology->driver->set_regdom != NULL) {
888                         technology->driver->set_regdom(technology, alpha2);
889                         return;
890                 }
891
892                 alpha2 = NULL;
893         }
894
895         connman_technology_regdom_notify(technology, alpha2);
896 }
897
898 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
899 {
900         struct connman_technology *technology = data;
901         int err;
902
903         DBG ("technology %p request from %s", technology,
904                         dbus_message_get_sender(msg));
905
906         dbus_message_ref(msg);
907         technology->scan_pending =
908                 g_slist_prepend(technology->scan_pending, msg);
909
910         err = __connman_device_request_scan(technology->type);
911         if (err < 0)
912                 reply_scan_pending(technology, err);
913
914         return NULL;
915 }
916
917 static const GDBusMethodTable technology_methods[] = {
918         { GDBUS_DEPRECATED_METHOD("GetProperties",
919                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
920                         get_properties) },
921         { GDBUS_ASYNC_METHOD("SetProperty",
922                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
923                         NULL, set_property) },
924         { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
925         { },
926 };
927
928 static const GDBusSignalTable technology_signals[] = {
929         { GDBUS_SIGNAL("PropertyChanged",
930                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
931         { },
932 };
933
934 static gboolean technology_dbus_register(struct connman_technology *technology)
935 {
936         if (technology->dbus_registered == TRUE)
937                 return TRUE;
938
939         if (g_dbus_register_interface(connection, technology->path,
940                                 CONNMAN_TECHNOLOGY_INTERFACE,
941                                 technology_methods, technology_signals,
942                                 NULL, technology, NULL) == FALSE) {
943                 connman_error("Failed to register %s", technology->path);
944                 return FALSE;
945         }
946
947         technology_added_signal(technology);
948         technology->dbus_registered = TRUE;
949
950         return TRUE;
951 }
952
953 static struct connman_technology *technology_get(enum connman_service_type type)
954 {
955         struct connman_technology *technology;
956         struct connman_technology_driver *driver = NULL;
957         const char *str;
958         GSList *list;
959         int err;
960
961         DBG("type %d", type);
962
963         str = __connman_service_type2string(type);
964         if (str == NULL)
965                 return NULL;
966
967         technology = technology_find(type);
968         if (technology != NULL) {
969                 __sync_fetch_and_add(&technology->refcount, 1);
970                 return technology;
971         }
972
973         /* First check if we have a driver for this technology type */
974         for (list = driver_list; list; list = list->next) {
975                 driver = list->data;
976
977                 if (driver->type == type)
978                         break;
979                 else
980                         driver = NULL;
981         }
982
983         if (driver == NULL) {
984                 DBG("No matching driver found for %s.",
985                                 __connman_service_type2string(type));
986                 return NULL;
987         }
988
989         technology = g_try_new0(struct connman_technology, 1);
990         if (technology == NULL)
991                 return NULL;
992
993         technology->refcount = 1;
994
995         if (type == CONNMAN_SERVICE_TYPE_ETHERNET)
996                 technology->hardblocked = FALSE;
997         else
998                 technology->hardblocked = TRUE;
999
1000         technology->type = type;
1001         technology->path = g_strdup_printf("%s/technology/%s",
1002                                                         CONNMAN_PATH, str);
1003
1004         technology->device_list = NULL;
1005
1006         technology->pending_reply = NULL;
1007
1008         technology_load(technology);
1009
1010         if (technology_dbus_register(technology) == FALSE) {
1011                 g_free(technology);
1012                 return NULL;
1013         }
1014
1015         technology_list = g_slist_prepend(technology_list, technology);
1016
1017         technology->driver = driver;
1018         err = driver->probe(technology);
1019         if (err != 0)
1020                 DBG("Driver probe failed for technology %p", technology);
1021
1022         DBG("technology %p", technology);
1023
1024         return technology;
1025 }
1026
1027 static void technology_dbus_unregister(struct connman_technology *technology)
1028 {
1029         if (technology->dbus_registered == FALSE)
1030                 return;
1031
1032         technology_removed_signal(technology);
1033         g_dbus_unregister_interface(connection, technology->path,
1034                 CONNMAN_TECHNOLOGY_INTERFACE);
1035
1036         technology->dbus_registered = FALSE;
1037 }
1038
1039 static void technology_put(struct connman_technology *technology)
1040 {
1041         DBG("technology %p", technology);
1042
1043         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1044                 return;
1045
1046         reply_scan_pending(technology, -EINTR);
1047
1048         if (technology->driver) {
1049                 technology->driver->remove(technology);
1050                 technology->driver = NULL;
1051         }
1052
1053         technology_list = g_slist_remove(technology_list, technology);
1054
1055         technology_dbus_unregister(technology);
1056
1057         g_slist_free(technology->device_list);
1058
1059         g_free(technology->path);
1060         g_free(technology->regdom);
1061         g_free(technology->tethering_ident);
1062         g_free(technology->tethering_passphrase);
1063         g_free(technology);
1064 }
1065
1066 void __connman_technology_add_interface(enum connman_service_type type,
1067                                 int index, const char *name, const char *ident)
1068 {
1069         struct connman_technology *technology;
1070
1071         switch (type) {
1072         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1073         case CONNMAN_SERVICE_TYPE_SYSTEM:
1074                 return;
1075         case CONNMAN_SERVICE_TYPE_ETHERNET:
1076         case CONNMAN_SERVICE_TYPE_WIFI:
1077         case CONNMAN_SERVICE_TYPE_WIMAX:
1078         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1079         case CONNMAN_SERVICE_TYPE_CELLULAR:
1080         case CONNMAN_SERVICE_TYPE_GPS:
1081         case CONNMAN_SERVICE_TYPE_VPN:
1082         case CONNMAN_SERVICE_TYPE_GADGET:
1083                 break;
1084         }
1085
1086         connman_info("Adding interface %s [ %s ]", name,
1087                                 __connman_service_type2string(type));
1088
1089         technology = technology_find(type);
1090
1091         if (technology == NULL || technology->driver == NULL
1092                         || technology->driver->add_interface == NULL)
1093                 return;
1094
1095         technology->driver->add_interface(technology,
1096                                         index, name, ident);
1097 }
1098
1099 void __connman_technology_remove_interface(enum connman_service_type type,
1100                                 int index, const char *name, const char *ident)
1101 {
1102         struct connman_technology *technology;
1103
1104         switch (type) {
1105         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1106         case CONNMAN_SERVICE_TYPE_SYSTEM:
1107                 return;
1108         case CONNMAN_SERVICE_TYPE_ETHERNET:
1109         case CONNMAN_SERVICE_TYPE_WIFI:
1110         case CONNMAN_SERVICE_TYPE_WIMAX:
1111         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1112         case CONNMAN_SERVICE_TYPE_CELLULAR:
1113         case CONNMAN_SERVICE_TYPE_GPS:
1114         case CONNMAN_SERVICE_TYPE_VPN:
1115         case CONNMAN_SERVICE_TYPE_GADGET:
1116                 break;
1117         }
1118
1119         connman_info("Remove interface %s [ %s ]", name,
1120                                 __connman_service_type2string(type));
1121
1122         technology = technology_find(type);
1123
1124         if (technology == NULL || technology->driver == NULL)
1125                 return;
1126
1127         if (technology->driver->remove_interface)
1128                 technology->driver->remove_interface(technology, index);
1129 }
1130
1131 int __connman_technology_add_device(struct connman_device *device)
1132 {
1133         struct connman_technology *technology;
1134         enum connman_service_type type;
1135
1136         DBG("device %p", device);
1137
1138         type = __connman_device_get_service_type(device);
1139
1140         technology = technology_get(type);
1141         if (technology == NULL) {
1142                 /*
1143                  * Since no driver can be found for this device at the moment we
1144                  * add it to the techless device list.
1145                 */
1146                 techless_device_list = g_slist_prepend(techless_device_list,
1147                                                                 device);
1148
1149                 return -ENXIO;
1150         }
1151
1152         if (technology->enable_persistent &&
1153                                         global_offlinemode == FALSE &&
1154                                         technology->hardblocked == FALSE) {
1155                 int err = __connman_device_enable(device);
1156                 /*
1157                  * connman_technology_add_device() calls __connman_device_enable()
1158                  * but since the device is already enabled, the calls does not
1159                  * propagate through to connman_technology_enabled via
1160                  * connman_device_set_powered.
1161                  */
1162                 if (err == -EALREADY)
1163                         __connman_technology_enabled(type);
1164         }
1165         /* if technology persistent state is offline or hardblocked */
1166         if (technology->enable_persistent == FALSE ||
1167                                         technology->hardblocked == TRUE)
1168                 __connman_device_disable(device);
1169
1170         technology->device_list = g_slist_prepend(technology->device_list,
1171                                                                 device);
1172
1173         return 0;
1174 }
1175
1176 int __connman_technology_remove_device(struct connman_device *device)
1177 {
1178         struct connman_technology *technology;
1179         enum connman_service_type type;
1180
1181         DBG("device %p", device);
1182
1183         type = __connman_device_get_service_type(device);
1184
1185         technology = technology_find(type);
1186         if (technology == NULL) {
1187                 techless_device_list = g_slist_remove(techless_device_list,
1188                                                                 device);
1189                 return -ENXIO;
1190         }
1191
1192         technology->device_list = g_slist_remove(technology->device_list,
1193                                                                 device);
1194         technology_put(technology);
1195
1196         return 0;
1197 }
1198
1199 static void powered_changed(struct connman_technology *technology)
1200 {
1201         connman_bool_t powered;
1202
1203         __sync_synchronize();
1204         if (technology->enabled >0)
1205                 powered = TRUE;
1206         else
1207                 powered = FALSE;
1208
1209         connman_dbus_property_changed_basic(technology->path,
1210                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1211                         DBUS_TYPE_BOOLEAN, &powered);
1212 }
1213
1214 int __connman_technology_enabled(enum connman_service_type type)
1215 {
1216         struct connman_technology *technology;
1217
1218         technology = technology_find(type);
1219         if (technology == NULL)
1220                 return -ENXIO;
1221
1222         if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1223                 return -EALREADY;
1224
1225         powered_changed(technology);
1226
1227         if (technology->pending_reply != NULL) {
1228                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1229                 dbus_message_unref(technology->pending_reply);
1230                 g_source_remove(technology->pending_timeout);
1231                 technology->pending_reply = NULL;
1232                 technology->pending_timeout = 0;
1233         }
1234
1235         return 0;
1236 }
1237
1238 int __connman_technology_disabled(enum connman_service_type type)
1239 {
1240         struct connman_technology *technology;
1241
1242         technology = technology_find(type);
1243         if (technology == NULL)
1244                 return -ENXIO;
1245
1246         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1247                 return -EINPROGRESS;
1248
1249         if (technology->pending_reply != NULL) {
1250                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1251                 dbus_message_unref(technology->pending_reply);
1252                 g_source_remove(technology->pending_timeout);
1253                 technology->pending_reply = NULL;
1254                 technology->pending_timeout = 0;
1255         }
1256
1257         powered_changed(technology);
1258
1259         return 0;
1260 }
1261
1262 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1263 {
1264         GSList *list;
1265         int err = -EINVAL;
1266
1267         if (global_offlinemode == offlinemode)
1268                 return 0;
1269
1270         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1271
1272         /*
1273          * This is a bit tricky. When you set offlinemode, there is no
1274          * way to differentiate between attempting offline mode and
1275          * resuming offlinemode from last saved profile. We need that
1276          * information in rfkill_update, otherwise it falls back on the
1277          * technology's persistent state. Hence we set the offline mode here
1278          * but save it & call the notifier only if its successful.
1279          */
1280
1281         global_offlinemode = offlinemode;
1282
1283         /* Traverse technology list, enable/disable each technology. */
1284         for (list = technology_list; list; list = list->next) {
1285                 struct connman_technology *technology = list->data;
1286
1287                 if (offlinemode)
1288                         err = technology_disable(technology, FALSE);
1289
1290                 if (!offlinemode && technology->enable_persistent)
1291                         err = technology_enable(technology, FALSE);
1292         }
1293
1294         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1295                 connman_technology_save_offlinemode();
1296                 __connman_notifier_offlinemode(offlinemode);
1297         } else
1298                 global_offlinemode = connman_technology_load_offlinemode();
1299
1300         return err;
1301 }
1302
1303 void __connman_technology_set_connected(enum connman_service_type type,
1304                 connman_bool_t connected)
1305 {
1306         struct connman_technology *technology;
1307
1308         technology = technology_find(type);
1309         if (technology == NULL)
1310                 return;
1311
1312         DBG("technology %p connected %d", technology, connected);
1313
1314         technology->connected = connected;
1315
1316         connman_dbus_property_changed_basic(technology->path,
1317                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1318                         DBUS_TYPE_BOOLEAN, &connected);
1319 }
1320
1321 static void technology_apply_hardblock_change(struct connman_technology *technology,
1322                                                 connman_bool_t hardblock)
1323 {
1324         gboolean apply = TRUE;
1325         GList *start, *list;
1326
1327         if (technology->hardblocked == hardblock)
1328                 return;
1329
1330         start = g_hash_table_get_values(rfkill_list);
1331         for (list = start; list != NULL; list = list->next) {
1332                 struct connman_rfkill *rfkill = list->data;
1333
1334                 if (rfkill->type != technology->type)
1335                         continue;
1336
1337                 if (rfkill->hardblock != hardblock)
1338                         apply = FALSE;
1339         }
1340
1341         g_list_free(start);
1342
1343         if (apply == FALSE)
1344                 return;
1345
1346         technology->hardblocked = hardblock;
1347
1348         if (hardblock == TRUE) {
1349                 DBG("%s is switched off.", get_name(technology->type));
1350                 technology_disable(technology, TRUE);
1351         } else
1352                 technology_enable(technology, TRUE);
1353
1354 }
1355
1356 int __connman_technology_add_rfkill(unsigned int index,
1357                                         enum connman_service_type type,
1358                                                 connman_bool_t softblock,
1359                                                 connman_bool_t hardblock)
1360 {
1361         struct connman_technology *technology;
1362         struct connman_rfkill *rfkill;
1363
1364         DBG("index %u type %d soft %u hard %u", index, type,
1365                                                         softblock, hardblock);
1366
1367         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1368         if (rfkill != NULL)
1369                 goto done;
1370
1371         rfkill = g_try_new0(struct connman_rfkill, 1);
1372         if (rfkill == NULL)
1373                 return -ENOMEM;
1374
1375         rfkill->index = index;
1376         rfkill->type = type;
1377         rfkill->softblock = softblock;
1378         rfkill->hardblock = hardblock;
1379
1380         g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1381
1382 done:
1383         technology = technology_get(type);
1384         /* If there is no driver for this type, ignore it. */
1385         if (technology == NULL)
1386                 return -ENXIO;
1387
1388         technology_apply_hardblock_change(technology, hardblock);
1389
1390         /*
1391          * If Offline mode is on, we softblock the device if it isnt already.
1392          * If Offline mode is off, we rely on the persistent state of tech.
1393          */
1394         if (global_offlinemode) {
1395                 if (!softblock)
1396                         return __connman_rfkill_block(type, TRUE);
1397         } else {
1398                 if (technology->enable_persistent && softblock)
1399                         return __connman_rfkill_block(type, FALSE);
1400                 /* if technology persistent state is offline */
1401                 if (!technology->enable_persistent && !softblock)
1402                         return __connman_rfkill_block(type, TRUE);
1403         }
1404
1405         return 0;
1406 }
1407
1408 int __connman_technology_update_rfkill(unsigned int index,
1409                                         enum connman_service_type type,
1410                                                 connman_bool_t softblock,
1411                                                 connman_bool_t hardblock)
1412 {
1413         struct connman_technology *technology;
1414         struct connman_rfkill *rfkill;
1415
1416         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1417
1418         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1419         if (rfkill == NULL)
1420                 return -ENXIO;
1421
1422         if (rfkill->softblock == softblock &&
1423                 rfkill->hardblock == hardblock)
1424                 return 0;
1425
1426         rfkill->softblock = softblock;
1427         rfkill->hardblock = hardblock;
1428
1429         technology = technology_find(type);
1430         /* If there is no driver for this type, ignore it. */
1431         if (technology == NULL)
1432                 return -ENXIO;
1433
1434         technology_apply_hardblock_change(technology, hardblock);
1435
1436         if (!global_offlinemode) {
1437                 if (technology->enable_persistent && softblock)
1438                         return __connman_rfkill_block(type, FALSE);
1439                 if (!technology->enable_persistent && !softblock)
1440                         return __connman_rfkill_block(type, TRUE);
1441         }
1442
1443         return 0;
1444 }
1445
1446 int __connman_technology_remove_rfkill(unsigned int index,
1447                                         enum connman_service_type type)
1448 {
1449         struct connman_technology *technology;
1450         struct connman_rfkill *rfkill;
1451
1452         DBG("index %u", index);
1453
1454         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1455         if (rfkill == NULL)
1456                 return -ENXIO;
1457
1458         g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1459
1460         technology = technology_find(type);
1461         if (technology == NULL)
1462                 return -ENXIO;
1463
1464         technology_put(technology);
1465
1466         return 0;
1467 }
1468
1469 int __connman_technology_init(void)
1470 {
1471         DBG("");
1472
1473         connection = connman_dbus_get_connection();
1474
1475         rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1476                                                         NULL, free_rfkill);
1477
1478         global_offlinemode = connman_technology_load_offlinemode();
1479
1480         /* This will create settings file if it is missing */
1481         connman_technology_save_offlinemode();
1482
1483         return 0;
1484 }
1485
1486 void __connman_technology_cleanup(void)
1487 {
1488         DBG("");
1489
1490         g_hash_table_destroy(rfkill_list);
1491
1492         dbus_connection_unref(connection);
1493 }