technology: Add 'TechnologyAdded' and 'TechnologyRemoved' signals
[framework/connectivity/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <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 void append_properties(DBusMessageIter *iter,
428                 struct connman_technology *technology)
429 {
430         DBusMessageIter dict;
431         const char *str;
432
433         connman_dbus_dict_open(iter, &dict);
434
435         str = state2string(technology->state);
436         if (str != NULL)
437                 connman_dbus_dict_append_basic(&dict, "State",
438                                                 DBUS_TYPE_STRING, &str);
439
440         str = get_name(technology->type);
441         if (str != NULL)
442                 connman_dbus_dict_append_basic(&dict, "Name",
443                                                 DBUS_TYPE_STRING, &str);
444
445         str = __connman_service_type2string(technology->type);
446         if (str != NULL)
447                 connman_dbus_dict_append_basic(&dict, "Type",
448                                                 DBUS_TYPE_STRING, &str);
449
450         connman_dbus_dict_append_basic(&dict, "Tethering",
451                                         DBUS_TYPE_BOOLEAN,
452                                         &technology->tethering);
453
454         if (technology->tethering_ident != NULL)
455                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
456                                                 DBUS_TYPE_STRING,
457                                                 &technology->tethering_ident);
458
459         if (technology->tethering_passphrase != NULL)
460                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
461                                                 DBUS_TYPE_STRING,
462                                                 &technology->tethering_passphrase);
463
464         connman_dbus_dict_close(iter, &dict);
465 }
466
467 static void technology_added_signal(struct connman_technology *technology)
468 {
469         DBusMessage *signal;
470         DBusMessageIter iter;
471
472         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
473                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
474         if (signal == NULL)
475                 return;
476
477         dbus_message_iter_init_append(signal, &iter);
478         append_properties(&iter, technology);
479
480         dbus_connection_send(connection, signal, NULL);
481         dbus_message_unref(signal);
482 }
483
484 static void technology_removed_signal(struct connman_technology *technology)
485 {
486         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
487                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
488                         DBUS_TYPE_OBJECT_PATH, technology->path);
489 }
490
491 static DBusMessage *get_properties(DBusConnection *conn,
492                                         DBusMessage *message, void *user_data)
493 {
494         struct connman_technology *technology = user_data;
495         DBusMessage *reply;
496         DBusMessageIter iter;
497
498         reply = dbus_message_new_method_return(message);
499         if (reply == NULL)
500                 return NULL;
501
502         dbus_message_iter_init_append(reply, &iter);
503         append_properties(&iter, technology);
504
505         return reply;
506 }
507
508 static DBusMessage *set_property(DBusConnection *conn,
509                                         DBusMessage *msg, void *data)
510 {
511         struct connman_technology *technology = data;
512         DBusMessageIter iter, value;
513         const char *name;
514         int type;
515
516         DBG("conn %p", conn);
517
518         if (dbus_message_iter_init(msg, &iter) == FALSE)
519                 return __connman_error_invalid_arguments(msg);
520
521         dbus_message_iter_get_basic(&iter, &name);
522         dbus_message_iter_next(&iter);
523         dbus_message_iter_recurse(&iter, &value);
524
525         type = dbus_message_iter_get_arg_type(&value);
526
527         DBG("property %s", name);
528
529         if (g_str_equal(name, "Tethering") == TRUE) {
530                 int err;
531                 connman_bool_t tethering;
532
533                 if (type != DBUS_TYPE_BOOLEAN)
534                         return __connman_error_invalid_arguments(msg);
535
536                 dbus_message_iter_get_basic(&value, &tethering);
537
538                 if (technology->tethering == tethering)
539                         return __connman_error_in_progress(msg);
540
541                 err = set_tethering(technology, tethering);
542                 if (err < 0)
543                         return __connman_error_failed(msg, -err);
544
545         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
546                 const char *str;
547
548                 dbus_message_iter_get_basic(&value, &str);
549
550                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
551                         return __connman_error_not_supported(msg);
552
553                 technology->tethering_ident = g_strdup(str);
554         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
555                 const char *str;
556
557                 dbus_message_iter_get_basic(&value, &str);
558
559                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
560                         return __connman_error_not_supported(msg);
561
562                 if (strlen(str) < 8)
563                         return __connman_error_invalid_arguments(msg);
564
565                 technology->tethering_passphrase = g_strdup(str);
566         } else
567                 return __connman_error_invalid_property(msg);
568
569         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
570 }
571
572 static GDBusMethodTable technology_methods[] = {
573         { "GetProperties", "",   "a{sv}", get_properties },
574         { "SetProperty",   "sv", "",      set_property   },
575         { },
576 };
577
578 static GDBusSignalTable technology_signals[] = {
579         { "PropertyChanged", "sv" },
580         { },
581 };
582
583 static struct connman_technology *technology_find(enum connman_service_type type)
584 {
585         GSList *list;
586
587         DBG("type %d", type);
588
589         for (list = technology_list; list; list = list->next) {
590                 struct connman_technology *technology = list->data;
591
592                 if (technology->type == type)
593                         return technology;
594         }
595
596         return NULL;
597 }
598
599 static struct connman_technology *technology_get(enum connman_service_type type)
600 {
601         struct connman_technology *technology;
602         const char *str;
603         GSList *list;
604
605         DBG("type %d", type);
606
607         technology = technology_find(type);
608         if (technology != NULL) {
609                 __sync_fetch_and_add(&technology->refcount, 1);
610                 goto done;
611         }
612
613         str = __connman_service_type2string(type);
614         if (str == NULL)
615                 return NULL;
616
617         technology = g_try_new0(struct connman_technology, 1);
618         if (technology == NULL)
619                 return NULL;
620
621         technology->refcount = 1;
622
623         technology->type = type;
624         technology->path = g_strdup_printf("%s/technology/%s",
625                                                         CONNMAN_PATH, str);
626
627         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
628                                                         NULL, free_rfkill);
629         technology->device_list = NULL;
630
631         technology->pending_reply = NULL;
632         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
633
634         load_state(technology);
635
636         if (g_dbus_register_interface(connection, technology->path,
637                                         CONNMAN_TECHNOLOGY_INTERFACE,
638                                         technology_methods, technology_signals,
639                                         NULL, technology, NULL) == FALSE) {
640                 connman_error("Failed to register %s", technology->path);
641                 g_free(technology);
642                 return NULL;
643         }
644
645         technology_list = g_slist_append(technology_list, technology);
646
647         technologies_changed();
648         technology_added_signal(technology);
649
650         if (technology->driver != NULL)
651                 goto done;
652
653         for (list = driver_list; list; list = list->next) {
654                 struct connman_technology_driver *driver = list->data;
655
656                 DBG("driver %p name %s", driver, driver->name);
657
658                 if (driver->type != technology->type)
659                         continue;
660
661                 if (driver->probe(technology) == 0) {
662                         technology->driver = driver;
663                         break;
664                 }
665         }
666
667 done:
668         DBG("technology %p", technology);
669
670         return technology;
671 }
672
673 static void technology_put(struct connman_technology *technology)
674 {
675         DBG("technology %p", technology);
676
677         if (__sync_fetch_and_sub(&technology->refcount, 1) != 1)
678                 return;
679
680         if (technology->driver) {
681                 technology->driver->remove(technology);
682                 technology->driver = NULL;
683         }
684
685         technology_list = g_slist_remove(technology_list, technology);
686
687         technologies_changed();
688         technology_removed_signal(technology);
689
690         g_dbus_unregister_interface(connection, technology->path,
691                                                 CONNMAN_TECHNOLOGY_INTERFACE);
692
693         g_slist_free(technology->device_list);
694         g_hash_table_destroy(technology->rfkill_list);
695
696         g_free(technology->path);
697         g_free(technology->regdom);
698         g_free(technology);
699 }
700
701 void __connman_technology_add_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("Create interface %s [ %s ]", name,
722                                 __connman_service_type2string(type));
723
724         technology = technology_get(type);
725
726         if (technology == NULL || technology->driver == NULL
727                         || technology->driver->add_interface == NULL)
728                 return;
729
730         technology->driver->add_interface(technology,
731                                         index, name, ident);
732 }
733
734 void __connman_technology_remove_interface(enum connman_service_type type,
735                                 int index, const char *name, const char *ident)
736 {
737         struct connman_technology *technology;
738
739         switch (type) {
740         case CONNMAN_SERVICE_TYPE_UNKNOWN:
741         case CONNMAN_SERVICE_TYPE_SYSTEM:
742                 return;
743         case CONNMAN_SERVICE_TYPE_ETHERNET:
744         case CONNMAN_SERVICE_TYPE_WIFI:
745         case CONNMAN_SERVICE_TYPE_WIMAX:
746         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
747         case CONNMAN_SERVICE_TYPE_CELLULAR:
748         case CONNMAN_SERVICE_TYPE_GPS:
749         case CONNMAN_SERVICE_TYPE_VPN:
750         case CONNMAN_SERVICE_TYPE_GADGET:
751                 break;
752         }
753
754         connman_info("Remove interface %s [ %s ]", name,
755                                 __connman_service_type2string(type));
756
757         technology = technology_find(type);
758
759         if (technology == NULL || technology->driver == NULL)
760                 return;
761
762         if (technology->driver->remove_interface)
763                 technology->driver->remove_interface(technology, index);
764
765         technology_put(technology);
766 }
767
768 int __connman_technology_add_device(struct connman_device *device)
769 {
770         struct connman_technology *technology;
771         enum connman_service_type type;
772
773         DBG("device %p", device);
774
775         type = __connman_device_get_service_type(device);
776         __connman_notifier_register(type);
777
778         technology = technology_get(type);
779         if (technology == NULL)
780                 return -ENXIO;
781
782         if (technology->enable_persistent && !global_offlinemode)
783                 __connman_device_enable(device);
784         /* if technology persistent state is offline */
785         if (!technology->enable_persistent)
786                 __connman_device_disable(device);
787
788         technology->device_list = g_slist_append(technology->device_list,
789                                                                 device);
790
791         return 0;
792 }
793
794 int __connman_technology_remove_device(struct connman_device *device)
795 {
796         struct connman_technology *technology;
797         enum connman_service_type type;
798
799         DBG("device %p", device);
800
801         type = __connman_device_get_service_type(device);
802         __connman_notifier_unregister(type);
803
804         technology = technology_find(type);
805         if (technology == NULL)
806                 return -ENXIO;
807
808         technology->device_list = g_slist_remove(technology->device_list,
809                                                                 device);
810         if (technology->device_list == NULL) {
811                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
812                 state_changed(technology);
813         }
814
815         return 0;
816 }
817
818 static gboolean technology_pending_reply(gpointer user_data)
819 {
820         struct connman_technology *technology = user_data;
821         DBusMessage *reply;
822
823         /* Power request timedout, send ETIMEDOUT. */
824         if (technology->pending_reply != NULL) {
825                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
826                 if (reply != NULL)
827                         g_dbus_send_message(connection, reply);
828
829                 dbus_message_unref(technology->pending_reply);
830                 technology->pending_reply = NULL;
831                 technology->pending_timeout = 0;
832         }
833
834         return FALSE;
835 }
836
837 int __connman_technology_enabled(enum connman_service_type type)
838 {
839         struct connman_technology *technology;
840
841         technology = technology_find(type);
842         if (technology == NULL)
843                 return -ENXIO;
844
845         if (__sync_fetch_and_add(&technology->enabled, 1) == 0) {
846                 __connman_notifier_enable(type);
847                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
848                 state_changed(technology);
849         }
850
851         if (technology->pending_reply != NULL) {
852                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
853                 dbus_message_unref(technology->pending_reply);
854                 g_source_remove(technology->pending_timeout);
855                 technology->pending_reply = NULL;
856                 technology->pending_timeout = 0;
857         }
858
859         return 0;
860 }
861
862 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
863 {
864         struct connman_technology *technology;
865         GSList *list;
866         int err = 0;
867         int ret = -ENODEV;
868         DBusMessage *reply;
869
870         DBG("type %d enable", type);
871
872         technology = technology_find(type);
873         if (technology == NULL) {
874                 err = -ENXIO;
875                 goto done;
876         }
877
878         if (technology->pending_reply != NULL) {
879                 err = -EBUSY;
880                 goto done;
881         }
882
883         if (msg != NULL) {
884                 /*
885                  * This is a bit of a trick. When msg is not NULL it means
886                  * thats technology_enable was invoked from the manager API. Hence we save
887                  * the state here.
888                  */
889                 technology->enable_persistent = TRUE;
890                 save_state(technology);
891         }
892
893         __connman_rfkill_block(technology->type, FALSE);
894
895         /*
896          * An empty device list means that devices in the technology
897          * were rfkill blocked. The unblock above will enable the devs.
898          */
899         if (technology->device_list == NULL)
900                 return 0;
901
902         for (list = technology->device_list; list; list = list->next) {
903                 struct connman_device *device = list->data;
904
905                 err = __connman_device_enable(device);
906                 /*
907                  * err = 0 : Device was enabled right away.
908                  * If atleast one device gets enabled, we consider
909                  * the technology to be enabled.
910                  */
911                 if (err == 0)
912                         ret = 0;
913         }
914
915 done:
916         if (ret == 0) {
917                 if (msg != NULL)
918                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
919                 return ret;
920         }
921
922         if (msg != NULL) {
923                 if (err == -EINPROGRESS) {
924                         technology->pending_reply = dbus_message_ref(msg);
925                         technology->pending_timeout = g_timeout_add_seconds(10,
926                                         technology_pending_reply, technology);
927                 } else {
928                         reply = __connman_error_failed(msg, -err);
929                         if (reply != NULL)
930                                 g_dbus_send_message(connection, reply);
931                 }
932         }
933
934         return err;
935 }
936
937 int __connman_technology_disabled(enum connman_service_type type)
938 {
939         struct connman_technology *technology;
940
941         technology = technology_find(type);
942         if (technology == NULL)
943                 return -ENXIO;
944
945         if (technology->pending_reply != NULL) {
946                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
947                 dbus_message_unref(technology->pending_reply);
948                 g_source_remove(technology->pending_timeout);
949                 technology->pending_reply = NULL;
950                 technology->pending_timeout = 0;
951         }
952
953         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
954                 return 0;
955
956         __connman_notifier_disable(type);
957         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
958         state_changed(technology);
959
960         return 0;
961 }
962
963 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
964 {
965         struct connman_technology *technology;
966         GSList *list;
967         int err = 0;
968         int ret = -ENODEV;
969         DBusMessage *reply;
970
971         DBG("type %d disable", type);
972
973         technology = technology_find(type);
974         if (technology == NULL) {
975                 err = -ENXIO;
976                 goto done;
977         }
978
979         if (technology->pending_reply != NULL) {
980                 err = -EBUSY;
981                 goto done;
982         }
983
984         if (technology->tethering == TRUE)
985                 set_tethering(technology, FALSE);
986
987         if (msg != NULL) {
988                 technology->enable_persistent = FALSE;
989                 save_state(technology);
990         }
991
992         __connman_rfkill_block(technology->type, TRUE);
993
994         for (list = technology->device_list; list; list = list->next) {
995                 struct connman_device *device = list->data;
996
997                 err = __connman_device_disable(device);
998                 if (err == 0)
999                         ret = 0;
1000         }
1001
1002 done:
1003         if (ret == 0) {
1004                 if (msg != NULL)
1005                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
1006                 return ret;
1007         }
1008
1009         if (msg != NULL) {
1010                 if (err == -EINPROGRESS) {
1011                         technology->pending_reply = dbus_message_ref(msg);
1012                         technology->pending_timeout = g_timeout_add_seconds(10,
1013                                         technology_pending_reply, technology);
1014                 } else {
1015                         reply = __connman_error_failed(msg, -err);
1016                         if (reply != NULL)
1017                                 g_dbus_send_message(connection, reply);
1018                 }
1019         }
1020
1021         return err;
1022 }
1023
1024 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1025 {
1026         GSList *list;
1027         int err = -EINVAL;
1028
1029         if (global_offlinemode == offlinemode)
1030                 return 0;
1031
1032         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1033
1034         /*
1035          * This is a bit tricky. When you set offlinemode, there is no
1036          * way to differentiate between attempting offline mode and
1037          * resuming offlinemode from last saved profile. We need that
1038          * information in rfkill_update, otherwise it falls back on the
1039          * technology's persistent state. Hence we set the offline mode here
1040          * but save it & call the notifier only if its successful.
1041          */
1042
1043         global_offlinemode = offlinemode;
1044
1045         /* Traverse technology list, enable/disable each technology. */
1046         for (list = technology_list; list; list = list->next) {
1047                 struct connman_technology *technology = list->data;
1048
1049                 if (offlinemode)
1050                         err = __connman_technology_disable(technology->type, NULL);
1051
1052                 if (!offlinemode && technology->enable_persistent)
1053                         err = __connman_technology_enable(technology->type, NULL);
1054         }
1055
1056         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1057                 connman_technology_save_offlinemode();
1058                 __connman_notifier_offlinemode(offlinemode);
1059         } else
1060                 global_offlinemode = connman_technology_load_offlinemode();
1061
1062         return err;
1063 }
1064
1065 int __connman_technology_add_rfkill(unsigned int index,
1066                                         enum connman_service_type type,
1067                                                 connman_bool_t softblock,
1068                                                 connman_bool_t hardblock)
1069 {
1070         struct connman_technology *technology;
1071         struct connman_rfkill *rfkill;
1072
1073         DBG("index %u type %d soft %u hard %u", index, type,
1074                                                         softblock, hardblock);
1075
1076         technology = technology_get(type);
1077         if (technology == NULL)
1078                 return -ENXIO;
1079
1080         rfkill = g_try_new0(struct connman_rfkill, 1);
1081         if (rfkill == NULL)
1082                 return -ENOMEM;
1083
1084         __connman_notifier_register(type);
1085
1086         rfkill->index = index;
1087         rfkill->type = type;
1088         rfkill->softblock = softblock;
1089         rfkill->hardblock = hardblock;
1090
1091         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
1092
1093         if (hardblock) {
1094                 DBG("%s is switched off.", get_name(type));
1095                 return 0;
1096         }
1097
1098         /*
1099          * If Offline mode is on, we softblock the device if it isnt already.
1100          * If Offline mode is off, we rely on the persistent state of tech.
1101          */
1102         if (global_offlinemode) {
1103                 if (!softblock)
1104                         return __connman_rfkill_block(type, TRUE);
1105         } else {
1106                 if (technology->enable_persistent && softblock)
1107                         return __connman_rfkill_block(type, FALSE);
1108                 /* if technology persistent state is offline */
1109                 if (!technology->enable_persistent && !softblock)
1110                         return __connman_rfkill_block(type, TRUE);
1111         }
1112
1113         return 0;
1114 }
1115
1116 int __connman_technology_update_rfkill(unsigned int index,
1117                                         enum connman_service_type type,
1118                                                 connman_bool_t softblock,
1119                                                 connman_bool_t hardblock)
1120 {
1121         struct connman_technology *technology;
1122         struct connman_rfkill *rfkill;
1123
1124         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1125
1126         technology = technology_find(type);
1127         if (technology == NULL)
1128                 return -ENXIO;
1129
1130         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1131         if (rfkill == NULL)
1132                 return -ENXIO;
1133
1134         if (rfkill->softblock == softblock &&
1135                 rfkill->hardblock == hardblock)
1136                 return 0;
1137
1138         rfkill->softblock = softblock;
1139         rfkill->hardblock = hardblock;
1140
1141         if (hardblock) {
1142                 DBG("%s is switched off.", get_name(type));
1143                 return 0;
1144         }
1145
1146         if (!global_offlinemode) {
1147                 if (technology->enable_persistent && softblock)
1148                         return __connman_rfkill_block(type, FALSE);
1149                 if (!technology->enable_persistent && !softblock)
1150                         return __connman_rfkill_block(type, TRUE);
1151         }
1152
1153         return 0;
1154 }
1155
1156 int __connman_technology_remove_rfkill(unsigned int index,
1157                                         enum connman_service_type type)
1158 {
1159         struct connman_technology *technology;
1160         struct connman_rfkill *rfkill;
1161
1162         DBG("index %u", index);
1163
1164         technology = technology_find(type);
1165         if (technology == NULL)
1166                 return -ENXIO;
1167
1168         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1169         if (rfkill == NULL)
1170                 return -ENXIO;
1171
1172         g_hash_table_remove(technology->rfkill_list, &index);
1173
1174         technology_put(technology);
1175
1176         return 0;
1177 }
1178
1179 int __connman_technology_init(void)
1180 {
1181         DBG("");
1182
1183         connection = connman_dbus_get_connection();
1184
1185         global_offlinemode = connman_technology_load_offlinemode();
1186
1187         return 0;
1188 }
1189
1190 void __connman_technology_cleanup(void)
1191 {
1192         DBG("");
1193
1194         dbus_connection_unref(connection);
1195 }