Merge "Add dbus method for getting wifi passphrase" into tizen
[platform/core/connectivity/net-config.git] / src / dbus / netsupplicant.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "log.h"
21 #include "netdbus.h"
22 #include "netsupplicant.h"
23
24 char *netconfig_get_supplicant_interface(const char *ifname)
25 {
26         GVariant *message = NULL;
27         GVariant *params = NULL;
28         gchar *path = NULL;
29
30         if (!ifname)
31                 return NULL;
32
33         params = g_variant_new("(s)", ifname);
34
35         message = netconfig_supplicant_invoke_dbus_method(
36                         SUPPLICANT_SERVICE, SUPPLICANT_PATH,
37                         SUPPLICANT_INTERFACE, "GetInterface", params);
38
39         if (message == NULL) {
40                 ERR("Failed to get interface");
41                 return NULL;
42         }
43
44         g_variant_get(message, "(o)", &path);
45
46         g_variant_unref(message);
47
48         return (char *)path;
49 }
50
51 char *netconfig_wifi_get_supplicant_interface_path(const char *interface_name)
52 {
53         const char *ifname = interface_name ? interface_name : WIFI_IFNAME;
54         return netconfig_get_supplicant_interface(ifname);
55 }
56
57 char *netconfig_wifi_get_supplicant_p2p_interface(void)
58 {
59         GVariant *message = NULL;
60         gchar *path = NULL;
61
62         message = netconfig_supplicant_invoke_dbus_method(
63                         SUPPLICANT_SERVICE, SUPPLICANT_PATH,
64                         SUPPLICANT_INTERFACE, "GetP2PInterface", NULL);
65
66         if (message == NULL) {
67                 ERR("Failed to get object path");
68                 return NULL;
69         }
70
71         g_variant_get(message, "(o)", &path);
72
73         g_variant_unref(message);
74
75         return (char *)path;
76 }
77
78 GVariant *netconfig_supplicant_invoke_dbus_method(const char *dest, const char *path,
79                 const char *interface_name, const char *method, GVariant *params)
80 {
81         GError *error = NULL;
82         GVariant *reply = NULL;
83         GDBusConnection *connection = NULL;
84
85         if (g_strcmp0(method, "GetInterface") != 0)
86                 INFO("[DBUS Sync] %s %s %s", interface_name, method, path);
87
88         connection = netdbus_get_connection();
89         if (connection == NULL) {
90                 ERR("Failed to get GDBus Connection");
91                 return NULL;
92         }
93
94         reply = g_dbus_connection_call_sync(
95                         connection,
96                         dest,
97                         path,
98                         interface_name,
99                         method,
100                         params,
101                         NULL,
102                         G_DBUS_CALL_FLAGS_NONE,
103                         NETCONFIG_DBUS_REPLY_TIMEOUT,
104                         netdbus_get_cancellable(),
105                         &error);
106
107         if (reply == NULL) {
108                 if (error != NULL) {
109                         ERR("g_dbus_connection_call_sync() failed"
110                                                 "error [%d: %s]", error->code, error->message);
111                         g_error_free(error);
112                 } else {
113                         ERR("g_dbus_connection_call_sync() failed");
114                 }
115
116                 return NULL;
117         }
118
119         return reply;
120 }
121
122 gboolean netconfig_supplicant_invoke_dbus_method_nonblock(const char *dest,
123                 const char *path, const char *interface_name,
124                 const char *method, GVariant *params,
125                 GAsyncReadyCallback notify_func, void *user_data)
126 {
127         GDBusConnection *connection = NULL;
128
129         INFO("[DBUS Async] %s %s %s", interface_name, method, path);
130
131         connection = netdbus_get_connection();
132         if (connection == NULL) {
133                 DBG("Failed to get GDBusconnection");
134                 return FALSE;
135         }
136
137         g_dbus_connection_call(connection,
138                         dest,
139                         path,
140                         interface_name,
141                         method,
142                         params,
143                         NULL,
144                         G_DBUS_CALL_FLAGS_NONE,
145                         NETCONFIG_DBUS_REPLY_TIMEOUT,
146                         netdbus_get_cancellable(),
147                         (GAsyncReadyCallback) notify_func,
148                         user_data);
149
150         if (notify_func)
151                 netconfig_gdbus_pending_call_ref();
152
153         return TRUE;
154 }
155
156 GVariant *netconfig_supplicant_invoke_dbus_interface_property_get(const char *interface_name,
157                         const char *key)
158 {
159         GVariant *params = NULL;
160         GVariant *reply = NULL;
161         char *path;
162
163         ERR("[GDBUS] property_get : %s", key);
164
165         path = netconfig_wifi_get_supplicant_interface_path(interface_name);
166         if (path == NULL) {
167                 DBG("Failed to get wpa_supplicant DBus path");
168                 return NULL;
169         }
170
171         params = g_variant_new("(ss)", SUPPLICANT_IFACE_INTERFACE, key);
172
173         reply = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
174                         path,
175                         DBUS_INTERFACE_PROPERTIES,
176                         "Get",
177                         params);
178
179         g_free(path);
180         if (reply == NULL) {
181                 ERR("netconfig_supplicant_invoke_dbus_method() failed.");
182                 return NULL;
183         }
184
185         return reply;
186 }
187
188 gboolean netconfig_supplicant_invoke_dbus_interface_property_set(const char *interface_name,
189                         const char *key, GVariant *var,
190                         GAsyncReadyCallback notify_func)
191 {
192         gboolean result = FALSE;
193         GVariant *message = NULL;
194         char *path;
195
196         DBG("[DBUS] property_set : %s", key);
197
198         path = netconfig_wifi_get_supplicant_interface_path(interface_name);
199         if (path == NULL) {
200                 ERR("Failed to get wpa_supplicant DBus path");
201                 return result;
202         }
203
204         message = g_variant_new("(ssv)", SUPPLICANT_IFACE_INTERFACE, key, var);
205         result = netconfig_invoke_dbus_method_nonblock(SUPPLICANT_SERVICE,
206                         path,
207                         DBUS_INTERFACE_PROPERTIES,
208                         "Set",
209                         message,
210                         notify_func,
211                         NULL);
212
213         g_free(path);
214         if (result == FALSE) {
215                 ERR("dbus_connection_send_with_reply() failed");
216
217                 return result;
218         }
219
220         return result;
221 }