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