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