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