6ecff7de7d0d126f8466673bff206b70b9c4313d
[platform/upstream/connman.git] / src / service.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 = NULL;
31
32 static GSequence *service_list = NULL;
33 static GHashTable *service_hash = NULL;
34
35 struct connman_service {
36         gint refcount;
37         char *identifier;
38         char *path;
39         enum connman_service_type type;
40         enum connman_service_mode mode;
41         enum connman_service_security security;
42         enum connman_service_state state;
43         connman_uint8_t strength;
44         connman_bool_t favorite;
45         unsigned int order;
46         char *name;
47         struct connman_device *device;
48 };
49
50 static void append_path(gpointer value, gpointer user_data)
51 {
52         struct connman_service *service = value;
53         DBusMessageIter *iter = user_data;
54
55         if (service->path == NULL)
56                 return;
57
58         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
59                                                         &service->path);
60 }
61
62 void __connman_service_list(DBusMessageIter *iter)
63 {
64         DBG("");
65
66         g_sequence_foreach(service_list, append_path, iter);
67 }
68
69 static const char *type2string(enum connman_service_type type)
70 {
71         switch (type) {
72         case CONNMAN_SERVICE_TYPE_UNKNOWN:
73                 break;
74         case CONNMAN_SERVICE_TYPE_ETHERNET:
75                 return "ethernet";
76         case CONNMAN_SERVICE_TYPE_WIFI:
77                 return "wifi";
78         case CONNMAN_SERVICE_TYPE_WIMAX:
79                 return "wimax";
80         }
81
82         return NULL;
83 }
84
85 static const char *mode2string(enum connman_service_mode mode)
86 {
87         switch (mode) {
88         case CONNMAN_SERVICE_MODE_UNKNOWN:
89                 break;
90         case CONNMAN_SERVICE_MODE_MANAGED:
91                 return "managed";
92         case CONNMAN_SERVICE_MODE_ADHOC:
93                 return "adhoc";
94         }
95
96         return NULL;
97 }
98
99 static const char *security2string(enum connman_service_security security)
100 {
101         switch (security) {
102         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
103                 break;
104         case CONNMAN_SERVICE_SECURITY_NONE:
105                 return "none";
106         case CONNMAN_SERVICE_SECURITY_WEP:
107                 return "wep";
108         case CONNMAN_SERVICE_SECURITY_WPA:
109                 return "wpa";
110         case CONNMAN_SERVICE_SECURITY_WPA2:
111                 return "wpa2";
112         }
113
114         return NULL;
115 }
116
117 static const char *state2string(enum connman_service_state state)
118 {
119         switch (state) {
120         case CONNMAN_SERVICE_STATE_UNKNOWN:
121                 break;
122         case CONNMAN_SERVICE_STATE_IDLE:
123                 return "idle";
124         case CONNMAN_SERVICE_STATE_ASSOCIATION:
125                 return "association";
126         case CONNMAN_SERVICE_STATE_CONFIGURATION:
127                 return "configuration";
128         case CONNMAN_SERVICE_STATE_READY:
129                 return "ready";
130         case CONNMAN_SERVICE_STATE_DISCONNECT:
131                 return "disconnect";
132         case CONNMAN_SERVICE_STATE_FAILURE:
133                 return "failure";
134         }
135
136         return NULL;
137 }
138
139 static DBusMessage *get_properties(DBusConnection *conn,
140                                         DBusMessage *msg, void *data)
141 {
142         struct connman_service *service = data;
143         DBusMessage *reply;
144         DBusMessageIter array, dict;
145         const char *str;
146
147         DBG("conn %p", conn);
148
149         reply = dbus_message_new_method_return(msg);
150         if (reply == NULL)
151                 return NULL;
152
153         dbus_message_iter_init_append(reply, &array);
154
155         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
156                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
157                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
158                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
159
160         str = type2string(service->type);
161         if (str != NULL)
162                 connman_dbus_dict_append_variant(&dict, "Type",
163                                                 DBUS_TYPE_STRING, &str);
164
165         str = mode2string(service->mode);
166         if (str != NULL)
167                 connman_dbus_dict_append_variant(&dict, "Mode",
168                                                 DBUS_TYPE_STRING, &str);
169
170         str = security2string(service->security);
171         if (str != NULL)
172                 connman_dbus_dict_append_variant(&dict, "Security",
173                                                 DBUS_TYPE_STRING, &str);
174
175         str = state2string(service->state);
176         if (str != NULL)
177                 connman_dbus_dict_append_variant(&dict, "State",
178                                                 DBUS_TYPE_STRING, &str);
179
180         if (service->strength > 0)
181                 connman_dbus_dict_append_variant(&dict, "Strength",
182                                         DBUS_TYPE_BYTE, &service->strength);
183
184         connman_dbus_dict_append_variant(&dict, "Favorite",
185                                         DBUS_TYPE_BOOLEAN, &service->favorite);
186
187         if (service->name != NULL)
188                 connman_dbus_dict_append_variant(&dict, "Name",
189                                         DBUS_TYPE_STRING, &service->name);
190
191         dbus_message_iter_close_container(&array, &dict);
192
193         return reply;
194 }
195
196 static DBusMessage *connect_service(DBusConnection *conn,
197                                         DBusMessage *msg, void *data)
198 {
199         struct connman_service *service = data;
200
201         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
202                 return __connman_error_not_supported(msg);
203
204         return __connman_error_not_implemented(msg);
205 }
206
207 static DBusMessage *disconnect_service(DBusConnection *conn,
208                                         DBusMessage *msg, void *data)
209 {
210         struct connman_service *service = data;
211
212         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
213                 return __connman_error_not_supported(msg);
214
215         return __connman_error_not_implemented(msg);
216 }
217
218 static DBusMessage *remove_service(DBusConnection *conn,
219                                         DBusMessage *msg, void *data)
220 {
221         struct connman_service *service = data;
222
223         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
224                 return __connman_error_not_supported(msg);
225
226         service->favorite = FALSE;
227
228         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
229 }
230
231 static DBusMessage *move_before(DBusConnection *conn,
232                                         DBusMessage *msg, void *data)
233 {
234         struct connman_service *service = data;
235
236         if (service->favorite == FALSE)
237                 return __connman_error_not_supported(msg);
238
239         return __connman_error_not_implemented(msg);
240 }
241
242 static DBusMessage *move_after(DBusConnection *conn,
243                                         DBusMessage *msg, void *data)
244 {
245         struct connman_service *service = data;
246
247         if (service->favorite == FALSE)
248                 return __connman_error_not_supported(msg);
249
250         return __connman_error_not_implemented(msg);
251 }
252
253 static GDBusMethodTable service_methods[] = {
254         { "GetProperties", "",  "a{sv}", get_properties     },
255         { "Connect",       "",  "",      connect_service    },
256         { "Disconnect",    "",  "",      disconnect_service },
257         { "Remove",        "",  "",      remove_service     },
258         { "MoveBefore",    "o", "",      move_before        },
259         { "MoveAfter",     "o", "",      move_after         },
260         { },
261 };
262
263 static GDBusSignalTable service_signals[] = {
264         { "PropertyChanged", "sv" },
265         { },
266 };
267
268 static void service_free(gpointer data)
269 {
270         struct connman_service *service = data;
271         char *path = service->path;
272
273         DBG("service %p", service);
274
275         g_hash_table_remove(service_hash, service->identifier);
276
277         service->path = NULL;
278
279         if (path != NULL) {
280                 __connman_profile_changed();
281
282                 g_dbus_unregister_interface(connection, path,
283                                                 CONNMAN_SERVICE_INTERFACE);
284                 g_free(path);
285         }
286
287         g_free(service->name);
288         g_free(service->identifier);
289         g_free(service);
290 }
291
292 /**
293  * connman_service_put:
294  * @service: service structure
295  *
296  * Release service if no longer needed
297  */
298 void connman_service_put(struct connman_service *service)
299 {
300         DBG("service %p", service);
301
302         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
303                 GSequenceIter *iter;
304
305                 iter = g_hash_table_lookup(service_hash, service->identifier);
306                 if (iter != NULL)
307                         g_sequence_remove(iter);
308                 else
309                         service_free(service);
310         }
311 }
312
313 static void __connman_service_initialize(struct connman_service *service)
314 {
315         DBG("service %p", service);
316
317         service->refcount = 1;
318
319         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
320         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
321         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
322         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
323
324         service->favorite = FALSE;
325
326         service->order = 0;
327 }
328
329 /**
330  * connman_service_create:
331  *
332  * Allocate a new service.
333  *
334  * Returns: a newly-allocated #connman_service structure
335  */
336 struct connman_service *connman_service_create(void)
337 {
338         struct connman_service *service;
339
340         service = g_try_new0(struct connman_service, 1);
341         if (service == NULL)
342                 return NULL;
343
344         DBG("service %p", service);
345
346         __connman_service_initialize(service);
347
348         return service;
349 }
350
351 /**
352  * connman_service_ref:
353  * @service: service structure
354  *
355  * Increase reference counter of service
356  */
357 struct connman_service *connman_service_ref(struct connman_service *service)
358 {
359         g_atomic_int_inc(&service->refcount);
360
361         return service;
362 }
363
364 /**
365  * connman_service_unref:
366  * @service: service structure
367  *
368  * Decrease reference counter of service
369  */
370 void connman_service_unref(struct connman_service *service)
371 {
372         connman_service_put(service);
373 }
374
375 static gint service_compare(gconstpointer a, gconstpointer b,
376                                                         gpointer user_data)
377 {
378         struct connman_service *service_a = (void *) a;
379         struct connman_service *service_b = (void *) b;
380
381         if (service_a->order > service_b->order)
382                 return -1;
383
384         if (service_a->order < service_b->order)
385                 return 1;
386
387         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
388                 return -1;
389
390         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
391                 return 1;
392
393         return (gint) service_b->strength - (gint) service_a->strength;
394 }
395
396 /**
397  * connman_service_set_favorite:
398  * @service: service structure
399  * @favorite: favorite value
400  *
401  * Change the favorite setting of service
402  */
403 int connman_service_set_favorite(struct connman_service *service,
404                                                 connman_bool_t favorite)
405 {
406         GSequenceIter *iter;
407
408         iter = g_hash_table_lookup(service_hash, service->identifier);
409         if (iter == NULL)
410                 return -ENOENT;
411
412         if (service->favorite)
413                 return -EALREADY;
414
415         service->favorite = favorite;
416
417         g_sequence_sort_changed(iter, service_compare, NULL);
418
419         __connman_profile_changed();
420
421         return 0;
422 }
423
424 /**
425  * connman_service_lookup:
426  * @identifier: service identifier
427  *
428  * Look up a service by identifier (reference count will not be increased)
429  */
430 struct connman_service *connman_service_lookup(const char *identifier)
431 {
432         GSequenceIter *iter;
433
434         iter = g_hash_table_lookup(service_hash, identifier);
435         if (iter != NULL)
436                 return g_sequence_get(iter);
437
438         return NULL;
439 }
440
441 /**
442  * connman_service_get:
443  * @identifier: service identifier
444  *
445  * Look up a service by identifier or create a new one if not found
446  */
447 struct connman_service *connman_service_get(const char *identifier)
448 {
449         struct connman_service *service;
450         GSequenceIter *iter;
451
452         iter = g_hash_table_lookup(service_hash, identifier);
453         if (iter != NULL) {
454                 service = g_sequence_get(iter);
455                 if (service != NULL)
456                         g_atomic_int_inc(&service->refcount);
457                 return service;
458         }
459
460         service = g_try_new0(struct connman_service, 1);
461         if (service == NULL)
462                 return NULL;
463
464         DBG("service %p", service);
465
466         __connman_service_initialize(service);
467
468         service->identifier = g_strdup(identifier);
469
470         iter = g_sequence_insert_sorted(service_list, service,
471                                                 service_compare, NULL);
472
473         g_hash_table_insert(service_hash, service->identifier, iter);
474
475         return service;
476 }
477
478 static int service_register(struct connman_service *service)
479 {
480         const char *path = __connman_profile_active();
481
482         if (service->path != NULL)
483                 return -EALREADY;
484
485         service->path = g_strdup_printf("%s/%s", path, service->identifier);
486
487         g_dbus_register_interface(connection, service->path,
488                                         CONNMAN_SERVICE_INTERFACE,
489                                         service_methods, service_signals,
490                                                         NULL, service, NULL);
491
492         __connman_profile_changed();
493
494         return 0;
495 }
496
497 /**
498  * connman_service_lookup_from_device:
499  * @device: device structure
500  *
501  * Look up a service by device (reference count will not be increased)
502  */
503 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
504 {
505         struct connman_service *service;
506         char *name;
507
508         name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
509                                         connman_device_get_index(device));
510
511         service = connman_service_lookup(name);
512
513         g_free(name);
514
515         return service;
516 }
517
518 static enum connman_service_type convert_device_type(struct connman_device *device)
519 {
520         enum connman_device_type type = connman_device_get_type(device);
521
522         switch (type) {
523         case CONNMAN_DEVICE_TYPE_UNKNOWN:
524         case CONNMAN_DEVICE_TYPE_VENDOR:
525         case CONNMAN_DEVICE_TYPE_WIFI:
526         case CONNMAN_DEVICE_TYPE_WIMAX:
527         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
528         case CONNMAN_DEVICE_TYPE_GPS:
529         case CONNMAN_DEVICE_TYPE_HSO:
530         case CONNMAN_DEVICE_TYPE_NOZOMI:
531         case CONNMAN_DEVICE_TYPE_HUAWEI:
532         case CONNMAN_DEVICE_TYPE_NOVATEL:
533                 break;
534         case CONNMAN_DEVICE_TYPE_ETHERNET:
535                 return CONNMAN_SERVICE_TYPE_ETHERNET;
536         }
537
538         return CONNMAN_SERVICE_TYPE_UNKNOWN;
539 }
540
541 /**
542  * connman_service_create_from_device:
543  * @device: device structure
544  *
545  * Look up service by device and if not found, create one
546  */
547 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
548 {
549         struct connman_service *service;
550         char *name;
551
552         name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
553                                         connman_device_get_index(device));
554
555         service = connman_service_get(name);
556         if (service == NULL)
557                 goto done;
558
559         if (service->path != NULL) {
560                 connman_service_put(service);
561                 service = NULL;
562                 goto done;
563         }
564
565         service->type = convert_device_type(device);
566
567         service->device = device;
568
569         service_register(service);
570
571 done:
572         g_free(name);
573
574         return service;
575 }
576
577 /**
578  * connman_service_lookup_from_network:
579  * @device: device structure
580  *
581  * Look up a service by network (reference count will not be increased)
582  */
583 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
584 {
585         struct connman_service *service;
586         char *name;
587
588         name = g_strdup_printf("%s_%s", __connman_network_get_type(network),
589                                         __connman_network_get_group(network));
590
591         service = connman_service_lookup(name);
592
593         g_free(name);
594
595         return service;
596 }
597
598 static enum connman_service_type convert_network_type(struct connman_network *network)
599 {
600         enum connman_network_type type = connman_network_get_type(network);
601
602         switch (type) {
603         case CONNMAN_NETWORK_TYPE_UNKNOWN:
604         case CONNMAN_NETWORK_TYPE_VENDOR:
605         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
606         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
607         case CONNMAN_NETWORK_TYPE_HSO:
608                 break;
609         case CONNMAN_NETWORK_TYPE_WIFI:
610                 return CONNMAN_SERVICE_TYPE_WIFI;
611         case CONNMAN_NETWORK_TYPE_WIMAX:
612                 return CONNMAN_SERVICE_TYPE_WIMAX;
613         }
614
615         return CONNMAN_SERVICE_TYPE_UNKNOWN;
616 }
617
618 /**
619  * connman_service_create_from_network:
620  * @device: device structure
621  *
622  * Look up service by network and if not found, create one
623  */
624 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
625 {
626         struct connman_service *service;
627         char *name;
628
629         name = g_strdup_printf("%s_%s", __connman_network_get_type(network),
630                                         __connman_network_get_group(network));
631
632         service = connman_service_get(name);
633         if (service == NULL)
634                 goto done;
635
636         if (service->path != NULL) {
637                 connman_service_put(service);
638                 service = NULL;
639                 goto done;
640         }
641
642         service->type = convert_network_type(network);
643
644         service_register(service);
645
646 done:
647         g_free(name);
648
649         return service;
650 }
651
652 int __connman_service_init(void)
653 {
654         DBG("");
655
656         connection = connman_dbus_get_connection();
657
658         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
659                                                                 NULL, NULL);
660
661         service_list = g_sequence_new(service_free);
662
663         return 0;
664 }
665
666 void __connman_service_cleanup(void)
667 {
668         DBG("");
669
670         g_sequence_free(service_list);
671         service_list = NULL;
672
673         g_hash_table_destroy(service_hash);
674         service_hash = NULL;
675
676         dbus_connection_unref(connection);
677 }