service: Refactor the service unref function
[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         struct connman_technology_driver *driver = NULL;
589         const char *str;
590         GSList *list;
591         int err;
592
593         DBG("type %d", type);
594
595         str = __connman_service_type2string(type);
596         if (str == NULL)
597                 return NULL;
598
599         technology = technology_find(type);
600         if (technology != NULL)
601                 return technology;
602
603         /* First check if we have a driver for this technology type */
604         for (list = driver_list; list; list = list->next) {
605                 driver = list->data;
606
607                 if (driver->type == type)
608                         break;
609                 else
610                         driver = NULL;
611         }
612
613         if (driver == NULL) {
614                 DBG("No matching driver found for %s.",
615                                 __connman_service_type2string(type));
616                 return NULL;
617         }
618
619         technology = g_try_new0(struct connman_technology, 1);
620         if (technology == NULL)
621                 return NULL;
622
623         technology->refcount = 1;
624
625         technology->type = type;
626         technology->path = g_strdup_printf("%s/technology/%s",
627                                                         CONNMAN_PATH, str);
628
629         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
630                                                         NULL, free_rfkill);
631         technology->device_list = NULL;
632
633         technology->pending_reply = NULL;
634         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
635
636         load_state(technology);
637
638         if (g_dbus_register_interface(connection, technology->path,
639                                         CONNMAN_TECHNOLOGY_INTERFACE,
640                                         technology_methods, technology_signals,
641                                         NULL, technology, NULL) == FALSE) {
642                 connman_error("Failed to register %s", technology->path);
643                 g_free(technology);
644                 return NULL;
645         }
646
647         technology_list = g_slist_append(technology_list, technology);
648
649         technology_added_signal(technology);
650
651         technology->driver = driver;
652         err = driver->probe(technology);
653         if (err != 0)
654                 DBG("Driver probe failed for technology %p", technology);
655
656         DBG("technology %p", technology);
657
658         return technology;
659 }
660
661 static void technology_put(struct connman_technology *technology)
662 {
663         DBG("technology %p", technology);
664
665         if (__sync_fetch_and_sub(&technology->refcount, 1) != 1)
666                 return;
667
668         if (technology->driver) {
669                 technology->driver->remove(technology);
670                 technology->driver = NULL;
671         }
672
673         technology_list = g_slist_remove(technology_list, technology);
674
675         technology_removed_signal(technology);
676
677         g_dbus_unregister_interface(connection, technology->path,
678                                                 CONNMAN_TECHNOLOGY_INTERFACE);
679
680         g_slist_free(technology->device_list);
681         g_hash_table_destroy(technology->rfkill_list);
682
683         g_free(technology->path);
684         g_free(technology->regdom);
685         g_free(technology);
686 }
687
688 void __connman_technology_add_interface(enum connman_service_type type,
689                                 int index, const char *name, const char *ident)
690 {
691         struct connman_technology *technology;
692
693         switch (type) {
694         case CONNMAN_SERVICE_TYPE_UNKNOWN:
695         case CONNMAN_SERVICE_TYPE_SYSTEM:
696                 return;
697         case CONNMAN_SERVICE_TYPE_ETHERNET:
698         case CONNMAN_SERVICE_TYPE_WIFI:
699         case CONNMAN_SERVICE_TYPE_WIMAX:
700         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
701         case CONNMAN_SERVICE_TYPE_CELLULAR:
702         case CONNMAN_SERVICE_TYPE_GPS:
703         case CONNMAN_SERVICE_TYPE_VPN:
704         case CONNMAN_SERVICE_TYPE_GADGET:
705                 break;
706         }
707
708         connman_info("Adding interface %s [ %s ]", name,
709                                 __connman_service_type2string(type));
710
711         technology = technology_find(type);
712
713         if (technology == NULL || technology->driver == NULL
714                         || technology->driver->add_interface == NULL)
715                 return;
716
717         technology->driver->add_interface(technology,
718                                         index, name, ident);
719 }
720
721 void __connman_technology_remove_interface(enum connman_service_type type,
722                                 int index, const char *name, const char *ident)
723 {
724         struct connman_technology *technology;
725
726         switch (type) {
727         case CONNMAN_SERVICE_TYPE_UNKNOWN:
728         case CONNMAN_SERVICE_TYPE_SYSTEM:
729                 return;
730         case CONNMAN_SERVICE_TYPE_ETHERNET:
731         case CONNMAN_SERVICE_TYPE_WIFI:
732         case CONNMAN_SERVICE_TYPE_WIMAX:
733         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
734         case CONNMAN_SERVICE_TYPE_CELLULAR:
735         case CONNMAN_SERVICE_TYPE_GPS:
736         case CONNMAN_SERVICE_TYPE_VPN:
737         case CONNMAN_SERVICE_TYPE_GADGET:
738                 break;
739         }
740
741         connman_info("Remove interface %s [ %s ]", name,
742                                 __connman_service_type2string(type));
743
744         technology = technology_find(type);
745
746         if (technology == NULL || technology->driver == NULL)
747                 return;
748
749         if (technology->driver->remove_interface)
750                 technology->driver->remove_interface(technology, index);
751 }
752
753 int __connman_technology_add_device(struct connman_device *device)
754 {
755         struct connman_technology *technology;
756         enum connman_service_type type;
757
758         DBG("device %p", device);
759
760         type = __connman_device_get_service_type(device);
761         __connman_notifier_register(type);
762
763         technology = technology_get(type);
764         if (technology == NULL)
765                 return -ENXIO;
766
767         if (technology->enable_persistent && !global_offlinemode)
768                 __connman_device_enable(device);
769         /* if technology persistent state is offline */
770         if (!technology->enable_persistent)
771                 __connman_device_disable(device);
772
773         technology->device_list = g_slist_append(technology->device_list,
774                                                                 device);
775
776         return 0;
777 }
778
779 int __connman_technology_remove_device(struct connman_device *device)
780 {
781         struct connman_technology *technology;
782         enum connman_service_type type;
783
784         DBG("device %p", device);
785
786         type = __connman_device_get_service_type(device);
787         __connman_notifier_unregister(type);
788
789         technology = technology_find(type);
790         if (technology == NULL)
791                 return -ENXIO;
792
793         technology->device_list = g_slist_remove(technology->device_list,
794                                                                 device);
795         if (technology->device_list == NULL) {
796                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
797                 state_changed(technology);
798         }
799
800         return 0;
801 }
802
803 static gboolean technology_pending_reply(gpointer user_data)
804 {
805         struct connman_technology *technology = user_data;
806         DBusMessage *reply;
807
808         /* Power request timedout, send ETIMEDOUT. */
809         if (technology->pending_reply != NULL) {
810                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
811                 if (reply != NULL)
812                         g_dbus_send_message(connection, reply);
813
814                 dbus_message_unref(technology->pending_reply);
815                 technology->pending_reply = NULL;
816                 technology->pending_timeout = 0;
817         }
818
819         return FALSE;
820 }
821
822 int __connman_technology_enabled(enum connman_service_type type)
823 {
824         struct connman_technology *technology;
825
826         technology = technology_find(type);
827         if (technology == NULL)
828                 return -ENXIO;
829
830         if (__sync_fetch_and_add(&technology->enabled, 1) == 0) {
831                 __connman_notifier_enable(type);
832                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
833                 state_changed(technology);
834         }
835
836         if (technology->pending_reply != NULL) {
837                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
838                 dbus_message_unref(technology->pending_reply);
839                 g_source_remove(technology->pending_timeout);
840                 technology->pending_reply = NULL;
841                 technology->pending_timeout = 0;
842         }
843
844         return 0;
845 }
846
847 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
848 {
849         struct connman_technology *technology;
850         GSList *list;
851         int err = 0;
852         int ret = -ENODEV;
853         DBusMessage *reply;
854
855         DBG("type %d enable", type);
856
857         technology = technology_find(type);
858         if (technology == NULL) {
859                 err = -ENXIO;
860                 goto done;
861         }
862
863         if (technology->pending_reply != NULL) {
864                 err = -EBUSY;
865                 goto done;
866         }
867
868         if (msg != NULL) {
869                 /*
870                  * This is a bit of a trick. When msg is not NULL it means
871                  * thats technology_enable was invoked from the manager API. Hence we save
872                  * the state here.
873                  */
874                 technology->enable_persistent = TRUE;
875                 save_state(technology);
876         }
877
878         __connman_rfkill_block(technology->type, FALSE);
879
880         /*
881          * An empty device list means that devices in the technology
882          * were rfkill blocked. The unblock above will enable the devs.
883          */
884         if (technology->device_list == NULL) {
885                 ret = 0;
886                 goto done;
887         }
888
889         for (list = technology->device_list; list; list = list->next) {
890                 struct connman_device *device = list->data;
891
892                 err = __connman_device_enable(device);
893                 /*
894                  * err = 0 : Device was enabled right away.
895                  * If atleast one device gets enabled, we consider
896                  * the technology to be enabled.
897                  */
898                 if (err == 0)
899                         ret = 0;
900         }
901
902 done:
903         if (ret == 0) {
904                 if (msg != NULL)
905                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
906                 return ret;
907         }
908
909         if (msg != NULL) {
910                 if (err == -EINPROGRESS) {
911                         technology->pending_reply = dbus_message_ref(msg);
912                         technology->pending_timeout = g_timeout_add_seconds(10,
913                                         technology_pending_reply, technology);
914                 } else {
915                         reply = __connman_error_failed(msg, -err);
916                         if (reply != NULL)
917                                 g_dbus_send_message(connection, reply);
918                 }
919         }
920
921         return err;
922 }
923
924 int __connman_technology_disabled(enum connman_service_type type)
925 {
926         struct connman_technology *technology;
927
928         technology = technology_find(type);
929         if (technology == NULL)
930                 return -ENXIO;
931
932         if (technology->pending_reply != NULL) {
933                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
934                 dbus_message_unref(technology->pending_reply);
935                 g_source_remove(technology->pending_timeout);
936                 technology->pending_reply = NULL;
937                 technology->pending_timeout = 0;
938         }
939
940         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
941                 return 0;
942
943         __connman_notifier_disable(type);
944         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
945         state_changed(technology);
946
947         return 0;
948 }
949
950 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
951 {
952         struct connman_technology *technology;
953         GSList *list;
954         int err = 0;
955         int ret = -ENODEV;
956         DBusMessage *reply;
957
958         DBG("type %d disable", type);
959
960         technology = technology_find(type);
961         if (technology == NULL) {
962                 err = -ENXIO;
963                 goto done;
964         }
965
966         if (technology->pending_reply != NULL) {
967                 err = -EBUSY;
968                 goto done;
969         }
970
971         if (technology->tethering == TRUE)
972                 set_tethering(technology, FALSE);
973
974         if (msg != NULL) {
975                 technology->enable_persistent = FALSE;
976                 save_state(technology);
977         }
978
979         __connman_rfkill_block(technology->type, TRUE);
980
981         for (list = technology->device_list; list; list = list->next) {
982                 struct connman_device *device = list->data;
983
984                 err = __connman_device_disable(device);
985                 if (err == 0)
986                         ret = 0;
987         }
988
989 done:
990         if (ret == 0) {
991                 if (msg != NULL)
992                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
993                 return ret;
994         }
995
996         if (msg != NULL) {
997                 if (err == -EINPROGRESS) {
998                         technology->pending_reply = dbus_message_ref(msg);
999                         technology->pending_timeout = g_timeout_add_seconds(10,
1000                                         technology_pending_reply, technology);
1001                 } else {
1002                         reply = __connman_error_failed(msg, -err);
1003                         if (reply != NULL)
1004                                 g_dbus_send_message(connection, reply);
1005                 }
1006         }
1007
1008         return err;
1009 }
1010
1011 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1012 {
1013         GSList *list;
1014         int err = -EINVAL;
1015
1016         if (global_offlinemode == offlinemode)
1017                 return 0;
1018
1019         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1020
1021         /*
1022          * This is a bit tricky. When you set offlinemode, there is no
1023          * way to differentiate between attempting offline mode and
1024          * resuming offlinemode from last saved profile. We need that
1025          * information in rfkill_update, otherwise it falls back on the
1026          * technology's persistent state. Hence we set the offline mode here
1027          * but save it & call the notifier only if its successful.
1028          */
1029
1030         global_offlinemode = offlinemode;
1031
1032         /* Traverse technology list, enable/disable each technology. */
1033         for (list = technology_list; list; list = list->next) {
1034                 struct connman_technology *technology = list->data;
1035
1036                 if (offlinemode)
1037                         err = __connman_technology_disable(technology->type, NULL);
1038
1039                 if (!offlinemode && technology->enable_persistent)
1040                         err = __connman_technology_enable(technology->type, NULL);
1041         }
1042
1043         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1044                 connman_technology_save_offlinemode();
1045                 __connman_notifier_offlinemode(offlinemode);
1046         } else
1047                 global_offlinemode = connman_technology_load_offlinemode();
1048
1049         return err;
1050 }
1051
1052 int __connman_technology_add_rfkill(unsigned int index,
1053                                         enum connman_service_type type,
1054                                                 connman_bool_t softblock,
1055                                                 connman_bool_t hardblock)
1056 {
1057         struct connman_technology *technology;
1058         struct connman_rfkill *rfkill;
1059
1060         DBG("index %u type %d soft %u hard %u", index, type,
1061                                                         softblock, hardblock);
1062
1063         technology = technology_get(type);
1064         if (technology == NULL)
1065                 return -ENXIO;
1066
1067         rfkill = g_try_new0(struct connman_rfkill, 1);
1068         if (rfkill == NULL)
1069                 return -ENOMEM;
1070
1071         __connman_notifier_register(type);
1072
1073         rfkill->index = index;
1074         rfkill->type = type;
1075         rfkill->softblock = softblock;
1076         rfkill->hardblock = hardblock;
1077
1078         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
1079
1080         if (hardblock) {
1081                 DBG("%s is switched off.", get_name(type));
1082                 return 0;
1083         }
1084
1085         /*
1086          * If Offline mode is on, we softblock the device if it isnt already.
1087          * If Offline mode is off, we rely on the persistent state of tech.
1088          */
1089         if (global_offlinemode) {
1090                 if (!softblock)
1091                         return __connman_rfkill_block(type, TRUE);
1092         } else {
1093                 if (technology->enable_persistent && softblock)
1094                         return __connman_rfkill_block(type, FALSE);
1095                 /* if technology persistent state is offline */
1096                 if (!technology->enable_persistent && !softblock)
1097                         return __connman_rfkill_block(type, TRUE);
1098         }
1099
1100         return 0;
1101 }
1102
1103 int __connman_technology_update_rfkill(unsigned int index,
1104                                         enum connman_service_type type,
1105                                                 connman_bool_t softblock,
1106                                                 connman_bool_t hardblock)
1107 {
1108         struct connman_technology *technology;
1109         struct connman_rfkill *rfkill;
1110
1111         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1112
1113         technology = technology_find(type);
1114         if (technology == NULL)
1115                 return -ENXIO;
1116
1117         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1118         if (rfkill == NULL)
1119                 return -ENXIO;
1120
1121         if (rfkill->softblock == softblock &&
1122                 rfkill->hardblock == hardblock)
1123                 return 0;
1124
1125         rfkill->softblock = softblock;
1126         rfkill->hardblock = hardblock;
1127
1128         if (hardblock) {
1129                 DBG("%s is switched off.", get_name(type));
1130                 return 0;
1131         }
1132
1133         if (!global_offlinemode) {
1134                 if (technology->enable_persistent && softblock)
1135                         return __connman_rfkill_block(type, FALSE);
1136                 if (!technology->enable_persistent && !softblock)
1137                         return __connman_rfkill_block(type, TRUE);
1138         }
1139
1140         return 0;
1141 }
1142
1143 int __connman_technology_remove_rfkill(unsigned int index,
1144                                         enum connman_service_type type)
1145 {
1146         struct connman_technology *technology;
1147         struct connman_rfkill *rfkill;
1148
1149         DBG("index %u", index);
1150
1151         technology = technology_find(type);
1152         if (technology == NULL)
1153                 return -ENXIO;
1154
1155         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1156         if (rfkill == NULL)
1157                 return -ENXIO;
1158
1159         g_hash_table_remove(technology->rfkill_list, &index);
1160
1161         technology_put(technology);
1162
1163         return 0;
1164 }
1165
1166 int __connman_technology_init(void)
1167 {
1168         DBG("");
1169
1170         connection = connman_dbus_get_connection();
1171
1172         global_offlinemode = connman_technology_load_offlinemode();
1173
1174         return 0;
1175 }
1176
1177 void __connman_technology_cleanup(void)
1178 {
1179         DBG("");
1180
1181         dbus_connection_unref(connection);
1182 }