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