b315e9529cb2a43f44af6c04c6a6b1de574d2bbc
[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 <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 void connman_technology_tethering_notify(struct connman_technology *technology,
181                                                         connman_bool_t enabled)
182 {
183         DBG("technology %p enabled %u", technology, enabled);
184
185         if (enabled == TRUE)
186                 __connman_tethering_set_enabled();
187         else
188                 __connman_tethering_set_disabled();
189 }
190
191 static int set_tethering(const char *bridge, connman_bool_t enabled)
192 {
193         GSList *list;
194
195         for (list = technology_list; list; list = list->next) {
196                 struct connman_technology *technology = list->data;
197
198                 if (technology->driver == NULL)
199                         continue;
200
201                 if (technology->driver->set_tethering)
202                         technology->driver->set_tethering(technology,
203                                                         bridge, enabled);
204         }
205
206         return 0;
207 }
208
209 int __connman_technology_enable_tethering(const char *bridge)
210 {
211         return set_tethering(bridge, TRUE);
212 }
213
214 int __connman_technology_disable_tethering(const char *bridge)
215 {
216         return set_tethering(bridge, FALSE);
217 }
218
219 static void free_rfkill(gpointer data)
220 {
221         struct connman_rfkill *rfkill = data;
222
223         g_free(rfkill);
224 }
225
226 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
227 {
228         GSList *list;
229
230         for (list = technology_list; list; list = list->next) {
231                 struct connman_technology *technology = list->data;
232
233                 if (technology->path == NULL)
234                         continue;
235
236                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
237                                                         &technology->path);
238         }
239 }
240
241 static void technologies_changed(void)
242 {
243         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
244                         CONNMAN_MANAGER_INTERFACE, "Technologies",
245                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
246 }
247
248 static const char *state2string(enum connman_technology_state state)
249 {
250         switch (state) {
251         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
252                 break;
253         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
254                 return "offline";
255         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
256                 return "available";
257         case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
258                 return "blocked";
259         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
260                 return "enabled";
261         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
262                 return "connected";
263         }
264
265         return NULL;
266 }
267
268 static void state_changed(struct connman_technology *technology)
269 {
270         const char *str;
271
272         str = state2string(technology->state);
273         if (str == NULL)
274                 return;
275
276         connman_dbus_property_changed_basic(technology->path,
277                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
278                                                 DBUS_TYPE_STRING, &str);
279 }
280
281 static const char *get_name(enum connman_service_type type)
282 {
283         switch (type) {
284         case CONNMAN_SERVICE_TYPE_UNKNOWN:
285         case CONNMAN_SERVICE_TYPE_SYSTEM:
286         case CONNMAN_SERVICE_TYPE_GPS:
287         case CONNMAN_SERVICE_TYPE_VPN:
288                 break;
289         case CONNMAN_SERVICE_TYPE_ETHERNET:
290                 return "Wired";
291         case CONNMAN_SERVICE_TYPE_WIFI:
292                 return "WiFi";
293         case CONNMAN_SERVICE_TYPE_WIMAX:
294                 return "WiMAX";
295         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
296                 return "Bluetooth";
297         case CONNMAN_SERVICE_TYPE_CELLULAR:
298                 return "3G";
299         }
300
301         return NULL;
302 }
303
304 static DBusMessage *get_properties(DBusConnection *conn,
305                                         DBusMessage *message, void *user_data)
306 {
307         struct connman_technology *technology = user_data;
308         DBusMessage *reply;
309         DBusMessageIter array, dict;
310         const char *str;
311
312         reply = dbus_message_new_method_return(message);
313         if (reply == NULL)
314                 return NULL;
315
316         dbus_message_iter_init_append(reply, &array);
317
318         connman_dbus_dict_open(&array, &dict);
319
320         str = state2string(technology->state);
321         if (str != NULL)
322                 connman_dbus_dict_append_basic(&dict, "State",
323                                                 DBUS_TYPE_STRING, &str);
324
325         str = get_name(technology->type);
326         if (str != NULL)
327                 connman_dbus_dict_append_basic(&dict, "Name",
328                                                 DBUS_TYPE_STRING, &str);
329
330         str = __connman_service_type2string(technology->type);
331         if (str != NULL)
332                 connman_dbus_dict_append_basic(&dict, "Type",
333                                                 DBUS_TYPE_STRING, &str);
334
335         connman_dbus_dict_close(&array, &dict);
336
337         return reply;
338 }
339
340 static GDBusMethodTable technology_methods[] = {
341         { "GetProperties", "", "a{sv}", get_properties },
342         { },
343 };
344
345 static GDBusSignalTable technology_signals[] = {
346         { "PropertyChanged", "sv" },
347         { },
348 };
349
350 static struct connman_technology *technology_find(enum connman_service_type type)
351 {
352         GSList *list;
353
354         DBG("type %d", type);
355
356         for (list = technology_list; list; list = list->next) {
357                 struct connman_technology *technology = list->data;
358
359                 if (technology->type == type)
360                         return technology;
361         }
362
363         return NULL;
364 }
365
366 static struct connman_technology *technology_get(enum connman_service_type type)
367 {
368         struct connman_technology *technology;
369         const char *str;
370         GSList *list;
371
372         DBG("type %d", type);
373
374         technology = technology_find(type);
375         if (technology != NULL) {
376                 g_atomic_int_inc(&technology->refcount);
377                 goto done;
378         }
379
380         str = __connman_service_type2string(type);
381         if (str == NULL)
382                 return NULL;
383
384         technology = g_try_new0(struct connman_technology, 1);
385         if (technology == NULL)
386                 return NULL;
387
388         technology->refcount = 1;
389
390         technology->type = type;
391         technology->path = g_strdup_printf("%s/technology/%s",
392                                                         CONNMAN_PATH, str);
393
394         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
395                                                         NULL, free_rfkill);
396         technology->device_list = NULL;
397
398         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
399
400         if (g_dbus_register_interface(connection, technology->path,
401                                         CONNMAN_TECHNOLOGY_INTERFACE,
402                                         technology_methods, technology_signals,
403                                         NULL, technology, NULL) == FALSE) {
404                 connman_error("Failed to register %s", technology->path);
405                 g_free(technology);
406                 return NULL;
407         }
408
409         technology_list = g_slist_append(technology_list, technology);
410
411         technologies_changed();
412
413         if (technology->driver != NULL)
414                 goto done;
415
416         for (list = driver_list; list; list = list->next) {
417                 struct connman_technology_driver *driver = list->data;
418
419                 DBG("driver %p name %s", driver, driver->name);
420
421                 if (driver->type != technology->type)
422                         continue;
423
424                 if (driver->probe(technology) == 0) {
425                         technology->driver = driver;
426                         break;
427                 }
428         }
429
430 done:
431         DBG("technology %p", technology);
432
433         return technology;
434 }
435
436 static void technology_put(struct connman_technology *technology)
437 {
438         DBG("technology %p", technology);
439
440         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
441                 return;
442
443         if (technology->driver) {
444                 technology->driver->remove(technology);
445                 technology->driver = NULL;
446         }
447
448         technology_list = g_slist_remove(technology_list, technology);
449
450         technologies_changed();
451
452         g_dbus_unregister_interface(connection, technology->path,
453                                                 CONNMAN_TECHNOLOGY_INTERFACE);
454
455         g_slist_free(technology->device_list);
456         g_hash_table_destroy(technology->rfkill_list);
457
458         g_free(technology->path);
459         g_free(technology);
460 }
461
462 static void unregister_technology(gpointer data)
463 {
464         struct connman_technology *technology = data;
465
466         technology_put(technology);
467 }
468
469 int __connman_technology_add_device(struct connman_device *device)
470 {
471         struct connman_technology *technology;
472         enum connman_service_type type;
473
474         DBG("device %p", device);
475
476         type = __connman_device_get_service_type(device);
477         __connman_notifier_register(type);
478
479         technology = technology_get(type);
480         if (technology == NULL)
481                 return -ENXIO;
482
483         g_hash_table_insert(device_table, device, technology);
484
485         if (g_atomic_int_get(&technology->blocked))
486                 goto done;
487
488         technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
489
490         state_changed(technology);
491
492 done:
493
494         technology->device_list = g_slist_append(technology->device_list,
495                                                                 device);
496
497         return 0;
498 }
499
500 int __connman_technology_remove_device(struct connman_device *device)
501 {
502         struct connman_technology *technology;
503         enum connman_service_type type;
504
505         DBG("device %p", device);
506
507         type = __connman_device_get_service_type(device);
508         __connman_notifier_unregister(type);
509
510         technology = g_hash_table_lookup(device_table, device);
511         if (technology == NULL)
512                 return -ENXIO;
513
514         technology->device_list = g_slist_remove(technology->device_list,
515                                                                 device);
516         if (technology->device_list == NULL) {
517                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
518                 state_changed(technology);
519         }
520
521         g_hash_table_remove(device_table, device);
522
523         return 0;
524 }
525
526 int __connman_technology_enable_device(struct connman_device *device)
527 {
528         struct connman_technology *technology;
529         enum connman_service_type type;
530
531         DBG("device %p", device);
532
533         technology = g_hash_table_lookup(device_table, device);
534         if (technology == NULL)
535                 return -ENXIO;
536
537         if (g_atomic_int_get(&technology->blocked))
538                 return -ERFKILL;
539
540         type = __connman_device_get_service_type(device);
541         __connman_notifier_enable(type);
542
543         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
544                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
545                 state_changed(technology);
546         }
547
548         return 0;
549 }
550
551 int __connman_technology_disable_device(struct connman_device *device)
552 {
553         struct connman_technology *technology;
554         enum connman_service_type type;
555         GSList *list;
556
557         DBG("device %p", device);
558
559         type = __connman_device_get_service_type(device);
560         __connman_notifier_disable(type);
561
562         technology = g_hash_table_lookup(device_table, device);
563         if (technology == NULL)
564                 return -ENXIO;
565
566         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
567                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
568                 state_changed(technology);
569         }
570
571         for (list = technology->device_list; list; list = list->next) {
572                 struct connman_device *device = list->data;
573
574                 if (__connman_device_get_blocked(device) == FALSE)
575                         return 0;
576         }
577
578         technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
579         state_changed(technology);
580
581         return 0;
582 }
583
584 static void technology_blocked(struct connman_technology *technology,
585                                 connman_bool_t blocked)
586 {
587         GSList *list;
588
589         for (list = technology->device_list; list; list = list->next) {
590                 struct connman_device *device = list->data;
591
592                 __connman_device_set_blocked(device, blocked);
593         }
594 }
595
596 int __connman_technology_add_rfkill(unsigned int index,
597                                         enum connman_service_type type,
598                                                 connman_bool_t softblock,
599                                                 connman_bool_t hardblock)
600 {
601         struct connman_technology *technology;
602         struct connman_rfkill *rfkill;
603         connman_bool_t blocked;
604
605         DBG("index %u type %d soft %u hard %u", index, type,
606                                                         softblock, hardblock);
607
608         technology = technology_get(type);
609         if (technology == NULL)
610                 return -ENXIO;
611
612         rfkill = g_try_new0(struct connman_rfkill, 1);
613         if (rfkill == NULL)
614                 return -ENOMEM;
615
616         rfkill->index = index;
617         rfkill->type = type;
618         rfkill->softblock = softblock;
619         rfkill->hardblock = hardblock;
620
621         g_hash_table_replace(rfkill_table, &rfkill->index, technology);
622
623         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
624
625         blocked = (softblock || hardblock) ? TRUE : FALSE;
626         if (blocked == FALSE)
627                 return 0;
628
629         if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
630                 technology_blocked(technology, TRUE);
631
632                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
633                 state_changed(technology);
634         }
635
636         return 0;
637 }
638
639 int __connman_technology_update_rfkill(unsigned int index,
640                                                 connman_bool_t softblock,
641                                                 connman_bool_t hardblock)
642 {
643         struct connman_technology *technology;
644         struct connman_rfkill *rfkill;
645         connman_bool_t blocked, old_blocked;
646
647         DBG("index %u soft %u hard %u", index, softblock, hardblock);
648
649         technology = g_hash_table_lookup(rfkill_table, &index);
650         if (technology == NULL)
651                 return -ENXIO;
652
653         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
654         if (rfkill == NULL)
655                 return -ENXIO;
656
657         old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
658         blocked = (softblock || hardblock) ? TRUE : FALSE;
659
660         rfkill->softblock = softblock;
661         rfkill->hardblock = hardblock;
662
663         if (blocked == old_blocked)
664                 return 0;
665
666         if (blocked) {
667                 guint n_blocked;
668
669                 n_blocked =
670                         g_atomic_int_exchange_and_add(&technology->blocked, 1);
671                 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
672                         return 0;
673
674                 technology_blocked(technology, blocked);
675                 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
676                 state_changed(technology);
677         } else {
678                 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
679                         return 0;
680
681                 technology_blocked(technology, blocked);
682                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
683                 state_changed(technology);
684         }
685
686         return 0;
687 }
688
689 int __connman_technology_remove_rfkill(unsigned int index)
690 {
691         struct connman_technology *technology;
692
693         DBG("index %u", index);
694
695         technology = g_hash_table_lookup(rfkill_table, &index);
696         if (technology == NULL)
697                 return -ENXIO;
698
699         g_hash_table_remove(technology->rfkill_list, &index);
700
701         g_hash_table_remove(rfkill_table, &index);
702
703         if (g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
704                 technology_blocked(technology, FALSE);
705                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
706                 state_changed(technology);
707         }
708
709         return 0;
710 }
711
712 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
713 {
714         struct connman_technology *technology;
715
716         technology = technology_get(type);
717         if (technology == NULL)
718                 return FALSE;
719
720         if (g_atomic_int_get(&technology->blocked))
721                 return TRUE;
722
723         return FALSE;
724 }
725
726 int __connman_technology_init(void)
727 {
728         DBG("");
729
730         connection = connman_dbus_get_connection();
731
732         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
733                                                 NULL, unregister_technology);
734         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
735                                                 NULL, unregister_technology);
736
737         return 0;
738 }
739
740 void __connman_technology_cleanup(void)
741 {
742         DBG("");
743
744         g_hash_table_destroy(device_table);
745         g_hash_table_destroy(rfkill_table);
746
747         dbus_connection_unref(connection);
748 }