Remove syspopup
[platform/core/connectivity/net-config.git] / src / wifi-agent.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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 <stdio.h>
21 #include <unistd.h>
22
23 #include "wifi-agent.h"
24 #include "log.h"
25 #include "wifi.h"
26 #include "netdbus.h"
27
28 #define NETCONFIG_AGENT_FIELD_PASSPHRASE                "Passphrase"
29 #define NETCONFIG_AGENT_FIELD_WPS                               "WPS"
30 #define NETCONFIG_AGENT_FIELD_WPS_PBC                   "WPS_PBC"
31 #define NETCONFIG_AGENT_FIELD_WPS_PIN                   "WPS_PIN"
32
33 struct netconfig_wifi_agent {
34         char *passphrase;
35         char *wps_pin;
36         gboolean wps_pbc;
37 };
38
39 static struct netconfig_wifi_agent agent;
40
41 static void __netconfig_agent_clear_fields(void)
42 {
43         DBG("__netconfig_agent_clear_fields");
44
45         g_free(agent.passphrase);
46         g_free(agent.wps_pin);
47
48         agent.passphrase = NULL;
49         agent.wps_pin = NULL;
50         agent.wps_pbc = FALSE;
51 }
52
53 gboolean netconfig_agent_register(void)
54 {
55         DBG("netconfig_agent_register");
56
57         DBusMessage *reply = NULL;
58         char param1[64] = "";
59         char *param_array[] = {NULL, NULL};
60
61         snprintf(param1, 64, "objpath:%s", NETCONFIG_WIFI_PATH);
62         param_array[0] = param1;
63
64         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
65                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
66                         "RegisterAgent", param_array);
67
68         if (reply == NULL) {
69                 ERR("Error! Request failed");
70                 return FALSE;
71         }
72
73         dbus_message_unref(reply);
74
75         return TRUE;
76 }
77
78 gboolean netconfig_agent_unregister(void)
79 {
80         DBG("netconfig_agent_unregister");
81
82         DBusMessage *reply = NULL;
83         char param1[64] = "";
84         char *param_array[] = {NULL, NULL};
85
86         snprintf(param1, 64, "objpath:%s", NETCONFIG_WIFI_PATH);
87         param_array[0] = param1;
88
89         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
90                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
91                         "UnregisterAgent", param_array);
92
93         if (reply == NULL) {
94                 ERR("Error! Request failed");
95                 return FALSE;
96         }
97
98         dbus_message_unref(reply);
99
100         /* Clearing the agent fields */
101         __netconfig_agent_clear_fields();
102
103         return TRUE;
104 }
105
106 gboolean netconfig_iface_wifi_set_field(NetconfigWifi *wifi,
107                 GHashTable *fields, GError **error)
108 {
109         GHashTableIter iter;
110         gpointer field, value;
111
112         DBG("Set agent fields");
113
114         g_return_val_if_fail(wifi != NULL, FALSE);
115
116         __netconfig_agent_clear_fields();
117
118         g_hash_table_iter_init(&iter, fields);
119
120         while (g_hash_table_iter_next(&iter, &field, &value)) {
121                 DBG("Field - [%s]", field);
122                 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
123                         g_free(agent.passphrase);
124                         agent.passphrase = g_strdup(value);
125
126                         DBG("Field [%s] - []", field);
127                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
128                         agent.wps_pbc = FALSE;
129                         if (g_strcmp0(value, "enable") == 0)
130                                 agent.wps_pbc = TRUE;
131
132                         DBG("Field [%s] - [%d]", field, agent.wps_pbc);
133                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
134                         g_free(agent.wps_pin);
135                         agent.wps_pbc = FALSE;
136                         agent.wps_pin = g_strdup(value);
137
138                         DBG("Field [%s] - []", field);
139                 }
140         }
141
142         return TRUE;
143 }
144
145 gboolean netconfig_iface_wifi_request_input(NetconfigWifi *wifi,
146                 gchar *service, GHashTable *fields,
147                 DBusGMethodInvocation *context)
148 {
149         GHashTableIter iter;
150         gpointer field, value;
151         GHashTable *out_table = NULL;
152         GValue *ret_value = NULL;
153
154         g_return_val_if_fail(wifi != NULL, FALSE);
155
156         if (NULL == service)
157                 return FALSE;
158
159         DBG("Agent fields requested for service: %s", service);
160
161         out_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
162         if (NULL == out_table)
163                 return FALSE;
164
165         g_hash_table_iter_init(&iter, fields);
166
167         while (g_hash_table_iter_next(&iter, &field, &value)) {
168                 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
169                                 agent.passphrase != NULL) {
170                         ret_value = g_slice_new0(GValue);
171
172                         g_value_init(ret_value, G_TYPE_STRING);
173                         g_value_set_string(ret_value, agent.passphrase);
174                         g_hash_table_insert(out_table, g_strdup(field), ret_value);
175
176                         DBG("Setting [%s] - []", field);
177                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
178                                 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
179                         ret_value = g_slice_new0(GValue);
180
181                         g_value_init(ret_value, G_TYPE_STRING);
182
183                         if (agent.wps_pbc == TRUE) {
184                                 /* Sending empty string for WPS push button method */
185                                 g_value_set_string(ret_value, "");
186
187                                 DBG("Setting empty string for [%s]", field);
188                         } else if (agent.wps_pin != NULL) {
189                                 g_value_set_string(ret_value, agent.wps_pin);
190
191                                 DBG("Setting string [%s] - []", field);
192                         }
193
194                         g_hash_table_insert(out_table, g_strdup(field), ret_value);
195                 }
196         }
197
198         dbus_g_method_return(context, out_table);
199
200         __netconfig_agent_clear_fields();
201
202         return TRUE;
203 }