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.
25 #include <vconf-keys.h>
31 #include "wifi-agent.h"
32 #include "wifi-state.h"
33 #include "wifi-eap-config.h"
34 #include "network-state.h"
35 #include "network-accessibility.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)
60 g_byte_array_free(agent.ssid, TRUE);
62 g_free(agent.identity);
63 g_free(agent.passphrase);
64 g_free(agent.wps_pin);
68 agent.identity = NULL;
69 agent.passphrase = NULL;
71 agent.wps_pbc = FALSE;
74 int connman_register_agent(void)
76 GVariant *reply = NULL;
77 GVariant *params = NULL;
79 params = g_variant_new("(o)", NETCONFIG_WIFI_PATH);
80 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
81 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
82 "RegisterAgent", params);
85 ERR("Fail to register agent");
88 g_variant_unref(reply);
90 INFO("Registered to connman agent successfully");
95 int connman_unregister_agent(void)
97 gboolean reply = FALSE;
98 GVariant *param = NULL;
99 const char *path = NETCONFIG_WIFI_PATH;
101 param = g_variant_new("(o)", path);
103 DBG("ConnMan agent unregister");
105 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
106 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
107 "UnregisterAgent", param, NULL);
110 ERR("Fail to unregister agent");
112 /* Clearing the agent fields */
113 __netconfig_agent_clear_fields();
118 gboolean netconfig_wifi_set_agent_field_for_eap_network(
119 const char *name, const char *identity, const char *passphrase)
126 __netconfig_agent_clear_fields();
128 name_len = strlen(name);
129 agent.ssid = g_byte_array_sized_new(name_len);
130 agent.ssid->len = name_len;
131 memcpy(agent.ssid->data, name, name_len);
134 agent.identity = g_strdup(identity);
137 agent.passphrase = g_strdup(passphrase);
139 DBG("Successfully configured for EAP network");
144 gboolean handle_set_field(NetConnmanAgent *connman_agent,
145 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
147 GError *error = NULL;
151 gboolean updated = FALSE;
152 gboolean reply = FALSE;
154 g_return_val_if_fail(connman_agent != NULL, FALSE);
156 DBG("Set agent fields for %s", service);
158 if (netconfig_is_wifi_profile(service) != TRUE) {
159 error = g_error_new(G_DBUS_ERROR,
160 G_DBUS_ERROR_AUTH_FAILED,
161 CONNMAN_ERROR_INTERFACE ".InvalidService");
163 g_dbus_method_invocation_return_gerror(context, error);
164 g_clear_error(&error);
169 __netconfig_agent_clear_fields();
170 g_variant_get(fields, "a{sv}", &iter);
171 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
172 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
173 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
174 agent.passphrase = g_strdup(g_variant_get_string(value, NULL));
177 DBG("Field [%s] - []", field);
179 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
180 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) &&
181 g_strcmp0(g_variant_get_string(value, NULL), "enable") == 0) {
182 agent.wps_pbc = TRUE;
185 DBG("Field [%s] - [%d]", field, agent.wps_pbc);
187 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
188 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
189 agent.wps_pin = g_strdup(g_variant_get_string(value, NULL));
192 DBG("Field [%s] - []", field);
194 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0) {
195 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
196 agent.name = g_strdup(g_variant_get_string(value, NULL));
199 DBG("Field [%s] - []", field);
201 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0) {
202 if (agent.ssid != NULL) {
203 g_byte_array_free(agent.ssid, TRUE);
207 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING)) {
210 GByteArray *array = g_byte_array_new();
212 g_variant_get(value, "ay", &iter1);
213 while (g_variant_iter_loop(iter1, "y", &char_value))
214 g_byte_array_append(array, &char_value, 1);
215 g_variant_iter_free(iter1);
216 if (array != NULL && (array->len > 0)) {
217 agent.ssid = g_byte_array_sized_new(array->len);
218 agent.ssid->len = array->len;
219 memcpy(agent.ssid->data, array->data, array->len);
222 DBG("Field [%s] - []", field);
225 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0) {
226 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
227 agent.identity = g_strdup(g_variant_get_string(value, NULL));
230 DBG("Field [%s] - []", field);
235 if (updated == TRUE) {
236 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
237 service, CONNMAN_SERVICE_INTERFACE, "Connect",
238 NULL, __netconfig_wifi_connect_reply);
240 g_dbus_method_invocation_return_value(context, NULL);
242 error = g_error_new(G_DBUS_ERROR,
243 G_DBUS_ERROR_AUTH_FAILED,
244 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
246 g_dbus_method_invocation_return_gerror(context, error);
247 g_clear_error(&error);
250 error = g_error_new(G_DBUS_ERROR,
251 G_DBUS_ERROR_AUTH_FAILED,
252 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
254 g_dbus_method_invocation_return_gerror(context, error);
255 g_clear_error(&error);
259 ERR("Fail to connect Wi-Fi");
261 __netconfig_agent_clear_fields();
263 g_variant_iter_free(iter);
265 net_connman_agent_complete_set_field(connman_agent, context);
269 gboolean handle_request_input(NetConnmanAgent *connman_agent,
270 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
274 GVariant *r_value = NULL;
275 GVariant *out_table = NULL;
276 gboolean updated = FALSE;
277 GVariantBuilder *builder = NULL;
279 g_return_val_if_fail(connman_agent != NULL, FALSE);
284 DBG("Agent fields requested for service: %s", service);
286 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
288 g_variant_get(fields, "a{sv}", &iter);
289 while (g_variant_iter_loop(iter, "{sv}", &field, &r_value)) {
291 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
292 agent.passphrase != NULL) {
293 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_PASSPHRASE,
294 g_variant_new_string(agent.passphrase));
297 DBG("Setting [%s] - []", field);
298 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
299 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
300 if (agent.wps_pbc == TRUE) {
301 /* Sending empty string for WPS push button method */
302 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(""));
305 DBG("Setting empty string for [%s]", field);
306 } else if (agent.wps_pin != NULL) {
307 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(agent.wps_pin));
310 DBG("Setting string [%s] - []", field);
312 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0 &&
313 agent.name != NULL) {
314 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_NAME, g_variant_new_string(agent.name));
317 DBG("Settings [%s] - []", field);
318 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0 &&
319 agent.ssid != NULL) {
321 GVariantBuilder *builder1 = NULL;
322 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
324 for (i = 0; i < (agent.ssid->len); i++)
325 g_variant_builder_add(builder1, "y", agent.ssid->data[i]);
327 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_SSID, g_variant_builder_end(builder1));
328 if (builder1 != NULL)
329 g_variant_builder_unref(builder1);
332 DBG("Settings [%s] - []", field);
333 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0 &&
334 agent.identity != NULL) {
335 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_IDENTITY, g_variant_new_string(agent.identity));
338 DBG("Settings [%s] - []", field);
342 out_table = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
345 g_variant_builder_unref(builder);
347 g_variant_iter_free(iter);
350 if (NULL == out_table) {
351 net_connman_agent_complete_request_input(connman_agent, context, out_table);
357 g_dbus_method_invocation_return_value(context, out_table);
359 GError *error = NULL;
360 error = g_error_new(G_DBUS_ERROR,
361 G_DBUS_ERROR_AUTH_FAILED,
362 "net.connman.Agent.Error.Canceled");
364 g_dbus_method_invocation_return_gerror(context, error);
365 g_clear_error(&error);
368 __netconfig_agent_clear_fields();
369 g_variant_unref(out_table);
375 gboolean handle_report_error(NetConnmanAgent *connman_agent,
376 GDBusMethodInvocation *context, const gchar *service, const gchar *error)
380 g_return_val_if_fail(connman_agent != NULL, FALSE);
382 net_connman_agent_complete_report_error(connman_agent, context);
383 DBG("Agent error for service[%s] - [%s]", service, error);
385 /* Do something when it failed to make a connection */
390 #if defined TIZEN_CAPTIVE_PORTAL
391 #if defined TIZEN_WEARABLE
392 #define QUERY_FOR_INTERNET_INTERVAL 2
393 #define TIMER_THRESHOLD 4
395 #define QUERY_FOR_INTERNET_INTERVAL 20
396 #define TIMER_THRESHOLD 120
399 static gboolean is_monitor_notifier_registered = FALSE;
401 #if defined TIZEN_WEARABLE
402 static gboolean is_portal_msg_shown = FALSE;
403 static guint portal_msg_timer = 0;
406 struct poll_timer_data {
412 static struct poll_timer_data timer_data = {
413 QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
415 static gboolean __check_ignore_portal_list(const char * ssid)
419 int ignore_ap_count = 0;
424 DBG("checking ssid [%s]", ssid);
426 DBG("csc string [%s]", def_str);
427 gchar ** ignore_ap_list = g_strsplit(def_str, ",", 0);
428 ignore_ap_count = g_strv_length(ignore_ap_list);
429 for (i = 0; i < ignore_ap_count; i++) {
430 DBG("[%d] - [%s]", i, ignore_ap_list[i]);
431 if (strncmp(ignore_ap_list[i], ssid, strlen(ssid)) == 0) {
432 g_strfreev(ignore_ap_list);
437 g_strfreev(ignore_ap_list);
441 static void __wifi_state_monitor(wifi_service_state_e state,
444 static wifi_state_notifier wifi_state_monitor_notifier = {
445 .wifi_state_changed = __wifi_state_monitor,
449 static void __wifi_state_monitor(wifi_service_state_e state,
452 DBG("Wi-Fi state: %x", state);
454 if (state == NETCONFIG_WIFI_CONNECTED)
457 if (is_monitor_notifier_registered == TRUE) {
458 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
459 is_monitor_notifier_registered = FALSE;
462 #if defined TIZEN_WEARABLE
463 is_portal_msg_shown = FALSE;
466 /* suspend if Internet check activity in progress */
467 if (timer_data.timer_id == 0)
470 netconfig_stop_timer(&timer_data.timer_id);
471 netconfig_stop_internet_check();
473 DBG("Stopped Internet accessibility check");
476 static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
478 char *service_profile = NULL;
479 GVariant *reply = NULL;
483 struct poll_timer_data *timer = (struct poll_timer_data *)data;
487 if (TRUE == netconfig_get_internet_status()) {
488 if (is_monitor_notifier_registered == TRUE) {
489 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
490 is_monitor_notifier_registered = FALSE;
493 DBG("Portal logged in successfully and update ConnMan state");
494 return FALSE; /* to stop the timer */
496 if (timer->time_elapsed >= TIMER_THRESHOLD) {
497 DBG("Login failed, update ConnMan");
499 if (is_monitor_notifier_registered == TRUE) {
500 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
501 is_monitor_notifier_registered = FALSE;
504 /* Disconnect and forget the AP */
505 service_profile = (char*) netconfig_get_default_profile();
506 if (service_profile && netconfig_is_wifi_profile(service_profile)) {
507 /* Now forget the AP*/
508 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
509 service_profile, CONNMAN_SERVICE_INTERFACE, "Remove",
513 g_variant_unref(reply);
515 ERR("Failed to forget the AP ");
518 if (NETCONFIG_WIFI_CONNECTED ==
519 wifi_state_get_service_state()) {
520 /* check Internet availability by sending and receiving data*/
521 netconfig_check_internet_accessibility();
522 /* Returning TRUE itself is enough to restart the timer */
523 timer->time_elapsed = timer->time_elapsed +
524 QUERY_FOR_INTERNET_INTERVAL;
533 #if defined TIZEN_WEARABLE
534 static gboolean __netconfig_display_portal_msg(gpointer data)
537 wc_launch_popup(WC_POPUP_TYPE_CAPTIVE_PORTAL);
539 netconfig_stop_timer(&portal_msg_timer);
545 static void __netconfig_wifi_portal_login_timer_start(struct poll_timer_data
548 DBG("__netconfig_wifi_browser_start_timer...starting timer");
553 netconfig_stop_timer(&(data->timer_id));
555 /* Timer logic: After successful launch of browser, we would check for
556 * Internet status for every 20s until a threshold of 120s
559 data->time_elapsed = QUERY_FOR_INTERNET_INTERVAL;
560 netconfig_start_timer_seconds(QUERY_FOR_INTERNET_INTERVAL,
561 __netconfig_wifi_portal_login_timeout, data, &(data->timer_id));
565 gboolean handle_request_browser(NetConnmanAgent *connman_agent,
566 GDBusMethodInvocation *context, const gchar *service, const gchar *url)
568 #if defined TIZEN_CAPTIVE_PORTAL
569 gboolean ret = FALSE;
570 gboolean ignore_portal = FALSE;
571 const char * ssid = NULL;
573 g_return_val_if_fail(connman_agent != NULL, FALSE);
575 DBG("service[%s] - url[%s]", service, url);
577 ssid = netconfig_wifi_get_connected_essid(netconfig_get_default_profile());
579 ERR("Connected AP name is NULL!!");
580 net_connman_agent_complete_request_browser(connman_agent, context);
584 ignore_portal = __check_ignore_portal_list(ssid);
586 if (ignore_portal == TRUE) {
587 net_connman_agent_complete_request_browser(connman_agent, context);
590 /* Register for Wifi state change notifier*/
591 if (is_monitor_notifier_registered == FALSE) {
592 wifi_state_notifier_register(&wifi_state_monitor_notifier);
593 is_monitor_notifier_registered = TRUE;
596 #if defined TIZEN_WEARABLE
597 if (is_portal_msg_shown) {
598 net_connman_agent_complete_request_browser(connman_agent, context);
602 is_portal_msg_shown = TRUE;
603 netconfig_start_timer_seconds(4, __netconfig_display_portal_msg, NULL, &portal_msg_timer);
605 ret = netconfig_send_notification_to_net_popup(NETCONFIG_ADD_PORTAL_NOTI, ssid);
608 timer_data.time_elapsed = 0;
609 __netconfig_wifi_portal_login_timer_start(&timer_data);
611 net_connman_agent_complete_request_browser(connman_agent, context);
614 GError *error = NULL;
615 error = g_error_new(G_DBUS_ERROR,
616 G_DBUS_ERROR_AUTH_FAILED,
617 CONNMAN_ERROR_INTERFACE ".NotSupported");
619 g_dbus_method_invocation_return_gerror(context, error);
620 g_clear_error(&error);