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