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