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