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