technology: Offline mode is OFF by default
[framework/connectivity/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 load_state(struct connman_technology *technology)
296 {
297         GKeyFile *keyfile;
298         gchar *identifier;
299         GError *error = NULL;
300         connman_bool_t enable;
301
302         DBG("technology %p", technology);
303
304         keyfile = __connman_storage_load_global();
305         /* Fallback on disabling technology if file not found. */
306         if (keyfile == NULL) {
307                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
308                         /* We enable ethernet by default */
309                         technology->enable_persistent = TRUE;
310                 else
311                         technology->enable_persistent = FALSE;
312                 return;
313         }
314
315         identifier = g_strdup_printf("%s", get_name(technology->type));
316         if (identifier == NULL)
317                 goto done;
318
319         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
320         if (error == NULL)
321                 technology->enable_persistent = enable;
322         else {
323                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
324                         technology->enable_persistent = TRUE;
325                 else
326                         technology->enable_persistent = FALSE;
327                 g_clear_error(&error);
328         }
329 done:
330         g_free(identifier);
331
332         g_key_file_free(keyfile);
333
334         return;
335 }
336
337 static void save_state(struct connman_technology *technology)
338 {
339         GKeyFile *keyfile;
340         gchar *identifier;
341
342         DBG("technology %p", technology);
343
344         keyfile = __connman_storage_load_global();
345         if (keyfile == NULL)
346                 keyfile = g_key_file_new();
347
348         identifier = g_strdup_printf("%s", get_name(technology->type));
349         if (identifier == NULL)
350                 goto done;
351
352         g_key_file_set_boolean(keyfile, identifier, "Enable",
353                                 technology->enable_persistent);
354
355 done:
356         g_free(identifier);
357
358         __connman_storage_save_global(keyfile);
359
360         g_key_file_free(keyfile);
361
362         return;
363 }
364
365 connman_bool_t __connman_technology_get_offlinemode(void)
366 {
367         return global_offlinemode;
368 }
369
370 static void connman_technology_save_offlinemode()
371 {
372         GKeyFile *keyfile;
373
374         keyfile = __connman_storage_load_global();
375         if (keyfile == NULL)
376                 keyfile = g_key_file_new();
377
378         g_key_file_set_boolean(keyfile, "global",
379                                         "OfflineMode", global_offlinemode);
380
381         __connman_storage_save_global(keyfile);
382
383         g_key_file_free(keyfile);
384
385         return;
386 }
387
388 static connman_bool_t connman_technology_load_offlinemode()
389 {
390         GKeyFile *keyfile;
391         GError *error = NULL;
392         connman_bool_t offlinemode;
393
394         /* If there is a error, we enable offlinemode */
395         keyfile = __connman_storage_load_global();
396         if (keyfile == NULL)
397                 return FALSE;
398
399         offlinemode = g_key_file_get_boolean(keyfile, "global",
400                                                 "OfflineMode", &error);
401         if (error != NULL) {
402                 offlinemode = FALSE;
403                 g_clear_error(&error);
404         }
405
406         g_key_file_free(keyfile);
407
408         return offlinemode;
409 }
410
411 static void append_properties(DBusMessageIter *iter,
412                 struct connman_technology *technology)
413 {
414         DBusMessageIter dict;
415         const char *str;
416         connman_bool_t powered;
417
418         connman_dbus_dict_open(iter, &dict);
419
420         str = get_name(technology->type);
421         if (str != NULL)
422                 connman_dbus_dict_append_basic(&dict, "Name",
423                                                 DBUS_TYPE_STRING, &str);
424
425         str = __connman_service_type2string(technology->type);
426         if (str != NULL)
427                 connman_dbus_dict_append_basic(&dict, "Type",
428                                                 DBUS_TYPE_STRING, &str);
429
430         __sync_synchronize();
431         if (technology->enabled > 0)
432                 powered = TRUE;
433         else
434                 powered = FALSE;
435         connman_dbus_dict_append_basic(&dict, "Powered",
436                                         DBUS_TYPE_BOOLEAN, &powered);
437
438         connman_dbus_dict_append_basic(&dict, "Connected",
439                                         DBUS_TYPE_BOOLEAN,
440                                         &technology->connected);
441
442         connman_dbus_dict_append_basic(&dict, "Tethering",
443                                         DBUS_TYPE_BOOLEAN,
444                                         &technology->tethering);
445
446         if (technology->tethering_ident != NULL)
447                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
448                                                 DBUS_TYPE_STRING,
449                                                 &technology->tethering_ident);
450
451         if (technology->tethering_passphrase != NULL)
452                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
453                                                 DBUS_TYPE_STRING,
454                                                 &technology->tethering_passphrase);
455
456         connman_dbus_dict_close(iter, &dict);
457 }
458
459 static void technology_added_signal(struct connman_technology *technology)
460 {
461         DBusMessage *signal;
462         DBusMessageIter iter;
463
464         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
465                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
466         if (signal == NULL)
467                 return;
468
469         dbus_message_iter_init_append(signal, &iter);
470         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
471                                                         &technology->path);
472         append_properties(&iter, technology);
473
474         dbus_connection_send(connection, signal, NULL);
475         dbus_message_unref(signal);
476 }
477
478 static void technology_removed_signal(struct connman_technology *technology)
479 {
480         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
481                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
482                         DBUS_TYPE_OBJECT_PATH, &technology->path,
483                         DBUS_TYPE_INVALID);
484 }
485
486 static DBusMessage *get_properties(DBusConnection *conn,
487                                         DBusMessage *message, void *user_data)
488 {
489         struct connman_technology *technology = user_data;
490         DBusMessage *reply;
491         DBusMessageIter iter;
492
493         reply = dbus_message_new_method_return(message);
494         if (reply == NULL)
495                 return NULL;
496
497         dbus_message_iter_init_append(reply, &iter);
498         append_properties(&iter, technology);
499
500         return reply;
501 }
502
503 void __connman_technology_list_struct(DBusMessageIter *array)
504 {
505         GSList *list;
506         DBusMessageIter entry;
507
508         for (list = technology_list; list; list = list->next) {
509                 struct connman_technology *technology = list->data;
510
511                 if (technology->path == NULL)
512                         continue;
513
514                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
515                                 NULL, &entry);
516                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
517                                 &technology->path);
518                 append_properties(&entry, technology);
519                 dbus_message_iter_close_container(array, &entry);
520         }
521 }
522
523 static gboolean technology_pending_reply(gpointer user_data)
524 {
525         struct connman_technology *technology = user_data;
526         DBusMessage *reply;
527
528         /* Power request timedout, send ETIMEDOUT. */
529         if (technology->pending_reply != NULL) {
530                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
531                 if (reply != NULL)
532                         g_dbus_send_message(connection, reply);
533
534                 dbus_message_unref(technology->pending_reply);
535                 technology->pending_reply = NULL;
536                 technology->pending_timeout = 0;
537         }
538
539         return FALSE;
540 }
541
542 static int technology_enable(struct connman_technology *technology,
543                 DBusMessage *msg)
544 {
545         GSList *list;
546         int err = 0;
547         int ret = -ENODEV;
548         DBusMessage *reply;
549
550         DBG("technology %p enable", technology);
551
552         __sync_synchronize();
553         if (technology->enabled > 0) {
554                 err = -EALREADY;
555                 goto done;
556         }
557
558         if (technology->pending_reply != NULL) {
559                 err = -EBUSY;
560                 goto done;
561         }
562
563         if (msg != NULL) {
564                 /*
565                  * This is a bit of a trick. When msg is not NULL it means
566                  * thats technology_enable was invoked from the manager API.
567                  * Hence we save the state here.
568                  */
569                 technology->enable_persistent = TRUE;
570                 save_state(technology);
571         }
572
573         __connman_rfkill_block(technology->type, FALSE);
574
575         /*
576          * An empty device list means that devices in the technology
577          * were rfkill blocked. The unblock above will enable the devs.
578          */
579         if (technology->device_list == NULL) {
580                 ret = 0;
581                 goto done;
582         }
583
584         for (list = technology->device_list; list; list = list->next) {
585                 struct connman_device *device = list->data;
586
587                 err = __connman_device_enable(device);
588                 /*
589                  * err = 0 : Device was enabled right away.
590                  * If atleast one device gets enabled, we consider
591                  * the technology to be enabled.
592                  */
593                 if (err == 0)
594                         ret = 0;
595         }
596
597 done:
598         if (ret == 0) {
599                 if (msg != NULL)
600                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
601                 return ret;
602         }
603
604         if (msg != NULL) {
605                 if (err == -EINPROGRESS) {
606                         technology->pending_reply = dbus_message_ref(msg);
607                         technology->pending_timeout = g_timeout_add_seconds(10,
608                                         technology_pending_reply, technology);
609                 } else {
610                         reply = __connman_error_failed(msg, -err);
611                         if (reply != NULL)
612                                 g_dbus_send_message(connection, reply);
613                 }
614         }
615
616         return err;
617 }
618
619 static int technology_disable(struct connman_technology *technology,
620                 DBusMessage *msg)
621 {
622         GSList *list;
623         int err = 0;
624         int ret = -ENODEV;
625         DBusMessage *reply;
626
627         DBG("technology %p disable", technology);
628
629         __sync_synchronize();
630         if (technology->enabled == 0) {
631                 err = -EALREADY;
632                 goto done;
633         }
634
635         if (technology->pending_reply != NULL) {
636                 err = -EBUSY;
637                 goto done;
638         }
639
640         if (technology->tethering == TRUE)
641                 set_tethering(technology, FALSE);
642
643         if (msg != NULL) {
644                 technology->enable_persistent = FALSE;
645                 save_state(technology);
646         }
647
648         __connman_rfkill_block(technology->type, TRUE);
649
650         for (list = technology->device_list; list; list = list->next) {
651                 struct connman_device *device = list->data;
652
653                 err = __connman_device_disable(device);
654                 if (err == 0)
655                         ret = 0;
656         }
657
658 done:
659         if (ret == 0) {
660                 if (msg != NULL)
661                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
662                 return ret;
663         }
664
665         if (msg != NULL) {
666                 if (err == -EINPROGRESS) {
667                         technology->pending_reply = dbus_message_ref(msg);
668                         technology->pending_timeout = g_timeout_add_seconds(10,
669                                         technology_pending_reply, technology);
670                 } else {
671                         reply = __connman_error_failed(msg, -err);
672                         if (reply != NULL)
673                                 g_dbus_send_message(connection, reply);
674                 }
675         }
676
677         return err;
678 }
679
680 static DBusMessage *set_property(DBusConnection *conn,
681                                         DBusMessage *msg, void *data)
682 {
683         struct connman_technology *technology = data;
684         DBusMessageIter iter, value;
685         const char *name;
686         int type;
687
688         DBG("conn %p", conn);
689
690         if (dbus_message_iter_init(msg, &iter) == FALSE)
691                 return __connman_error_invalid_arguments(msg);
692
693         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
694                 return __connman_error_invalid_arguments(msg);
695
696         dbus_message_iter_get_basic(&iter, &name);
697         dbus_message_iter_next(&iter);
698
699         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
700                 return __connman_error_invalid_arguments(msg);
701
702         dbus_message_iter_recurse(&iter, &value);
703
704         type = dbus_message_iter_get_arg_type(&value);
705
706         DBG("property %s", name);
707
708         if (g_str_equal(name, "Tethering") == TRUE) {
709                 int err;
710                 connman_bool_t tethering;
711
712                 if (type != DBUS_TYPE_BOOLEAN)
713                         return __connman_error_invalid_arguments(msg);
714
715                 dbus_message_iter_get_basic(&value, &tethering);
716
717                 if (technology->tethering == tethering)
718                         return __connman_error_in_progress(msg);
719
720                 err = set_tethering(technology, tethering);
721                 if (err < 0)
722                         return __connman_error_failed(msg, -err);
723
724         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
725                 const char *str;
726
727                 dbus_message_iter_get_basic(&value, &str);
728
729                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
730                         return __connman_error_not_supported(msg);
731
732                 technology->tethering_ident = g_strdup(str);
733         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
734                 const char *str;
735
736                 dbus_message_iter_get_basic(&value, &str);
737
738                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
739                         return __connman_error_not_supported(msg);
740
741                 if (strlen(str) < 8)
742                         return __connman_error_invalid_arguments(msg);
743
744                 technology->tethering_passphrase = g_strdup(str);
745         } else if (g_str_equal(name, "Powered") == TRUE) {
746                 connman_bool_t enable;
747
748                 if (type != DBUS_TYPE_BOOLEAN)
749                         return __connman_error_invalid_arguments(msg);
750
751                 dbus_message_iter_get_basic(&value, &enable);
752                 if (enable == TRUE)
753                         technology_enable(technology, msg);
754                 else
755                         technology_disable(technology, msg);
756
757         } else
758                 return __connman_error_invalid_property(msg);
759
760         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
761 }
762
763 static struct connman_technology *technology_find(enum connman_service_type type)
764 {
765         GSList *list;
766
767         DBG("type %d", type);
768
769         for (list = technology_list; list; list = list->next) {
770                 struct connman_technology *technology = list->data;
771
772                 if (technology->type == type)
773                         return technology;
774         }
775
776         return NULL;
777 }
778
779 static void reply_scan_pending(struct connman_technology *technology, int err)
780 {
781         DBusMessage *reply;
782
783         DBG("technology %p err %d", technology, err);
784
785         while (technology->scan_pending != NULL) {
786                 DBusMessage *msg = technology->scan_pending->data;
787
788                 DBG("reply to %s", dbus_message_get_sender(msg));
789
790                 if (err == 0)
791                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
792                 else
793                         reply = __connman_error_failed(msg, -err);
794                 g_dbus_send_message(connection, reply);
795                 dbus_message_unref(msg);
796
797                 technology->scan_pending =
798                         g_slist_delete_link(technology->scan_pending,
799                                         technology->scan_pending);
800         }
801 }
802
803 void __connman_technology_scan_started(struct connman_device *device)
804 {
805         DBG("device %p", device);
806 }
807
808 void __connman_technology_scan_stopped(struct connman_device *device)
809 {
810         int count = 0;
811         struct connman_technology *technology;
812         enum connman_service_type type;
813         GSList *list;
814
815         type = __connman_device_get_service_type(device);
816         technology = technology_find(type);
817
818         DBG("technology %p device %p", technology, device);
819
820         if (technology == NULL)
821                 return;
822
823         for (list = technology->device_list; list != NULL; list = list->next) {
824                 struct connman_device *other_device = list->data;
825
826                 if (device == other_device)
827                         continue;
828
829                 if (__connman_device_get_service_type(other_device) != type)
830                         continue;
831
832                 if (__connman_device_scanning(other_device))
833                         count += 1;
834         }
835
836         if (count == 0)
837                 reply_scan_pending(technology, 0);
838 }
839
840 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
841 {
842         struct connman_technology *technology = data;
843         int err;
844
845         DBG ("technology %p request from %s", technology,
846                         dbus_message_get_sender(msg));
847
848         dbus_message_ref(msg);
849         technology->scan_pending =
850                 g_slist_prepend(technology->scan_pending, msg);
851
852         err = __connman_device_request_scan(technology->type);
853         if (err < 0)
854                 reply_scan_pending(technology, err);
855
856         return NULL;
857 }
858
859 static GDBusMethodTable technology_methods[] = {
860         { "GetProperties", "",   "a{sv}", get_properties },
861         { "SetProperty",   "sv", "",      set_property   },
862         { "Scan",          "",    "",     scan,
863                                                 G_DBUS_METHOD_FLAG_ASYNC },
864         { },
865 };
866
867 static GDBusSignalTable technology_signals[] = {
868         { "PropertyChanged", "sv" },
869         { },
870 };
871
872 static struct connman_technology *technology_get(enum connman_service_type type)
873 {
874         struct connman_technology *technology;
875         struct connman_technology_driver *driver = NULL;
876         const char *str;
877         GSList *list;
878         int err;
879
880         DBG("type %d", type);
881
882         str = __connman_service_type2string(type);
883         if (str == NULL)
884                 return NULL;
885
886         technology = technology_find(type);
887         if (technology != NULL) {
888                 __sync_fetch_and_add(&technology->refcount, 1);
889                 return technology;
890         }
891
892         /* First check if we have a driver for this technology type */
893         for (list = driver_list; list; list = list->next) {
894                 driver = list->data;
895
896                 if (driver->type == type)
897                         break;
898                 else
899                         driver = NULL;
900         }
901
902         if (driver == NULL) {
903                 DBG("No matching driver found for %s.",
904                                 __connman_service_type2string(type));
905                 return NULL;
906         }
907
908         technology = g_try_new0(struct connman_technology, 1);
909         if (technology == NULL)
910                 return NULL;
911
912         technology->refcount = 1;
913
914         technology->type = type;
915         technology->path = g_strdup_printf("%s/technology/%s",
916                                                         CONNMAN_PATH, str);
917
918         technology->device_list = NULL;
919
920         technology->pending_reply = NULL;
921
922         load_state(technology);
923
924         if (g_dbus_register_interface(connection, technology->path,
925                                         CONNMAN_TECHNOLOGY_INTERFACE,
926                                         technology_methods, technology_signals,
927                                         NULL, technology, NULL) == FALSE) {
928                 connman_error("Failed to register %s", technology->path);
929                 g_free(technology);
930                 return NULL;
931         }
932
933         technology_list = g_slist_append(technology_list, technology);
934
935         technology_added_signal(technology);
936
937         technology->driver = driver;
938         err = driver->probe(technology);
939         if (err != 0)
940                 DBG("Driver probe failed for technology %p", technology);
941
942         DBG("technology %p", technology);
943
944         return technology;
945 }
946
947 static void technology_put(struct connman_technology *technology)
948 {
949         DBG("technology %p", technology);
950
951         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
952                 return;
953
954         reply_scan_pending(technology, -EINTR);
955
956         if (technology->driver) {
957                 technology->driver->remove(technology);
958                 technology->driver = NULL;
959         }
960
961         technology_list = g_slist_remove(technology_list, technology);
962
963         technology_removed_signal(technology);
964
965         g_dbus_unregister_interface(connection, technology->path,
966                                                 CONNMAN_TECHNOLOGY_INTERFACE);
967
968         g_slist_free(technology->device_list);
969
970         g_free(technology->path);
971         g_free(technology->regdom);
972         g_free(technology);
973 }
974
975 void __connman_technology_add_interface(enum connman_service_type type,
976                                 int index, const char *name, const char *ident)
977 {
978         struct connman_technology *technology;
979
980         switch (type) {
981         case CONNMAN_SERVICE_TYPE_UNKNOWN:
982         case CONNMAN_SERVICE_TYPE_SYSTEM:
983                 return;
984         case CONNMAN_SERVICE_TYPE_ETHERNET:
985         case CONNMAN_SERVICE_TYPE_WIFI:
986         case CONNMAN_SERVICE_TYPE_WIMAX:
987         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
988         case CONNMAN_SERVICE_TYPE_CELLULAR:
989         case CONNMAN_SERVICE_TYPE_GPS:
990         case CONNMAN_SERVICE_TYPE_VPN:
991         case CONNMAN_SERVICE_TYPE_GADGET:
992                 break;
993         }
994
995         connman_info("Adding interface %s [ %s ]", name,
996                                 __connman_service_type2string(type));
997
998         technology = technology_find(type);
999
1000         if (technology == NULL || technology->driver == NULL
1001                         || technology->driver->add_interface == NULL)
1002                 return;
1003
1004         technology->driver->add_interface(technology,
1005                                         index, name, ident);
1006 }
1007
1008 void __connman_technology_remove_interface(enum connman_service_type type,
1009                                 int index, const char *name, const char *ident)
1010 {
1011         struct connman_technology *technology;
1012
1013         switch (type) {
1014         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1015         case CONNMAN_SERVICE_TYPE_SYSTEM:
1016                 return;
1017         case CONNMAN_SERVICE_TYPE_ETHERNET:
1018         case CONNMAN_SERVICE_TYPE_WIFI:
1019         case CONNMAN_SERVICE_TYPE_WIMAX:
1020         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1021         case CONNMAN_SERVICE_TYPE_CELLULAR:
1022         case CONNMAN_SERVICE_TYPE_GPS:
1023         case CONNMAN_SERVICE_TYPE_VPN:
1024         case CONNMAN_SERVICE_TYPE_GADGET:
1025                 break;
1026         }
1027
1028         connman_info("Remove interface %s [ %s ]", name,
1029                                 __connman_service_type2string(type));
1030
1031         technology = technology_find(type);
1032
1033         if (technology == NULL || technology->driver == NULL)
1034                 return;
1035
1036         if (technology->driver->remove_interface)
1037                 technology->driver->remove_interface(technology, index);
1038 }
1039
1040 int __connman_technology_add_device(struct connman_device *device)
1041 {
1042         struct connman_technology *technology;
1043         enum connman_service_type type;
1044
1045         DBG("device %p", device);
1046
1047         type = __connman_device_get_service_type(device);
1048
1049         technology = technology_get(type);
1050         if (technology == NULL) {
1051                 /*
1052                  * Since no driver can be found for this device at the moment we
1053                  * add it to the techless device list.
1054                 */
1055                 techless_device_list = g_slist_prepend(techless_device_list,
1056                                                                 device);
1057
1058                 return -ENXIO;
1059         }
1060
1061         if (technology->enable_persistent && !global_offlinemode) {
1062                 int err = __connman_device_enable(device);
1063                 /*
1064                  * connman_technology_add_device() calls __connman_device_enable()
1065                  * but since the device is already enabled, the calls does not
1066                  * propagate through to connman_technology_enabled via
1067                  * connman_device_set_powered.
1068                  */
1069                 if (err == -EALREADY)
1070                         __connman_technology_enabled(type);
1071         }
1072         /* if technology persistent state is offline */
1073         if (!technology->enable_persistent)
1074                 __connman_device_disable(device);
1075
1076         technology->device_list = g_slist_append(technology->device_list,
1077                                                                 device);
1078
1079         return 0;
1080 }
1081
1082 int __connman_technology_remove_device(struct connman_device *device)
1083 {
1084         struct connman_technology *technology;
1085         enum connman_service_type type;
1086
1087         DBG("device %p", device);
1088
1089         type = __connman_device_get_service_type(device);
1090
1091         technology = technology_find(type);
1092         if (technology == NULL) {
1093                 techless_device_list = g_slist_remove(techless_device_list,
1094                                                                 device);
1095                 return -ENXIO;
1096         }
1097
1098         if (__connman_device_scanning(device))
1099                 __connman_technology_scan_stopped(device);
1100
1101         technology->device_list = g_slist_remove(technology->device_list,
1102                                                                 device);
1103         technology_put(technology);
1104
1105         return 0;
1106 }
1107
1108 static void powered_changed(struct connman_technology *technology)
1109 {
1110         connman_bool_t powered;
1111
1112         __sync_synchronize();
1113         if (technology->enabled >0)
1114                 powered = TRUE;
1115         else
1116                 powered = FALSE;
1117
1118         connman_dbus_property_changed_basic(technology->path,
1119                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1120                         DBUS_TYPE_BOOLEAN, &powered);
1121 }
1122
1123 int __connman_technology_enabled(enum connman_service_type type)
1124 {
1125         struct connman_technology *technology;
1126
1127         technology = technology_find(type);
1128         if (technology == NULL)
1129                 return -ENXIO;
1130
1131         if (__sync_fetch_and_add(&technology->enabled, 1) == 0)
1132                 powered_changed(technology);
1133
1134         if (technology->pending_reply != NULL) {
1135                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1136                 dbus_message_unref(technology->pending_reply);
1137                 g_source_remove(technology->pending_timeout);
1138                 technology->pending_reply = NULL;
1139                 technology->pending_timeout = 0;
1140         }
1141
1142         return 0;
1143 }
1144
1145 int __connman_technology_disabled(enum connman_service_type type)
1146 {
1147         struct connman_technology *technology;
1148
1149         technology = technology_find(type);
1150         if (technology == NULL)
1151                 return -ENXIO;
1152
1153         if (technology->pending_reply != NULL) {
1154                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1155                 dbus_message_unref(technology->pending_reply);
1156                 g_source_remove(technology->pending_timeout);
1157                 technology->pending_reply = NULL;
1158                 technology->pending_timeout = 0;
1159         }
1160
1161         if (__sync_fetch_and_sub(&technology->enabled, 1) == 1)
1162                 powered_changed(technology);
1163
1164         return 0;
1165 }
1166
1167 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1168 {
1169         GSList *list;
1170         int err = -EINVAL;
1171
1172         if (global_offlinemode == offlinemode)
1173                 return 0;
1174
1175         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1176
1177         /*
1178          * This is a bit tricky. When you set offlinemode, there is no
1179          * way to differentiate between attempting offline mode and
1180          * resuming offlinemode from last saved profile. We need that
1181          * information in rfkill_update, otherwise it falls back on the
1182          * technology's persistent state. Hence we set the offline mode here
1183          * but save it & call the notifier only if its successful.
1184          */
1185
1186         global_offlinemode = offlinemode;
1187
1188         /* Traverse technology list, enable/disable each technology. */
1189         for (list = technology_list; list; list = list->next) {
1190                 struct connman_technology *technology = list->data;
1191
1192                 if (offlinemode)
1193                         err = technology_disable(technology, NULL);
1194
1195                 if (!offlinemode && technology->enable_persistent)
1196                         err = technology_enable(technology, NULL);
1197         }
1198
1199         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1200                 connman_technology_save_offlinemode();
1201                 __connman_notifier_offlinemode(offlinemode);
1202         } else
1203                 global_offlinemode = connman_technology_load_offlinemode();
1204
1205         return err;
1206 }
1207
1208 void __connman_technology_set_connected(enum connman_service_type type,
1209                 connman_bool_t connected)
1210 {
1211         struct connman_technology *technology;
1212
1213         technology = technology_find(type);
1214         if (technology == NULL)
1215                 return;
1216
1217         DBG("technology %p connected %d", technology, connected);
1218
1219         technology->connected = connected;
1220
1221         connman_dbus_property_changed_basic(technology->path,
1222                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1223                         DBUS_TYPE_BOOLEAN, &connected);
1224 }
1225
1226 int __connman_technology_add_rfkill(unsigned int index,
1227                                         enum connman_service_type type,
1228                                                 connman_bool_t softblock,
1229                                                 connman_bool_t hardblock)
1230 {
1231         struct connman_technology *technology;
1232         struct connman_rfkill *rfkill;
1233
1234         DBG("index %u type %d soft %u hard %u", index, type,
1235                                                         softblock, hardblock);
1236
1237         rfkill = g_hash_table_lookup(rfkill_list, &index);
1238         if (rfkill != NULL)
1239                 goto done;
1240
1241         rfkill = g_try_new0(struct connman_rfkill, 1);
1242         if (rfkill == NULL)
1243                 return -ENOMEM;
1244
1245         rfkill->index = index;
1246         rfkill->type = type;
1247         rfkill->softblock = softblock;
1248         rfkill->hardblock = hardblock;
1249
1250         g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1251
1252 done:
1253         technology = technology_get(type);
1254         /* If there is no driver for this type, ignore it. */
1255         if (technology == NULL)
1256                 return -ENXIO;
1257
1258         if (hardblock) {
1259                 DBG("%s is switched off.", get_name(type));
1260                 return 0;
1261         }
1262
1263         /*
1264          * If Offline mode is on, we softblock the device if it isnt already.
1265          * If Offline mode is off, we rely on the persistent state of tech.
1266          */
1267         if (global_offlinemode) {
1268                 if (!softblock)
1269                         return __connman_rfkill_block(type, TRUE);
1270         } else {
1271                 if (technology->enable_persistent && softblock)
1272                         return __connman_rfkill_block(type, FALSE);
1273                 /* if technology persistent state is offline */
1274                 if (!technology->enable_persistent && !softblock)
1275                         return __connman_rfkill_block(type, TRUE);
1276         }
1277
1278         return 0;
1279 }
1280
1281 int __connman_technology_update_rfkill(unsigned int index,
1282                                         enum connman_service_type type,
1283                                                 connman_bool_t softblock,
1284                                                 connman_bool_t hardblock)
1285 {
1286         struct connman_technology *technology;
1287         struct connman_rfkill *rfkill;
1288
1289         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1290
1291         rfkill = g_hash_table_lookup(rfkill_list, &index);
1292         if (rfkill == NULL)
1293                 return -ENXIO;
1294
1295         if (rfkill->softblock == softblock &&
1296                 rfkill->hardblock == hardblock)
1297                 return 0;
1298
1299         rfkill->softblock = softblock;
1300         rfkill->hardblock = hardblock;
1301
1302         if (hardblock) {
1303                 DBG("%s is switched off.", get_name(type));
1304                 return 0;
1305         }
1306
1307         technology = technology_find(type);
1308         /* If there is no driver for this type, ignore it. */
1309         if (technology == NULL)
1310                 return -ENXIO;
1311
1312         if (!global_offlinemode) {
1313                 if (technology->enable_persistent && softblock)
1314                         return __connman_rfkill_block(type, FALSE);
1315                 if (!technology->enable_persistent && !softblock)
1316                         return __connman_rfkill_block(type, TRUE);
1317         }
1318
1319         return 0;
1320 }
1321
1322 int __connman_technology_remove_rfkill(unsigned int index,
1323                                         enum connman_service_type type)
1324 {
1325         struct connman_technology *technology;
1326         struct connman_rfkill *rfkill;
1327
1328         DBG("index %u", index);
1329
1330         rfkill = g_hash_table_lookup(rfkill_list, &index);
1331         if (rfkill == NULL)
1332                 return -ENXIO;
1333
1334         g_hash_table_remove(rfkill_list, &index);
1335
1336         technology = technology_find(type);
1337         if (technology == NULL)
1338                 return -ENXIO;
1339
1340         technology_put(technology);
1341
1342         return 0;
1343 }
1344
1345 int __connman_technology_init(void)
1346 {
1347         DBG("");
1348
1349         connection = connman_dbus_get_connection();
1350
1351         rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1352                                                         NULL, free_rfkill);
1353
1354         global_offlinemode = connman_technology_load_offlinemode();
1355
1356         return 0;
1357 }
1358
1359 void __connman_technology_cleanup(void)
1360 {
1361         DBG("");
1362
1363         g_hash_table_destroy(rfkill_list);
1364
1365         dbus_connection_unref(connection);
1366 }