manager: Remove manager 'Technologies' property
[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 static connman_bool_t global_offlinemode;
38
39 struct connman_rfkill {
40         unsigned int index;
41         enum connman_service_type type;
42         connman_bool_t softblock;
43         connman_bool_t hardblock;
44 };
45
46 enum connman_technology_state {
47         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
48         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
49         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 2,
50         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 3,
51 };
52
53 struct connman_technology {
54         int refcount;
55         enum connman_service_type type;
56         enum connman_technology_state state;
57         char *path;
58         GHashTable *rfkill_list;
59         GSList *device_list;
60         int enabled;
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         GSList *list;
158
159         DBG("technology %p enabled %u", technology, enabled);
160
161         if (technology->tethering == enabled)
162                 return;
163
164         technology->tethering = enabled;
165
166         tethering_changed(technology);
167
168         if (enabled == TRUE)
169                 __connman_tethering_set_enabled();
170         else {
171                 for (list = technology_list; list; list = list->next) {
172                         struct connman_technology *other_tech = list->data;
173                         if (other_tech->tethering == TRUE)
174                                 break;
175                 }
176                 if (list == NULL)
177                         __connman_tethering_set_disabled();
178         }
179 }
180
181 static int set_tethering(struct connman_technology *technology,
182                                 connman_bool_t enabled)
183 {
184         const char *ident, *passphrase, *bridge;
185
186         ident = technology->tethering_ident;
187         passphrase = technology->tethering_passphrase;
188
189         if (technology->driver == NULL ||
190                         technology->driver->set_tethering == NULL)
191                 return -EOPNOTSUPP;
192
193         bridge = __connman_tethering_get_bridge();
194         if (bridge == NULL)
195                 return -EOPNOTSUPP;
196
197         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
198             (ident == NULL || passphrase == NULL))
199                 return -EINVAL;
200
201         return technology->driver->set_tethering(technology, ident, passphrase,
202                                                         bridge, enabled);
203 }
204
205 void connman_technology_regdom_notify(struct connman_technology *technology,
206                                                         const char *alpha2)
207 {
208         DBG("");
209
210         if (alpha2 == NULL)
211                 connman_error("Failed to set regulatory domain");
212         else
213                 DBG("Regulatory domain set to %s", alpha2);
214
215         g_free(technology->regdom);
216         technology->regdom = g_strdup(alpha2);
217 }
218
219 int connman_technology_set_regdom(const char *alpha2)
220 {
221         GSList *list;
222
223         for (list = technology_list; list; list = list->next) {
224                 struct connman_technology *technology = list->data;
225
226                 if (technology->driver == NULL)
227                         continue;
228
229                 if (technology->driver->set_regdom)
230                         technology->driver->set_regdom(technology, alpha2);
231         }
232
233         return 0;
234 }
235
236 static void free_rfkill(gpointer data)
237 {
238         struct connman_rfkill *rfkill = data;
239
240         g_free(rfkill);
241 }
242
243 static const char *state2string(enum connman_technology_state state)
244 {
245         switch (state) {
246         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
247                 break;
248         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
249                 return "offline";
250         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
251                 return "enabled";
252         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
253                 return "connected";
254         }
255
256         return NULL;
257 }
258
259 static void state_changed(struct connman_technology *technology)
260 {
261         const char *str;
262
263         str = state2string(technology->state);
264         if (str == NULL)
265                 return;
266
267         connman_dbus_property_changed_basic(technology->path,
268                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
269                                                 DBUS_TYPE_STRING, &str);
270 }
271
272 static const char *get_name(enum connman_service_type type)
273 {
274         switch (type) {
275         case CONNMAN_SERVICE_TYPE_UNKNOWN:
276         case CONNMAN_SERVICE_TYPE_SYSTEM:
277         case CONNMAN_SERVICE_TYPE_GPS:
278         case CONNMAN_SERVICE_TYPE_VPN:
279         case CONNMAN_SERVICE_TYPE_GADGET:
280                 break;
281         case CONNMAN_SERVICE_TYPE_ETHERNET:
282                 return "Wired";
283         case CONNMAN_SERVICE_TYPE_WIFI:
284                 return "WiFi";
285         case CONNMAN_SERVICE_TYPE_WIMAX:
286                 return "WiMAX";
287         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
288                 return "Bluetooth";
289         case CONNMAN_SERVICE_TYPE_CELLULAR:
290                 return "Cellular";
291         }
292
293         return NULL;
294 }
295
296 static void load_state(struct connman_technology *technology)
297 {
298         GKeyFile *keyfile;
299         gchar *identifier;
300         GError *error = NULL;
301         connman_bool_t enable;
302
303         DBG("technology %p", technology);
304
305         keyfile = __connman_storage_load_global();
306         /* Fallback on disabling technology if file not found. */
307         if (keyfile == NULL) {
308                 technology->enable_persistent = FALSE;
309                 return;
310         }
311
312         identifier = g_strdup_printf("%s", get_name(technology->type));
313         if (identifier == NULL)
314                 goto done;
315
316         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
317         if (error == NULL)
318                 technology->enable_persistent = enable;
319         else {
320                 technology->enable_persistent = FALSE;
321                 g_clear_error(&error);
322         }
323 done:
324         g_free(identifier);
325
326         g_key_file_free(keyfile);
327
328         return;
329 }
330
331 static void save_state(struct connman_technology *technology)
332 {
333         GKeyFile *keyfile;
334         gchar *identifier;
335
336         DBG("technology %p", technology);
337
338         keyfile = __connman_storage_load_global();
339         if (keyfile == NULL)
340                 keyfile = g_key_file_new();
341
342         identifier = g_strdup_printf("%s", get_name(technology->type));
343         if (identifier == NULL)
344                 goto done;
345
346         g_key_file_set_boolean(keyfile, identifier, "Enable",
347                                 technology->enable_persistent);
348
349 done:
350         g_free(identifier);
351
352         __connman_storage_save_global(keyfile);
353
354         g_key_file_free(keyfile);
355
356         return;
357 }
358
359 connman_bool_t __connman_technology_get_offlinemode(void)
360 {
361         return global_offlinemode;
362 }
363
364 static void connman_technology_save_offlinemode()
365 {
366         GKeyFile *keyfile;
367
368         keyfile = __connman_storage_load_global();
369         if (keyfile == NULL)
370                 keyfile = g_key_file_new();
371
372         g_key_file_set_boolean(keyfile, "global",
373                                         "OfflineMode", global_offlinemode);
374
375         __connman_storage_save_global(keyfile);
376
377         g_key_file_free(keyfile);
378
379         return;
380 }
381
382 static connman_bool_t connman_technology_load_offlinemode()
383 {
384         GKeyFile *keyfile;
385         GError *error = NULL;
386         connman_bool_t offlinemode;
387
388         /* If there is a error, we enable offlinemode */
389         keyfile = __connman_storage_load_global();
390         if (keyfile == NULL)
391                 return TRUE;
392
393         offlinemode = g_key_file_get_boolean(keyfile, "global",
394                                                 "OfflineMode", &error);
395         if (error != NULL) {
396                 offlinemode = TRUE;
397                 g_clear_error(&error);
398         }
399
400         g_key_file_free(keyfile);
401
402         return offlinemode;
403 }
404
405 static void append_properties(DBusMessageIter *iter,
406                 struct connman_technology *technology)
407 {
408         DBusMessageIter dict;
409         const char *str;
410
411         connman_dbus_dict_open(iter, &dict);
412
413         str = state2string(technology->state);
414         if (str != NULL)
415                 connman_dbus_dict_append_basic(&dict, "State",
416                                                 DBUS_TYPE_STRING, &str);
417
418         str = get_name(technology->type);
419         if (str != NULL)
420                 connman_dbus_dict_append_basic(&dict, "Name",
421                                                 DBUS_TYPE_STRING, &str);
422
423         str = __connman_service_type2string(technology->type);
424         if (str != NULL)
425                 connman_dbus_dict_append_basic(&dict, "Type",
426                                                 DBUS_TYPE_STRING, &str);
427
428         connman_dbus_dict_append_basic(&dict, "Tethering",
429                                         DBUS_TYPE_BOOLEAN,
430                                         &technology->tethering);
431
432         if (technology->tethering_ident != NULL)
433                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
434                                                 DBUS_TYPE_STRING,
435                                                 &technology->tethering_ident);
436
437         if (technology->tethering_passphrase != NULL)
438                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
439                                                 DBUS_TYPE_STRING,
440                                                 &technology->tethering_passphrase);
441
442         connman_dbus_dict_close(iter, &dict);
443 }
444
445 static void technology_added_signal(struct connman_technology *technology)
446 {
447         DBusMessage *signal;
448         DBusMessageIter iter;
449
450         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
451                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
452         if (signal == NULL)
453                 return;
454
455         dbus_message_iter_init_append(signal, &iter);
456         append_properties(&iter, technology);
457
458         dbus_connection_send(connection, signal, NULL);
459         dbus_message_unref(signal);
460 }
461
462 static void technology_removed_signal(struct connman_technology *technology)
463 {
464         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
465                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
466                         DBUS_TYPE_OBJECT_PATH, technology->path);
467 }
468
469 static DBusMessage *get_properties(DBusConnection *conn,
470                                         DBusMessage *message, void *user_data)
471 {
472         struct connman_technology *technology = user_data;
473         DBusMessage *reply;
474         DBusMessageIter iter;
475
476         reply = dbus_message_new_method_return(message);
477         if (reply == NULL)
478                 return NULL;
479
480         dbus_message_iter_init_append(reply, &iter);
481         append_properties(&iter, technology);
482
483         return reply;
484 }
485
486 void __connman_technology_list_struct(DBusMessageIter *array)
487 {
488         GSList *list;
489         DBusMessageIter entry;
490
491         for (list = technology_list; list; list = list->next) {
492                 struct connman_technology *technology = list->data;
493
494                 if (technology->path == NULL)
495                         continue;
496
497                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
498                                 NULL, &entry);
499                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
500                                 &technology->path);
501                 append_properties(&entry, technology);
502                 dbus_message_iter_close_container(array, &entry);
503         }
504 }
505
506 static DBusMessage *set_property(DBusConnection *conn,
507                                         DBusMessage *msg, void *data)
508 {
509         struct connman_technology *technology = data;
510         DBusMessageIter iter, value;
511         const char *name;
512         int type;
513
514         DBG("conn %p", conn);
515
516         if (dbus_message_iter_init(msg, &iter) == FALSE)
517                 return __connman_error_invalid_arguments(msg);
518
519         dbus_message_iter_get_basic(&iter, &name);
520         dbus_message_iter_next(&iter);
521         dbus_message_iter_recurse(&iter, &value);
522
523         type = dbus_message_iter_get_arg_type(&value);
524
525         DBG("property %s", name);
526
527         if (g_str_equal(name, "Tethering") == TRUE) {
528                 int err;
529                 connman_bool_t tethering;
530
531                 if (type != DBUS_TYPE_BOOLEAN)
532                         return __connman_error_invalid_arguments(msg);
533
534                 dbus_message_iter_get_basic(&value, &tethering);
535
536                 if (technology->tethering == tethering)
537                         return __connman_error_in_progress(msg);
538
539                 err = set_tethering(technology, tethering);
540                 if (err < 0)
541                         return __connman_error_failed(msg, -err);
542
543         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
544                 const char *str;
545
546                 dbus_message_iter_get_basic(&value, &str);
547
548                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
549                         return __connman_error_not_supported(msg);
550
551                 technology->tethering_ident = g_strdup(str);
552         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
553                 const char *str;
554
555                 dbus_message_iter_get_basic(&value, &str);
556
557                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
558                         return __connman_error_not_supported(msg);
559
560                 if (strlen(str) < 8)
561                         return __connman_error_invalid_arguments(msg);
562
563                 technology->tethering_passphrase = g_strdup(str);
564         } else
565                 return __connman_error_invalid_property(msg);
566
567         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
568 }
569
570 static GDBusMethodTable technology_methods[] = {
571         { "GetProperties", "",   "a{sv}", get_properties },
572         { "SetProperty",   "sv", "",      set_property   },
573         { },
574 };
575
576 static GDBusSignalTable technology_signals[] = {
577         { "PropertyChanged", "sv" },
578         { },
579 };
580
581 static struct connman_technology *technology_find(enum connman_service_type type)
582 {
583         GSList *list;
584
585         DBG("type %d", type);
586
587         for (list = technology_list; list; list = list->next) {
588                 struct connman_technology *technology = list->data;
589
590                 if (technology->type == type)
591                         return technology;
592         }
593
594         return NULL;
595 }
596
597 static struct connman_technology *technology_get(enum connman_service_type type)
598 {
599         struct connman_technology *technology;
600         const char *str;
601         GSList *list;
602
603         DBG("type %d", type);
604
605         technology = technology_find(type);
606         if (technology != NULL) {
607                 __sync_fetch_and_add(&technology->refcount, 1);
608                 goto done;
609         }
610
611         str = __connman_service_type2string(type);
612         if (str == NULL)
613                 return NULL;
614
615         technology = g_try_new0(struct connman_technology, 1);
616         if (technology == NULL)
617                 return NULL;
618
619         technology->refcount = 1;
620
621         technology->type = type;
622         technology->path = g_strdup_printf("%s/technology/%s",
623                                                         CONNMAN_PATH, str);
624
625         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
626                                                         NULL, free_rfkill);
627         technology->device_list = NULL;
628
629         technology->pending_reply = NULL;
630         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
631
632         load_state(technology);
633
634         if (g_dbus_register_interface(connection, technology->path,
635                                         CONNMAN_TECHNOLOGY_INTERFACE,
636                                         technology_methods, technology_signals,
637                                         NULL, technology, NULL) == FALSE) {
638                 connman_error("Failed to register %s", technology->path);
639                 g_free(technology);
640                 return NULL;
641         }
642
643         technology_list = g_slist_append(technology_list, technology);
644
645         technology_added_signal(technology);
646
647         if (technology->driver != NULL)
648                 goto done;
649
650         for (list = driver_list; list; list = list->next) {
651                 struct connman_technology_driver *driver = list->data;
652
653                 DBG("driver %p name %s", driver, driver->name);
654
655                 if (driver->type != technology->type)
656                         continue;
657
658                 if (driver->probe(technology) == 0) {
659                         technology->driver = driver;
660                         break;
661                 }
662         }
663
664 done:
665         DBG("technology %p", technology);
666
667         return technology;
668 }
669
670 static void technology_put(struct connman_technology *technology)
671 {
672         DBG("technology %p", technology);
673
674         if (__sync_fetch_and_sub(&technology->refcount, 1) != 1)
675                 return;
676
677         if (technology->driver) {
678                 technology->driver->remove(technology);
679                 technology->driver = NULL;
680         }
681
682         technology_list = g_slist_remove(technology_list, technology);
683
684         technology_removed_signal(technology);
685
686         g_dbus_unregister_interface(connection, technology->path,
687                                                 CONNMAN_TECHNOLOGY_INTERFACE);
688
689         g_slist_free(technology->device_list);
690         g_hash_table_destroy(technology->rfkill_list);
691
692         g_free(technology->path);
693         g_free(technology->regdom);
694         g_free(technology);
695 }
696
697 void __connman_technology_add_interface(enum connman_service_type type,
698                                 int index, const char *name, const char *ident)
699 {
700         struct connman_technology *technology;
701
702         switch (type) {
703         case CONNMAN_SERVICE_TYPE_UNKNOWN:
704         case CONNMAN_SERVICE_TYPE_SYSTEM:
705                 return;
706         case CONNMAN_SERVICE_TYPE_ETHERNET:
707         case CONNMAN_SERVICE_TYPE_WIFI:
708         case CONNMAN_SERVICE_TYPE_WIMAX:
709         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
710         case CONNMAN_SERVICE_TYPE_CELLULAR:
711         case CONNMAN_SERVICE_TYPE_GPS:
712         case CONNMAN_SERVICE_TYPE_VPN:
713         case CONNMAN_SERVICE_TYPE_GADGET:
714                 break;
715         }
716
717         connman_info("Create interface %s [ %s ]", name,
718                                 __connman_service_type2string(type));
719
720         technology = technology_get(type);
721
722         if (technology == NULL || technology->driver == NULL
723                         || technology->driver->add_interface == NULL)
724                 return;
725
726         technology->driver->add_interface(technology,
727                                         index, name, ident);
728 }
729
730 void __connman_technology_remove_interface(enum connman_service_type type,
731                                 int index, const char *name, const char *ident)
732 {
733         struct connman_technology *technology;
734
735         switch (type) {
736         case CONNMAN_SERVICE_TYPE_UNKNOWN:
737         case CONNMAN_SERVICE_TYPE_SYSTEM:
738                 return;
739         case CONNMAN_SERVICE_TYPE_ETHERNET:
740         case CONNMAN_SERVICE_TYPE_WIFI:
741         case CONNMAN_SERVICE_TYPE_WIMAX:
742         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
743         case CONNMAN_SERVICE_TYPE_CELLULAR:
744         case CONNMAN_SERVICE_TYPE_GPS:
745         case CONNMAN_SERVICE_TYPE_VPN:
746         case CONNMAN_SERVICE_TYPE_GADGET:
747                 break;
748         }
749
750         connman_info("Remove interface %s [ %s ]", name,
751                                 __connman_service_type2string(type));
752
753         technology = technology_find(type);
754
755         if (technology == NULL || technology->driver == NULL)
756                 return;
757
758         if (technology->driver->remove_interface)
759                 technology->driver->remove_interface(technology, index);
760
761         technology_put(technology);
762 }
763
764 int __connman_technology_add_device(struct connman_device *device)
765 {
766         struct connman_technology *technology;
767         enum connman_service_type type;
768
769         DBG("device %p", device);
770
771         type = __connman_device_get_service_type(device);
772         __connman_notifier_register(type);
773
774         technology = technology_get(type);
775         if (technology == NULL)
776                 return -ENXIO;
777
778         if (technology->enable_persistent && !global_offlinemode)
779                 __connman_device_enable(device);
780         /* if technology persistent state is offline */
781         if (!technology->enable_persistent)
782                 __connman_device_disable(device);
783
784         technology->device_list = g_slist_append(technology->device_list,
785                                                                 device);
786
787         return 0;
788 }
789
790 int __connman_technology_remove_device(struct connman_device *device)
791 {
792         struct connman_technology *technology;
793         enum connman_service_type type;
794
795         DBG("device %p", device);
796
797         type = __connman_device_get_service_type(device);
798         __connman_notifier_unregister(type);
799
800         technology = technology_find(type);
801         if (technology == NULL)
802                 return -ENXIO;
803
804         technology->device_list = g_slist_remove(technology->device_list,
805                                                                 device);
806         if (technology->device_list == NULL) {
807                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
808                 state_changed(technology);
809         }
810
811         return 0;
812 }
813
814 static gboolean technology_pending_reply(gpointer user_data)
815 {
816         struct connman_technology *technology = user_data;
817         DBusMessage *reply;
818
819         /* Power request timedout, send ETIMEDOUT. */
820         if (technology->pending_reply != NULL) {
821                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
822                 if (reply != NULL)
823                         g_dbus_send_message(connection, reply);
824
825                 dbus_message_unref(technology->pending_reply);
826                 technology->pending_reply = NULL;
827                 technology->pending_timeout = 0;
828         }
829
830         return FALSE;
831 }
832
833 int __connman_technology_enabled(enum connman_service_type type)
834 {
835         struct connman_technology *technology;
836
837         technology = technology_find(type);
838         if (technology == NULL)
839                 return -ENXIO;
840
841         if (__sync_fetch_and_add(&technology->enabled, 1) == 0) {
842                 __connman_notifier_enable(type);
843                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
844                 state_changed(technology);
845         }
846
847         if (technology->pending_reply != NULL) {
848                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
849                 dbus_message_unref(technology->pending_reply);
850                 g_source_remove(technology->pending_timeout);
851                 technology->pending_reply = NULL;
852                 technology->pending_timeout = 0;
853         }
854
855         return 0;
856 }
857
858 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
859 {
860         struct connman_technology *technology;
861         GSList *list;
862         int err = 0;
863         int ret = -ENODEV;
864         DBusMessage *reply;
865
866         DBG("type %d enable", type);
867
868         technology = technology_find(type);
869         if (technology == NULL) {
870                 err = -ENXIO;
871                 goto done;
872         }
873
874         if (technology->pending_reply != NULL) {
875                 err = -EBUSY;
876                 goto done;
877         }
878
879         if (msg != NULL) {
880                 /*
881                  * This is a bit of a trick. When msg is not NULL it means
882                  * thats technology_enable was invoked from the manager API. Hence we save
883                  * the state here.
884                  */
885                 technology->enable_persistent = TRUE;
886                 save_state(technology);
887         }
888
889         __connman_rfkill_block(technology->type, FALSE);
890
891         /*
892          * An empty device list means that devices in the technology
893          * were rfkill blocked. The unblock above will enable the devs.
894          */
895         if (technology->device_list == NULL)
896                 return 0;
897
898         for (list = technology->device_list; list; list = list->next) {
899                 struct connman_device *device = list->data;
900
901                 err = __connman_device_enable(device);
902                 /*
903                  * err = 0 : Device was enabled right away.
904                  * If atleast one device gets enabled, we consider
905                  * the technology to be enabled.
906                  */
907                 if (err == 0)
908                         ret = 0;
909         }
910
911 done:
912         if (ret == 0) {
913                 if (msg != NULL)
914                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
915                 return ret;
916         }
917
918         if (msg != NULL) {
919                 if (err == -EINPROGRESS) {
920                         technology->pending_reply = dbus_message_ref(msg);
921                         technology->pending_timeout = g_timeout_add_seconds(10,
922                                         technology_pending_reply, technology);
923                 } else {
924                         reply = __connman_error_failed(msg, -err);
925                         if (reply != NULL)
926                                 g_dbus_send_message(connection, reply);
927                 }
928         }
929
930         return err;
931 }
932
933 int __connman_technology_disabled(enum connman_service_type type)
934 {
935         struct connman_technology *technology;
936
937         technology = technology_find(type);
938         if (technology == NULL)
939                 return -ENXIO;
940
941         if (technology->pending_reply != NULL) {
942                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
943                 dbus_message_unref(technology->pending_reply);
944                 g_source_remove(technology->pending_timeout);
945                 technology->pending_reply = NULL;
946                 technology->pending_timeout = 0;
947         }
948
949         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
950                 return 0;
951
952         __connman_notifier_disable(type);
953         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
954         state_changed(technology);
955
956         return 0;
957 }
958
959 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
960 {
961         struct connman_technology *technology;
962         GSList *list;
963         int err = 0;
964         int ret = -ENODEV;
965         DBusMessage *reply;
966
967         DBG("type %d disable", type);
968
969         technology = technology_find(type);
970         if (technology == NULL) {
971                 err = -ENXIO;
972                 goto done;
973         }
974
975         if (technology->pending_reply != NULL) {
976                 err = -EBUSY;
977                 goto done;
978         }
979
980         if (technology->tethering == TRUE)
981                 set_tethering(technology, FALSE);
982
983         if (msg != NULL) {
984                 technology->enable_persistent = FALSE;
985                 save_state(technology);
986         }
987
988         __connman_rfkill_block(technology->type, TRUE);
989
990         for (list = technology->device_list; list; list = list->next) {
991                 struct connman_device *device = list->data;
992
993                 err = __connman_device_disable(device);
994                 if (err == 0)
995                         ret = 0;
996         }
997
998 done:
999         if (ret == 0) {
1000                 if (msg != NULL)
1001                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
1002                 return ret;
1003         }
1004
1005         if (msg != NULL) {
1006                 if (err == -EINPROGRESS) {
1007                         technology->pending_reply = dbus_message_ref(msg);
1008                         technology->pending_timeout = g_timeout_add_seconds(10,
1009                                         technology_pending_reply, technology);
1010                 } else {
1011                         reply = __connman_error_failed(msg, -err);
1012                         if (reply != NULL)
1013                                 g_dbus_send_message(connection, reply);
1014                 }
1015         }
1016
1017         return err;
1018 }
1019
1020 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1021 {
1022         GSList *list;
1023         int err = -EINVAL;
1024
1025         if (global_offlinemode == offlinemode)
1026                 return 0;
1027
1028         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1029
1030         /*
1031          * This is a bit tricky. When you set offlinemode, there is no
1032          * way to differentiate between attempting offline mode and
1033          * resuming offlinemode from last saved profile. We need that
1034          * information in rfkill_update, otherwise it falls back on the
1035          * technology's persistent state. Hence we set the offline mode here
1036          * but save it & call the notifier only if its successful.
1037          */
1038
1039         global_offlinemode = offlinemode;
1040
1041         /* Traverse technology list, enable/disable each technology. */
1042         for (list = technology_list; list; list = list->next) {
1043                 struct connman_technology *technology = list->data;
1044
1045                 if (offlinemode)
1046                         err = __connman_technology_disable(technology->type, NULL);
1047
1048                 if (!offlinemode && technology->enable_persistent)
1049                         err = __connman_technology_enable(technology->type, NULL);
1050         }
1051
1052         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1053                 connman_technology_save_offlinemode();
1054                 __connman_notifier_offlinemode(offlinemode);
1055         } else
1056                 global_offlinemode = connman_technology_load_offlinemode();
1057
1058         return err;
1059 }
1060
1061 int __connman_technology_add_rfkill(unsigned int index,
1062                                         enum connman_service_type type,
1063                                                 connman_bool_t softblock,
1064                                                 connman_bool_t hardblock)
1065 {
1066         struct connman_technology *technology;
1067         struct connman_rfkill *rfkill;
1068
1069         DBG("index %u type %d soft %u hard %u", index, type,
1070                                                         softblock, hardblock);
1071
1072         technology = technology_get(type);
1073         if (technology == NULL)
1074                 return -ENXIO;
1075
1076         rfkill = g_try_new0(struct connman_rfkill, 1);
1077         if (rfkill == NULL)
1078                 return -ENOMEM;
1079
1080         __connman_notifier_register(type);
1081
1082         rfkill->index = index;
1083         rfkill->type = type;
1084         rfkill->softblock = softblock;
1085         rfkill->hardblock = hardblock;
1086
1087         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
1088
1089         if (hardblock) {
1090                 DBG("%s is switched off.", get_name(type));
1091                 return 0;
1092         }
1093
1094         /*
1095          * If Offline mode is on, we softblock the device if it isnt already.
1096          * If Offline mode is off, we rely on the persistent state of tech.
1097          */
1098         if (global_offlinemode) {
1099                 if (!softblock)
1100                         return __connman_rfkill_block(type, TRUE);
1101         } else {
1102                 if (technology->enable_persistent && softblock)
1103                         return __connman_rfkill_block(type, FALSE);
1104                 /* if technology persistent state is offline */
1105                 if (!technology->enable_persistent && !softblock)
1106                         return __connman_rfkill_block(type, TRUE);
1107         }
1108
1109         return 0;
1110 }
1111
1112 int __connman_technology_update_rfkill(unsigned int index,
1113                                         enum connman_service_type type,
1114                                                 connman_bool_t softblock,
1115                                                 connman_bool_t hardblock)
1116 {
1117         struct connman_technology *technology;
1118         struct connman_rfkill *rfkill;
1119
1120         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1121
1122         technology = technology_find(type);
1123         if (technology == NULL)
1124                 return -ENXIO;
1125
1126         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1127         if (rfkill == NULL)
1128                 return -ENXIO;
1129
1130         if (rfkill->softblock == softblock &&
1131                 rfkill->hardblock == hardblock)
1132                 return 0;
1133
1134         rfkill->softblock = softblock;
1135         rfkill->hardblock = hardblock;
1136
1137         if (hardblock) {
1138                 DBG("%s is switched off.", get_name(type));
1139                 return 0;
1140         }
1141
1142         if (!global_offlinemode) {
1143                 if (technology->enable_persistent && softblock)
1144                         return __connman_rfkill_block(type, FALSE);
1145                 if (!technology->enable_persistent && !softblock)
1146                         return __connman_rfkill_block(type, TRUE);
1147         }
1148
1149         return 0;
1150 }
1151
1152 int __connman_technology_remove_rfkill(unsigned int index,
1153                                         enum connman_service_type type)
1154 {
1155         struct connman_technology *technology;
1156         struct connman_rfkill *rfkill;
1157
1158         DBG("index %u", index);
1159
1160         technology = technology_find(type);
1161         if (technology == NULL)
1162                 return -ENXIO;
1163
1164         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1165         if (rfkill == NULL)
1166                 return -ENXIO;
1167
1168         g_hash_table_remove(technology->rfkill_list, &index);
1169
1170         technology_put(technology);
1171
1172         return 0;
1173 }
1174
1175 int __connman_technology_init(void)
1176 {
1177         DBG("");
1178
1179         connection = connman_dbus_get_connection();
1180
1181         global_offlinemode = connman_technology_load_offlinemode();
1182
1183         return 0;
1184 }
1185
1186 void __connman_technology_cleanup(void)
1187 {
1188         DBG("");
1189
1190         dbus_connection_unref(connection);
1191 }