technology: Modify technology enable/disable APIs
[platform/upstream/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection;
34
35 static GHashTable *rfkill_table;
36 static GHashTable *device_table;
37 static GSList *technology_list = NULL;
38
39 struct connman_rfkill {
40         unsigned int index;
41         enum connman_service_type type;
42         connman_bool_t softblock;
43         connman_bool_t hardblock;
44 };
45
46 enum connman_technology_state {
47         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
48         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
49         CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
50         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 3,
51         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
52 };
53
54 struct connman_technology {
55         gint refcount;
56         enum connman_service_type type;
57         enum connman_technology_state state;
58         char *path;
59         GHashTable *rfkill_list;
60         GSList *device_list;
61         gint enabled;
62         gint blocked;
63         char *regdom;
64
65         connman_bool_t tethering;
66         char *tethering_ident;
67         char *tethering_passphrase;
68
69         struct connman_technology_driver *driver;
70         void *driver_data;
71
72         DBusMessage *pending_reply;
73         guint pending_timeout;
74 };
75
76 static GSList *driver_list = NULL;
77
78 static gint compare_priority(gconstpointer a, gconstpointer b)
79 {
80         const struct connman_technology_driver *driver1 = a;
81         const struct connman_technology_driver *driver2 = b;
82
83         return driver2->priority - driver1->priority;
84 }
85
86 /**
87  * connman_technology_driver_register:
88  * @driver: Technology driver definition
89  *
90  * Register a new technology driver
91  *
92  * Returns: %0 on success
93  */
94 int connman_technology_driver_register(struct connman_technology_driver *driver)
95 {
96         GSList *list;
97         struct connman_technology *technology;
98
99         DBG("driver %p name %s", driver, driver->name);
100
101         driver_list = g_slist_insert_sorted(driver_list, driver,
102                                                         compare_priority);
103
104         for (list = technology_list; list; list = list->next) {
105                 technology = list->data;
106
107                 if (technology->driver != NULL)
108                         continue;
109
110                 if (technology->type == driver->type)
111                         technology->driver = driver;
112         }
113
114         return 0;
115 }
116
117 /**
118  * connman_technology_driver_unregister:
119  * @driver: Technology driver definition
120  *
121  * Remove a previously registered technology driver
122  */
123 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
124 {
125         GSList *list;
126         struct connman_technology *technology;
127
128         DBG("driver %p name %s", driver, driver->name);
129
130         for (list = technology_list; list; list = list->next) {
131                 technology = list->data;
132
133                 if (technology->driver == NULL)
134                         continue;
135
136                 if (technology->type == driver->type) {
137                         technology->driver->remove(technology);
138                         technology->driver = NULL;
139                 }
140         }
141
142         driver_list = g_slist_remove(driver_list, driver);
143 }
144
145 static void tethering_changed(struct connman_technology *technology)
146 {
147         connman_bool_t tethering = technology->tethering;
148
149         connman_dbus_property_changed_basic(technology->path,
150                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
151                                                 DBUS_TYPE_BOOLEAN, &tethering);
152 }
153
154 void connman_technology_tethering_notify(struct connman_technology *technology,
155                                                         connman_bool_t enabled)
156 {
157         DBG("technology %p enabled %u", technology, enabled);
158
159         if (technology->tethering == enabled)
160                 return;
161
162         technology->tethering = enabled;
163
164         tethering_changed(technology);
165
166         if (enabled == TRUE)
167                 __connman_tethering_set_enabled();
168         else
169                 __connman_tethering_set_disabled();
170 }
171
172 static int set_tethering(struct connman_technology *technology,
173                                 const char *bridge, connman_bool_t enabled)
174 {
175         const char *ident, *passphrase;
176
177         ident = technology->tethering_ident;
178         passphrase = technology->tethering_passphrase;
179
180         if (technology->driver == NULL ||
181                         technology->driver->set_tethering == NULL)
182                 return -EOPNOTSUPP;
183
184         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
185             (ident == NULL || passphrase == NULL))
186                 return -EINVAL;
187
188         return technology->driver->set_tethering(technology, ident, passphrase,
189                                                         bridge, enabled);
190 }
191
192 void connman_technology_regdom_notify(struct connman_technology *technology,
193                                                         const char *alpha2)
194 {
195         DBG("");
196
197         if (alpha2 == NULL)
198                 connman_error("Failed to set regulatory domain");
199         else
200                 DBG("Regulatory domain set to %s", alpha2);
201
202         g_free(technology->regdom);
203         technology->regdom = g_strdup(alpha2);
204 }
205
206 int connman_technology_set_regdom(const char *alpha2)
207 {
208         GSList *list;
209
210         for (list = technology_list; list; list = list->next) {
211                 struct connman_technology *technology = list->data;
212
213                 if (technology->driver == NULL)
214                         continue;
215
216                 if (technology->driver->set_regdom)
217                         technology->driver->set_regdom(technology, alpha2);
218         }
219
220         return 0;
221 }
222
223 static void free_rfkill(gpointer data)
224 {
225         struct connman_rfkill *rfkill = data;
226
227         g_free(rfkill);
228 }
229
230 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
231 {
232         GSList *list;
233
234         for (list = technology_list; list; list = list->next) {
235                 struct connman_technology *technology = list->data;
236
237                 if (technology->path == NULL)
238                         continue;
239
240                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
241                                                         &technology->path);
242         }
243 }
244
245 static void technologies_changed(void)
246 {
247         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
248                         CONNMAN_MANAGER_INTERFACE, "Technologies",
249                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
250 }
251
252 static const char *state2string(enum connman_technology_state state)
253 {
254         switch (state) {
255         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
256                 break;
257         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
258                 return "offline";
259         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
260                 return "available";
261         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
262                 return "enabled";
263         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
264                 return "connected";
265         }
266
267         return NULL;
268 }
269
270 static void state_changed(struct connman_technology *technology)
271 {
272         const char *str;
273
274         str = state2string(technology->state);
275         if (str == NULL)
276                 return;
277
278         connman_dbus_property_changed_basic(technology->path,
279                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
280                                                 DBUS_TYPE_STRING, &str);
281 }
282
283 static const char *get_name(enum connman_service_type type)
284 {
285         switch (type) {
286         case CONNMAN_SERVICE_TYPE_UNKNOWN:
287         case CONNMAN_SERVICE_TYPE_SYSTEM:
288         case CONNMAN_SERVICE_TYPE_GPS:
289         case CONNMAN_SERVICE_TYPE_VPN:
290         case CONNMAN_SERVICE_TYPE_GADGET:
291                 break;
292         case CONNMAN_SERVICE_TYPE_ETHERNET:
293                 return "Wired";
294         case CONNMAN_SERVICE_TYPE_WIFI:
295                 return "WiFi";
296         case CONNMAN_SERVICE_TYPE_WIMAX:
297                 return "WiMAX";
298         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
299                 return "Bluetooth";
300         case CONNMAN_SERVICE_TYPE_CELLULAR:
301                 return "3G";
302         }
303
304         return NULL;
305 }
306
307 static DBusMessage *get_properties(DBusConnection *conn,
308                                         DBusMessage *message, void *user_data)
309 {
310         struct connman_technology *technology = user_data;
311         DBusMessage *reply;
312         DBusMessageIter array, dict;
313         const char *str;
314
315         reply = dbus_message_new_method_return(message);
316         if (reply == NULL)
317                 return NULL;
318
319         dbus_message_iter_init_append(reply, &array);
320
321         connman_dbus_dict_open(&array, &dict);
322
323         str = state2string(technology->state);
324         if (str != NULL)
325                 connman_dbus_dict_append_basic(&dict, "State",
326                                                 DBUS_TYPE_STRING, &str);
327
328         str = get_name(technology->type);
329         if (str != NULL)
330                 connman_dbus_dict_append_basic(&dict, "Name",
331                                                 DBUS_TYPE_STRING, &str);
332
333         str = __connman_service_type2string(technology->type);
334         if (str != NULL)
335                 connman_dbus_dict_append_basic(&dict, "Type",
336                                                 DBUS_TYPE_STRING, &str);
337
338         connman_dbus_dict_append_basic(&dict, "Tethering",
339                                         DBUS_TYPE_BOOLEAN,
340                                         &technology->tethering);
341
342         if (technology->tethering_ident != NULL)
343                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
344                                                 DBUS_TYPE_STRING,
345                                                 &technology->tethering_ident);
346
347         if (technology->tethering_passphrase != NULL)
348                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
349                                                 DBUS_TYPE_STRING,
350                                                 &technology->tethering_passphrase);
351
352         connman_dbus_dict_close(&array, &dict);
353
354         return reply;
355 }
356
357 static DBusMessage *set_property(DBusConnection *conn,
358                                         DBusMessage *msg, void *data)
359 {
360         struct connman_technology *technology = data;
361         DBusMessageIter iter, value;
362         const char *name;
363         int type;
364
365         DBG("conn %p", conn);
366
367         if (dbus_message_iter_init(msg, &iter) == FALSE)
368                 return __connman_error_invalid_arguments(msg);
369
370         dbus_message_iter_get_basic(&iter, &name);
371         dbus_message_iter_next(&iter);
372         dbus_message_iter_recurse(&iter, &value);
373
374         type = dbus_message_iter_get_arg_type(&value);
375
376         DBG("property %s", name);
377
378         if (g_str_equal(name, "Tethering") == TRUE) {
379                 int err;
380                 connman_bool_t tethering;
381                 const char *bridge;
382
383                 if (type != DBUS_TYPE_BOOLEAN)
384                         return __connman_error_invalid_arguments(msg);
385
386                 dbus_message_iter_get_basic(&value, &tethering);
387
388                 if (technology->tethering == tethering)
389                         return __connman_error_in_progress(msg);
390
391                 bridge = __connman_tethering_get_bridge();
392                 if (bridge == NULL)
393                         return __connman_error_not_supported(msg);
394
395                 err = set_tethering(technology, bridge, tethering);
396                 if (err < 0)
397                         return __connman_error_failed(msg, -err);
398
399         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
400                 const char *str;
401
402                 dbus_message_iter_get_basic(&value, &str);
403
404                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
405                         return __connman_error_not_supported(msg);
406
407                 technology->tethering_ident = g_strdup(str);
408         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
409                 const char *str;
410
411                 dbus_message_iter_get_basic(&value, &str);
412
413                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
414                         return __connman_error_not_supported(msg);
415
416                 if (strlen(str) < 8)
417                         return __connman_error_invalid_arguments(msg);
418
419                 technology->tethering_passphrase = g_strdup(str);
420         } else
421                 return __connman_error_invalid_property(msg);
422
423         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
424 }
425
426 static GDBusMethodTable technology_methods[] = {
427         { "GetProperties", "",   "a{sv}", get_properties },
428         { "SetProperty",   "sv", "",      set_property   },
429         { },
430 };
431
432 static GDBusSignalTable technology_signals[] = {
433         { "PropertyChanged", "sv" },
434         { },
435 };
436
437 static struct connman_technology *technology_find(enum connman_service_type type)
438 {
439         GSList *list;
440
441         DBG("type %d", type);
442
443         for (list = technology_list; list; list = list->next) {
444                 struct connman_technology *technology = list->data;
445
446                 if (technology->type == type)
447                         return technology;
448         }
449
450         return NULL;
451 }
452
453 static struct connman_technology *technology_get(enum connman_service_type type)
454 {
455         struct connman_technology *technology;
456         const char *str;
457         GSList *list;
458
459         DBG("type %d", type);
460
461         technology = technology_find(type);
462         if (technology != NULL) {
463                 g_atomic_int_inc(&technology->refcount);
464                 goto done;
465         }
466
467         str = __connman_service_type2string(type);
468         if (str == NULL)
469                 return NULL;
470
471         technology = g_try_new0(struct connman_technology, 1);
472         if (technology == NULL)
473                 return NULL;
474
475         technology->refcount = 1;
476
477         technology->type = type;
478         technology->path = g_strdup_printf("%s/technology/%s",
479                                                         CONNMAN_PATH, str);
480
481         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
482                                                         NULL, free_rfkill);
483         technology->device_list = NULL;
484
485         technology->pending_reply = NULL;
486         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
487
488         if (g_dbus_register_interface(connection, technology->path,
489                                         CONNMAN_TECHNOLOGY_INTERFACE,
490                                         technology_methods, technology_signals,
491                                         NULL, technology, NULL) == FALSE) {
492                 connman_error("Failed to register %s", technology->path);
493                 g_free(technology);
494                 return NULL;
495         }
496
497         technology_list = g_slist_append(technology_list, technology);
498
499         technologies_changed();
500
501         if (technology->driver != NULL)
502                 goto done;
503
504         for (list = driver_list; list; list = list->next) {
505                 struct connman_technology_driver *driver = list->data;
506
507                 DBG("driver %p name %s", driver, driver->name);
508
509                 if (driver->type != technology->type)
510                         continue;
511
512                 if (driver->probe(technology) == 0) {
513                         technology->driver = driver;
514                         break;
515                 }
516         }
517
518 done:
519         DBG("technology %p", technology);
520
521         return technology;
522 }
523
524 static void technology_put(struct connman_technology *technology)
525 {
526         DBG("technology %p", technology);
527
528         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
529                 return;
530
531         if (technology->driver) {
532                 technology->driver->remove(technology);
533                 technology->driver = NULL;
534         }
535
536         technology_list = g_slist_remove(technology_list, technology);
537
538         technologies_changed();
539
540         g_dbus_unregister_interface(connection, technology->path,
541                                                 CONNMAN_TECHNOLOGY_INTERFACE);
542
543         g_slist_free(technology->device_list);
544         g_hash_table_destroy(technology->rfkill_list);
545
546         g_free(technology->path);
547         g_free(technology->regdom);
548         g_free(technology);
549 }
550
551 void __connman_technology_add_interface(enum connman_service_type type,
552                                 int index, const char *name, const char *ident)
553 {
554         struct connman_technology *technology;
555
556         switch (type) {
557         case CONNMAN_SERVICE_TYPE_UNKNOWN:
558         case CONNMAN_SERVICE_TYPE_SYSTEM:
559                 return;
560         case CONNMAN_SERVICE_TYPE_ETHERNET:
561         case CONNMAN_SERVICE_TYPE_WIFI:
562         case CONNMAN_SERVICE_TYPE_WIMAX:
563         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
564         case CONNMAN_SERVICE_TYPE_CELLULAR:
565         case CONNMAN_SERVICE_TYPE_GPS:
566         case CONNMAN_SERVICE_TYPE_VPN:
567         case CONNMAN_SERVICE_TYPE_GADGET:
568                 break;
569         }
570
571         connman_info("Create interface %s [ %s ]", name,
572                                 __connman_service_type2string(type));
573
574         technology = technology_get(type);
575
576         if (technology == NULL || technology->driver == NULL
577                         || technology->driver->add_interface == NULL)
578                 return;
579
580         technology->driver->add_interface(technology,
581                                         index, name, ident);
582 }
583
584 void __connman_technology_remove_interface(enum connman_service_type type,
585                                 int index, const char *name, const char *ident)
586 {
587         struct connman_technology *technology;
588
589         switch (type) {
590         case CONNMAN_SERVICE_TYPE_UNKNOWN:
591         case CONNMAN_SERVICE_TYPE_SYSTEM:
592                 return;
593         case CONNMAN_SERVICE_TYPE_ETHERNET:
594         case CONNMAN_SERVICE_TYPE_WIFI:
595         case CONNMAN_SERVICE_TYPE_WIMAX:
596         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
597         case CONNMAN_SERVICE_TYPE_CELLULAR:
598         case CONNMAN_SERVICE_TYPE_GPS:
599         case CONNMAN_SERVICE_TYPE_VPN:
600         case CONNMAN_SERVICE_TYPE_GADGET:
601                 break;
602         }
603
604         connman_info("Remove interface %s [ %s ]", name,
605                                 __connman_service_type2string(type));
606
607         technology = technology_find(type);
608
609         if (technology == NULL || technology->driver == NULL)
610                 return;
611
612         if (technology->driver->remove_interface)
613                 technology->driver->remove_interface(technology, index);
614
615         technology_put(technology);
616 }
617
618 static void unregister_technology(gpointer data)
619 {
620         struct connman_technology *technology = data;
621
622         technology_put(technology);
623 }
624
625 int __connman_technology_add_device(struct connman_device *device)
626 {
627         struct connman_technology *technology;
628         enum connman_service_type type;
629
630         DBG("device %p", device);
631
632         type = __connman_device_get_service_type(device);
633         __connman_notifier_register(type);
634
635         technology = technology_get(type);
636         if (technology == NULL)
637                 return -ENXIO;
638
639         g_hash_table_insert(device_table, device, technology);
640
641         if (g_atomic_int_get(&technology->blocked))
642                 goto done;
643
644         technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
645
646         state_changed(technology);
647
648 done:
649
650         technology->device_list = g_slist_append(technology->device_list,
651                                                                 device);
652
653         return 0;
654 }
655
656 int __connman_technology_remove_device(struct connman_device *device)
657 {
658         struct connman_technology *technology;
659         enum connman_service_type type;
660
661         DBG("device %p", device);
662
663         type = __connman_device_get_service_type(device);
664         __connman_notifier_unregister(type);
665
666         technology = g_hash_table_lookup(device_table, device);
667         if (technology == NULL)
668                 return -ENXIO;
669
670         technology->device_list = g_slist_remove(technology->device_list,
671                                                                 device);
672         if (technology->device_list == NULL) {
673                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
674                 state_changed(technology);
675         }
676
677         g_hash_table_remove(device_table, device);
678
679         return 0;
680 }
681
682 static gboolean technology_pending_reply(gpointer user_data)
683 {
684         struct connman_technology *technology = user_data;
685         DBusMessage *reply;
686
687         /* Power request timedout, send ETIMEDOUT. */
688         if (technology->pending_reply != NULL) {
689                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
690                 if (reply != NULL)
691                         g_dbus_send_message(connection, reply);
692
693                 dbus_message_unref(technology->pending_reply);
694                 technology->pending_reply = NULL;
695                 technology->pending_timeout = 0;
696         }
697
698         return FALSE;
699 }
700
701 int __connman_technology_enabled(enum connman_service_type type)
702 {
703         struct connman_technology *technology;
704
705         technology = technology_find(type);
706         if (technology == NULL)
707                 return -ENXIO;
708
709         if (g_atomic_int_get(&technology->blocked))
710                 return -ERFKILL;
711
712         __connman_notifier_enable(type);
713
714         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
715                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
716                 state_changed(technology);
717         }
718
719         if (__connman_profile_get_offlinemode() == TRUE) {
720                 __connman_profile_set_offlinemode(FALSE, FALSE);
721                 __connman_profile_save_default();
722         }
723
724         if (technology->pending_reply != NULL) {
725                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
726                 dbus_message_unref(technology->pending_reply);
727                 technology->pending_reply = NULL;
728                 technology->pending_timeout = 0;
729         }
730
731         return 0;
732 }
733
734 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
735 {
736         struct connman_technology *technology;
737         GSList *list;
738         int err = 0;
739         int ret = -ENODEV;
740         DBusMessage *reply;
741
742         DBG("type %d enable", type);
743
744         technology = technology_find(type);
745         if (technology == NULL) {
746                 err = -ENXIO;
747                 goto done;
748         }
749
750         if (technology->pending_reply != NULL) {
751                 err = -EBUSY;
752                 goto done;
753         }
754
755         if (msg != NULL)
756                 technology->pending_reply = dbus_message_ref(msg);
757
758         for (list = technology->device_list; list; list = list->next) {
759                 struct connman_device *device = list->data;
760
761                 err = __connman_device_enable_persistent(device);
762                 /*
763                  * err = 0 : Device was enabled right away.
764                  * If atleast one device gets enabled, we consider
765                  * the technology to be enabled.
766                  */
767                 if (err == 0)
768                         ret = 0;
769         }
770
771 done:
772         if (ret == 0)
773                 return ret;
774
775         if (msg != NULL) {
776                 if (err == -EINPROGRESS)
777                         technology->pending_timeout = g_timeout_add_seconds(10,
778                                         technology_pending_reply, technology);
779                 else {
780                         reply = __connman_error_failed(msg, -err);
781                         if (reply != NULL)
782                                 g_dbus_send_message(connection, reply);
783                 }
784         }
785
786         return err;
787 }
788
789 int __connman_technology_disabled(enum connman_service_type type)
790 {
791         struct connman_technology *technology;
792         GSList *list;
793
794         technology = technology_find(type);
795         if (technology == NULL)
796                 return -ENXIO;
797
798         if (technology->pending_reply != NULL) {
799                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
800                 dbus_message_unref(technology->pending_reply);
801                 technology->pending_reply = NULL;
802         }
803
804         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
805                 __connman_notifier_disable(type);
806
807                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
808                 state_changed(technology);
809         }
810
811         for (list = technology->device_list; list; list = list->next) {
812                 struct connman_device *device = list->data;
813
814                 if (__connman_device_get_blocked(device) == FALSE)
815                         return 0;
816         }
817
818         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
819         state_changed(technology);
820
821         return 0;
822 }
823
824 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
825 {
826         struct connman_technology *technology;
827         GSList *list;
828         int err = 0;
829         int ret = -ENODEV;
830         DBusMessage *reply;
831
832         DBG("type %d disable", type);
833
834         technology = technology_find(type);
835         if (technology == NULL) {
836                 err = -ENXIO;
837                 goto done;
838         }
839
840         if (technology->pending_reply != NULL) {
841                 err = -EBUSY;
842                 goto done;
843         }
844
845         if (msg != NULL)
846                 technology->pending_reply = dbus_message_ref(msg);
847
848         for (list = technology->device_list; list; list = list->next) {
849                 struct connman_device *device = list->data;
850
851                 err = __connman_device_disable_persistent(device);
852                 if (err == 0)
853                         ret = 0;
854         }
855
856 done:
857         if (ret == 0)
858                 return ret;
859
860         if (msg != NULL) {
861                 if (err == -EINPROGRESS)
862                         technology->pending_timeout = g_timeout_add_seconds(10,
863                                         technology_pending_reply, technology);
864                 else {
865                         reply = __connman_error_failed(msg, -err);
866                         if (reply != NULL)
867                                 g_dbus_send_message(connection, reply);
868                 }
869         }
870
871         return err;
872 }
873
874 static void technology_blocked(struct connman_technology *technology,
875                                 connman_bool_t blocked)
876 {
877         GSList *list;
878
879         for (list = technology->device_list; list; list = list->next) {
880                 struct connman_device *device = list->data;
881
882                 __connman_device_set_blocked(device, blocked);
883         }
884 }
885
886 int __connman_technology_add_rfkill(unsigned int index,
887                                         enum connman_service_type type,
888                                                 connman_bool_t softblock,
889                                                 connman_bool_t hardblock)
890 {
891         struct connman_technology *technology;
892         struct connman_rfkill *rfkill;
893         connman_bool_t blocked;
894
895         DBG("index %u type %d soft %u hard %u", index, type,
896                                                         softblock, hardblock);
897
898         technology = technology_get(type);
899         if (technology == NULL)
900                 return -ENXIO;
901
902         rfkill = g_try_new0(struct connman_rfkill, 1);
903         if (rfkill == NULL)
904                 return -ENOMEM;
905
906         rfkill->index = index;
907         rfkill->type = type;
908         rfkill->softblock = softblock;
909         rfkill->hardblock = hardblock;
910
911         g_hash_table_replace(rfkill_table, &rfkill->index, technology);
912
913         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
914
915         blocked = (softblock || hardblock) ? TRUE : FALSE;
916         if (blocked == FALSE)
917                 return 0;
918
919         if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
920                 technology_blocked(technology, TRUE);
921
922                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
923                 state_changed(technology);
924         }
925
926         return 0;
927 }
928
929 int __connman_technology_update_rfkill(unsigned int index,
930                                                 connman_bool_t softblock,
931                                                 connman_bool_t hardblock)
932 {
933         struct connman_technology *technology;
934         struct connman_rfkill *rfkill;
935         connman_bool_t blocked, old_blocked;
936
937         DBG("index %u soft %u hard %u", index, softblock, hardblock);
938
939         technology = g_hash_table_lookup(rfkill_table, &index);
940         if (technology == NULL)
941                 return -ENXIO;
942
943         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
944         if (rfkill == NULL)
945                 return -ENXIO;
946
947         old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
948         blocked = (softblock || hardblock) ? TRUE : FALSE;
949
950         rfkill->softblock = softblock;
951         rfkill->hardblock = hardblock;
952
953         if (blocked == old_blocked)
954                 return 0;
955
956         if (blocked) {
957                 guint n_blocked;
958
959                 n_blocked =
960                         g_atomic_int_exchange_and_add(&technology->blocked, 1);
961                 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
962                         return 0;
963
964                 technology_blocked(technology, blocked);
965                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
966                 state_changed(technology);
967         } else {
968                 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
969                         return 0;
970
971                 technology_blocked(technology, blocked);
972                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
973                 state_changed(technology);
974         }
975
976         return 0;
977 }
978
979 int __connman_technology_remove_rfkill(unsigned int index)
980 {
981         struct connman_technology *technology;
982         struct connman_rfkill *rfkill;
983         connman_bool_t blocked;
984
985         DBG("index %u", index);
986
987         technology = g_hash_table_lookup(rfkill_table, &index);
988         if (technology == NULL)
989                 return -ENXIO;
990
991         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
992         if (rfkill == NULL)
993                 return -ENXIO;
994
995         blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
996
997         g_hash_table_remove(technology->rfkill_list, &index);
998
999         g_hash_table_remove(rfkill_table, &index);
1000
1001         if (blocked &&
1002                 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
1003                 technology_blocked(technology, FALSE);
1004                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
1005                 state_changed(technology);
1006         }
1007
1008         return 0;
1009 }
1010
1011 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
1012 {
1013         struct connman_technology *technology;
1014
1015         technology = technology_find(type);
1016         if (technology == NULL)
1017                 return FALSE;
1018
1019         if (g_atomic_int_get(&technology->blocked))
1020                 return TRUE;
1021
1022         return FALSE;
1023 }
1024
1025 int __connman_technology_init(void)
1026 {
1027         DBG("");
1028
1029         connection = connman_dbus_get_connection();
1030
1031         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
1032                                                 NULL, unregister_technology);
1033         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1034                                                 NULL, unregister_technology);
1035
1036         return 0;
1037 }
1038
1039 void __connman_technology_cleanup(void)
1040 {
1041         DBG("");
1042
1043         g_hash_table_destroy(device_table);
1044         g_hash_table_destroy(rfkill_table);
1045
1046         dbus_connection_unref(connection);
1047 }