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