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