Apply coding rule
[platform/core/connectivity/net-config.git] / src / wifi-agent.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 <app.h>
21 #include <stdio.h>
22 #include <vconf.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <vconf-keys.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "wifi.h"
30 #include "netdbus.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"
36
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"
44
45 #define NETCONFIG_AGENT_ERR_CONNECT_FAILED              "connect-failed"
46
47 struct netconfig_wifi_agent {
48         GByteArray *ssid;
49         char *name;
50         char *identity;
51         char *passphrase;
52         char *wps_pin;
53         gboolean wps_pbc;
54 };
55
56 static struct netconfig_wifi_agent agent;
57
58 static void __netconfig_agent_clear_fields(void)
59 {
60         g_byte_array_free(agent.ssid, TRUE);
61         g_free(agent.name);
62         g_free(agent.identity);
63         g_free(agent.passphrase);
64         g_free(agent.wps_pin);
65
66         agent.ssid = NULL;
67         agent.name = NULL;
68         agent.identity = NULL;
69         agent.passphrase = NULL;
70         agent.wps_pin = NULL;
71         agent.wps_pbc = FALSE;
72 }
73
74 int connman_register_agent(void)
75 {
76         GVariant *reply = NULL;
77         GVariant *params = NULL;
78         GError *error;
79         GDBusConnection *connection = NULL;
80
81         connection = netdbus_get_connection();
82         if (connection == NULL) {
83                 ERR("GDBusconnection is NULL");
84                 return -1;
85         }
86
87         do {
88                 error = NULL;
89                 params = g_variant_new("(o)", NETCONFIG_WIFI_PATH);
90
91                 reply = g_dbus_connection_call_sync(
92                                 connection,
93                                 CONNMAN_SERVICE,
94                                 CONNMAN_MANAGER_PATH,
95                                 CONNMAN_MANAGER_INTERFACE,
96                                 "RegisterAgent",
97                                 params,
98                                 NULL,
99                                 G_DBUS_CALL_FLAGS_NONE,
100                                 DBUS_REPLY_TIMEOUT,
101                                 netdbus_get_cancellable(),
102                                 &error);
103
104                 if (reply == NULL) {
105                         if (error != NULL) {
106                                 if (g_strcmp0(error->message,
107                                                 "GDBus.Error:net.connman.Error.AlreadyExists: Already exists") == 0) {
108                                         break;
109                                 } else {
110                                         ERR("Fail to register agent [%d: %s]",
111                                                 error->code, error->message);
112                                 }
113
114                                 g_error_free(error);
115                         } else
116                                 ERR("Fail to register agent");
117                 } else
118                         g_variant_unref(reply);
119
120                 sleep(1);
121         } while (TRUE);
122
123         INFO("Registered to connman agent successfully");
124
125         return 0;
126 }
127
128 int connman_unregister_agent(void)
129 {
130         gboolean reply = FALSE;
131         GVariant *param = NULL;
132         const char *path = NETCONFIG_WIFI_PATH;
133
134         param = g_variant_new("(o)", path);
135
136         DBG("ConnMan agent unregister");
137
138         reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
139                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
140                         "UnregisterAgent", param, NULL);
141
142         if (reply != TRUE)
143                 ERR("Fail to unregister agent");
144
145         /* Clearing the agent fields */
146         __netconfig_agent_clear_fields();
147
148         return reply;
149 }
150
151 gboolean netconfig_wifi_set_agent_field_for_eap_network(
152                 const char *name, const char *identity, const char *passphrase)
153 {
154         int name_len;
155
156         if (name == NULL)
157                 return FALSE;
158
159         __netconfig_agent_clear_fields();
160
161         name_len = strlen(name);
162         agent.ssid = g_byte_array_sized_new(name_len);
163         agent.ssid->len = name_len;
164         memcpy(agent.ssid->data, name, name_len);
165
166         if (identity)
167                 agent.identity = g_strdup(identity);
168
169         if (passphrase)
170                 agent.passphrase = g_strdup(passphrase);
171
172         DBG("Successfully configured for EAP network");
173
174         return TRUE;
175 }
176
177 gboolean handle_set_field(NetConnmanAgent *connman_agent,
178                 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
179 {
180         GError *error = NULL;
181         GVariantIter *iter;
182         gpointer field;
183         GVariant *value;
184         gboolean updated = FALSE;
185         gboolean reply = FALSE;
186
187         g_return_val_if_fail(connman_agent != NULL, FALSE);
188
189         DBG("Set agent fields for %s", service);
190
191         if (netconfig_is_wifi_profile(service) != TRUE) {
192                 error = g_error_new(G_DBUS_ERROR,
193                                 G_DBUS_ERROR_AUTH_FAILED,
194                                 CONNMAN_ERROR_INTERFACE ".InvalidService");
195
196                 g_dbus_method_invocation_return_gerror(context, error);
197                 g_clear_error(&error);
198
199                 return reply;
200         }
201
202         __netconfig_agent_clear_fields();
203         g_variant_get(fields, "a{sv}", &iter);
204         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
205                 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
206                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
207                                 agent.passphrase = g_strdup(g_variant_get_string(value, NULL));
208                                 updated = TRUE;
209
210                                 DBG("Field [%s] - []", field);
211                         }
212                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
213                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) &&
214                                         g_strcmp0(g_variant_get_string(value, NULL), "enable") == 0) {
215                                 agent.wps_pbc = TRUE;
216                                 updated = TRUE;
217
218                                 DBG("Field [%s] - [%d]", field, agent.wps_pbc);
219                         }
220                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
221                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
222                                 agent.wps_pin = g_strdup(g_variant_get_string(value, NULL));
223                                 updated = TRUE;
224
225                                 DBG("Field [%s] - []", field);
226                         }
227                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0) {
228                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
229                                 agent.name = g_strdup(g_variant_get_string(value, NULL));
230                                 updated = TRUE;
231
232                                 DBG("Field [%s] - []", field);
233                         }
234                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0) {
235                         if (agent.ssid != NULL) {
236                                 g_byte_array_free(agent.ssid, TRUE);
237                                 agent.ssid = NULL;
238                         }
239
240                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING)) {
241                                 guint8 char_value;
242                                 GVariantIter *iter1;
243                                 GByteArray *array = g_byte_array_new();
244
245                                 g_variant_get(value, "ay", &iter1);
246                                 while (g_variant_iter_loop(iter1, "y", &char_value))
247                                         g_byte_array_append(array, &char_value, 1);
248                                 g_variant_iter_free(iter1);
249                                 if (array != NULL && (array->len > 0)) {
250                                         agent.ssid = g_byte_array_sized_new(array->len);
251                                         agent.ssid->len = array->len;
252                                         memcpy(agent.ssid->data, array->data, array->len);
253                                         updated = TRUE;
254
255                                         DBG("Field [%s] - []", field);
256                                 }
257                         }
258                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0) {
259                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
260                                 agent.identity = g_strdup(g_variant_get_string(value, NULL));
261                                 updated = TRUE;
262
263                                 DBG("Field [%s] - []", field);
264                         }
265                 }
266         }
267
268         if (updated == TRUE) {
269                 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
270                                 service, CONNMAN_SERVICE_INTERFACE, "Connect",
271                                 NULL, __netconfig_wifi_connect_reply);
272                 if (reply == TRUE) {
273                         g_dbus_method_invocation_return_value(context, NULL);
274                 } else {
275                         error = g_error_new(G_DBUS_ERROR,
276                                         G_DBUS_ERROR_AUTH_FAILED,
277                                         CONNMAN_ERROR_INTERFACE ".InvalidArguments");
278
279                         g_dbus_method_invocation_return_gerror(context, error);
280                         g_clear_error(&error);
281                 }
282         } else {
283                 error = g_error_new(G_DBUS_ERROR,
284                                 G_DBUS_ERROR_AUTH_FAILED,
285                                 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
286
287                 g_dbus_method_invocation_return_gerror(context, error);
288                 g_clear_error(&error);
289         }
290
291         if (reply != TRUE) {
292                 ERR("Fail to connect Wi-Fi");
293
294                 __netconfig_agent_clear_fields();
295         }
296         g_variant_iter_free(iter);
297
298         net_connman_agent_complete_set_field(connman_agent, context);
299         return reply;
300 }
301
302 gboolean handle_request_input(NetConnmanAgent *connman_agent,
303                 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
304 {
305         GVariantIter *iter;
306         gchar *field = NULL;
307         GVariant *r_value = NULL;
308         GVariant *out_table = NULL;
309         gboolean updated = FALSE;
310         GVariantBuilder *builder = NULL;
311
312         g_return_val_if_fail(connman_agent != NULL, FALSE);
313
314         if (NULL == service)
315                 return FALSE;
316
317         DBG("Agent fields requested for service: %s", service);
318
319         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
320
321         g_variant_get(fields, "a{sv}", &iter);
322         while (g_variant_iter_loop(iter, "{sv}", &field, &r_value)) {
323
324                 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
325                                 agent.passphrase != NULL) {
326                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_PASSPHRASE,
327                                                         g_variant_new_string(agent.passphrase));
328
329                         updated = TRUE;
330                         DBG("Setting [%s] - []", field);
331                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
332                                 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
333                         if (agent.wps_pbc == TRUE) {
334                                 /* Sending empty string for WPS push button method */
335                                 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(""));
336
337                                 updated = TRUE;
338                                 DBG("Setting empty string for [%s]", field);
339                         } else if (agent.wps_pin != NULL) {
340                                 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(agent.wps_pin));
341
342                                 updated = TRUE;
343                                 DBG("Setting string [%s] - []", field);
344                         }
345                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0 &&
346                                 agent.name != NULL) {
347                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_NAME, g_variant_new_string(agent.name));
348
349                         updated = TRUE;
350                         DBG("Settings [%s] - []", field);
351                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0 &&
352                                 agent.ssid != NULL) {
353                         int i = 0;
354                         GVariantBuilder *builder1 = NULL;
355                         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
356
357                         for (i = 0; i < (agent.ssid->len); i++)
358                                 g_variant_builder_add(builder1, "y", agent.ssid->data[i]);
359
360                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_SSID, g_variant_builder_end(builder1));
361                         if (builder1 != NULL)
362                                 g_variant_builder_unref(builder1);
363
364                         updated = TRUE;
365                         DBG("Settings [%s] - []", field);
366                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0 &&
367                                 agent.identity != NULL) {
368                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_IDENTITY, g_variant_new_string(agent.identity));
369
370                         updated = TRUE;
371                         DBG("Settings [%s] - []", field);
372                 }
373         }
374
375         out_table = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
376
377         if (builder)
378                 g_variant_builder_unref(builder);
379
380         g_variant_iter_free(iter);
381
382
383         if (NULL == out_table) {
384                 net_connman_agent_complete_request_input(connman_agent, context, out_table);
385
386                 return FALSE;
387         }
388
389         if (updated == TRUE)
390                 g_dbus_method_invocation_return_value(context, out_table);
391         else {
392                 GError *error = NULL;
393                 error = g_error_new(G_DBUS_ERROR,
394                                 G_DBUS_ERROR_AUTH_FAILED,
395                                 "net.connman.Agent.Error.Canceled");
396
397                 g_dbus_method_invocation_return_gerror(context, error);
398                 g_clear_error(&error);
399         }
400
401         __netconfig_agent_clear_fields();
402         g_variant_unref(out_table);
403
404         return updated;
405 }
406
407
408 gboolean handle_report_error(NetConnmanAgent *connman_agent,
409                 GDBusMethodInvocation *context, const gchar *service, const gchar *error)
410 {
411         gboolean ret = TRUE;
412
413         g_return_val_if_fail(connman_agent != NULL, FALSE);
414
415         net_connman_agent_complete_report_error(connman_agent, context);
416         DBG("Agent error for service[%s] - [%s]", service, error);
417
418         /* Do something when it failed to make a connection */
419
420         return ret;
421 }
422
423 #if defined TIZEN_CAPTIVE_PORTAL
424 #if defined TIZEN_WEARABLE
425 #define QUERY_FOR_INTERNET_INTERVAL                     2
426 #define TIMER_THRESHOLD                                         4
427 #else
428 #define QUERY_FOR_INTERNET_INTERVAL                     20
429 #define TIMER_THRESHOLD                                         120
430 #endif
431
432 static gboolean is_monitor_notifier_registered = FALSE;
433
434 #if defined TIZEN_WEARABLE
435 static gboolean is_portal_msg_shown = FALSE;
436 static guint portal_msg_timer = 0;
437 #endif
438
439 struct poll_timer_data {
440         guint time_elapsed;
441         guint timer_id;
442         void* data;
443 };
444
445 static struct poll_timer_data timer_data = {
446                 QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
447
448 static gboolean __check_ignore_portal_list(const char * ssid)
449 {
450         char def_str[1024];
451         int i = 0;
452         int ignore_ap_count = 0;
453
454         if (ssid == NULL)
455                 return FALSE;
456
457         DBG("checking ssid [%s]", ssid);
458
459         DBG("csc string [%s]", def_str);
460         gchar ** ignore_ap_list = g_strsplit(def_str, ",", 0);
461         ignore_ap_count = g_strv_length(ignore_ap_list);
462         for (i = 0; i < ignore_ap_count; i++) {
463                 DBG("[%d] - [%s]", i, ignore_ap_list[i]);
464                 if (strncmp(ignore_ap_list[i], ssid, strlen(ssid)) == 0) {
465                         g_strfreev(ignore_ap_list);
466                         return TRUE;
467                 }
468         }
469
470         g_strfreev(ignore_ap_list);
471         return FALSE;
472 }
473
474 static void __wifi_state_monitor(wifi_service_state_e state,
475                 void *user_data);
476
477 static wifi_state_notifier wifi_state_monitor_notifier = {
478                 .wifi_state_changed = __wifi_state_monitor,
479                 .user_data = NULL,
480 };
481
482 static void __wifi_state_monitor(wifi_service_state_e state,
483                 void *user_data)
484 {
485         DBG("Wi-Fi state: %x", state);
486
487         if (state == NETCONFIG_WIFI_CONNECTED)
488                 return;
489
490         if (is_monitor_notifier_registered == TRUE) {
491                 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
492                 is_monitor_notifier_registered = FALSE;
493         }
494
495 #if defined TIZEN_WEARABLE
496         is_portal_msg_shown = FALSE;
497 #endif
498
499         /* suspend if Internet check activity in progress */
500         if (timer_data.timer_id == 0)
501                 return;
502
503         netconfig_stop_timer(&timer_data.timer_id);
504         netconfig_stop_internet_check();
505
506         DBG("Stopped Internet accessibility check");
507 }
508
509 static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
510 {
511         char *service_profile = NULL;
512         GVariant *reply = NULL;
513
514         DBG("");
515
516         struct poll_timer_data *timer = (struct poll_timer_data *)data;
517         if (timer == NULL)
518                 return FALSE;
519
520         if (TRUE == netconfig_get_internet_status()) {
521                 if (is_monitor_notifier_registered == TRUE) {
522                         wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
523                         is_monitor_notifier_registered = FALSE;
524                 }
525
526                 DBG("Portal logged in successfully and update ConnMan state");
527                 return FALSE; /* to stop the timer */
528         } else {
529                 if (timer->time_elapsed >= TIMER_THRESHOLD) {
530                         DBG("Login failed, update ConnMan");
531
532                         if (is_monitor_notifier_registered == TRUE) {
533                                 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
534                                 is_monitor_notifier_registered = FALSE;
535                         }
536
537                         /* Disconnect and forget the AP */
538                         service_profile = (char*) netconfig_get_default_profile();
539                         if (service_profile && netconfig_is_wifi_profile(service_profile)) {
540                                 /* Now forget the AP*/
541                                 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
542                                                 service_profile, CONNMAN_SERVICE_INTERFACE, "Remove",
543                                                 NULL);
544
545                                 if (reply != NULL)
546                                         g_variant_unref(reply);
547                                 else
548                                         ERR("Failed to forget the AP ");
549                         }
550                 } else {
551                         if (NETCONFIG_WIFI_CONNECTED ==
552                                         wifi_state_get_service_state()) {
553                                 /* check Internet availability by sending and receiving data*/
554                                 netconfig_check_internet_accessibility();
555                                 /* Returning TRUE itself is enough to restart the timer */
556                                 timer->time_elapsed = timer->time_elapsed +
557                                                                         QUERY_FOR_INTERNET_INTERVAL;
558                                 return TRUE;
559                         }
560                 }
561         }
562
563         return FALSE;
564 }
565
566 #if defined TIZEN_WEARABLE
567 static gboolean __netconfig_display_portal_msg(gpointer data)
568 {
569         DBG("");
570         wc_launch_popup(WC_POPUP_TYPE_CAPTIVE_PORTAL);
571
572         netconfig_stop_timer(&portal_msg_timer);
573
574         return FALSE;
575 }
576 #endif
577
578 static void __netconfig_wifi_portal_login_timer_start(struct poll_timer_data
579                 *data)
580 {
581         DBG("__netconfig_wifi_browser_start_timer...starting timer");
582
583         if (data == NULL)
584                 return;
585
586         netconfig_stop_timer(&(data->timer_id));
587
588         /* Timer logic: After successful launch of browser, we would check for
589          * Internet status for every 20s until a threshold of 120s
590          */
591
592         data->time_elapsed = QUERY_FOR_INTERNET_INTERVAL;
593         netconfig_start_timer_seconds(QUERY_FOR_INTERNET_INTERVAL,
594                 __netconfig_wifi_portal_login_timeout, data, &(data->timer_id));
595 }
596 #endif
597
598 gboolean handle_request_browser(NetConnmanAgent *connman_agent,
599                 GDBusMethodInvocation *context, const gchar *service, const gchar *url)
600 {
601 #if defined TIZEN_CAPTIVE_PORTAL
602         gboolean ret = FALSE;
603         gboolean ignore_portal = FALSE;
604         const char * ssid = NULL;
605
606         g_return_val_if_fail(connman_agent != NULL, FALSE);
607
608         DBG("service[%s] - url[%s]", service, url);
609
610         ssid = netconfig_wifi_get_connected_essid(netconfig_get_default_profile());
611         if (ssid == NULL) {
612                 ERR("Connected AP name is NULL!!");
613                 net_connman_agent_complete_request_browser(connman_agent, context);
614                 return FALSE;
615         }
616
617         ignore_portal = __check_ignore_portal_list(ssid);
618
619         if (ignore_portal == TRUE) {
620                 net_connman_agent_complete_request_browser(connman_agent, context);
621                 return TRUE;
622         }
623         /* Register for Wifi state change notifier*/
624         if (is_monitor_notifier_registered == FALSE) {
625                 wifi_state_notifier_register(&wifi_state_monitor_notifier);
626                 is_monitor_notifier_registered = TRUE;
627         }
628
629 #if defined TIZEN_WEARABLE
630         if (is_portal_msg_shown) {
631                 net_connman_agent_complete_request_browser(connman_agent, context);
632                 return TRUE;
633         }
634
635         is_portal_msg_shown = TRUE;
636         netconfig_start_timer_seconds(4, __netconfig_display_portal_msg, NULL, &portal_msg_timer);
637 #else
638         ret = netconfig_send_notification_to_net_popup(NETCONFIG_ADD_PORTAL_NOTI, ssid);
639 #endif
640
641         timer_data.time_elapsed = 0;
642         __netconfig_wifi_portal_login_timer_start(&timer_data);
643
644         net_connman_agent_complete_request_browser(connman_agent, context);
645         return ret;
646 #else
647         GError *error = NULL;
648         error = g_error_new(G_DBUS_ERROR,
649                         G_DBUS_ERROR_AUTH_FAILED,
650                         CONNMAN_ERROR_INTERFACE ".NotSupported");
651
652         g_dbus_method_invocation_return_gerror(context, error);
653         g_clear_error(&error);
654
655         return FALSE;
656 #endif
657 }