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