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"
36 #define NETCONFIG_AGENT_FIELD_NAME "Name"
37 #define NETCONFIG_AGENT_FIELD_SSID "SSID"
38 #define NETCONFIG_AGENT_FIELD_IDENTITY "Identity"
39 #define NETCONFIG_AGENT_FIELD_PASSPHRASE "Passphrase"
40 #define NETCONFIG_AGENT_FIELD_WPS "WPS"
41 #define NETCONFIG_AGENT_FIELD_WPS_PBC "WPS_PBC"
42 #define NETCONFIG_AGENT_FIELD_WPS_PIN "WPS_PIN"
44 #define NETCONFIG_AGENT_ERR_CONNECT_FAILED "connect-failed"
46 struct netconfig_wifi_agent {
55 static struct netconfig_wifi_agent agent;
57 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 + 1);
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 ERR("Fail to connect Wi-Fi");
241 __netconfig_agent_clear_fields();
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);
251 error = g_error_new(G_DBUS_ERROR,
252 G_DBUS_ERROR_AUTH_FAILED,
253 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
255 g_dbus_method_invocation_return_gerror(context, error);
256 g_clear_error(&error);
260 g_variant_iter_free(iter);
262 net_connman_agent_complete_set_field(connman_agent, context);
266 gboolean handle_request_input(NetConnmanAgent *connman_agent,
267 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
271 GVariant *r_value = NULL;
272 GVariant *out_table = NULL;
273 gboolean updated = FALSE;
274 GVariantBuilder *builder = NULL;
276 g_return_val_if_fail(connman_agent != NULL, FALSE);
281 DBG("Agent fields requested for service: %s", service);
283 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
285 g_variant_get(fields, "a{sv}", &iter);
286 while (g_variant_iter_loop(iter, "{sv}", &field, &r_value)) {
288 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
289 agent.passphrase != NULL) {
290 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_PASSPHRASE,
291 g_variant_new_string(agent.passphrase));
294 DBG("Setting [%s] - []", field);
295 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
296 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
297 if (agent.wps_pbc == TRUE) {
298 /* Sending empty string for WPS push button method */
299 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(""));
302 DBG("Setting empty string for [%s]", field);
303 } else if (agent.wps_pin != NULL) {
304 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(agent.wps_pin));
307 DBG("Setting string [%s] - []", field);
309 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0 &&
310 agent.name != NULL) {
311 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_NAME, g_variant_new_string(agent.name));
314 DBG("Settings [%s] - []", field);
315 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0 &&
316 agent.ssid != NULL) {
318 GVariantBuilder *builder1 = NULL;
319 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
321 for (i = 0; i < (agent.ssid->len); i++)
322 g_variant_builder_add(builder1, "y", agent.ssid->data[i]);
324 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_SSID, g_variant_builder_end(builder1));
325 if (builder1 != NULL)
326 g_variant_builder_unref(builder1);
329 DBG("Settings [%s] - []", field);
330 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0 &&
331 agent.identity != NULL) {
332 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_IDENTITY, g_variant_new_string(agent.identity));
335 DBG("Settings [%s] - []", field);
339 out_table = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
342 g_variant_builder_unref(builder);
344 g_variant_iter_free(iter);
347 if (NULL == out_table) {
348 net_connman_agent_complete_request_input(connman_agent, context, out_table);
354 g_dbus_method_invocation_return_value(context, out_table);
356 GError *error = NULL;
357 error = g_error_new(G_DBUS_ERROR,
358 G_DBUS_ERROR_AUTH_FAILED,
359 "net.connman.Agent.Error.Canceled");
361 g_dbus_method_invocation_return_gerror(context, error);
362 g_clear_error(&error);
365 __netconfig_agent_clear_fields();
371 gboolean handle_report_error(NetConnmanAgent *connman_agent,
372 GDBusMethodInvocation *context, const gchar *service, const gchar *error)
376 g_return_val_if_fail(connman_agent != NULL, FALSE);
378 net_connman_agent_complete_report_error(connman_agent, context);
379 DBG("Agent error for service[%s] - [%s]", service, error);
381 /* Do something when it failed to make a connection */
386 #if defined TIZEN_CAPTIVE_PORTAL
387 #if defined TIZEN_WEARABLE
388 #define QUERY_FOR_INTERNET_INTERVAL 2
389 #define TIMER_THRESHOLD 4
391 #define QUERY_FOR_INTERNET_INTERVAL 20
392 #define TIMER_THRESHOLD 120
395 static gboolean is_monitor_notifier_registered = FALSE;
397 #if defined TIZEN_WEARABLE
398 static gboolean is_portal_msg_shown = FALSE;
401 struct poll_timer_data {
407 static struct poll_timer_data timer_data = {
408 QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
410 static gboolean __check_ignore_portal_list(const char * ssid)
414 int ignore_ap_count = 0;
419 DBG("checking ssid [%s]", ssid);
421 DBG("csc string [%s]", def_str);
422 gchar ** ignore_ap_list = g_strsplit(def_str, ",", 0);
423 ignore_ap_count = g_strv_length(ignore_ap_list);
424 for (i = 0; i < ignore_ap_count; i++) {
425 DBG("[%d] - [%s]", i, ignore_ap_list[i]);
426 if (strncmp(ignore_ap_list[i], ssid, strlen(ssid)) == 0) {
427 g_strfreev(ignore_ap_list);
432 g_strfreev(ignore_ap_list);
436 static void __wifi_state_monitor(wifi_service_state_e state,
439 static wifi_state_notifier wifi_state_monitor_notifier = {
440 .wifi_state_changed = __wifi_state_monitor,
444 static void __wifi_state_monitor(wifi_service_state_e state,
447 DBG("Wi-Fi state: %x", state);
449 if (state == NETCONFIG_WIFI_CONNECTED)
452 if (is_monitor_notifier_registered == TRUE) {
453 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
454 is_monitor_notifier_registered = FALSE;
457 #if defined TIZEN_WEARABLE
458 is_portal_msg_shown = FALSE;
461 /* suspend if Internet check activity in progress */
462 if (timer_data.timer_id == 0)
465 netconfig_stop_timer(&timer_data.timer_id);
466 netconfig_stop_internet_check();
468 DBG("Stopped Internet accessibility check");
471 static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
473 char *service_profile = NULL;
474 GVariant *reply = NULL;
478 struct poll_timer_data *timer = (struct poll_timer_data *)data;
482 if (TRUE == netconfig_get_internet_status()) {
483 if (is_monitor_notifier_registered == TRUE) {
484 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
485 is_monitor_notifier_registered = FALSE;
488 DBG("Portal logged in successfully and update ConnMan state");
489 return FALSE; /* to stop the timer */
491 if (timer->time_elapsed >= TIMER_THRESHOLD) {
492 DBG("Login failed, update ConnMan");
494 if (is_monitor_notifier_registered == TRUE) {
495 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
496 is_monitor_notifier_registered = FALSE;
499 /* Disconnect and forget the AP */
500 service_profile = (char*) netconfig_get_default_profile();
501 if (service_profile && netconfig_is_wifi_profile(service_profile)) {
502 /* Now forget the AP*/
503 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
504 service_profile, CONNMAN_SERVICE_INTERFACE, "Remove",
508 g_variant_unref(reply);
510 ERR("Failed to forget the AP ");
513 if (NETCONFIG_WIFI_CONNECTED ==
514 wifi_state_get_service_state()) {
515 /* check Internet availability by sending and receiving data*/
516 netconfig_check_internet_accessibility();
517 /* Returning TRUE itself is enough to restart the timer */
518 timer->time_elapsed = timer->time_elapsed +
519 QUERY_FOR_INTERNET_INTERVAL;
528 static void __netconfig_wifi_portal_login_timer_start(struct poll_timer_data
531 DBG("__netconfig_wifi_browser_start_timer...starting timer");
536 netconfig_stop_timer(&(data->timer_id));
538 /* Timer logic: After successful launch of browser, we would check for
539 * Internet status for every 20s until a threshold of 120s
542 data->time_elapsed = QUERY_FOR_INTERNET_INTERVAL;
543 netconfig_start_timer_seconds(QUERY_FOR_INTERNET_INTERVAL,
544 __netconfig_wifi_portal_login_timeout, data, &(data->timer_id));
548 gboolean handle_request_browser(NetConnmanAgent *connman_agent,
549 GDBusMethodInvocation *context, const gchar *service, const gchar *url)
551 #if defined TIZEN_CAPTIVE_PORTAL
552 gboolean ret = FALSE;
553 gboolean ignore_portal = FALSE;
554 const char * ssid = NULL;
556 g_return_val_if_fail(connman_agent != NULL, FALSE);
558 DBG("service[%s] - url[%s]", service, url);
560 ssid = netconfig_wifi_get_connected_essid(netconfig_get_default_profile());
562 ERR("Connected AP name is NULL!!");
563 net_connman_agent_complete_request_browser(connman_agent, context);
567 ignore_portal = __check_ignore_portal_list(ssid);
569 if (ignore_portal == TRUE) {
570 net_connman_agent_complete_request_browser(connman_agent, context);
573 /* Register for Wifi state change notifier*/
574 if (is_monitor_notifier_registered == FALSE) {
575 wifi_state_notifier_register(&wifi_state_monitor_notifier);
576 is_monitor_notifier_registered = TRUE;
579 ret = netconfig_send_notification_to_net_popup(NETCONFIG_ADD_PORTAL_NOTI, ssid);
581 timer_data.time_elapsed = 0;
582 __netconfig_wifi_portal_login_timer_start(&timer_data);
584 net_connman_agent_complete_request_browser(connman_agent, context);
587 GError *error = NULL;
588 error = g_error_new(G_DBUS_ERROR,
589 G_DBUS_ERROR_AUTH_FAILED,
590 CONNMAN_ERROR_INTERFACE ".NotSupported");
592 g_dbus_method_invocation_return_gerror(context, error);
593 g_clear_error(&error);