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