Fixed memory related issues reported by valgrind.
[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         if (notify_func)
120                 netconfig_gdbus_pending_call_ref();
121
122         return TRUE;
123 }
124
125 GVariant *netconfig_supplicant_invoke_dbus_interface_property_get(const char *interface,
126                         const char *key)
127 {
128         GVariant *params = NULL;
129         GVariant *reply = NULL;
130         char *path;
131
132         ERR("[GDBUS] property_get : %s", key);
133
134         path = netconfig_wifi_get_supplicant_interface();
135         if (path == NULL) {
136                 DBG("Failed to get wpa_supplicant DBus path");
137                 return NULL;
138         }
139
140         params = g_variant_new("(ss)", interface, key);
141
142         reply = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
143                         path,
144                         DBUS_INTERFACE_PROPERTIES,
145                         "Get",
146                         params);
147
148         g_free(path);
149         if (reply == NULL) {
150                 ERR("netconfig_supplicant_invoke_dbus_method() failed.");
151                 return NULL;
152         }
153
154         return reply;
155 }
156
157 gboolean netconfig_supplicant_invoke_dbus_interface_property_set(const char *interface,
158                         const char *key, GVariant *var,
159                         GAsyncReadyCallback notify_func)
160 {
161         gboolean result = FALSE;
162         GVariant *message = NULL;
163         char *path;
164
165         DBG("[DBUS] property_set : %s", key);
166
167         path = netconfig_wifi_get_supplicant_interface();
168         if (path == NULL) {
169                 ERR("Failed to get wpa_supplicant DBus path");
170                 return result;
171         }
172
173         message = g_variant_new("(ssv)", interface, key, var);
174         result = netconfig_invoke_dbus_method_nonblock(SUPPLICANT_SERVICE,
175                         path,
176                         DBUS_INTERFACE_PROPERTIES,
177                         "Set",
178                         message,
179                         notify_func);
180
181         g_free(path);
182         if (result == FALSE) {
183                 ERR("dbus_connection_send_with_reply() failed");
184
185                 return result;
186         }
187
188         return result;
189 }