Trigger auto-connect when new service is added
[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         enum connman_service_error error;
44         connman_uint8_t strength;
45         connman_bool_t favorite;
46         connman_bool_t hidden;
47         GTimeVal modified;
48         unsigned int order;
49         char *name;
50         char *passphrase;
51         char *profile;
52         struct connman_ipconfig *ipconfig;
53         struct connman_device *device;
54         struct connman_network *network;
55         DBusMessage *pending;
56         guint timeout;
57 };
58
59 static void append_path(gpointer value, gpointer user_data)
60 {
61         struct connman_service *service = value;
62         DBusMessageIter *iter = user_data;
63
64         if (service->path == NULL)
65                 return;
66
67         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
68                                                         &service->path);
69 }
70
71 void __connman_service_list(DBusMessageIter *iter)
72 {
73         DBG("");
74
75         g_sequence_foreach(service_list, append_path, iter);
76 }
77
78 struct find_data {
79         const char *path;
80         struct connman_service *service;
81 };
82
83 static void compare_path(gpointer value, gpointer user_data)
84 {
85         struct connman_service *service = value;
86         struct find_data *data = user_data;
87
88         if (data->service != NULL)
89                 return;
90
91         if (g_strcmp0(service->path, data->path) == 0)
92                 data->service = service;
93 }
94
95 static struct connman_service *find_service(const char *path)
96 {
97         struct find_data data = { .path = path, .service = NULL };
98
99         DBG("path %s", path);
100
101         g_sequence_foreach(service_list, compare_path, &data);
102
103         return data.service;
104 }
105
106 static const char *type2string(enum connman_service_type type)
107 {
108         switch (type) {
109         case CONNMAN_SERVICE_TYPE_UNKNOWN:
110                 break;
111         case CONNMAN_SERVICE_TYPE_ETHERNET:
112                 return "ethernet";
113         case CONNMAN_SERVICE_TYPE_WIFI:
114                 return "wifi";
115         case CONNMAN_SERVICE_TYPE_WIMAX:
116                 return "wimax";
117         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
118                 return "bluetooth";
119         case CONNMAN_SERVICE_TYPE_CELLULAR:
120                 return "cellular";
121         }
122
123         return NULL;
124 }
125
126 static const char *mode2string(enum connman_service_mode mode)
127 {
128         switch (mode) {
129         case CONNMAN_SERVICE_MODE_UNKNOWN:
130                 break;
131         case CONNMAN_SERVICE_MODE_MANAGED:
132                 return "managed";
133         case CONNMAN_SERVICE_MODE_ADHOC:
134                 return "adhoc";
135         }
136
137         return NULL;
138 }
139
140 static const char *security2string(enum connman_service_security security)
141 {
142         switch (security) {
143         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
144                 break;
145         case CONNMAN_SERVICE_SECURITY_NONE:
146                 return "none";
147         case CONNMAN_SERVICE_SECURITY_WEP:
148                 return "wep";
149         case CONNMAN_SERVICE_SECURITY_WPA:
150                 return "wpa";
151         case CONNMAN_SERVICE_SECURITY_RSN:
152                 return "rsn";
153         }
154
155         return NULL;
156 }
157
158 static const char *state2string(enum connman_service_state state)
159 {
160         switch (state) {
161         case CONNMAN_SERVICE_STATE_UNKNOWN:
162                 break;
163         case CONNMAN_SERVICE_STATE_IDLE:
164                 return "idle";
165         case CONNMAN_SERVICE_STATE_CARRIER:
166                 return "carrier";
167         case CONNMAN_SERVICE_STATE_ASSOCIATION:
168                 return "association";
169         case CONNMAN_SERVICE_STATE_CONFIGURATION:
170                 return "configuration";
171         case CONNMAN_SERVICE_STATE_READY:
172                 return "ready";
173         case CONNMAN_SERVICE_STATE_DISCONNECT:
174                 return "disconnect";
175         case CONNMAN_SERVICE_STATE_FAILURE:
176                 return "failure";
177         }
178
179         return NULL;
180 }
181
182 static const char *error2string(enum connman_service_error error)
183 {
184         switch (error) {
185         case CONNMAN_SERVICE_ERROR_UNKNOWN:
186                 break;
187         case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
188                 return "dhcp-failed";
189         case CONNMAN_SERVICE_ERROR_PIN_MISSING:
190                 return "pin-missing";
191         }
192
193         return NULL;
194 }
195
196 static enum connman_service_error string2error(const char *error)
197 {
198         if (g_strcmp0(error, "dhcp-failed") == 0)
199                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
200         else if (g_strcmp0(error, "pin-missing") == 0)
201                 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
202
203         return CONNMAN_SERVICE_ERROR_UNKNOWN;
204 }
205
206 static void state_changed(struct connman_service *service)
207 {
208         DBusMessage *signal;
209         DBusMessageIter entry, value;
210         const char *str, *key = "State";
211
212         if (service->path == NULL)
213                 return;
214
215         str = state2string(service->state);
216         if (str == NULL)
217                 return;
218
219         signal = dbus_message_new_signal(service->path,
220                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
221         if (signal == NULL)
222                 return;
223
224         dbus_message_iter_init_append(signal, &entry);
225
226         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
227
228         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
229                                         DBUS_TYPE_STRING_AS_STRING, &value);
230         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
231         dbus_message_iter_close_container(&entry, &value);
232
233         g_dbus_send_message(connection, signal);
234 }
235
236 static void strength_changed(struct connman_service *service)
237 {
238         DBusMessage *signal;
239         DBusMessageIter entry, value;
240         const char *key = "Strength";
241
242         if (service->path == NULL)
243                 return;
244
245         if (service->strength == 0)
246                 return;
247
248         signal = dbus_message_new_signal(service->path,
249                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
250         if (signal == NULL)
251                 return;
252
253         dbus_message_iter_init_append(signal, &entry);
254
255         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
256
257         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
258                                         DBUS_TYPE_BYTE_AS_STRING, &value);
259         dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
260                                                         &service->strength);
261         dbus_message_iter_close_container(&entry, &value);
262
263         g_dbus_send_message(connection, signal);
264 }
265
266 static DBusMessage *get_properties(DBusConnection *conn,
267                                         DBusMessage *msg, void *user_data)
268 {
269         struct connman_service *service = user_data;
270         DBusMessage *reply;
271         DBusMessageIter array, dict;
272         const char *str;
273
274         DBG("service %p", service);
275
276         reply = dbus_message_new_method_return(msg);
277         if (reply == NULL)
278                 return NULL;
279
280         dbus_message_iter_init_append(reply, &array);
281
282         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
283                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
284                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
285                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
286
287         str = type2string(service->type);
288         if (str != NULL)
289                 connman_dbus_dict_append_variant(&dict, "Type",
290                                                 DBUS_TYPE_STRING, &str);
291
292         str = mode2string(service->mode);
293         if (str != NULL)
294                 connman_dbus_dict_append_variant(&dict, "Mode",
295                                                 DBUS_TYPE_STRING, &str);
296
297         str = security2string(service->security);
298         if (str != NULL)
299                 connman_dbus_dict_append_variant(&dict, "Security",
300                                                 DBUS_TYPE_STRING, &str);
301
302         str = state2string(service->state);
303         if (str != NULL)
304                 connman_dbus_dict_append_variant(&dict, "State",
305                                                 DBUS_TYPE_STRING, &str);
306
307         str = error2string(service->error);
308         if (str != NULL)
309                 connman_dbus_dict_append_variant(&dict, "Error",
310                                                 DBUS_TYPE_STRING, &str);
311
312         if (service->strength > 0)
313                 connman_dbus_dict_append_variant(&dict, "Strength",
314                                         DBUS_TYPE_BYTE, &service->strength);
315
316         connman_dbus_dict_append_variant(&dict, "Favorite",
317                                         DBUS_TYPE_BOOLEAN, &service->favorite);
318
319         if (service->name != NULL)
320                 connman_dbus_dict_append_variant(&dict, "Name",
321                                         DBUS_TYPE_STRING, &service->name);
322
323         if (service->passphrase != NULL &&
324                         __connman_security_check_privilege(msg,
325                                 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
326                 connman_dbus_dict_append_variant(&dict, "Passphrase",
327                                 DBUS_TYPE_STRING, &service->passphrase);
328
329         __connman_ipconfig_append_ipv4(service->ipconfig, &dict, "IPv4.");
330
331         dbus_message_iter_close_container(&array, &dict);
332
333         return reply;
334 }
335
336 static DBusMessage *set_property(DBusConnection *conn,
337                                         DBusMessage *msg, void *user_data)
338 {
339         struct connman_service *service = user_data;
340         DBusMessageIter iter, value;
341         const char *name;
342         int type;
343
344         DBG("service %p", service);
345
346         if (dbus_message_iter_init(msg, &iter) == FALSE)
347                 return __connman_error_invalid_arguments(msg);
348
349         dbus_message_iter_get_basic(&iter, &name);
350         dbus_message_iter_next(&iter);
351         dbus_message_iter_recurse(&iter, &value);
352
353         if (__connman_security_check_privilege(msg,
354                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
355                 return __connman_error_permission_denied(msg);
356
357         type = dbus_message_iter_get_arg_type(&value);
358
359         if (g_str_equal(name, "Passphrase") == TRUE) {
360                 const char *passphrase;
361
362                 if (type != DBUS_TYPE_STRING)
363                         return __connman_error_invalid_arguments(msg);
364
365                 if (__connman_security_check_privilege(msg,
366                                         CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
367                         return __connman_error_permission_denied(msg);
368
369                 dbus_message_iter_get_basic(&value, &passphrase);
370
371                 g_free(service->passphrase);
372                 service->passphrase = g_strdup(passphrase);
373
374                 if (service->network != NULL)
375                         connman_network_set_string(service->network,
376                                 "WiFi.Passphrase", service->passphrase);
377
378                 __connman_storage_save_service(service);
379         } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
380                 int err;
381
382                 err = __connman_ipconfig_set_ipv4(service->ipconfig,
383                                                         name + 5, &value);
384                 if (err < 0)
385                         return __connman_error_failed(msg, -err);
386         } else
387                 return __connman_error_invalid_property(msg);
388
389         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
390 }
391
392 static DBusMessage *clear_property(DBusConnection *conn,
393                                         DBusMessage *msg, void *user_data)
394 {
395         struct connman_service *service = user_data;
396         const char *name;
397
398         DBG("service %p", service);
399
400         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
401                                                         DBUS_TYPE_INVALID);
402
403         if (__connman_security_check_privilege(msg,
404                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
405                 return __connman_error_permission_denied(msg);
406
407         if (g_str_equal(name, "Error") == TRUE) {
408                 service->state = CONNMAN_SERVICE_STATE_IDLE;
409                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
410                 state_changed(service);
411
412                 g_get_current_time(&service->modified);
413                 __connman_storage_save_service(service);
414         } else
415                 return __connman_error_invalid_property(msg);
416
417         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
418 }
419
420 static gboolean connect_timeout(gpointer user_data)
421 {
422         struct connman_service *service = user_data;
423
424         DBG("service %p", service);
425
426         service->timeout = 0;
427
428         if (service->network != NULL) {
429                 connman_bool_t connected;
430
431                 connected = connman_network_get_connected(service->network);
432                 if (connected == TRUE) {
433                         __connman_service_indicate_state(service,
434                                                 CONNMAN_SERVICE_STATE_READY);
435                         return FALSE;
436                 }
437
438                 __connman_network_disconnect(service->network);
439         } else if (service->device != NULL) {
440                 connman_bool_t disconnected;
441
442                 disconnected = connman_device_get_disconnected(service->device);
443                 if (disconnected == FALSE) {
444                         __connman_service_indicate_state(service,
445                                                 CONNMAN_SERVICE_STATE_READY);
446                         return FALSE;
447                 }
448
449                 __connman_device_disconnect(service->device);
450         }
451
452         if (service->pending != NULL) {
453                 DBusMessage *reply;
454
455                 reply = __connman_error_operation_timeout(service->pending);
456                 if (reply != NULL)
457                         g_dbus_send_message(connection, reply);
458
459                 dbus_message_unref(service->pending);
460                 service->pending = NULL;
461         }
462
463         __connman_service_indicate_state(service,
464                                         CONNMAN_SERVICE_STATE_FAILURE);
465
466         return FALSE;
467 }
468
469 static DBusMessage *connect_service(DBusConnection *conn,
470                                         DBusMessage *msg, void *user_data)
471 {
472         struct connman_service *service = user_data;
473         int err;
474
475         DBG("service %p", service);
476
477         if (service->pending != NULL)
478                 return __connman_error_in_progress(msg);
479
480         service->pending = dbus_message_ref(msg);
481
482         err = __connman_service_connect(service);
483         if (err < 0) {
484                 if (err != -EINPROGRESS) {
485                         dbus_message_unref(service->pending);
486                         service->pending = NULL;
487
488                         return __connman_error_failed(msg, -err);
489                 }
490
491                 return NULL;
492         }
493
494         dbus_message_unref(service->pending);
495         service->pending = NULL;
496
497         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
498 }
499
500 static DBusMessage *disconnect_service(DBusConnection *conn,
501                                         DBusMessage *msg, void *user_data)
502 {
503         struct connman_service *service = user_data;
504         int err;
505
506         DBG("service %p", service);
507
508         if (service->pending != NULL) {
509                 DBusMessage *reply;
510
511                 reply = __connman_error_operation_aborted(service->pending);
512                 if (reply != NULL)
513                         g_dbus_send_message(conn, reply);
514
515                 dbus_message_unref(service->pending);
516                 service->pending = NULL;
517
518                 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
519         }
520
521         err = __connman_service_disconnect(service);
522         if (err < 0) {
523                 if (err != -EINPROGRESS)
524                         return __connman_error_failed(msg, -err);
525
526                 return NULL;
527         }
528
529         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
530 }
531
532 static DBusMessage *remove_service(DBusConnection *conn,
533                                         DBusMessage *msg, void *user_data)
534 {
535         struct connman_service *service = user_data;
536
537         DBG("service %p", service);
538
539         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
540                 return __connman_error_not_supported(msg);
541
542         if (service->favorite == FALSE)
543                 return __connman_error_not_supported(msg);
544
545         if (service->network != NULL)
546                 __connman_network_disconnect(service->network);
547
548         g_free(service->passphrase);
549         service->passphrase = NULL;
550
551         connman_service_set_favorite(service, FALSE);
552         __connman_storage_save_service(service);
553
554         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
555 }
556
557 static DBusMessage *move_before(DBusConnection *conn,
558                                         DBusMessage *msg, void *user_data)
559 {
560         struct connman_service *service = user_data;
561         struct connman_service *target;
562         const char *path;
563         GSequenceIter *src, *dst;
564
565         DBG("service %p", service);
566
567         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
568                                                         DBUS_TYPE_INVALID);
569
570         if (service->favorite == FALSE)
571                 return __connman_error_not_supported(msg);
572
573         target = find_service(path);
574         if (target == NULL || target->favorite == FALSE || target == service)
575                 return __connman_error_invalid_service(msg);
576
577         DBG("target %s", target->identifier);
578
579         g_get_current_time(&service->modified);
580         __connman_storage_save_service(service);
581
582         src = g_hash_table_lookup(service_hash, service->identifier);
583         dst = g_hash_table_lookup(service_hash, target->identifier);
584
585 #if 0
586         g_sequence_move(src, dst);
587
588         __connman_profile_changed();
589
590         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
591 #endif
592         return __connman_error_not_implemented(msg);
593 }
594
595 static DBusMessage *move_after(DBusConnection *conn,
596                                         DBusMessage *msg, void *user_data)
597 {
598         struct connman_service *service = user_data;
599         struct connman_service *target;
600         const char *path;
601
602         DBG("service %p", service);
603
604         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
605                                                         DBUS_TYPE_INVALID);
606
607         if (service->favorite == FALSE)
608                 return __connman_error_not_supported(msg);
609
610         target = find_service(path);
611         if (target == NULL || target->favorite == FALSE || target == service)
612                 return __connman_error_invalid_service(msg);
613
614         DBG("target %s", target->identifier);
615
616         g_get_current_time(&service->modified);
617         __connman_storage_save_service(service);
618
619         return __connman_error_not_implemented(msg);
620 }
621
622 static GDBusMethodTable service_methods[] = {
623         { "GetProperties", "",   "a{sv}", get_properties     },
624         { "SetProperty",   "sv", "",      set_property       },
625         { "ClearProperty", "s",  "",      clear_property     },
626         { "Connect",       "",   "",      connect_service,
627                                                 G_DBUS_METHOD_FLAG_ASYNC },
628         { "Disconnect",    "",   "",      disconnect_service },
629         { "Remove",        "",   "",      remove_service     },
630         { "MoveBefore",    "o",  "",      move_before        },
631         { "MoveAfter",     "o",  "",      move_after         },
632         { },
633 };
634
635 static GDBusSignalTable service_signals[] = {
636         { "PropertyChanged", "sv" },
637         { },
638 };
639
640 static void service_free(gpointer user_data)
641 {
642         struct connman_service *service = user_data;
643         char *path = service->path;
644
645         DBG("service %p", service);
646
647         g_hash_table_remove(service_hash, service->identifier);
648
649         if (service->timeout > 0) {
650                 g_source_remove(service->timeout);
651                 service->timeout = 0;
652         }
653
654         if (service->pending != NULL) {
655                 dbus_message_unref(service->pending);
656                 service->pending = NULL;
657         }
658
659         service->path = NULL;
660
661         if (path != NULL) {
662                 __connman_profile_changed();
663
664                 g_dbus_unregister_interface(connection, path,
665                                                 CONNMAN_SERVICE_INTERFACE);
666                 g_free(path);
667         }
668
669         if (service->network != NULL)
670                 connman_network_unref(service->network);
671
672         connman_ipconfig_unref(service->ipconfig);
673
674         g_free(service->profile);
675         g_free(service->name);
676         g_free(service->passphrase);
677         g_free(service->identifier);
678         g_free(service);
679 }
680
681 /**
682  * __connman_service_put:
683  * @service: service structure
684  *
685  * Release service if no longer needed
686  */
687 void __connman_service_put(struct connman_service *service)
688 {
689         DBG("service %p", service);
690
691         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
692                 GSequenceIter *iter;
693
694                 iter = g_hash_table_lookup(service_hash, service->identifier);
695                 if (iter != NULL)
696                         g_sequence_remove(iter);
697                 else
698                         service_free(service);
699         }
700 }
701
702 static void __connman_service_initialize(struct connman_service *service)
703 {
704         DBG("service %p", service);
705
706         service->refcount = 1;
707
708         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
709         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
710         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
711         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
712
713         service->favorite = FALSE;
714         service->hidden = FALSE;
715
716         service->order = 0;
717 }
718
719 /**
720  * connman_service_create:
721  *
722  * Allocate a new service.
723  *
724  * Returns: a newly-allocated #connman_service structure
725  */
726 struct connman_service *connman_service_create(void)
727 {
728         struct connman_service *service;
729
730         service = g_try_new0(struct connman_service, 1);
731         if (service == NULL)
732                 return NULL;
733
734         DBG("service %p", service);
735
736         __connman_service_initialize(service);
737
738         service->ipconfig = connman_ipconfig_create();
739         if (service->ipconfig == NULL) {
740                 g_free(service);
741                 return NULL;
742         }
743
744         connman_ipconfig_set_method(service->ipconfig,
745                                         CONNMAN_IPCONFIG_METHOD_DHCP);
746
747         return service;
748 }
749
750 /**
751  * connman_service_ref:
752  * @service: service structure
753  *
754  * Increase reference counter of service
755  */
756 struct connman_service *connman_service_ref(struct connman_service *service)
757 {
758         g_atomic_int_inc(&service->refcount);
759
760         return service;
761 }
762
763 /**
764  * connman_service_unref:
765  * @service: service structure
766  *
767  * Decrease reference counter of service
768  */
769 void connman_service_unref(struct connman_service *service)
770 {
771         __connman_service_put(service);
772 }
773
774 static gint service_compare(gconstpointer a, gconstpointer b,
775                                                         gpointer user_data)
776 {
777         struct connman_service *service_a = (void *) a;
778         struct connman_service *service_b = (void *) b;
779
780         if (service_a->state != service_b->state) {
781                 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
782                         return -1;
783                 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
784                         return 1;
785         }
786
787         if (service_a->order > service_b->order)
788                 return -1;
789
790         if (service_a->order < service_b->order)
791                 return 1;
792
793         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
794                 return -1;
795
796         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
797                 return 1;
798
799         return (gint) service_b->strength - (gint) service_a->strength;
800 }
801
802 /**
803  * connman_service_set_favorite:
804  * @service: service structure
805  * @favorite: favorite value
806  *
807  * Change the favorite setting of service
808  */
809 int connman_service_set_favorite(struct connman_service *service,
810                                                 connman_bool_t favorite)
811 {
812         GSequenceIter *iter;
813
814         iter = g_hash_table_lookup(service_hash, service->identifier);
815         if (iter == NULL)
816                 return -ENOENT;
817
818         if (service->favorite == favorite)
819                 return -EALREADY;
820
821         service->favorite = favorite;
822
823         g_sequence_sort_changed(iter, service_compare, NULL);
824
825         __connman_profile_changed();
826
827         return 0;
828 }
829
830 int __connman_service_set_carrier(struct connman_service *service,
831                                                 connman_bool_t carrier)
832 {
833         DBG("service %p carrier %d", service, carrier);
834
835         if (service == NULL)
836                 return -EINVAL;
837
838         switch (service->type) {
839         case CONNMAN_SERVICE_TYPE_UNKNOWN:
840         case CONNMAN_SERVICE_TYPE_WIFI:
841         case CONNMAN_SERVICE_TYPE_WIMAX:
842         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
843         case CONNMAN_SERVICE_TYPE_CELLULAR:
844                 return -EINVAL;
845         case CONNMAN_SERVICE_TYPE_ETHERNET:
846                 break;
847         }
848
849         if (carrier == FALSE) {
850                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
851                 state_changed(service);
852
853                 service->state = CONNMAN_SERVICE_STATE_IDLE;
854                 state_changed(service);
855         } else {
856                 service->state = CONNMAN_SERVICE_STATE_CARRIER;
857                 state_changed(service);
858         }
859
860         return connman_service_set_favorite(service, carrier);
861 }
862
863 int __connman_service_indicate_state(struct connman_service *service,
864                                         enum connman_service_state state)
865 {
866         GSequenceIter *iter;
867
868         DBG("service %p state %d", service, state);
869
870         if (service == NULL)
871                 return -EINVAL;
872
873         if (state == CONNMAN_SERVICE_STATE_CARRIER)
874                 return __connman_service_set_carrier(service, TRUE);
875
876         if (service->state == state)
877                 return -EALREADY;
878
879         if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
880                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
881                 return -EINVAL;
882
883         if (state == CONNMAN_SERVICE_STATE_IDLE &&
884                         service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
885                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
886                 state_changed(service);
887         }
888
889         service->state = state;
890         state_changed(service);
891
892         if (state == CONNMAN_SERVICE_STATE_READY) {
893                 connman_service_set_favorite(service, TRUE);
894
895                 if (service->timeout > 0) {
896                         g_source_remove(service->timeout);
897                         service->timeout = 0;
898                 }
899
900                 if (service->pending != NULL) {
901                         g_dbus_send_reply(connection, service->pending,
902                                                         DBUS_TYPE_INVALID);
903
904                         dbus_message_unref(service->pending);
905                         service->pending = NULL;
906                 }
907
908                 g_get_current_time(&service->modified);
909                 __connman_storage_save_service(service);
910         }
911
912         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
913                 if (service->timeout > 0) {
914                         g_source_remove(service->timeout);
915                         service->timeout = 0;
916                 }
917
918                 if (service->pending != NULL) {
919                         DBusMessage *reply;
920
921                         reply = __connman_error_failed(service->pending, EIO);
922                         if (reply != NULL)
923                                 g_dbus_send_message(connection, reply);
924
925                         dbus_message_unref(service->pending);
926                         service->pending = NULL;
927                 }
928
929                 g_get_current_time(&service->modified);
930                 __connman_storage_save_service(service);
931         } else
932                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
933
934         iter = g_hash_table_lookup(service_hash, service->identifier);
935         if (iter != NULL)
936                 g_sequence_sort_changed(iter, service_compare, NULL);
937
938         __connman_profile_changed();
939
940         return 0;
941 }
942
943 int __connman_service_indicate_error(struct connman_service *service,
944                                         enum connman_service_error error)
945 {
946         DBG("service %p error %d", service, error);
947
948         if (service == NULL)
949                 return -EINVAL;
950
951         service->error = error;
952
953         return __connman_service_indicate_state(service,
954                                         CONNMAN_SERVICE_STATE_FAILURE);
955 }
956
957 int __connman_service_indicate_default(struct connman_service *service)
958 {
959         DBG("service %p", service);
960
961         return 0;
962 }
963
964 int __connman_service_connect(struct connman_service *service)
965 {
966         int err;
967
968         DBG("service %p", service);
969
970         if (service->state == CONNMAN_SERVICE_STATE_READY)
971                 return -EISCONN;
972
973         if (service->network != NULL) {
974                 if (service->hidden == TRUE)
975                         return -EINVAL;
976
977                 connman_network_set_string(service->network,
978                                 "WiFi.Passphrase", service->passphrase);
979
980                 err = __connman_network_connect(service->network);
981         } else if (service->device != NULL) {
982                 if (service->favorite == FALSE)
983                         return -ENOLINK;
984
985                 err = __connman_device_connect(service->device);
986         } else
987                 return -EOPNOTSUPP;
988
989         if (err < 0) {
990                 if (err != -EINPROGRESS)
991                         return err;
992
993                 service->timeout = g_timeout_add_seconds(45,
994                                                 connect_timeout, service);
995
996                 return -EINPROGRESS;
997         }
998
999         return 0;
1000 }
1001
1002 int __connman_service_disconnect(struct connman_service *service)
1003 {
1004         int err;
1005
1006         DBG("service %p", service);
1007
1008         if (service->network != NULL) {
1009                 err = __connman_network_disconnect(service->network);
1010         } else if (service->device != NULL) {
1011                 if (service->favorite == FALSE)
1012                         return -ENOLINK;
1013                 err = __connman_device_disconnect(service->device);
1014         } else
1015                 return -EOPNOTSUPP;
1016
1017         if (err < 0) {
1018                 if (err != -EINPROGRESS)
1019                         return err;
1020
1021                 return -EINPROGRESS;
1022         }
1023
1024         return 0;
1025 }
1026
1027 /**
1028  * __connman_service_lookup:
1029  * @identifier: service identifier
1030  *
1031  * Look up a service by identifier (reference count will not be increased)
1032  */
1033 static struct connman_service *__connman_service_lookup(const char *identifier)
1034 {
1035         GSequenceIter *iter;
1036
1037         iter = g_hash_table_lookup(service_hash, identifier);
1038         if (iter != NULL)
1039                 return g_sequence_get(iter);
1040
1041         return NULL;
1042 }
1043
1044 /**
1045  * __connman_service_get:
1046  * @identifier: service identifier
1047  *
1048  * Look up a service by identifier or create a new one if not found
1049  */
1050 static struct connman_service *__connman_service_get(const char *identifier)
1051 {
1052         struct connman_service *service;
1053         GSequenceIter *iter;
1054
1055         iter = g_hash_table_lookup(service_hash, identifier);
1056         if (iter != NULL) {
1057                 service = g_sequence_get(iter);
1058                 if (service != NULL)
1059                         g_atomic_int_inc(&service->refcount);
1060                 return service;
1061         }
1062
1063         service = connman_service_create();
1064         if (service == NULL)
1065                 return NULL;
1066
1067         DBG("service %p", service);
1068
1069         service->identifier = g_strdup(identifier);
1070
1071         service->profile = g_strdup(__connman_profile_active_ident());
1072
1073         __connman_storage_load_service(service);
1074
1075         iter = g_sequence_insert_sorted(service_list, service,
1076                                                 service_compare, NULL);
1077
1078         g_hash_table_insert(service_hash, service->identifier, iter);
1079
1080         return service;
1081 }
1082
1083 static int service_register(struct connman_service *service)
1084 {
1085         const char *path = __connman_profile_active_path();
1086         GSequenceIter *iter;
1087
1088         DBG("service %p", service);
1089
1090         if (service->path != NULL)
1091                 return -EALREADY;
1092
1093         service->path = g_strdup_printf("%s/%s", path, service->identifier);
1094
1095         DBG("path %s", service->path);
1096
1097         g_dbus_register_interface(connection, service->path,
1098                                         CONNMAN_SERVICE_INTERFACE,
1099                                         service_methods, service_signals,
1100                                                         NULL, service, NULL);
1101
1102         __connman_storage_load_service(service);
1103
1104         iter = g_hash_table_lookup(service_hash, service->identifier);
1105         if (iter != NULL)
1106                 g_sequence_sort_changed(iter, service_compare, NULL);
1107
1108         __connman_profile_changed();
1109
1110         return 0;
1111 }
1112
1113 static connman_bool_t is_connecting(struct connman_service *service)
1114 {
1115         switch (service->state) {
1116         case CONNMAN_SERVICE_STATE_UNKNOWN:
1117         case CONNMAN_SERVICE_STATE_IDLE:
1118         case CONNMAN_SERVICE_STATE_CARRIER:
1119         case CONNMAN_SERVICE_STATE_FAILURE:
1120         case CONNMAN_SERVICE_STATE_DISCONNECT:
1121         case CONNMAN_SERVICE_STATE_READY:
1122                 break;
1123         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1124         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1125                 return TRUE;
1126         }
1127
1128         return FALSE;
1129 }
1130
1131 static void __connman_service_auto_connect(void)
1132 {
1133         struct connman_service *service;
1134         GSequenceIter *iter;
1135
1136         DBG("");
1137
1138         iter = g_sequence_get_begin_iter(service_list);
1139         if (g_sequence_iter_is_end(iter) == TRUE)
1140                 return;
1141
1142         service = g_sequence_get(iter);
1143
1144         while (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1145                 iter = g_sequence_iter_next(iter);
1146                 if (g_sequence_iter_is_end(iter))
1147                         return;
1148                 service = g_sequence_get(iter);
1149         }
1150
1151         if (service->favorite == FALSE)
1152                 return;
1153
1154         if (service->state == CONNMAN_SERVICE_STATE_READY)
1155                 return;
1156
1157         if (is_connecting(service) == TRUE)
1158                 return;
1159
1160         if (service->state == CONNMAN_SERVICE_STATE_IDLE)
1161                 __connman_service_connect(service);
1162 }
1163
1164 /**
1165  * connman_service_lookup_from_device:
1166  * @device: device structure
1167  *
1168  * Look up a service by device (reference count will not be increased)
1169  */
1170 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1171 {
1172         struct connman_service *service;
1173         const char *ident;
1174         char *name;
1175
1176         ident = __connman_device_get_ident(device);
1177         if (ident == NULL)
1178                 return NULL;
1179
1180         name = g_strdup_printf("%s_%s",
1181                                 __connman_device_get_type(device), ident);
1182
1183         service = __connman_service_lookup(name);
1184
1185         g_free(name);
1186
1187         return service;
1188 }
1189
1190 static enum connman_service_type convert_device_type(struct connman_device *device)
1191 {
1192         enum connman_device_type type = connman_device_get_type(device);
1193
1194         switch (type) {
1195         case CONNMAN_DEVICE_TYPE_UNKNOWN:
1196         case CONNMAN_DEVICE_TYPE_VENDOR:
1197         case CONNMAN_DEVICE_TYPE_WIFI:
1198         case CONNMAN_DEVICE_TYPE_WIMAX:
1199         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1200         case CONNMAN_DEVICE_TYPE_GPS:
1201         case CONNMAN_DEVICE_TYPE_MBM:
1202         case CONNMAN_DEVICE_TYPE_HSO:
1203         case CONNMAN_DEVICE_TYPE_NOZOMI:
1204         case CONNMAN_DEVICE_TYPE_HUAWEI:
1205         case CONNMAN_DEVICE_TYPE_NOVATEL:
1206                 break;
1207         case CONNMAN_DEVICE_TYPE_ETHERNET:
1208                 return CONNMAN_SERVICE_TYPE_ETHERNET;
1209         }
1210
1211         return CONNMAN_SERVICE_TYPE_UNKNOWN;
1212 }
1213
1214 /**
1215  * connman_service_create_from_device:
1216  * @device: device structure
1217  *
1218  * Look up service by device and if not found, create one
1219  */
1220 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1221 {
1222         struct connman_service *service;
1223         const char *ident;
1224         char *name;
1225
1226         ident = __connman_device_get_ident(device);
1227         if (ident == NULL)
1228                 return NULL;
1229
1230         name = g_strdup_printf("%s_%s",
1231                                 __connman_device_get_type(device), ident);
1232
1233         service = __connman_service_get(name);
1234         if (service == NULL)
1235                 goto done;
1236
1237         if (service->path != NULL) {
1238                 __connman_service_put(service);
1239                 service = NULL;
1240                 goto done;
1241         }
1242
1243         service->type = convert_device_type(device);
1244
1245         service->device = device;
1246
1247         service_register(service);
1248
1249         if (service->favorite == TRUE)
1250                 __connman_service_auto_connect();
1251
1252 done:
1253         g_free(name);
1254
1255         return service;
1256 }
1257
1258 /**
1259  * connman_service_lookup_from_network:
1260  * @network: network structure
1261  *
1262  * Look up a service by network (reference count will not be increased)
1263  */
1264 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1265 {
1266         struct connman_service *service;
1267         const char *ident, *group;
1268         char *name;
1269
1270         ident = __connman_network_get_ident(network);
1271         if (ident == NULL)
1272                 return NULL;
1273
1274         group = __connman_network_get_group(network);
1275         if (group == NULL)
1276                 return NULL;
1277
1278         name = g_strdup_printf("%s_%s_%s",
1279                         __connman_network_get_type(network), ident, group);
1280
1281         service = __connman_service_lookup(name);
1282
1283         g_free(name);
1284
1285         return service;
1286 }
1287
1288 unsigned int __connman_service_get_order(struct connman_service *service)
1289 {
1290         return service->order;
1291 }
1292
1293 static enum connman_service_type convert_network_type(struct connman_network *network)
1294 {
1295         enum connman_network_type type = connman_network_get_type(network);
1296
1297         switch (type) {
1298         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1299         case CONNMAN_NETWORK_TYPE_VENDOR:
1300         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1301         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1302         case CONNMAN_NETWORK_TYPE_HSO:
1303                 break;
1304         case CONNMAN_NETWORK_TYPE_WIFI:
1305                 return CONNMAN_SERVICE_TYPE_WIFI;
1306         case CONNMAN_NETWORK_TYPE_WIMAX:
1307                 return CONNMAN_SERVICE_TYPE_WIMAX;
1308         }
1309
1310         return CONNMAN_SERVICE_TYPE_UNKNOWN;
1311 }
1312
1313 static enum connman_service_mode convert_wifi_mode(const char *mode)
1314 {
1315         if (mode == NULL)
1316                 return CONNMAN_SERVICE_MODE_UNKNOWN;
1317         else if (g_str_equal(mode, "managed") == TRUE)
1318                 return CONNMAN_SERVICE_MODE_MANAGED;
1319         else if (g_str_equal(mode, "adhoc") == TRUE)
1320                 return CONNMAN_SERVICE_MODE_ADHOC;
1321         else
1322                 return CONNMAN_SERVICE_MODE_UNKNOWN;
1323 }
1324
1325 static enum connman_service_mode convert_wifi_security(const char *security)
1326 {
1327         if (security == NULL)
1328                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1329         else if (g_str_equal(security, "none") == TRUE)
1330                 return CONNMAN_SERVICE_SECURITY_NONE;
1331         else if (g_str_equal(security, "wep") == TRUE)
1332                 return CONNMAN_SERVICE_SECURITY_WEP;
1333         else if (g_str_equal(security, "wpa") == TRUE)
1334                 return CONNMAN_SERVICE_SECURITY_WPA;
1335         else if (g_str_equal(security, "rsn") == TRUE)
1336                 return CONNMAN_SERVICE_SECURITY_RSN;
1337         else
1338                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1339 }
1340
1341 static void update_from_network(struct connman_service *service,
1342                                         struct connman_network *network)
1343 {
1344         connman_uint8_t strength = service->strength;
1345         GSequenceIter *iter;
1346         const char *str;
1347
1348         str = connman_network_get_string(network, "Name");
1349         if (str != NULL) {
1350                 g_free(service->name);
1351                 service->name = g_strdup(str);
1352                 service->hidden = FALSE;
1353         } else {
1354                 g_free(service->name);
1355                 service->name = NULL;
1356                 service->hidden = TRUE;
1357         }
1358
1359         service->strength = connman_network_get_uint8(network, "Strength");
1360
1361         str = connman_network_get_string(network, "WiFi.Mode");
1362         service->mode = convert_wifi_mode(str);
1363
1364         str = connman_network_get_string(network, "WiFi.Security");
1365         service->security = convert_wifi_security(str);
1366
1367         if (service->strength > strength && service->network != NULL) {
1368                 connman_network_unref(service->network);
1369                 service->network = NULL;
1370
1371                 strength_changed(service);
1372         }
1373
1374         if (service->network == NULL) {
1375                 service->network = connman_network_ref(network);
1376
1377                 str = connman_network_get_string(network, "WiFi.Passphrase");
1378                 if (str != NULL) {
1379                         g_free(service->passphrase);
1380                         service->passphrase = g_strdup(str);
1381                 }
1382         }
1383
1384         iter = g_hash_table_lookup(service_hash, service->identifier);
1385         if (iter != NULL)
1386                 g_sequence_sort_changed(iter, service_compare, NULL);
1387 }
1388
1389 /**
1390  * connman_service_create_from_network:
1391  * @network: network structure
1392  *
1393  * Look up service by network and if not found, create one
1394  */
1395 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1396 {
1397         struct connman_service *service;
1398         const char *ident, *group;
1399         char *name;
1400
1401         service = __connman_service_lookup_from_network(network);
1402         if (service != NULL) {
1403                 update_from_network(service, network);
1404                 return service;
1405         }
1406
1407         ident = __connman_network_get_ident(network);
1408         if (ident == NULL)
1409                 return NULL;
1410
1411         group = __connman_network_get_group(network);
1412         if (group == NULL)
1413                 return NULL;
1414
1415         name = g_strdup_printf("%s_%s_%s",
1416                         __connman_network_get_type(network), ident, group);
1417
1418         service = __connman_service_get(name);
1419         if (service == NULL)
1420                 goto done;
1421
1422         if (service->path != NULL) {
1423                 update_from_network(service, network);
1424
1425                 __connman_profile_changed();
1426
1427                 __connman_service_put(service);
1428                 service = NULL;
1429                 goto done;
1430         }
1431
1432         service->type = convert_network_type(network);
1433
1434         service->state = CONNMAN_SERVICE_STATE_IDLE;
1435
1436         update_from_network(service, network);
1437
1438         service_register(service);
1439
1440         if (service->favorite == TRUE)
1441                 __connman_service_auto_connect();
1442
1443 done:
1444         g_free(name);
1445
1446         return service;
1447 }
1448
1449 static int service_load(struct connman_service *service)
1450 {
1451         GKeyFile *keyfile;
1452         gchar *pathname, *data = NULL;
1453         gsize length;
1454         gchar *str;
1455
1456         DBG("service %p", service);
1457
1458         if (service->profile == NULL)
1459                 return -EINVAL;
1460
1461         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1462         if (pathname == NULL)
1463                 return -ENOMEM;
1464
1465         keyfile = g_key_file_new();
1466
1467         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1468                 g_free(pathname);
1469                 return -ENOENT;
1470         }
1471
1472         g_free(pathname);
1473
1474         if (g_key_file_load_from_data(keyfile, data, length,
1475                                                         0, NULL) == FALSE) {
1476                 g_free(data);
1477                 return -EILSEQ;
1478         }
1479
1480         g_free(data);
1481
1482         switch (service->type) {
1483         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1484         case CONNMAN_SERVICE_TYPE_ETHERNET:
1485                 break;
1486         case CONNMAN_SERVICE_TYPE_WIFI:
1487         case CONNMAN_SERVICE_TYPE_WIMAX:
1488         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1489         case CONNMAN_SERVICE_TYPE_CELLULAR:
1490                 service->favorite = g_key_file_get_boolean(keyfile,
1491                                 service->identifier, "Favorite", NULL);
1492
1493                 str = g_key_file_get_string(keyfile,
1494                                 service->identifier, "Failure", NULL);
1495                 if (str != NULL) {
1496                         service->state = CONNMAN_SERVICE_STATE_FAILURE;
1497                         service->error = string2error(str);
1498                 }
1499                 break;
1500         }
1501
1502         str = g_key_file_get_string(keyfile,
1503                                 service->identifier, "Modified", NULL);
1504         if (str != NULL) {
1505                 g_time_val_from_iso8601(str, &service->modified);
1506                 g_free(str);
1507         }
1508
1509         str = g_key_file_get_string(keyfile,
1510                                 service->identifier, "Passphrase", NULL);
1511         if (str != NULL) {
1512                 g_free(service->passphrase);
1513                 service->passphrase = str;
1514         }
1515
1516         __connman_ipconfig_load(service->ipconfig, keyfile,
1517                                         service->identifier, "IPv4.");
1518
1519         g_key_file_free(keyfile);
1520
1521         return 0;
1522 }
1523
1524 static int service_save(struct connman_service *service)
1525 {
1526         GKeyFile *keyfile;
1527         gchar *pathname, *data = NULL;
1528         gsize length;
1529         gchar *str;
1530
1531         DBG("service %p", service);
1532
1533         if (service->profile == NULL)
1534                 return -EINVAL;
1535
1536         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1537         if (pathname == NULL)
1538                 return -ENOMEM;
1539
1540         keyfile = g_key_file_new();
1541
1542         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1543                 goto update;
1544
1545         if (length > 0) {
1546                 if (g_key_file_load_from_data(keyfile, data, length,
1547                                                         0, NULL) == FALSE)
1548                         goto done;
1549         }
1550
1551         g_free(data);
1552
1553 update:
1554         if (service->name != NULL)
1555                 g_key_file_set_string(keyfile, service->identifier,
1556                                                 "Name", service->name);
1557
1558         switch (service->type) {
1559         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1560         case CONNMAN_SERVICE_TYPE_ETHERNET:
1561                 break;
1562         case CONNMAN_SERVICE_TYPE_WIFI:
1563         case CONNMAN_SERVICE_TYPE_WIMAX:
1564         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1565         case CONNMAN_SERVICE_TYPE_CELLULAR:
1566                 g_key_file_set_boolean(keyfile, service->identifier,
1567                                         "Favorite", service->favorite);
1568
1569                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1570                         const char *failure = error2string(service->error);
1571                         if (failure != NULL)
1572                                 g_key_file_set_string(keyfile,
1573                                                         service->identifier,
1574                                                         "Failure", failure);
1575                 } else {
1576                         g_key_file_remove_key(keyfile, service->identifier,
1577                                                         "Failure", NULL);
1578                 }
1579                 break;
1580         }
1581
1582         str = g_time_val_to_iso8601(&service->modified);
1583         if (str != NULL) {
1584                 g_key_file_set_string(keyfile, service->identifier,
1585                                                         "Modified", str);
1586                 g_free(str);
1587         }
1588
1589         if (service->passphrase != NULL)
1590                 g_key_file_set_string(keyfile, service->identifier,
1591                                         "Passphrase", service->passphrase);
1592         else
1593                 g_key_file_remove_key(keyfile, service->identifier,
1594                                                         "Passphrase", NULL);
1595
1596         __connman_ipconfig_save(service->ipconfig, keyfile,
1597                                         service->identifier, "IPv4.");
1598
1599         data = g_key_file_to_data(keyfile, &length, NULL);
1600
1601         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1602                 connman_error("Failed to store service information");
1603
1604 done:
1605         g_free(data);
1606
1607         g_key_file_free(keyfile);
1608
1609         g_free(pathname);
1610
1611         return 0;
1612 }
1613
1614 static struct connman_storage service_storage = {
1615         .name           = "service",
1616         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1617         .service_load   = service_load,
1618         .service_save   = service_save,
1619 };
1620
1621 int __connman_service_init(void)
1622 {
1623         DBG("");
1624
1625         connection = connman_dbus_get_connection();
1626
1627         if (connman_storage_register(&service_storage) < 0)
1628                 connman_error("Failed to register service storage");
1629
1630         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1631                                                                 NULL, NULL);
1632
1633         service_list = g_sequence_new(service_free);
1634
1635         return 0;
1636 }
1637
1638 void __connman_service_cleanup(void)
1639 {
1640         DBG("");
1641
1642         g_sequence_free(service_list);
1643         service_list = NULL;
1644
1645         g_hash_table_destroy(service_hash);
1646         service_hash = NULL;
1647
1648         connman_storage_unregister(&service_storage);
1649
1650         dbus_connection_unref(connection);
1651 }