technology: Soft block on offline mode or disabled technology
[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         connman_bool_t 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         connman_bool_t rfkill_driven;
77         connman_bool_t softblocked;
78         connman_bool_t hardblocked;
79         connman_bool_t dbus_registered;
80 };
81
82 static GSList *driver_list = NULL;
83
84 static gint compare_priority(gconstpointer a, gconstpointer b)
85 {
86         const struct connman_technology_driver *driver1 = a;
87         const struct connman_technology_driver *driver2 = b;
88
89         return driver2->priority - driver1->priority;
90 }
91
92 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
93 {
94         struct connman_rfkill *rfkill = value;
95         enum connman_service_type type = GPOINTER_TO_INT(user_data);
96
97         /* Calling _technology_rfkill_add will update the tech. */
98         if (rfkill->type == type)
99                 __connman_technology_add_rfkill(rfkill->index, type,
100                                 rfkill->softblock, rfkill->hardblock);
101 }
102
103 /**
104  * connman_technology_driver_register:
105  * @driver: Technology driver definition
106  *
107  * Register a new technology driver
108  *
109  * Returns: %0 on success
110  */
111 int connman_technology_driver_register(struct connman_technology_driver *driver)
112 {
113         GSList *list;
114         struct connman_device *device;
115         enum connman_service_type type;
116
117         DBG("Registering %s driver", driver->name);
118
119         driver_list = g_slist_insert_sorted(driver_list, driver,
120                                                         compare_priority);
121
122         if (techless_device_list == NULL)
123                 goto check_rfkill;
124
125         /*
126          * Check for technology less devices if this driver
127          * can service any of them.
128         */
129         for (list = techless_device_list; list; list = list->next) {
130                 device = list->data;
131
132                 type = __connman_device_get_service_type(device);
133                 if (type != driver->type)
134                         continue;
135
136                 techless_device_list = g_slist_remove(techless_device_list,
137                                                                 device);
138
139                 __connman_technology_add_device(device);
140         }
141
142 check_rfkill:
143         /* Check for orphaned rfkill switches. */
144         g_hash_table_foreach(rfkill_list, rfkill_check,
145                                         GINT_TO_POINTER(driver->type));
146
147         return 0;
148 }
149
150 /**
151  * connman_technology_driver_unregister:
152  * @driver: Technology driver definition
153  *
154  * Remove a previously registered technology driver
155  */
156 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
157 {
158         GSList *list;
159         struct connman_technology *technology;
160
161         DBG("Unregistering driver %p name %s", driver, driver->name);
162
163         for (list = technology_list; list; list = list->next) {
164                 technology = list->data;
165
166                 if (technology->driver == NULL)
167                         continue;
168
169                 if (technology->type == driver->type) {
170                         technology->driver->remove(technology);
171                         technology->driver = NULL;
172                 }
173         }
174
175         driver_list = g_slist_remove(driver_list, driver);
176 }
177
178 static void tethering_changed(struct connman_technology *technology)
179 {
180         connman_bool_t tethering = technology->tethering;
181
182         connman_dbus_property_changed_basic(technology->path,
183                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
184                                                 DBUS_TYPE_BOOLEAN, &tethering);
185 }
186
187 void connman_technology_tethering_notify(struct connman_technology *technology,
188                                                         connman_bool_t enabled)
189 {
190         GSList *list;
191
192         DBG("technology %p enabled %u", technology, enabled);
193
194         if (technology->tethering == enabled)
195                 return;
196
197         technology->tethering = enabled;
198
199         tethering_changed(technology);
200
201         if (enabled == TRUE)
202                 __connman_tethering_set_enabled();
203         else {
204                 for (list = technology_list; list; list = list->next) {
205                         struct connman_technology *other_tech = list->data;
206                         if (other_tech->tethering == TRUE)
207                                 break;
208                 }
209                 if (list == NULL)
210                         __connman_tethering_set_disabled();
211         }
212 }
213
214 static int set_tethering(struct connman_technology *technology,
215                                 connman_bool_t enabled)
216 {
217         const char *ident, *passphrase, *bridge;
218
219         ident = technology->tethering_ident;
220         passphrase = technology->tethering_passphrase;
221
222         if (technology->driver == NULL ||
223                         technology->driver->set_tethering == NULL)
224                 return -EOPNOTSUPP;
225
226         __sync_synchronize();
227         if (technology->enabled == FALSE)
228                 return -EACCES;
229
230         bridge = __connman_tethering_get_bridge();
231         if (bridge == NULL)
232                 return -EOPNOTSUPP;
233
234         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
235             (ident == NULL || passphrase == NULL))
236                 return -EINVAL;
237
238         return technology->driver->set_tethering(technology, ident, passphrase,
239                                                         bridge, enabled);
240 }
241
242 void connman_technology_regdom_notify(struct connman_technology *technology,
243                                                         const char *alpha2)
244 {
245         DBG("");
246
247         if (alpha2 == NULL)
248                 connman_error("Failed to set regulatory domain");
249         else
250                 DBG("Regulatory domain set to %s", alpha2);
251
252         g_free(technology->regdom);
253         technology->regdom = g_strdup(alpha2);
254 }
255
256 static int set_regdom_by_device(struct connman_technology *technology,
257                                                         const char *alpha2)
258 {
259         GSList *list;
260
261         for (list = technology->device_list; list; list = list->next) {
262                 struct connman_device *device = list->data;
263
264                 if (connman_device_set_regdom(device, alpha2) != 0)
265                         return -ENOTSUP;
266         }
267
268         return 0;
269 }
270
271 int connman_technology_set_regdom(const char *alpha2)
272 {
273         GSList *list;
274
275         for (list = technology_list; list; list = list->next) {
276                 struct connman_technology *technology = list->data;
277
278                 if (set_regdom_by_device(technology, alpha2) != 0) {
279                         if (technology->driver == NULL)
280                                 continue;
281
282                         if (technology->driver->set_regdom != NULL)
283                                 technology->driver->set_regdom(technology,
284                                                                 alpha2);
285                 }
286         }
287
288         return 0;
289 }
290
291 static void free_rfkill(gpointer data)
292 {
293         struct connman_rfkill *rfkill = data;
294
295         g_free(rfkill);
296 }
297
298 static const char *get_name(enum connman_service_type type)
299 {
300         switch (type) {
301         case CONNMAN_SERVICE_TYPE_UNKNOWN:
302         case CONNMAN_SERVICE_TYPE_SYSTEM:
303         case CONNMAN_SERVICE_TYPE_GPS:
304         case CONNMAN_SERVICE_TYPE_VPN:
305         case CONNMAN_SERVICE_TYPE_GADGET:
306                 break;
307         case CONNMAN_SERVICE_TYPE_ETHERNET:
308                 return "Wired";
309         case CONNMAN_SERVICE_TYPE_WIFI:
310                 return "WiFi";
311         case CONNMAN_SERVICE_TYPE_WIMAX:
312                 return "WiMAX";
313         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
314                 return "Bluetooth";
315         case CONNMAN_SERVICE_TYPE_CELLULAR:
316                 return "Cellular";
317         }
318
319         return NULL;
320 }
321
322 static void technology_save(struct connman_technology *technology)
323 {
324         GKeyFile *keyfile;
325         gchar *identifier;
326
327         DBG("technology %p", technology);
328
329         keyfile = __connman_storage_load_global();
330         if (keyfile == NULL)
331                 keyfile = g_key_file_new();
332
333         identifier = g_strdup_printf("%s", get_name(technology->type));
334         if (identifier == NULL)
335                 goto done;
336
337         g_key_file_set_boolean(keyfile, identifier, "Enable",
338                                 technology->enable_persistent);
339
340         if (technology->tethering_ident != NULL)
341                 g_key_file_set_string(keyfile, identifier,
342                                         "Tethering.Identifier",
343                                         technology->tethering_ident);
344
345         if (technology->tethering_passphrase != NULL)
346                 g_key_file_set_string(keyfile, identifier,
347                                         "Tethering.Passphrase",
348                                         technology->tethering_passphrase);
349
350 done:
351         g_free(identifier);
352
353         __connman_storage_save_global(keyfile);
354
355         g_key_file_free(keyfile);
356
357         return;
358 }
359
360 static void technology_load(struct connman_technology *technology)
361 {
362         GKeyFile *keyfile;
363         gchar *identifier;
364         GError *error = NULL;
365         connman_bool_t enable;
366
367         DBG("technology %p", technology);
368
369         keyfile = __connman_storage_load_global();
370         /* Fallback on disabling technology if file not found. */
371         if (keyfile == NULL) {
372                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
373                         /* We enable ethernet by default */
374                         technology->enable_persistent = TRUE;
375                 else
376                         technology->enable_persistent = FALSE;
377                 return;
378         }
379
380         identifier = g_strdup_printf("%s", get_name(technology->type));
381         if (identifier == NULL)
382                 goto done;
383
384         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
385         if (error == NULL)
386                 technology->enable_persistent = enable;
387         else {
388                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
389                         technology->enable_persistent = TRUE;
390                 else
391                         technology->enable_persistent = FALSE;
392
393                 technology_save(technology);
394                 g_clear_error(&error);
395         }
396
397         technology->tethering_ident = g_key_file_get_string(keyfile,
398                                 identifier, "Tethering.Identifier", NULL);
399
400         technology->tethering_passphrase = g_key_file_get_string(keyfile,
401                                 identifier, "Tethering.Passphrase", NULL);
402 done:
403         g_free(identifier);
404
405         g_key_file_free(keyfile);
406
407         return;
408 }
409
410 connman_bool_t __connman_technology_get_offlinemode(void)
411 {
412         return global_offlinemode;
413 }
414
415 static void connman_technology_save_offlinemode()
416 {
417         GKeyFile *keyfile;
418
419         keyfile = __connman_storage_load_global();
420         if (keyfile == NULL)
421                 keyfile = g_key_file_new();
422
423         g_key_file_set_boolean(keyfile, "global",
424                                         "OfflineMode", global_offlinemode);
425
426         __connman_storage_save_global(keyfile);
427
428         g_key_file_free(keyfile);
429
430         return;
431 }
432
433 static connman_bool_t connman_technology_load_offlinemode()
434 {
435         GKeyFile *keyfile;
436         GError *error = NULL;
437         connman_bool_t offlinemode;
438
439         /* If there is a error, we enable offlinemode */
440         keyfile = __connman_storage_load_global();
441         if (keyfile == NULL)
442                 return FALSE;
443
444         offlinemode = g_key_file_get_boolean(keyfile, "global",
445                                                 "OfflineMode", &error);
446         if (error != NULL) {
447                 offlinemode = FALSE;
448                 g_clear_error(&error);
449         }
450
451         g_key_file_free(keyfile);
452
453         return offlinemode;
454 }
455
456 static void append_properties(DBusMessageIter *iter,
457                 struct connman_technology *technology)
458 {
459         DBusMessageIter dict;
460         const char *str;
461
462         connman_dbus_dict_open(iter, &dict);
463
464         str = get_name(technology->type);
465         if (str != NULL)
466                 connman_dbus_dict_append_basic(&dict, "Name",
467                                                 DBUS_TYPE_STRING, &str);
468
469         str = __connman_service_type2string(technology->type);
470         if (str != NULL)
471                 connman_dbus_dict_append_basic(&dict, "Type",
472                                                 DBUS_TYPE_STRING, &str);
473
474         __sync_synchronize();
475         connman_dbus_dict_append_basic(&dict, "Powered",
476                                         DBUS_TYPE_BOOLEAN,
477                                         &technology->enabled);
478
479         connman_dbus_dict_append_basic(&dict, "Connected",
480                                         DBUS_TYPE_BOOLEAN,
481                                         &technology->connected);
482
483         connman_dbus_dict_append_basic(&dict, "Tethering",
484                                         DBUS_TYPE_BOOLEAN,
485                                         &technology->tethering);
486
487         if (technology->tethering_ident != NULL)
488                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
489                                         DBUS_TYPE_STRING,
490                                         &technology->tethering_ident);
491
492         if (technology->tethering_passphrase != NULL)
493                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
494                                         DBUS_TYPE_STRING,
495                                         &technology->tethering_passphrase);
496
497         connman_dbus_dict_close(iter, &dict);
498 }
499
500 static void technology_added_signal(struct connman_technology *technology)
501 {
502         DBusMessage *signal;
503         DBusMessageIter iter;
504
505         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
506                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
507         if (signal == NULL)
508                 return;
509
510         dbus_message_iter_init_append(signal, &iter);
511         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
512                                                         &technology->path);
513         append_properties(&iter, technology);
514
515         dbus_connection_send(connection, signal, NULL);
516         dbus_message_unref(signal);
517 }
518
519 static void technology_removed_signal(struct connman_technology *technology)
520 {
521         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
522                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
523                         DBUS_TYPE_OBJECT_PATH, &technology->path,
524                         DBUS_TYPE_INVALID);
525 }
526
527 static DBusMessage *get_properties(DBusConnection *conn,
528                                         DBusMessage *message, void *user_data)
529 {
530         struct connman_technology *technology = user_data;
531         DBusMessage *reply;
532         DBusMessageIter iter;
533
534         reply = dbus_message_new_method_return(message);
535         if (reply == NULL)
536                 return NULL;
537
538         dbus_message_iter_init_append(reply, &iter);
539         append_properties(&iter, technology);
540
541         return reply;
542 }
543
544 void __connman_technology_list_struct(DBusMessageIter *array)
545 {
546         GSList *list;
547         DBusMessageIter entry;
548
549         for (list = technology_list; list; list = list->next) {
550                 struct connman_technology *technology = list->data;
551
552                 if (technology->path == NULL ||
553                                 (technology->rfkill_driven == TRUE &&
554                                  technology->hardblocked == TRUE))
555                         continue;
556
557                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
558                                 NULL, &entry);
559                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
560                                 &technology->path);
561                 append_properties(&entry, technology);
562                 dbus_message_iter_close_container(array, &entry);
563         }
564 }
565
566 static gboolean technology_pending_reply(gpointer user_data)
567 {
568         struct connman_technology *technology = user_data;
569         DBusMessage *reply;
570
571         /* Power request timedout, send ETIMEDOUT. */
572         if (technology->pending_reply != NULL) {
573                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
574                 if (reply != NULL)
575                         g_dbus_send_message(connection, reply);
576
577                 dbus_message_unref(technology->pending_reply);
578                 technology->pending_reply = NULL;
579                 technology->pending_timeout = 0;
580         }
581
582         return FALSE;
583 }
584
585 static int technology_affect_devices(struct connman_technology *technology,
586                                                 connman_bool_t enable_device)
587 {
588         GSList *list;
589         int err = 0;
590
591         for (list = technology->device_list; list; list = list->next) {
592                 struct connman_device *device = list->data;
593
594                 if (enable_device == TRUE)
595                         err = __connman_device_enable(device);
596                 else
597                         err = __connman_device_disable(device);
598         }
599
600         return err;
601 }
602
603 static int technology_enable(struct connman_technology *technology)
604 {
605         DBG("technology %p enable", technology);
606
607         __sync_synchronize();
608         if (technology->enabled == TRUE)
609                 return -EALREADY;
610
611         if (technology->pending_reply != NULL)
612                 return -EBUSY;
613
614         if (technology->rfkill_driven == TRUE)
615                 return __connman_rfkill_block(technology->type, FALSE);
616
617         return technology_affect_devices(technology, TRUE);
618 }
619
620 static int technology_disable(struct connman_technology *technology)
621 {
622         DBG("technology %p disable", technology);
623
624         __sync_synchronize();
625         if (technology->enabled == FALSE)
626                 return -EALREADY;
627
628         if (technology->pending_reply != NULL)
629                 return -EBUSY;
630
631         if (technology->tethering == TRUE)
632                 set_tethering(technology, FALSE);
633
634         if (technology->rfkill_driven == TRUE)
635                 return __connman_rfkill_block(technology->type, TRUE);
636
637         return technology_affect_devices(technology, FALSE);
638 }
639
640 static DBusMessage *set_powered(struct connman_technology *technology,
641                                 DBusMessage *msg, connman_bool_t powered)
642 {
643         DBusMessage *reply = NULL;
644         int err = 0;
645
646         if (technology->rfkill_driven && technology->hardblocked == TRUE) {
647                 err = -EACCES;
648                 goto make_reply;
649         }
650
651         if (powered == TRUE)
652                 err = technology_enable(technology);
653         else
654                 err = technology_disable(technology);
655
656         if (err != -EBUSY) {
657                 technology->enable_persistent = powered;
658                 technology_save(technology);
659         }
660
661 make_reply:
662         if (err == -EINPROGRESS) {
663                 technology->pending_reply = dbus_message_ref(msg);
664                 technology->pending_timeout = g_timeout_add_seconds(10,
665                                         technology_pending_reply, technology);
666         } else if (err == -EALREADY) {
667                 if (powered == TRUE)
668                         reply = __connman_error_already_enabled(msg);
669                 else
670                         reply = __connman_error_already_disabled(msg);
671         } else if (err < 0)
672                 reply = __connman_error_failed(msg, -err);
673         else
674                 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
675
676         return reply;
677 }
678
679 static DBusMessage *set_property(DBusConnection *conn,
680                                         DBusMessage *msg, void *data)
681 {
682         struct connman_technology *technology = data;
683         DBusMessageIter iter, value;
684         const char *name;
685         int type;
686
687         DBG("conn %p", conn);
688
689         if (dbus_message_iter_init(msg, &iter) == FALSE)
690                 return __connman_error_invalid_arguments(msg);
691
692         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
693                 return __connman_error_invalid_arguments(msg);
694
695         dbus_message_iter_get_basic(&iter, &name);
696         dbus_message_iter_next(&iter);
697
698         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
699                 return __connman_error_invalid_arguments(msg);
700
701         dbus_message_iter_recurse(&iter, &value);
702
703         type = dbus_message_iter_get_arg_type(&value);
704
705         DBG("property %s", name);
706
707         if (g_str_equal(name, "Tethering") == TRUE) {
708                 int err;
709                 connman_bool_t tethering;
710
711                 if (type != DBUS_TYPE_BOOLEAN)
712                         return __connman_error_invalid_arguments(msg);
713
714                 dbus_message_iter_get_basic(&value, &tethering);
715
716                 if (technology->tethering == tethering) {
717                         if (tethering == FALSE)
718                                 return __connman_error_already_disabled(msg);
719                         else
720                                 return __connman_error_already_enabled(msg);
721                 }
722
723                 err = set_tethering(technology, tethering);
724                 if (err < 0)
725                         return __connman_error_failed(msg, -err);
726
727         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
728                 const char *str;
729
730                 dbus_message_iter_get_basic(&value, &str);
731
732                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
733                         return __connman_error_not_supported(msg);
734
735                 if (strlen(str) < 1 || strlen(str) > 32)
736                         return __connman_error_invalid_arguments(msg);
737
738                 if (g_strcmp0(technology->tethering_ident, str) != 0) {
739                         g_free(technology->tethering_ident);
740                         technology->tethering_ident = g_strdup(str);
741                         technology_save(technology);
742
743                         connman_dbus_property_changed_basic(technology->path,
744                                                 CONNMAN_TECHNOLOGY_INTERFACE,
745                                                 "TetheringIdentifier",
746                                                 DBUS_TYPE_STRING,
747                                                 &technology->tethering_ident);
748                 }
749         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
750                 const char *str;
751
752                 dbus_message_iter_get_basic(&value, &str);
753
754                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
755                         return __connman_error_not_supported(msg);
756
757                 if (strlen(str) < 8 || strlen(str) > 63)
758                         return __connman_error_passphrase_required(msg);
759
760                 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
761                         g_free(technology->tethering_passphrase);
762                         technology->tethering_passphrase = g_strdup(str);
763                         technology_save(technology);
764
765                         connman_dbus_property_changed_basic(technology->path,
766                                         CONNMAN_TECHNOLOGY_INTERFACE,
767                                         "TetheringPassphrase",
768                                         DBUS_TYPE_STRING,
769                                         &technology->tethering_passphrase);
770                 }
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
779                 return set_powered(technology, msg, enable);
780         } else
781                 return __connman_error_invalid_property(msg);
782
783         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
784 }
785
786 static struct connman_technology *technology_find(enum connman_service_type type)
787 {
788         GSList *list;
789
790         DBG("type %d", type);
791
792         for (list = technology_list; list; list = list->next) {
793                 struct connman_technology *technology = list->data;
794
795                 if (technology->type == type)
796                         return technology;
797         }
798
799         return NULL;
800 }
801
802 static void reply_scan_pending(struct connman_technology *technology, int err)
803 {
804         DBusMessage *reply;
805
806         DBG("technology %p err %d", technology, err);
807
808         while (technology->scan_pending != NULL) {
809                 DBusMessage *msg = technology->scan_pending->data;
810
811                 DBG("reply to %s", dbus_message_get_sender(msg));
812
813                 if (err == 0)
814                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
815                 else
816                         reply = __connman_error_failed(msg, -err);
817                 g_dbus_send_message(connection, reply);
818                 dbus_message_unref(msg);
819
820                 technology->scan_pending =
821                         g_slist_delete_link(technology->scan_pending,
822                                         technology->scan_pending);
823         }
824 }
825
826 void __connman_technology_scan_started(struct connman_device *device)
827 {
828         DBG("device %p", device);
829 }
830
831 void __connman_technology_scan_stopped(struct connman_device *device)
832 {
833         int count = 0;
834         struct connman_technology *technology;
835         enum connman_service_type type;
836         GSList *list;
837
838         type = __connman_device_get_service_type(device);
839         technology = technology_find(type);
840
841         DBG("technology %p device %p", technology, device);
842
843         if (technology == NULL)
844                 return;
845
846         for (list = technology->device_list; list != NULL; list = list->next) {
847                 struct connman_device *other_device = list->data;
848
849                 if (device == other_device)
850                         continue;
851
852                 if (__connman_device_get_service_type(other_device) != type)
853                         continue;
854
855                 if (connman_device_get_scanning(other_device) == TRUE)
856                         count += 1;
857         }
858
859         if (count == 0)
860                 reply_scan_pending(technology, 0);
861 }
862
863 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
864                                                 int result, const char *alpha2)
865 {
866         struct connman_technology *technology;
867         enum connman_service_type type;
868
869         type = __connman_device_get_service_type(device);
870         technology = technology_find(type);
871
872         if (technology == NULL)
873                 return;
874
875         if (result < 0) {
876                 if (technology->driver != NULL &&
877                                 technology->driver->set_regdom != NULL) {
878                         technology->driver->set_regdom(technology, alpha2);
879                         return;
880                 }
881
882                 alpha2 = NULL;
883         }
884
885         connman_technology_regdom_notify(technology, alpha2);
886 }
887
888 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
889 {
890         struct connman_technology *technology = data;
891         int err;
892
893         DBG ("technology %p request from %s", technology,
894                         dbus_message_get_sender(msg));
895
896         dbus_message_ref(msg);
897         technology->scan_pending =
898                 g_slist_prepend(technology->scan_pending, msg);
899
900         err = __connman_device_request_scan(technology->type);
901         if (err < 0)
902                 reply_scan_pending(technology, err);
903
904         return NULL;
905 }
906
907 static const GDBusMethodTable technology_methods[] = {
908         { GDBUS_DEPRECATED_METHOD("GetProperties",
909                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
910                         get_properties) },
911         { GDBUS_ASYNC_METHOD("SetProperty",
912                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
913                         NULL, set_property) },
914         { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
915         { },
916 };
917
918 static const GDBusSignalTable technology_signals[] = {
919         { GDBUS_SIGNAL("PropertyChanged",
920                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
921         { },
922 };
923
924 static gboolean technology_dbus_register(struct connman_technology *technology)
925 {
926         if (technology->dbus_registered == TRUE ||
927                                 (technology->rfkill_driven == TRUE &&
928                                  technology->hardblocked == TRUE))
929                 return TRUE;
930
931         if (g_dbus_register_interface(connection, technology->path,
932                                 CONNMAN_TECHNOLOGY_INTERFACE,
933                                 technology_methods, technology_signals,
934                                 NULL, technology, NULL) == FALSE) {
935                 connman_error("Failed to register %s", technology->path);
936                 return FALSE;
937         }
938
939         technology_added_signal(technology);
940         technology->dbus_registered = TRUE;
941
942         return TRUE;
943 }
944
945 static struct connman_technology *technology_get(enum connman_service_type type)
946 {
947         struct connman_technology *technology;
948         struct connman_technology_driver *driver = NULL;
949         const char *str;
950         GSList *list;
951         int err;
952
953         DBG("type %d", type);
954
955         str = __connman_service_type2string(type);
956         if (str == NULL)
957                 return NULL;
958
959         technology = technology_find(type);
960         if (technology != NULL) {
961                 __sync_fetch_and_add(&technology->refcount, 1);
962                 return technology;
963         }
964
965         /* First check if we have a driver for this technology type */
966         for (list = driver_list; list; list = list->next) {
967                 driver = list->data;
968
969                 if (driver->type == type)
970                         break;
971                 else
972                         driver = NULL;
973         }
974
975         if (driver == NULL) {
976                 DBG("No matching driver found for %s.",
977                                 __connman_service_type2string(type));
978                 return NULL;
979         }
980
981         technology = g_try_new0(struct connman_technology, 1);
982         if (technology == NULL)
983                 return NULL;
984
985         technology->refcount = 1;
986
987         technology->rfkill_driven = FALSE;
988         technology->softblocked = FALSE;
989         technology->hardblocked = FALSE;
990
991         technology->type = type;
992         technology->path = g_strdup_printf("%s/technology/%s",
993                                                         CONNMAN_PATH, str);
994
995         technology->device_list = NULL;
996
997         technology->pending_reply = NULL;
998
999         technology_load(technology);
1000
1001         if (technology_dbus_register(technology) == FALSE) {
1002                 g_free(technology);
1003                 return NULL;
1004         }
1005
1006         technology_list = g_slist_prepend(technology_list, technology);
1007
1008         technology->driver = driver;
1009         err = driver->probe(technology);
1010         if (err != 0)
1011                 DBG("Driver probe failed for technology %p", technology);
1012
1013         DBG("technology %p", technology);
1014
1015         return technology;
1016 }
1017
1018 static void technology_dbus_unregister(struct connman_technology *technology)
1019 {
1020         if (technology->dbus_registered == FALSE)
1021                 return;
1022
1023         technology_removed_signal(technology);
1024         g_dbus_unregister_interface(connection, technology->path,
1025                 CONNMAN_TECHNOLOGY_INTERFACE);
1026
1027         technology->dbus_registered = FALSE;
1028 }
1029
1030 static void technology_put(struct connman_technology *technology)
1031 {
1032         DBG("technology %p", technology);
1033
1034         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1035                 return;
1036
1037         reply_scan_pending(technology, -EINTR);
1038
1039         if (technology->driver) {
1040                 technology->driver->remove(technology);
1041                 technology->driver = NULL;
1042         }
1043
1044         technology_list = g_slist_remove(technology_list, technology);
1045
1046         technology_dbus_unregister(technology);
1047
1048         g_slist_free(technology->device_list);
1049
1050         g_free(technology->path);
1051         g_free(technology->regdom);
1052         g_free(technology->tethering_ident);
1053         g_free(technology->tethering_passphrase);
1054         g_free(technology);
1055 }
1056
1057 void __connman_technology_add_interface(enum connman_service_type type,
1058                                 int index, const char *name, const char *ident)
1059 {
1060         struct connman_technology *technology;
1061
1062         switch (type) {
1063         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1064         case CONNMAN_SERVICE_TYPE_SYSTEM:
1065                 return;
1066         case CONNMAN_SERVICE_TYPE_ETHERNET:
1067         case CONNMAN_SERVICE_TYPE_WIFI:
1068         case CONNMAN_SERVICE_TYPE_WIMAX:
1069         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1070         case CONNMAN_SERVICE_TYPE_CELLULAR:
1071         case CONNMAN_SERVICE_TYPE_GPS:
1072         case CONNMAN_SERVICE_TYPE_VPN:
1073         case CONNMAN_SERVICE_TYPE_GADGET:
1074                 break;
1075         }
1076
1077         connman_info("Adding interface %s [ %s ]", name,
1078                                 __connman_service_type2string(type));
1079
1080         technology = technology_find(type);
1081
1082         if (technology == NULL || technology->driver == NULL
1083                         || technology->driver->add_interface == NULL)
1084                 return;
1085
1086         technology->driver->add_interface(technology,
1087                                         index, name, ident);
1088 }
1089
1090 void __connman_technology_remove_interface(enum connman_service_type type,
1091                                 int index, const char *name, const char *ident)
1092 {
1093         struct connman_technology *technology;
1094
1095         switch (type) {
1096         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1097         case CONNMAN_SERVICE_TYPE_SYSTEM:
1098                 return;
1099         case CONNMAN_SERVICE_TYPE_ETHERNET:
1100         case CONNMAN_SERVICE_TYPE_WIFI:
1101         case CONNMAN_SERVICE_TYPE_WIMAX:
1102         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1103         case CONNMAN_SERVICE_TYPE_CELLULAR:
1104         case CONNMAN_SERVICE_TYPE_GPS:
1105         case CONNMAN_SERVICE_TYPE_VPN:
1106         case CONNMAN_SERVICE_TYPE_GADGET:
1107                 break;
1108         }
1109
1110         connman_info("Remove interface %s [ %s ]", name,
1111                                 __connman_service_type2string(type));
1112
1113         technology = technology_find(type);
1114
1115         if (technology == NULL || technology->driver == NULL)
1116                 return;
1117
1118         if (technology->driver->remove_interface)
1119                 technology->driver->remove_interface(technology, index);
1120 }
1121
1122 int __connman_technology_add_device(struct connman_device *device)
1123 {
1124         struct connman_technology *technology;
1125         enum connman_service_type type;
1126
1127         DBG("device %p", device);
1128
1129         type = __connman_device_get_service_type(device);
1130
1131         technology = technology_get(type);
1132         if (technology == NULL) {
1133                 /*
1134                  * Since no driver can be found for this device at the moment we
1135                  * add it to the techless device list.
1136                 */
1137                 techless_device_list = g_slist_prepend(techless_device_list,
1138                                                                 device);
1139
1140                 return -ENXIO;
1141         }
1142
1143         __sync_synchronize();
1144         if (technology->rfkill_driven == TRUE) {
1145                 if (technology->enabled == TRUE)
1146                         __connman_device_enable(device);
1147                 else
1148                         __connman_device_disable(device);
1149
1150                 goto done;
1151         }
1152
1153         if (technology->enable_persistent == TRUE &&
1154                                         global_offlinemode == FALSE) {
1155                 int err = __connman_device_enable(device);
1156                 /*
1157                  * connman_technology_add_device() calls __connman_device_enable()
1158                  * but since the device is already enabled, the calls does not
1159                  * propagate through to connman_technology_enabled via
1160                  * connman_device_set_powered.
1161                  */
1162                 if (err == -EALREADY)
1163                         __connman_technology_enabled(type);
1164         }
1165         /* if technology persistent state is offline */
1166         if (technology->enable_persistent == FALSE)
1167                 __connman_device_disable(device);
1168
1169 done:
1170         technology->device_list = g_slist_prepend(technology->device_list,
1171                                                                 device);
1172
1173         return 0;
1174 }
1175
1176 int __connman_technology_remove_device(struct connman_device *device)
1177 {
1178         struct connman_technology *technology;
1179         enum connman_service_type type;
1180
1181         DBG("device %p", device);
1182
1183         type = __connman_device_get_service_type(device);
1184
1185         technology = technology_find(type);
1186         if (technology == NULL) {
1187                 techless_device_list = g_slist_remove(techless_device_list,
1188                                                                 device);
1189                 return -ENXIO;
1190         }
1191
1192         technology->device_list = g_slist_remove(technology->device_list,
1193                                                                 device);
1194         technology_put(technology);
1195
1196         return 0;
1197 }
1198
1199 static void powered_changed(struct connman_technology *technology)
1200 {
1201         if (technology->dbus_registered == FALSE)
1202                 return;
1203
1204         if (technology->pending_reply != NULL) {
1205                 g_dbus_send_reply(connection,
1206                                 technology->pending_reply, DBUS_TYPE_INVALID);
1207                 dbus_message_unref(technology->pending_reply);
1208                 technology->pending_reply = NULL;
1209
1210                 g_source_remove(technology->pending_timeout);
1211                 technology->pending_timeout = 0;
1212         }
1213
1214         __sync_synchronize();
1215         connman_dbus_property_changed_basic(technology->path,
1216                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1217                         DBUS_TYPE_BOOLEAN, &technology->enabled);
1218 }
1219
1220 static int technology_enabled(struct connman_technology *technology)
1221 {
1222         __sync_synchronize();
1223         if (technology->enabled == TRUE)
1224                 return -EALREADY;
1225
1226         technology->enabled = TRUE;
1227
1228         powered_changed(technology);
1229
1230         return 0;
1231 }
1232
1233 int __connman_technology_enabled(enum connman_service_type type)
1234 {
1235         struct connman_technology *technology;
1236
1237         technology = technology_find(type);
1238         if (technology == NULL)
1239                 return -ENXIO;
1240
1241         if (technology->rfkill_driven == TRUE)
1242                 return 0;
1243
1244         return technology_enabled(technology);
1245 }
1246
1247 static int technology_disabled(struct connman_technology *technology)
1248 {
1249         __sync_synchronize();
1250         if (technology->enabled == FALSE)
1251                 return -EALREADY;
1252
1253         technology->enabled = FALSE;
1254
1255         powered_changed(technology);
1256
1257         return 0;
1258 }
1259
1260 int __connman_technology_disabled(enum connman_service_type type)
1261 {
1262         struct connman_technology *technology;
1263         GSList *list;
1264
1265         technology = technology_find(type);
1266         if (technology == NULL)
1267                 return -ENXIO;
1268
1269         if (technology->rfkill_driven == TRUE)
1270                 return 0;
1271
1272         for (list = technology->device_list; list != NULL; list = list->next) {
1273                 struct connman_device *device = list->data;
1274
1275                 if (connman_device_get_powered(device) == TRUE)
1276                         return 0;
1277         }
1278
1279         return technology_disabled(technology);
1280 }
1281
1282 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1283 {
1284         GSList *list;
1285         int err = -EINVAL;
1286
1287         if (global_offlinemode == offlinemode)
1288                 return 0;
1289
1290         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1291
1292         /*
1293          * This is a bit tricky. When you set offlinemode, there is no
1294          * way to differentiate between attempting offline mode and
1295          * resuming offlinemode from last saved profile. We need that
1296          * information in rfkill_update, otherwise it falls back on the
1297          * technology's persistent state. Hence we set the offline mode here
1298          * but save it & call the notifier only if its successful.
1299          */
1300
1301         global_offlinemode = offlinemode;
1302
1303         /* Traverse technology list, enable/disable each technology. */
1304         for (list = technology_list; list; list = list->next) {
1305                 struct connman_technology *technology = list->data;
1306
1307                 if (offlinemode)
1308                         err = technology_disable(technology);
1309
1310                 if (!offlinemode && technology->enable_persistent)
1311                         err = technology_enable(technology);
1312         }
1313
1314         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1315                 connman_technology_save_offlinemode();
1316                 __connman_notifier_offlinemode(offlinemode);
1317         } else
1318                 global_offlinemode = connman_technology_load_offlinemode();
1319
1320         return err;
1321 }
1322
1323 void __connman_technology_set_connected(enum connman_service_type type,
1324                 connman_bool_t connected)
1325 {
1326         struct connman_technology *technology;
1327
1328         technology = technology_find(type);
1329         if (technology == NULL)
1330                 return;
1331
1332         DBG("technology %p connected %d", technology, connected);
1333
1334         technology->connected = connected;
1335
1336         connman_dbus_property_changed_basic(technology->path,
1337                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1338                         DBUS_TYPE_BOOLEAN, &connected);
1339 }
1340
1341 static connman_bool_t technology_apply_rfkill_change(struct connman_technology *technology,
1342                                                 connman_bool_t softblock,
1343                                                 connman_bool_t hardblock,
1344                                                 connman_bool_t new_rfkill)
1345 {
1346         gboolean hardblock_changed = FALSE;
1347         gboolean apply = TRUE;
1348         GList *start, *list;
1349
1350         DBG("technology %p --> %d/%d vs %d/%d",
1351                         technology, softblock, hardblock,
1352                         technology->softblocked, technology->hardblocked);
1353
1354         if (technology->hardblocked == hardblock)
1355                 goto softblock_change;
1356
1357         if (!(new_rfkill == TRUE && hardblock == FALSE)) {
1358                 start = g_hash_table_get_values(rfkill_list);
1359
1360                 for (list = start; list != NULL; list = list->next) {
1361                         struct connman_rfkill *rfkill = list->data;
1362
1363                         if (rfkill->type != technology->type)
1364                                 continue;
1365
1366                         if (rfkill->hardblock != hardblock)
1367                                 apply = FALSE;
1368                 }
1369
1370                 g_list_free(start);
1371         }
1372
1373         if (apply == FALSE)
1374                 goto softblock_change;
1375
1376         technology->hardblocked = hardblock;
1377         hardblock_changed = TRUE;
1378
1379 softblock_change:
1380         if (apply == FALSE && technology->softblocked != softblock)
1381                 apply = TRUE;
1382
1383         if (apply == FALSE)
1384                 return technology->hardblocked;
1385
1386         technology->softblocked = softblock;
1387
1388         if (technology->hardblocked == TRUE ||
1389                                         technology->softblocked == TRUE) {
1390                 if (technology_disabled(technology) != -EALREADY)
1391                         technology_affect_devices(technology, FALSE);
1392         } else if (technology->hardblocked == FALSE &&
1393                                         technology->softblocked == FALSE) {
1394                 if (technology_enabled(technology) != -EALREADY)
1395                         technology_affect_devices(technology, TRUE);
1396         }
1397
1398         if (hardblock_changed == TRUE) {
1399                 if (technology->hardblocked == TRUE) {
1400                         DBG("%s is switched off.", get_name(technology->type));
1401                         technology_dbus_unregister(technology);
1402                 } else
1403                         technology_dbus_register(technology);
1404         }
1405
1406         return technology->hardblocked;
1407 }
1408
1409 int __connman_technology_add_rfkill(unsigned int index,
1410                                         enum connman_service_type type,
1411                                                 connman_bool_t softblock,
1412                                                 connman_bool_t hardblock)
1413 {
1414         struct connman_technology *technology;
1415         struct connman_rfkill *rfkill;
1416
1417         DBG("index %u type %d soft %u hard %u", index, type,
1418                                                         softblock, hardblock);
1419
1420         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1421         if (rfkill != NULL)
1422                 goto done;
1423
1424         rfkill = g_try_new0(struct connman_rfkill, 1);
1425         if (rfkill == NULL)
1426                 return -ENOMEM;
1427
1428         rfkill->index = index;
1429         rfkill->type = type;
1430         rfkill->softblock = softblock;
1431         rfkill->hardblock = hardblock;
1432
1433         g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1434
1435 done:
1436         technology = technology_get(type);
1437         /* If there is no driver for this type, ignore it. */
1438         if (technology == NULL)
1439                 return -ENXIO;
1440
1441         technology->rfkill_driven = TRUE;
1442
1443         /* If hardblocked, there is no need to handle softblocked state */
1444         if (technology_apply_rfkill_change(technology,
1445                                 softblock, hardblock, TRUE) == TRUE)
1446                 return 0;
1447
1448         /*
1449          * Depending on softblocked state we unblock/block according to
1450          * offlinemode and persistente state.
1451          */
1452         if (technology->softblocked == TRUE &&
1453                                 global_offlinemode == FALSE &&
1454                                 technology->enable_persistent == TRUE)
1455                 return __connman_rfkill_block(type, FALSE);
1456         else if (technology->softblocked == FALSE &&
1457                         (global_offlinemode == TRUE ||
1458                                 technology->enable_persistent == FALSE))
1459                 return __connman_rfkill_block(type, TRUE);
1460
1461         return 0;
1462 }
1463
1464 int __connman_technology_update_rfkill(unsigned int index,
1465                                         enum connman_service_type type,
1466                                                 connman_bool_t softblock,
1467                                                 connman_bool_t hardblock)
1468 {
1469         struct connman_technology *technology;
1470         struct connman_rfkill *rfkill;
1471
1472         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1473
1474         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1475         if (rfkill == NULL)
1476                 return -ENXIO;
1477
1478         if (rfkill->softblock == softblock &&
1479                                 rfkill->hardblock == hardblock)
1480                 return 0;
1481
1482         rfkill->softblock = softblock;
1483         rfkill->hardblock = hardblock;
1484
1485         technology = technology_find(type);
1486         /* If there is no driver for this type, ignore it. */
1487         if (technology == NULL)
1488                 return -ENXIO;
1489
1490         /* If hardblocked, there is no need to handle softblocked state */
1491         if (technology_apply_rfkill_change(technology,
1492                                 softblock, hardblock, FALSE) == TRUE)
1493                 return 0;
1494
1495         if (global_offlinemode == TRUE)
1496                 return 0;
1497
1498         /*
1499          * Depending on softblocked state we unblock/block according to
1500          * persistent state.
1501          */
1502         if (technology->softblocked == TRUE &&
1503                                 technology->enable_persistent == TRUE)
1504                 return __connman_rfkill_block(type, FALSE);
1505         else if (technology->softblocked == FALSE &&
1506                                 technology->enable_persistent == FALSE)
1507                 return __connman_rfkill_block(type, TRUE);
1508
1509         return 0;
1510 }
1511
1512 int __connman_technology_remove_rfkill(unsigned int index,
1513                                         enum connman_service_type type)
1514 {
1515         struct connman_technology *technology;
1516         struct connman_rfkill *rfkill;
1517
1518         DBG("index %u", index);
1519
1520         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1521         if (rfkill == NULL)
1522                 return -ENXIO;
1523
1524         g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1525
1526         technology = technology_find(type);
1527         if (technology == NULL)
1528                 return -ENXIO;
1529
1530         technology_apply_rfkill_change(technology,
1531                 technology->softblocked, !technology->hardblocked, FALSE);
1532
1533         technology_put(technology);
1534
1535         return 0;
1536 }
1537
1538 int __connman_technology_init(void)
1539 {
1540         DBG("");
1541
1542         connection = connman_dbus_get_connection();
1543
1544         rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1545                                                         NULL, free_rfkill);
1546
1547         global_offlinemode = connman_technology_load_offlinemode();
1548
1549         /* This will create settings file if it is missing */
1550         connman_technology_save_offlinemode();
1551
1552         return 0;
1553 }
1554
1555 void __connman_technology_cleanup(void)
1556 {
1557         DBG("");
1558
1559         g_hash_table_destroy(rfkill_list);
1560
1561         dbus_connection_unref(connection);
1562 }