technology: Set regulatory domain by device or fallback to technology based
[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_in_progress(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                 technology->tethering_ident = g_strdup(str);
759         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
760                 const char *str;
761
762                 dbus_message_iter_get_basic(&value, &str);
763
764                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
765                         return __connman_error_not_supported(msg);
766
767                 if (strlen(str) < 8)
768                         return __connman_error_invalid_arguments(msg);
769
770                 technology->tethering_passphrase = g_strdup(str);
771         } else if (g_str_equal(name, "Powered") == TRUE) {
772                 connman_bool_t enable;
773
774                 if (type != DBUS_TYPE_BOOLEAN)
775                         return __connman_error_invalid_arguments(msg);
776
777                 dbus_message_iter_get_basic(&value, &enable);
778                 if (enable == TRUE)
779                         technology_enable(technology, msg);
780                 else
781                         technology_disable(technology, msg);
782
783         } else
784                 return __connman_error_invalid_property(msg);
785
786         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
787 }
788
789 static struct connman_technology *technology_find(enum connman_service_type type)
790 {
791         GSList *list;
792
793         DBG("type %d", type);
794
795         for (list = technology_list; list; list = list->next) {
796                 struct connman_technology *technology = list->data;
797
798                 if (technology->type == type)
799                         return technology;
800         }
801
802         return NULL;
803 }
804
805 static void reply_scan_pending(struct connman_technology *technology, int err)
806 {
807         DBusMessage *reply;
808
809         DBG("technology %p err %d", technology, err);
810
811         while (technology->scan_pending != NULL) {
812                 DBusMessage *msg = technology->scan_pending->data;
813
814                 DBG("reply to %s", dbus_message_get_sender(msg));
815
816                 if (err == 0)
817                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
818                 else
819                         reply = __connman_error_failed(msg, -err);
820                 g_dbus_send_message(connection, reply);
821                 dbus_message_unref(msg);
822
823                 technology->scan_pending =
824                         g_slist_delete_link(technology->scan_pending,
825                                         technology->scan_pending);
826         }
827 }
828
829 void __connman_technology_scan_started(struct connman_device *device)
830 {
831         DBG("device %p", device);
832 }
833
834 void __connman_technology_scan_stopped(struct connman_device *device)
835 {
836         int count = 0;
837         struct connman_technology *technology;
838         enum connman_service_type type;
839         GSList *list;
840
841         type = __connman_device_get_service_type(device);
842         technology = technology_find(type);
843
844         DBG("technology %p device %p", technology, device);
845
846         if (technology == NULL)
847                 return;
848
849         for (list = technology->device_list; list != NULL; list = list->next) {
850                 struct connman_device *other_device = list->data;
851
852                 if (device == other_device)
853                         continue;
854
855                 if (__connman_device_get_service_type(other_device) != type)
856                         continue;
857
858                 if (connman_device_get_scanning(other_device) == TRUE)
859                         count += 1;
860         }
861
862         if (count == 0)
863                 reply_scan_pending(technology, 0);
864 }
865
866 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
867                                                 int result, const char *alpha2)
868 {
869         struct connman_technology *technology;
870         enum connman_service_type type;
871
872         type = __connman_device_get_service_type(device);
873         technology = technology_find(type);
874
875         if (technology == NULL)
876                 return;
877
878         if (result < 0) {
879                 if (technology->driver != NULL &&
880                                 technology->driver->set_regdom != NULL) {
881                         technology->driver->set_regdom(technology, alpha2);
882                         return;
883                 }
884
885                 alpha2 = NULL;
886         }
887
888         connman_technology_regdom_notify(technology, alpha2);
889 }
890
891 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
892 {
893         struct connman_technology *technology = data;
894         int err;
895
896         DBG ("technology %p request from %s", technology,
897                         dbus_message_get_sender(msg));
898
899         dbus_message_ref(msg);
900         technology->scan_pending =
901                 g_slist_prepend(technology->scan_pending, msg);
902
903         err = __connman_device_request_scan(technology->type);
904         if (err < 0)
905                 reply_scan_pending(technology, err);
906
907         return NULL;
908 }
909
910 static const GDBusMethodTable technology_methods[] = {
911         { GDBUS_DEPRECATED_METHOD("GetProperties",
912                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
913                         get_properties) },
914         { GDBUS_METHOD("SetProperty",
915                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
916                         NULL, set_property) },
917         { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
918         { },
919 };
920
921 static const GDBusSignalTable technology_signals[] = {
922         { GDBUS_SIGNAL("PropertyChanged",
923                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
924         { },
925 };
926
927 static struct connman_technology *technology_get(enum connman_service_type type)
928 {
929         struct connman_technology *technology;
930         struct connman_technology_driver *driver = NULL;
931         const char *str;
932         GSList *list;
933         int err;
934
935         DBG("type %d", type);
936
937         str = __connman_service_type2string(type);
938         if (str == NULL)
939                 return NULL;
940
941         technology = technology_find(type);
942         if (technology != NULL) {
943                 __sync_fetch_and_add(&technology->refcount, 1);
944                 return technology;
945         }
946
947         /* First check if we have a driver for this technology type */
948         for (list = driver_list; list; list = list->next) {
949                 driver = list->data;
950
951                 if (driver->type == type)
952                         break;
953                 else
954                         driver = NULL;
955         }
956
957         if (driver == NULL) {
958                 DBG("No matching driver found for %s.",
959                                 __connman_service_type2string(type));
960                 return NULL;
961         }
962
963         technology = g_try_new0(struct connman_technology, 1);
964         if (technology == NULL)
965                 return NULL;
966
967         technology->refcount = 1;
968
969         technology->type = type;
970         technology->path = g_strdup_printf("%s/technology/%s",
971                                                         CONNMAN_PATH, str);
972
973         technology->device_list = NULL;
974
975         technology->pending_reply = NULL;
976
977         load_state(technology);
978
979         if (g_dbus_register_interface(connection, technology->path,
980                                         CONNMAN_TECHNOLOGY_INTERFACE,
981                                         technology_methods, technology_signals,
982                                         NULL, technology, NULL) == FALSE) {
983                 connman_error("Failed to register %s", technology->path);
984                 g_free(technology);
985                 return NULL;
986         }
987
988         technology_list = g_slist_append(technology_list, technology);
989
990         technology_added_signal(technology);
991
992         technology->driver = driver;
993         err = driver->probe(technology);
994         if (err != 0)
995                 DBG("Driver probe failed for technology %p", technology);
996
997         DBG("technology %p", technology);
998
999         return technology;
1000 }
1001
1002 static void technology_put(struct connman_technology *technology)
1003 {
1004         DBG("technology %p", technology);
1005
1006         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1007                 return;
1008
1009         reply_scan_pending(technology, -EINTR);
1010
1011         if (technology->driver) {
1012                 technology->driver->remove(technology);
1013                 technology->driver = NULL;
1014         }
1015
1016         technology_list = g_slist_remove(technology_list, technology);
1017
1018         technology_removed_signal(technology);
1019
1020         g_dbus_unregister_interface(connection, technology->path,
1021                                                 CONNMAN_TECHNOLOGY_INTERFACE);
1022
1023         g_slist_free(technology->device_list);
1024
1025         g_free(technology->path);
1026         g_free(technology->regdom);
1027         g_free(technology);
1028 }
1029
1030 void __connman_technology_add_interface(enum connman_service_type type,
1031                                 int index, const char *name, const char *ident)
1032 {
1033         struct connman_technology *technology;
1034
1035         switch (type) {
1036         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1037         case CONNMAN_SERVICE_TYPE_SYSTEM:
1038                 return;
1039         case CONNMAN_SERVICE_TYPE_ETHERNET:
1040         case CONNMAN_SERVICE_TYPE_WIFI:
1041         case CONNMAN_SERVICE_TYPE_WIMAX:
1042         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1043         case CONNMAN_SERVICE_TYPE_CELLULAR:
1044         case CONNMAN_SERVICE_TYPE_GPS:
1045         case CONNMAN_SERVICE_TYPE_VPN:
1046         case CONNMAN_SERVICE_TYPE_GADGET:
1047                 break;
1048         }
1049
1050         connman_info("Adding interface %s [ %s ]", name,
1051                                 __connman_service_type2string(type));
1052
1053         technology = technology_find(type);
1054
1055         if (technology == NULL || technology->driver == NULL
1056                         || technology->driver->add_interface == NULL)
1057                 return;
1058
1059         technology->driver->add_interface(technology,
1060                                         index, name, ident);
1061 }
1062
1063 void __connman_technology_remove_interface(enum connman_service_type type,
1064                                 int index, const char *name, const char *ident)
1065 {
1066         struct connman_technology *technology;
1067
1068         switch (type) {
1069         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1070         case CONNMAN_SERVICE_TYPE_SYSTEM:
1071                 return;
1072         case CONNMAN_SERVICE_TYPE_ETHERNET:
1073         case CONNMAN_SERVICE_TYPE_WIFI:
1074         case CONNMAN_SERVICE_TYPE_WIMAX:
1075         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1076         case CONNMAN_SERVICE_TYPE_CELLULAR:
1077         case CONNMAN_SERVICE_TYPE_GPS:
1078         case CONNMAN_SERVICE_TYPE_VPN:
1079         case CONNMAN_SERVICE_TYPE_GADGET:
1080                 break;
1081         }
1082
1083         connman_info("Remove interface %s [ %s ]", name,
1084                                 __connman_service_type2string(type));
1085
1086         technology = technology_find(type);
1087
1088         if (technology == NULL || technology->driver == NULL)
1089                 return;
1090
1091         if (technology->driver->remove_interface)
1092                 technology->driver->remove_interface(technology, index);
1093 }
1094
1095 int __connman_technology_add_device(struct connman_device *device)
1096 {
1097         struct connman_technology *technology;
1098         enum connman_service_type type;
1099
1100         DBG("device %p", device);
1101
1102         type = __connman_device_get_service_type(device);
1103
1104         technology = technology_get(type);
1105         if (technology == NULL) {
1106                 /*
1107                  * Since no driver can be found for this device at the moment we
1108                  * add it to the techless device list.
1109                 */
1110                 techless_device_list = g_slist_prepend(techless_device_list,
1111                                                                 device);
1112
1113                 return -ENXIO;
1114         }
1115
1116         if (technology->enable_persistent && !global_offlinemode) {
1117                 int err = __connman_device_enable(device);
1118                 /*
1119                  * connman_technology_add_device() calls __connman_device_enable()
1120                  * but since the device is already enabled, the calls does not
1121                  * propagate through to connman_technology_enabled via
1122                  * connman_device_set_powered.
1123                  */
1124                 if (err == -EALREADY)
1125                         __connman_technology_enabled(type);
1126         }
1127         /* if technology persistent state is offline */
1128         if (!technology->enable_persistent)
1129                 __connman_device_disable(device);
1130
1131         technology->device_list = g_slist_append(technology->device_list,
1132                                                                 device);
1133
1134         return 0;
1135 }
1136
1137 int __connman_technology_remove_device(struct connman_device *device)
1138 {
1139         struct connman_technology *technology;
1140         enum connman_service_type type;
1141
1142         DBG("device %p", device);
1143
1144         type = __connman_device_get_service_type(device);
1145
1146         technology = technology_find(type);
1147         if (technology == NULL) {
1148                 techless_device_list = g_slist_remove(techless_device_list,
1149                                                                 device);
1150                 return -ENXIO;
1151         }
1152
1153         technology->device_list = g_slist_remove(technology->device_list,
1154                                                                 device);
1155         technology_put(technology);
1156
1157         return 0;
1158 }
1159
1160 static void powered_changed(struct connman_technology *technology)
1161 {
1162         connman_bool_t powered;
1163
1164         __sync_synchronize();
1165         if (technology->enabled >0)
1166                 powered = TRUE;
1167         else
1168                 powered = FALSE;
1169
1170         connman_dbus_property_changed_basic(technology->path,
1171                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1172                         DBUS_TYPE_BOOLEAN, &powered);
1173 }
1174
1175 int __connman_technology_enabled(enum connman_service_type type)
1176 {
1177         struct connman_technology *technology;
1178
1179         technology = technology_find(type);
1180         if (technology == NULL)
1181                 return -ENXIO;
1182
1183         if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1184                 return -EALREADY;
1185
1186         powered_changed(technology);
1187
1188         if (technology->pending_reply != NULL) {
1189                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1190                 dbus_message_unref(technology->pending_reply);
1191                 g_source_remove(technology->pending_timeout);
1192                 technology->pending_reply = NULL;
1193                 technology->pending_timeout = 0;
1194         }
1195
1196         return 0;
1197 }
1198
1199 int __connman_technology_disabled(enum connman_service_type type)
1200 {
1201         struct connman_technology *technology;
1202
1203         technology = technology_find(type);
1204         if (technology == NULL)
1205                 return -ENXIO;
1206
1207         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1208                 return -EINPROGRESS;
1209
1210         if (technology->pending_reply != NULL) {
1211                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1212                 dbus_message_unref(technology->pending_reply);
1213                 g_source_remove(technology->pending_timeout);
1214                 technology->pending_reply = NULL;
1215                 technology->pending_timeout = 0;
1216         }
1217
1218         powered_changed(technology);
1219
1220         return 0;
1221 }
1222
1223 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1224 {
1225         GSList *list;
1226         int err = -EINVAL;
1227
1228         if (global_offlinemode == offlinemode)
1229                 return 0;
1230
1231         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1232
1233         /*
1234          * This is a bit tricky. When you set offlinemode, there is no
1235          * way to differentiate between attempting offline mode and
1236          * resuming offlinemode from last saved profile. We need that
1237          * information in rfkill_update, otherwise it falls back on the
1238          * technology's persistent state. Hence we set the offline mode here
1239          * but save it & call the notifier only if its successful.
1240          */
1241
1242         global_offlinemode = offlinemode;
1243
1244         /* Traverse technology list, enable/disable each technology. */
1245         for (list = technology_list; list; list = list->next) {
1246                 struct connman_technology *technology = list->data;
1247
1248                 if (offlinemode)
1249                         err = technology_disable(technology, NULL);
1250
1251                 if (!offlinemode && technology->enable_persistent)
1252                         err = technology_enable(technology, NULL);
1253         }
1254
1255         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1256                 connman_technology_save_offlinemode();
1257                 __connman_notifier_offlinemode(offlinemode);
1258         } else
1259                 global_offlinemode = connman_technology_load_offlinemode();
1260
1261         return err;
1262 }
1263
1264 void __connman_technology_set_connected(enum connman_service_type type,
1265                 connman_bool_t connected)
1266 {
1267         struct connman_technology *technology;
1268
1269         technology = technology_find(type);
1270         if (technology == NULL)
1271                 return;
1272
1273         DBG("technology %p connected %d", technology, connected);
1274
1275         technology->connected = connected;
1276
1277         connman_dbus_property_changed_basic(technology->path,
1278                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1279                         DBUS_TYPE_BOOLEAN, &connected);
1280 }
1281
1282 int __connman_technology_add_rfkill(unsigned int index,
1283                                         enum connman_service_type type,
1284                                                 connman_bool_t softblock,
1285                                                 connman_bool_t hardblock)
1286 {
1287         struct connman_technology *technology;
1288         struct connman_rfkill *rfkill;
1289
1290         DBG("index %u type %d soft %u hard %u", index, type,
1291                                                         softblock, hardblock);
1292
1293         rfkill = g_hash_table_lookup(rfkill_list, &index);
1294         if (rfkill != NULL)
1295                 goto done;
1296
1297         rfkill = g_try_new0(struct connman_rfkill, 1);
1298         if (rfkill == NULL)
1299                 return -ENOMEM;
1300
1301         rfkill->index = index;
1302         rfkill->type = type;
1303         rfkill->softblock = softblock;
1304         rfkill->hardblock = hardblock;
1305
1306         g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1307
1308 done:
1309         technology = technology_get(type);
1310         /* If there is no driver for this type, ignore it. */
1311         if (technology == NULL)
1312                 return -ENXIO;
1313
1314         if (hardblock) {
1315                 DBG("%s is switched off.", get_name(type));
1316                 return 0;
1317         }
1318
1319         /*
1320          * If Offline mode is on, we softblock the device if it isnt already.
1321          * If Offline mode is off, we rely on the persistent state of tech.
1322          */
1323         if (global_offlinemode) {
1324                 if (!softblock)
1325                         return __connman_rfkill_block(type, TRUE);
1326         } else {
1327                 if (technology->enable_persistent && softblock)
1328                         return __connman_rfkill_block(type, FALSE);
1329                 /* if technology persistent state is offline */
1330                 if (!technology->enable_persistent && !softblock)
1331                         return __connman_rfkill_block(type, TRUE);
1332         }
1333
1334         return 0;
1335 }
1336
1337 int __connman_technology_update_rfkill(unsigned int index,
1338                                         enum connman_service_type type,
1339                                                 connman_bool_t softblock,
1340                                                 connman_bool_t hardblock)
1341 {
1342         struct connman_technology *technology;
1343         struct connman_rfkill *rfkill;
1344
1345         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1346
1347         rfkill = g_hash_table_lookup(rfkill_list, &index);
1348         if (rfkill == NULL)
1349                 return -ENXIO;
1350
1351         if (rfkill->softblock == softblock &&
1352                 rfkill->hardblock == hardblock)
1353                 return 0;
1354
1355         rfkill->softblock = softblock;
1356         rfkill->hardblock = hardblock;
1357
1358         if (hardblock) {
1359                 DBG("%s is switched off.", get_name(type));
1360                 return 0;
1361         }
1362
1363         technology = technology_find(type);
1364         /* If there is no driver for this type, ignore it. */
1365         if (technology == NULL)
1366                 return -ENXIO;
1367
1368         if (!global_offlinemode) {
1369                 if (technology->enable_persistent && softblock)
1370                         return __connman_rfkill_block(type, FALSE);
1371                 if (!technology->enable_persistent && !softblock)
1372                         return __connman_rfkill_block(type, TRUE);
1373         }
1374
1375         return 0;
1376 }
1377
1378 int __connman_technology_remove_rfkill(unsigned int index,
1379                                         enum connman_service_type type)
1380 {
1381         struct connman_technology *technology;
1382         struct connman_rfkill *rfkill;
1383
1384         DBG("index %u", index);
1385
1386         rfkill = g_hash_table_lookup(rfkill_list, &index);
1387         if (rfkill == NULL)
1388                 return -ENXIO;
1389
1390         g_hash_table_remove(rfkill_list, &index);
1391
1392         technology = technology_find(type);
1393         if (technology == NULL)
1394                 return -ENXIO;
1395
1396         technology_put(technology);
1397
1398         return 0;
1399 }
1400
1401 int __connman_technology_init(void)
1402 {
1403         DBG("");
1404
1405         connection = connman_dbus_get_connection();
1406
1407         rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1408                                                         NULL, free_rfkill);
1409
1410         global_offlinemode = connman_technology_load_offlinemode();
1411
1412         /* This will create settings file if it is missing */
1413         connman_technology_save_offlinemode();
1414
1415         return 0;
1416 }
1417
1418 void __connman_technology_cleanup(void)
1419 {
1420         DBG("");
1421
1422         g_hash_table_destroy(rfkill_list);
1423
1424         dbus_connection_unref(connection);
1425 }