7d89b2ef707b16127f7c676aff3cdd09ccea29c7
[framework/connectivity/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 <string.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 static DBusConnection *connection;
33
34 static GHashTable *rfkill_table;
35 static GHashTable *device_table;
36 static GSList *technology_list = NULL;
37
38 struct connman_rfkill {
39         unsigned int index;
40         enum connman_service_type type;
41         connman_bool_t softblock;
42         connman_bool_t hardblock;
43 };
44
45 enum connman_technology_state {
46         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
47         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
48         CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
49         CONNMAN_TECHNOLOGY_STATE_BLOCKED   = 3,
50         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 4,
51         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 5,
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
73 static GSList *driver_list = NULL;
74
75 static gint compare_priority(gconstpointer a, gconstpointer b)
76 {
77         const struct connman_technology_driver *driver1 = a;
78         const struct connman_technology_driver *driver2 = b;
79
80         return driver2->priority - driver1->priority;
81 }
82
83 /**
84  * connman_technology_driver_register:
85  * @driver: Technology driver definition
86  *
87  * Register a new technology driver
88  *
89  * Returns: %0 on success
90  */
91 int connman_technology_driver_register(struct connman_technology_driver *driver)
92 {
93         GSList *list;
94         struct connman_technology *technology;
95
96         DBG("driver %p name %s", driver, driver->name);
97
98         driver_list = g_slist_insert_sorted(driver_list, driver,
99                                                         compare_priority);
100
101         for (list = technology_list; list; list = list->next) {
102                 technology = list->data;
103
104                 if (technology->driver != NULL)
105                         continue;
106
107                 if (technology->type == driver->type)
108                         technology->driver = driver;
109         }
110
111         return 0;
112 }
113
114 /**
115  * connman_technology_driver_unregister:
116  * @driver: Technology driver definition
117  *
118  * Remove a previously registered technology driver
119  */
120 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
121 {
122         GSList *list;
123         struct connman_technology *technology;
124
125         DBG("driver %p name %s", driver, driver->name);
126
127         for (list = technology_list; list; list = list->next) {
128                 technology = list->data;
129
130                 if (technology->driver == NULL)
131                         continue;
132
133                 if (technology->type == driver->type) {
134                         technology->driver->remove(technology);
135                         technology->driver = NULL;
136                 }
137         }
138
139         driver_list = g_slist_remove(driver_list, driver);
140 }
141
142 void __connman_technology_add_interface(enum connman_service_type type,
143                                 int index, const char *name, const char *ident)
144 {
145         GSList *list;
146
147         switch (type) {
148         case CONNMAN_SERVICE_TYPE_UNKNOWN:
149         case CONNMAN_SERVICE_TYPE_SYSTEM:
150                 return;
151         case CONNMAN_SERVICE_TYPE_ETHERNET:
152         case CONNMAN_SERVICE_TYPE_WIFI:
153         case CONNMAN_SERVICE_TYPE_WIMAX:
154         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
155         case CONNMAN_SERVICE_TYPE_CELLULAR:
156         case CONNMAN_SERVICE_TYPE_GPS:
157         case CONNMAN_SERVICE_TYPE_VPN:
158         case CONNMAN_SERVICE_TYPE_GADGET:
159                 break;
160         }
161
162         connman_info("Create interface %s [ %s ]", name,
163                                 __connman_service_type2string(type));
164
165         for (list = technology_list; list; list = list->next) {
166                 struct connman_technology *technology = list->data;
167
168                 if (technology->type != type)
169                         continue;
170
171                 if (technology->driver == NULL)
172                         continue;
173
174                 if (technology->driver->add_interface)
175                         technology->driver->add_interface(technology,
176                                                         index, name, ident);
177         }
178 }
179
180 void __connman_technology_remove_interface(enum connman_service_type type,
181                                 int index, const char *name, const char *ident)
182 {
183         GSList *list;
184
185         switch (type) {
186         case CONNMAN_SERVICE_TYPE_UNKNOWN:
187         case CONNMAN_SERVICE_TYPE_SYSTEM:
188                 return;
189         case CONNMAN_SERVICE_TYPE_ETHERNET:
190         case CONNMAN_SERVICE_TYPE_WIFI:
191         case CONNMAN_SERVICE_TYPE_WIMAX:
192         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
193         case CONNMAN_SERVICE_TYPE_CELLULAR:
194         case CONNMAN_SERVICE_TYPE_GPS:
195         case CONNMAN_SERVICE_TYPE_VPN:
196         case CONNMAN_SERVICE_TYPE_GADGET:
197                 break;
198         }
199
200         connman_info("Remove interface %s [ %s ]", name,
201                                 __connman_service_type2string(type));
202
203         for (list = technology_list; list; list = list->next) {
204                 struct connman_technology *technology = list->data;
205
206                 if (technology->type != type)
207                         continue;
208
209                 if (technology->driver == NULL)
210                         continue;
211
212                 if (technology->driver->remove_interface)
213                         technology->driver->remove_interface(technology, index);
214         }
215 }
216
217 static void tethering_changed(struct connman_technology *technology)
218 {
219         connman_bool_t tethering = technology->tethering;
220
221         connman_dbus_property_changed_basic(technology->path,
222                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
223                                                 DBUS_TYPE_BOOLEAN, &tethering);
224 }
225
226 void connman_technology_tethering_notify(struct connman_technology *technology,
227                                                         connman_bool_t enabled)
228 {
229         DBG("technology %p enabled %u", technology, enabled);
230
231         if (technology->tethering == enabled)
232                 return;
233
234         technology->tethering = enabled;
235
236         tethering_changed(technology);
237
238         if (enabled == TRUE)
239                 __connman_tethering_set_enabled();
240         else
241                 __connman_tethering_set_disabled();
242 }
243
244 static int set_tethering(struct connman_technology *technology,
245                                 const char *bridge, connman_bool_t enabled)
246 {
247         const char *ident, *passphrase;
248
249         ident = technology->tethering_ident;
250         passphrase = technology->tethering_passphrase;
251
252         if (technology->driver == NULL ||
253                         technology->driver->set_tethering == NULL)
254                 return -EOPNOTSUPP;
255
256         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
257             (ident == NULL || passphrase == NULL))
258                 return -EINVAL;
259
260         return technology->driver->set_tethering(technology, ident, passphrase,
261                                                         bridge, enabled);
262 }
263
264 void connman_technology_regdom_notify(struct connman_technology *technology,
265                                                         const char *alpha2)
266 {
267         DBG("");
268
269         if (alpha2 == NULL)
270                 connman_error("Failed to set regulatory domain");
271         else
272                 DBG("Regulatory domain set to %s", alpha2);
273
274         g_free(technology->regdom);
275         technology->regdom = g_strdup(alpha2);
276 }
277
278 int connman_technology_set_regdom(const char *alpha2)
279 {
280         GSList *list;
281
282         for (list = technology_list; list; list = list->next) {
283                 struct connman_technology *technology = list->data;
284
285                 if (technology->driver == NULL)
286                         continue;
287
288                 if (technology->driver->set_regdom)
289                         technology->driver->set_regdom(technology, alpha2);
290         }
291
292         return 0;
293 }
294
295 static void free_rfkill(gpointer data)
296 {
297         struct connman_rfkill *rfkill = data;
298
299         g_free(rfkill);
300 }
301
302 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
303 {
304         GSList *list;
305
306         for (list = technology_list; list; list = list->next) {
307                 struct connman_technology *technology = list->data;
308
309                 if (technology->path == NULL)
310                         continue;
311
312                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
313                                                         &technology->path);
314         }
315 }
316
317 static void technologies_changed(void)
318 {
319         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
320                         CONNMAN_MANAGER_INTERFACE, "Technologies",
321                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
322 }
323
324 static const char *state2string(enum connman_technology_state state)
325 {
326         switch (state) {
327         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
328                 break;
329         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
330                 return "offline";
331         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
332                 return "available";
333         case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
334                 return "blocked";
335         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
336                 return "enabled";
337         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
338                 return "connected";
339         }
340
341         return NULL;
342 }
343
344 static void state_changed(struct connman_technology *technology)
345 {
346         const char *str;
347
348         str = state2string(technology->state);
349         if (str == NULL)
350                 return;
351
352         connman_dbus_property_changed_basic(technology->path,
353                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
354                                                 DBUS_TYPE_STRING, &str);
355 }
356
357 static const char *get_name(enum connman_service_type type)
358 {
359         switch (type) {
360         case CONNMAN_SERVICE_TYPE_UNKNOWN:
361         case CONNMAN_SERVICE_TYPE_SYSTEM:
362         case CONNMAN_SERVICE_TYPE_GPS:
363         case CONNMAN_SERVICE_TYPE_VPN:
364         case CONNMAN_SERVICE_TYPE_GADGET:
365                 break;
366         case CONNMAN_SERVICE_TYPE_ETHERNET:
367                 return "Wired";
368         case CONNMAN_SERVICE_TYPE_WIFI:
369                 return "WiFi";
370         case CONNMAN_SERVICE_TYPE_WIMAX:
371                 return "WiMAX";
372         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
373                 return "Bluetooth";
374         case CONNMAN_SERVICE_TYPE_CELLULAR:
375                 return "3G";
376         }
377
378         return NULL;
379 }
380
381 static DBusMessage *get_properties(DBusConnection *conn,
382                                         DBusMessage *message, void *user_data)
383 {
384         struct connman_technology *technology = user_data;
385         DBusMessage *reply;
386         DBusMessageIter array, dict;
387         const char *str;
388
389         reply = dbus_message_new_method_return(message);
390         if (reply == NULL)
391                 return NULL;
392
393         dbus_message_iter_init_append(reply, &array);
394
395         connman_dbus_dict_open(&array, &dict);
396
397         str = state2string(technology->state);
398         if (str != NULL)
399                 connman_dbus_dict_append_basic(&dict, "State",
400                                                 DBUS_TYPE_STRING, &str);
401
402         str = get_name(technology->type);
403         if (str != NULL)
404                 connman_dbus_dict_append_basic(&dict, "Name",
405                                                 DBUS_TYPE_STRING, &str);
406
407         str = __connman_service_type2string(technology->type);
408         if (str != NULL)
409                 connman_dbus_dict_append_basic(&dict, "Type",
410                                                 DBUS_TYPE_STRING, &str);
411
412         connman_dbus_dict_append_basic(&dict, "Tethering",
413                                         DBUS_TYPE_BOOLEAN,
414                                         &technology->tethering);
415
416         if (technology->tethering_ident != NULL)
417                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
418                                                 DBUS_TYPE_STRING,
419                                                 &technology->tethering_ident);
420
421         if (technology->tethering_passphrase != NULL)
422                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
423                                                 DBUS_TYPE_STRING,
424                                                 &technology->tethering_passphrase);
425
426         connman_dbus_dict_close(&array, &dict);
427
428         return reply;
429 }
430
431 static DBusMessage *set_property(DBusConnection *conn,
432                                         DBusMessage *msg, void *data)
433 {
434         struct connman_technology *technology = data;
435         DBusMessageIter iter, value;
436         const char *name;
437         int type;
438
439         DBG("conn %p", conn);
440
441         if (dbus_message_iter_init(msg, &iter) == FALSE)
442                 return __connman_error_invalid_arguments(msg);
443
444         dbus_message_iter_get_basic(&iter, &name);
445         dbus_message_iter_next(&iter);
446         dbus_message_iter_recurse(&iter, &value);
447
448         type = dbus_message_iter_get_arg_type(&value);
449
450         DBG("property %s", name);
451
452         if (g_str_equal(name, "Tethering") == TRUE) {
453                 int err;
454                 connman_bool_t tethering;
455                 const char *bridge;
456
457                 if (type != DBUS_TYPE_BOOLEAN)
458                         return __connman_error_invalid_arguments(msg);
459
460                 dbus_message_iter_get_basic(&value, &tethering);
461
462                 if (technology->tethering == tethering)
463                         return __connman_error_in_progress(msg);
464
465                 bridge = __connman_tethering_get_bridge();
466
467                 err = set_tethering(technology, bridge, tethering);
468                 if (err < 0)
469                         return __connman_error_failed(msg, -err);
470
471         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
472                 const char *str;
473
474                 dbus_message_iter_get_basic(&value, &str);
475
476                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
477                         return __connman_error_not_supported(msg);
478
479                 technology->tethering_ident = g_strdup(str);
480         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
481                 const char *str;
482
483                 dbus_message_iter_get_basic(&value, &str);
484
485                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
486                         return __connman_error_not_supported(msg);
487
488                 if (strlen(str) < 8)
489                         return __connman_error_invalid_arguments(msg);
490
491                 technology->tethering_passphrase = g_strdup(str);
492         } else
493                 return __connman_error_invalid_property(msg);
494
495         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
496 }
497
498 static GDBusMethodTable technology_methods[] = {
499         { "GetProperties", "",   "a{sv}", get_properties },
500         { "SetProperty",   "sv", "",      set_property   },
501         { },
502 };
503
504 static GDBusSignalTable technology_signals[] = {
505         { "PropertyChanged", "sv" },
506         { },
507 };
508
509 static struct connman_technology *technology_find(enum connman_service_type type)
510 {
511         GSList *list;
512
513         DBG("type %d", type);
514
515         for (list = technology_list; list; list = list->next) {
516                 struct connman_technology *technology = list->data;
517
518                 if (technology->type == type)
519                         return technology;
520         }
521
522         return NULL;
523 }
524
525 static struct connman_technology *technology_get(enum connman_service_type type)
526 {
527         struct connman_technology *technology;
528         const char *str;
529         GSList *list;
530
531         DBG("type %d", type);
532
533         technology = technology_find(type);
534         if (technology != NULL) {
535                 g_atomic_int_inc(&technology->refcount);
536                 goto done;
537         }
538
539         str = __connman_service_type2string(type);
540         if (str == NULL)
541                 return NULL;
542
543         technology = g_try_new0(struct connman_technology, 1);
544         if (technology == NULL)
545                 return NULL;
546
547         technology->refcount = 1;
548
549         technology->type = type;
550         technology->path = g_strdup_printf("%s/technology/%s",
551                                                         CONNMAN_PATH, str);
552
553         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
554                                                         NULL, free_rfkill);
555         technology->device_list = NULL;
556
557         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
558
559         if (g_dbus_register_interface(connection, technology->path,
560                                         CONNMAN_TECHNOLOGY_INTERFACE,
561                                         technology_methods, technology_signals,
562                                         NULL, technology, NULL) == FALSE) {
563                 connman_error("Failed to register %s", technology->path);
564                 g_free(technology);
565                 return NULL;
566         }
567
568         technology_list = g_slist_append(technology_list, technology);
569
570         technologies_changed();
571
572         if (technology->driver != NULL)
573                 goto done;
574
575         for (list = driver_list; list; list = list->next) {
576                 struct connman_technology_driver *driver = list->data;
577
578                 DBG("driver %p name %s", driver, driver->name);
579
580                 if (driver->type != technology->type)
581                         continue;
582
583                 if (driver->probe(technology) == 0) {
584                         technology->driver = driver;
585                         break;
586                 }
587         }
588
589 done:
590         DBG("technology %p", technology);
591
592         return technology;
593 }
594
595 static void technology_put(struct connman_technology *technology)
596 {
597         DBG("technology %p", technology);
598
599         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
600                 return;
601
602         if (technology->driver) {
603                 technology->driver->remove(technology);
604                 technology->driver = NULL;
605         }
606
607         technology_list = g_slist_remove(technology_list, technology);
608
609         technologies_changed();
610
611         g_dbus_unregister_interface(connection, technology->path,
612                                                 CONNMAN_TECHNOLOGY_INTERFACE);
613
614         g_slist_free(technology->device_list);
615         g_hash_table_destroy(technology->rfkill_list);
616
617         g_free(technology->path);
618         g_free(technology->regdom);
619         g_free(technology);
620 }
621
622 static void unregister_technology(gpointer data)
623 {
624         struct connman_technology *technology = data;
625
626         technology_put(technology);
627 }
628
629 int __connman_technology_add_device(struct connman_device *device)
630 {
631         struct connman_technology *technology;
632         enum connman_service_type type;
633
634         DBG("device %p", device);
635
636         type = __connman_device_get_service_type(device);
637         __connman_notifier_register(type);
638
639         technology = technology_get(type);
640         if (technology == NULL)
641                 return -ENXIO;
642
643         g_hash_table_insert(device_table, device, technology);
644
645         if (g_atomic_int_get(&technology->blocked))
646                 goto done;
647
648         technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
649
650         state_changed(technology);
651
652 done:
653
654         technology->device_list = g_slist_append(technology->device_list,
655                                                                 device);
656
657         return 0;
658 }
659
660 int __connman_technology_remove_device(struct connman_device *device)
661 {
662         struct connman_technology *technology;
663         enum connman_service_type type;
664
665         DBG("device %p", device);
666
667         type = __connman_device_get_service_type(device);
668         __connman_notifier_unregister(type);
669
670         technology = g_hash_table_lookup(device_table, device);
671         if (technology == NULL)
672                 return -ENXIO;
673
674         technology->device_list = g_slist_remove(technology->device_list,
675                                                                 device);
676         if (technology->device_list == NULL) {
677                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
678                 state_changed(technology);
679         }
680
681         g_hash_table_remove(device_table, device);
682
683         return 0;
684 }
685
686 int __connman_technology_enable_device(struct connman_device *device)
687 {
688         struct connman_technology *technology;
689         enum connman_service_type type;
690
691         DBG("device %p", device);
692
693         technology = g_hash_table_lookup(device_table, device);
694         if (technology == NULL)
695                 return -ENXIO;
696
697         if (g_atomic_int_get(&technology->blocked))
698                 return -ERFKILL;
699
700         type = __connman_device_get_service_type(device);
701         __connman_notifier_enable(type);
702
703         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
704                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
705                 state_changed(technology);
706         }
707
708         return 0;
709 }
710
711 int __connman_technology_disable_device(struct connman_device *device)
712 {
713         struct connman_technology *technology;
714         enum connman_service_type type;
715         GSList *list;
716
717         DBG("device %p", device);
718
719         type = __connman_device_get_service_type(device);
720         __connman_notifier_disable(type);
721
722         technology = g_hash_table_lookup(device_table, device);
723         if (technology == NULL)
724                 return -ENXIO;
725
726         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
727                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
728                 state_changed(technology);
729         }
730
731         for (list = technology->device_list; list; list = list->next) {
732                 struct connman_device *device = list->data;
733
734                 if (__connman_device_get_blocked(device) == FALSE)
735                         return 0;
736         }
737
738         technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
739         state_changed(technology);
740
741         return 0;
742 }
743
744 static void technology_blocked(struct connman_technology *technology,
745                                 connman_bool_t blocked)
746 {
747         GSList *list;
748
749         for (list = technology->device_list; list; list = list->next) {
750                 struct connman_device *device = list->data;
751
752                 __connman_device_set_blocked(device, blocked);
753         }
754 }
755
756 int __connman_technology_add_rfkill(unsigned int index,
757                                         enum connman_service_type type,
758                                                 connman_bool_t softblock,
759                                                 connman_bool_t hardblock)
760 {
761         struct connman_technology *technology;
762         struct connman_rfkill *rfkill;
763         connman_bool_t blocked;
764
765         DBG("index %u type %d soft %u hard %u", index, type,
766                                                         softblock, hardblock);
767
768         technology = technology_get(type);
769         if (technology == NULL)
770                 return -ENXIO;
771
772         rfkill = g_try_new0(struct connman_rfkill, 1);
773         if (rfkill == NULL)
774                 return -ENOMEM;
775
776         rfkill->index = index;
777         rfkill->type = type;
778         rfkill->softblock = softblock;
779         rfkill->hardblock = hardblock;
780
781         g_hash_table_replace(rfkill_table, &rfkill->index, technology);
782
783         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
784
785         blocked = (softblock || hardblock) ? TRUE : FALSE;
786         if (blocked == FALSE)
787                 return 0;
788
789         if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
790                 technology_blocked(technology, TRUE);
791
792                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
793                 state_changed(technology);
794         }
795
796         return 0;
797 }
798
799 int __connman_technology_update_rfkill(unsigned int index,
800                                                 connman_bool_t softblock,
801                                                 connman_bool_t hardblock)
802 {
803         struct connman_technology *technology;
804         struct connman_rfkill *rfkill;
805         connman_bool_t blocked, old_blocked;
806
807         DBG("index %u soft %u hard %u", index, softblock, hardblock);
808
809         technology = g_hash_table_lookup(rfkill_table, &index);
810         if (technology == NULL)
811                 return -ENXIO;
812
813         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
814         if (rfkill == NULL)
815                 return -ENXIO;
816
817         old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
818         blocked = (softblock || hardblock) ? TRUE : FALSE;
819
820         rfkill->softblock = softblock;
821         rfkill->hardblock = hardblock;
822
823         if (blocked == old_blocked)
824                 return 0;
825
826         if (blocked) {
827                 guint n_blocked;
828
829                 n_blocked =
830                         g_atomic_int_exchange_and_add(&technology->blocked, 1);
831                 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
832                         return 0;
833
834                 technology_blocked(technology, blocked);
835                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
836                 state_changed(technology);
837         } else {
838                 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
839                         return 0;
840
841                 technology_blocked(technology, blocked);
842                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
843                 state_changed(technology);
844         }
845
846         return 0;
847 }
848
849 int __connman_technology_remove_rfkill(unsigned int index)
850 {
851         struct connman_technology *technology;
852         struct connman_rfkill *rfkill;
853         connman_bool_t blocked;
854
855         DBG("index %u", index);
856
857         technology = g_hash_table_lookup(rfkill_table, &index);
858         if (technology == NULL)
859                 return -ENXIO;
860
861         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
862         if (rfkill == NULL)
863                 return -ENXIO;
864
865         blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
866
867         g_hash_table_remove(technology->rfkill_list, &index);
868
869         g_hash_table_remove(rfkill_table, &index);
870
871         if (blocked &&
872                 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
873                 technology_blocked(technology, FALSE);
874                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
875                 state_changed(technology);
876         }
877
878         return 0;
879 }
880
881 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
882 {
883         struct connman_technology *technology;
884
885         technology = technology_find(type);
886         if (technology == NULL)
887                 return FALSE;
888
889         if (g_atomic_int_get(&technology->blocked))
890                 return TRUE;
891
892         return FALSE;
893 }
894
895 int __connman_technology_init(void)
896 {
897         DBG("");
898
899         connection = connman_dbus_get_connection();
900
901         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
902                                                 NULL, unregister_technology);
903         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
904                                                 NULL, unregister_technology);
905
906         return 0;
907 }
908
909 void __connman_technology_cleanup(void)
910 {
911         DBG("");
912
913         g_hash_table_destroy(device_table);
914         g_hash_table_destroy(rfkill_table);
915
916         dbus_connection_unref(connection);
917 }