5 * Copyright (C) 2007-2014 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
38 #define IFF_LOWER_UP 0x10000
41 #include <dbus/dbus.h>
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/service.h>
51 #include <connman/peer.h>
52 #include <connman/log.h>
53 #include <connman/option.h>
54 #include <connman/storage.h>
55 #include <include/setting.h>
56 #include <connman/provision.h>
57 #include <connman/utsname.h>
58 #include <connman/machine.h>
60 #include <gsupplicant/gsupplicant.h>
62 #define CLEANUP_TIMEOUT 8 /* in seconds */
63 #define INACTIVE_TIMEOUT 12 /* in seconds */
64 #define FAVORITE_MAXIMUM_RETRIES 2
66 #define BGSCAN_DEFAULT "simple:30:-45:300"
67 #define AUTOSCAN_DEFAULT "exponential:3:300"
69 #define P2P_FIND_TIMEOUT 30
70 #define P2P_CONNECTION_TIMEOUT 100
71 #define P2P_LISTEN_PERIOD 500
72 #define P2P_LISTEN_INTERVAL 2000
74 static struct connman_technology *wifi_technology = NULL;
75 static struct connman_technology *p2p_technology = NULL;
77 struct hidden_params {
79 unsigned int ssid_len;
83 GSupplicantScanParams *scan_params;
88 * Used for autoscan "emulation".
89 * Should be removed when wpa_s autoscan support will be by default.
91 struct autoscan_params {
100 struct connman_device *device;
101 struct connman_network *network;
102 struct connman_network *pending_network;
104 GSupplicantInterface *interface;
105 GSupplicantState state;
110 bool interface_ready;
116 struct hidden_params *hidden;
117 bool postpone_hidden;
119 * autoscan "emulation".
121 struct autoscan_params *autoscan;
123 GSupplicantScanParams *scan_params;
124 unsigned int p2p_find_timeout;
125 unsigned int p2p_connection_timeout;
126 struct connman_peer *pending_peer;
127 GSupplicantPeer *peer;
131 #if defined TIZEN_EXT
132 int assoc_retry_count;
133 struct connman_network *scan_pending_network;
137 #if defined TIZEN_EXT
140 #define TIZEN_ASSOC_RETRY_COUNT 4
142 static gboolean wifi_first_scan = false;
143 static gboolean found_with_first_scan = false;
144 static gboolean is_wifi_notifier_registered = false;
147 static GList *iface_list = NULL;
149 static GList *pending_wifi_device = NULL;
150 static GList *p2p_iface_list = NULL;
151 bool wfd_service_registered = false;
153 static void start_autoscan(struct connman_device *device);
155 static int p2p_tech_probe(struct connman_technology *technology)
157 p2p_technology = technology;
162 static void p2p_tech_remove(struct connman_technology *technology)
164 p2p_technology = NULL;
167 static struct connman_technology_driver p2p_tech_driver = {
169 .type = CONNMAN_SERVICE_TYPE_P2P,
170 .probe = p2p_tech_probe,
171 .remove = p2p_tech_remove,
174 static bool is_p2p_connecting(void)
178 for (list = iface_list; list; list = list->next) {
179 struct wifi_data *wifi = list->data;
181 if (wifi->p2p_connecting)
188 static void add_pending_wifi_device(struct wifi_data *wifi)
190 if (g_list_find(pending_wifi_device, wifi))
193 pending_wifi_device = g_list_append(pending_wifi_device, wifi);
196 static struct wifi_data *get_pending_wifi_data(const char *ifname)
200 for (list = pending_wifi_device; list; list = list->next) {
201 struct wifi_data *wifi;
202 const char *dev_name;
205 if (!wifi || !wifi->device)
208 dev_name = connman_device_get_string(wifi->device, "Interface");
209 if (!g_strcmp0(ifname, dev_name)) {
210 pending_wifi_device = g_list_delete_link(
211 pending_wifi_device, list);
219 static void remove_pending_wifi_device(struct wifi_data *wifi)
223 link = g_list_find(pending_wifi_device, wifi);
228 pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
231 static void peer_cancel_timeout(struct wifi_data *wifi)
233 if (wifi->p2p_connection_timeout > 0)
234 g_source_remove(wifi->p2p_connection_timeout);
236 wifi->p2p_connection_timeout = 0;
237 wifi->p2p_connecting = false;
239 if (wifi->pending_peer) {
240 connman_peer_unref(wifi->pending_peer);
241 wifi->pending_peer = NULL;
247 static gboolean peer_connect_timeout(gpointer data)
249 struct wifi_data *wifi = data;
253 if (wifi->p2p_connecting) {
254 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
256 if (g_supplicant_peer_has_requested_connection(wifi->peer))
257 state = CONNMAN_PEER_STATE_IDLE;
259 connman_peer_set_state(wifi->pending_peer, state);
262 peer_cancel_timeout(wifi);
267 static void peer_connect_callback(int result, GSupplicantInterface *interface,
270 struct wifi_data *wifi = user_data;
271 struct connman_peer *peer = wifi->pending_peer;
273 DBG("peer %p - %d", peer, result);
279 peer_connect_timeout(wifi);
283 connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
285 wifi->p2p_connection_timeout = g_timeout_add_seconds(
286 P2P_CONNECTION_TIMEOUT,
287 peer_connect_timeout, wifi);
290 static int peer_connect(struct connman_peer *peer,
291 enum connman_peer_wps_method wps_method,
294 struct connman_device *device = connman_peer_get_device(peer);
295 GSupplicantPeerParams *peer_params;
296 GSupplicantPeer *gs_peer;
297 struct wifi_data *wifi;
301 DBG("peer %p", peer);
306 wifi = connman_device_get_data(device);
310 if (wifi->p2p_connecting)
315 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
316 connman_peer_get_identifier(peer));
320 pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
321 pin = g_supplicant_peer_is_wps_pin(gs_peer);
323 switch (wps_method) {
324 case CONNMAN_PEER_WPS_UNKNOWN:
325 if ((pbc && pin) || pin)
328 case CONNMAN_PEER_WPS_PBC:
333 case CONNMAN_PEER_WPS_PIN:
334 if (!pin || !wps_pin)
339 peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
343 peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
345 peer_params->wps_pin = g_strdup(wps_pin);
347 peer_params->master = connman_peer_service_is_master();
349 ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
350 peer_connect_callback, wifi);
351 if (ret == -EINPROGRESS) {
352 wifi->pending_peer = connman_peer_ref(peer);
353 wifi->peer = gs_peer;
354 wifi->p2p_connecting = true;
361 static int peer_disconnect(struct connman_peer *peer)
363 struct connman_device *device = connman_peer_get_device(peer);
364 GSupplicantPeerParams peer_params = {};
365 GSupplicantPeer *gs_peer;
366 struct wifi_data *wifi;
369 DBG("peer %p", peer);
374 wifi = connman_device_get_data(device);
378 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
379 connman_peer_get_identifier(peer));
383 peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
385 ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
387 g_free(peer_params.path);
389 if (ret == -EINPROGRESS)
390 peer_cancel_timeout(wifi);
395 struct peer_service_registration {
396 peer_service_registration_cb_t callback;
400 static bool is_service_wfd(const unsigned char *specs, int length)
402 if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
408 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
410 struct wifi_data *wifi = data;
412 if (!wifi->interface ||
413 !g_supplicant_interface_has_p2p(wifi->interface))
416 if (!wifi->servicing) {
417 g_supplicant_interface_p2p_listen(wifi->interface,
418 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
424 static void register_wfd_service_cb(int result,
425 GSupplicantInterface *iface, void *user_data)
427 struct peer_service_registration *reg_data = user_data;
432 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
434 if (reg_data && reg_data->callback) {
435 reg_data->callback(result, reg_data->user_data);
440 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
441 const unsigned char *spec,
442 int spec_length, const unsigned char *query,
443 int query_length, int version)
445 GSupplicantP2PServiceParams *params;
447 params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
452 params->version = version;
453 params->service = g_memdup(spec, spec_length);
454 } else if (query_length > 0 && spec_length > 0) {
455 params->query = g_memdup(query, query_length);
456 params->query_length = query_length;
458 params->response = g_memdup(spec, spec_length);
459 params->response_length = spec_length;
461 params->wfd_ies = g_memdup(spec, spec_length);
462 params->wfd_ies_length = spec_length;
468 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
473 g_free(params->service);
474 g_free(params->query);
475 g_free(params->response);
476 g_free(params->wfd_ies);
481 static int peer_register_wfd_service(const unsigned char *specification,
482 int specification_length,
483 peer_service_registration_cb_t callback,
486 struct peer_service_registration *reg_data = NULL;
487 static GSupplicantP2PServiceParams *params;
492 if (wfd_service_registered)
495 params = fill_in_peer_service_params(specification,
496 specification_length, NULL, 0, 0);
500 reg_data = g_try_malloc0(sizeof(*reg_data));
506 reg_data->callback = callback;
507 reg_data->user_data = user_data;
509 ret = g_supplicant_set_widi_ies(params,
510 register_wfd_service_cb, reg_data);
511 if (ret < 0 && ret != -EINPROGRESS)
514 wfd_service_registered = true;
518 free_peer_service_params(params);
524 static void register_peer_service_cb(int result,
525 GSupplicantInterface *iface, void *user_data)
527 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
528 struct peer_service_registration *reg_data = user_data;
533 apply_p2p_listen_on_iface(wifi, NULL);
535 if (reg_data->callback)
536 reg_data->callback(result, reg_data->user_data);
541 static int peer_register_service(const unsigned char *specification,
542 int specification_length,
543 const unsigned char *query,
544 int query_length, int version,
545 peer_service_registration_cb_t callback,
548 struct peer_service_registration *reg_data;
549 GSupplicantP2PServiceParams *params;
556 if (specification && !version && !query &&
557 is_service_wfd(specification, specification_length)) {
558 return peer_register_wfd_service(specification,
559 specification_length, callback, user_data);
562 reg_data = g_try_malloc0(sizeof(*reg_data));
566 reg_data->callback = callback;
567 reg_data->user_data = user_data;
571 for (list = iface_list; list; list = list->next) {
572 struct wifi_data *wifi = list->data;
573 GSupplicantInterface *iface = wifi->interface;
575 if (!g_supplicant_interface_has_p2p(iface))
578 params = fill_in_peer_service_params(specification,
579 specification_length, query,
580 query_length, version);
587 ret_f = g_supplicant_interface_p2p_add_service(iface,
588 register_peer_service_cb, params, reg_data);
589 if (ret_f == 0 || ret_f == -EINPROGRESS)
593 ret = g_supplicant_interface_p2p_add_service(iface,
594 register_peer_service_cb, params, NULL);
595 if (ret != 0 && ret != -EINPROGRESS)
596 free_peer_service_params(params);
599 if (ret_f != 0 && ret_f != -EINPROGRESS)
605 static int peer_unregister_wfd_service(void)
607 GSupplicantP2PServiceParams *params;
610 if (!wfd_service_registered)
613 params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
617 wfd_service_registered = false;
619 g_supplicant_set_widi_ies(params, NULL, NULL);
621 for (list = iface_list; list; list = list->next) {
622 struct wifi_data *wifi = list->data;
624 if (!g_supplicant_interface_has_p2p(wifi->interface))
628 if (!wifi->servicing || wifi->servicing < 0) {
629 g_supplicant_interface_p2p_listen(wifi->interface,
638 static int peer_unregister_service(const unsigned char *specification,
639 int specification_length,
640 const unsigned char *query,
641 int query_length, int version)
643 GSupplicantP2PServiceParams *params;
648 if (specification && !version && !query &&
649 is_service_wfd(specification, specification_length)) {
650 ret = peer_unregister_wfd_service();
651 if (ret != 0 && ret != -EINPROGRESS)
656 for (list = iface_list; list; list = list->next) {
657 struct wifi_data *wifi = list->data;
658 GSupplicantInterface *iface = wifi->interface;
663 if (!g_supplicant_interface_has_p2p(iface))
666 params = fill_in_peer_service_params(specification,
667 specification_length, query,
668 query_length, version);
674 ret = g_supplicant_interface_p2p_del_service(iface, params);
675 if (ret != 0 && ret != -EINPROGRESS)
676 free_peer_service_params(params);
679 if (!wifi->servicing || wifi->servicing < 0) {
680 g_supplicant_interface_p2p_listen(iface, 0, 0);
688 static struct connman_peer_driver peer_driver = {
689 .connect = peer_connect,
690 .disconnect = peer_disconnect,
691 .register_service = peer_register_service,
692 .unregister_service = peer_unregister_service,
695 static void handle_tethering(struct wifi_data *wifi)
697 if (!wifi->tethering)
706 DBG("index %d bridge %s", wifi->index, wifi->bridge);
708 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
711 wifi->bridged = true;
714 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
716 struct connman_device *device = user_data;
717 struct wifi_data *wifi = connman_device_get_data(device);
722 DBG("index %d flags %d change %d", wifi->index, flags, change);
724 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
728 DBG("interface down");
731 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
732 if (flags & IFF_LOWER_UP) {
735 handle_tethering(wifi);
743 static int wifi_probe(struct connman_device *device)
745 struct wifi_data *wifi;
747 DBG("device %p", device);
749 wifi = g_try_new0(struct wifi_data, 1);
753 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
755 connman_device_set_data(device, wifi);
756 wifi->device = connman_device_ref(device);
758 wifi->index = connman_device_get_index(device);
761 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
762 wifi_newlink, device);
763 if (is_p2p_connecting())
764 add_pending_wifi_device(wifi);
766 iface_list = g_list_append(iface_list, wifi);
771 static void remove_networks(struct connman_device *device,
772 struct wifi_data *wifi)
776 for (list = wifi->networks; list; list = list->next) {
777 struct connman_network *network = list->data;
779 connman_device_remove_network(device, network);
780 connman_network_unref(network);
783 g_slist_free(wifi->networks);
784 wifi->networks = NULL;
787 static void reset_autoscan(struct connman_device *device)
789 struct wifi_data *wifi = connman_device_get_data(device);
790 struct autoscan_params *autoscan;
794 if (!wifi || !wifi->autoscan)
797 autoscan = wifi->autoscan;
799 if (autoscan->timeout == 0 && autoscan->interval == 0)
802 g_source_remove(autoscan->timeout);
804 autoscan->timeout = 0;
805 autoscan->interval = 0;
807 connman_device_unref(device);
810 static void stop_autoscan(struct connman_device *device)
812 const struct wifi_data *wifi = connman_device_get_data(device);
814 if (!wifi || !wifi->autoscan)
817 reset_autoscan(device);
819 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
822 static void check_p2p_technology(void)
824 bool p2p_exists = false;
827 for (list = iface_list; list; list = list->next) {
828 struct wifi_data *w = list->data;
831 g_supplicant_interface_has_p2p(w->interface))
836 connman_technology_driver_unregister(&p2p_tech_driver);
837 connman_peer_driver_unregister(&peer_driver);
841 static void wifi_remove(struct connman_device *device)
843 struct wifi_data *wifi = connman_device_get_data(device);
845 DBG("device %p wifi %p", device, wifi);
850 stop_autoscan(device);
852 if (wifi->p2p_device)
853 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
855 iface_list = g_list_remove(iface_list, wifi);
857 check_p2p_technology();
859 remove_pending_wifi_device(wifi);
861 if (wifi->p2p_find_timeout) {
862 g_source_remove(wifi->p2p_find_timeout);
863 connman_device_unref(wifi->device);
866 if (wifi->p2p_connection_timeout)
867 g_source_remove(wifi->p2p_connection_timeout);
869 remove_networks(device, wifi);
871 connman_device_set_powered(device, false);
872 connman_device_set_data(device, NULL);
873 connman_device_unref(wifi->device);
874 connman_rtnl_remove_watch(wifi->watch);
876 g_supplicant_interface_set_data(wifi->interface, NULL);
878 g_supplicant_interface_cancel(wifi->interface);
880 if (wifi->scan_params)
881 g_supplicant_free_scan_params(wifi->scan_params);
883 g_free(wifi->autoscan);
884 g_free(wifi->identifier);
888 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
892 for (iter = list; iter; iter = g_slist_next(iter)) {
893 struct scan_ssid *scan_ssid = iter->data;
895 if (ssid_len == scan_ssid->ssid_len &&
896 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
903 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
904 int freq, GSupplicantScanParams *scan_data,
905 int driver_max_scan_ssids, char *ssid_name)
908 struct scan_ssid *scan_ssid;
910 if ((driver_max_scan_ssids == 0 ||
911 driver_max_scan_ssids > scan_data->num_ssids) &&
912 (hex_ssid || raw_ssid)) {
914 unsigned int j = 0, hex;
917 size_t hex_ssid_len = strlen(hex_ssid);
919 ssid = g_try_malloc0(hex_ssid_len / 2);
923 for (i = 0; i < hex_ssid_len; i += 2) {
924 sscanf(hex_ssid + i, "%02x", &hex);
933 * If we have already added hidden AP to the list,
934 * then do not do it again. This might happen if you have
935 * used or are using multiple wifi cards, so in that case
936 * you might have multiple service files for same AP.
938 if (is_duplicate(scan_data->ssids, ssid, j)) {
944 scan_ssid = g_try_new(struct scan_ssid, 1);
951 memcpy(scan_ssid->ssid, ssid, j);
952 scan_ssid->ssid_len = j;
953 scan_data->ssids = g_slist_prepend(scan_data->ssids,
956 scan_data->num_ssids++;
958 DBG("SSID %s added to scanned list of %d entries", ssid_name,
959 scan_data->num_ssids);
966 scan_data->ssids = g_slist_reverse(scan_data->ssids);
968 if (!scan_data->freqs) {
969 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
970 if (!scan_data->freqs) {
971 g_slist_free_full(scan_data->ssids, g_free);
975 scan_data->num_freqs = 1;
976 scan_data->freqs[0] = freq;
978 bool duplicate = false;
980 /* Don't add duplicate entries */
981 for (i = 0; i < scan_data->num_freqs; i++) {
982 if (scan_data->freqs[i] == freq) {
989 scan_data->num_freqs++;
990 scan_data->freqs = g_try_realloc(scan_data->freqs,
991 sizeof(uint16_t) * scan_data->num_freqs);
992 if (!scan_data->freqs) {
993 g_slist_free_full(scan_data->ssids, g_free);
996 scan_data->freqs[scan_data->num_freqs - 1] = freq;
1003 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1005 struct connman_config_entry **entries;
1011 int num_ssids = 0, add_param_failed = 0;
1013 services = connman_storage_get_services();
1014 for (i = 0; services && services[i]; i++) {
1015 if (strncmp(services[i], "wifi_", 5) != 0)
1018 keyfile = connman_storage_load_service(services[i]);
1022 value = g_key_file_get_boolean(keyfile,
1023 services[i], "Hidden", NULL);
1025 g_key_file_free(keyfile);
1029 value = g_key_file_get_boolean(keyfile,
1030 services[i], "Favorite", NULL);
1032 g_key_file_free(keyfile);
1036 #if defined TIZEN_EXT
1037 value = g_key_file_get_boolean(keyfile,
1038 services[i], "AutoConnect", NULL);
1040 g_key_file_free(keyfile);
1045 ssid = g_key_file_get_string(keyfile,
1046 services[i], "SSID", NULL);
1048 name = g_key_file_get_string(keyfile, services[i], "Name",
1051 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1059 g_key_file_free(keyfile);
1063 * Check if there are any hidden AP that needs to be provisioned.
1065 entries = connman_config_get_entries("wifi");
1066 for (i = 0; entries && entries[i]; i++) {
1069 if (!entries[i]->hidden)
1072 if (!entries[i]->ssid) {
1073 ssid = entries[i]->name;
1076 ssid = entries[i]->ssid;
1077 len = entries[i]->ssid_len;
1083 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1090 connman_config_free_entries(entries);
1092 if (add_param_failed > 0)
1093 DBG("Unable to scan %d out of %d SSIDs",
1094 add_param_failed, num_ssids);
1096 g_strfreev(services);
1101 static int get_hidden_connections_params(struct wifi_data *wifi,
1102 GSupplicantScanParams *scan_params)
1104 int driver_max_ssids, i;
1105 GSupplicantScanParams *orig_params;
1108 * Scan hidden networks so that we can autoconnect to them.
1109 * We will assume 1 as a default number of ssid to scan.
1111 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1113 if (driver_max_ssids == 0)
1114 driver_max_ssids = 1;
1116 DBG("max ssids %d", driver_max_ssids);
1118 if (!wifi->scan_params) {
1119 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1120 if (!wifi->scan_params)
1123 if (get_hidden_connections(wifi->scan_params) == 0) {
1124 g_supplicant_free_scan_params(wifi->scan_params);
1125 wifi->scan_params = NULL;
1131 orig_params = wifi->scan_params;
1133 /* Let's transfer driver_max_ssids params */
1134 for (i = 0; i < driver_max_ssids; i++) {
1135 struct scan_ssid *ssid;
1137 if (!wifi->scan_params->ssids)
1140 ssid = orig_params->ssids->data;
1141 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1142 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1146 scan_params->num_ssids = i;
1147 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1149 scan_params->freqs = g_memdup(orig_params->freqs,
1150 sizeof(uint16_t) * orig_params->num_freqs);
1151 if (!scan_params->freqs)
1154 scan_params->num_freqs = orig_params->num_freqs;
1159 orig_params->num_ssids -= scan_params->num_ssids;
1161 return scan_params->num_ssids;
1164 g_slist_free_full(scan_params->ssids, g_free);
1165 g_supplicant_free_scan_params(wifi->scan_params);
1166 wifi->scan_params = NULL;
1171 static int throw_wifi_scan(struct connman_device *device,
1172 GSupplicantInterfaceCallback callback)
1174 struct wifi_data *wifi = connman_device_get_data(device);
1180 DBG("device %p %p", device, wifi->interface);
1182 if (wifi->tethering)
1185 if (connman_device_get_scanning(device))
1188 connman_device_ref(device);
1190 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1193 connman_device_set_scanning(device,
1194 CONNMAN_SERVICE_TYPE_WIFI, true);
1196 connman_device_unref(device);
1201 static void hidden_free(struct hidden_params *hidden)
1206 if (hidden->scan_params)
1207 g_supplicant_free_scan_params(hidden->scan_params);
1208 g_free(hidden->identity);
1209 g_free(hidden->passphrase);
1210 g_free(hidden->security);
1214 #if defined TIZEN_EXT
1215 static void service_state_changed(struct connman_service *service,
1216 enum connman_service_state state);
1218 static int network_connect(struct connman_network *network);
1220 static struct connman_notifier notifier = {
1222 .priority = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
1223 .service_state_changed = service_state_changed,
1226 static void service_state_changed(struct connman_service *service,
1227 enum connman_service_state state)
1229 enum connman_service_type type;
1231 type = connman_service_get_type(service);
1232 if (type != CONNMAN_SERVICE_TYPE_WIFI)
1235 DBG("service %p state %d", service, state);
1238 case CONNMAN_SERVICE_STATE_READY:
1239 case CONNMAN_SERVICE_STATE_ONLINE:
1240 case CONNMAN_SERVICE_STATE_FAILURE:
1241 connman_notifier_unregister(¬ifier);
1242 is_wifi_notifier_registered = FALSE;
1244 __connman_device_request_scan(type);
1253 static void scan_callback(int result, GSupplicantInterface *interface,
1256 struct connman_device *device = user_data;
1257 struct wifi_data *wifi = connman_device_get_data(device);
1260 DBG("result %d wifi %p", result, wifi);
1263 if (wifi->hidden && !wifi->postpone_hidden) {
1264 connman_network_clear_hidden(wifi->hidden->user_data);
1265 hidden_free(wifi->hidden);
1266 wifi->hidden = NULL;
1269 if (wifi->scan_params) {
1270 g_supplicant_free_scan_params(wifi->scan_params);
1271 wifi->scan_params = NULL;
1276 connman_device_reset_scanning(device);
1278 /* User is connecting to a hidden AP, let's wait for finished event */
1279 if (wifi && wifi->hidden && wifi->postpone_hidden) {
1280 GSupplicantScanParams *scan_params;
1283 wifi->postpone_hidden = false;
1284 scan_params = wifi->hidden->scan_params;
1285 wifi->hidden->scan_params = NULL;
1287 reset_autoscan(device);
1289 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1290 scan_callback, device);
1294 /* On error, let's recall scan_callback, which will cleanup */
1295 return scan_callback(ret, interface, user_data);
1298 scanning = connman_device_get_scanning(device);
1301 connman_device_set_scanning(device,
1302 CONNMAN_SERVICE_TYPE_WIFI, false);
1305 if (result != -ENOLINK)
1306 #if defined TIZEN_EXT
1309 start_autoscan(device);
1312 * If we are here then we were scanning; however, if we are
1313 * also mid-flight disabling the interface, then wifi_disable
1314 * has already cleared the device scanning state and
1315 * unreferenced the device, obviating the need to do it here.
1319 connman_device_unref(device);
1321 #if defined TIZEN_EXT
1322 if (wifi && wifi->scan_pending_network && result != -EIO) {
1323 network_connect(wifi->scan_pending_network);
1324 wifi->scan_pending_network = NULL;
1325 connman_network_set_connecting(wifi->network);
1328 if (is_wifi_notifier_registered != true &&
1329 wifi_first_scan == true && found_with_first_scan == true) {
1330 wifi_first_scan = false;
1331 found_with_first_scan = false;
1333 connman_notifier_register(¬ifier);
1334 is_wifi_notifier_registered = true;
1339 static void scan_callback_hidden(int result,
1340 GSupplicantInterface *interface, void *user_data)
1342 struct connman_device *device = user_data;
1343 struct wifi_data *wifi = connman_device_get_data(device);
1344 GSupplicantScanParams *scan_params;
1347 DBG("result %d wifi %p", result, wifi);
1352 /* User is trying to connect to a hidden AP */
1353 if (wifi->hidden && wifi->postpone_hidden)
1356 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1360 if (get_hidden_connections_params(wifi, scan_params) > 0) {
1361 ret = g_supplicant_interface_scan(wifi->interface,
1363 scan_callback_hidden,
1369 g_supplicant_free_scan_params(scan_params);
1372 scan_callback(result, interface, user_data);
1375 static gboolean autoscan_timeout(gpointer data)
1377 struct connman_device *device = data;
1378 struct wifi_data *wifi = connman_device_get_data(device);
1379 struct autoscan_params *autoscan;
1385 autoscan = wifi->autoscan;
1387 if (autoscan->interval <= 0) {
1388 interval = autoscan->base;
1391 interval = autoscan->interval * autoscan->base;
1393 #if defined TIZEN_EXT
1394 if (autoscan->interval >= autoscan->limit)
1396 if (interval > autoscan->limit)
1398 interval = autoscan->limit;
1400 throw_wifi_scan(wifi->device, scan_callback_hidden);
1403 DBG("interval %d", interval);
1405 autoscan->interval = interval;
1407 autoscan->timeout = g_timeout_add_seconds(interval,
1408 autoscan_timeout, device);
1413 static void start_autoscan(struct connman_device *device)
1415 struct wifi_data *wifi = connman_device_get_data(device);
1416 struct autoscan_params *autoscan;
1423 if (wifi->p2p_device)
1426 if (wifi->connected)
1429 autoscan = wifi->autoscan;
1433 if (autoscan->timeout > 0 || autoscan->interval > 0)
1436 connman_device_ref(device);
1438 autoscan_timeout(device);
1441 static struct autoscan_params *parse_autoscan_params(const char *params)
1443 struct autoscan_params *autoscan;
1448 DBG("Emulating autoscan");
1450 list_params = g_strsplit(params, ":", 0);
1451 if (list_params == 0)
1454 if (g_strv_length(list_params) < 3) {
1455 g_strfreev(list_params);
1459 base = atoi(list_params[1]);
1460 limit = atoi(list_params[2]);
1462 g_strfreev(list_params);
1464 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1466 DBG("Could not allocate memory for autoscan");
1470 DBG("base %d - limit %d", base, limit);
1471 autoscan->base = base;
1472 autoscan->limit = limit;
1477 static void setup_autoscan(struct wifi_data *wifi)
1479 if (!wifi->autoscan)
1480 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
1482 start_autoscan(wifi->device);
1485 static void finalize_interface_creation(struct wifi_data *wifi)
1487 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1489 if (!wifi->device) {
1490 connman_error("WiFi device not set");
1494 connman_device_set_powered(wifi->device, true);
1496 if (!connman_setting_get_bool("BackgroundScanning"))
1499 if (wifi->p2p_device)
1502 setup_autoscan(wifi);
1505 static void interface_create_callback(int result,
1506 GSupplicantInterface *interface,
1509 struct wifi_data *wifi = user_data;
1511 DBG("result %d ifname %s, wifi %p", result,
1512 g_supplicant_interface_get_ifname(interface),
1515 if (result < 0 || !wifi)
1518 wifi->interface = interface;
1519 g_supplicant_interface_set_data(interface, wifi);
1521 if (g_supplicant_interface_get_ready(interface)) {
1522 wifi->interface_ready = true;
1523 finalize_interface_creation(wifi);
1527 static int wifi_enable(struct connman_device *device)
1529 struct wifi_data *wifi = connman_device_get_data(device);
1532 const char *driver = connman_option_get_string("wifi");
1535 DBG("device %p %p", device, wifi);
1537 index = connman_device_get_index(device);
1538 if (!wifi || index < 0)
1541 if (is_p2p_connecting())
1542 return -EINPROGRESS;
1544 interface = connman_inet_ifname(index);
1545 ret = g_supplicant_interface_create(interface, driver, NULL,
1546 interface_create_callback,
1553 return -EINPROGRESS;
1556 static int wifi_disable(struct connman_device *device)
1558 struct wifi_data *wifi = connman_device_get_data(device);
1561 DBG("device %p wifi %p", device, wifi);
1566 wifi->connected = false;
1567 wifi->disconnecting = false;
1569 if (wifi->pending_network)
1570 wifi->pending_network = NULL;
1572 stop_autoscan(device);
1574 if (wifi->p2p_find_timeout) {
1575 g_source_remove(wifi->p2p_find_timeout);
1576 wifi->p2p_find_timeout = 0;
1577 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1578 connman_device_unref(wifi->device);
1581 /* In case of a user scan, device is still referenced */
1582 if (connman_device_get_scanning(device)) {
1583 connman_device_set_scanning(device,
1584 CONNMAN_SERVICE_TYPE_WIFI, false);
1585 connman_device_unref(wifi->device);
1588 remove_networks(device, wifi);
1590 #if defined TIZEN_EXT
1591 wifi->scan_pending_network = NULL;
1593 if (is_wifi_notifier_registered == true) {
1594 connman_notifier_unregister(¬ifier);
1595 is_wifi_notifier_registered = false;
1599 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1603 return -EINPROGRESS;
1606 struct last_connected {
1612 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1614 GTimeVal *aval = (GTimeVal *)a;
1615 GTimeVal *bval = (GTimeVal *)b;
1617 /* Note that the sort order is descending */
1618 if (aval->tv_sec < bval->tv_sec)
1621 if (aval->tv_sec > bval->tv_sec)
1627 static void free_entry(gpointer data)
1629 struct last_connected *entry = data;
1631 g_free(entry->ssid);
1635 static int get_latest_connections(int max_ssids,
1636 GSupplicantScanParams *scan_data)
1638 GSequenceIter *iter;
1639 GSequence *latest_list;
1640 struct last_connected *entry;
1649 latest_list = g_sequence_new(free_entry);
1653 services = connman_storage_get_services();
1654 for (i = 0; services && services[i]; i++) {
1655 if (strncmp(services[i], "wifi_", 5) != 0)
1658 keyfile = connman_storage_load_service(services[i]);
1662 str = g_key_file_get_string(keyfile,
1663 services[i], "Favorite", NULL);
1664 if (!str || g_strcmp0(str, "true")) {
1666 g_key_file_free(keyfile);
1671 str = g_key_file_get_string(keyfile,
1672 services[i], "AutoConnect", NULL);
1673 if (!str || g_strcmp0(str, "true")) {
1675 g_key_file_free(keyfile);
1680 str = g_key_file_get_string(keyfile,
1681 services[i], "Modified", NULL);
1683 g_key_file_free(keyfile);
1686 g_time_val_from_iso8601(str, &modified);
1689 ssid = g_key_file_get_string(keyfile,
1690 services[i], "SSID", NULL);
1692 freq = g_key_file_get_integer(keyfile, services[i],
1695 entry = g_try_new(struct last_connected, 1);
1697 g_sequence_free(latest_list);
1698 g_key_file_free(keyfile);
1704 entry->modified = modified;
1707 g_sequence_insert_sorted(latest_list, entry,
1713 g_key_file_free(keyfile);
1716 g_strfreev(services);
1718 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1720 iter = g_sequence_get_begin_iter(latest_list);
1722 for (i = 0; i < num_ssids; i++) {
1723 entry = g_sequence_get(iter);
1725 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1726 entry->modified.tv_sec);
1728 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1729 max_ssids, entry->ssid);
1731 iter = g_sequence_iter_next(iter);
1734 g_sequence_free(latest_list);
1738 static int wifi_scan_simple(struct connman_device *device)
1740 reset_autoscan(device);
1742 return throw_wifi_scan(device, scan_callback_hidden);
1745 static gboolean p2p_find_stop(gpointer data)
1747 struct connman_device *device = data;
1748 struct wifi_data *wifi = connman_device_get_data(device);
1752 wifi->p2p_find_timeout = 0;
1754 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1756 g_supplicant_interface_p2p_stop_find(wifi->interface);
1758 connman_device_unref(device);
1759 reset_autoscan(device);
1764 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1767 struct connman_device *device = user_data;
1768 struct wifi_data *wifi = connman_device_get_data(device);
1770 DBG("result %d wifi %p", result, wifi);
1772 if (wifi->p2p_find_timeout) {
1773 g_source_remove(wifi->p2p_find_timeout);
1774 wifi->p2p_find_timeout = 0;
1780 wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1781 p2p_find_stop, device);
1782 if (!wifi->p2p_find_timeout)
1787 p2p_find_stop(device);
1790 static int p2p_find(struct connman_device *device)
1792 struct wifi_data *wifi;
1797 if (!p2p_technology)
1800 wifi = connman_device_get_data(device);
1802 if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1805 reset_autoscan(device);
1806 connman_device_ref(device);
1808 ret = g_supplicant_interface_p2p_find(wifi->interface,
1809 p2p_find_callback, device);
1811 connman_device_unref(device);
1812 start_autoscan(device);
1814 connman_device_set_scanning(device,
1815 CONNMAN_SERVICE_TYPE_P2P, true);
1822 * Note that the hidden scan is only used when connecting to this specific
1823 * hidden AP first time. It is not used when system autoconnects to hidden AP.
1825 static int wifi_scan(enum connman_service_type type,
1826 struct connman_device *device,
1827 const char *ssid, unsigned int ssid_len,
1828 const char *identity, const char* passphrase,
1829 const char *security, void *user_data)
1831 struct wifi_data *wifi = connman_device_get_data(device);
1832 GSupplicantScanParams *scan_params = NULL;
1833 struct scan_ssid *scan_ssid;
1834 struct hidden_params *hidden;
1836 int driver_max_ssids = 0;
1843 if (wifi->p2p_device)
1846 if (type == CONNMAN_SERVICE_TYPE_P2P)
1847 return p2p_find(device);
1849 DBG("device %p wifi %p hidden ssid %s", device, wifi->interface, ssid);
1851 if (wifi->tethering)
1854 scanning = connman_device_get_scanning(device);
1856 if (!ssid || ssid_len == 0 || ssid_len > 32) {
1860 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1862 DBG("max ssids %d", driver_max_ssids);
1863 if (driver_max_ssids == 0)
1864 return wifi_scan_simple(device);
1868 if (scanning && wifi->hidden && wifi->postpone_hidden)
1874 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1879 scan_ssid = g_try_new(struct scan_ssid, 1);
1881 g_free(scan_params);
1885 memcpy(scan_ssid->ssid, ssid, ssid_len);
1886 scan_ssid->ssid_len = ssid_len;
1887 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1889 scan_params->num_ssids = 1;
1891 hidden = g_try_new0(struct hidden_params, 1);
1893 g_supplicant_free_scan_params(scan_params);
1898 hidden_free(wifi->hidden);
1899 wifi->hidden = NULL;
1902 memcpy(hidden->ssid, ssid, ssid_len);
1903 hidden->ssid_len = ssid_len;
1904 hidden->identity = g_strdup(identity);
1905 hidden->passphrase = g_strdup(passphrase);
1906 hidden->security = g_strdup(security);
1907 hidden->user_data = user_data;
1908 wifi->hidden = hidden;
1911 /* Let's keep this active scan for later,
1912 * when current scan will be over. */
1913 wifi->postpone_hidden = TRUE;
1914 hidden->scan_params = scan_params;
1918 } else if (wifi->connected) {
1919 g_supplicant_free_scan_params(scan_params);
1920 return wifi_scan_simple(device);
1922 ret = get_latest_connections(driver_max_ssids, scan_params);
1924 g_supplicant_free_scan_params(scan_params);
1925 return wifi_scan_simple(device);
1929 connman_device_ref(device);
1931 reset_autoscan(device);
1933 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1934 scan_callback, device);
1937 connman_device_set_scanning(device,
1938 CONNMAN_SERVICE_TYPE_WIFI, true);
1940 g_supplicant_free_scan_params(scan_params);
1941 connman_device_unref(device);
1944 hidden_free(wifi->hidden);
1945 wifi->hidden = NULL;
1952 static void wifi_regdom_callback(int result,
1956 struct connman_device *device = user_data;
1958 connman_device_regdom_notify(device, result, alpha2);
1960 connman_device_unref(device);
1963 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1965 struct wifi_data *wifi = connman_device_get_data(device);
1971 connman_device_ref(device);
1973 ret = g_supplicant_interface_set_country(wifi->interface,
1974 wifi_regdom_callback,
1977 connman_device_unref(device);
1982 static struct connman_device_driver wifi_ng_driver = {
1984 .type = CONNMAN_DEVICE_TYPE_WIFI,
1985 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
1986 .probe = wifi_probe,
1987 .remove = wifi_remove,
1988 .enable = wifi_enable,
1989 .disable = wifi_disable,
1991 .set_regdom = wifi_set_regdom,
1994 static void system_ready(void)
1998 if (connman_device_driver_register(&wifi_ng_driver) < 0)
1999 connman_error("Failed to register WiFi driver");
2002 static void system_killed(void)
2006 connman_device_driver_unregister(&wifi_ng_driver);
2009 static int network_probe(struct connman_network *network)
2011 DBG("network %p", network);
2016 static void network_remove(struct connman_network *network)
2018 struct connman_device *device = connman_network_get_device(network);
2019 struct wifi_data *wifi;
2021 DBG("network %p", network);
2023 wifi = connman_device_get_data(device);
2027 if (wifi->network != network)
2030 wifi->network = NULL;
2032 #if defined TIZEN_EXT
2033 wifi->disconnecting = false;
2035 if (wifi->pending_network == network)
2036 wifi->pending_network = NULL;
2038 if (wifi->scan_pending_network == network)
2039 wifi->scan_pending_network = NULL;
2043 static void connect_callback(int result, GSupplicantInterface *interface,
2046 #if defined TIZEN_EXT
2048 struct wifi_data *wifi;
2050 struct connman_network *network = user_data;
2052 DBG("network %p result %d", network, result);
2054 #if defined TIZEN_EXT
2055 for (list = iface_list; list; list = list->next) {
2058 if (wifi && wifi->network == network)
2062 /* wifi_data may be invalid because wifi is already disabled */
2067 if (result == -ENOKEY) {
2068 connman_network_set_error(network,
2069 CONNMAN_NETWORK_ERROR_INVALID_KEY);
2070 } else if (result < 0) {
2071 connman_network_set_error(network,
2072 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
2075 connman_network_unref(network);
2078 static GSupplicantSecurity network_security(const char *security)
2080 if (g_str_equal(security, "none"))
2081 return G_SUPPLICANT_SECURITY_NONE;
2082 else if (g_str_equal(security, "wep"))
2083 return G_SUPPLICANT_SECURITY_WEP;
2084 else if (g_str_equal(security, "psk"))
2085 return G_SUPPLICANT_SECURITY_PSK;
2086 else if (g_str_equal(security, "wpa"))
2087 return G_SUPPLICANT_SECURITY_PSK;
2088 else if (g_str_equal(security, "rsn"))
2089 return G_SUPPLICANT_SECURITY_PSK;
2090 else if (g_str_equal(security, "ieee8021x"))
2091 return G_SUPPLICANT_SECURITY_IEEE8021X;
2092 #if defined TIZEN_EXT
2093 else if (g_str_equal(security, "ft_psk") == TRUE)
2094 return G_SUPPLICANT_SECURITY_FT_PSK;
2095 else if (g_str_equal(security, "ft_ieee8021x") == TRUE)
2096 return G_SUPPLICANT_SECURITY_FT_IEEE8021X;
2099 return G_SUPPLICANT_SECURITY_UNKNOWN;
2102 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
2104 const char *security;
2106 memset(ssid, 0, sizeof(*ssid));
2107 ssid->mode = G_SUPPLICANT_MODE_INFRA;
2108 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
2110 ssid->scan_ssid = 1;
2111 security = connman_network_get_string(network, "WiFi.Security");
2112 ssid->security = network_security(security);
2113 ssid->passphrase = connman_network_get_string(network,
2116 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2119 * If our private key password is unset,
2120 * we use the supplied passphrase. That is needed
2121 * for PEAP where 2 passphrases (identity and client
2122 * cert may have to be provided.
2124 if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2125 connman_network_set_string(network,
2126 "WiFi.PrivateKeyPassphrase",
2128 /* We must have an identity for both PEAP and TLS */
2129 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2131 /* Use agent provided identity as a fallback */
2132 if (!ssid->identity || strlen(ssid->identity) == 0)
2133 ssid->identity = connman_network_get_string(network,
2134 "WiFi.AgentIdentity");
2136 ssid->ca_cert_path = connman_network_get_string(network,
2138 ssid->client_cert_path = connman_network_get_string(network,
2139 "WiFi.ClientCertFile");
2140 ssid->private_key_path = connman_network_get_string(network,
2141 "WiFi.PrivateKeyFile");
2142 ssid->private_key_passphrase = connman_network_get_string(network,
2143 "WiFi.PrivateKeyPassphrase");
2144 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2146 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2147 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2149 #if defined TIZEN_EXT
2150 ssid->bssid = connman_network_get_bssid(network);
2153 if (connman_setting_get_bool("BackgroundScanning"))
2154 ssid->bgscan = BGSCAN_DEFAULT;
2157 static int network_connect(struct connman_network *network)
2159 struct connman_device *device = connman_network_get_device(network);
2160 struct wifi_data *wifi;
2161 GSupplicantInterface *interface;
2162 GSupplicantSSID *ssid;
2164 DBG("network %p", network);
2169 wifi = connman_device_get_data(device);
2173 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2177 interface = wifi->interface;
2179 ssid_init(ssid, network);
2181 if (wifi->disconnecting) {
2182 wifi->pending_network = network;
2185 wifi->network = connman_network_ref(network);
2187 #if defined TIZEN_EXT
2188 wifi->scan_pending_network = NULL;
2191 return g_supplicant_interface_connect(interface, ssid,
2192 connect_callback, network);
2195 return -EINPROGRESS;
2198 static void disconnect_callback(int result, GSupplicantInterface *interface,
2201 #if defined TIZEN_EXT
2203 struct wifi_data *wifi;
2204 struct connman_network *network = user_data;
2206 DBG("network %p result %d", network, result);
2208 for (list = iface_list; list; list = list->next) {
2211 if (wifi->network == NULL && wifi->disconnecting == true)
2212 wifi->disconnecting = false;
2214 if (wifi->network == network)
2218 /* wifi_data may be invalid because wifi is already disabled */
2223 struct wifi_data *wifi = user_data;
2226 DBG("result %d supplicant interface %p wifi %p",
2227 result, interface, wifi);
2229 if (result == -ECONNABORTED) {
2230 DBG("wifi interface no longer available");
2234 if (wifi->network) {
2236 * if result < 0 supplican return an error because
2237 * the network is not current.
2238 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
2239 * failed, call connman_network_set_connected to report
2240 * disconnect is completed.
2243 connman_network_set_connected(wifi->network, false);
2246 wifi->network = NULL;
2248 wifi->disconnecting = false;
2250 if (wifi->pending_network) {
2251 network_connect(wifi->pending_network);
2252 wifi->pending_network = NULL;
2255 start_autoscan(wifi->device);
2258 static int network_disconnect(struct connman_network *network)
2260 struct connman_device *device = connman_network_get_device(network);
2261 struct wifi_data *wifi;
2263 #if defined TIZEN_EXT
2264 struct connman_service *service;
2267 DBG("network %p", network);
2269 wifi = connman_device_get_data(device);
2270 if (!wifi || !wifi->interface)
2273 #if defined TIZEN_EXT
2274 if (connman_network_get_associating(network) == true) {
2275 connman_network_clear_associating(network);
2276 connman_network_set_bool(network, "WiFi.UseWPS", false);
2278 service = connman_service_lookup_from_network(network);
2280 if (service != NULL &&
2281 (__connman_service_is_connected_state(service,
2282 CONNMAN_IPCONFIG_TYPE_IPV4) == false &&
2283 __connman_service_is_connected_state(service,
2284 CONNMAN_IPCONFIG_TYPE_IPV6) == false) &&
2285 (connman_service_get_favorite(service) == false))
2286 __connman_service_set_passphrase(service, NULL);
2289 if (wifi->pending_network == network)
2290 wifi->pending_network = NULL;
2292 if (wifi->scan_pending_network == network)
2293 wifi->scan_pending_network = NULL;
2296 connman_network_set_associating(network, false);
2298 if (wifi->disconnecting)
2301 wifi->disconnecting = true;
2303 #if defined TIZEN_EXT
2304 err = g_supplicant_interface_disconnect(wifi->interface,
2305 disconnect_callback, network);
2307 err = g_supplicant_interface_disconnect(wifi->interface,
2308 disconnect_callback, wifi);
2312 wifi->disconnecting = false;
2317 static struct connman_network_driver network_driver = {
2319 .type = CONNMAN_NETWORK_TYPE_WIFI,
2320 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
2321 .probe = network_probe,
2322 .remove = network_remove,
2323 .connect = network_connect,
2324 .disconnect = network_disconnect,
2327 static void interface_added(GSupplicantInterface *interface)
2329 const char *ifname = g_supplicant_interface_get_ifname(interface);
2330 const char *driver = g_supplicant_interface_get_driver(interface);
2331 struct wifi_data *wifi;
2333 wifi = g_supplicant_interface_get_data(interface);
2335 wifi = get_pending_wifi_data(ifname);
2339 g_supplicant_interface_set_data(interface, wifi);
2340 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2341 wifi->p2p_device = true;
2344 DBG("ifname %s driver %s wifi %p tethering %d",
2345 ifname, driver, wifi, wifi->tethering);
2347 if (!wifi->device) {
2348 connman_error("WiFi device not set");
2352 connman_device_set_powered(wifi->device, true);
2355 static bool is_idle(struct wifi_data *wifi)
2357 DBG("state %d", wifi->state);
2359 switch (wifi->state) {
2360 case G_SUPPLICANT_STATE_UNKNOWN:
2361 case G_SUPPLICANT_STATE_DISABLED:
2362 case G_SUPPLICANT_STATE_DISCONNECTED:
2363 case G_SUPPLICANT_STATE_INACTIVE:
2364 case G_SUPPLICANT_STATE_SCANNING:
2367 case G_SUPPLICANT_STATE_AUTHENTICATING:
2368 case G_SUPPLICANT_STATE_ASSOCIATING:
2369 case G_SUPPLICANT_STATE_ASSOCIATED:
2370 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2371 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2372 case G_SUPPLICANT_STATE_COMPLETED:
2379 static bool is_idle_wps(GSupplicantInterface *interface,
2380 struct wifi_data *wifi)
2382 /* First, let's check if WPS processing did not went wrong */
2383 if (g_supplicant_interface_get_wps_state(interface) ==
2384 G_SUPPLICANT_WPS_STATE_FAIL)
2387 /* Unlike normal connection, being associated while processing wps
2388 * actually means that we are idling. */
2389 switch (wifi->state) {
2390 case G_SUPPLICANT_STATE_UNKNOWN:
2391 case G_SUPPLICANT_STATE_DISABLED:
2392 case G_SUPPLICANT_STATE_DISCONNECTED:
2393 case G_SUPPLICANT_STATE_INACTIVE:
2394 case G_SUPPLICANT_STATE_SCANNING:
2395 case G_SUPPLICANT_STATE_ASSOCIATED:
2397 case G_SUPPLICANT_STATE_AUTHENTICATING:
2398 case G_SUPPLICANT_STATE_ASSOCIATING:
2399 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2400 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2401 case G_SUPPLICANT_STATE_COMPLETED:
2408 static bool handle_wps_completion(GSupplicantInterface *interface,
2409 struct connman_network *network,
2410 struct connman_device *device,
2411 struct wifi_data *wifi)
2415 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2417 const unsigned char *ssid, *wps_ssid;
2418 unsigned int ssid_len, wps_ssid_len;
2419 const char *wps_key;
2421 /* Checking if we got associated with requested
2423 ssid = connman_network_get_blob(network, "WiFi.SSID",
2426 wps_ssid = g_supplicant_interface_get_wps_ssid(
2427 interface, &wps_ssid_len);
2429 if (!wps_ssid || wps_ssid_len != ssid_len ||
2430 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2431 connman_network_set_associating(network, false);
2432 #if defined TIZEN_EXT
2433 g_supplicant_interface_disconnect(wifi->interface,
2434 disconnect_callback, wifi->network);
2436 connman_network_set_bool(network, "WiFi.UseWPS", false);
2437 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2439 g_supplicant_interface_disconnect(wifi->interface,
2440 disconnect_callback, wifi);
2445 wps_key = g_supplicant_interface_get_wps_key(interface);
2446 connman_network_set_string(network, "WiFi.Passphrase",
2449 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2455 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2456 struct connman_network *network,
2457 struct wifi_data *wifi)
2459 #if defined TIZEN_EXT
2460 const char *security;
2461 struct connman_service *service;
2463 if (wifi->connected)
2466 security = connman_network_get_string(network, "WiFi.Security");
2468 if (g_str_equal(security, "ieee8021x") == true &&
2469 wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
2471 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2476 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2479 struct connman_service *service;
2481 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2484 if (wifi->connected)
2488 service = connman_service_lookup_from_network(network);
2494 if (connman_service_get_favorite(service)) {
2495 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2500 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2505 #if defined TIZEN_EXT
2506 static bool handle_wifi_assoc_retry(struct connman_network *network,
2507 struct wifi_data *wifi)
2509 const char *security;
2511 if (!wifi->network || wifi->connected || wifi->disconnecting ||
2512 connman_network_get_connecting(network) != true) {
2513 wifi->assoc_retry_count = 0;
2517 if (wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
2518 wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
2519 wifi->assoc_retry_count = 0;
2523 security = connman_network_get_string(network, "WiFi.Security");
2524 if (g_str_equal(security, "ieee8021x") == true &&
2525 wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
2526 wifi->assoc_retry_count = 0;
2530 if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
2531 wifi->assoc_retry_count = 0;
2533 /* Honestly it's not an invalid-key error,
2534 * however QA team recommends that the invalid-key error
2535 * might be better to display for user experience.
2537 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
2546 static void interface_state(GSupplicantInterface *interface)
2548 struct connman_network *network;
2549 struct connman_device *device;
2550 struct wifi_data *wifi;
2551 GSupplicantState state = g_supplicant_interface_get_state(interface);
2554 wifi = g_supplicant_interface_get_data(interface);
2556 DBG("wifi %p interface state %d", wifi, state);
2561 device = wifi->device;
2565 if (g_supplicant_interface_get_ready(interface) &&
2566 !wifi->interface_ready) {
2567 wifi->interface_ready = true;
2568 finalize_interface_creation(wifi);
2571 network = wifi->network;
2576 case G_SUPPLICANT_STATE_SCANNING:
2579 case G_SUPPLICANT_STATE_AUTHENTICATING:
2580 case G_SUPPLICANT_STATE_ASSOCIATING:
2581 #if defined TIZEN_EXT
2582 reset_autoscan(device);
2584 stop_autoscan(device);
2587 if (!wifi->connected)
2588 connman_network_set_associating(network, true);
2592 case G_SUPPLICANT_STATE_COMPLETED:
2593 #if defined TIZEN_EXT
2594 /* though it should be already reset: */
2595 reset_autoscan(device);
2597 wifi->assoc_retry_count = 0;
2599 wifi->scan_pending_network = NULL;
2601 /* though it should be already stopped: */
2602 stop_autoscan(device);
2605 if (!handle_wps_completion(interface, network, device, wifi))
2608 connman_network_set_connected(network, true);
2611 case G_SUPPLICANT_STATE_DISCONNECTED:
2613 * If we're in one of the idle modes, we have
2614 * not started association yet and thus setting
2615 * those ones to FALSE could cancel an association
2618 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2620 if (is_idle_wps(interface, wifi))
2626 /* If previous state was 4way-handshake, then
2627 * it's either: psk was incorrect and thus we retry
2628 * or if we reach the maximum retries we declare the
2630 if (handle_4way_handshake_failure(interface,
2634 /* We disable the selected network, if not then
2635 * wpa_supplicant will loop retrying */
2636 if (g_supplicant_interface_enable_selected_network(interface,
2638 DBG("Could not disables selected network");
2640 #if defined TIZEN_EXT
2642 int reason_code = 0;
2644 err = g_supplicant_interface_remove_network(wifi->interface);
2646 DBG("Failed to remove network(%d)", err);
2648 reason_code = g_supplicant_interface_get_disconnect_reason(wifi->interface);
2650 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
2651 * Retry association until its retry count is expired */
2652 if (handle_wifi_assoc_retry(network, wifi) == true) {
2653 throw_wifi_scan(wifi->device, scan_callback);
2654 wifi->scan_pending_network = wifi->network;
2658 if(reason_code > 0){
2659 DBG("Set disconnect reason code(%d)", reason_code);
2660 connman_network_set_disconnect_reason(network, reason_code);
2663 /* To avoid unnecessary repeated association in wpa_supplicant,
2664 * "RemoveNetwork" should be made when Wi-Fi is disconnected */
2665 if (wps != true && wifi->network && wifi->disconnecting == false) {
2666 wifi->disconnecting = true;
2667 err = g_supplicant_interface_disconnect(wifi->interface,
2668 disconnect_callback, wifi->network);
2670 wifi->disconnecting = false;
2672 connman_network_set_connected(network, false);
2673 connman_network_set_associating(network, false);
2675 start_autoscan(device);
2681 connman_network_set_connected(network, false);
2682 connman_network_set_associating(network, false);
2683 wifi->disconnecting = false;
2685 start_autoscan(device);
2689 case G_SUPPLICANT_STATE_INACTIVE:
2690 #if defined TIZEN_EXT
2691 if (handle_wps_completion(interface, network, device, wifi) == false)
2694 connman_network_set_associating(network, false);
2695 start_autoscan(device);
2699 case G_SUPPLICANT_STATE_UNKNOWN:
2700 case G_SUPPLICANT_STATE_DISABLED:
2701 case G_SUPPLICANT_STATE_ASSOCIATED:
2702 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2703 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2707 wifi->state = state;
2709 /* Saving wpa_s state policy:
2710 * If connected and if the state changes are roaming related:
2711 * --> We stay connected
2713 * --> We are connected
2715 * --> We are not connected
2718 #if defined TIZEN_EXT
2719 case G_SUPPLICANT_STATE_SCANNING:
2722 case G_SUPPLICANT_STATE_AUTHENTICATING:
2723 case G_SUPPLICANT_STATE_ASSOCIATING:
2724 case G_SUPPLICANT_STATE_ASSOCIATED:
2725 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2726 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2727 if (wifi->connected)
2728 connman_warn("Probably roaming right now!"
2729 " Staying connected...");
2731 wifi->connected = false;
2733 case G_SUPPLICANT_STATE_COMPLETED:
2734 wifi->connected = true;
2737 wifi->connected = false;
2744 static void interface_removed(GSupplicantInterface *interface)
2746 const char *ifname = g_supplicant_interface_get_ifname(interface);
2747 struct wifi_data *wifi;
2749 DBG("ifname %s", ifname);
2751 wifi = g_supplicant_interface_get_data(interface);
2754 wifi->interface = NULL;
2756 if (wifi && wifi->tethering)
2759 if (!wifi || !wifi->device) {
2760 DBG("wifi interface already removed");
2764 connman_device_set_powered(wifi->device, false);
2766 check_p2p_technology();
2769 static void set_device_type(const char *type, char dev_type[17])
2771 const char *oui = "0050F204";
2772 const char *category = "0100";
2773 const char *sub_category = "0000";
2775 if (!g_strcmp0(type, "handset")) {
2777 sub_category = "0500";
2778 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2779 sub_category = "0100";
2780 else if (!g_strcmp0(type, "server"))
2781 sub_category = "0200";
2782 else if (!g_strcmp0(type, "laptop"))
2783 sub_category = "0500";
2784 else if (!g_strcmp0(type, "desktop"))
2785 sub_category = "0600";
2786 else if (!g_strcmp0(type, "tablet"))
2787 sub_category = "0900";
2788 else if (!g_strcmp0(type, "watch"))
2791 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2794 static void p2p_support(GSupplicantInterface *interface)
2796 char dev_type[17] = {};
2797 const char *hostname;
2801 if (!g_supplicant_interface_has_p2p(interface))
2804 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2805 DBG("Could not register P2P technology driver");
2809 hostname = connman_utsname_get_hostname();
2811 hostname = "ConnMan";
2813 set_device_type(connman_machine_get_type(), dev_type);
2814 g_supplicant_interface_set_p2p_device_config(interface,
2815 hostname, dev_type);
2816 connman_peer_driver_register(&peer_driver);
2819 static void scan_started(GSupplicantInterface *interface)
2824 static void scan_finished(GSupplicantInterface *interface)
2826 #if defined TIZEN_EXT
2827 struct wifi_data *wifi;
2828 bool is_associating = false;
2829 static bool is_scanning = true;
2834 #if defined TIZEN_EXT
2835 wifi = g_supplicant_interface_get_data(interface);
2836 if (wifi && wifi->scan_pending_network) {
2837 network_connect(wifi->scan_pending_network);
2838 wifi->scan_pending_network = NULL;
2841 //service state - associating
2842 if(!wifi || !wifi->network)
2845 is_associating = connman_network_get_associating(wifi->network);
2846 if(is_associating && is_scanning){
2847 is_scanning = false;
2848 DBG("send scan for connecting");
2849 throw_wifi_scan(wifi->device, scan_callback);
2860 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2862 unsigned char strength;
2864 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2871 static void network_added(GSupplicantNetwork *supplicant_network)
2873 struct connman_network *network;
2874 GSupplicantInterface *interface;
2875 struct wifi_data *wifi;
2876 const char *name, *identifier, *security, *group, *mode;
2877 const unsigned char *ssid;
2878 unsigned int ssid_len;
2882 bool wps_advertizing;
2884 mode = g_supplicant_network_get_mode(supplicant_network);
2885 identifier = g_supplicant_network_get_identifier(supplicant_network);
2887 DBG("%s", identifier);
2889 if (!g_strcmp0(mode, "adhoc"))
2892 interface = g_supplicant_network_get_interface(supplicant_network);
2893 wifi = g_supplicant_interface_get_data(interface);
2894 name = g_supplicant_network_get_name(supplicant_network);
2895 security = g_supplicant_network_get_security(supplicant_network);
2896 group = g_supplicant_network_get_identifier(supplicant_network);
2897 wps = g_supplicant_network_get_wps(supplicant_network);
2898 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2899 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2900 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2901 supplicant_network);
2906 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2908 network = connman_device_get_network(wifi->device, identifier);
2911 network = connman_network_create(identifier,
2912 CONNMAN_NETWORK_TYPE_WIFI);
2916 connman_network_set_index(network, wifi->index);
2918 if (connman_device_add_network(wifi->device, network) < 0) {
2919 connman_network_unref(network);
2923 wifi->networks = g_slist_prepend(wifi->networks, network);
2926 if (name && name[0] != '\0')
2927 connman_network_set_name(network, name);
2929 connman_network_set_blob(network, "WiFi.SSID",
2931 connman_network_set_string(network, "WiFi.Security", security);
2932 connman_network_set_strength(network,
2933 calculate_strength(supplicant_network));
2934 connman_network_set_bool(network, "WiFi.WPS", wps);
2937 /* Is AP advertizing for WPS association?
2938 * If so, we decide to use WPS by default */
2939 if (wps_ready && wps_pbc &&
2941 #if !defined TIZEN_EXT
2942 connman_network_set_bool(network, "WiFi.UseWPS", true);
2944 DBG("wps is activating by ap but ignore it.");
2949 connman_network_set_frequency(network,
2950 g_supplicant_network_get_frequency(supplicant_network));
2951 #if defined TIZEN_EXT
2952 connman_network_set_bssid(network,
2953 g_supplicant_network_get_bssid(supplicant_network));
2954 connman_network_set_maxrate(network,
2955 g_supplicant_network_get_maxrate(supplicant_network));
2956 connman_network_set_enc_mode(network,
2957 g_supplicant_network_get_enc_mode(supplicant_network));
2958 connman_network_set_rsn_mode(network,
2959 g_supplicant_network_get_rsn_mode(supplicant_network));
2960 connman_network_set_keymgmt(network,
2961 g_supplicant_network_get_keymgmt(supplicant_network));
2963 connman_network_set_available(network, true);
2964 connman_network_set_string(network, "WiFi.Mode", mode);
2966 #if defined TIZEN_EXT
2971 connman_network_set_group(network, group);
2973 #if defined TIZEN_EXT
2974 if (wifi_first_scan == true)
2975 found_with_first_scan = true;
2978 if (wifi->hidden && ssid) {
2979 if (!g_strcmp0(wifi->hidden->security, security) &&
2980 wifi->hidden->ssid_len == ssid_len &&
2981 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2982 connman_network_connect_hidden(network,
2983 wifi->hidden->identity,
2984 wifi->hidden->passphrase,
2985 wifi->hidden->user_data);
2986 wifi->hidden->user_data = NULL;
2987 hidden_free(wifi->hidden);
2988 wifi->hidden = NULL;
2993 static void network_removed(GSupplicantNetwork *network)
2995 GSupplicantInterface *interface;
2996 struct wifi_data *wifi;
2997 const char *name, *identifier;
2998 struct connman_network *connman_network;
3000 interface = g_supplicant_network_get_interface(network);
3001 wifi = g_supplicant_interface_get_data(interface);
3002 identifier = g_supplicant_network_get_identifier(network);
3003 name = g_supplicant_network_get_name(network);
3005 DBG("name %s", name);
3010 connman_network = connman_device_get_network(wifi->device, identifier);
3011 if (!connman_network)
3014 #if defined TIZEN_EXT
3015 if (connman_network == wifi->scan_pending_network)
3016 wifi->scan_pending_network = NULL;
3018 if (connman_network == wifi->pending_network)
3019 wifi->pending_network = NULL;
3021 if(connman_network_get_connecting(connman_network) == true){
3022 connman_network_set_connected(connman_network, false);
3026 wifi->networks = g_slist_remove(wifi->networks, connman_network);
3028 connman_device_remove_network(wifi->device, connman_network);
3029 connman_network_unref(connman_network);
3032 static void network_changed(GSupplicantNetwork *network, const char *property)
3034 GSupplicantInterface *interface;
3035 struct wifi_data *wifi;
3036 const char *name, *identifier;
3037 struct connman_network *connman_network;
3039 #if defined TIZEN_EXT
3040 const unsigned char *bssid;
3041 unsigned int maxrate;
3046 interface = g_supplicant_network_get_interface(network);
3047 wifi = g_supplicant_interface_get_data(interface);
3048 identifier = g_supplicant_network_get_identifier(network);
3049 name = g_supplicant_network_get_name(network);
3051 DBG("name %s", name);
3056 connman_network = connman_device_get_network(wifi->device, identifier);
3057 if (!connman_network)
3060 if (g_str_equal(property, "Signal")) {
3061 connman_network_set_strength(connman_network,
3062 calculate_strength(network));
3063 connman_network_update(connman_network);
3066 #if defined TIZEN_EXT
3067 bssid = g_supplicant_network_get_bssid(network);
3068 maxrate = g_supplicant_network_get_maxrate(network);
3069 frequency = g_supplicant_network_get_frequency(network);
3070 wps = g_supplicant_network_get_wps(network);
3072 connman_network_set_bssid(connman_network, bssid);
3073 connman_network_set_maxrate(connman_network, maxrate);
3074 connman_network_set_frequency(connman_network, frequency);
3075 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
3079 static void apply_peer_services(GSupplicantPeer *peer,
3080 struct connman_peer *connman_peer)
3082 const unsigned char *data;
3087 connman_peer_reset_services(connman_peer);
3089 data = g_supplicant_peer_get_widi_ies(peer, &length);
3091 connman_peer_add_service(connman_peer,
3092 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
3096 static void add_station(const char *mac)
3098 connman_technology_tethering_add_station(CONNMAN_SERVICE_TYPE_WIFI,
3102 static void remove_station(const char *mac)
3104 connman_technology_tethering_remove_station(mac);
3107 static void peer_found(GSupplicantPeer *peer)
3109 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3110 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3111 struct connman_peer *connman_peer;
3112 const char *identifier, *name;
3115 identifier = g_supplicant_peer_get_identifier(peer);
3116 name = g_supplicant_peer_get_name(peer);
3118 DBG("ident: %s", identifier);
3120 connman_peer = connman_peer_get(wifi->device, identifier);
3124 connman_peer = connman_peer_create(identifier);
3125 connman_peer_set_name(connman_peer, name);
3126 connman_peer_set_device(connman_peer, wifi->device);
3127 apply_peer_services(peer, connman_peer);
3129 ret = connman_peer_register(connman_peer);
3130 if (ret < 0 && ret != -EALREADY)
3131 connman_peer_unref(connman_peer);
3134 static void peer_lost(GSupplicantPeer *peer)
3136 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3137 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3138 struct connman_peer *connman_peer;
3139 const char *identifier;
3144 identifier = g_supplicant_peer_get_identifier(peer);
3146 DBG("ident: %s", identifier);
3148 connman_peer = connman_peer_get(wifi->device, identifier);
3150 if (wifi->p2p_connecting &&
3151 wifi->pending_peer == connman_peer) {
3152 peer_connect_timeout(wifi);
3154 connman_peer_unregister(connman_peer);
3155 connman_peer_unref(connman_peer);
3159 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
3161 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3162 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3163 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
3164 struct connman_peer *connman_peer;
3165 const char *identifier;
3167 identifier = g_supplicant_peer_get_identifier(peer);
3169 DBG("ident: %s", identifier);
3171 connman_peer = connman_peer_get(wifi->device, identifier);
3176 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
3177 apply_peer_services(peer, connman_peer);
3178 connman_peer_services_changed(connman_peer);
3180 case G_SUPPLICANT_PEER_GROUP_CHANGED:
3181 if (!g_supplicant_peer_is_in_a_group(peer))
3182 p_state = CONNMAN_PEER_STATE_IDLE;
3184 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
3186 case G_SUPPLICANT_PEER_GROUP_STARTED:
3188 case G_SUPPLICANT_PEER_GROUP_FINISHED:
3189 p_state = CONNMAN_PEER_STATE_IDLE;
3191 case G_SUPPLICANT_PEER_GROUP_JOINED:
3192 connman_peer_set_iface_address(connman_peer,
3193 g_supplicant_peer_get_iface_address(peer));
3195 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
3196 p_state = CONNMAN_PEER_STATE_IDLE;
3198 case G_SUPPLICANT_PEER_GROUP_FAILED:
3199 if (g_supplicant_peer_has_requested_connection(peer))
3200 p_state = CONNMAN_PEER_STATE_IDLE;
3202 p_state = CONNMAN_PEER_STATE_FAILURE;
3206 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
3207 p_state == CONNMAN_PEER_STATE_FAILURE) {
3208 if (wifi->p2p_connecting
3209 && connman_peer == wifi->pending_peer)
3210 peer_cancel_timeout(wifi);
3212 p_state = CONNMAN_PEER_STATE_UNKNOWN;
3215 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
3218 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
3219 GSupplicantInterface *g_iface;
3220 struct wifi_data *g_wifi;
3222 g_iface = g_supplicant_peer_get_group_interface(peer);
3226 g_wifi = g_supplicant_interface_get_data(g_iface);
3230 connman_peer_set_as_master(connman_peer,
3231 !g_supplicant_peer_is_client(peer));
3232 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3235 connman_peer_set_state(connman_peer, p_state);
3238 static void peer_request(GSupplicantPeer *peer)
3240 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3241 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3242 struct connman_peer *connman_peer;
3243 const char *identifier;
3245 identifier = g_supplicant_peer_get_identifier(peer);
3247 DBG("ident: %s", identifier);
3249 connman_peer = connman_peer_get(wifi->device, identifier);
3253 connman_peer_request_connection(connman_peer);
3256 #if defined TIZEN_EXT
3257 static void system_power_off(void)
3260 struct wifi_data *wifi;
3261 struct connman_service *service;
3262 struct connman_ipconfig *ipconfig_ipv4;
3264 if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
3265 for (list = iface_list; list; list = list->next) {
3268 if (wifi->network != NULL) {
3269 service = connman_service_lookup_from_network(wifi->network);
3270 ipconfig_ipv4 = __connman_service_get_ip4config(service);
3271 __connman_dhcp_stop(ipconfig_ipv4);
3277 static void network_merged(GSupplicantNetwork *network)
3279 GSupplicantInterface *interface;
3280 GSupplicantState state;
3281 struct wifi_data *wifi;
3282 const char *identifier;
3283 struct connman_network *connman_network;
3284 unsigned int ishs20AP = 0;
3287 interface = g_supplicant_network_get_interface(network);
3291 state = g_supplicant_interface_get_state(interface);
3292 if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
3295 wifi = g_supplicant_interface_get_data(interface);
3299 identifier = g_supplicant_network_get_identifier(network);
3301 connman_network = connman_device_get_network(wifi->device, identifier);
3302 if (!connman_network)
3305 DBG("merged identifier %s", identifier);
3307 if (wifi->connected == FALSE) {
3309 case G_SUPPLICANT_STATE_AUTHENTICATING:
3310 case G_SUPPLICANT_STATE_ASSOCIATING:
3311 case G_SUPPLICANT_STATE_ASSOCIATED:
3312 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
3313 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
3314 connman_network_set_associating(connman_network, TRUE);
3316 case G_SUPPLICANT_STATE_COMPLETED:
3317 connman_network_set_connected(connman_network, TRUE);
3320 DBG("Not handled the state : %d", state);
3325 ishs20AP = g_supplicant_network_is_hs20AP(network);
3326 connman_network_set_is_hs20AP(connman_network, ishs20AP);
3329 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
3330 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
3331 connman_network_set_string(connman_network, "WiFi.EAP",
3333 connman_network_set_string(connman_network, "WiFi.Identity",
3334 g_supplicant_network_get_identity(network));
3335 connman_network_set_string(connman_network, "WiFi.Phase2",
3336 g_supplicant_network_get_phase2(network));
3341 wifi->network = connman_network;
3345 static void debug(const char *str)
3347 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3348 connman_debug("%s", str);
3351 static const GSupplicantCallbacks callbacks = {
3352 .system_ready = system_ready,
3353 .system_killed = system_killed,
3354 .interface_added = interface_added,
3355 .interface_state = interface_state,
3356 .interface_removed = interface_removed,
3357 .p2p_support = p2p_support,
3358 .scan_started = scan_started,
3359 .scan_finished = scan_finished,
3360 .network_added = network_added,
3361 .network_removed = network_removed,
3362 .network_changed = network_changed,
3363 .add_station = add_station,
3364 .remove_station = remove_station,
3365 .peer_found = peer_found,
3366 .peer_lost = peer_lost,
3367 .peer_changed = peer_changed,
3368 .peer_request = peer_request,
3369 #if defined TIZEN_EXT
3370 .system_power_off = system_power_off,
3371 .network_merged = network_merged,
3377 static int tech_probe(struct connman_technology *technology)
3379 wifi_technology = technology;
3384 static void tech_remove(struct connman_technology *technology)
3386 wifi_technology = NULL;
3389 struct wifi_tethering_info {
3390 struct wifi_data *wifi;
3391 struct connman_technology *technology;
3393 GSupplicantSSID *ssid;
3396 static GSupplicantSSID *ssid_ap_init(const char *ssid,
3397 const char *passphrase, bool hidden)
3399 GSupplicantSSID *ap;
3401 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3405 ap->mode = G_SUPPLICANT_MODE_MASTER;
3407 ap->ssid_len = strlen(ssid);
3411 if (!passphrase || strlen(passphrase) == 0) {
3412 ap->security = G_SUPPLICANT_SECURITY_NONE;
3413 ap->passphrase = NULL;
3415 ap->security = G_SUPPLICANT_SECURITY_PSK;
3416 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3417 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3418 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3419 ap->passphrase = passphrase;
3423 ap->ignore_broadcast_ssid =
3424 G_SUPPLICANT_AP_HIDDEN_SSID_ZERO_CONTENTS;
3426 ap->ignore_broadcast_ssid = G_SUPPLICANT_AP_NO_SSID_HIDING;
3431 static void ap_start_callback(int result, GSupplicantInterface *interface,
3434 struct wifi_tethering_info *info = user_data;
3436 DBG("result %d index %d bridge %s",
3437 result, info->wifi->index, info->wifi->bridge);
3440 connman_inet_remove_from_bridge(info->wifi->index,
3441 info->wifi->bridge);
3442 connman_technology_tethering_notify(info->technology, false);
3445 g_free(info->ifname);
3449 static void ap_create_callback(int result,
3450 GSupplicantInterface *interface,
3453 struct wifi_tethering_info *info = user_data;
3455 DBG("result %d ifname %s", result,
3456 g_supplicant_interface_get_ifname(interface));
3459 connman_inet_remove_from_bridge(info->wifi->index,
3460 info->wifi->bridge);
3461 connman_technology_tethering_notify(info->technology, false);
3463 g_free(info->ifname);
3469 info->wifi->interface = interface;
3470 g_supplicant_interface_set_data(interface, info->wifi);
3472 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3473 connman_error("Failed to set interface ap_scan property");
3475 g_supplicant_interface_connect(interface, info->ssid,
3476 ap_start_callback, info);
3479 static void sta_remove_callback(int result,
3480 GSupplicantInterface *interface,
3483 struct wifi_tethering_info *info = user_data;
3484 const char *driver = connman_option_get_string("wifi");
3486 DBG("ifname %s result %d ", info->ifname, result);
3489 info->wifi->tethering = true;
3491 g_free(info->ifname);
3497 info->wifi->interface = NULL;
3499 connman_technology_tethering_notify(info->technology, true);
3501 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3506 static int tech_set_tethering(struct connman_technology *technology,
3507 const char *identifier, const char *passphrase,
3508 const char *bridge, bool enabled, bool hidden)
3511 GSupplicantInterface *interface;
3512 struct wifi_data *wifi;
3513 struct wifi_tethering_info *info;
3521 for (list = iface_list; list; list = list->next) {
3524 if (wifi->tethering) {
3525 wifi->tethering = false;
3527 connman_inet_remove_from_bridge(wifi->index,
3529 wifi->bridged = false;
3533 connman_technology_tethering_notify(technology, false);
3538 for (list = iface_list; list; list = list->next) {
3541 interface = wifi->interface;
3546 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3548 mode = g_supplicant_interface_get_mode(interface);
3549 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3550 DBG("%s does not support AP mode", ifname);
3554 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3559 info->technology = technology;
3560 info->wifi->bridge = bridge;
3561 info->ssid = ssid_ap_init(identifier, passphrase, hidden);
3566 info->ifname = g_strdup(ifname);
3567 if (!info->ifname) {
3573 info->wifi->tethering = true;
3575 err = g_supplicant_interface_remove(interface,
3576 sta_remove_callback,
3585 static void regdom_callback(int result, const char *alpha2, void *user_data)
3589 if (!wifi_technology)
3595 connman_technology_regdom_notify(wifi_technology, alpha2);
3598 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3600 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3603 static struct connman_technology_driver tech_driver = {
3605 .type = CONNMAN_SERVICE_TYPE_WIFI,
3606 .probe = tech_probe,
3607 .remove = tech_remove,
3608 .set_tethering = tech_set_tethering,
3609 .set_regdom = tech_set_regdom,
3612 static int wifi_init(void)
3616 err = connman_network_driver_register(&network_driver);
3620 err = g_supplicant_register(&callbacks);
3622 connman_network_driver_unregister(&network_driver);
3626 err = connman_technology_driver_register(&tech_driver);
3628 g_supplicant_unregister(&callbacks);
3629 connman_network_driver_unregister(&network_driver);
3636 static void wifi_exit(void)
3640 connman_technology_driver_unregister(&tech_driver);
3642 g_supplicant_unregister(&callbacks);
3644 connman_network_driver_unregister(&network_driver);
3647 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3648 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)