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