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