001208d63f83f67ffdb329d9f5797cd48c3efe68
[platform/upstream/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 static GHashTable *rfkill_list;
43
44 static connman_bool_t global_offlinemode;
45
46 struct connman_rfkill {
47         unsigned int index;
48         enum connman_service_type type;
49         connman_bool_t softblock;
50         connman_bool_t hardblock;
51 };
52
53 struct connman_technology {
54         int refcount;
55         enum connman_service_type type;
56         char *path;
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         GSList *scan_pending;
75 };
76
77 static GSList *driver_list = NULL;
78
79 static gint compare_priority(gconstpointer a, gconstpointer b)
80 {
81         const struct connman_technology_driver *driver1 = a;
82         const struct connman_technology_driver *driver2 = b;
83
84         return driver2->priority - driver1->priority;
85 }
86
87 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
88 {
89         struct connman_rfkill *rfkill = value;
90         enum connman_service_type type = GPOINTER_TO_INT(user_data);
91
92         /* Calling _technology_rfkill_add will update the tech. */
93         if (rfkill->type == type)
94                 __connman_technology_add_rfkill(rfkill->index, type,
95                                 rfkill->softblock, rfkill->hardblock);
96 }
97
98 /**
99  * connman_technology_driver_register:
100  * @driver: Technology driver definition
101  *
102  * Register a new technology driver
103  *
104  * Returns: %0 on success
105  */
106 int connman_technology_driver_register(struct connman_technology_driver *driver)
107 {
108         GSList *list;
109         struct connman_device *device;
110         enum connman_service_type type;
111
112         DBG("Registering %s driver", driver->name);
113
114         driver_list = g_slist_insert_sorted(driver_list, driver,
115                                                         compare_priority);
116
117         if (techless_device_list == NULL)
118                 goto check_rfkill;
119
120         /*
121          * Check for technology less devices if this driver
122          * can service any of them.
123         */
124         for (list = techless_device_list; list; list = list->next) {
125                 device = list->data;
126
127                 type = __connman_device_get_service_type(device);
128                 if (type != driver->type)
129                         continue;
130
131                 techless_device_list = g_slist_remove(techless_device_list,
132                                                                 device);
133
134                 __connman_technology_add_device(device);
135         }
136
137 check_rfkill:
138         /* Check for orphaned rfkill switches. */
139         g_hash_table_foreach(rfkill_list, rfkill_check,
140                                         GINT_TO_POINTER(driver->type));
141
142         return 0;
143 }
144
145 /**
146  * connman_technology_driver_unregister:
147  * @driver: Technology driver definition
148  *
149  * Remove a previously registered technology driver
150  */
151 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
152 {
153         GSList *list;
154         struct connman_technology *technology;
155
156         DBG("Unregistering driver %p name %s", driver, driver->name);
157
158         for (list = technology_list; list; list = list->next) {
159                 technology = list->data;
160
161                 if (technology->driver == NULL)
162                         continue;
163
164                 if (technology->type == driver->type) {
165                         technology->driver->remove(technology);
166                         technology->driver = NULL;
167                 }
168         }
169
170         driver_list = g_slist_remove(driver_list, driver);
171 }
172
173 static void tethering_changed(struct connman_technology *technology)
174 {
175         connman_bool_t tethering = technology->tethering;
176
177         connman_dbus_property_changed_basic(technology->path,
178                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
179                                                 DBUS_TYPE_BOOLEAN, &tethering);
180 }
181
182 void connman_technology_tethering_notify(struct connman_technology *technology,
183                                                         connman_bool_t enabled)
184 {
185         GSList *list;
186
187         DBG("technology %p enabled %u", technology, enabled);
188
189         if (technology->tethering == enabled)
190                 return;
191
192         technology->tethering = enabled;
193
194         tethering_changed(technology);
195
196         if (enabled == TRUE)
197                 __connman_tethering_set_enabled();
198         else {
199                 for (list = technology_list; list; list = list->next) {
200                         struct connman_technology *other_tech = list->data;
201                         if (other_tech->tethering == TRUE)
202                                 break;
203                 }
204                 if (list == NULL)
205                         __connman_tethering_set_disabled();
206         }
207 }
208
209 static int set_tethering(struct connman_technology *technology,
210                                 connman_bool_t enabled)
211 {
212         const char *ident, *passphrase, *bridge;
213
214         ident = technology->tethering_ident;
215         passphrase = technology->tethering_passphrase;
216
217         if (technology->driver == NULL ||
218                         technology->driver->set_tethering == NULL)
219                 return -EOPNOTSUPP;
220
221         bridge = __connman_tethering_get_bridge();
222         if (bridge == NULL)
223                 return -EOPNOTSUPP;
224
225         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
226             (ident == NULL || passphrase == NULL))
227                 return -EINVAL;
228
229         return technology->driver->set_tethering(technology, ident, passphrase,
230                                                         bridge, enabled);
231 }
232
233 void connman_technology_regdom_notify(struct connman_technology *technology,
234                                                         const char *alpha2)
235 {
236         DBG("");
237
238         if (alpha2 == NULL)
239                 connman_error("Failed to set regulatory domain");
240         else
241                 DBG("Regulatory domain set to %s", alpha2);
242
243         g_free(technology->regdom);
244         technology->regdom = g_strdup(alpha2);
245 }
246
247 static int set_regdom_by_device(struct connman_technology *technology,
248                                                         const char *alpha2)
249 {
250         GSList *list;
251
252         for (list = technology->device_list; list; list = list->next) {
253                 struct connman_device *device = list->data;
254
255                 if (connman_device_set_regdom(device, alpha2) != 0)
256                         return -ENOTSUP;
257         }
258
259         return 0;
260 }
261
262 int connman_technology_set_regdom(const char *alpha2)
263 {
264         GSList *list;
265
266         for (list = technology_list; list; list = list->next) {
267                 struct connman_technology *technology = list->data;
268
269                 if (set_regdom_by_device(technology, alpha2) != 0) {
270                         if (technology->driver == NULL)
271                                 continue;
272
273                         if (technology->driver->set_regdom != NULL)
274                                 technology->driver->set_regdom(technology,
275                                                                 alpha2);
276                 }
277         }
278
279         return 0;
280 }
281
282 static void free_rfkill(gpointer data)
283 {
284         struct connman_rfkill *rfkill = data;
285
286         g_free(rfkill);
287 }
288
289 static const char *get_name(enum connman_service_type type)
290 {
291         switch (type) {
292         case CONNMAN_SERVICE_TYPE_UNKNOWN:
293         case CONNMAN_SERVICE_TYPE_SYSTEM:
294         case CONNMAN_SERVICE_TYPE_GPS:
295         case CONNMAN_SERVICE_TYPE_VPN:
296         case CONNMAN_SERVICE_TYPE_GADGET:
297                 break;
298         case CONNMAN_SERVICE_TYPE_ETHERNET:
299                 return "Wired";
300         case CONNMAN_SERVICE_TYPE_WIFI:
301                 return "WiFi";
302         case CONNMAN_SERVICE_TYPE_WIMAX:
303                 return "WiMAX";
304         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
305                 return "Bluetooth";
306         case CONNMAN_SERVICE_TYPE_CELLULAR:
307                 return "Cellular";
308         }
309
310         return NULL;
311 }
312
313 static void technology_save(struct connman_technology *technology)
314 {
315         GKeyFile *keyfile;
316         gchar *identifier;
317
318         DBG("technology %p", technology);
319
320         keyfile = __connman_storage_load_global();
321         if (keyfile == NULL)
322                 keyfile = g_key_file_new();
323
324         identifier = g_strdup_printf("%s", get_name(technology->type));
325         if (identifier == NULL)
326                 goto done;
327
328         g_key_file_set_boolean(keyfile, identifier, "Enable",
329                                 technology->enable_persistent);
330
331         if (technology->tethering_ident != NULL)
332                 g_key_file_set_string(keyfile, identifier,
333                                         "Tethering.Identifier",
334                                         technology->tethering_ident);
335
336         if (technology->tethering_passphrase != NULL)
337                 g_key_file_set_string(keyfile, identifier,
338                                         "Tethering.Passphrase",
339                                         technology->tethering_passphrase);
340
341 done:
342         g_free(identifier);
343
344         __connman_storage_save_global(keyfile);
345
346         g_key_file_free(keyfile);
347
348         return;
349 }
350
351 static void technology_load(struct connman_technology *technology)
352 {
353         GKeyFile *keyfile;
354         gchar *identifier;
355         GError *error = NULL;
356         connman_bool_t enable;
357
358         DBG("technology %p", technology);
359
360         keyfile = __connman_storage_load_global();
361         /* Fallback on disabling technology if file not found. */
362         if (keyfile == NULL) {
363                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
364                         /* We enable ethernet by default */
365                         technology->enable_persistent = TRUE;
366                 else
367                         technology->enable_persistent = FALSE;
368                 return;
369         }
370
371         identifier = g_strdup_printf("%s", get_name(technology->type));
372         if (identifier == NULL)
373                 goto done;
374
375         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
376         if (error == NULL)
377                 technology->enable_persistent = enable;
378         else {
379                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
380                         technology->enable_persistent = TRUE;
381                 else
382                         technology->enable_persistent = FALSE;
383
384                 technology_save(technology);
385                 g_clear_error(&error);
386         }
387
388         technology->tethering_ident = g_key_file_get_string(keyfile,
389                                 identifier, "Tethering.Identifier", NULL);
390
391         technology->tethering_passphrase = g_key_file_get_string(keyfile,
392                                 identifier, "Tethering.Passphrase", NULL);
393 done:
394         g_free(identifier);
395
396         g_key_file_free(keyfile);
397
398         return;
399 }
400
401 connman_bool_t __connman_technology_get_offlinemode(void)
402 {
403         return global_offlinemode;
404 }
405
406 static void connman_technology_save_offlinemode()
407 {
408         GKeyFile *keyfile;
409
410         keyfile = __connman_storage_load_global();
411         if (keyfile == NULL)
412                 keyfile = g_key_file_new();
413
414         g_key_file_set_boolean(keyfile, "global",
415                                         "OfflineMode", global_offlinemode);
416
417         __connman_storage_save_global(keyfile);
418
419         g_key_file_free(keyfile);
420
421         return;
422 }
423
424 static connman_bool_t connman_technology_load_offlinemode()
425 {
426         GKeyFile *keyfile;
427         GError *error = NULL;
428         connman_bool_t offlinemode;
429
430         /* If there is a error, we enable offlinemode */
431         keyfile = __connman_storage_load_global();
432         if (keyfile == NULL)
433                 return FALSE;
434
435         offlinemode = g_key_file_get_boolean(keyfile, "global",
436                                                 "OfflineMode", &error);
437         if (error != NULL) {
438                 offlinemode = FALSE;
439                 g_clear_error(&error);
440         }
441
442         g_key_file_free(keyfile);
443
444         return offlinemode;
445 }
446
447 static void append_properties(DBusMessageIter *iter,
448                 struct connman_technology *technology)
449 {
450         DBusMessageIter dict;
451         const char *str;
452         connman_bool_t powered;
453
454         connman_dbus_dict_open(iter, &dict);
455
456         str = get_name(technology->type);
457         if (str != NULL)
458                 connman_dbus_dict_append_basic(&dict, "Name",
459                                                 DBUS_TYPE_STRING, &str);
460
461         str = __connman_service_type2string(technology->type);
462         if (str != NULL)
463                 connman_dbus_dict_append_basic(&dict, "Type",
464                                                 DBUS_TYPE_STRING, &str);
465
466         __sync_synchronize();
467         if (technology->enabled > 0)
468                 powered = TRUE;
469         else
470                 powered = FALSE;
471         connman_dbus_dict_append_basic(&dict, "Powered",
472                                         DBUS_TYPE_BOOLEAN, &powered);
473
474         connman_dbus_dict_append_basic(&dict, "Connected",
475                                         DBUS_TYPE_BOOLEAN,
476                                         &technology->connected);
477
478         connman_dbus_dict_append_basic(&dict, "Tethering",
479                                         DBUS_TYPE_BOOLEAN,
480                                         &technology->tethering);
481
482         if (technology->tethering_ident != NULL)
483                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
484                                                 DBUS_TYPE_STRING,
485                                                 &technology->tethering_ident);
486
487         if (technology->tethering_passphrase != NULL)
488                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
489                                                 DBUS_TYPE_STRING,
490                                                 &technology->tethering_passphrase);
491
492         connman_dbus_dict_close(iter, &dict);
493 }
494
495 static void technology_added_signal(struct connman_technology *technology)
496 {
497         DBusMessage *signal;
498         DBusMessageIter iter;
499
500         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
501                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
502         if (signal == NULL)
503                 return;
504
505         dbus_message_iter_init_append(signal, &iter);
506         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
507                                                         &technology->path);
508         append_properties(&iter, technology);
509
510         dbus_connection_send(connection, signal, NULL);
511         dbus_message_unref(signal);
512 }
513
514 static void technology_removed_signal(struct connman_technology *technology)
515 {
516         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
517                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
518                         DBUS_TYPE_OBJECT_PATH, &technology->path,
519                         DBUS_TYPE_INVALID);
520 }
521
522 static DBusMessage *get_properties(DBusConnection *conn,
523                                         DBusMessage *message, void *user_data)
524 {
525         struct connman_technology *technology = user_data;
526         DBusMessage *reply;
527         DBusMessageIter iter;
528
529         reply = dbus_message_new_method_return(message);
530         if (reply == NULL)
531                 return NULL;
532
533         dbus_message_iter_init_append(reply, &iter);
534         append_properties(&iter, technology);
535
536         return reply;
537 }
538
539 void __connman_technology_list_struct(DBusMessageIter *array)
540 {
541         GSList *list;
542         DBusMessageIter entry;
543
544         for (list = technology_list; list; list = list->next) {
545                 struct connman_technology *technology = list->data;
546
547                 if (technology->path == NULL)
548                         continue;
549
550                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
551                                 NULL, &entry);
552                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
553                                 &technology->path);
554                 append_properties(&entry, technology);
555                 dbus_message_iter_close_container(array, &entry);
556         }
557 }
558
559 static gboolean technology_pending_reply(gpointer user_data)
560 {
561         struct connman_technology *technology = user_data;
562         DBusMessage *reply;
563
564         /* Power request timedout, send ETIMEDOUT. */
565         if (technology->pending_reply != NULL) {
566                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
567                 if (reply != NULL)
568                         g_dbus_send_message(connection, reply);
569
570                 dbus_message_unref(technology->pending_reply);
571                 technology->pending_reply = NULL;
572                 technology->pending_timeout = 0;
573         }
574
575         return FALSE;
576 }
577
578 static int technology_enable(struct connman_technology *technology)
579 {
580         GSList *list;
581         int err = 0;
582
583         DBG("technology %p enable", technology);
584
585         __sync_synchronize();
586         if (technology->enabled > 0) {
587                 err = -EALREADY;
588                 goto done;
589         }
590
591         if (technology->pending_reply != NULL) {
592                 err = -EBUSY;
593                 goto done;
594         }
595
596         __connman_rfkill_block(technology->type, FALSE);
597
598         for (list = technology->device_list; list; list = list->next) {
599                 struct connman_device *device = list->data;
600
601                 err = __connman_device_enable(device);
602         }
603
604 done:
605         return err;
606 }
607
608 static int technology_disable(struct connman_technology *technology)
609 {
610         GSList *list;
611         int err = 0;
612
613         DBG("technology %p disable", technology);
614
615         __sync_synchronize();
616         if (technology->enabled == 0) {
617                 err = -EALREADY;
618                 goto done;
619         }
620
621         if (technology->pending_reply != NULL) {
622                 err = -EBUSY;
623                 goto done;
624         }
625
626         if (technology->tethering == TRUE)
627                 set_tethering(technology, FALSE);
628
629         __connman_rfkill_block(technology->type, TRUE);
630
631         for (list = technology->device_list; list; list = list->next) {
632                 struct connman_device *device = list->data;
633
634                 err = __connman_device_disable(device);
635         }
636
637 done:
638         return err;
639 }
640
641 static DBusMessage *set_powered(struct connman_technology *technology,
642                                 DBusMessage *msg, connman_bool_t powered)
643 {
644         DBusMessage *reply = NULL;
645         connman_bool_t persistent;
646         int err;
647
648         if (powered == TRUE) {
649                 err = technology_enable(technology);
650                 persistent = TRUE;
651         } else {
652                 err = technology_disable(technology);
653                 persistent = FALSE;
654         }
655
656         if (err != -EBUSY) {
657                 technology->enable_persistent = persistent;
658                 technology_save(technology);
659         }
660
661         if (err == -EINPROGRESS) {
662                 technology->pending_reply = dbus_message_ref(msg);
663                 technology->pending_timeout = g_timeout_add_seconds(10,
664                                         technology_pending_reply, technology);
665         } else if (err == -EALREADY) {
666                 if (powered == TRUE)
667                         reply = __connman_error_already_enabled(msg);
668                 else
669                         reply = __connman_error_already_disabled(msg);
670         } else if (err < 0)
671                 reply = __connman_error_failed(msg, -err);
672         else
673                 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
674
675         return reply;
676 }
677
678 static DBusMessage *set_property(DBusConnection *conn,
679                                         DBusMessage *msg, void *data)
680 {
681         struct connman_technology *technology = data;
682         DBusMessageIter iter, value;
683         const char *name;
684         int type;
685
686         DBG("conn %p", conn);
687
688         if (dbus_message_iter_init(msg, &iter) == FALSE)
689                 return __connman_error_invalid_arguments(msg);
690
691         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
692                 return __connman_error_invalid_arguments(msg);
693
694         dbus_message_iter_get_basic(&iter, &name);
695         dbus_message_iter_next(&iter);
696
697         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
698                 return __connman_error_invalid_arguments(msg);
699
700         dbus_message_iter_recurse(&iter, &value);
701
702         type = dbus_message_iter_get_arg_type(&value);
703
704         DBG("property %s", name);
705
706         if (g_str_equal(name, "Tethering") == TRUE) {
707                 int err;
708                 connman_bool_t tethering;
709
710                 if (type != DBUS_TYPE_BOOLEAN)
711                         return __connman_error_invalid_arguments(msg);
712
713                 dbus_message_iter_get_basic(&value, &tethering);
714
715                 if (technology->tethering == tethering) {
716                         if (tethering == FALSE)
717                                 return __connman_error_already_disabled(msg);
718                         else
719                                 return __connman_error_already_enabled(msg);
720                 }
721
722                 err = set_tethering(technology, tethering);
723                 if (err < 0)
724                         return __connman_error_failed(msg, -err);
725
726         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
727                 const char *str;
728
729                 dbus_message_iter_get_basic(&value, &str);
730
731                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
732                         return __connman_error_not_supported(msg);
733
734                 if (strlen(str) < 1 || strlen(str) > 32)
735                         return __connman_error_invalid_arguments(msg);
736
737                 if (g_strcmp0(technology->tethering_ident, str) != 0) {
738                         g_free(technology->tethering_ident);
739                         technology->tethering_ident = g_strdup(str);
740                         technology_save(technology);
741
742                         connman_dbus_property_changed_basic(technology->path,
743                                                 CONNMAN_TECHNOLOGY_INTERFACE,
744                                                 "TetheringIdentifier",
745                                                 DBUS_TYPE_STRING,
746                                                 &technology->tethering_ident);
747                 }
748         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
749                 const char *str;
750
751                 dbus_message_iter_get_basic(&value, &str);
752
753                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
754                         return __connman_error_not_supported(msg);
755
756                 if (strlen(str) < 8 || strlen(str) > 63)
757                         return __connman_error_passphrase_required(msg);
758
759                 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
760                         g_free(technology->tethering_passphrase);
761                         technology->tethering_passphrase = g_strdup(str);
762                         technology_save(technology);
763
764                         connman_dbus_property_changed_basic(technology->path,
765                                         CONNMAN_TECHNOLOGY_INTERFACE,
766                                         "TetheringPassphrase",
767                                         DBUS_TYPE_STRING,
768                                         &technology->tethering_passphrase);
769                 }
770         } else if (g_str_equal(name, "Powered") == TRUE) {
771                 connman_bool_t enable;
772
773                 if (type != DBUS_TYPE_BOOLEAN)
774                         return __connman_error_invalid_arguments(msg);
775
776                 dbus_message_iter_get_basic(&value, &enable);
777
778                 return set_powered(technology, msg, enable);
779         } else
780                 return __connman_error_invalid_property(msg);
781
782         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
783 }
784
785 static struct connman_technology *technology_find(enum connman_service_type type)
786 {
787         GSList *list;
788
789         DBG("type %d", type);
790
791         for (list = technology_list; list; list = list->next) {
792                 struct connman_technology *technology = list->data;
793
794                 if (technology->type == type)
795                         return technology;
796         }
797
798         return NULL;
799 }
800
801 static void reply_scan_pending(struct connman_technology *technology, int err)
802 {
803         DBusMessage *reply;
804
805         DBG("technology %p err %d", technology, err);
806
807         while (technology->scan_pending != NULL) {
808                 DBusMessage *msg = technology->scan_pending->data;
809
810                 DBG("reply to %s", dbus_message_get_sender(msg));
811
812                 if (err == 0)
813                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
814                 else
815                         reply = __connman_error_failed(msg, -err);
816                 g_dbus_send_message(connection, reply);
817                 dbus_message_unref(msg);
818
819                 technology->scan_pending =
820                         g_slist_delete_link(technology->scan_pending,
821                                         technology->scan_pending);
822         }
823 }
824
825 void __connman_technology_scan_started(struct connman_device *device)
826 {
827         DBG("device %p", device);
828 }
829
830 void __connman_technology_scan_stopped(struct connman_device *device)
831 {
832         int count = 0;
833         struct connman_technology *technology;
834         enum connman_service_type type;
835         GSList *list;
836
837         type = __connman_device_get_service_type(device);
838         technology = technology_find(type);
839
840         DBG("technology %p device %p", technology, device);
841
842         if (technology == NULL)
843                 return;
844
845         for (list = technology->device_list; list != NULL; list = list->next) {
846                 struct connman_device *other_device = list->data;
847
848                 if (device == other_device)
849                         continue;
850
851                 if (__connman_device_get_service_type(other_device) != type)
852                         continue;
853
854                 if (connman_device_get_scanning(other_device) == TRUE)
855                         count += 1;
856         }
857
858         if (count == 0)
859                 reply_scan_pending(technology, 0);
860 }
861
862 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
863                                                 int result, const char *alpha2)
864 {
865         struct connman_technology *technology;
866         enum connman_service_type type;
867
868         type = __connman_device_get_service_type(device);
869         technology = technology_find(type);
870
871         if (technology == NULL)
872                 return;
873
874         if (result < 0) {
875                 if (technology->driver != NULL &&
876                                 technology->driver->set_regdom != NULL) {
877                         technology->driver->set_regdom(technology, alpha2);
878                         return;
879                 }
880
881                 alpha2 = NULL;
882         }
883
884         connman_technology_regdom_notify(technology, alpha2);
885 }
886
887 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
888 {
889         struct connman_technology *technology = data;
890         int err;
891
892         DBG ("technology %p request from %s", technology,
893                         dbus_message_get_sender(msg));
894
895         dbus_message_ref(msg);
896         technology->scan_pending =
897                 g_slist_prepend(technology->scan_pending, msg);
898
899         err = __connman_device_request_scan(technology->type);
900         if (err < 0)
901                 reply_scan_pending(technology, err);
902
903         return NULL;
904 }
905
906 static const GDBusMethodTable technology_methods[] = {
907         { GDBUS_DEPRECATED_METHOD("GetProperties",
908                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
909                         get_properties) },
910         { GDBUS_ASYNC_METHOD("SetProperty",
911                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
912                         NULL, set_property) },
913         { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
914         { },
915 };
916
917 static const GDBusSignalTable technology_signals[] = {
918         { GDBUS_SIGNAL("PropertyChanged",
919                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
920         { },
921 };
922
923 static struct connman_technology *technology_get(enum connman_service_type type)
924 {
925         struct connman_technology *technology;
926         struct connman_technology_driver *driver = NULL;
927         const char *str;
928         GSList *list;
929         int err;
930
931         DBG("type %d", type);
932
933         str = __connman_service_type2string(type);
934         if (str == NULL)
935                 return NULL;
936
937         technology = technology_find(type);
938         if (technology != NULL) {
939                 __sync_fetch_and_add(&technology->refcount, 1);
940                 return technology;
941         }
942
943         /* First check if we have a driver for this technology type */
944         for (list = driver_list; list; list = list->next) {
945                 driver = list->data;
946
947                 if (driver->type == type)
948                         break;
949                 else
950                         driver = NULL;
951         }
952
953         if (driver == NULL) {
954                 DBG("No matching driver found for %s.",
955                                 __connman_service_type2string(type));
956                 return NULL;
957         }
958
959         technology = g_try_new0(struct connman_technology, 1);
960         if (technology == NULL)
961                 return NULL;
962
963         technology->refcount = 1;
964
965         technology->type = type;
966         technology->path = g_strdup_printf("%s/technology/%s",
967                                                         CONNMAN_PATH, str);
968
969         technology->device_list = NULL;
970
971         technology->pending_reply = NULL;
972
973         technology_load(technology);
974
975         if (g_dbus_register_interface(connection, technology->path,
976                                         CONNMAN_TECHNOLOGY_INTERFACE,
977                                         technology_methods, technology_signals,
978                                         NULL, technology, NULL) == FALSE) {
979                 connman_error("Failed to register %s", technology->path);
980                 g_free(technology);
981                 return NULL;
982         }
983
984         technology_list = g_slist_prepend(technology_list, technology);
985
986         technology_added_signal(technology);
987
988         technology->driver = driver;
989         err = driver->probe(technology);
990         if (err != 0)
991                 DBG("Driver probe failed for technology %p", technology);
992
993         DBG("technology %p", technology);
994
995         return technology;
996 }
997
998 static void technology_put(struct connman_technology *technology)
999 {
1000         DBG("technology %p", technology);
1001
1002         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1003                 return;
1004
1005         reply_scan_pending(technology, -EINTR);
1006
1007         if (technology->driver) {
1008                 technology->driver->remove(technology);
1009                 technology->driver = NULL;
1010         }
1011
1012         technology_list = g_slist_remove(technology_list, technology);
1013
1014         technology_removed_signal(technology);
1015
1016         g_dbus_unregister_interface(connection, technology->path,
1017                                                 CONNMAN_TECHNOLOGY_INTERFACE);
1018
1019         g_slist_free(technology->device_list);
1020
1021         g_free(technology->path);
1022         g_free(technology->regdom);
1023         g_free(technology->tethering_ident);
1024         g_free(technology->tethering_passphrase);
1025         g_free(technology);
1026 }
1027
1028 void __connman_technology_add_interface(enum connman_service_type type,
1029                                 int index, const char *name, const char *ident)
1030 {
1031         struct connman_technology *technology;
1032
1033         switch (type) {
1034         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1035         case CONNMAN_SERVICE_TYPE_SYSTEM:
1036                 return;
1037         case CONNMAN_SERVICE_TYPE_ETHERNET:
1038         case CONNMAN_SERVICE_TYPE_WIFI:
1039         case CONNMAN_SERVICE_TYPE_WIMAX:
1040         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1041         case CONNMAN_SERVICE_TYPE_CELLULAR:
1042         case CONNMAN_SERVICE_TYPE_GPS:
1043         case CONNMAN_SERVICE_TYPE_VPN:
1044         case CONNMAN_SERVICE_TYPE_GADGET:
1045                 break;
1046         }
1047
1048         connman_info("Adding interface %s [ %s ]", name,
1049                                 __connman_service_type2string(type));
1050
1051         technology = technology_find(type);
1052
1053         if (technology == NULL || technology->driver == NULL
1054                         || technology->driver->add_interface == NULL)
1055                 return;
1056
1057         technology->driver->add_interface(technology,
1058                                         index, name, ident);
1059 }
1060
1061 void __connman_technology_remove_interface(enum connman_service_type type,
1062                                 int index, const char *name, const char *ident)
1063 {
1064         struct connman_technology *technology;
1065
1066         switch (type) {
1067         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1068         case CONNMAN_SERVICE_TYPE_SYSTEM:
1069                 return;
1070         case CONNMAN_SERVICE_TYPE_ETHERNET:
1071         case CONNMAN_SERVICE_TYPE_WIFI:
1072         case CONNMAN_SERVICE_TYPE_WIMAX:
1073         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1074         case CONNMAN_SERVICE_TYPE_CELLULAR:
1075         case CONNMAN_SERVICE_TYPE_GPS:
1076         case CONNMAN_SERVICE_TYPE_VPN:
1077         case CONNMAN_SERVICE_TYPE_GADGET:
1078                 break;
1079         }
1080
1081         connman_info("Remove interface %s [ %s ]", name,
1082                                 __connman_service_type2string(type));
1083
1084         technology = technology_find(type);
1085
1086         if (technology == NULL || technology->driver == NULL)
1087                 return;
1088
1089         if (technology->driver->remove_interface)
1090                 technology->driver->remove_interface(technology, index);
1091 }
1092
1093 int __connman_technology_add_device(struct connman_device *device)
1094 {
1095         struct connman_technology *technology;
1096         enum connman_service_type type;
1097
1098         DBG("device %p", device);
1099
1100         type = __connman_device_get_service_type(device);
1101
1102         technology = technology_get(type);
1103         if (technology == NULL) {
1104                 /*
1105                  * Since no driver can be found for this device at the moment we
1106                  * add it to the techless device list.
1107                 */
1108                 techless_device_list = g_slist_prepend(techless_device_list,
1109                                                                 device);
1110
1111                 return -ENXIO;
1112         }
1113
1114         if (technology->enable_persistent && !global_offlinemode) {
1115                 int err = __connman_device_enable(device);
1116                 /*
1117                  * connman_technology_add_device() calls __connman_device_enable()
1118                  * but since the device is already enabled, the calls does not
1119                  * propagate through to connman_technology_enabled via
1120                  * connman_device_set_powered.
1121                  */
1122                 if (err == -EALREADY)
1123                         __connman_technology_enabled(type);
1124         }
1125         /* if technology persistent state is offline */
1126         if (!technology->enable_persistent)
1127                 __connman_device_disable(device);
1128
1129         technology->device_list = g_slist_prepend(technology->device_list,
1130                                                                 device);
1131
1132         return 0;
1133 }
1134
1135 int __connman_technology_remove_device(struct connman_device *device)
1136 {
1137         struct connman_technology *technology;
1138         enum connman_service_type type;
1139
1140         DBG("device %p", device);
1141
1142         type = __connman_device_get_service_type(device);
1143
1144         technology = technology_find(type);
1145         if (technology == NULL) {
1146                 techless_device_list = g_slist_remove(techless_device_list,
1147                                                                 device);
1148                 return -ENXIO;
1149         }
1150
1151         technology->device_list = g_slist_remove(technology->device_list,
1152                                                                 device);
1153         technology_put(technology);
1154
1155         return 0;
1156 }
1157
1158 static void powered_changed(struct connman_technology *technology)
1159 {
1160         connman_bool_t powered;
1161
1162         __sync_synchronize();
1163         if (technology->enabled >0)
1164                 powered = TRUE;
1165         else
1166                 powered = FALSE;
1167
1168         connman_dbus_property_changed_basic(technology->path,
1169                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1170                         DBUS_TYPE_BOOLEAN, &powered);
1171 }
1172
1173 int __connman_technology_enabled(enum connman_service_type type)
1174 {
1175         struct connman_technology *technology;
1176
1177         technology = technology_find(type);
1178         if (technology == NULL)
1179                 return -ENXIO;
1180
1181         if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1182                 return -EALREADY;
1183
1184         powered_changed(technology);
1185
1186         if (technology->pending_reply != NULL) {
1187                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1188                 dbus_message_unref(technology->pending_reply);
1189                 g_source_remove(technology->pending_timeout);
1190                 technology->pending_reply = NULL;
1191                 technology->pending_timeout = 0;
1192         }
1193
1194         return 0;
1195 }
1196
1197 int __connman_technology_disabled(enum connman_service_type type)
1198 {
1199         struct connman_technology *technology;
1200
1201         technology = technology_find(type);
1202         if (technology == NULL)
1203                 return -ENXIO;
1204
1205         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1206                 return -EINPROGRESS;
1207
1208         if (technology->pending_reply != NULL) {
1209                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1210                 dbus_message_unref(technology->pending_reply);
1211                 g_source_remove(technology->pending_timeout);
1212                 technology->pending_reply = NULL;
1213                 technology->pending_timeout = 0;
1214         }
1215
1216         powered_changed(technology);
1217
1218         return 0;
1219 }
1220
1221 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1222 {
1223         GSList *list;
1224         int err = -EINVAL;
1225
1226         if (global_offlinemode == offlinemode)
1227                 return 0;
1228
1229         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1230
1231         /*
1232          * This is a bit tricky. When you set offlinemode, there is no
1233          * way to differentiate between attempting offline mode and
1234          * resuming offlinemode from last saved profile. We need that
1235          * information in rfkill_update, otherwise it falls back on the
1236          * technology's persistent state. Hence we set the offline mode here
1237          * but save it & call the notifier only if its successful.
1238          */
1239
1240         global_offlinemode = offlinemode;
1241
1242         /* Traverse technology list, enable/disable each technology. */
1243         for (list = technology_list; list; list = list->next) {
1244                 struct connman_technology *technology = list->data;
1245
1246                 if (offlinemode)
1247                         err = technology_disable(technology);
1248
1249                 if (!offlinemode && technology->enable_persistent)
1250                         err = technology_enable(technology);
1251         }
1252
1253         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1254                 connman_technology_save_offlinemode();
1255                 __connman_notifier_offlinemode(offlinemode);
1256         } else
1257                 global_offlinemode = connman_technology_load_offlinemode();
1258
1259         return err;
1260 }
1261
1262 void __connman_technology_set_connected(enum connman_service_type type,
1263                 connman_bool_t connected)
1264 {
1265         struct connman_technology *technology;
1266
1267         technology = technology_find(type);
1268         if (technology == NULL)
1269                 return;
1270
1271         DBG("technology %p connected %d", technology, connected);
1272
1273         technology->connected = connected;
1274
1275         connman_dbus_property_changed_basic(technology->path,
1276                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1277                         DBUS_TYPE_BOOLEAN, &connected);
1278 }
1279
1280 int __connman_technology_add_rfkill(unsigned int index,
1281                                         enum connman_service_type type,
1282                                                 connman_bool_t softblock,
1283                                                 connman_bool_t hardblock)
1284 {
1285         struct connman_technology *technology;
1286         struct connman_rfkill *rfkill;
1287
1288         DBG("index %u type %d soft %u hard %u", index, type,
1289                                                         softblock, hardblock);
1290
1291         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1292         if (rfkill != NULL)
1293                 goto done;
1294
1295         rfkill = g_try_new0(struct connman_rfkill, 1);
1296         if (rfkill == NULL)
1297                 return -ENOMEM;
1298
1299         rfkill->index = index;
1300         rfkill->type = type;
1301         rfkill->softblock = softblock;
1302         rfkill->hardblock = hardblock;
1303
1304         g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1305
1306 done:
1307         technology = technology_get(type);
1308         /* If there is no driver for this type, ignore it. */
1309         if (technology == NULL)
1310                 return -ENXIO;
1311
1312         if (hardblock) {
1313                 DBG("%s is switched off.", get_name(type));
1314                 return 0;
1315         }
1316
1317         /*
1318          * If Offline mode is on, we softblock the device if it isnt already.
1319          * If Offline mode is off, we rely on the persistent state of tech.
1320          */
1321         if (global_offlinemode) {
1322                 if (!softblock)
1323                         return __connman_rfkill_block(type, TRUE);
1324         } else {
1325                 if (technology->enable_persistent && softblock)
1326                         return __connman_rfkill_block(type, FALSE);
1327                 /* if technology persistent state is offline */
1328                 if (!technology->enable_persistent && !softblock)
1329                         return __connman_rfkill_block(type, TRUE);
1330         }
1331
1332         return 0;
1333 }
1334
1335 int __connman_technology_update_rfkill(unsigned int index,
1336                                         enum connman_service_type type,
1337                                                 connman_bool_t softblock,
1338                                                 connman_bool_t hardblock)
1339 {
1340         struct connman_technology *technology;
1341         struct connman_rfkill *rfkill;
1342
1343         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1344
1345         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1346         if (rfkill == NULL)
1347                 return -ENXIO;
1348
1349         if (rfkill->softblock == softblock &&
1350                 rfkill->hardblock == hardblock)
1351                 return 0;
1352
1353         rfkill->softblock = softblock;
1354         rfkill->hardblock = hardblock;
1355
1356         if (hardblock) {
1357                 DBG("%s is switched off.", get_name(type));
1358                 return 0;
1359         }
1360
1361         technology = technology_find(type);
1362         /* If there is no driver for this type, ignore it. */
1363         if (technology == NULL)
1364                 return -ENXIO;
1365
1366         if (!global_offlinemode) {
1367                 if (technology->enable_persistent && softblock)
1368                         return __connman_rfkill_block(type, FALSE);
1369                 if (!technology->enable_persistent && !softblock)
1370                         return __connman_rfkill_block(type, TRUE);
1371         }
1372
1373         return 0;
1374 }
1375
1376 int __connman_technology_remove_rfkill(unsigned int index,
1377                                         enum connman_service_type type)
1378 {
1379         struct connman_technology *technology;
1380         struct connman_rfkill *rfkill;
1381
1382         DBG("index %u", index);
1383
1384         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1385         if (rfkill == NULL)
1386                 return -ENXIO;
1387
1388         g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1389
1390         technology = technology_find(type);
1391         if (technology == NULL)
1392                 return -ENXIO;
1393
1394         technology_put(technology);
1395
1396         return 0;
1397 }
1398
1399 int __connman_technology_init(void)
1400 {
1401         DBG("");
1402
1403         connection = connman_dbus_get_connection();
1404
1405         rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1406                                                         NULL, free_rfkill);
1407
1408         global_offlinemode = connman_technology_load_offlinemode();
1409
1410         /* This will create settings file if it is missing */
1411         connman_technology_save_offlinemode();
1412
1413         return 0;
1414 }
1415
1416 void __connman_technology_cleanup(void)
1417 {
1418         DBG("");
1419
1420         g_hash_table_destroy(rfkill_list);
1421
1422         dbus_connection_unref(connection);
1423 }