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