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