Add technology driver callback for setting Tethering
[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 };
40
41 enum connman_technology_state {
42         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
43         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
44         CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
45         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 3,
46         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
47 };
48
49 struct connman_technology {
50         gint refcount;
51         enum connman_service_type type;
52         enum connman_technology_state state;
53         char *path;
54         GHashTable *rfkill_list;
55         GSList *device_list;
56         gint enabled;
57
58         struct connman_technology_driver *driver;
59         void *driver_data;
60 };
61
62 static GSList *driver_list = NULL;
63
64 static gint compare_priority(gconstpointer a, gconstpointer b)
65 {
66         const struct connman_technology_driver *driver1 = a;
67         const struct connman_technology_driver *driver2 = b;
68
69         return driver2->priority - driver1->priority;
70 }
71
72 /**
73  * connman_technology_driver_register:
74  * @driver: Technology driver definition
75  *
76  * Register a new technology driver
77  *
78  * Returns: %0 on success
79  */
80 int connman_technology_driver_register(struct connman_technology_driver *driver)
81 {
82         DBG("driver %p name %s", driver, driver->name);
83
84         driver_list = g_slist_insert_sorted(driver_list, driver,
85                                                         compare_priority);
86
87         return 0;
88 }
89
90 /**
91  * connman_technology_driver_unregister:
92  * @driver: Technology driver definition
93  *
94  * Remove a previously registered technology driver
95  */
96 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
97 {
98         DBG("driver %p name %s", driver, driver->name);
99
100         driver_list = g_slist_remove(driver_list, driver);
101 }
102
103 static int set_tethering(connman_bool_t enabled)
104 {
105         GSList *list;
106
107         for (list = technology_list; list; list = list->next) {
108                 struct connman_technology *technology = list->data;
109
110                 if (technology->driver == NULL)
111                         continue;
112
113                 if (technology->driver->set_tethering)
114                         technology->driver->set_tethering(technology, enabled);
115         }
116
117         return 0;
118 }
119
120 int __connman_technology_enable_tethering(void)
121 {
122         return set_tethering(TRUE);
123 }
124
125 int __connman_technology_disable_tethering(void)
126 {
127         return set_tethering(FALSE);
128 }
129
130 static void free_rfkill(gpointer data)
131 {
132         struct connman_rfkill *rfkill = data;
133
134         g_free(rfkill);
135 }
136
137 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
138 {
139         GSList *list;
140
141         for (list = technology_list; list; list = list->next) {
142                 struct connman_technology *technology = list->data;
143
144                 if (technology->path == NULL)
145                         continue;
146
147                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
148                                                         &technology->path);
149         }
150 }
151
152 static void technologies_changed(void)
153 {
154         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
155                         CONNMAN_MANAGER_INTERFACE, "Technologies",
156                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
157 }
158
159 static void device_list(DBusMessageIter *iter, void *user_data)
160 {
161         struct connman_technology *technology = user_data;
162         GSList *list;
163
164         for (list = technology->device_list; list; list = list->next) {
165                 struct connman_device *device = list->data;
166                 const char *path;
167
168                 path = connman_device_get_path(device);
169                 if (path == NULL)
170                         continue;
171
172                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
173                                                                         &path);
174         }
175 }
176
177 static void devices_changed(struct connman_technology *technology)
178 {
179         connman_dbus_property_changed_array(technology->path,
180                         CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
181                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
182 }
183
184 static const char *state2string(enum connman_technology_state state)
185 {
186         switch (state) {
187         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
188                 break;
189         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
190                 return "offline";
191         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
192                 return "available";
193         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
194                 return "enabled";
195         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
196                 return "connected";
197         }
198
199         return NULL;
200 }
201
202 static void state_changed(struct connman_technology *technology)
203 {
204         const char *str;
205
206         str = state2string(technology->state);
207         if (str == NULL)
208                 return;
209
210         connman_dbus_property_changed_basic(technology->path,
211                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
212                                                 DBUS_TYPE_STRING, &str);
213 }
214
215 static const char *get_name(enum connman_service_type type)
216 {
217         switch (type) {
218         case CONNMAN_SERVICE_TYPE_UNKNOWN:
219         case CONNMAN_SERVICE_TYPE_SYSTEM:
220         case CONNMAN_SERVICE_TYPE_GPS:
221         case CONNMAN_SERVICE_TYPE_VPN:
222                 break;
223         case CONNMAN_SERVICE_TYPE_ETHERNET:
224                 return "Wired";
225         case CONNMAN_SERVICE_TYPE_WIFI:
226                 return "WiFi";
227         case CONNMAN_SERVICE_TYPE_WIMAX:
228                 return "WiMAX";
229         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
230                 return "Bluetooth";
231         case CONNMAN_SERVICE_TYPE_CELLULAR:
232                 return "3G";
233         }
234
235         return NULL;
236 }
237
238 static DBusMessage *get_properties(DBusConnection *conn,
239                                         DBusMessage *message, void *user_data)
240 {
241         struct connman_technology *technology = user_data;
242         DBusMessage *reply;
243         DBusMessageIter array, dict;
244         const char *str;
245
246         reply = dbus_message_new_method_return(message);
247         if (reply == NULL)
248                 return NULL;
249
250         dbus_message_iter_init_append(reply, &array);
251
252         connman_dbus_dict_open(&array, &dict);
253
254         str = state2string(technology->state);
255         if (str != NULL)
256                 connman_dbus_dict_append_basic(&dict, "State",
257                                                 DBUS_TYPE_STRING, &str);
258
259         str = get_name(technology->type);
260         if (str != NULL)
261                 connman_dbus_dict_append_basic(&dict, "Name",
262                                                 DBUS_TYPE_STRING, &str);
263
264         str = __connman_service_type2string(technology->type);
265         if (str != NULL)
266                 connman_dbus_dict_append_basic(&dict, "Type",
267                                                 DBUS_TYPE_STRING, &str);
268
269         connman_dbus_dict_append_array(&dict, "Devices",
270                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
271
272         connman_dbus_dict_close(&array, &dict);
273
274         return reply;
275 }
276
277 static GDBusMethodTable technology_methods[] = {
278         { "GetProperties", "", "a{sv}", get_properties },
279         { },
280 };
281
282 static GDBusSignalTable technology_signals[] = {
283         { "PropertyChanged", "sv" },
284         { },
285 };
286
287 static struct connman_technology *technology_find(enum connman_service_type type)
288 {
289         GSList *list;
290
291         DBG("type %d", type);
292
293         for (list = technology_list; list; list = list->next) {
294                 struct connman_technology *technology = list->data;
295
296                 if (technology->type == type)
297                         return technology;
298         }
299
300         return NULL;
301 }
302
303 static struct connman_technology *technology_get(enum connman_service_type type)
304 {
305         struct connman_technology *technology;
306         const char *str;
307         GSList *list;
308
309         DBG("type %d", type);
310
311         technology = technology_find(type);
312         if (technology != NULL) {
313                 g_atomic_int_inc(&technology->refcount);
314                 goto done;
315         }
316
317         str = __connman_service_type2string(type);
318         if (str == NULL)
319                 return NULL;
320
321         technology = g_try_new0(struct connman_technology, 1);
322         if (technology == NULL)
323                 return NULL;
324
325         technology->refcount = 1;
326
327         technology->type = type;
328         technology->path = g_strdup_printf("%s/technology/%s",
329                                                         CONNMAN_PATH, str);
330
331         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
332                                                         NULL, free_rfkill);
333         technology->device_list = NULL;
334
335         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
336
337         if (g_dbus_register_interface(connection, technology->path,
338                                         CONNMAN_TECHNOLOGY_INTERFACE,
339                                         technology_methods, technology_signals,
340                                         NULL, technology, NULL) == FALSE) {
341                 connman_error("Failed to register %s", technology->path);
342                 g_free(technology);
343                 return NULL;
344         }
345
346         technology_list = g_slist_append(technology_list, technology);
347
348         technologies_changed();
349
350         if (technology->driver != NULL)
351                 goto done;
352
353         for (list = driver_list; list; list = list->next) {
354                 struct connman_technology_driver *driver = list->data;
355
356                 DBG("driver %p name %s", driver, driver->name);
357
358                 if (driver->type != technology->type)
359                         continue;
360
361                 if (driver->probe(technology) == 0) {
362                         technology->driver = driver;
363                         break;
364                 }
365         }
366
367 done:
368         DBG("technology %p", technology);
369
370         return technology;
371 }
372
373 static void technology_put(struct connman_technology *technology)
374 {
375         DBG("technology %p", technology);
376
377         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
378                 return;
379
380         if (technology->driver) {
381                 technology->driver->remove(technology);
382                 technology->driver = NULL;
383         }
384
385         technology_list = g_slist_remove(technology_list, technology);
386
387         technologies_changed();
388
389         g_dbus_unregister_interface(connection, technology->path,
390                                                 CONNMAN_TECHNOLOGY_INTERFACE);
391
392         g_slist_free(technology->device_list);
393         g_hash_table_destroy(technology->rfkill_list);
394
395         g_free(technology->path);
396         g_free(technology);
397 }
398
399 static void unregister_technology(gpointer data)
400 {
401         struct connman_technology *technology = data;
402
403         technology_put(technology);
404 }
405
406 int __connman_technology_add_device(struct connman_device *device)
407 {
408         struct connman_technology *technology;
409         enum connman_service_type type;
410
411         DBG("device %p", device);
412
413         type = __connman_device_get_service_type(device);
414         __connman_notifier_register(type);
415
416         technology = technology_get(type);
417         if (technology == NULL)
418                 return -ENXIO;
419
420         g_hash_table_insert(device_table, device, technology);
421
422         if (technology->device_list == NULL) {
423                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
424                 state_changed(technology);
425         }
426
427         technology->device_list = g_slist_append(technology->device_list,
428                                                                 device);
429         devices_changed(technology);
430
431         return 0;
432 }
433
434 int __connman_technology_remove_device(struct connman_device *device)
435 {
436         struct connman_technology *technology;
437         enum connman_service_type type;
438
439         DBG("device %p", device);
440
441         type = __connman_device_get_service_type(device);
442         __connman_notifier_disable(type);
443         __connman_notifier_unregister(type);
444
445         technology = g_hash_table_lookup(device_table, device);
446         if (technology == NULL)
447                 return -ENXIO;
448
449         technology->device_list = g_slist_remove(technology->device_list,
450                                                                 device);
451         devices_changed(technology);
452
453         if (technology->device_list == NULL) {
454                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
455                 state_changed(technology);
456         }
457
458         g_hash_table_remove(device_table, device);
459
460         return 0;
461 }
462
463 int __connman_technology_enable_device(struct connman_device *device)
464 {
465         struct connman_technology *technology;
466         enum connman_service_type type;
467
468         DBG("device %p", device);
469
470         type = __connman_device_get_service_type(device);
471         __connman_notifier_enable(type);
472
473         technology = g_hash_table_lookup(device_table, device);
474         if (technology == NULL)
475                 return -ENXIO;
476
477         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
478                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
479                 state_changed(technology);
480         }
481
482         return 0;
483 }
484
485 int __connman_technology_disable_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_disable(type);
494
495         technology = g_hash_table_lookup(device_table, device);
496         if (technology == NULL)
497                 return -ENXIO;
498
499         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
500                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
501                 state_changed(technology);
502         }
503
504         return 0;
505 }
506
507 int __connman_technology_add_rfkill(unsigned int index,
508                                         enum connman_service_type type,
509                                                 connman_bool_t softblock,
510                                                 connman_bool_t hardblock)
511 {
512         struct connman_technology *technology;
513         struct connman_rfkill *rfkill;
514
515         DBG("index %u type %d soft %u hard %u", index, type,
516                                                         softblock, hardblock);
517
518         rfkill = g_try_new0(struct connman_rfkill, 1);
519         if (rfkill == NULL)
520                 return -ENOMEM;
521
522         rfkill->index = index;
523         rfkill->type = type;
524
525         technology = technology_get(type);
526         if (technology == NULL) {
527                 g_free(rfkill);
528                 return -ENXIO;
529         }
530
531         g_hash_table_replace(rfkill_table, &index, technology);
532
533         g_hash_table_replace(technology->rfkill_list, &index, rfkill);
534
535         return 0;
536 }
537
538 int __connman_technology_update_rfkill(unsigned int index,
539                                                 connman_bool_t softblock,
540                                                 connman_bool_t hardblock)
541 {
542         struct connman_technology *technology;
543
544         DBG("index %u soft %u hard %u", index, softblock, hardblock);
545
546         technology = g_hash_table_lookup(rfkill_table, &index);
547         if (technology == NULL)
548                 return -ENXIO;
549
550         return 0;
551 }
552
553 int __connman_technology_remove_rfkill(unsigned int index)
554 {
555         struct connman_technology *technology;
556
557         DBG("index %u", index);
558
559         technology = g_hash_table_lookup(rfkill_table, &index);
560         if (technology == NULL)
561                 return -ENXIO;
562
563         g_hash_table_remove(technology->rfkill_list, &index);
564
565         g_hash_table_remove(rfkill_table, &index);
566
567         return 0;
568 }
569
570 int __connman_technology_init(void)
571 {
572         DBG("");
573
574         connection = connman_dbus_get_connection();
575
576         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
577                                                 NULL, unregister_technology);
578         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
579                                                 NULL, unregister_technology);
580
581         return 0;
582 }
583
584 void __connman_technology_cleanup(void)
585 {
586         DBG("");
587
588         g_hash_table_destroy(device_table);
589         g_hash_table_destroy(rfkill_table);
590
591         dbus_connection_unref(connection);
592 }