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