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