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