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