5ef3d10ed9ee2fbc82e45a28206a1648b060524e
[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 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         GSList *list;
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_get(type);
571
572         for (list = technology_list; list; list = list->next) {
573                 struct connman_technology *technology = list->data;
574
575                 if (technology->type != type)
576                         continue;
577
578                 if (technology->driver == NULL)
579                         continue;
580
581                 if (technology->driver->add_interface)
582                         technology->driver->add_interface(technology,
583                                                         index, name, ident);
584         }
585 }
586
587 void __connman_technology_remove_interface(enum connman_service_type type,
588                                 int index, const char *name, const char *ident)
589 {
590         GSList *list;
591
592         switch (type) {
593         case CONNMAN_SERVICE_TYPE_UNKNOWN:
594         case CONNMAN_SERVICE_TYPE_SYSTEM:
595                 return;
596         case CONNMAN_SERVICE_TYPE_ETHERNET:
597         case CONNMAN_SERVICE_TYPE_WIFI:
598         case CONNMAN_SERVICE_TYPE_WIMAX:
599         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
600         case CONNMAN_SERVICE_TYPE_CELLULAR:
601         case CONNMAN_SERVICE_TYPE_GPS:
602         case CONNMAN_SERVICE_TYPE_VPN:
603         case CONNMAN_SERVICE_TYPE_GADGET:
604                 break;
605         }
606
607         connman_info("Remove interface %s [ %s ]", name,
608                                 __connman_service_type2string(type));
609
610         for (list = technology_list; list; list = list->next) {
611                 struct connman_technology *technology = list->data;
612
613                 if (technology->type != type)
614                         continue;
615
616                 if (technology->driver == NULL)
617                         continue;
618
619                 if (technology->driver->remove_interface)
620                         technology->driver->remove_interface(technology, index);
621
622                 technology_put(technology);
623         }
624 }
625
626 static void unregister_technology(gpointer data)
627 {
628         struct connman_technology *technology = data;
629
630         technology_put(technology);
631 }
632
633 int __connman_technology_add_device(struct connman_device *device)
634 {
635         struct connman_technology *technology;
636         enum connman_service_type type;
637
638         DBG("device %p", device);
639
640         type = __connman_device_get_service_type(device);
641         __connman_notifier_register(type);
642
643         technology = technology_get(type);
644         if (technology == NULL)
645                 return -ENXIO;
646
647         g_hash_table_insert(device_table, device, technology);
648
649         if (g_atomic_int_get(&technology->blocked))
650                 goto done;
651
652         technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
653
654         state_changed(technology);
655
656 done:
657
658         technology->device_list = g_slist_append(technology->device_list,
659                                                                 device);
660
661         return 0;
662 }
663
664 int __connman_technology_remove_device(struct connman_device *device)
665 {
666         struct connman_technology *technology;
667         enum connman_service_type type;
668
669         DBG("device %p", device);
670
671         type = __connman_device_get_service_type(device);
672         __connman_notifier_unregister(type);
673
674         technology = g_hash_table_lookup(device_table, device);
675         if (technology == NULL)
676                 return -ENXIO;
677
678         technology->device_list = g_slist_remove(technology->device_list,
679                                                                 device);
680         if (technology->device_list == NULL) {
681                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
682                 state_changed(technology);
683         }
684
685         g_hash_table_remove(device_table, device);
686
687         return 0;
688 }
689
690 int __connman_technology_enable_device(struct connman_device *device)
691 {
692         struct connman_technology *technology;
693         enum connman_service_type type;
694
695         DBG("device %p", device);
696
697         technology = g_hash_table_lookup(device_table, device);
698         if (technology == NULL)
699                 return -ENXIO;
700
701         if (g_atomic_int_get(&technology->blocked))
702                 return -ERFKILL;
703
704         type = __connman_device_get_service_type(device);
705         __connman_notifier_enable(type);
706
707         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
708                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
709                 state_changed(technology);
710         }
711
712         return 0;
713 }
714
715 int __connman_technology_disable_device(struct connman_device *device)
716 {
717         struct connman_technology *technology;
718         enum connman_service_type type;
719         GSList *list;
720
721         DBG("device %p", device);
722
723         type = __connman_device_get_service_type(device);
724         __connman_notifier_disable(type);
725
726         technology = g_hash_table_lookup(device_table, device);
727         if (technology == NULL)
728                 return -ENXIO;
729
730         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
731                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
732                 state_changed(technology);
733         }
734
735         for (list = technology->device_list; list; list = list->next) {
736                 struct connman_device *device = list->data;
737
738                 if (__connman_device_get_blocked(device) == FALSE)
739                         return 0;
740         }
741
742         technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
743         state_changed(technology);
744
745         return 0;
746 }
747
748 static void technology_blocked(struct connman_technology *technology,
749                                 connman_bool_t blocked)
750 {
751         GSList *list;
752
753         for (list = technology->device_list; list; list = list->next) {
754                 struct connman_device *device = list->data;
755
756                 __connman_device_set_blocked(device, blocked);
757         }
758 }
759
760 int __connman_technology_add_rfkill(unsigned int index,
761                                         enum connman_service_type type,
762                                                 connman_bool_t softblock,
763                                                 connman_bool_t hardblock)
764 {
765         struct connman_technology *technology;
766         struct connman_rfkill *rfkill;
767         connman_bool_t blocked;
768
769         DBG("index %u type %d soft %u hard %u", index, type,
770                                                         softblock, hardblock);
771
772         technology = technology_get(type);
773         if (technology == NULL)
774                 return -ENXIO;
775
776         rfkill = g_try_new0(struct connman_rfkill, 1);
777         if (rfkill == NULL)
778                 return -ENOMEM;
779
780         rfkill->index = index;
781         rfkill->type = type;
782         rfkill->softblock = softblock;
783         rfkill->hardblock = hardblock;
784
785         g_hash_table_replace(rfkill_table, &rfkill->index, technology);
786
787         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
788
789         blocked = (softblock || hardblock) ? TRUE : FALSE;
790         if (blocked == FALSE)
791                 return 0;
792
793         if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
794                 technology_blocked(technology, TRUE);
795
796                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
797                 state_changed(technology);
798         }
799
800         return 0;
801 }
802
803 int __connman_technology_update_rfkill(unsigned int index,
804                                                 connman_bool_t softblock,
805                                                 connman_bool_t hardblock)
806 {
807         struct connman_technology *technology;
808         struct connman_rfkill *rfkill;
809         connman_bool_t blocked, old_blocked;
810
811         DBG("index %u soft %u hard %u", index, softblock, hardblock);
812
813         technology = g_hash_table_lookup(rfkill_table, &index);
814         if (technology == NULL)
815                 return -ENXIO;
816
817         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
818         if (rfkill == NULL)
819                 return -ENXIO;
820
821         old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
822         blocked = (softblock || hardblock) ? TRUE : FALSE;
823
824         rfkill->softblock = softblock;
825         rfkill->hardblock = hardblock;
826
827         if (blocked == old_blocked)
828                 return 0;
829
830         if (blocked) {
831                 guint n_blocked;
832
833                 n_blocked =
834                         g_atomic_int_exchange_and_add(&technology->blocked, 1);
835                 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
836                         return 0;
837
838                 technology_blocked(technology, blocked);
839                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
840                 state_changed(technology);
841         } else {
842                 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
843                         return 0;
844
845                 technology_blocked(technology, blocked);
846                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
847                 state_changed(technology);
848         }
849
850         return 0;
851 }
852
853 int __connman_technology_remove_rfkill(unsigned int index)
854 {
855         struct connman_technology *technology;
856         struct connman_rfkill *rfkill;
857         connman_bool_t blocked;
858
859         DBG("index %u", index);
860
861         technology = g_hash_table_lookup(rfkill_table, &index);
862         if (technology == NULL)
863                 return -ENXIO;
864
865         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
866         if (rfkill == NULL)
867                 return -ENXIO;
868
869         blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
870
871         g_hash_table_remove(technology->rfkill_list, &index);
872
873         g_hash_table_remove(rfkill_table, &index);
874
875         if (blocked &&
876                 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
877                 technology_blocked(technology, FALSE);
878                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
879                 state_changed(technology);
880         }
881
882         return 0;
883 }
884
885 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
886 {
887         struct connman_technology *technology;
888
889         technology = technology_find(type);
890         if (technology == NULL)
891                 return FALSE;
892
893         if (g_atomic_int_get(&technology->blocked))
894                 return TRUE;
895
896         return FALSE;
897 }
898
899 int __connman_technology_init(void)
900 {
901         DBG("");
902
903         connection = connman_dbus_get_connection();
904
905         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
906                                                 NULL, unregister_technology);
907         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
908                                                 NULL, unregister_technology);
909
910         return 0;
911 }
912
913 void __connman_technology_cleanup(void)
914 {
915         DBG("");
916
917         g_hash_table_destroy(device_table);
918         g_hash_table_destroy(rfkill_table);
919
920         dbus_connection_unref(connection);
921 }