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