Provide unique identifier along with the interface callbacks
[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 void __connman_technology_add_interface(enum connman_service_type type,
104                                 int index, const char *name, const char *ident)
105 {
106         GSList *list;
107
108         switch (type) {
109         case CONNMAN_SERVICE_TYPE_UNKNOWN:
110         case CONNMAN_SERVICE_TYPE_SYSTEM:
111                 return;
112         case CONNMAN_SERVICE_TYPE_ETHERNET:
113         case CONNMAN_SERVICE_TYPE_WIFI:
114         case CONNMAN_SERVICE_TYPE_WIMAX:
115         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
116         case CONNMAN_SERVICE_TYPE_CELLULAR:
117         case CONNMAN_SERVICE_TYPE_GPS:
118         case CONNMAN_SERVICE_TYPE_VPN:
119                 break;
120         }
121
122         connman_info("Create interface %s [ %s ]", name,
123                                 __connman_service_type2string(type));
124
125         for (list = technology_list; list; list = list->next) {
126                 struct connman_technology *technology = list->data;
127
128                 if (technology->type != type)
129                         continue;
130
131                 if (technology->driver == NULL)
132                         continue;
133
134                 if (technology->driver->add_interface)
135                         technology->driver->add_interface(technology,
136                                                         index, name, ident);
137         }
138 }
139
140 void __connman_technology_remove_interface(enum connman_service_type type,
141                                 int index, const char *name, const char *ident)
142 {
143         GSList *list;
144
145         switch (type) {
146         case CONNMAN_SERVICE_TYPE_UNKNOWN:
147         case CONNMAN_SERVICE_TYPE_SYSTEM:
148                 return;
149         case CONNMAN_SERVICE_TYPE_ETHERNET:
150         case CONNMAN_SERVICE_TYPE_WIFI:
151         case CONNMAN_SERVICE_TYPE_WIMAX:
152         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
153         case CONNMAN_SERVICE_TYPE_CELLULAR:
154         case CONNMAN_SERVICE_TYPE_GPS:
155         case CONNMAN_SERVICE_TYPE_VPN:
156                 break;
157         }
158
159         connman_info("Remove interface %s [ %s ]", name,
160                                 __connman_service_type2string(type));
161
162         for (list = technology_list; list; list = list->next) {
163                 struct connman_technology *technology = list->data;
164
165                 if (technology->type != type)
166                         continue;
167
168                 if (technology->driver == NULL)
169                         continue;
170
171                 if (technology->driver->remove_interface)
172                         technology->driver->remove_interface(technology, index);
173         }
174 }
175
176 static int set_tethering(connman_bool_t enabled)
177 {
178         GSList *list;
179
180         for (list = technology_list; list; list = list->next) {
181                 struct connman_technology *technology = list->data;
182
183                 if (technology->driver == NULL)
184                         continue;
185
186                 if (technology->driver->set_tethering)
187                         technology->driver->set_tethering(technology, enabled);
188         }
189
190         return 0;
191 }
192
193 int __connman_technology_enable_tethering(void)
194 {
195         return set_tethering(TRUE);
196 }
197
198 int __connman_technology_disable_tethering(void)
199 {
200         return set_tethering(FALSE);
201 }
202
203 static void free_rfkill(gpointer data)
204 {
205         struct connman_rfkill *rfkill = data;
206
207         g_free(rfkill);
208 }
209
210 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
211 {
212         GSList *list;
213
214         for (list = technology_list; list; list = list->next) {
215                 struct connman_technology *technology = list->data;
216
217                 if (technology->path == NULL)
218                         continue;
219
220                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
221                                                         &technology->path);
222         }
223 }
224
225 static void technologies_changed(void)
226 {
227         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
228                         CONNMAN_MANAGER_INTERFACE, "Technologies",
229                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
230 }
231
232 static void device_list(DBusMessageIter *iter, void *user_data)
233 {
234         struct connman_technology *technology = user_data;
235         GSList *list;
236
237         for (list = technology->device_list; list; list = list->next) {
238                 struct connman_device *device = list->data;
239                 const char *path;
240
241                 path = connman_device_get_path(device);
242                 if (path == NULL)
243                         continue;
244
245                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
246                                                                         &path);
247         }
248 }
249
250 static void devices_changed(struct connman_technology *technology)
251 {
252         connman_dbus_property_changed_array(technology->path,
253                         CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
254                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
255 }
256
257 static const char *state2string(enum connman_technology_state state)
258 {
259         switch (state) {
260         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
261                 break;
262         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
263                 return "offline";
264         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
265                 return "available";
266         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
267                 return "enabled";
268         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
269                 return "connected";
270         }
271
272         return NULL;
273 }
274
275 static void state_changed(struct connman_technology *technology)
276 {
277         const char *str;
278
279         str = state2string(technology->state);
280         if (str == NULL)
281                 return;
282
283         connman_dbus_property_changed_basic(technology->path,
284                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
285                                                 DBUS_TYPE_STRING, &str);
286 }
287
288 static const char *get_name(enum connman_service_type type)
289 {
290         switch (type) {
291         case CONNMAN_SERVICE_TYPE_UNKNOWN:
292         case CONNMAN_SERVICE_TYPE_SYSTEM:
293         case CONNMAN_SERVICE_TYPE_GPS:
294         case CONNMAN_SERVICE_TYPE_VPN:
295                 break;
296         case CONNMAN_SERVICE_TYPE_ETHERNET:
297                 return "Wired";
298         case CONNMAN_SERVICE_TYPE_WIFI:
299                 return "WiFi";
300         case CONNMAN_SERVICE_TYPE_WIMAX:
301                 return "WiMAX";
302         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
303                 return "Bluetooth";
304         case CONNMAN_SERVICE_TYPE_CELLULAR:
305                 return "3G";
306         }
307
308         return NULL;
309 }
310
311 static DBusMessage *get_properties(DBusConnection *conn,
312                                         DBusMessage *message, void *user_data)
313 {
314         struct connman_technology *technology = user_data;
315         DBusMessage *reply;
316         DBusMessageIter array, dict;
317         const char *str;
318
319         reply = dbus_message_new_method_return(message);
320         if (reply == NULL)
321                 return NULL;
322
323         dbus_message_iter_init_append(reply, &array);
324
325         connman_dbus_dict_open(&array, &dict);
326
327         str = state2string(technology->state);
328         if (str != NULL)
329                 connman_dbus_dict_append_basic(&dict, "State",
330                                                 DBUS_TYPE_STRING, &str);
331
332         str = get_name(technology->type);
333         if (str != NULL)
334                 connman_dbus_dict_append_basic(&dict, "Name",
335                                                 DBUS_TYPE_STRING, &str);
336
337         str = __connman_service_type2string(technology->type);
338         if (str != NULL)
339                 connman_dbus_dict_append_basic(&dict, "Type",
340                                                 DBUS_TYPE_STRING, &str);
341
342         connman_dbus_dict_append_array(&dict, "Devices",
343                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
344
345         connman_dbus_dict_close(&array, &dict);
346
347         return reply;
348 }
349
350 static GDBusMethodTable technology_methods[] = {
351         { "GetProperties", "", "a{sv}", get_properties },
352         { },
353 };
354
355 static GDBusSignalTable technology_signals[] = {
356         { "PropertyChanged", "sv" },
357         { },
358 };
359
360 static struct connman_technology *technology_find(enum connman_service_type type)
361 {
362         GSList *list;
363
364         DBG("type %d", type);
365
366         for (list = technology_list; list; list = list->next) {
367                 struct connman_technology *technology = list->data;
368
369                 if (technology->type == type)
370                         return technology;
371         }
372
373         return NULL;
374 }
375
376 static struct connman_technology *technology_get(enum connman_service_type type)
377 {
378         struct connman_technology *technology;
379         const char *str;
380         GSList *list;
381
382         DBG("type %d", type);
383
384         technology = technology_find(type);
385         if (technology != NULL) {
386                 g_atomic_int_inc(&technology->refcount);
387                 goto done;
388         }
389
390         str = __connman_service_type2string(type);
391         if (str == NULL)
392                 return NULL;
393
394         technology = g_try_new0(struct connman_technology, 1);
395         if (technology == NULL)
396                 return NULL;
397
398         technology->refcount = 1;
399
400         technology->type = type;
401         technology->path = g_strdup_printf("%s/technology/%s",
402                                                         CONNMAN_PATH, str);
403
404         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
405                                                         NULL, free_rfkill);
406         technology->device_list = NULL;
407
408         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
409
410         if (g_dbus_register_interface(connection, technology->path,
411                                         CONNMAN_TECHNOLOGY_INTERFACE,
412                                         technology_methods, technology_signals,
413                                         NULL, technology, NULL) == FALSE) {
414                 connman_error("Failed to register %s", technology->path);
415                 g_free(technology);
416                 return NULL;
417         }
418
419         technology_list = g_slist_append(technology_list, technology);
420
421         technologies_changed();
422
423         if (technology->driver != NULL)
424                 goto done;
425
426         for (list = driver_list; list; list = list->next) {
427                 struct connman_technology_driver *driver = list->data;
428
429                 DBG("driver %p name %s", driver, driver->name);
430
431                 if (driver->type != technology->type)
432                         continue;
433
434                 if (driver->probe(technology) == 0) {
435                         technology->driver = driver;
436                         break;
437                 }
438         }
439
440 done:
441         DBG("technology %p", technology);
442
443         return technology;
444 }
445
446 static void technology_put(struct connman_technology *technology)
447 {
448         DBG("technology %p", technology);
449
450         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
451                 return;
452
453         if (technology->driver) {
454                 technology->driver->remove(technology);
455                 technology->driver = NULL;
456         }
457
458         technology_list = g_slist_remove(technology_list, technology);
459
460         technologies_changed();
461
462         g_dbus_unregister_interface(connection, technology->path,
463                                                 CONNMAN_TECHNOLOGY_INTERFACE);
464
465         g_slist_free(technology->device_list);
466         g_hash_table_destroy(technology->rfkill_list);
467
468         g_free(technology->path);
469         g_free(technology);
470 }
471
472 static void unregister_technology(gpointer data)
473 {
474         struct connman_technology *technology = data;
475
476         technology_put(technology);
477 }
478
479 int __connman_technology_add_device(struct connman_device *device)
480 {
481         struct connman_technology *technology;
482         enum connman_service_type type;
483
484         DBG("device %p", device);
485
486         type = __connman_device_get_service_type(device);
487         __connman_notifier_register(type);
488
489         technology = technology_get(type);
490         if (technology == NULL)
491                 return -ENXIO;
492
493         g_hash_table_insert(device_table, device, technology);
494
495         if (technology->device_list == NULL) {
496                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
497                 state_changed(technology);
498         }
499
500         technology->device_list = g_slist_append(technology->device_list,
501                                                                 device);
502         devices_changed(technology);
503
504         return 0;
505 }
506
507 int __connman_technology_remove_device(struct connman_device *device)
508 {
509         struct connman_technology *technology;
510         enum connman_service_type type;
511
512         DBG("device %p", device);
513
514         type = __connman_device_get_service_type(device);
515         __connman_notifier_disable(type);
516         __connman_notifier_unregister(type);
517
518         technology = g_hash_table_lookup(device_table, device);
519         if (technology == NULL)
520                 return -ENXIO;
521
522         technology->device_list = g_slist_remove(technology->device_list,
523                                                                 device);
524         devices_changed(technology);
525
526         if (technology->device_list == NULL) {
527                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
528                 state_changed(technology);
529         }
530
531         g_hash_table_remove(device_table, device);
532
533         return 0;
534 }
535
536 int __connman_technology_enable_device(struct connman_device *device)
537 {
538         struct connman_technology *technology;
539         enum connman_service_type type;
540
541         DBG("device %p", device);
542
543         type = __connman_device_get_service_type(device);
544         __connman_notifier_enable(type);
545
546         technology = g_hash_table_lookup(device_table, device);
547         if (technology == NULL)
548                 return -ENXIO;
549
550         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
551                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
552                 state_changed(technology);
553         }
554
555         return 0;
556 }
557
558 int __connman_technology_disable_device(struct connman_device *device)
559 {
560         struct connman_technology *technology;
561         enum connman_service_type type;
562
563         DBG("device %p", device);
564
565         type = __connman_device_get_service_type(device);
566         __connman_notifier_disable(type);
567
568         technology = g_hash_table_lookup(device_table, device);
569         if (technology == NULL)
570                 return -ENXIO;
571
572         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
573                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
574                 state_changed(technology);
575         }
576
577         return 0;
578 }
579
580 int __connman_technology_add_rfkill(unsigned int index,
581                                         enum connman_service_type type,
582                                                 connman_bool_t softblock,
583                                                 connman_bool_t hardblock)
584 {
585         struct connman_technology *technology;
586         struct connman_rfkill *rfkill;
587
588         DBG("index %u type %d soft %u hard %u", index, type,
589                                                         softblock, hardblock);
590
591         rfkill = g_try_new0(struct connman_rfkill, 1);
592         if (rfkill == NULL)
593                 return -ENOMEM;
594
595         rfkill->index = index;
596         rfkill->type = type;
597
598         technology = technology_get(type);
599         if (technology == NULL) {
600                 g_free(rfkill);
601                 return -ENXIO;
602         }
603
604         g_hash_table_replace(rfkill_table, &index, technology);
605
606         g_hash_table_replace(technology->rfkill_list, &index, rfkill);
607
608         return 0;
609 }
610
611 int __connman_technology_update_rfkill(unsigned int index,
612                                                 connman_bool_t softblock,
613                                                 connman_bool_t hardblock)
614 {
615         struct connman_technology *technology;
616
617         DBG("index %u soft %u hard %u", index, softblock, hardblock);
618
619         technology = g_hash_table_lookup(rfkill_table, &index);
620         if (technology == NULL)
621                 return -ENXIO;
622
623         return 0;
624 }
625
626 int __connman_technology_remove_rfkill(unsigned int index)
627 {
628         struct connman_technology *technology;
629
630         DBG("index %u", index);
631
632         technology = g_hash_table_lookup(rfkill_table, &index);
633         if (technology == NULL)
634                 return -ENXIO;
635
636         g_hash_table_remove(technology->rfkill_list, &index);
637
638         g_hash_table_remove(rfkill_table, &index);
639
640         return 0;
641 }
642
643 int __connman_technology_init(void)
644 {
645         DBG("");
646
647         connection = connman_dbus_get_connection();
648
649         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
650                                                 NULL, unregister_technology);
651         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
652                                                 NULL, unregister_technology);
653
654         return 0;
655 }
656
657 void __connman_technology_cleanup(void)
658 {
659         DBG("");
660
661         g_hash_table_destroy(device_table);
662         g_hash_table_destroy(rfkill_table);
663
664         dbus_connection_unref(connection);
665 }