2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <vconf-keys.h>
30 #include "wifi-agent.h"
31 #include "wifi-state.h"
32 #include "wifi-eap-config.h"
33 #include "network-state.h"
34 #include "network-accessibility.h"
35 #include "wifi-key-encryption.h"
37 #define NETCONFIG_AGENT_FIELD_NAME "Name"
38 #define NETCONFIG_AGENT_FIELD_SSID "SSID"
39 #define NETCONFIG_AGENT_FIELD_IDENTITY "Identity"
40 #define NETCONFIG_AGENT_FIELD_PASSPHRASE "Passphrase"
41 #define NETCONFIG_AGENT_FIELD_WPS "WPS"
42 #define NETCONFIG_AGENT_FIELD_WPS_PBC "WPS_PBC"
43 #define NETCONFIG_AGENT_FIELD_WPS_PIN "WPS_PIN"
45 #define NETCONFIG_AGENT_ERR_CONNECT_FAILED "connect-failed"
47 struct netconfig_wifi_agent {
56 static struct netconfig_wifi_agent agent;
58 static void __netconfig_agent_clear_fields(void)
61 g_byte_array_free(agent.ssid, TRUE);
63 g_free(agent.identity);
64 g_free(agent.passphrase);
65 g_free(agent.wps_pin);
69 agent.identity = NULL;
70 agent.passphrase = NULL;
72 agent.wps_pbc = FALSE;
75 int connman_register_agent(void)
77 GVariant *reply = NULL;
78 GVariant *params = NULL;
80 params = g_variant_new("(o)", NETCONFIG_WIFI_PATH);
81 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
82 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
83 "RegisterAgent", params);
86 ERR("Fail to register agent");
89 g_variant_unref(reply);
91 INFO("Registered to connman agent successfully");
96 int connman_unregister_agent(void)
98 gboolean reply = FALSE;
99 GVariant *param = NULL;
100 const char *path = NETCONFIG_WIFI_PATH;
102 param = g_variant_new("(o)", path);
104 DBG("ConnMan agent unregister");
106 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
107 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
108 "UnregisterAgent", param, NULL);
111 ERR("Fail to unregister agent");
113 /* Clearing the agent fields */
114 __netconfig_agent_clear_fields();
119 gboolean netconfig_wifi_set_agent_field_for_eap_network(
120 const char *name, const char *identity, const char *passphrase)
127 __netconfig_agent_clear_fields();
129 name_len = strlen(name);
130 agent.ssid = g_byte_array_sized_new(name_len);
131 agent.ssid->len = name_len;
132 memcpy(agent.ssid->data, name, name_len + 1);
135 agent.identity = g_strdup(identity);
138 agent.passphrase = g_strdup(passphrase);
140 gchar *enc_data = NULL;
141 enc_data = _netconfig_encrypt_passphrase(agent.passphrase);
144 ERR("Failed to encrypt the passphrase");
146 g_free(agent.passphrase);
147 agent.passphrase = enc_data;
150 DBG("Successfully configured for EAP network");
155 gboolean handle_set_field(NetConnmanAgent *connman_agent,
156 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
158 GError *error = NULL;
162 gboolean updated = FALSE;
163 gboolean reply = FALSE;
165 g_return_val_if_fail(connman_agent != NULL, FALSE);
167 DBG("Set agent fields for %s", service);
169 if (netconfig_is_wifi_profile(service) != TRUE) {
170 error = g_error_new(G_DBUS_ERROR,
171 G_DBUS_ERROR_AUTH_FAILED,
172 CONNMAN_ERROR_INTERFACE ".InvalidService");
174 g_dbus_method_invocation_return_gerror(context, error);
175 g_clear_error(&error);
180 __netconfig_agent_clear_fields();
181 g_variant_get(fields, "a{sv}", &iter);
182 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
183 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
184 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
185 agent.passphrase = g_strdup(g_variant_get_string(value, NULL));
188 DBG("Field [%s] - []", field);
190 if (agent.passphrase == NULL)
193 if (netconfig_check_passphrase(service, agent.passphrase) == FALSE) {
194 ERR("Invalid passphrase");
196 g_free(agent.passphrase);
197 agent.passphrase = NULL;
203 gchar *enc_data = NULL;
204 enc_data = _netconfig_encrypt_passphrase(agent.passphrase);
207 ERR("Failed to encrypt the passphrase");
211 g_free(agent.passphrase);
212 agent.passphrase = enc_data;
214 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
215 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) &&
216 g_strcmp0(g_variant_get_string(value, NULL), "enable") == 0) {
217 agent.wps_pbc = TRUE;
220 DBG("Field [%s] - [%d]", field, agent.wps_pbc);
222 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
223 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
224 agent.wps_pin = g_strdup(g_variant_get_string(value, NULL));
227 DBG("Field [%s] - []", field);
229 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0) {
230 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
231 agent.name = g_strdup(g_variant_get_string(value, NULL));
234 DBG("Field [%s] - []", field);
236 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0) {
237 if (agent.ssid != NULL) {
238 g_byte_array_free(agent.ssid, TRUE);
242 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING)) {
245 GByteArray *array = g_byte_array_new();
247 g_variant_get(value, "ay", &iter1);
248 while (g_variant_iter_loop(iter1, "y", &char_value))
249 g_byte_array_append(array, &char_value, 1);
250 g_variant_iter_free(iter1);
251 if (array != NULL && (array->len > 0)) {
252 agent.ssid = g_byte_array_sized_new(array->len);
253 agent.ssid->len = array->len;
254 memcpy(agent.ssid->data, array->data, array->len);
257 DBG("Field [%s] - []", field);
260 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0) {
261 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
262 agent.identity = g_strdup(g_variant_get_string(value, NULL));
265 DBG("Field [%s] - []", field);
270 if (updated == TRUE) {
271 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
272 service, CONNMAN_SERVICE_INTERFACE, "Connect",
273 NULL, __netconfig_wifi_connect_reply);
275 ERR("Fail to connect Wi-Fi");
276 __netconfig_agent_clear_fields();
277 error = g_error_new(G_DBUS_ERROR,
278 G_DBUS_ERROR_AUTH_FAILED,
279 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
281 g_dbus_method_invocation_return_gerror(context, error);
282 g_clear_error(&error);
283 g_variant_iter_free(iter);
287 error = g_error_new(G_DBUS_ERROR,
288 G_DBUS_ERROR_AUTH_FAILED,
289 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
291 g_dbus_method_invocation_return_gerror(context, error);
292 g_clear_error(&error);
293 g_variant_iter_free(iter);
297 g_variant_iter_free(iter);
299 net_connman_agent_complete_set_field(connman_agent, context);
303 gboolean handle_request_input(NetConnmanAgent *connman_agent,
304 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
308 GVariant *r_value = NULL;
309 GVariant *out_table = NULL;
310 gboolean updated = FALSE;
311 GVariantBuilder *builder = NULL;
313 g_return_val_if_fail(connman_agent != NULL, FALSE);
318 DBG("Agent fields requested for service: %s", service);
320 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
322 g_variant_get(fields, "a{sv}", &iter);
323 while (g_variant_iter_loop(iter, "{sv}", &field, &r_value)) {
325 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
326 agent.passphrase != NULL) {
327 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_PASSPHRASE,
328 g_variant_new_string(agent.passphrase));
331 DBG("Setting [%s] - []", field);
332 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
333 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
334 if (agent.wps_pbc == TRUE) {
335 /* Sending empty string for WPS push button method */
336 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(""));
339 DBG("Setting empty string for [%s]", field);
340 } else if (agent.wps_pin != NULL) {
341 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(agent.wps_pin));
344 DBG("Setting string [%s] - []", field);
346 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0 &&
347 agent.name != NULL) {
348 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_NAME, g_variant_new_string(agent.name));
351 DBG("Settings [%s] - []", field);
352 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0 &&
353 agent.ssid != NULL) {
355 GVariantBuilder *builder1 = NULL;
356 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
358 for (i = 0; i < (agent.ssid->len); i++)
359 g_variant_builder_add(builder1, "y", agent.ssid->data[i]);
361 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_SSID, g_variant_builder_end(builder1));
362 if (builder1 != NULL)
363 g_variant_builder_unref(builder1);
366 DBG("Settings [%s] - []", field);
367 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0 &&
368 agent.identity != NULL) {
369 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_IDENTITY, g_variant_new_string(agent.identity));
372 DBG("Settings [%s] - []", field);
376 out_table = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
379 g_variant_builder_unref(builder);
381 g_variant_iter_free(iter);
384 if (NULL == out_table) {
385 net_connman_agent_complete_request_input(connman_agent, context, out_table);
391 g_dbus_method_invocation_return_value(context, out_table);
393 GError *error = NULL;
394 error = g_error_new(G_DBUS_ERROR,
395 G_DBUS_ERROR_AUTH_FAILED,
396 "net.connman.Agent.Error.Canceled");
398 g_dbus_method_invocation_return_gerror(context, error);
399 g_clear_error(&error);
402 __netconfig_agent_clear_fields();
408 gboolean handle_report_error(NetConnmanAgent *connman_agent,
409 GDBusMethodInvocation *context, const gchar *service, const gchar *error)
413 g_return_val_if_fail(connman_agent != NULL, FALSE);
415 net_connman_agent_complete_report_error(connman_agent, context);
416 DBG("Agent error for service[%s] - [%s]", service, error);
418 /* Do something when it failed to make a connection */
423 #if defined TIZEN_CAPTIVE_PORTAL
424 #if defined TIZEN_WEARABLE
425 #define QUERY_FOR_INTERNET_INTERVAL 2
426 #define TIMER_THRESHOLD 4
428 #define QUERY_FOR_INTERNET_INTERVAL 20
429 #define TIMER_THRESHOLD 120
432 static gboolean is_monitor_notifier_registered = FALSE;
434 #if defined TIZEN_WEARABLE
435 static gboolean is_portal_msg_shown = FALSE;
438 struct poll_timer_data {
444 static struct poll_timer_data timer_data = {
445 QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
447 static gboolean __check_ignore_portal_list(const char * ssid)
451 int ignore_ap_count = 0;
456 DBG("checking ssid [%s]", ssid);
458 DBG("csc string [%s]", def_str);
459 gchar ** ignore_ap_list = g_strsplit(def_str, ",", 0);
460 ignore_ap_count = g_strv_length(ignore_ap_list);
461 for (i = 0; i < ignore_ap_count; i++) {
462 DBG("[%d] - [%s]", i, ignore_ap_list[i]);
463 if (strncmp(ignore_ap_list[i], ssid, strlen(ssid)) == 0) {
464 g_strfreev(ignore_ap_list);
469 g_strfreev(ignore_ap_list);
473 static void __wifi_state_monitor(wifi_service_state_e state,
476 static wifi_state_notifier wifi_state_monitor_notifier = {
477 .wifi_state_changed = __wifi_state_monitor,
481 static void __wifi_state_monitor(wifi_service_state_e state,
484 DBG("Wi-Fi state: %x", state);
486 if (state == NETCONFIG_WIFI_CONNECTED)
489 if (is_monitor_notifier_registered == TRUE) {
490 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
491 is_monitor_notifier_registered = FALSE;
494 #if defined TIZEN_WEARABLE
495 is_portal_msg_shown = FALSE;
498 /* suspend if Internet check activity in progress */
499 if (timer_data.timer_id == 0)
502 netconfig_stop_timer(&timer_data.timer_id);
503 netconfig_stop_internet_check();
505 DBG("Stopped Internet accessibility check");
508 static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
510 char *service_profile = NULL;
511 GVariant *reply = NULL;
515 struct poll_timer_data *timer = (struct poll_timer_data *)data;
519 if (TRUE == netconfig_get_internet_status()) {
520 if (is_monitor_notifier_registered == TRUE) {
521 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
522 is_monitor_notifier_registered = FALSE;
525 DBG("Portal logged in successfully and update ConnMan state");
526 return FALSE; /* to stop the timer */
528 if (timer->time_elapsed >= TIMER_THRESHOLD) {
529 DBG("Login failed, update ConnMan");
531 if (is_monitor_notifier_registered == TRUE) {
532 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
533 is_monitor_notifier_registered = FALSE;
536 /* Disconnect and forget the AP */
537 service_profile = (char*) netconfig_get_default_profile();
538 if (service_profile && netconfig_is_wifi_profile(service_profile)) {
539 /* Now forget the AP*/
540 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
541 service_profile, CONNMAN_SERVICE_INTERFACE, "Remove",
545 g_variant_unref(reply);
547 ERR("Failed to forget the AP ");
550 if (NETCONFIG_WIFI_CONNECTED ==
551 wifi_state_get_service_state()) {
552 /* check Internet availability by sending and receiving data*/
553 netconfig_check_internet_accessibility();
554 /* Returning TRUE itself is enough to restart the timer */
555 timer->time_elapsed = timer->time_elapsed +
556 QUERY_FOR_INTERNET_INTERVAL;
565 static void __netconfig_wifi_portal_login_timer_start(struct poll_timer_data
568 DBG("__netconfig_wifi_browser_start_timer...starting timer");
573 netconfig_stop_timer(&(data->timer_id));
575 /* Timer logic: After successful launch of browser, we would check for
576 * Internet status for every 20s until a threshold of 120s
579 data->time_elapsed = QUERY_FOR_INTERNET_INTERVAL;
580 netconfig_start_timer_seconds(QUERY_FOR_INTERNET_INTERVAL,
581 __netconfig_wifi_portal_login_timeout, data, &(data->timer_id));
585 gboolean handle_request_browser(NetConnmanAgent *connman_agent,
586 GDBusMethodInvocation *context, const gchar *service, const gchar *url)
588 #if defined TIZEN_CAPTIVE_PORTAL
589 gboolean ret = FALSE;
590 gboolean ignore_portal = FALSE;
591 const char * ssid = NULL;
593 g_return_val_if_fail(connman_agent != NULL, FALSE);
595 DBG("service[%s] - url[%s]", service, url);
597 ssid = netconfig_wifi_get_connected_essid(netconfig_get_default_profile());
599 ERR("Connected AP name is NULL!!");
600 net_connman_agent_complete_request_browser(connman_agent, context);
604 ignore_portal = __check_ignore_portal_list(ssid);
606 if (ignore_portal == TRUE) {
607 net_connman_agent_complete_request_browser(connman_agent, context);
610 /* Register for Wifi state change notifier*/
611 if (is_monitor_notifier_registered == FALSE) {
612 wifi_state_notifier_register(&wifi_state_monitor_notifier);
613 is_monitor_notifier_registered = TRUE;
616 ret = netconfig_send_notification_to_net_popup(NETCONFIG_ADD_PORTAL_NOTI, ssid);
618 timer_data.time_elapsed = 0;
619 __netconfig_wifi_portal_login_timer_start(&timer_data);
621 net_connman_agent_complete_request_browser(connman_agent, context);
624 GError *error = NULL;
625 error = g_error_new(G_DBUS_ERROR,
626 G_DBUS_ERROR_AUTH_FAILED,
627 CONNMAN_ERROR_INTERFACE ".NotSupported");
629 g_dbus_method_invocation_return_gerror(context, error);
630 g_clear_error(&error);