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