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