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