Merge "Using updated clatd's Dbus interface and Path values" 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_wifi_get_supplicant_interface(void)
25 {
26         GVariant *message = NULL;
27         GVariant *params = NULL;
28         gchar *path = NULL;
29
30         params = g_variant_new("(s)", WIFI_IFNAME);
31
32         message = netconfig_supplicant_invoke_dbus_method(
33                         SUPPLICANT_SERVICE, SUPPLICANT_PATH,
34                         SUPPLICANT_INTERFACE, "GetInterface", params);
35
36         if (message == NULL) {
37                 ERR("Failed to get object path");
38                 return NULL;
39         }
40
41         g_variant_get(message, "(o)", &path);
42
43         g_variant_unref(message);
44
45         return (char *)path;
46 }
47
48 char *netconfig_wifi_get_supplicant_p2p_interface(void)
49 {
50         GVariant *message = NULL;
51         gchar *path = NULL;
52
53         message = netconfig_supplicant_invoke_dbus_method(
54                         SUPPLICANT_SERVICE, SUPPLICANT_PATH,
55                         SUPPLICANT_INTERFACE, "GetP2PInterface", NULL);
56
57         if (message == NULL) {
58                 ERR("Failed to get object path");
59                 return NULL;
60         }
61
62         g_variant_get(message, "(o)", &path);
63
64         g_variant_unref(message);
65
66         return (char *)path;
67 }
68
69 GVariant *netconfig_supplicant_invoke_dbus_method(const char *dest, const char *path,
70                 const char *interface_name, const char *method, GVariant *params)
71 {
72         GError *error = NULL;
73         GVariant *reply = NULL;
74         GDBusConnection *connection = NULL;
75
76         INFO("[DBUS Sync] %s %s %s", interface_name, method, path);
77
78         connection = netdbus_get_connection();
79         if (connection == NULL) {
80                 ERR("Failed to get GDBus Connection");
81                 return NULL;
82         }
83
84         reply = g_dbus_connection_call_sync(
85                         connection,
86                         dest,
87                         path,
88                         interface_name,
89                         method,
90                         params,
91                         NULL,
92                         G_DBUS_CALL_FLAGS_NONE,
93                         NETCONFIG_DBUS_REPLY_TIMEOUT,
94                         netdbus_get_cancellable(),
95                         &error);
96
97         if (reply == NULL) {
98                 if (error != NULL) {
99                         ERR("g_dbus_connection_call_sync() failed"
100                                                 "error [%d: %s]", error->code, error->message);
101                         g_error_free(error);
102                 } else {
103                         ERR("g_dbus_connection_call_sync() failed");
104                 }
105
106                 return NULL;
107         }
108
109         return reply;
110 }
111
112 gboolean netconfig_supplicant_invoke_dbus_method_nonblock(const char *dest,
113                 const char *path, const char *interface_name,
114                 const char *method, GVariant *params,
115                 GAsyncReadyCallback notify_func)
116 {
117         GDBusConnection *connection = NULL;
118
119         INFO("[DBUS Async] %s %s %s", interface_name, method, path);
120
121         connection = netdbus_get_connection();
122         if (connection == NULL) {
123                 DBG("Failed to get GDBusconnection");
124                 return FALSE;
125         }
126
127         g_dbus_connection_call(connection,
128                         dest,
129                         path,
130                         interface_name,
131                         method,
132                         params,
133                         NULL,
134                         G_DBUS_CALL_FLAGS_NONE,
135                         NETCONFIG_DBUS_REPLY_TIMEOUT,
136                         netdbus_get_cancellable(),
137                         (GAsyncReadyCallback) notify_func,
138                         NULL);
139
140         if (notify_func)
141                 netconfig_gdbus_pending_call_ref();
142
143         return TRUE;
144 }
145
146 GVariant *netconfig_supplicant_invoke_dbus_interface_property_get(const char *interface,
147                         const char *key)
148 {
149         GVariant *params = NULL;
150         GVariant *reply = NULL;
151         char *path;
152
153         ERR("[GDBUS] property_get : %s", key);
154
155         path = netconfig_wifi_get_supplicant_interface();
156         if (path == NULL) {
157                 DBG("Failed to get wpa_supplicant DBus path");
158                 return NULL;
159         }
160
161         params = g_variant_new("(ss)", interface, key);
162
163         reply = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
164                         path,
165                         DBUS_INTERFACE_PROPERTIES,
166                         "Get",
167                         params);
168
169         g_free(path);
170         if (reply == NULL) {
171                 ERR("netconfig_supplicant_invoke_dbus_method() failed.");
172                 return NULL;
173         }
174
175         return reply;
176 }
177
178 gboolean netconfig_supplicant_invoke_dbus_interface_property_set(const char *interface,
179                         const char *key, GVariant *var,
180                         GAsyncReadyCallback notify_func)
181 {
182         gboolean result = FALSE;
183         GVariant *message = NULL;
184         char *path;
185
186         DBG("[DBUS] property_set : %s", key);
187
188         path = netconfig_wifi_get_supplicant_interface();
189         if (path == NULL) {
190                 ERR("Failed to get wpa_supplicant DBus path");
191                 return result;
192         }
193
194         message = g_variant_new("(ssv)", interface, key, var);
195         result = netconfig_invoke_dbus_method_nonblock(SUPPLICANT_SERVICE,
196                         path,
197                         DBUS_INTERFACE_PROPERTIES,
198                         "Set",
199                         message,
200                         notify_func);
201
202         g_free(path);
203         if (result == FALSE) {
204                 ERR("dbus_connection_send_with_reply() failed");
205
206                 return result;
207         }
208
209         return result;
210 }