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