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