technology: Enable/disable individual devices also with rfkill
[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         connman_bool_t 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 rfkill_driven;
77         connman_bool_t softblocked;
78         connman_bool_t hardblocked;
79         connman_bool_t dbus_registered;
80 };
81
82 static GSList *driver_list = NULL;
83
84 static gint compare_priority(gconstpointer a, gconstpointer b)
85 {
86         const struct connman_technology_driver *driver1 = a;
87         const struct connman_technology_driver *driver2 = b;
88
89         return driver2->priority - driver1->priority;
90 }
91
92 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
93 {
94         struct connman_rfkill *rfkill = value;
95         enum connman_service_type type = GPOINTER_TO_INT(user_data);
96
97         /* Calling _technology_rfkill_add will update the tech. */
98         if (rfkill->type == type)
99                 __connman_technology_add_rfkill(rfkill->index, type,
100                                 rfkill->softblock, rfkill->hardblock);
101 }
102
103 /**
104  * connman_technology_driver_register:
105  * @driver: Technology driver definition
106  *
107  * Register a new technology driver
108  *
109  * Returns: %0 on success
110  */
111 int connman_technology_driver_register(struct connman_technology_driver *driver)
112 {
113         GSList *list;
114         struct connman_device *device;
115         enum connman_service_type type;
116
117         DBG("Registering %s driver", driver->name);
118
119         driver_list = g_slist_insert_sorted(driver_list, driver,
120                                                         compare_priority);
121
122         /*
123          * Check for technology less devices if this driver
124          * can service any of them.
125         */
126         for (list = techless_device_list; list != NULL; list = list->next) {
127                 device = list->data;
128
129                 type = __connman_device_get_service_type(device);
130                 if (type != driver->type)
131                         continue;
132
133                 techless_device_list = g_slist_remove(techless_device_list,
134                                                                 device);
135
136                 __connman_technology_add_device(device);
137         }
138
139         /* Check for orphaned rfkill switches. */
140         g_hash_table_foreach(rfkill_list, rfkill_check,
141                                         GINT_TO_POINTER(driver->type));
142
143         return 0;
144 }
145
146 /**
147  * connman_technology_driver_unregister:
148  * @driver: Technology driver definition
149  *
150  * Remove a previously registered technology driver
151  */
152 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
153 {
154         GSList *list;
155         struct connman_technology *technology;
156
157         DBG("Unregistering driver %p name %s", driver, driver->name);
158
159         for (list = technology_list; list; list = list->next) {
160                 technology = list->data;
161
162                 if (technology->driver == NULL)
163                         continue;
164
165                 if (technology->driver == driver) {
166                         if (driver->remove != NULL)
167                                 driver->remove(technology);
168                         technology->driver = NULL;
169                         break;
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         __sync_synchronize();
225         if (technology->enabled == FALSE)
226                 return -EACCES;
227
228         bridge = __connman_tethering_get_bridge();
229         if (bridge == NULL)
230                 return -EOPNOTSUPP;
231
232         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
233             (ident == NULL || passphrase == NULL))
234                 return -EINVAL;
235
236         return technology->driver->set_tethering(technology, ident, passphrase,
237                                                         bridge, enabled);
238 }
239
240 void connman_technology_regdom_notify(struct connman_technology *technology,
241                                                         const char *alpha2)
242 {
243         DBG("");
244
245         if (alpha2 == NULL)
246                 connman_error("Failed to set regulatory domain");
247         else
248                 DBG("Regulatory domain set to %s", alpha2);
249
250         g_free(technology->regdom);
251         technology->regdom = g_strdup(alpha2);
252 }
253
254 static int set_regdom_by_device(struct connman_technology *technology,
255                                                         const char *alpha2)
256 {
257         GSList *list;
258
259         for (list = technology->device_list; list; list = list->next) {
260                 struct connman_device *device = list->data;
261
262                 if (connman_device_set_regdom(device, alpha2) != 0)
263                         return -ENOTSUP;
264         }
265
266         return 0;
267 }
268
269 int connman_technology_set_regdom(const char *alpha2)
270 {
271         GSList *list;
272
273         for (list = technology_list; list; list = list->next) {
274                 struct connman_technology *technology = list->data;
275
276                 if (set_regdom_by_device(technology, alpha2) != 0) {
277                         if (technology->driver == NULL)
278                                 continue;
279
280                         if (technology->driver->set_regdom != NULL)
281                                 technology->driver->set_regdom(technology,
282                                                                 alpha2);
283                 }
284         }
285
286         return 0;
287 }
288
289 static void free_rfkill(gpointer data)
290 {
291         struct connman_rfkill *rfkill = data;
292
293         g_free(rfkill);
294 }
295
296 static const char *get_name(enum connman_service_type type)
297 {
298         switch (type) {
299         case CONNMAN_SERVICE_TYPE_UNKNOWN:
300         case CONNMAN_SERVICE_TYPE_SYSTEM:
301         case CONNMAN_SERVICE_TYPE_GPS:
302         case CONNMAN_SERVICE_TYPE_VPN:
303         case CONNMAN_SERVICE_TYPE_GADGET:
304                 break;
305         case CONNMAN_SERVICE_TYPE_ETHERNET:
306                 return "Wired";
307         case CONNMAN_SERVICE_TYPE_WIFI:
308                 return "WiFi";
309         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
310                 return "Bluetooth";
311         case CONNMAN_SERVICE_TYPE_CELLULAR:
312                 return "Cellular";
313         }
314
315         return NULL;
316 }
317
318 static void technology_save(struct connman_technology *technology)
319 {
320         GKeyFile *keyfile;
321         gchar *identifier;
322
323         DBG("technology %p", technology);
324
325         keyfile = __connman_storage_load_global();
326         if (keyfile == NULL)
327                 keyfile = g_key_file_new();
328
329         identifier = g_strdup_printf("%s", get_name(technology->type));
330         if (identifier == NULL)
331                 goto done;
332
333         g_key_file_set_boolean(keyfile, identifier, "Enable",
334                                 technology->enable_persistent);
335
336         if (technology->tethering_ident != NULL)
337                 g_key_file_set_string(keyfile, identifier,
338                                         "Tethering.Identifier",
339                                         technology->tethering_ident);
340
341         if (technology->tethering_passphrase != NULL)
342                 g_key_file_set_string(keyfile, identifier,
343                                         "Tethering.Passphrase",
344                                         technology->tethering_passphrase);
345
346 done:
347         g_free(identifier);
348
349         __connman_storage_save_global(keyfile);
350
351         g_key_file_free(keyfile);
352
353         return;
354 }
355
356 static void technology_load(struct connman_technology *technology)
357 {
358         GKeyFile *keyfile;
359         gchar *identifier;
360         GError *error = NULL;
361         connman_bool_t enable;
362
363         DBG("technology %p", technology);
364
365         keyfile = __connman_storage_load_global();
366         /* Fallback on disabling technology if file not found. */
367         if (keyfile == NULL) {
368                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
369                         /* We enable ethernet by default */
370                         technology->enable_persistent = TRUE;
371                 else
372                         technology->enable_persistent = FALSE;
373                 return;
374         }
375
376         identifier = g_strdup_printf("%s", get_name(technology->type));
377         if (identifier == NULL)
378                 goto done;
379
380         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
381         if (error == NULL)
382                 technology->enable_persistent = enable;
383         else {
384                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
385                         technology->enable_persistent = TRUE;
386                 else
387                         technology->enable_persistent = FALSE;
388
389                 technology_save(technology);
390                 g_clear_error(&error);
391         }
392
393         technology->tethering_ident = g_key_file_get_string(keyfile,
394                                 identifier, "Tethering.Identifier", NULL);
395
396         technology->tethering_passphrase = g_key_file_get_string(keyfile,
397                                 identifier, "Tethering.Passphrase", NULL);
398 done:
399         g_free(identifier);
400
401         g_key_file_free(keyfile);
402
403         return;
404 }
405
406 connman_bool_t __connman_technology_get_offlinemode(void)
407 {
408         return global_offlinemode;
409 }
410
411 static void connman_technology_save_offlinemode()
412 {
413         GKeyFile *keyfile;
414
415         keyfile = __connman_storage_load_global();
416         if (keyfile == NULL)
417                 keyfile = g_key_file_new();
418
419         g_key_file_set_boolean(keyfile, "global",
420                                         "OfflineMode", global_offlinemode);
421
422         __connman_storage_save_global(keyfile);
423
424         g_key_file_free(keyfile);
425
426         return;
427 }
428
429 static connman_bool_t connman_technology_load_offlinemode()
430 {
431         GKeyFile *keyfile;
432         GError *error = NULL;
433         connman_bool_t offlinemode;
434
435         /* If there is a error, we enable offlinemode */
436         keyfile = __connman_storage_load_global();
437         if (keyfile == NULL)
438                 return FALSE;
439
440         offlinemode = g_key_file_get_boolean(keyfile, "global",
441                                                 "OfflineMode", &error);
442         if (error != NULL) {
443                 offlinemode = FALSE;
444                 g_clear_error(&error);
445         }
446
447         g_key_file_free(keyfile);
448
449         return offlinemode;
450 }
451
452 static void append_properties(DBusMessageIter *iter,
453                 struct connman_technology *technology)
454 {
455         DBusMessageIter dict;
456         const char *str;
457
458         connman_dbus_dict_open(iter, &dict);
459
460         str = get_name(technology->type);
461         if (str != NULL)
462                 connman_dbus_dict_append_basic(&dict, "Name",
463                                                 DBUS_TYPE_STRING, &str);
464
465         str = __connman_service_type2string(technology->type);
466         if (str != NULL)
467                 connman_dbus_dict_append_basic(&dict, "Type",
468                                                 DBUS_TYPE_STRING, &str);
469
470         __sync_synchronize();
471         connman_dbus_dict_append_basic(&dict, "Powered",
472                                         DBUS_TYPE_BOOLEAN,
473                                         &technology->enabled);
474
475         connman_dbus_dict_append_basic(&dict, "Connected",
476                                         DBUS_TYPE_BOOLEAN,
477                                         &technology->connected);
478
479         connman_dbus_dict_append_basic(&dict, "Tethering",
480                                         DBUS_TYPE_BOOLEAN,
481                                         &technology->tethering);
482
483         if (technology->tethering_ident != NULL)
484                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
485                                         DBUS_TYPE_STRING,
486                                         &technology->tethering_ident);
487
488         if (technology->tethering_passphrase != NULL)
489                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
490                                         DBUS_TYPE_STRING,
491                                         &technology->tethering_passphrase);
492
493         connman_dbus_dict_close(iter, &dict);
494 }
495
496 static void technology_added_signal(struct connman_technology *technology)
497 {
498         DBusMessage *signal;
499         DBusMessageIter iter;
500
501         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
502                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
503         if (signal == NULL)
504                 return;
505
506         dbus_message_iter_init_append(signal, &iter);
507         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
508                                                         &technology->path);
509         append_properties(&iter, technology);
510
511         dbus_connection_send(connection, signal, NULL);
512         dbus_message_unref(signal);
513 }
514
515 static void technology_removed_signal(struct connman_technology *technology)
516 {
517         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
518                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
519                         DBUS_TYPE_OBJECT_PATH, &technology->path,
520                         DBUS_TYPE_INVALID);
521 }
522
523 static DBusMessage *get_properties(DBusConnection *conn,
524                                         DBusMessage *message, void *user_data)
525 {
526         struct connman_technology *technology = user_data;
527         DBusMessage *reply;
528         DBusMessageIter iter;
529
530         reply = dbus_message_new_method_return(message);
531         if (reply == NULL)
532                 return NULL;
533
534         dbus_message_iter_init_append(reply, &iter);
535         append_properties(&iter, technology);
536
537         return reply;
538 }
539
540 void __connman_technology_list_struct(DBusMessageIter *array)
541 {
542         GSList *list;
543         DBusMessageIter entry;
544
545         for (list = technology_list; list; list = list->next) {
546                 struct connman_technology *technology = list->data;
547
548                 if (technology->path == NULL ||
549                                 (technology->rfkill_driven == TRUE &&
550                                  technology->hardblocked == TRUE))
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_affect_devices(struct connman_technology *technology,
582                                                 connman_bool_t enable_device)
583 {
584         GSList *list;
585         int err = 0;
586
587         for (list = technology->device_list; list; list = list->next) {
588                 struct connman_device *device = list->data;
589
590                 if (enable_device == TRUE)
591                         err = __connman_device_enable(device);
592                 else
593                         err = __connman_device_disable(device);
594         }
595
596         return err;
597 }
598
599 static int technology_enable(struct connman_technology *technology)
600 {
601         int err = 0;
602         int err_dev;
603
604         DBG("technology %p enable", technology);
605
606         __sync_synchronize();
607         if (technology->enabled == TRUE)
608                 return -EALREADY;
609
610         if (technology->pending_reply != NULL)
611                 return -EBUSY;
612
613         if (technology->rfkill_driven == TRUE)
614                 err = __connman_rfkill_block(technology->type, FALSE);
615
616         err_dev = technology_affect_devices(technology, TRUE);
617
618         if (technology->rfkill_driven == FALSE)
619                 err = err_dev;
620
621         return err;
622 }
623
624 static int technology_disable(struct connman_technology *technology)
625 {
626         int err;
627
628         DBG("technology %p disable", technology);
629
630         __sync_synchronize();
631         if (technology->enabled == FALSE)
632                 return -EALREADY;
633
634         if (technology->pending_reply != NULL)
635                 return -EBUSY;
636
637         if (technology->tethering == TRUE)
638                 set_tethering(technology, FALSE);
639
640         err = technology_affect_devices(technology, FALSE);
641
642         if (technology->rfkill_driven == TRUE)
643                 err = __connman_rfkill_block(technology->type, TRUE);
644
645         return err;
646 }
647
648 static DBusMessage *set_powered(struct connman_technology *technology,
649                                 DBusMessage *msg, connman_bool_t powered)
650 {
651         DBusMessage *reply = NULL;
652         int err = 0;
653
654         if (technology->rfkill_driven && technology->hardblocked == TRUE) {
655                 err = -EACCES;
656                 goto make_reply;
657         }
658
659         if (powered == TRUE)
660                 err = technology_enable(technology);
661         else
662                 err = technology_disable(technology);
663
664         if (err != -EBUSY) {
665                 technology->enable_persistent = powered;
666                 technology_save(technology);
667         }
668
669 make_reply:
670         if (err == -EINPROGRESS) {
671                 technology->pending_reply = dbus_message_ref(msg);
672                 technology->pending_timeout = g_timeout_add_seconds(10,
673                                         technology_pending_reply, technology);
674         } else if (err == -EALREADY) {
675                 if (powered == TRUE)
676                         reply = __connman_error_already_enabled(msg);
677                 else
678                         reply = __connman_error_already_disabled(msg);
679         } else if (err < 0)
680                 reply = __connman_error_failed(msg, -err);
681         else
682                 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
683
684         return reply;
685 }
686
687 static DBusMessage *set_property(DBusConnection *conn,
688                                         DBusMessage *msg, void *data)
689 {
690         struct connman_technology *technology = data;
691         DBusMessageIter iter, value;
692         const char *name;
693         int type;
694
695         DBG("conn %p", conn);
696
697         if (dbus_message_iter_init(msg, &iter) == FALSE)
698                 return __connman_error_invalid_arguments(msg);
699
700         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
701                 return __connman_error_invalid_arguments(msg);
702
703         dbus_message_iter_get_basic(&iter, &name);
704         dbus_message_iter_next(&iter);
705
706         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
707                 return __connman_error_invalid_arguments(msg);
708
709         dbus_message_iter_recurse(&iter, &value);
710
711         type = dbus_message_iter_get_arg_type(&value);
712
713         DBG("property %s", name);
714
715         if (g_str_equal(name, "Tethering") == TRUE) {
716                 int err;
717                 connman_bool_t tethering;
718
719                 if (type != DBUS_TYPE_BOOLEAN)
720                         return __connman_error_invalid_arguments(msg);
721
722                 dbus_message_iter_get_basic(&value, &tethering);
723
724                 if (technology->tethering == tethering) {
725                         if (tethering == FALSE)
726                                 return __connman_error_already_disabled(msg);
727                         else
728                                 return __connman_error_already_enabled(msg);
729                 }
730
731                 err = set_tethering(technology, tethering);
732                 if (err < 0)
733                         return __connman_error_failed(msg, -err);
734
735         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
736                 const char *str;
737
738                 dbus_message_iter_get_basic(&value, &str);
739
740                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
741                         return __connman_error_not_supported(msg);
742
743                 if (strlen(str) < 1 || strlen(str) > 32)
744                         return __connman_error_invalid_arguments(msg);
745
746                 if (g_strcmp0(technology->tethering_ident, str) != 0) {
747                         g_free(technology->tethering_ident);
748                         technology->tethering_ident = g_strdup(str);
749                         technology_save(technology);
750
751                         connman_dbus_property_changed_basic(technology->path,
752                                                 CONNMAN_TECHNOLOGY_INTERFACE,
753                                                 "TetheringIdentifier",
754                                                 DBUS_TYPE_STRING,
755                                                 &technology->tethering_ident);
756                 }
757         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
758                 const char *str;
759
760                 dbus_message_iter_get_basic(&value, &str);
761
762                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
763                         return __connman_error_not_supported(msg);
764
765                 if (strlen(str) < 8 || strlen(str) > 63)
766                         return __connman_error_passphrase_required(msg);
767
768                 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
769                         g_free(technology->tethering_passphrase);
770                         technology->tethering_passphrase = g_strdup(str);
771                         technology_save(technology);
772
773                         connman_dbus_property_changed_basic(technology->path,
774                                         CONNMAN_TECHNOLOGY_INTERFACE,
775                                         "TetheringPassphrase",
776                                         DBUS_TYPE_STRING,
777                                         &technology->tethering_passphrase);
778                 }
779         } else if (g_str_equal(name, "Powered") == TRUE) {
780                 connman_bool_t enable;
781
782                 if (type != DBUS_TYPE_BOOLEAN)
783                         return __connman_error_invalid_arguments(msg);
784
785                 dbus_message_iter_get_basic(&value, &enable);
786
787                 return set_powered(technology, msg, enable);
788         } else
789                 return __connman_error_invalid_property(msg);
790
791         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
792 }
793
794 static struct connman_technology *technology_find(enum connman_service_type type)
795 {
796         GSList *list;
797
798         DBG("type %d", type);
799
800         for (list = technology_list; list; list = list->next) {
801                 struct connman_technology *technology = list->data;
802
803                 if (technology->type == type)
804                         return technology;
805         }
806
807         return NULL;
808 }
809
810 static void reply_scan_pending(struct connman_technology *technology, int err)
811 {
812         DBusMessage *reply;
813
814         DBG("technology %p err %d", technology, err);
815
816         while (technology->scan_pending != NULL) {
817                 DBusMessage *msg = technology->scan_pending->data;
818
819                 DBG("reply to %s", dbus_message_get_sender(msg));
820
821                 if (err == 0)
822                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
823                 else
824                         reply = __connman_error_failed(msg, -err);
825                 g_dbus_send_message(connection, reply);
826                 dbus_message_unref(msg);
827
828                 technology->scan_pending =
829                         g_slist_delete_link(technology->scan_pending,
830                                         technology->scan_pending);
831         }
832 }
833
834 void __connman_technology_scan_started(struct connman_device *device)
835 {
836         DBG("device %p", device);
837 }
838
839 void __connman_technology_scan_stopped(struct connman_device *device)
840 {
841         int count = 0;
842         struct connman_technology *technology;
843         enum connman_service_type type;
844         GSList *list;
845
846         type = __connman_device_get_service_type(device);
847         technology = technology_find(type);
848
849         DBG("technology %p device %p", technology, device);
850
851         if (technology == NULL)
852                 return;
853
854         for (list = technology->device_list; list != NULL; list = list->next) {
855                 struct connman_device *other_device = list->data;
856
857                 if (device == other_device)
858                         continue;
859
860                 if (__connman_device_get_service_type(other_device) != type)
861                         continue;
862
863                 if (connman_device_get_scanning(other_device) == TRUE)
864                         count += 1;
865         }
866
867         if (count == 0)
868                 reply_scan_pending(technology, 0);
869 }
870
871 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
872                                                 int result, const char *alpha2)
873 {
874         struct connman_technology *technology;
875         enum connman_service_type type;
876
877         type = __connman_device_get_service_type(device);
878         technology = technology_find(type);
879
880         if (technology == NULL)
881                 return;
882
883         if (result < 0) {
884                 if (technology->driver != NULL &&
885                                 technology->driver->set_regdom != NULL) {
886                         technology->driver->set_regdom(technology, alpha2);
887                         return;
888                 }
889
890                 alpha2 = NULL;
891         }
892
893         connman_technology_regdom_notify(technology, alpha2);
894 }
895
896 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
897 {
898         struct connman_technology *technology = data;
899         int err;
900
901         DBG ("technology %p request from %s", technology,
902                         dbus_message_get_sender(msg));
903
904         dbus_message_ref(msg);
905         technology->scan_pending =
906                 g_slist_prepend(technology->scan_pending, msg);
907
908         err = __connman_device_request_scan(technology->type);
909         if (err < 0)
910                 reply_scan_pending(technology, err);
911
912         return NULL;
913 }
914
915 static const GDBusMethodTable technology_methods[] = {
916         { GDBUS_DEPRECATED_METHOD("GetProperties",
917                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
918                         get_properties) },
919         { GDBUS_ASYNC_METHOD("SetProperty",
920                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
921                         NULL, set_property) },
922         { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
923         { },
924 };
925
926 static const GDBusSignalTable technology_signals[] = {
927         { GDBUS_SIGNAL("PropertyChanged",
928                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
929         { },
930 };
931
932 static gboolean technology_dbus_register(struct connman_technology *technology)
933 {
934         if (technology->dbus_registered == TRUE ||
935                                 (technology->rfkill_driven == TRUE &&
936                                  technology->hardblocked == 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         technology->rfkill_driven = FALSE;
996         technology->softblocked = FALSE;
997         technology->hardblocked = FALSE;
998
999         technology->type = type;
1000         technology->path = g_strdup_printf("%s/technology/%s",
1001                                                         CONNMAN_PATH, str);
1002
1003         technology->device_list = NULL;
1004
1005         technology->pending_reply = NULL;
1006
1007         technology_load(technology);
1008
1009         if (technology_dbus_register(technology) == FALSE) {
1010                 g_free(technology);
1011                 return NULL;
1012         }
1013
1014         technology_list = g_slist_prepend(technology_list, technology);
1015
1016         technology->driver = driver;
1017         if (driver->probe != NULL)
1018                 err = driver->probe(technology);
1019         else
1020                 err = 0;
1021
1022         if (err != 0)
1023                 DBG("Driver probe failed for technology %p", technology);
1024
1025         DBG("technology %p", technology);
1026
1027         return technology;
1028 }
1029
1030 static void technology_dbus_unregister(struct connman_technology *technology)
1031 {
1032         if (technology->dbus_registered == FALSE)
1033                 return;
1034
1035         technology_removed_signal(technology);
1036         g_dbus_unregister_interface(connection, technology->path,
1037                 CONNMAN_TECHNOLOGY_INTERFACE);
1038
1039         technology->dbus_registered = FALSE;
1040 }
1041
1042 static void technology_put(struct connman_technology *technology)
1043 {
1044         DBG("technology %p", technology);
1045
1046         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1047                 return;
1048
1049         reply_scan_pending(technology, -EINTR);
1050
1051         if (technology->driver) {
1052                 technology->driver->remove(technology);
1053                 technology->driver = NULL;
1054         }
1055
1056         technology_list = g_slist_remove(technology_list, technology);
1057
1058         technology_dbus_unregister(technology);
1059
1060         g_slist_free(technology->device_list);
1061
1062         g_free(technology->path);
1063         g_free(technology->regdom);
1064         g_free(technology->tethering_ident);
1065         g_free(technology->tethering_passphrase);
1066         g_free(technology);
1067 }
1068
1069 void __connman_technology_add_interface(enum connman_service_type type,
1070                                 int index, const char *name, const char *ident)
1071 {
1072         struct connman_technology *technology;
1073
1074         switch (type) {
1075         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1076         case CONNMAN_SERVICE_TYPE_SYSTEM:
1077                 return;
1078         case CONNMAN_SERVICE_TYPE_ETHERNET:
1079         case CONNMAN_SERVICE_TYPE_WIFI:
1080         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1081         case CONNMAN_SERVICE_TYPE_CELLULAR:
1082         case CONNMAN_SERVICE_TYPE_GPS:
1083         case CONNMAN_SERVICE_TYPE_VPN:
1084         case CONNMAN_SERVICE_TYPE_GADGET:
1085                 break;
1086         }
1087
1088         connman_info("Adding interface %s [ %s ]", name,
1089                                 __connman_service_type2string(type));
1090
1091         technology = technology_find(type);
1092
1093         if (technology == NULL || technology->driver == NULL
1094                         || technology->driver->add_interface == NULL)
1095                 return;
1096
1097         technology->driver->add_interface(technology,
1098                                         index, name, ident);
1099 }
1100
1101 void __connman_technology_remove_interface(enum connman_service_type type,
1102                                 int index, const char *name, const char *ident)
1103 {
1104         struct connman_technology *technology;
1105
1106         switch (type) {
1107         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1108         case CONNMAN_SERVICE_TYPE_SYSTEM:
1109                 return;
1110         case CONNMAN_SERVICE_TYPE_ETHERNET:
1111         case CONNMAN_SERVICE_TYPE_WIFI:
1112         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1113         case CONNMAN_SERVICE_TYPE_CELLULAR:
1114         case CONNMAN_SERVICE_TYPE_GPS:
1115         case CONNMAN_SERVICE_TYPE_VPN:
1116         case CONNMAN_SERVICE_TYPE_GADGET:
1117                 break;
1118         }
1119
1120         connman_info("Remove interface %s [ %s ]", name,
1121                                 __connman_service_type2string(type));
1122
1123         technology = technology_find(type);
1124
1125         if (technology == NULL || technology->driver == NULL)
1126                 return;
1127
1128         if (technology->driver->remove_interface)
1129                 technology->driver->remove_interface(technology, index);
1130 }
1131
1132 int __connman_technology_add_device(struct connman_device *device)
1133 {
1134         struct connman_technology *technology;
1135         enum connman_service_type type;
1136
1137         DBG("device %p", device);
1138
1139         type = __connman_device_get_service_type(device);
1140
1141         technology = technology_get(type);
1142         if (technology == NULL) {
1143                 /*
1144                  * Since no driver can be found for this device at the moment we
1145                  * add it to the techless device list.
1146                 */
1147                 techless_device_list = g_slist_prepend(techless_device_list,
1148                                                                 device);
1149
1150                 return -ENXIO;
1151         }
1152
1153         __sync_synchronize();
1154         if (technology->rfkill_driven == TRUE) {
1155                 if (technology->enabled == TRUE)
1156                         __connman_device_enable(device);
1157                 else
1158                         __connman_device_disable(device);
1159
1160                 goto done;
1161         }
1162
1163         if (technology->enable_persistent == TRUE &&
1164                                         global_offlinemode == FALSE) {
1165                 int err = __connman_device_enable(device);
1166                 /*
1167                  * connman_technology_add_device() calls __connman_device_enable()
1168                  * but since the device is already enabled, the calls does not
1169                  * propagate through to connman_technology_enabled via
1170                  * connman_device_set_powered.
1171                  */
1172                 if (err == -EALREADY)
1173                         __connman_technology_enabled(type);
1174         }
1175         /* if technology persistent state is offline */
1176         if (technology->enable_persistent == FALSE)
1177                 __connman_device_disable(device);
1178
1179 done:
1180         technology->device_list = g_slist_prepend(technology->device_list,
1181                                                                 device);
1182
1183         return 0;
1184 }
1185
1186 int __connman_technology_remove_device(struct connman_device *device)
1187 {
1188         struct connman_technology *technology;
1189         enum connman_service_type type;
1190
1191         DBG("device %p", device);
1192
1193         type = __connman_device_get_service_type(device);
1194
1195         technology = technology_find(type);
1196         if (technology == NULL) {
1197                 techless_device_list = g_slist_remove(techless_device_list,
1198                                                                 device);
1199                 return -ENXIO;
1200         }
1201
1202         technology->device_list = g_slist_remove(technology->device_list,
1203                                                                 device);
1204         technology_put(technology);
1205
1206         return 0;
1207 }
1208
1209 static void powered_changed(struct connman_technology *technology)
1210 {
1211         if (technology->dbus_registered == FALSE)
1212                 return;
1213
1214         if (technology->pending_reply != NULL) {
1215                 g_dbus_send_reply(connection,
1216                                 technology->pending_reply, DBUS_TYPE_INVALID);
1217                 dbus_message_unref(technology->pending_reply);
1218                 technology->pending_reply = NULL;
1219
1220                 g_source_remove(technology->pending_timeout);
1221                 technology->pending_timeout = 0;
1222         }
1223
1224         __sync_synchronize();
1225         connman_dbus_property_changed_basic(technology->path,
1226                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1227                         DBUS_TYPE_BOOLEAN, &technology->enabled);
1228 }
1229
1230 static int technology_enabled(struct connman_technology *technology)
1231 {
1232         __sync_synchronize();
1233         if (technology->enabled == TRUE)
1234                 return -EALREADY;
1235
1236         technology->enabled = TRUE;
1237
1238         powered_changed(technology);
1239
1240         return 0;
1241 }
1242
1243 int __connman_technology_enabled(enum connman_service_type type)
1244 {
1245         struct connman_technology *technology;
1246
1247         technology = technology_find(type);
1248         if (technology == NULL)
1249                 return -ENXIO;
1250
1251         if (technology->rfkill_driven == TRUE)
1252                 return 0;
1253
1254         return technology_enabled(technology);
1255 }
1256
1257 static int technology_disabled(struct connman_technology *technology)
1258 {
1259         __sync_synchronize();
1260         if (technology->enabled == FALSE)
1261                 return -EALREADY;
1262
1263         technology->enabled = FALSE;
1264
1265         powered_changed(technology);
1266
1267         return 0;
1268 }
1269
1270 int __connman_technology_disabled(enum connman_service_type type)
1271 {
1272         struct connman_technology *technology;
1273         GSList *list;
1274
1275         technology = technology_find(type);
1276         if (technology == NULL)
1277                 return -ENXIO;
1278
1279         if (technology->rfkill_driven == TRUE)
1280                 return 0;
1281
1282         for (list = technology->device_list; list != NULL; list = list->next) {
1283                 struct connman_device *device = list->data;
1284
1285                 if (connman_device_get_powered(device) == TRUE)
1286                         return 0;
1287         }
1288
1289         return technology_disabled(technology);
1290 }
1291
1292 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1293 {
1294         GSList *list;
1295         int err = -EINVAL;
1296
1297         if (global_offlinemode == offlinemode)
1298                 return 0;
1299
1300         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1301
1302         /*
1303          * This is a bit tricky. When you set offlinemode, there is no
1304          * way to differentiate between attempting offline mode and
1305          * resuming offlinemode from last saved profile. We need that
1306          * information in rfkill_update, otherwise it falls back on the
1307          * technology's persistent state. Hence we set the offline mode here
1308          * but save it & call the notifier only if its successful.
1309          */
1310
1311         global_offlinemode = offlinemode;
1312
1313         /* Traverse technology list, enable/disable each technology. */
1314         for (list = technology_list; list; list = list->next) {
1315                 struct connman_technology *technology = list->data;
1316
1317                 if (offlinemode)
1318                         err = technology_disable(technology);
1319
1320                 if (!offlinemode && technology->enable_persistent)
1321                         err = technology_enable(technology);
1322         }
1323
1324         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1325                 connman_technology_save_offlinemode();
1326                 __connman_notifier_offlinemode(offlinemode);
1327         } else
1328                 global_offlinemode = connman_technology_load_offlinemode();
1329
1330         return err;
1331 }
1332
1333 void __connman_technology_set_connected(enum connman_service_type type,
1334                 connman_bool_t connected)
1335 {
1336         struct connman_technology *technology;
1337
1338         technology = technology_find(type);
1339         if (technology == NULL)
1340                 return;
1341
1342         DBG("technology %p connected %d", technology, connected);
1343
1344         technology->connected = connected;
1345
1346         connman_dbus_property_changed_basic(technology->path,
1347                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1348                         DBUS_TYPE_BOOLEAN, &connected);
1349 }
1350
1351 static connman_bool_t technology_apply_rfkill_change(struct connman_technology *technology,
1352                                                 connman_bool_t softblock,
1353                                                 connman_bool_t hardblock,
1354                                                 connman_bool_t new_rfkill)
1355 {
1356         gboolean hardblock_changed = FALSE;
1357         gboolean apply = TRUE;
1358         GList *start, *list;
1359
1360         DBG("technology %p --> %d/%d vs %d/%d",
1361                         technology, softblock, hardblock,
1362                         technology->softblocked, technology->hardblocked);
1363
1364         if (technology->hardblocked == hardblock)
1365                 goto softblock_change;
1366
1367         if (!(new_rfkill == TRUE && hardblock == FALSE)) {
1368                 start = g_hash_table_get_values(rfkill_list);
1369
1370                 for (list = start; list != NULL; list = list->next) {
1371                         struct connman_rfkill *rfkill = list->data;
1372
1373                         if (rfkill->type != technology->type)
1374                                 continue;
1375
1376                         if (rfkill->hardblock != hardblock)
1377                                 apply = FALSE;
1378                 }
1379
1380                 g_list_free(start);
1381         }
1382
1383         if (apply == FALSE)
1384                 goto softblock_change;
1385
1386         technology->hardblocked = hardblock;
1387         hardblock_changed = TRUE;
1388
1389 softblock_change:
1390         if (apply == FALSE && technology->softblocked != softblock)
1391                 apply = TRUE;
1392
1393         if (apply == FALSE)
1394                 return technology->hardblocked;
1395
1396         technology->softblocked = softblock;
1397
1398         if (technology->hardblocked == TRUE ||
1399                                         technology->softblocked == TRUE) {
1400                 if (technology_disabled(technology) != -EALREADY)
1401                         technology_affect_devices(technology, FALSE);
1402         } else if (technology->hardblocked == FALSE &&
1403                                         technology->softblocked == FALSE) {
1404                 if (technology_enabled(technology) != -EALREADY)
1405                         technology_affect_devices(technology, TRUE);
1406         }
1407
1408         if (hardblock_changed == TRUE) {
1409                 if (technology->hardblocked == TRUE) {
1410                         DBG("%s is switched off.", get_name(technology->type));
1411                         technology_dbus_unregister(technology);
1412                 } else
1413                         technology_dbus_register(technology);
1414         }
1415
1416         return technology->hardblocked;
1417 }
1418
1419 int __connman_technology_add_rfkill(unsigned int index,
1420                                         enum connman_service_type type,
1421                                                 connman_bool_t softblock,
1422                                                 connman_bool_t hardblock)
1423 {
1424         struct connman_technology *technology;
1425         struct connman_rfkill *rfkill;
1426
1427         DBG("index %u type %d soft %u hard %u", index, type,
1428                                                         softblock, hardblock);
1429
1430         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1431         if (rfkill != NULL)
1432                 goto done;
1433
1434         rfkill = g_try_new0(struct connman_rfkill, 1);
1435         if (rfkill == NULL)
1436                 return -ENOMEM;
1437
1438         rfkill->index = index;
1439         rfkill->type = type;
1440         rfkill->softblock = softblock;
1441         rfkill->hardblock = hardblock;
1442
1443         g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1444
1445 done:
1446         technology = technology_get(type);
1447         /* If there is no driver for this type, ignore it. */
1448         if (technology == NULL)
1449                 return -ENXIO;
1450
1451         technology->rfkill_driven = TRUE;
1452
1453         /* If hardblocked, there is no need to handle softblocked state */
1454         if (technology_apply_rfkill_change(technology,
1455                                 softblock, hardblock, TRUE) == TRUE)
1456                 return 0;
1457
1458         /*
1459          * Depending on softblocked state we unblock/block according to
1460          * offlinemode and persistente state.
1461          */
1462         if (technology->softblocked == TRUE &&
1463                                 global_offlinemode == FALSE &&
1464                                 technology->enable_persistent == TRUE)
1465                 return __connman_rfkill_block(type, FALSE);
1466         else if (technology->softblocked == FALSE &&
1467                         (global_offlinemode == TRUE ||
1468                                 technology->enable_persistent == FALSE))
1469                 return __connman_rfkill_block(type, TRUE);
1470
1471         return 0;
1472 }
1473
1474 int __connman_technology_update_rfkill(unsigned int index,
1475                                         enum connman_service_type type,
1476                                                 connman_bool_t softblock,
1477                                                 connman_bool_t hardblock)
1478 {
1479         struct connman_technology *technology;
1480         struct connman_rfkill *rfkill;
1481
1482         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1483
1484         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1485         if (rfkill == NULL)
1486                 return -ENXIO;
1487
1488         if (rfkill->softblock == softblock &&
1489                                 rfkill->hardblock == hardblock)
1490                 return 0;
1491
1492         rfkill->softblock = softblock;
1493         rfkill->hardblock = hardblock;
1494
1495         technology = technology_find(type);
1496         /* If there is no driver for this type, ignore it. */
1497         if (technology == NULL)
1498                 return -ENXIO;
1499
1500         /* If hardblocked, there is no need to handle softblocked state */
1501         if (technology_apply_rfkill_change(technology,
1502                                 softblock, hardblock, FALSE) == TRUE)
1503                 return 0;
1504
1505         if (global_offlinemode == TRUE)
1506                 return 0;
1507
1508         /*
1509          * Depending on softblocked state we unblock/block according to
1510          * persistent state.
1511          */
1512         if (technology->softblocked == TRUE &&
1513                                 technology->enable_persistent == TRUE)
1514                 return __connman_rfkill_block(type, FALSE);
1515         else if (technology->softblocked == FALSE &&
1516                                 technology->enable_persistent == FALSE)
1517                 return __connman_rfkill_block(type, TRUE);
1518
1519         return 0;
1520 }
1521
1522 int __connman_technology_remove_rfkill(unsigned int index,
1523                                         enum connman_service_type type)
1524 {
1525         struct connman_technology *technology;
1526         struct connman_rfkill *rfkill;
1527
1528         DBG("index %u", index);
1529
1530         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1531         if (rfkill == NULL)
1532                 return -ENXIO;
1533
1534         g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1535
1536         technology = technology_find(type);
1537         if (technology == NULL)
1538                 return -ENXIO;
1539
1540         technology_apply_rfkill_change(technology,
1541                 technology->softblocked, !technology->hardblocked, FALSE);
1542
1543         technology_put(technology);
1544
1545         return 0;
1546 }
1547
1548 int __connman_technology_init(void)
1549 {
1550         DBG("");
1551
1552         connection = connman_dbus_get_connection();
1553
1554         rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1555                                                         NULL, free_rfkill);
1556
1557         global_offlinemode = connman_technology_load_offlinemode();
1558
1559         /* This will create settings file if it is missing */
1560         connman_technology_save_offlinemode();
1561
1562         return 0;
1563 }
1564
1565 void __connman_technology_cleanup(void)
1566 {
1567         DBG("");
1568
1569         g_hash_table_destroy(rfkill_list);
1570
1571         dbus_connection_unref(connection);
1572 }