Only disconnect the specific service if asked for it
[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         char *passphrase;
48         char *profile;
49         struct connman_device *device;
50         struct connman_network *network;
51         DBusMessage *pending;
52         guint timeout;
53 };
54
55 static void append_path(gpointer value, gpointer user_data)
56 {
57         struct connman_service *service = value;
58         DBusMessageIter *iter = user_data;
59
60         if (service->path == NULL)
61                 return;
62
63         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
64                                                         &service->path);
65 }
66
67 void __connman_service_list(DBusMessageIter *iter)
68 {
69         DBG("");
70
71         g_sequence_foreach(service_list, append_path, iter);
72 }
73
74 static const char *type2string(enum connman_service_type type)
75 {
76         switch (type) {
77         case CONNMAN_SERVICE_TYPE_UNKNOWN:
78                 break;
79         case CONNMAN_SERVICE_TYPE_ETHERNET:
80                 return "ethernet";
81         case CONNMAN_SERVICE_TYPE_WIFI:
82                 return "wifi";
83         case CONNMAN_SERVICE_TYPE_WIMAX:
84                 return "wimax";
85         }
86
87         return NULL;
88 }
89
90 static const char *mode2string(enum connman_service_mode mode)
91 {
92         switch (mode) {
93         case CONNMAN_SERVICE_MODE_UNKNOWN:
94                 break;
95         case CONNMAN_SERVICE_MODE_MANAGED:
96                 return "managed";
97         case CONNMAN_SERVICE_MODE_ADHOC:
98                 return "adhoc";
99         }
100
101         return NULL;
102 }
103
104 static const char *security2string(enum connman_service_security security)
105 {
106         switch (security) {
107         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
108                 break;
109         case CONNMAN_SERVICE_SECURITY_NONE:
110                 return "none";
111         case CONNMAN_SERVICE_SECURITY_WEP:
112                 return "wep";
113         case CONNMAN_SERVICE_SECURITY_WPA:
114                 return "wpa";
115         case CONNMAN_SERVICE_SECURITY_WPA2:
116                 return "wpa2";
117         }
118
119         return NULL;
120 }
121
122 static const char *state2string(enum connman_service_state state)
123 {
124         switch (state) {
125         case CONNMAN_SERVICE_STATE_UNKNOWN:
126                 break;
127         case CONNMAN_SERVICE_STATE_IDLE:
128                 return "idle";
129         case CONNMAN_SERVICE_STATE_CARRIER:
130                 return "carrier";
131         case CONNMAN_SERVICE_STATE_ASSOCIATION:
132                 return "association";
133         case CONNMAN_SERVICE_STATE_CONFIGURATION:
134                 return "configuration";
135         case CONNMAN_SERVICE_STATE_READY:
136                 return "ready";
137         case CONNMAN_SERVICE_STATE_DISCONNECT:
138                 return "disconnect";
139         case CONNMAN_SERVICE_STATE_FAILURE:
140                 return "failure";
141         }
142
143         return NULL;
144 }
145
146 static void state_changed(struct connman_service *service)
147 {
148         DBusMessage *signal;
149         DBusMessageIter entry, value;
150         const char *str, *key = "State";
151
152         if (service->path == NULL)
153                 return;
154
155         str = state2string(service->state);
156         if (str == NULL)
157                 return;
158
159         signal = dbus_message_new_signal(service->path,
160                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
161         if (signal == NULL)
162                 return;
163
164         dbus_message_iter_init_append(signal, &entry);
165
166         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
167
168         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
169                                         DBUS_TYPE_STRING_AS_STRING, &value);
170         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
171         dbus_message_iter_close_container(&entry, &value);
172
173         g_dbus_send_message(connection, signal);
174 }
175
176 static DBusMessage *get_properties(DBusConnection *conn,
177                                         DBusMessage *msg, void *user_data)
178 {
179         struct connman_service *service = user_data;
180         DBusMessage *reply;
181         DBusMessageIter array, dict;
182         const char *str;
183
184         DBG("service %p", service);
185
186         reply = dbus_message_new_method_return(msg);
187         if (reply == NULL)
188                 return NULL;
189
190         dbus_message_iter_init_append(reply, &array);
191
192         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
193                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
194                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
195                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
196
197         str = type2string(service->type);
198         if (str != NULL)
199                 connman_dbus_dict_append_variant(&dict, "Type",
200                                                 DBUS_TYPE_STRING, &str);
201
202         str = mode2string(service->mode);
203         if (str != NULL)
204                 connman_dbus_dict_append_variant(&dict, "Mode",
205                                                 DBUS_TYPE_STRING, &str);
206
207         str = security2string(service->security);
208         if (str != NULL)
209                 connman_dbus_dict_append_variant(&dict, "Security",
210                                                 DBUS_TYPE_STRING, &str);
211
212         str = state2string(service->state);
213         if (str != NULL)
214                 connman_dbus_dict_append_variant(&dict, "State",
215                                                 DBUS_TYPE_STRING, &str);
216
217         if (service->strength > 0)
218                 connman_dbus_dict_append_variant(&dict, "Strength",
219                                         DBUS_TYPE_BYTE, &service->strength);
220
221         connman_dbus_dict_append_variant(&dict, "Favorite",
222                                         DBUS_TYPE_BOOLEAN, &service->favorite);
223
224         if (service->name != NULL)
225                 connman_dbus_dict_append_variant(&dict, "Name",
226                                         DBUS_TYPE_STRING, &service->name);
227
228         if (service->passphrase != NULL &&
229                         __connman_security_check_privilege(msg,
230                                 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
231                 connman_dbus_dict_append_variant(&dict, "Passphrase",
232                                 DBUS_TYPE_STRING, &service->passphrase);
233
234         dbus_message_iter_close_container(&array, &dict);
235
236         return reply;
237 }
238
239 static DBusMessage *set_property(DBusConnection *conn,
240                                         DBusMessage *msg, void *user_data)
241 {
242         struct connman_service *service = user_data;
243         DBusMessageIter iter, value;
244         const char *name;
245         int type;
246
247         DBG("service %p", service);
248
249         if (dbus_message_iter_init(msg, &iter) == FALSE)
250                 return __connman_error_invalid_arguments(msg);
251
252         dbus_message_iter_get_basic(&iter, &name);
253         dbus_message_iter_next(&iter);
254         dbus_message_iter_recurse(&iter, &value);
255
256         if (__connman_security_check_privilege(msg,
257                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
258                 return __connman_error_permission_denied(msg);
259
260         type = dbus_message_iter_get_arg_type(&value);
261
262         if (g_str_equal(name, "Passphrase") == TRUE) {
263                 const char *passphrase;
264
265                 if (type != DBUS_TYPE_STRING)
266                         return __connman_error_invalid_arguments(msg);
267
268                 if (__connman_security_check_privilege(msg,
269                                         CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
270                         return __connman_error_permission_denied(msg);
271
272                 dbus_message_iter_get_basic(&value, &passphrase);
273
274                 g_free(service->passphrase);
275                 service->passphrase = g_strdup(passphrase);
276
277                 if (service->network != NULL)
278                         connman_network_set_string(service->network,
279                                 "WiFi.Passphrase", service->passphrase);
280
281                 __connman_storage_save_service(service);
282         }
283
284         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
285 }
286
287 static gboolean connect_timeout(gpointer user_data)
288 {
289         struct connman_service *service = user_data;
290
291         DBG("service %p", service);
292
293         service->timeout = 0;
294
295         if (service->network != NULL)
296                 __connman_network_disconnect(service->network);
297
298         if (service->pending != NULL) {
299                 DBusMessage *reply;
300
301                 reply = __connman_error_operation_timeout(service->pending);
302                 if (reply != NULL)
303                         g_dbus_send_message(connection, reply);
304
305                 dbus_message_unref(service->pending);
306                 service->pending = NULL;
307
308                 __connman_service_indicate_state(service,
309                                         CONNMAN_SERVICE_STATE_FAILURE);
310         }
311
312         return FALSE;
313 }
314
315 static DBusMessage *connect_service(DBusConnection *conn,
316                                         DBusMessage *msg, void *user_data)
317 {
318         struct connman_service *service = user_data;
319
320         DBG("service %p", service);
321
322         if (service->pending != NULL)
323                 return __connman_error_in_progress(msg);
324
325         if (service->state == CONNMAN_SERVICE_STATE_READY)
326                 return __connman_error_already_connected(msg);
327
328         if (service->network != NULL) {
329                 int err;
330
331                 connman_network_set_string(service->network,
332                                 "WiFi.Passphrase", service->passphrase);
333
334                 err = connman_network_connect(service->network);
335                 if (err < 0 && err != -EINPROGRESS)
336                         return __connman_error_failed(msg, -err);
337
338                 service->pending = dbus_message_ref(msg);
339
340                 service->timeout = g_timeout_add_seconds(45,
341                                                 connect_timeout, service);
342
343                 return NULL;
344         } else if (service->device != NULL) {
345                 if (service->favorite == FALSE)
346                         return __connman_error_no_carrier(msg);
347
348                 if (__connman_device_connect(service->device) < 0)
349                         return __connman_error_failed(msg, EINVAL);
350
351                 service->pending = dbus_message_ref(msg);
352                 service->timeout = g_timeout_add_seconds(15,
353                                                 connect_timeout, service);
354
355                 return NULL;
356         }
357
358         return __connman_error_not_supported(msg);
359 }
360
361 static DBusMessage *disconnect_service(DBusConnection *conn,
362                                         DBusMessage *msg, void *user_data)
363 {
364         struct connman_service *service = user_data;
365
366         DBG("service %p", service);
367
368         if (service->pending != NULL) {
369                 DBusMessage *reply;
370
371                 reply = __connman_error_operation_aborted(service->pending);
372                 if (reply != NULL)
373                         g_dbus_send_message(conn, reply);
374
375                 dbus_message_unref(service->pending);
376                 service->pending = NULL;
377
378                 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
379         }
380
381         if (service->network != NULL) {
382                 int err;
383
384                 err = __connman_network_disconnect(service->network);
385                 if (err < 0 && err != -EINPROGRESS)
386                         return __connman_error_failed(msg, -err);
387
388                 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
389         } else if (service->device != NULL) {
390                 int err;
391
392                 if (service->favorite == FALSE)
393                         return __connman_error_no_carrier(msg);
394
395                 err = __connman_device_disconnect(service->device);
396                 if (err < 0)
397                         return __connman_error_failed(msg, -err);
398
399                 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
400         }
401
402         return __connman_error_not_supported(msg);
403 }
404
405 static DBusMessage *remove_service(DBusConnection *conn,
406                                         DBusMessage *msg, void *user_data)
407 {
408         struct connman_service *service = user_data;
409
410         DBG("service %p", service);
411
412         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
413                 return __connman_error_not_supported(msg);
414
415         if (service->favorite == FALSE)
416                 return __connman_error_not_supported(msg);
417
418         if (service->network != NULL) {
419                 struct connman_device *device;
420
421                 device = connman_network_get_device(service->network);
422                 if (device != NULL)
423                         __connman_device_disconnect(device);
424
425                 __connman_network_disconnect(service->network);
426         }
427
428         connman_service_set_favorite(service, FALSE);
429         __connman_storage_save_service(service);
430
431         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
432 }
433
434 static DBusMessage *move_before(DBusConnection *conn,
435                                         DBusMessage *msg, void *user_data)
436 {
437         struct connman_service *service = user_data;
438
439         DBG("service %p", service);
440
441         if (service->favorite == FALSE)
442                 return __connman_error_not_supported(msg);
443
444         return __connman_error_not_implemented(msg);
445 }
446
447 static DBusMessage *move_after(DBusConnection *conn,
448                                         DBusMessage *msg, void *user_data)
449 {
450         struct connman_service *service = user_data;
451
452         DBG("service %p", service);
453
454         if (service->favorite == FALSE)
455                 return __connman_error_not_supported(msg);
456
457         return __connman_error_not_implemented(msg);
458 }
459
460 static GDBusMethodTable service_methods[] = {
461         { "GetProperties", "",   "a{sv}", get_properties     },
462         { "SetProperty",   "sv", "",      set_property       },
463         { "Connect",       "",   "",      connect_service,
464                                                 G_DBUS_METHOD_FLAG_ASYNC },
465         { "Disconnect",    "",   "",      disconnect_service },
466         { "Remove",        "",   "",      remove_service     },
467         { "MoveBefore",    "o",  "",      move_before        },
468         { "MoveAfter",     "o",  "",      move_after         },
469         { },
470 };
471
472 static GDBusSignalTable service_signals[] = {
473         { "PropertyChanged", "sv" },
474         { },
475 };
476
477 static void service_free(gpointer user_data)
478 {
479         struct connman_service *service = user_data;
480         char *path = service->path;
481
482         DBG("service %p", service);
483
484         g_hash_table_remove(service_hash, service->identifier);
485
486         if (service->timeout > 0)
487                 g_source_remove(service->timeout);
488
489         if (service->pending != NULL) {
490                 dbus_message_unref(service->pending);
491                 service->pending = NULL;
492         }
493
494         service->path = NULL;
495
496         if (path != NULL) {
497                 __connman_profile_changed();
498
499                 g_dbus_unregister_interface(connection, path,
500                                                 CONNMAN_SERVICE_INTERFACE);
501                 g_free(path);
502         }
503
504         if (service->network != NULL)
505                 connman_network_unref(service->network);
506
507         g_free(service->profile);
508         g_free(service->name);
509         g_free(service->passphrase);
510         g_free(service->identifier);
511         g_free(service);
512 }
513
514 /**
515  * __connman_service_put:
516  * @service: service structure
517  *
518  * Release service if no longer needed
519  */
520 void __connman_service_put(struct connman_service *service)
521 {
522         DBG("service %p", service);
523
524         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
525                 GSequenceIter *iter;
526
527                 iter = g_hash_table_lookup(service_hash, service->identifier);
528                 if (iter != NULL)
529                         g_sequence_remove(iter);
530                 else
531                         service_free(service);
532         }
533 }
534
535 static void __connman_service_initialize(struct connman_service *service)
536 {
537         DBG("service %p", service);
538
539         service->refcount = 1;
540
541         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
542         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
543         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
544         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
545
546         service->favorite = FALSE;
547
548         service->order = 0;
549 }
550
551 /**
552  * connman_service_create:
553  *
554  * Allocate a new service.
555  *
556  * Returns: a newly-allocated #connman_service structure
557  */
558 struct connman_service *connman_service_create(void)
559 {
560         struct connman_service *service;
561
562         service = g_try_new0(struct connman_service, 1);
563         if (service == NULL)
564                 return NULL;
565
566         DBG("service %p", service);
567
568         __connman_service_initialize(service);
569
570         return service;
571 }
572
573 /**
574  * connman_service_ref:
575  * @service: service structure
576  *
577  * Increase reference counter of service
578  */
579 struct connman_service *connman_service_ref(struct connman_service *service)
580 {
581         g_atomic_int_inc(&service->refcount);
582
583         return service;
584 }
585
586 /**
587  * connman_service_unref:
588  * @service: service structure
589  *
590  * Decrease reference counter of service
591  */
592 void connman_service_unref(struct connman_service *service)
593 {
594         __connman_service_put(service);
595 }
596
597 static gint service_compare(gconstpointer a, gconstpointer b,
598                                                         gpointer user_data)
599 {
600         struct connman_service *service_a = (void *) a;
601         struct connman_service *service_b = (void *) b;
602
603         if (service_a->state != service_b->state) {
604                 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
605                         return -1;
606                 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
607                         return 1;
608         }
609
610         if (service_a->order > service_b->order)
611                 return -1;
612
613         if (service_a->order < service_b->order)
614                 return 1;
615
616         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
617                 return -1;
618
619         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
620                 return 1;
621
622         return (gint) service_b->strength - (gint) service_a->strength;
623 }
624
625 /**
626  * connman_service_set_favorite:
627  * @service: service structure
628  * @favorite: favorite value
629  *
630  * Change the favorite setting of service
631  */
632 int connman_service_set_favorite(struct connman_service *service,
633                                                 connman_bool_t favorite)
634 {
635         GSequenceIter *iter;
636
637         iter = g_hash_table_lookup(service_hash, service->identifier);
638         if (iter == NULL)
639                 return -ENOENT;
640
641         if (service->favorite == favorite)
642                 return -EALREADY;
643
644         service->favorite = favorite;
645
646         g_sequence_sort_changed(iter, service_compare, NULL);
647
648         __connman_profile_changed();
649
650         return 0;
651 }
652
653 int __connman_service_set_carrier(struct connman_service *service,
654                                                 connman_bool_t carrier)
655 {
656         DBG("service %p carrier %d", service, carrier);
657
658         if (service == NULL)
659                 return -EINVAL;
660
661         switch (service->type) {
662         case CONNMAN_SERVICE_TYPE_UNKNOWN:
663         case CONNMAN_SERVICE_TYPE_WIFI:
664         case CONNMAN_SERVICE_TYPE_WIMAX:
665                 return -EINVAL;
666         case CONNMAN_SERVICE_TYPE_ETHERNET:
667                 break;
668         }
669
670         if (carrier == FALSE) {
671                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
672                 state_changed(service);
673
674                 service->state = CONNMAN_SERVICE_STATE_IDLE;
675                 state_changed(service);
676         } else {
677                 service->state = CONNMAN_SERVICE_STATE_CARRIER;
678                 state_changed(service);
679         }
680
681         return connman_service_set_favorite(service, carrier);
682 }
683
684 int __connman_service_indicate_state(struct connman_service *service,
685                                         enum connman_service_state state)
686 {
687         GSequenceIter *iter;
688
689         DBG("service %p state %d", service, state);
690
691         if (service == NULL)
692                 return -EINVAL;
693
694         if (state == CONNMAN_SERVICE_STATE_CARRIER)
695                 return __connman_service_set_carrier(service, TRUE);
696
697         if (service->state == state)
698                 return -EALREADY;
699
700         if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
701                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
702                 return -EINVAL;
703
704         if (state == CONNMAN_SERVICE_STATE_IDLE &&
705                         service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
706                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
707                 state_changed(service);
708         }
709
710         service->state = state;
711         state_changed(service);
712
713         if (state == CONNMAN_SERVICE_STATE_READY) {
714                 connman_service_set_favorite(service, TRUE);
715                 __connman_storage_save_service(service);
716
717                 if (service->timeout > 0)
718                         g_source_remove(service->timeout);
719
720                 if (service->pending != NULL) {
721                         g_dbus_send_reply(connection, service->pending,
722                                                         DBUS_TYPE_INVALID);
723
724                         dbus_message_unref(service->pending);
725                         service->pending = NULL;
726                 }
727         }
728
729         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
730                 if (service->timeout > 0)
731                         g_source_remove(service->timeout);
732
733                 if (service->pending != NULL) {
734                         DBusMessage *reply;
735
736                         reply = __connman_error_failed(service->pending, EIO);
737                         if (reply != NULL)
738                                 g_dbus_send_message(connection, reply);
739
740                         dbus_message_unref(service->pending);
741                         service->pending = NULL;
742                 }
743
744                 service->state = CONNMAN_SERVICE_STATE_IDLE;
745                 state_changed(service);
746         }
747
748         iter = g_hash_table_lookup(service_hash, service->identifier);
749         if (iter != NULL)
750                 g_sequence_sort_changed(iter, service_compare, NULL);
751
752         __connman_profile_changed();
753
754         return 0;
755 }
756
757 /**
758  * __connman_service_lookup:
759  * @identifier: service identifier
760  *
761  * Look up a service by identifier (reference count will not be increased)
762  */
763 static struct connman_service *__connman_service_lookup(const char *identifier)
764 {
765         GSequenceIter *iter;
766
767         iter = g_hash_table_lookup(service_hash, identifier);
768         if (iter != NULL)
769                 return g_sequence_get(iter);
770
771         return NULL;
772 }
773
774 /**
775  * __connman_service_get:
776  * @identifier: service identifier
777  *
778  * Look up a service by identifier or create a new one if not found
779  */
780 static struct connman_service *__connman_service_get(const char *identifier)
781 {
782         struct connman_service *service;
783         GSequenceIter *iter;
784
785         iter = g_hash_table_lookup(service_hash, identifier);
786         if (iter != NULL) {
787                 service = g_sequence_get(iter);
788                 if (service != NULL)
789                         g_atomic_int_inc(&service->refcount);
790                 return service;
791         }
792
793         service = g_try_new0(struct connman_service, 1);
794         if (service == NULL)
795                 return NULL;
796
797         DBG("service %p", service);
798
799         __connman_service_initialize(service);
800
801         service->identifier = g_strdup(identifier);
802
803         service->profile = g_strdup(__connman_profile_active_ident());
804
805         __connman_storage_load_service(service);
806
807         iter = g_sequence_insert_sorted(service_list, service,
808                                                 service_compare, NULL);
809
810         g_hash_table_insert(service_hash, service->identifier, iter);
811
812         return service;
813 }
814
815 static int service_register(struct connman_service *service)
816 {
817         const char *path = __connman_profile_active_path();
818         GSequenceIter *iter;
819
820         DBG("service %p", service);
821
822         if (service->path != NULL)
823                 return -EALREADY;
824
825         service->path = g_strdup_printf("%s/%s", path, service->identifier);
826
827         DBG("path %s", service->path);
828
829         g_dbus_register_interface(connection, service->path,
830                                         CONNMAN_SERVICE_INTERFACE,
831                                         service_methods, service_signals,
832                                                         NULL, service, NULL);
833
834         __connman_storage_load_service(service);
835
836         iter = g_hash_table_lookup(service_hash, service->identifier);
837         if (iter != NULL)
838                 g_sequence_sort_changed(iter, service_compare, NULL);
839
840         __connman_profile_changed();
841
842         return 0;
843 }
844
845 /**
846  * connman_service_lookup_from_device:
847  * @device: device structure
848  *
849  * Look up a service by device (reference count will not be increased)
850  */
851 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
852 {
853         struct connman_service *service;
854         const char *ident;
855         char *name;
856
857         ident = __connman_device_get_ident(device);
858         if (ident == NULL)
859                 return NULL;
860
861         name = g_strdup_printf("%s_%s",
862                                 __connman_device_get_type(device), ident);
863
864         service = __connman_service_lookup(name);
865
866         g_free(name);
867
868         return service;
869 }
870
871 static enum connman_service_type convert_device_type(struct connman_device *device)
872 {
873         enum connman_device_type type = connman_device_get_type(device);
874
875         switch (type) {
876         case CONNMAN_DEVICE_TYPE_UNKNOWN:
877         case CONNMAN_DEVICE_TYPE_VENDOR:
878         case CONNMAN_DEVICE_TYPE_WIFI:
879         case CONNMAN_DEVICE_TYPE_WIMAX:
880         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
881         case CONNMAN_DEVICE_TYPE_GPS:
882         case CONNMAN_DEVICE_TYPE_HSO:
883         case CONNMAN_DEVICE_TYPE_NOZOMI:
884         case CONNMAN_DEVICE_TYPE_HUAWEI:
885         case CONNMAN_DEVICE_TYPE_NOVATEL:
886                 break;
887         case CONNMAN_DEVICE_TYPE_ETHERNET:
888                 return CONNMAN_SERVICE_TYPE_ETHERNET;
889         }
890
891         return CONNMAN_SERVICE_TYPE_UNKNOWN;
892 }
893
894 /**
895  * connman_service_create_from_device:
896  * @device: device structure
897  *
898  * Look up service by device and if not found, create one
899  */
900 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
901 {
902         struct connman_service *service;
903         const char *ident;
904         char *name;
905
906         ident = __connman_device_get_ident(device);
907         if (ident == NULL)
908                 return NULL;
909
910         name = g_strdup_printf("%s_%s",
911                                 __connman_device_get_type(device), ident);
912
913         service = __connman_service_get(name);
914         if (service == NULL)
915                 goto done;
916
917         if (service->path != NULL) {
918                 __connman_service_put(service);
919                 service = NULL;
920                 goto done;
921         }
922
923         service->type = convert_device_type(device);
924
925         service->device = device;
926
927         service_register(service);
928
929 done:
930         g_free(name);
931
932         return service;
933 }
934
935 /**
936  * connman_service_lookup_from_network:
937  * @network: network structure
938  *
939  * Look up a service by network (reference count will not be increased)
940  */
941 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
942 {
943         struct connman_service *service;
944         const char *ident, *group;
945         char *name;
946
947         ident = __connman_network_get_ident(network);
948         if (ident == NULL)
949                 return NULL;
950
951         group = __connman_network_get_group(network);
952         if (group == NULL)
953                 return NULL;
954
955         name = g_strdup_printf("%s_%s_%s",
956                         __connman_network_get_type(network), ident, group);
957
958         service = __connman_service_lookup(name);
959
960         g_free(name);
961
962         return service;
963 }
964
965 static enum connman_service_type convert_network_type(struct connman_network *network)
966 {
967         enum connman_network_type type = connman_network_get_type(network);
968
969         switch (type) {
970         case CONNMAN_NETWORK_TYPE_UNKNOWN:
971         case CONNMAN_NETWORK_TYPE_VENDOR:
972         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
973         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
974         case CONNMAN_NETWORK_TYPE_HSO:
975                 break;
976         case CONNMAN_NETWORK_TYPE_WIFI:
977                 return CONNMAN_SERVICE_TYPE_WIFI;
978         case CONNMAN_NETWORK_TYPE_WIMAX:
979                 return CONNMAN_SERVICE_TYPE_WIMAX;
980         }
981
982         return CONNMAN_SERVICE_TYPE_UNKNOWN;
983 }
984
985 static enum connman_service_mode convert_wifi_mode(const char *mode)
986 {
987         if (mode == NULL)
988                 return CONNMAN_SERVICE_MODE_UNKNOWN;
989         else if (g_str_equal(mode, "managed") == TRUE)
990                 return CONNMAN_SERVICE_MODE_MANAGED;
991         else if (g_str_equal(mode, "adhoc") == TRUE)
992                 return CONNMAN_SERVICE_MODE_ADHOC;
993         else
994                 return CONNMAN_SERVICE_MODE_UNKNOWN;
995 }
996
997 static enum connman_service_mode convert_wifi_security(const char *security)
998 {
999         if (security == NULL)
1000                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1001         else if (g_str_equal(security, "none") == TRUE)
1002                 return CONNMAN_SERVICE_SECURITY_NONE;
1003         else if (g_str_equal(security, "wep") == TRUE)
1004                 return CONNMAN_SERVICE_SECURITY_WEP;
1005         else if (g_str_equal(security, "wpa") == TRUE)
1006                 return CONNMAN_SERVICE_SECURITY_WPA;
1007         else if (g_str_equal(security, "wpa2") == TRUE)
1008                 return CONNMAN_SERVICE_SECURITY_WPA2;
1009         else
1010                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1011 }
1012
1013 static void update_from_network(struct connman_service *service,
1014                                         struct connman_network *network)
1015 {
1016         connman_uint8_t strength = service->strength;
1017         GSequenceIter *iter;
1018         const char *str;
1019
1020         str = connman_network_get_string(network, "Name");
1021         if (str != NULL) {
1022                 g_free(service->name);
1023                 service->name = g_strdup(str);
1024         }
1025
1026         service->strength = connman_network_get_uint8(network, "Strength");
1027
1028         str = connman_network_get_string(network, "WiFi.Mode");
1029         service->mode = convert_wifi_mode(str);
1030
1031         str = connman_network_get_string(network, "WiFi.Security");
1032         service->security = convert_wifi_security(str);
1033
1034         if (service->strength > strength && service->network != NULL) {
1035                 connman_network_unref(service->network);
1036                 service->network = NULL;
1037         }
1038
1039         if (service->network == NULL) {
1040                 service->network = connman_network_ref(network);
1041
1042                 str = connman_network_get_string(network, "WiFi.Passphrase");
1043                 if (str != NULL) {
1044                         g_free(service->passphrase);
1045                         service->passphrase = g_strdup(str);
1046                 }
1047         }
1048
1049         iter = g_hash_table_lookup(service_hash, service->identifier);
1050         if (iter != NULL)
1051                 g_sequence_sort_changed(iter, service_compare, NULL);
1052 }
1053
1054 /**
1055  * connman_service_create_from_network:
1056  * @network: network structure
1057  *
1058  * Look up service by network and if not found, create one
1059  */
1060 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1061 {
1062         struct connman_service *service;
1063         const char *ident, *group;
1064         char *name;
1065
1066         ident = __connman_network_get_ident(network);
1067         if (ident == NULL)
1068                 return NULL;
1069
1070         group = __connman_network_get_group(network);
1071         if (group == NULL)
1072                 return NULL;
1073
1074         name = g_strdup_printf("%s_%s_%s",
1075                         __connman_network_get_type(network), ident, group);
1076
1077         service = __connman_service_get(name);
1078         if (service == NULL)
1079                 goto done;
1080
1081         if (service->path != NULL) {
1082                 update_from_network(service, network);
1083
1084                 __connman_profile_changed();
1085
1086                 __connman_service_put(service);
1087                 service = NULL;
1088                 goto done;
1089         }
1090
1091         service->type = convert_network_type(network);
1092
1093         service->state = CONNMAN_SERVICE_STATE_IDLE;
1094
1095         update_from_network(service, network);
1096
1097         service_register(service);
1098
1099 done:
1100         g_free(name);
1101
1102         return service;
1103 }
1104
1105 static int service_load(struct connman_service *service)
1106 {
1107         GKeyFile *keyfile;
1108         gchar *pathname, *data = NULL;
1109         gsize length;
1110         char *str;
1111
1112         DBG("service %p", service);
1113
1114         if (service->profile == NULL)
1115                 return -EINVAL;
1116
1117         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1118         if (pathname == NULL)
1119                 return -ENOMEM;
1120
1121         keyfile = g_key_file_new();
1122
1123         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1124                 g_free(pathname);
1125                 return -ENOENT;
1126         }
1127
1128         g_free(pathname);
1129
1130         if (g_key_file_load_from_data(keyfile, data, length,
1131                                                         0, NULL) == FALSE) {
1132                 g_free(data);
1133                 return -EILSEQ;
1134         }
1135
1136         g_free(data);
1137
1138         switch (service->type) {
1139         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1140         case CONNMAN_SERVICE_TYPE_ETHERNET:
1141                 break;
1142         case CONNMAN_SERVICE_TYPE_WIFI:
1143         case CONNMAN_SERVICE_TYPE_WIMAX:
1144                 service->favorite = g_key_file_get_boolean(keyfile,
1145                                 service->identifier, "Favorite", NULL);
1146                 break;
1147         }
1148
1149         str = g_key_file_get_string(keyfile,
1150                                 service->identifier, "Passphrase", NULL);
1151         if (str != NULL) {
1152                 g_free(service->passphrase);
1153                 service->passphrase = str;
1154         }
1155
1156         g_key_file_free(keyfile);
1157
1158         return 0;
1159 }
1160
1161 static int service_save(struct connman_service *service)
1162 {
1163         GKeyFile *keyfile;
1164         gchar *pathname, *data = NULL;
1165         gsize length;
1166
1167         DBG("service %p", service);
1168
1169         if (service->profile == NULL)
1170                 return -EINVAL;
1171
1172         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1173         if (pathname == NULL)
1174                 return -ENOMEM;
1175
1176         keyfile = g_key_file_new();
1177
1178         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1179                 goto update;
1180
1181         if (length > 0) {
1182                 if (g_key_file_load_from_data(keyfile, data, length,
1183                                                         0, NULL) == FALSE)
1184                         goto done;
1185         }
1186
1187         g_free(data);
1188
1189 update:
1190         if (service->name != NULL)
1191                 g_key_file_set_string(keyfile, service->identifier,
1192                                                 "Name", service->name);
1193
1194         switch (service->type) {
1195         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1196         case CONNMAN_SERVICE_TYPE_ETHERNET:
1197                 break;
1198         case CONNMAN_SERVICE_TYPE_WIFI:
1199         case CONNMAN_SERVICE_TYPE_WIMAX:
1200                 g_key_file_set_boolean(keyfile, service->identifier,
1201                                         "Favorite", service->favorite);
1202                 break;
1203         }
1204
1205         if (service->passphrase != NULL)
1206                 g_key_file_set_string(keyfile, service->identifier,
1207                                         "Passphrase", service->passphrase);
1208
1209         data = g_key_file_to_data(keyfile, &length, NULL);
1210
1211         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1212                 connman_error("Failed to store service information");
1213
1214 done:
1215         g_free(data);
1216
1217         g_key_file_free(keyfile);
1218
1219         g_free(pathname);
1220
1221         return 0;
1222 }
1223
1224 static struct connman_storage service_storage = {
1225         .name           = "service",
1226         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1227         .service_load   = service_load,
1228         .service_save   = service_save,
1229 };
1230
1231 int __connman_service_init(void)
1232 {
1233         DBG("");
1234
1235         connection = connman_dbus_get_connection();
1236
1237         if (connman_storage_register(&service_storage) < 0)
1238                 connman_error("Failed to register service storage");
1239
1240         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1241                                                                 NULL, NULL);
1242
1243         service_list = g_sequence_new(service_free);
1244
1245         return 0;
1246 }
1247
1248 void __connman_service_cleanup(void)
1249 {
1250         DBG("");
1251
1252         g_sequence_free(service_list);
1253         service_list = NULL;
1254
1255         g_hash_table_destroy(service_hash);
1256         service_hash = NULL;
1257
1258         connman_storage_unregister(&service_storage);
1259
1260         dbus_connection_unref(connection);
1261 }