technology: Block devices based on technology rfkill status
[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 <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusConnection *connection;
31
32 static GHashTable *rfkill_table;
33 static GHashTable *device_table;
34 static GSList *technology_list = NULL;
35
36 struct connman_rfkill {
37         unsigned int index;
38         enum connman_service_type type;
39         connman_bool_t softblock;
40         connman_bool_t hardblock;
41 };
42
43 enum connman_technology_state {
44         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
45         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
46         CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
47         CONNMAN_TECHNOLOGY_STATE_BLOCKED   = 3,
48         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 4,
49         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 5,
50 };
51
52 struct connman_technology {
53         gint refcount;
54         enum connman_service_type type;
55         enum connman_technology_state state;
56         char *path;
57         GHashTable *rfkill_list;
58         GSList *device_list;
59         gint enabled;
60         gint blocked;
61
62         struct connman_technology_driver *driver;
63         void *driver_data;
64 };
65
66 static GSList *driver_list = NULL;
67
68 static gint compare_priority(gconstpointer a, gconstpointer b)
69 {
70         const struct connman_technology_driver *driver1 = a;
71         const struct connman_technology_driver *driver2 = b;
72
73         return driver2->priority - driver1->priority;
74 }
75
76 /**
77  * connman_technology_driver_register:
78  * @driver: Technology driver definition
79  *
80  * Register a new technology driver
81  *
82  * Returns: %0 on success
83  */
84 int connman_technology_driver_register(struct connman_technology_driver *driver)
85 {
86         DBG("driver %p name %s", driver, driver->name);
87
88         driver_list = g_slist_insert_sorted(driver_list, driver,
89                                                         compare_priority);
90
91         return 0;
92 }
93
94 /**
95  * connman_technology_driver_unregister:
96  * @driver: Technology driver definition
97  *
98  * Remove a previously registered technology driver
99  */
100 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
101 {
102         DBG("driver %p name %s", driver, driver->name);
103
104         driver_list = g_slist_remove(driver_list, driver);
105 }
106
107 void __connman_technology_add_interface(enum connman_service_type type,
108                                 int index, const char *name, const char *ident)
109 {
110         GSList *list;
111
112         switch (type) {
113         case CONNMAN_SERVICE_TYPE_UNKNOWN:
114         case CONNMAN_SERVICE_TYPE_SYSTEM:
115                 return;
116         case CONNMAN_SERVICE_TYPE_ETHERNET:
117         case CONNMAN_SERVICE_TYPE_WIFI:
118         case CONNMAN_SERVICE_TYPE_WIMAX:
119         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
120         case CONNMAN_SERVICE_TYPE_CELLULAR:
121         case CONNMAN_SERVICE_TYPE_GPS:
122         case CONNMAN_SERVICE_TYPE_VPN:
123                 break;
124         }
125
126         connman_info("Create interface %s [ %s ]", name,
127                                 __connman_service_type2string(type));
128
129         for (list = technology_list; list; list = list->next) {
130                 struct connman_technology *technology = list->data;
131
132                 if (technology->type != type)
133                         continue;
134
135                 if (technology->driver == NULL)
136                         continue;
137
138                 if (technology->driver->add_interface)
139                         technology->driver->add_interface(technology,
140                                                         index, name, ident);
141         }
142 }
143
144 void __connman_technology_remove_interface(enum connman_service_type type,
145                                 int index, const char *name, const char *ident)
146 {
147         GSList *list;
148
149         switch (type) {
150         case CONNMAN_SERVICE_TYPE_UNKNOWN:
151         case CONNMAN_SERVICE_TYPE_SYSTEM:
152                 return;
153         case CONNMAN_SERVICE_TYPE_ETHERNET:
154         case CONNMAN_SERVICE_TYPE_WIFI:
155         case CONNMAN_SERVICE_TYPE_WIMAX:
156         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
157         case CONNMAN_SERVICE_TYPE_CELLULAR:
158         case CONNMAN_SERVICE_TYPE_GPS:
159         case CONNMAN_SERVICE_TYPE_VPN:
160                 break;
161         }
162
163         connman_info("Remove interface %s [ %s ]", name,
164                                 __connman_service_type2string(type));
165
166         for (list = technology_list; list; list = list->next) {
167                 struct connman_technology *technology = list->data;
168
169                 if (technology->type != type)
170                         continue;
171
172                 if (technology->driver == NULL)
173                         continue;
174
175                 if (technology->driver->remove_interface)
176                         technology->driver->remove_interface(technology, index);
177         }
178 }
179
180 static int set_tethering(connman_bool_t enabled)
181 {
182         GSList *list;
183
184         for (list = technology_list; list; list = list->next) {
185                 struct connman_technology *technology = list->data;
186
187                 if (technology->driver == NULL)
188                         continue;
189
190                 if (technology->driver->set_tethering)
191                         technology->driver->set_tethering(technology, enabled);
192         }
193
194         return 0;
195 }
196
197 int __connman_technology_enable_tethering(void)
198 {
199         return set_tethering(TRUE);
200 }
201
202 int __connman_technology_disable_tethering(void)
203 {
204         return set_tethering(FALSE);
205 }
206
207 static void free_rfkill(gpointer data)
208 {
209         struct connman_rfkill *rfkill = data;
210
211         g_free(rfkill);
212 }
213
214 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
215 {
216         GSList *list;
217
218         for (list = technology_list; list; list = list->next) {
219                 struct connman_technology *technology = list->data;
220
221                 if (technology->path == NULL)
222                         continue;
223
224                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
225                                                         &technology->path);
226         }
227 }
228
229 static void technologies_changed(void)
230 {
231         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
232                         CONNMAN_MANAGER_INTERFACE, "Technologies",
233                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
234 }
235
236 static void device_list(DBusMessageIter *iter, void *user_data)
237 {
238         struct connman_technology *technology = user_data;
239         GSList *list;
240
241         for (list = technology->device_list; list; list = list->next) {
242                 struct connman_device *device = list->data;
243                 const char *path;
244
245                 path = connman_device_get_path(device);
246                 if (path == NULL)
247                         continue;
248
249                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
250                                                                         &path);
251         }
252 }
253
254 static void devices_changed(struct connman_technology *technology)
255 {
256         connman_dbus_property_changed_array(technology->path,
257                         CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
258                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
259 }
260
261 static const char *state2string(enum connman_technology_state state)
262 {
263         switch (state) {
264         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
265                 break;
266         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
267                 return "offline";
268         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
269                 return "available";
270         case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
271                 return "blocked";
272         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
273                 return "enabled";
274         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
275                 return "connected";
276         }
277
278         return NULL;
279 }
280
281 static void state_changed(struct connman_technology *technology)
282 {
283         const char *str;
284
285         str = state2string(technology->state);
286         if (str == NULL)
287                 return;
288
289         connman_dbus_property_changed_basic(technology->path,
290                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
291                                                 DBUS_TYPE_STRING, &str);
292 }
293
294 static const char *get_name(enum connman_service_type type)
295 {
296         switch (type) {
297         case CONNMAN_SERVICE_TYPE_UNKNOWN:
298         case CONNMAN_SERVICE_TYPE_SYSTEM:
299         case CONNMAN_SERVICE_TYPE_GPS:
300         case CONNMAN_SERVICE_TYPE_VPN:
301                 break;
302         case CONNMAN_SERVICE_TYPE_ETHERNET:
303                 return "Wired";
304         case CONNMAN_SERVICE_TYPE_WIFI:
305                 return "WiFi";
306         case CONNMAN_SERVICE_TYPE_WIMAX:
307                 return "WiMAX";
308         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
309                 return "Bluetooth";
310         case CONNMAN_SERVICE_TYPE_CELLULAR:
311                 return "3G";
312         }
313
314         return NULL;
315 }
316
317 static DBusMessage *get_properties(DBusConnection *conn,
318                                         DBusMessage *message, void *user_data)
319 {
320         struct connman_technology *technology = user_data;
321         DBusMessage *reply;
322         DBusMessageIter array, dict;
323         const char *str;
324
325         reply = dbus_message_new_method_return(message);
326         if (reply == NULL)
327                 return NULL;
328
329         dbus_message_iter_init_append(reply, &array);
330
331         connman_dbus_dict_open(&array, &dict);
332
333         str = state2string(technology->state);
334         if (str != NULL)
335                 connman_dbus_dict_append_basic(&dict, "State",
336                                                 DBUS_TYPE_STRING, &str);
337
338         str = get_name(technology->type);
339         if (str != NULL)
340                 connman_dbus_dict_append_basic(&dict, "Name",
341                                                 DBUS_TYPE_STRING, &str);
342
343         str = __connman_service_type2string(technology->type);
344         if (str != NULL)
345                 connman_dbus_dict_append_basic(&dict, "Type",
346                                                 DBUS_TYPE_STRING, &str);
347
348         connman_dbus_dict_append_array(&dict, "Devices",
349                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
350
351         connman_dbus_dict_close(&array, &dict);
352
353         return reply;
354 }
355
356 static GDBusMethodTable technology_methods[] = {
357         { "GetProperties", "", "a{sv}", get_properties },
358         { },
359 };
360
361 static GDBusSignalTable technology_signals[] = {
362         { "PropertyChanged", "sv" },
363         { },
364 };
365
366 static struct connman_technology *technology_find(enum connman_service_type type)
367 {
368         GSList *list;
369
370         DBG("type %d", type);
371
372         for (list = technology_list; list; list = list->next) {
373                 struct connman_technology *technology = list->data;
374
375                 if (technology->type == type)
376                         return technology;
377         }
378
379         return NULL;
380 }
381
382 static struct connman_technology *technology_get(enum connman_service_type type)
383 {
384         struct connman_technology *technology;
385         const char *str;
386         GSList *list;
387
388         DBG("type %d", type);
389
390         technology = technology_find(type);
391         if (technology != NULL) {
392                 g_atomic_int_inc(&technology->refcount);
393                 goto done;
394         }
395
396         str = __connman_service_type2string(type);
397         if (str == NULL)
398                 return NULL;
399
400         technology = g_try_new0(struct connman_technology, 1);
401         if (technology == NULL)
402                 return NULL;
403
404         technology->refcount = 1;
405
406         technology->type = type;
407         technology->path = g_strdup_printf("%s/technology/%s",
408                                                         CONNMAN_PATH, str);
409
410         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
411                                                         NULL, free_rfkill);
412         technology->device_list = NULL;
413
414         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
415
416         if (g_dbus_register_interface(connection, technology->path,
417                                         CONNMAN_TECHNOLOGY_INTERFACE,
418                                         technology_methods, technology_signals,
419                                         NULL, technology, NULL) == FALSE) {
420                 connman_error("Failed to register %s", technology->path);
421                 g_free(technology);
422                 return NULL;
423         }
424
425         technology_list = g_slist_append(technology_list, technology);
426
427         technologies_changed();
428
429         if (technology->driver != NULL)
430                 goto done;
431
432         for (list = driver_list; list; list = list->next) {
433                 struct connman_technology_driver *driver = list->data;
434
435                 DBG("driver %p name %s", driver, driver->name);
436
437                 if (driver->type != technology->type)
438                         continue;
439
440                 if (driver->probe(technology) == 0) {
441                         technology->driver = driver;
442                         break;
443                 }
444         }
445
446 done:
447         DBG("technology %p", technology);
448
449         return technology;
450 }
451
452 static void technology_put(struct connman_technology *technology)
453 {
454         DBG("technology %p", technology);
455
456         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
457                 return;
458
459         if (technology->driver) {
460                 technology->driver->remove(technology);
461                 technology->driver = NULL;
462         }
463
464         technology_list = g_slist_remove(technology_list, technology);
465
466         technologies_changed();
467
468         g_dbus_unregister_interface(connection, technology->path,
469                                                 CONNMAN_TECHNOLOGY_INTERFACE);
470
471         g_slist_free(technology->device_list);
472         g_hash_table_destroy(technology->rfkill_list);
473
474         g_free(technology->path);
475         g_free(technology);
476 }
477
478 static void unregister_technology(gpointer data)
479 {
480         struct connman_technology *technology = data;
481
482         technology_put(technology);
483 }
484
485 int __connman_technology_add_device(struct connman_device *device)
486 {
487         struct connman_technology *technology;
488         enum connman_service_type type;
489
490         DBG("device %p", device);
491
492         type = __connman_device_get_service_type(device);
493         __connman_notifier_register(type);
494
495         technology = technology_get(type);
496         if (technology == NULL)
497                 return -ENXIO;
498
499         g_hash_table_insert(device_table, device, technology);
500
501         if (g_atomic_int_get(&technology->blocked))
502                 goto done;
503
504         technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
505
506         state_changed(technology);
507
508 done:
509
510         technology->device_list = g_slist_append(technology->device_list,
511                                                                 device);
512         devices_changed(technology);
513
514         return 0;
515 }
516
517 int __connman_technology_remove_device(struct connman_device *device)
518 {
519         struct connman_technology *technology;
520         enum connman_service_type type;
521
522         DBG("device %p", device);
523
524         type = __connman_device_get_service_type(device);
525         __connman_notifier_unregister(type);
526
527         technology = g_hash_table_lookup(device_table, device);
528         if (technology == NULL)
529                 return -ENXIO;
530
531         technology->device_list = g_slist_remove(technology->device_list,
532                                                                 device);
533         devices_changed(technology);
534
535         if (technology->device_list == NULL) {
536                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
537                 state_changed(technology);
538         }
539
540         g_hash_table_remove(device_table, device);
541
542         return 0;
543 }
544
545 int __connman_technology_enable_device(struct connman_device *device)
546 {
547         struct connman_technology *technology;
548         enum connman_service_type type;
549
550         DBG("device %p", device);
551
552         technology = g_hash_table_lookup(device_table, device);
553         if (technology == NULL)
554                 return -ENXIO;
555
556         if (g_atomic_int_get(&technology->blocked))
557                 return -ERFKILL;
558
559         type = __connman_device_get_service_type(device);
560         __connman_notifier_enable(type);
561
562         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
563                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
564                 state_changed(technology);
565         }
566
567         return 0;
568 }
569
570 int __connman_technology_disable_device(struct connman_device *device)
571 {
572         struct connman_technology *technology;
573         enum connman_service_type type;
574         GSList *list;
575
576         DBG("device %p", device);
577
578         type = __connman_device_get_service_type(device);
579         __connman_notifier_disable(type);
580
581         technology = g_hash_table_lookup(device_table, device);
582         if (technology == NULL)
583                 return -ENXIO;
584
585         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
586                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
587                 state_changed(technology);
588         }
589
590         for (list = technology->device_list; list; list = list->next) {
591                 struct connman_device *device = list->data;
592
593                 if (__connman_device_get_blocked(device) == FALSE)
594                         return 0;
595         }
596
597         technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
598         state_changed(technology);
599
600         return 0;
601 }
602
603 static void technology_blocked(struct connman_technology *technology,
604                                 connman_bool_t blocked)
605 {
606         GSList *list;
607
608         for (list = technology->device_list; list; list = list->next) {
609                 struct connman_device *device = list->data;
610
611                 __connman_device_set_blocked(device, blocked);
612         }
613 }
614
615 int __connman_technology_add_rfkill(unsigned int index,
616                                         enum connman_service_type type,
617                                                 connman_bool_t softblock,
618                                                 connman_bool_t hardblock)
619 {
620         struct connman_technology *technology;
621         struct connman_rfkill *rfkill;
622         connman_bool_t blocked;
623
624         DBG("index %u type %d soft %u hard %u", index, type,
625                                                         softblock, hardblock);
626
627         technology = technology_get(type);
628         if (technology == NULL)
629                 return -ENXIO;
630
631         rfkill = g_try_new0(struct connman_rfkill, 1);
632         if (rfkill == NULL)
633                 return -ENOMEM;
634
635         rfkill->index = index;
636         rfkill->type = type;
637         rfkill->softblock = softblock;
638         rfkill->hardblock = hardblock;
639
640         g_hash_table_replace(rfkill_table, &rfkill->index, technology);
641
642         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
643
644         blocked = (softblock || hardblock) ? TRUE : FALSE;
645         if (blocked == FALSE)
646                 return 0;
647
648         if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
649                 technology_blocked(technology, TRUE);
650
651                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
652                 state_changed(technology);
653         }
654
655         return 0;
656 }
657
658 int __connman_technology_update_rfkill(unsigned int index,
659                                                 connman_bool_t softblock,
660                                                 connman_bool_t hardblock)
661 {
662         struct connman_technology *technology;
663         struct connman_rfkill *rfkill;
664         connman_bool_t blocked;
665
666         DBG("index %u soft %u hard %u", index, softblock, hardblock);
667
668         technology = g_hash_table_lookup(rfkill_table, &index);
669         if (technology == NULL)
670                 return -ENXIO;
671
672         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
673         if (rfkill == NULL)
674                 return -ENXIO;
675
676         rfkill->softblock = softblock;
677         rfkill->hardblock = hardblock;
678
679         blocked = (softblock || hardblock) ? TRUE : FALSE;
680
681         if (blocked) {
682                 guint n_blocked;
683
684                 n_blocked =
685                         g_atomic_int_exchange_and_add(&technology->blocked, 1);
686                 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
687                         return 0;
688
689                 technology_blocked(technology, blocked);
690                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
691                 state_changed(technology);
692         } else {
693                 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
694                         return 0;
695
696                 technology_blocked(technology, blocked);
697                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
698                 state_changed(technology);
699         }
700
701         return 0;
702 }
703
704 int __connman_technology_remove_rfkill(unsigned int index)
705 {
706         struct connman_technology *technology;
707
708         DBG("index %u", index);
709
710         technology = g_hash_table_lookup(rfkill_table, &index);
711         if (technology == NULL)
712                 return -ENXIO;
713
714         g_hash_table_remove(technology->rfkill_list, &index);
715
716         g_hash_table_remove(rfkill_table, &index);
717
718         if (g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
719                 technology_blocked(technology, FALSE);
720                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
721                 state_changed(technology);
722         }
723
724         return 0;
725 }
726
727 int __connman_technology_init(void)
728 {
729         DBG("");
730
731         connection = connman_dbus_get_connection();
732
733         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
734                                                 NULL, unregister_technology);
735         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
736                                                 NULL, unregister_technology);
737
738         return 0;
739 }
740
741 void __connman_technology_cleanup(void)
742 {
743         DBG("");
744
745         g_hash_table_destroy(device_table);
746         g_hash_table_destroy(rfkill_table);
747
748         dbus_connection_unref(connection);
749 }