c1c33f735f40b99c26bc241a1b4e3222a7a9570f
[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 <stdio.h>
27 #include <string.h>
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 #define MAX_CONNECT_RETRIES     2
33
34 static DBusConnection *connection = NULL;
35
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38
39 struct connman_service {
40         gint refcount;
41         char *identifier;
42         char *path;
43         enum connman_service_type type;
44         enum connman_service_mode mode;
45         enum connman_service_security security;
46         enum connman_service_state state;
47         enum connman_service_error error;
48         connman_uint8_t strength;
49         connman_bool_t favorite;
50         connman_bool_t hidden;
51         connman_bool_t ignore;
52         connman_bool_t autoconnect;
53         connman_bool_t userconnect;
54         GTimeVal modified;
55         unsigned int order;
56         unsigned int failcounter;
57         char *name;
58         char *passphrase;
59         char *profile;
60         char *apn;
61         char *username;
62         char *password;
63         connman_bool_t roaming;
64         struct connman_ipconfig *ipconfig;
65         struct connman_device *device;
66         struct connman_network *network;
67         DBusMessage *pending;
68         guint timeout;
69 };
70
71 static void append_path(gpointer value, gpointer user_data)
72 {
73         struct connman_service *service = value;
74         DBusMessageIter *iter = user_data;
75
76         if (service->path == NULL || service->hidden == TRUE)
77                 return;
78
79         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
80                                                         &service->path);
81 }
82
83 void __connman_service_list(DBusMessageIter *iter)
84 {
85         DBG("");
86
87         g_sequence_foreach(service_list, append_path, iter);
88 }
89
90 struct find_data {
91         const char *path;
92         struct connman_service *service;
93 };
94
95 static void compare_path(gpointer value, gpointer user_data)
96 {
97         struct connman_service *service = value;
98         struct find_data *data = user_data;
99
100         if (data->service != NULL)
101                 return;
102
103         if (g_strcmp0(service->path, data->path) == 0)
104                 data->service = service;
105 }
106
107 static struct connman_service *find_service(const char *path)
108 {
109         struct find_data data = { .path = path, .service = NULL };
110
111         DBG("path %s", path);
112
113         g_sequence_foreach(service_list, compare_path, &data);
114
115         return data.service;
116 }
117
118 static const char *type2string(enum connman_service_type type)
119 {
120         switch (type) {
121         case CONNMAN_SERVICE_TYPE_UNKNOWN:
122                 break;
123         case CONNMAN_SERVICE_TYPE_ETHERNET:
124                 return "ethernet";
125         case CONNMAN_SERVICE_TYPE_WIFI:
126                 return "wifi";
127         case CONNMAN_SERVICE_TYPE_WIMAX:
128                 return "wimax";
129         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
130                 return "bluetooth";
131         case CONNMAN_SERVICE_TYPE_CELLULAR:
132                 return "cellular";
133         }
134
135         return NULL;
136 }
137
138 static const char *mode2string(enum connman_service_mode mode)
139 {
140         switch (mode) {
141         case CONNMAN_SERVICE_MODE_UNKNOWN:
142                 break;
143         case CONNMAN_SERVICE_MODE_MANAGED:
144                 return "managed";
145         case CONNMAN_SERVICE_MODE_ADHOC:
146                 return "adhoc";
147         }
148
149         return NULL;
150 }
151
152 static const char *security2string(enum connman_service_security security)
153 {
154         switch (security) {
155         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
156                 break;
157         case CONNMAN_SERVICE_SECURITY_NONE:
158                 return "none";
159         case CONNMAN_SERVICE_SECURITY_WEP:
160                 return "wep";
161         case CONNMAN_SERVICE_SECURITY_WPA:
162                 return "wpa";
163         case CONNMAN_SERVICE_SECURITY_RSN:
164                 return "rsn";
165         }
166
167         return NULL;
168 }
169
170 static const char *state2string(enum connman_service_state state)
171 {
172         switch (state) {
173         case CONNMAN_SERVICE_STATE_UNKNOWN:
174                 break;
175         case CONNMAN_SERVICE_STATE_IDLE:
176                 return "idle";
177         case CONNMAN_SERVICE_STATE_CARRIER:
178                 return "carrier";
179         case CONNMAN_SERVICE_STATE_ASSOCIATION:
180                 return "association";
181         case CONNMAN_SERVICE_STATE_CONFIGURATION:
182                 return "configuration";
183         case CONNMAN_SERVICE_STATE_READY:
184                 return "ready";
185         case CONNMAN_SERVICE_STATE_DISCONNECT:
186                 return "disconnect";
187         case CONNMAN_SERVICE_STATE_FAILURE:
188                 return "failure";
189         }
190
191         return NULL;
192 }
193
194 static const char *error2string(enum connman_service_error error)
195 {
196         switch (error) {
197         case CONNMAN_SERVICE_ERROR_UNKNOWN:
198                 break;
199         case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
200                 return "out-of-range";
201         case CONNMAN_SERVICE_ERROR_PIN_MISSING:
202                 return "pin-missing";
203         case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
204                 return "dhcp-failed";
205         case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
206                 return "connect-failed";
207         }
208
209         return NULL;
210 }
211
212 static enum connman_service_error string2error(const char *error)
213 {
214         if (g_strcmp0(error, "dhcp-failed") == 0)
215                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
216         else if (g_strcmp0(error, "pin-missing") == 0)
217                 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
218
219         return CONNMAN_SERVICE_ERROR_UNKNOWN;
220 }
221
222 const char *__connman_service_default(void)
223 {
224         struct connman_service *service;
225         GSequenceIter *iter;
226
227         iter = g_sequence_get_begin_iter(service_list);
228
229         if (g_sequence_iter_is_end(iter) == TRUE)
230                 return "";
231
232         service = g_sequence_get(iter);
233         if (service == NULL)
234                 return "";
235
236         if (service->state != CONNMAN_SERVICE_STATE_READY)
237                 return "";
238
239         return type2string(service->type);
240 }
241
242 static void state_changed(struct connman_service *service)
243 {
244         DBusMessage *signal;
245         DBusMessageIter entry, value;
246         const char *str, *key = "State";
247
248         if (service->path == NULL)
249                 return;
250
251         str = state2string(service->state);
252         if (str == NULL)
253                 return;
254
255         signal = dbus_message_new_signal(service->path,
256                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
257         if (signal == NULL)
258                 return;
259
260         dbus_message_iter_init_append(signal, &entry);
261
262         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
263
264         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
265                                         DBUS_TYPE_STRING_AS_STRING, &value);
266         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
267         dbus_message_iter_close_container(&entry, &value);
268
269         g_dbus_send_message(connection, signal);
270 }
271
272 static void strength_changed(struct connman_service *service)
273 {
274         DBusMessage *signal;
275         DBusMessageIter entry, value;
276         const char *key = "Strength";
277
278         if (service->path == NULL)
279                 return;
280
281         if (service->strength == 0)
282                 return;
283
284         signal = dbus_message_new_signal(service->path,
285                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
286         if (signal == NULL)
287                 return;
288
289         dbus_message_iter_init_append(signal, &entry);
290
291         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
292
293         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
294                                         DBUS_TYPE_BYTE_AS_STRING, &value);
295         dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
296                                                         &service->strength);
297         dbus_message_iter_close_container(&entry, &value);
298
299         g_dbus_send_message(connection, signal);
300 }
301
302 static void autoconnect_changed(struct connman_service *service)
303 {
304         DBusMessage *signal;
305         DBusMessageIter entry, value;
306         const char *key = "AutoConnect";
307
308         if (service->path == NULL)
309                 return;
310
311         signal = dbus_message_new_signal(service->path,
312                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
313         if (signal == NULL)
314                 return;
315
316         dbus_message_iter_init_append(signal, &entry);
317
318         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
319
320         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
321                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
322         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
323                                                         &service->autoconnect);
324         dbus_message_iter_close_container(&entry, &value);
325
326         g_dbus_send_message(connection, signal);
327 }
328
329 static void passphrase_changed(struct connman_service *service)
330 {
331         DBusMessage *signal;
332         DBusMessageIter entry, value;
333         dbus_bool_t required;
334         const char *key = "PassphraseRequired";
335
336         if (service->path == NULL)
337                 return;
338
339         switch (service->type) {
340         case CONNMAN_SERVICE_TYPE_UNKNOWN:
341         case CONNMAN_SERVICE_TYPE_ETHERNET:
342         case CONNMAN_SERVICE_TYPE_WIMAX:
343         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
344         case CONNMAN_SERVICE_TYPE_CELLULAR:
345                 return;
346         case CONNMAN_SERVICE_TYPE_WIFI:
347                 required = FALSE;
348
349                 switch (service->security) {
350                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
351                 case CONNMAN_SERVICE_SECURITY_NONE:
352                         break;
353                 case CONNMAN_SERVICE_SECURITY_WEP:
354                 case CONNMAN_SERVICE_SECURITY_WPA:
355                 case CONNMAN_SERVICE_SECURITY_RSN:
356                         if (service->passphrase == NULL)
357                                 required = TRUE;
358                         break;
359                 }
360                 break;
361         }
362
363         signal = dbus_message_new_signal(service->path,
364                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
365         if (signal == NULL)
366                 return;
367
368         dbus_message_iter_init_append(signal, &entry);
369
370         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
371
372         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
373                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
374         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
375         dbus_message_iter_close_container(&entry, &value);
376
377         g_dbus_send_message(connection, signal);
378 }
379
380 static void apn_changed(struct connman_service *service)
381 {
382         DBusMessage *signal;
383         DBusMessageIter entry, value;
384         dbus_bool_t required;
385         const char *key = "SetupRequired";
386
387         if (service->path == NULL)
388                 return;
389
390         switch (service->type) {
391         case CONNMAN_SERVICE_TYPE_UNKNOWN:
392         case CONNMAN_SERVICE_TYPE_ETHERNET:
393         case CONNMAN_SERVICE_TYPE_WIMAX:
394         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
395         case CONNMAN_SERVICE_TYPE_WIFI:
396                 return;
397         case CONNMAN_SERVICE_TYPE_CELLULAR:
398                 break;
399         }
400
401         required = (service->apn == NULL) ? TRUE : FALSE;
402
403         signal = dbus_message_new_signal(service->path,
404                                 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
405         if (signal == NULL)
406                 return;
407
408         dbus_message_iter_init_append(signal, &entry);
409
410         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
411
412         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
413                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
414         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
415         dbus_message_iter_close_container(&entry, &value);
416
417         g_dbus_send_message(connection, signal);
418 }
419
420 static DBusMessage *get_properties(DBusConnection *conn,
421                                         DBusMessage *msg, void *user_data)
422 {
423         struct connman_service *service = user_data;
424         DBusMessage *reply;
425         DBusMessageIter array, dict;
426         dbus_bool_t required;
427         const char *str;
428
429         DBG("service %p", service);
430
431         reply = dbus_message_new_method_return(msg);
432         if (reply == NULL)
433                 return NULL;
434
435         dbus_message_iter_init_append(reply, &array);
436
437         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
438                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
439                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
440                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
441
442         str = type2string(service->type);
443         if (str != NULL)
444                 connman_dbus_dict_append_variant(&dict, "Type",
445                                                 DBUS_TYPE_STRING, &str);
446
447         str = mode2string(service->mode);
448         if (str != NULL)
449                 connman_dbus_dict_append_variant(&dict, "Mode",
450                                                 DBUS_TYPE_STRING, &str);
451
452         str = security2string(service->security);
453         if (str != NULL)
454                 connman_dbus_dict_append_variant(&dict, "Security",
455                                                 DBUS_TYPE_STRING, &str);
456
457         str = state2string(service->state);
458         if (str != NULL)
459                 connman_dbus_dict_append_variant(&dict, "State",
460                                                 DBUS_TYPE_STRING, &str);
461
462         str = error2string(service->error);
463         if (str != NULL)
464                 connman_dbus_dict_append_variant(&dict, "Error",
465                                                 DBUS_TYPE_STRING, &str);
466
467         if (service->strength > 0)
468                 connman_dbus_dict_append_variant(&dict, "Strength",
469                                         DBUS_TYPE_BYTE, &service->strength);
470
471         connman_dbus_dict_append_variant(&dict, "Favorite",
472                                         DBUS_TYPE_BOOLEAN, &service->favorite);
473
474         if (service->favorite == TRUE)
475                 connman_dbus_dict_append_variant(&dict, "AutoConnect",
476                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
477         else
478                 connman_dbus_dict_append_variant(&dict, "AutoConnect",
479                                         DBUS_TYPE_BOOLEAN, &service->favorite);
480
481         if (service->name != NULL)
482                 connman_dbus_dict_append_variant(&dict, "Name",
483                                         DBUS_TYPE_STRING, &service->name);
484
485         switch (service->type) {
486         case CONNMAN_SERVICE_TYPE_UNKNOWN:
487         case CONNMAN_SERVICE_TYPE_ETHERNET:
488         case CONNMAN_SERVICE_TYPE_WIMAX:
489         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
490                 break;
491         case CONNMAN_SERVICE_TYPE_CELLULAR:
492                 connman_dbus_dict_append_variant(&dict, "Roaming",
493                                         DBUS_TYPE_BOOLEAN, &service->roaming);
494
495                 if (service->apn != NULL) {
496                         connman_dbus_dict_append_variant(&dict, "APN",
497                                         DBUS_TYPE_STRING, &service->apn);
498                         required = FALSE;
499                 } else
500                         required = TRUE;
501
502                 connman_dbus_dict_append_variant(&dict, "SetupRequired",
503                                                 DBUS_TYPE_BOOLEAN, &required);
504                 break;
505         case CONNMAN_SERVICE_TYPE_WIFI:
506                 if (service->passphrase != NULL &&
507                                 __connman_security_check_privilege(msg,
508                                         CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
509                         connman_dbus_dict_append_variant(&dict, "Passphrase",
510                                 DBUS_TYPE_STRING, &service->passphrase);
511
512                 required = FALSE;
513
514                 switch (service->security) {
515                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
516                 case CONNMAN_SERVICE_SECURITY_NONE:
517                         break;
518                 case CONNMAN_SERVICE_SECURITY_WEP:
519                 case CONNMAN_SERVICE_SECURITY_WPA:
520                 case CONNMAN_SERVICE_SECURITY_RSN:
521                         if (service->passphrase == NULL)
522                                 required = TRUE;
523                         break;
524                 }
525
526                 connman_dbus_dict_append_variant(&dict, "PassphraseRequired",
527                                                 DBUS_TYPE_BOOLEAN, &required);
528                 break;
529         }
530
531         if (service->ipconfig != NULL)
532                 __connman_ipconfig_append_ipv4(service->ipconfig,
533                                                         &dict, "IPv4.");
534
535         dbus_message_iter_close_container(&array, &dict);
536
537         return reply;
538 }
539
540 static DBusMessage *set_property(DBusConnection *conn,
541                                         DBusMessage *msg, void *user_data)
542 {
543         struct connman_service *service = user_data;
544         DBusMessageIter iter, value;
545         const char *name;
546         int type;
547
548         DBG("service %p", service);
549
550         if (dbus_message_iter_init(msg, &iter) == FALSE)
551                 return __connman_error_invalid_arguments(msg);
552
553         dbus_message_iter_get_basic(&iter, &name);
554         dbus_message_iter_next(&iter);
555         dbus_message_iter_recurse(&iter, &value);
556
557         if (__connman_security_check_privilege(msg,
558                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
559                 return __connman_error_permission_denied(msg);
560
561         type = dbus_message_iter_get_arg_type(&value);
562
563         if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
564                 connman_bool_t autoconnect;
565
566                 if (type != DBUS_TYPE_BOOLEAN)
567                         return __connman_error_invalid_arguments(msg);
568
569                 if (service->favorite == FALSE)
570                         return __connman_error_invalid_service(msg);
571
572                 dbus_message_iter_get_basic(&value, &autoconnect);
573
574                 if (service->autoconnect == autoconnect)
575                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
576
577                 service->autoconnect = autoconnect;
578
579                 autoconnect_changed(service);
580
581                 __connman_storage_save_service(service);
582         } else if (g_str_equal(name, "Passphrase") == TRUE) {
583                 const char *passphrase;
584
585                 if (type != DBUS_TYPE_STRING)
586                         return __connman_error_invalid_arguments(msg);
587
588                 if (__connman_security_check_privilege(msg,
589                                         CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
590                         return __connman_error_permission_denied(msg);
591
592                 dbus_message_iter_get_basic(&value, &passphrase);
593
594                 g_free(service->passphrase);
595                 service->passphrase = g_strdup(passphrase);
596
597                 passphrase_changed(service);
598
599                 if (service->network != NULL)
600                         connman_network_set_string(service->network,
601                                 "WiFi.Passphrase", service->passphrase);
602
603                 __connman_storage_save_service(service);
604         } else if (g_str_equal(name, "APN") == TRUE) {
605                 const char *apn;
606
607                 if (type != DBUS_TYPE_STRING)
608                         return __connman_error_invalid_arguments(msg);
609
610                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
611                         return __connman_error_invalid_service(msg);
612
613                 dbus_message_iter_get_basic(&value, &apn);
614
615                 g_free(service->apn);
616                 service->apn = g_strdup(apn);
617
618                 apn_changed(service);
619
620                 if (service->network != NULL)
621                         connman_network_set_string(service->network,
622                                                 "Cellular.APN", service->apn);
623
624                 __connman_storage_save_service(service);
625         } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
626                 int err;
627
628                 if (service->ipconfig == NULL)
629                         return __connman_error_invalid_property(msg);
630
631                 err = __connman_ipconfig_set_ipv4(service->ipconfig,
632                                                         name + 5, &value);
633                 if (err < 0)
634                         return __connman_error_failed(msg, -err);
635         } else
636                 return __connman_error_invalid_property(msg);
637
638         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
639 }
640
641 static void set_idle(struct connman_service *service)
642 {
643         service->state = CONNMAN_SERVICE_STATE_IDLE;
644         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
645         state_changed(service);
646 }
647
648 static DBusMessage *clear_property(DBusConnection *conn,
649                                         DBusMessage *msg, void *user_data)
650 {
651         struct connman_service *service = user_data;
652         const char *name;
653
654         DBG("service %p", service);
655
656         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
657                                                         DBUS_TYPE_INVALID);
658
659         if (__connman_security_check_privilege(msg,
660                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
661                 return __connman_error_permission_denied(msg);
662
663         if (g_str_equal(name, "Error") == TRUE) {
664                 set_idle(service);
665
666                 g_get_current_time(&service->modified);
667                 __connman_storage_save_service(service);
668         } else if (g_str_equal(name, "Passphrase") == TRUE) {
669                 g_free(service->passphrase);
670                 service->passphrase = NULL;
671
672                 passphrase_changed(service);
673
674                 __connman_storage_save_service(service);
675         } else
676                 return __connman_error_invalid_property(msg);
677
678         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
679 }
680
681 static connman_bool_t is_connecting(struct connman_service *service)
682 {
683         switch (service->state) {
684         case CONNMAN_SERVICE_STATE_UNKNOWN:
685         case CONNMAN_SERVICE_STATE_IDLE:
686         case CONNMAN_SERVICE_STATE_CARRIER:
687         case CONNMAN_SERVICE_STATE_FAILURE:
688         case CONNMAN_SERVICE_STATE_DISCONNECT:
689         case CONNMAN_SERVICE_STATE_READY:
690                 break;
691         case CONNMAN_SERVICE_STATE_ASSOCIATION:
692         case CONNMAN_SERVICE_STATE_CONFIGURATION:
693                 return TRUE;
694         }
695
696         return FALSE;
697 }
698
699 static connman_bool_t is_ignore(struct connman_service *service)
700 {
701         if (service->autoconnect == FALSE)
702                 return TRUE;
703
704         if (service->ignore == TRUE)
705                 return TRUE;
706
707         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
708                 return TRUE;
709
710         return FALSE;
711 }
712
713 void __connman_service_auto_connect(void)
714 {
715         struct connman_service *service = NULL;
716         GSequenceIter *iter;
717
718         DBG("");
719
720         iter = g_sequence_get_begin_iter(service_list);
721
722         while (g_sequence_iter_is_end(iter) == FALSE) {
723                 service = g_sequence_get(iter);
724
725                 if (service->pending != NULL)
726                         return;
727
728                 if (is_connecting(service) == TRUE)
729                         return;
730
731                 if (service->favorite == FALSE)
732                         return;
733
734                 if (service->state == CONNMAN_SERVICE_STATE_READY)
735                         return;
736
737                 if (is_ignore(service) == FALSE &&
738                                 service->state == CONNMAN_SERVICE_STATE_IDLE)
739                         break;
740
741                 service = NULL;
742
743                 iter = g_sequence_iter_next(iter);
744         }
745
746         if (service != NULL) {
747                 service->userconnect = FALSE;
748                 __connman_service_connect(service);
749         }
750 }
751
752 static void reply_pending(struct connman_service *service, int error)
753 {
754         if (service->timeout > 0) {
755                 g_source_remove(service->timeout);
756                 service->timeout = 0;
757         }
758
759         if (service->pending != NULL) {
760                 if (error > 0) {
761                         DBusMessage *reply;
762
763                         reply = __connman_error_failed(service->pending,
764                                                                 error);
765                         if (reply != NULL)
766                                 g_dbus_send_message(connection, reply);
767                 } else
768                         g_dbus_send_reply(connection, service->pending,
769                                                         DBUS_TYPE_INVALID);
770
771                 dbus_message_unref(service->pending);
772                 service->pending = NULL;
773         }
774 }
775
776 static gboolean connect_timeout(gpointer user_data)
777 {
778         struct connman_service *service = user_data;
779         connman_bool_t autoconnect = FALSE;
780
781         DBG("service %p", service);
782
783         service->timeout = 0;
784
785         if (service->network != NULL)
786                 __connman_network_disconnect(service->network);
787         else if (service->device != NULL)
788                 __connman_device_disconnect(service->device);
789
790         __connman_ipconfig_disable(service->ipconfig);
791
792         if (service->pending != NULL) {
793                 DBusMessage *reply;
794
795                 reply = __connman_error_operation_timeout(service->pending);
796                 if (reply != NULL)
797                         g_dbus_send_message(connection, reply);
798
799                 dbus_message_unref(service->pending);
800                 service->pending = NULL;
801         } else
802                 autoconnect = TRUE;
803
804         __connman_service_indicate_state(service,
805                                         CONNMAN_SERVICE_STATE_FAILURE);
806
807         if (autoconnect == TRUE && service->userconnect == FALSE)
808                 __connman_service_auto_connect();
809
810         return FALSE;
811 }
812
813 static DBusMessage *connect_service(DBusConnection *conn,
814                                         DBusMessage *msg, void *user_data)
815 {
816         struct connman_service *service = user_data;
817         GSequenceIter *iter;
818         int err;
819
820         DBG("service %p", service);
821
822         if (service->pending != NULL)
823                 return __connman_error_in_progress(msg);
824
825         iter = g_sequence_get_begin_iter(service_list);
826
827         while (g_sequence_iter_is_end(iter) == FALSE) {
828                 struct connman_service *temp = g_sequence_get(iter);
829
830                 if (service->type == temp->type &&
831                                         is_connecting(temp) == TRUE)
832                         return __connman_error_in_progress(msg);
833
834                 iter = g_sequence_iter_next(iter);
835         }
836
837         service->ignore = FALSE;
838
839         service->userconnect = TRUE;
840         service->failcounter = 0;
841
842         service->pending = dbus_message_ref(msg);
843
844         err = __connman_service_connect(service);
845         if (err < 0) {
846                 if (err == -ENOKEY) {
847                         if (__connman_agent_request_passphrase(service,
848                                                         NULL, NULL) == 0)
849                                 return NULL;
850                 }
851
852                 if (err != -EINPROGRESS) {
853                         dbus_message_unref(service->pending);
854                         service->pending = NULL;
855
856                         return __connman_error_failed(msg, -err);
857                 }
858
859                 return NULL;
860         }
861
862         dbus_message_unref(service->pending);
863         service->pending = NULL;
864
865         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
866 }
867
868 static DBusMessage *disconnect_service(DBusConnection *conn,
869                                         DBusMessage *msg, void *user_data)
870 {
871         struct connman_service *service = user_data;
872         int err;
873
874         DBG("service %p", service);
875
876         reply_pending(service, ECONNABORTED);
877
878         service->ignore = TRUE;
879
880         err = __connman_service_disconnect(service);
881         if (err < 0) {
882                 if (err != -EINPROGRESS)
883                         return __connman_error_failed(msg, -err);
884
885                 return NULL;
886         }
887
888         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
889 }
890
891 static DBusMessage *remove_service(DBusConnection *conn,
892                                         DBusMessage *msg, void *user_data)
893 {
894         struct connman_service *service = user_data;
895
896         DBG("service %p", service);
897
898         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
899                 return __connman_error_not_supported(msg);
900
901         if (service->favorite == FALSE)
902                 return __connman_error_not_supported(msg);
903
904         if (service->network != NULL)
905                 __connman_network_disconnect(service->network);
906
907         g_free(service->passphrase);
908         service->passphrase = NULL;
909
910         passphrase_changed(service);
911
912         connman_service_set_favorite(service, FALSE);
913         __connman_storage_save_service(service);
914
915         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
916 }
917
918 static DBusMessage *move_before(DBusConnection *conn,
919                                         DBusMessage *msg, void *user_data)
920 {
921         struct connman_service *service = user_data;
922         struct connman_service *target;
923         const char *path;
924         GSequenceIter *src, *dst;
925
926         DBG("service %p", service);
927
928         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
929                                                         DBUS_TYPE_INVALID);
930
931         if (service->favorite == FALSE)
932                 return __connman_error_not_supported(msg);
933
934         target = find_service(path);
935         if (target == NULL || target->favorite == FALSE || target == service)
936                 return __connman_error_invalid_service(msg);
937
938         DBG("target %s", target->identifier);
939
940         if (target->state != service->state)
941                 return __connman_error_invalid_service(msg);
942
943         g_get_current_time(&service->modified);
944         __connman_storage_save_service(service);
945
946         src = g_hash_table_lookup(service_hash, service->identifier);
947         dst = g_hash_table_lookup(service_hash, target->identifier);
948
949         g_sequence_move(src, dst);
950
951         __connman_profile_changed(FALSE);
952
953         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
954 }
955
956 static DBusMessage *move_after(DBusConnection *conn,
957                                         DBusMessage *msg, void *user_data)
958 {
959         struct connman_service *service = user_data;
960         struct connman_service *target;
961         const char *path;
962
963         DBG("service %p", service);
964
965         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
966                                                         DBUS_TYPE_INVALID);
967
968         if (service->favorite == FALSE)
969                 return __connman_error_not_supported(msg);
970
971         target = find_service(path);
972         if (target == NULL || target->favorite == FALSE || target == service)
973                 return __connman_error_invalid_service(msg);
974
975         DBG("target %s", target->identifier);
976
977         if (target->state != service->state)
978                 return __connman_error_invalid_service(msg);
979
980         g_get_current_time(&service->modified);
981         __connman_storage_save_service(service);
982
983         return __connman_error_not_implemented(msg);
984 }
985
986 static GDBusMethodTable service_methods[] = {
987         { "GetProperties", "",   "a{sv}", get_properties     },
988         { "SetProperty",   "sv", "",      set_property       },
989         { "ClearProperty", "s",  "",      clear_property     },
990         { "Connect",       "",   "",      connect_service,
991                                                 G_DBUS_METHOD_FLAG_ASYNC },
992         { "Disconnect",    "",   "",      disconnect_service },
993         { "Remove",        "",   "",      remove_service     },
994         { "MoveBefore",    "o",  "",      move_before        },
995         { "MoveAfter",     "o",  "",      move_after         },
996         { },
997 };
998
999 static GDBusSignalTable service_signals[] = {
1000         { "PropertyChanged", "sv" },
1001         { },
1002 };
1003
1004 static void service_free(gpointer user_data)
1005 {
1006         struct connman_service *service = user_data;
1007         char *path = service->path;
1008
1009         DBG("service %p", service);
1010
1011         reply_pending(service, ENOENT);
1012
1013         g_hash_table_remove(service_hash, service->identifier);
1014
1015         service->path = NULL;
1016
1017         if (path != NULL) {
1018                 __connman_profile_changed(FALSE);
1019
1020                 g_dbus_unregister_interface(connection, path,
1021                                                 CONNMAN_SERVICE_INTERFACE);
1022                 g_free(path);
1023         }
1024
1025         if (service->network != NULL)
1026                 connman_network_unref(service->network);
1027
1028         if (service->ipconfig != NULL) {
1029                 connman_ipconfig_unref(service->ipconfig);
1030                 service->ipconfig = NULL;
1031         }
1032
1033         g_free(service->apn);
1034         g_free(service->username);
1035         g_free(service->passphrase);
1036         g_free(service->profile);
1037         g_free(service->name);
1038         g_free(service->passphrase);
1039         g_free(service->identifier);
1040         g_free(service);
1041 }
1042
1043 /**
1044  * __connman_service_put:
1045  * @service: service structure
1046  *
1047  * Release service if no longer needed
1048  */
1049 void __connman_service_put(struct connman_service *service)
1050 {
1051         DBG("service %p", service);
1052
1053         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1054                 GSequenceIter *iter;
1055
1056                 iter = g_hash_table_lookup(service_hash, service->identifier);
1057                 if (iter != NULL) {
1058                         reply_pending(service, ECONNABORTED);
1059
1060                         __connman_service_disconnect(service);
1061
1062                         g_sequence_remove(iter);
1063                 } else
1064                         service_free(service);
1065         }
1066 }
1067
1068 static void __connman_service_initialize(struct connman_service *service)
1069 {
1070         DBG("service %p", service);
1071
1072         service->refcount = 1;
1073
1074         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
1075         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
1076         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1077         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
1078
1079         service->favorite = FALSE;
1080         service->hidden = FALSE;
1081
1082         service->ignore = FALSE;
1083
1084         service->userconnect = FALSE;
1085
1086         service->order = 0;
1087 }
1088
1089 /**
1090  * connman_service_create:
1091  *
1092  * Allocate a new service.
1093  *
1094  * Returns: a newly-allocated #connman_service structure
1095  */
1096 struct connman_service *connman_service_create(void)
1097 {
1098         struct connman_service *service;
1099
1100         service = g_try_new0(struct connman_service, 1);
1101         if (service == NULL)
1102                 return NULL;
1103
1104         DBG("service %p", service);
1105
1106         __connman_service_initialize(service);
1107
1108         return service;
1109 }
1110
1111 /**
1112  * connman_service_ref:
1113  * @service: service structure
1114  *
1115  * Increase reference counter of service
1116  */
1117 struct connman_service *connman_service_ref(struct connman_service *service)
1118 {
1119         g_atomic_int_inc(&service->refcount);
1120
1121         return service;
1122 }
1123
1124 /**
1125  * connman_service_unref:
1126  * @service: service structure
1127  *
1128  * Decrease reference counter of service
1129  */
1130 void connman_service_unref(struct connman_service *service)
1131 {
1132         __connman_service_put(service);
1133 }
1134
1135 static gint service_compare(gconstpointer a, gconstpointer b,
1136                                                         gpointer user_data)
1137 {
1138         struct connman_service *service_a = (void *) a;
1139         struct connman_service *service_b = (void *) b;
1140
1141         if (service_a->state != service_b->state) {
1142                 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1143                         return -1;
1144                 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1145                         return 1;
1146         }
1147
1148         if (service_a->order > service_b->order)
1149                 return -1;
1150
1151         if (service_a->order < service_b->order)
1152                 return 1;
1153
1154         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1155                 return -1;
1156
1157         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1158                 return 1;
1159
1160         if (service_a->type != service_b->type) {
1161                 switch (service_a->type) {
1162                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1163                 case CONNMAN_SERVICE_TYPE_ETHERNET:
1164                         break;
1165                 case CONNMAN_SERVICE_TYPE_WIFI:
1166                         return 1;
1167                 case CONNMAN_SERVICE_TYPE_WIMAX:
1168                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1169                 case CONNMAN_SERVICE_TYPE_CELLULAR:
1170                         return -1;
1171                 }
1172         }
1173
1174         return (gint) service_b->strength - (gint) service_a->strength;
1175 }
1176
1177 /**
1178  * connman_service_set_favorite:
1179  * @service: service structure
1180  * @favorite: favorite value
1181  *
1182  * Change the favorite setting of service
1183  */
1184 int connman_service_set_favorite(struct connman_service *service,
1185                                                 connman_bool_t favorite)
1186 {
1187         GSequenceIter *iter;
1188
1189         iter = g_hash_table_lookup(service_hash, service->identifier);
1190         if (iter == NULL)
1191                 return -ENOENT;
1192
1193         if (service->favorite == favorite)
1194                 return -EALREADY;
1195
1196         service->favorite = favorite;
1197
1198         g_sequence_sort_changed(iter, service_compare, NULL);
1199
1200         __connman_profile_changed(FALSE);
1201
1202         return 0;
1203 }
1204
1205 static void default_changed(void)
1206 {
1207         DBusMessage *signal;
1208         DBusMessageIter entry, value;
1209         const char *key = "DefaultTechnology";
1210         const char *str = __connman_service_default();
1211
1212         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1213                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1214         if (signal == NULL)
1215                 return;
1216
1217         dbus_message_iter_init_append(signal, &entry);
1218
1219         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1220
1221         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1222                                         DBUS_TYPE_STRING_AS_STRING, &value);
1223         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
1224         dbus_message_iter_close_container(&entry, &value);
1225
1226         g_dbus_send_message(connection, signal);
1227 }
1228
1229 int __connman_service_indicate_state(struct connman_service *service,
1230                                         enum connman_service_state state)
1231 {
1232         GSequenceIter *iter;
1233
1234         DBG("service %p state %d", service, state);
1235
1236         if (service == NULL)
1237                 return -EINVAL;
1238
1239         if (service->state == state)
1240                 return -EALREADY;
1241
1242         if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1243                                 state == CONNMAN_SERVICE_STATE_IDLE)
1244                 return -EINVAL;
1245
1246         if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1247                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1248                 return -EINVAL;
1249
1250         if (state == CONNMAN_SERVICE_STATE_IDLE &&
1251                         service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1252                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1253                 state_changed(service);
1254
1255                 reply_pending(service, ECONNABORTED);
1256
1257                 __connman_service_disconnect(service);
1258         }
1259
1260         service->state = state;
1261         state_changed(service);
1262
1263         if (state == CONNMAN_SERVICE_STATE_READY) {
1264                 connman_service_set_favorite(service, TRUE);
1265
1266                 reply_pending(service, 0);
1267
1268                 service->userconnect = FALSE;
1269                 service->failcounter = 0;
1270
1271                 g_get_current_time(&service->modified);
1272                 __connman_storage_save_service(service);
1273
1274                 __connman_notifier_connect(service->type);
1275
1276                 default_changed();
1277         } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1278                 default_changed();
1279
1280                 __connman_notifier_disconnect(service->type);
1281         }
1282
1283         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1284                 if (service->failcounter++ < MAX_CONNECT_RETRIES) {
1285                         connman_warn("Connecting again (try %d)",
1286                                                 service->failcounter);
1287                         __connman_service_disconnect(service);
1288                         __connman_service_connect(service);
1289                         return 0;
1290                 }
1291
1292                 reply_pending(service, EIO);
1293
1294                 if (service->userconnect == FALSE)
1295                         __connman_service_auto_connect();
1296
1297                 g_get_current_time(&service->modified);
1298                 __connman_storage_save_service(service);
1299         } else
1300                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1301
1302         iter = g_hash_table_lookup(service_hash, service->identifier);
1303         if (iter != NULL)
1304                 g_sequence_sort_changed(iter, service_compare, NULL);
1305
1306         __connman_profile_changed(FALSE);
1307
1308         if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1309                         service->state == CONNMAN_SERVICE_STATE_FAILURE)
1310                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1311
1312         return 0;
1313 }
1314
1315 int __connman_service_indicate_error(struct connman_service *service,
1316                                         enum connman_service_error error)
1317 {
1318         DBG("service %p error %d", service, error);
1319
1320         if (service == NULL)
1321                 return -EINVAL;
1322
1323         service->error = error;
1324
1325         return __connman_service_indicate_state(service,
1326                                         CONNMAN_SERVICE_STATE_FAILURE);
1327 }
1328
1329 int __connman_service_indicate_default(struct connman_service *service)
1330 {
1331         DBG("service %p", service);
1332
1333         default_changed();
1334
1335         return 0;
1336 }
1337
1338 static connman_bool_t prepare_network(struct connman_service *service)
1339 {
1340         enum connman_network_type type;
1341         unsigned int ssid_len;
1342
1343         type = connman_network_get_type(service->network);
1344
1345         switch (type) {
1346         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1347         case CONNMAN_NETWORK_TYPE_VENDOR:
1348                 return FALSE;
1349         case CONNMAN_NETWORK_TYPE_WIFI:
1350                 if (connman_network_get_blob(service->network, "WiFi.SSID",
1351                                                         &ssid_len) == NULL)
1352                         return FALSE;
1353
1354                 connman_network_set_string(service->network,
1355                                 "WiFi.Passphrase", service->passphrase);
1356                 break;
1357         case CONNMAN_NETWORK_TYPE_WIMAX:
1358         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1359         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1360                 break;
1361         case CONNMAN_NETWORK_TYPE_MBM:
1362         case CONNMAN_NETWORK_TYPE_HSO:
1363                 connman_network_set_string(service->network,
1364                                                 "Cellular.APN", service->apn);
1365                 break;
1366         }
1367
1368         return TRUE;
1369 }
1370
1371 int __connman_service_connect(struct connman_service *service)
1372 {
1373         int err;
1374
1375         DBG("service %p", service);
1376
1377         if (service->state == CONNMAN_SERVICE_STATE_READY)
1378                 return -EISCONN;
1379
1380         if (is_connecting(service) == TRUE)
1381                 return -EALREADY;
1382
1383         switch (service->type) {
1384         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1385                 return -EINVAL;
1386         case CONNMAN_SERVICE_TYPE_ETHERNET:
1387         case CONNMAN_SERVICE_TYPE_WIMAX:
1388         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1389                 break;
1390         case CONNMAN_SERVICE_TYPE_CELLULAR:
1391                 if (service->apn == NULL)
1392                         return -EINVAL;
1393                 break;
1394         case CONNMAN_SERVICE_TYPE_WIFI:
1395                 switch (service->security) {
1396                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1397                 case CONNMAN_SERVICE_SECURITY_NONE:
1398                         break;
1399                 case CONNMAN_SERVICE_SECURITY_WEP:
1400                 case CONNMAN_SERVICE_SECURITY_WPA:
1401                 case CONNMAN_SERVICE_SECURITY_RSN:
1402                         if (service->passphrase == NULL)
1403                                 return -ENOKEY;
1404                         break;
1405                 }
1406                 break;
1407         }
1408
1409         if (service->network != NULL) {
1410                 if (prepare_network(service) == FALSE)
1411                         return -EINVAL;
1412
1413                 __connman_ipconfig_enable(service->ipconfig);
1414
1415                 err = __connman_network_connect(service->network);
1416         } else if (service->device != NULL) {
1417                 if (service->favorite == FALSE)
1418                         return -ENOLINK;
1419
1420                 __connman_ipconfig_enable(service->ipconfig);
1421
1422                 err = __connman_device_connect(service->device);
1423         } else
1424                 return -EOPNOTSUPP;
1425
1426         if (err < 0) {
1427                 if (err != -EINPROGRESS) {
1428                         __connman_ipconfig_disable(service->ipconfig);
1429                         return err;
1430                 }
1431
1432                 service->timeout = g_timeout_add_seconds(45,
1433                                                 connect_timeout, service);
1434
1435                 return -EINPROGRESS;
1436         }
1437
1438         return 0;
1439 }
1440
1441 int __connman_service_disconnect(struct connman_service *service)
1442 {
1443         int err;
1444
1445         DBG("service %p", service);
1446
1447         if (service->network != NULL) {
1448                 err = __connman_network_disconnect(service->network);
1449         } else if (service->device != NULL) {
1450                 if (service->favorite == FALSE)
1451                         return -ENOLINK;
1452                 err = __connman_device_disconnect(service->device);
1453         } else
1454                 return -EOPNOTSUPP;
1455
1456         __connman_ipconfig_disable(service->ipconfig);
1457
1458         if (err < 0) {
1459                 if (err != -EINPROGRESS)
1460                         return err;
1461
1462                 return -EINPROGRESS;
1463         }
1464
1465         return 0;
1466 }
1467
1468 /**
1469  * __connman_service_lookup:
1470  * @identifier: service identifier
1471  *
1472  * Look up a service by identifier (reference count will not be increased)
1473  */
1474 static struct connman_service *__connman_service_lookup(const char *identifier)
1475 {
1476         GSequenceIter *iter;
1477
1478         iter = g_hash_table_lookup(service_hash, identifier);
1479         if (iter != NULL)
1480                 return g_sequence_get(iter);
1481
1482         return NULL;
1483 }
1484
1485 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1486                 const char *ssid, const char *mode, const char *security)
1487 {
1488         struct connman_network *network;
1489         char *name;
1490         int index;
1491         unsigned int i, ssid_len;
1492
1493         ssid_len = strlen(ssid);
1494         if (ssid_len < 1)
1495                 return NULL;
1496
1497         network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1498         if (network == NULL)
1499                 return NULL;
1500
1501         connman_network_set_blob(network, "WiFi.SSID",
1502                                         (unsigned char *) ssid, ssid_len);
1503
1504         connman_network_set_string(network, "WiFi.Mode", mode);
1505         connman_network_set_string(network, "WiFi.Security", security);
1506
1507         name = g_try_malloc0(ssid_len + 1);
1508         if (name == NULL) {
1509                 connman_network_unref(network);
1510                 return NULL;
1511         }
1512
1513         for (i = 0; i < ssid_len; i++) {
1514                 if (g_ascii_isprint(ssid[i]))
1515                         name[i] = ssid[i];
1516                 else
1517                         name[i] = ' ';
1518         }
1519
1520         connman_network_set_name(network, name);
1521
1522         g_free(name);
1523
1524         index = connman_device_get_index(device);
1525         connman_network_set_index(network, index);
1526
1527         connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1528
1529         if (connman_device_add_network(device, network) < 0) {
1530                 connman_network_unref(network);
1531                 return NULL;
1532         }
1533
1534         connman_network_set_available(network, TRUE);
1535
1536         return network;
1537 }
1538
1539 int __connman_service_create_and_connect(DBusMessage *msg)
1540 {
1541         struct connman_service *service;
1542         struct connman_network *network;
1543         struct connman_device *device;
1544         DBusMessageIter iter, array;
1545         const char *mode = "managed", *security = "none";
1546         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1547         unsigned int ssid_len = 0;
1548         const char *ident;
1549         char *name, *group;
1550         gboolean created = FALSE;
1551         int err;
1552
1553         dbus_message_iter_init(msg, &iter);
1554         dbus_message_iter_recurse(&iter, &array);
1555
1556         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1557                 DBusMessageIter entry, value;
1558                 const char *key;
1559
1560                 dbus_message_iter_recurse(&array, &entry);
1561                 dbus_message_iter_get_basic(&entry, &key);
1562
1563                 dbus_message_iter_next(&entry);
1564                 dbus_message_iter_recurse(&entry, &value);
1565
1566                 switch (dbus_message_iter_get_arg_type(&value)) {
1567                 case DBUS_TYPE_STRING:
1568                         if (g_str_equal(key, "Type") == TRUE)
1569                                 dbus_message_iter_get_basic(&value, &type);
1570                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1571                                         g_str_equal(key, "Mode") == TRUE)
1572                                 dbus_message_iter_get_basic(&value, &mode);
1573                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1574                                         g_str_equal(key, "Security") == TRUE)
1575                                 dbus_message_iter_get_basic(&value, &security);
1576                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1577                                         g_str_equal(key, "Passphrase") == TRUE)
1578                                 dbus_message_iter_get_basic(&value, &passphrase);
1579                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1580                                         g_str_equal(key, "SSID") == TRUE)
1581                                 dbus_message_iter_get_basic(&value, &ssid);
1582                 }
1583
1584                 dbus_message_iter_next(&array);
1585         }
1586
1587         if (type == NULL)
1588                 return -EINVAL;
1589
1590         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1591                 return -EOPNOTSUPP;
1592
1593         if (ssid == NULL)
1594                 return -EINVAL;
1595
1596         ssid_len = strlen(ssid);
1597         if (ssid_len < 1)
1598                 return -EINVAL;
1599
1600         if (g_strcmp0(security, "none") != 0 &&
1601                         g_strcmp0(security, "wep") != 0 &&
1602                                 g_strcmp0(security, "wpa") != 0 &&
1603                                         g_strcmp0(security, "rsn") != 0)
1604                 return -EINVAL;
1605
1606         device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
1607         if (device == NULL)
1608                 return -EOPNOTSUPP;
1609
1610         ident = __connman_device_get_ident(device);
1611         if (ident == NULL)
1612                 return -EOPNOTSUPP;
1613
1614         group = connman_wifi_build_group_name((unsigned char *) ssid,
1615                                                 ssid_len, mode, security);
1616         if (group == NULL)
1617                 return -EINVAL;
1618
1619         name = g_strdup_printf("%s_%s_%s", type, ident, group);
1620
1621         service = __connman_service_lookup(name);
1622
1623         if (service != NULL)
1624                 goto done;
1625
1626         network = create_hidden_wifi(device, ssid, mode, security);
1627         if (network != NULL) {
1628                 connman_network_set_group(network, group);
1629                 created = TRUE;
1630         }
1631
1632         service = __connman_service_lookup(name);
1633
1634 done:
1635         g_free(name);
1636         g_free(group);
1637
1638         if (service == NULL) {
1639                 err = -EOPNOTSUPP;
1640                 goto failed;
1641         }
1642
1643         __connman_device_disconnect(device);
1644
1645         if (passphrase != NULL) {
1646                 g_free(service->passphrase);
1647                 service->passphrase = g_strdup(passphrase);
1648         }
1649
1650         service->userconnect = TRUE;
1651
1652         err = __connman_service_connect(service);
1653         if (err < 0 && err != -EINPROGRESS)
1654                 goto failed;
1655
1656         g_dbus_send_reply(connection, msg,
1657                                 DBUS_TYPE_OBJECT_PATH, &service->path,
1658                                                         DBUS_TYPE_INVALID);
1659
1660         return 0;
1661
1662 failed:
1663         if (service != NULL && created == TRUE) {
1664                 struct connman_network *network = service->network;
1665
1666                 if (network != NULL) {
1667                         connman_network_set_available(network, FALSE);
1668                         __connman_device_cleanup_networks(device);
1669                 }
1670
1671                 __connman_service_put(service);
1672         }
1673
1674         return err;
1675 }
1676
1677 /**
1678  * __connman_service_get:
1679  * @identifier: service identifier
1680  *
1681  * Look up a service by identifier or create a new one if not found
1682  */
1683 static struct connman_service *__connman_service_get(const char *identifier)
1684 {
1685         struct connman_service *service;
1686         GSequenceIter *iter;
1687
1688         iter = g_hash_table_lookup(service_hash, identifier);
1689         if (iter != NULL) {
1690                 service = g_sequence_get(iter);
1691                 if (service != NULL)
1692                         g_atomic_int_inc(&service->refcount);
1693                 return service;
1694         }
1695
1696         service = connman_service_create();
1697         if (service == NULL)
1698                 return NULL;
1699
1700         DBG("service %p", service);
1701
1702         service->identifier = g_strdup(identifier);
1703
1704         service->profile = g_strdup(__connman_profile_active_ident());
1705
1706         __connman_storage_load_service(service);
1707
1708         iter = g_sequence_insert_sorted(service_list, service,
1709                                                 service_compare, NULL);
1710
1711         g_hash_table_insert(service_hash, service->identifier, iter);
1712
1713         return service;
1714 }
1715
1716 static int service_register(struct connman_service *service)
1717 {
1718         const char *path = __connman_profile_active_path();
1719         GSequenceIter *iter;
1720
1721         DBG("service %p", service);
1722
1723         if (service->path != NULL)
1724                 return -EALREADY;
1725
1726         service->path = g_strdup_printf("%s/%s", path, service->identifier);
1727
1728         DBG("path %s", service->path);
1729
1730         g_dbus_register_interface(connection, service->path,
1731                                         CONNMAN_SERVICE_INTERFACE,
1732                                         service_methods, service_signals,
1733                                                         NULL, service, NULL);
1734
1735         __connman_storage_load_service(service);
1736
1737         iter = g_hash_table_lookup(service_hash, service->identifier);
1738         if (iter != NULL)
1739                 g_sequence_sort_changed(iter, service_compare, NULL);
1740
1741         __connman_profile_changed(TRUE);
1742
1743         return 0;
1744 }
1745
1746 static void service_up(struct connman_ipconfig *ipconfig)
1747 {
1748         connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
1749 }
1750
1751 static void service_down(struct connman_ipconfig *ipconfig)
1752 {
1753         connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
1754 }
1755
1756 static void service_lower_up(struct connman_ipconfig *ipconfig)
1757 {
1758         connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
1759 }
1760
1761 static void service_lower_down(struct connman_ipconfig *ipconfig)
1762 {
1763         connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
1764 }
1765
1766 static void service_ip_bound(struct connman_ipconfig *ipconfig)
1767 {
1768         connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
1769 }
1770
1771 static void service_ip_release(struct connman_ipconfig *ipconfig)
1772 {
1773         connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
1774 }
1775
1776 static const struct connman_ipconfig_ops service_ops = {
1777         .up             = service_up,
1778         .down           = service_down,
1779         .lower_up       = service_lower_up,
1780         .lower_down     = service_lower_down,
1781         .ip_bound       = service_ip_bound,
1782         .ip_release     = service_ip_release,
1783 };
1784
1785 static void setup_ipconfig(struct connman_service *service, int index)
1786 {
1787         if (index < 0)
1788                 return;
1789
1790         service->ipconfig = connman_ipconfig_create(index);
1791         if (service->ipconfig == NULL)
1792                 return;
1793
1794         connman_ipconfig_set_method(service->ipconfig,
1795                                         CONNMAN_IPCONFIG_METHOD_DHCP);
1796
1797         __connman_storage_load_service(service);
1798
1799         connman_ipconfig_set_data(service->ipconfig, service);
1800
1801         connman_ipconfig_set_ops(service->ipconfig, &service_ops);
1802         connman_ipconfig_set_ops(service->ipconfig, NULL);
1803 }
1804
1805 /**
1806  * __connman_service_lookup_from_device:
1807  * @device: device structure
1808  *
1809  * Look up a service by device (reference count will not be increased)
1810  */
1811 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1812 {
1813         struct connman_service *service;
1814         const char *ident;
1815         char *name;
1816
1817         ident = __connman_device_get_ident(device);
1818         if (ident == NULL)
1819                 return NULL;
1820
1821         name = g_strdup_printf("%s_%s",
1822                                 __connman_device_get_type(device), ident);
1823         service = __connman_service_lookup(name);
1824         g_free(name);
1825
1826         return service;
1827 }
1828
1829 /**
1830  * __connman_service_create_from_device:
1831  * @device: device structure
1832  *
1833  * Look up service by device and if not found, create one
1834  */
1835 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1836 {
1837         struct connman_service *service;
1838         const char *ident;
1839         char *name;
1840
1841         ident = __connman_device_get_ident(device);
1842         if (ident == NULL)
1843                 return NULL;
1844
1845         name = g_strdup_printf("%s_%s",
1846                                 __connman_device_get_type(device), ident);
1847         service = __connman_service_get(name);
1848         g_free(name);
1849
1850         if (service == NULL)
1851                 return NULL;
1852
1853         if (service->path != NULL) {
1854                 __connman_profile_changed(TRUE);
1855                 return service;
1856         }
1857
1858         service->type = __connman_device_get_service_type(device);
1859
1860         service->autoconnect = FALSE;
1861
1862         service->device = device;
1863
1864         setup_ipconfig(service, connman_device_get_index(device));
1865
1866         service_register(service);
1867
1868         __connman_profile_changed(TRUE);
1869
1870         if (service->favorite == TRUE)
1871                 __connman_service_auto_connect();
1872
1873         return service;
1874 }
1875
1876 void __connman_service_remove_from_device(struct connman_device *device)
1877 {
1878         struct connman_service *service;
1879         enum connman_service_type type;
1880
1881         service = __connman_service_lookup_from_device(device);
1882         if (service == NULL)
1883                 return;
1884
1885         type = service->type;
1886
1887         __connman_service_put(service);
1888
1889         default_changed();
1890
1891         __connman_notifier_disconnect(type);
1892
1893         __connman_service_auto_connect();
1894 }
1895
1896 /**
1897  * __connman_service_lookup_from_network:
1898  * @network: network structure
1899  *
1900  * Look up a service by network (reference count will not be increased)
1901  */
1902 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1903 {
1904         struct connman_service *service;
1905         const char *ident, *group;
1906         char *name;
1907
1908         ident = __connman_network_get_ident(network);
1909         if (ident == NULL)
1910                 return NULL;
1911
1912         group = connman_network_get_group(network);
1913         if (group == NULL)
1914                 return NULL;
1915
1916         name = g_strdup_printf("%s_%s_%s",
1917                         __connman_network_get_type(network), ident, group);
1918         service = __connman_service_lookup(name);
1919         g_free(name);
1920
1921         return service;
1922 }
1923
1924 const char *__connman_service_get_path(struct connman_service *service)
1925 {
1926         return service->path;
1927 }
1928
1929 unsigned int __connman_service_get_order(struct connman_service *service)
1930 {
1931         GSequenceIter *iter;
1932
1933         if (service == NULL)
1934                 return 0;
1935
1936         if (service->favorite == FALSE) {
1937                 service->order = 0;
1938                 goto done;
1939         }
1940
1941         iter = g_hash_table_lookup(service_hash, service->identifier);
1942         if (iter != NULL) {
1943                 if (g_sequence_iter_get_position(iter) == 0)
1944                         service->order = 1;
1945                 else
1946                         service->order = 0;
1947         }
1948
1949 done:
1950         return service->order;
1951 }
1952
1953 static enum connman_service_type convert_network_type(struct connman_network *network)
1954 {
1955         enum connman_network_type type = connman_network_get_type(network);
1956
1957         switch (type) {
1958         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1959         case CONNMAN_NETWORK_TYPE_VENDOR:
1960                 break;
1961         case CONNMAN_NETWORK_TYPE_WIFI:
1962                 return CONNMAN_SERVICE_TYPE_WIFI;
1963         case CONNMAN_NETWORK_TYPE_WIMAX:
1964                 return CONNMAN_SERVICE_TYPE_WIMAX;
1965         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1966         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1967                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
1968         case CONNMAN_NETWORK_TYPE_MBM:
1969         case CONNMAN_NETWORK_TYPE_HSO:
1970                 return CONNMAN_SERVICE_TYPE_CELLULAR;
1971         }
1972
1973         return CONNMAN_SERVICE_TYPE_UNKNOWN;
1974 }
1975
1976 static enum connman_service_mode convert_wifi_mode(const char *mode)
1977 {
1978         if (mode == NULL)
1979                 return CONNMAN_SERVICE_MODE_UNKNOWN;
1980         else if (g_str_equal(mode, "managed") == TRUE)
1981                 return CONNMAN_SERVICE_MODE_MANAGED;
1982         else if (g_str_equal(mode, "adhoc") == TRUE)
1983                 return CONNMAN_SERVICE_MODE_ADHOC;
1984         else
1985                 return CONNMAN_SERVICE_MODE_UNKNOWN;
1986 }
1987
1988 static enum connman_service_mode convert_wifi_security(const char *security)
1989 {
1990         if (security == NULL)
1991                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1992         else if (g_str_equal(security, "none") == TRUE)
1993                 return CONNMAN_SERVICE_SECURITY_NONE;
1994         else if (g_str_equal(security, "wep") == TRUE)
1995                 return CONNMAN_SERVICE_SECURITY_WEP;
1996         else if (g_str_equal(security, "wpa") == TRUE)
1997                 return CONNMAN_SERVICE_SECURITY_WPA;
1998         else if (g_str_equal(security, "rsn") == TRUE)
1999                 return CONNMAN_SERVICE_SECURITY_RSN;
2000         else
2001                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2002 }
2003
2004 static void update_from_network(struct connman_service *service,
2005                                         struct connman_network *network)
2006 {
2007         connman_uint8_t strength = service->strength;
2008         GSequenceIter *iter;
2009         const char *str;
2010
2011         if (service->state == CONNMAN_SERVICE_STATE_READY)
2012                 return;
2013
2014         if (is_connecting(service) == TRUE)
2015                 return;
2016
2017         str = connman_network_get_string(network, "Name");
2018         if (str != NULL) {
2019                 g_free(service->name);
2020                 service->name = g_strdup(str);
2021                 service->hidden = FALSE;
2022         } else {
2023                 g_free(service->name);
2024                 service->name = NULL;
2025                 service->hidden = TRUE;
2026         }
2027
2028         service->strength = connman_network_get_uint8(network, "Strength");
2029
2030         str = connman_network_get_string(network, "WiFi.Mode");
2031         service->mode = convert_wifi_mode(str);
2032
2033         str = connman_network_get_string(network, "WiFi.Security");
2034         service->security = convert_wifi_security(str);
2035
2036         if (service->strength > strength && service->network != NULL) {
2037                 connman_network_unref(service->network);
2038                 service->network = connman_network_ref(network);
2039
2040                 strength_changed(service);
2041         }
2042
2043         if (service->network == NULL)
2044                 service->network = connman_network_ref(network);
2045
2046         iter = g_hash_table_lookup(service_hash, service->identifier);
2047         if (iter != NULL)
2048                 g_sequence_sort_changed(iter, service_compare, NULL);
2049 }
2050
2051 /**
2052  * __connman_service_create_from_network:
2053  * @network: network structure
2054  *
2055  * Look up service by network and if not found, create one
2056  */
2057 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2058 {
2059         struct connman_service *service;
2060         const char *ident, *group;
2061         char *name;
2062
2063         ident = __connman_network_get_ident(network);
2064         if (ident == NULL)
2065                 return NULL;
2066
2067         group = connman_network_get_group(network);
2068         if (group == NULL)
2069                 return NULL;
2070
2071         name = g_strdup_printf("%s_%s_%s",
2072                         __connman_network_get_type(network), ident, group);
2073         service = __connman_service_get(name);
2074         g_free(name);
2075
2076         if (service == NULL)
2077                 return NULL;
2078
2079         if (__connman_network_get_weakness(network) == TRUE)
2080                 return service;
2081
2082         if (service->path != NULL) {
2083                 update_from_network(service, network);
2084                 __connman_profile_changed(TRUE);
2085                 return service;
2086         }
2087
2088         service->type = convert_network_type(network);
2089
2090         switch (service->type) {
2091         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2092         case CONNMAN_SERVICE_TYPE_ETHERNET:
2093         case CONNMAN_SERVICE_TYPE_WIMAX:
2094         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2095         case CONNMAN_SERVICE_TYPE_CELLULAR:
2096                 service->autoconnect = FALSE;
2097                 break;
2098         case CONNMAN_SERVICE_TYPE_WIFI:
2099                 service->autoconnect = TRUE;
2100                 break;
2101         }
2102
2103         service->state = CONNMAN_SERVICE_STATE_IDLE;
2104
2105         update_from_network(service, network);
2106
2107         setup_ipconfig(service, connman_network_get_index(network));
2108
2109         service_register(service);
2110
2111         __connman_profile_changed(TRUE);
2112
2113         if (service->favorite == TRUE)
2114                 __connman_service_auto_connect();
2115
2116         return service;
2117 }
2118
2119 void __connman_service_update_from_network(struct connman_network *network)
2120 {
2121         struct connman_service *service;
2122         connman_uint8_t strength;
2123
2124         service = __connman_service_lookup_from_network(network);
2125         if (service == NULL)
2126                 return;
2127
2128         if (service->network == NULL)
2129                 return;
2130
2131         strength = connman_network_get_uint8(service->network, "Strength");
2132         if (strength == service->strength)
2133                 return;
2134
2135         service->strength = strength;
2136
2137         strength_changed(service);
2138 }
2139
2140 void __connman_service_remove_from_network(struct connman_network *network)
2141 {
2142         struct connman_service *service;
2143
2144         service = __connman_service_lookup_from_network(network);
2145         if (service == NULL)
2146                 return;
2147
2148         __connman_service_put(service);
2149 }
2150
2151 static int service_load(struct connman_service *service)
2152 {
2153         const char *ident = service->profile;
2154         GKeyFile *keyfile;
2155         GError *error = NULL;
2156         gchar *pathname, *data = NULL;
2157         gsize length;
2158         gchar *str;
2159         connman_bool_t autoconnect;
2160         unsigned int ssid_len;
2161         int err = 0;
2162
2163         DBG("service %p", service);
2164
2165         if (ident == NULL)
2166                 return -EINVAL;
2167
2168         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2169         if (pathname == NULL)
2170                 return -ENOMEM;
2171
2172         keyfile = g_key_file_new();
2173
2174         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2175                 g_free(pathname);
2176                 return -ENOENT;
2177         }
2178
2179         g_free(pathname);
2180
2181         if (g_key_file_load_from_data(keyfile, data, length,
2182                                                         0, NULL) == FALSE) {
2183                 g_free(data);
2184                 return -EILSEQ;
2185         }
2186
2187         g_free(data);
2188
2189         switch (service->type) {
2190         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2191         case CONNMAN_SERVICE_TYPE_ETHERNET:
2192                 break;
2193         case CONNMAN_SERVICE_TYPE_WIFI:
2194                 if (service->name == NULL) {
2195                         gchar *name;
2196
2197                         name = g_key_file_get_string(keyfile,
2198                                         service->identifier, "Name", NULL);
2199                         if (name != NULL) {
2200                                 g_free(service->name);
2201                                 service->name = name;
2202                         }
2203
2204                         if (service->network != NULL)
2205                                 connman_network_set_name(service->network,
2206                                                                         name);
2207                 }
2208
2209                 if (service->network &&
2210                                 connman_network_get_blob(service->network,
2211                                         "WiFi.SSID", &ssid_len) == NULL) {
2212                         gchar *hex_ssid;
2213
2214                         hex_ssid = g_key_file_get_string(keyfile,
2215                                                         service->identifier,
2216                                                                 "SSID", NULL);
2217
2218                         if (hex_ssid != NULL) {
2219                                 gchar *ssid;
2220                                 unsigned int i, j = 0, hex;
2221                                 size_t hex_ssid_len = strlen(hex_ssid);
2222
2223                                 ssid = g_try_malloc0(hex_ssid_len / 2);
2224                                 if (ssid == NULL) {
2225                                         g_free(hex_ssid);
2226                                         err = -ENOMEM;
2227                                         goto done;
2228                                 }
2229
2230                                 for (i = 0; i < hex_ssid_len; i += 2) {
2231                                         sscanf(hex_ssid + i, "%02x", &hex);
2232                                         ssid[j++] = hex;
2233                                 }
2234
2235                                 connman_network_set_blob(service->network,
2236                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
2237                         }
2238
2239                         g_free(hex_ssid);
2240                 }
2241                 /* fall through */
2242
2243         case CONNMAN_SERVICE_TYPE_WIMAX:
2244         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2245         case CONNMAN_SERVICE_TYPE_CELLULAR:
2246                 service->apn = g_key_file_get_string(keyfile,
2247                                 service->identifier, "APN", NULL);
2248
2249                 service->favorite = g_key_file_get_boolean(keyfile,
2250                                 service->identifier, "Favorite", NULL);
2251
2252                 autoconnect = g_key_file_get_boolean(keyfile,
2253                                 service->identifier, "AutoConnect", &error);
2254                 if (error == NULL)
2255                         service->autoconnect = autoconnect;
2256                 g_clear_error(&error);
2257
2258                 str = g_key_file_get_string(keyfile,
2259                                 service->identifier, "Failure", NULL);
2260                 if (str != NULL) {
2261                         service->state = CONNMAN_SERVICE_STATE_FAILURE;
2262                         service->error = string2error(str);
2263                 }
2264                 break;
2265         }
2266
2267         str = g_key_file_get_string(keyfile,
2268                                 service->identifier, "Modified", NULL);
2269         if (str != NULL) {
2270                 g_time_val_from_iso8601(str, &service->modified);
2271                 g_free(str);
2272         }
2273
2274         str = g_key_file_get_string(keyfile,
2275                                 service->identifier, "Passphrase", NULL);
2276         if (str != NULL) {
2277                 g_free(service->passphrase);
2278                 service->passphrase = str;
2279         }
2280
2281         if (service->ipconfig != NULL)
2282                 __connman_ipconfig_load(service->ipconfig, keyfile,
2283                                         service->identifier, "IPv4.");
2284
2285 done:
2286         g_key_file_free(keyfile);
2287
2288         return err;
2289 }
2290
2291 static int service_save(struct connman_service *service)
2292 {
2293         const char *ident = service->profile;
2294         GKeyFile *keyfile;
2295         gchar *pathname, *data = NULL;
2296         gsize length;
2297         gchar *str;
2298         int err = 0;
2299
2300         DBG("service %p", service);
2301
2302         if (ident == NULL)
2303                 return -EINVAL;
2304
2305         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2306         if (pathname == NULL)
2307                 return -ENOMEM;
2308
2309         keyfile = g_key_file_new();
2310
2311         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2312                 goto update;
2313
2314         if (length > 0) {
2315                 if (g_key_file_load_from_data(keyfile, data, length,
2316                                                         0, NULL) == FALSE)
2317                         goto done;
2318         }
2319
2320         g_free(data);
2321
2322 update:
2323         if (service->name != NULL)
2324                 g_key_file_set_string(keyfile, service->identifier,
2325                                                 "Name", service->name);
2326
2327         switch (service->type) {
2328         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2329         case CONNMAN_SERVICE_TYPE_ETHERNET:
2330                 break;
2331         case CONNMAN_SERVICE_TYPE_WIFI:
2332                 if (service->network) {
2333                         const unsigned char *ssid;
2334                         unsigned int ssid_len = 0;
2335
2336                         ssid = connman_network_get_blob(service->network,
2337                                                         "WiFi.SSID", &ssid_len);
2338
2339                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2340                                 char *identifier = service->identifier;
2341                                 GString *str;
2342                                 unsigned int i;
2343
2344                                 str = g_string_sized_new(ssid_len * 2);
2345                                 if (str == NULL) {
2346                                         err = -ENOMEM;
2347                                         goto done;
2348                                 }
2349
2350                                 for (i = 0; i < ssid_len; i++)
2351                                         g_string_append_printf(str,
2352                                                         "%02x", ssid[i]);
2353
2354                                 g_key_file_set_string(keyfile, identifier,
2355                                                         "SSID", str->str);
2356
2357                                 g_string_free(str, TRUE);
2358                         }
2359                 }
2360                 /* fall through */
2361
2362         case CONNMAN_SERVICE_TYPE_WIMAX:
2363         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2364         case CONNMAN_SERVICE_TYPE_CELLULAR:
2365                 if (service->apn != NULL)
2366                         g_key_file_set_string(keyfile, service->identifier,
2367                                                         "APN", service->apn);
2368
2369                 g_key_file_set_boolean(keyfile, service->identifier,
2370                                         "Favorite", service->favorite);
2371
2372                 if (service->favorite == TRUE)
2373                         g_key_file_set_boolean(keyfile, service->identifier,
2374                                         "AutoConnect", service->autoconnect);
2375
2376                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2377                         const char *failure = error2string(service->error);
2378                         if (failure != NULL)
2379                                 g_key_file_set_string(keyfile,
2380                                                         service->identifier,
2381                                                         "Failure", failure);
2382                 } else {
2383                         g_key_file_remove_key(keyfile, service->identifier,
2384                                                         "Failure", NULL);
2385                 }
2386                 break;
2387         }
2388
2389         str = g_time_val_to_iso8601(&service->modified);
2390         if (str != NULL) {
2391                 g_key_file_set_string(keyfile, service->identifier,
2392                                                         "Modified", str);
2393                 g_free(str);
2394         }
2395
2396         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2397                 g_key_file_set_string(keyfile, service->identifier,
2398                                         "Passphrase", service->passphrase);
2399         else
2400                 g_key_file_remove_key(keyfile, service->identifier,
2401                                                         "Passphrase", NULL);
2402
2403         if (service->ipconfig != NULL)
2404                 __connman_ipconfig_save(service->ipconfig, keyfile,
2405                                         service->identifier, "IPv4.");
2406
2407         data = g_key_file_to_data(keyfile, &length, NULL);
2408
2409         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2410                 connman_error("Failed to store service information");
2411
2412 done:
2413         g_free(data);
2414
2415         g_key_file_free(keyfile);
2416
2417         g_free(pathname);
2418
2419         return err;
2420 }
2421
2422 static struct connman_storage service_storage = {
2423         .name           = "service",
2424         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
2425         .service_load   = service_load,
2426         .service_save   = service_save,
2427 };
2428
2429 int __connman_service_init(void)
2430 {
2431         DBG("");
2432
2433         connection = connman_dbus_get_connection();
2434
2435         if (connman_storage_register(&service_storage) < 0)
2436                 connman_error("Failed to register service storage");
2437
2438         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2439                                                                 NULL, NULL);
2440
2441         service_list = g_sequence_new(service_free);
2442
2443         return 0;
2444 }
2445
2446 void __connman_service_cleanup(void)
2447 {
2448         DBG("");
2449
2450         g_sequence_free(service_list);
2451         service_list = NULL;
2452
2453         g_hash_table_destroy(service_hash);
2454         service_hash = NULL;
2455
2456         connman_storage_unregister(&service_storage);
2457
2458         dbus_connection_unref(connection);
2459 }