1/*
2 * libwebsockets - lws_netdev_wifi generic state handling
3 *
4 * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * The generic wifi netdevs follow a
25 */
26
27#include "private-lib-core.h"
28
29int
30lws_netdev_wifi_rssi_sort_compare(const lws_dll2_t *d, const lws_dll2_t *i)
31{
32	const lws_wifi_sta_t *wsd = (const lws_wifi_sta_t *)d,
33			     *wsi = (const lws_wifi_sta_t *)i;
34	return rssi_averaged(wsd) > rssi_averaged(wsi);
35}
36
37void
38lws_netdev_wifi_scan_empty(lws_netdev_instance_wifi_t *wnd)
39{
40	lws_start_foreach_dll_safe(struct lws_dll2 *, p, p1, lws_dll2_get_head(
41	                                                       &wnd->scan)) {
42		lws_wifi_sta_t *s = lws_container_of(p, lws_wifi_sta_t, list);
43
44		lws_dll2_remove(p);
45		lws_free(s);
46
47	} lws_end_foreach_dll_safe(p, p1);
48}
49
50void
51lws_netdev_wifi_scan(lws_sorted_usec_list_t *sul)
52{
53	lws_netdev_instance_wifi_t *wnd = lws_container_of(sul,
54					lws_netdev_instance_wifi_t, sul_scan);
55
56	wnd->inst.ops->scan(&wnd->inst);
57}
58
59lws_wifi_sta_t *
60lws_netdev_wifi_scan_find(lws_netdev_instance_wifi_t *wnd, const char *ssid,
61			  const uint8_t *bssid)
62{
63	lws_start_foreach_dll(struct lws_dll2 *, p, lws_dll2_get_head(
64	                                                       &wnd->scan)) {
65		lws_wifi_sta_t *w = lws_container_of(p, lws_wifi_sta_t, list);
66
67		if (!strcmp(ssid, (const char *)&w[1]) &&
68		    !memcmp(bssid, w->bssid, 6))
69			return w;
70
71	} lws_end_foreach_dll(p);
72
73	return NULL;
74}
75
76int
77lws_netdev_wifi_scan_select(lws_netdev_instance_wifi_t *wnd)
78{
79	lws_netdevs_t *netdevs = lws_netdevs_from_ndi(&wnd->inst);
80	struct lws_context *cx = lws_context_from_netdevs(netdevs);
81	uint32_t least_recent = 0xffffffff;
82	lws_wifi_creds_t *pc = NULL;
83	lws_wifi_sta_t *pw = NULL;
84
85	/*
86	 * Trim enough of the lowest RSSI guys in order to get us below the
87	 * limit we are allowed to keep track of...
88	 */
89
90	while (wnd->scan.count > LWS_WIFI_MAX_SCAN_TRACK) {
91		struct lws_dll2 *p = lws_dll2_get_tail(&wnd->scan);
92		lws_wifi_sta_t *w = lws_container_of(p, lws_wifi_sta_t, list);
93
94		lws_dll2_remove(p);
95		lws_free(w);
96	}
97
98	/*
99	 * ... let's dump what's left
100	 */
101
102	lws_start_foreach_dll(struct lws_dll2 *, p, lws_dll2_get_head(
103	                                                       &wnd->scan)) {
104		lws_wifi_sta_t *w = lws_container_of(p, lws_wifi_sta_t, list);
105
106		lwsl_notice("%s: %s, %02X:%02X:%02X:%02X:%02X:%02X, ch %d, rssi %d\n",
107			    __func__, (const char *)&w[1], w->bssid[0],
108			    w->bssid[1], w->bssid[2], w->bssid[3], w->bssid[4],
109			    w->bssid[5], w->ch, rssi_averaged(w));
110
111	} lws_end_foreach_dll(p);
112
113	/*
114	 * make sure we have our device's connection credentials at hand
115	 */
116
117	if (!netdevs->ac_creds &&
118	    lws_netdev_credentials_settings_get(netdevs))
119		return 0;
120	netdevs->refcount_creds++;
121
122	/*
123	 * Let's go through each starting from the best RSSI seeing if we
124	 * have credentials... if we do, pick the one we least-recently tried
125	 */
126
127	lws_start_foreach_dll(struct lws_dll2 *, p1, wnd->scan.head) {
128		lws_wifi_sta_t *w = lws_container_of(p1, lws_wifi_sta_t, list);
129
130		lws_start_foreach_dll(struct lws_dll2 *, q,
131				      netdevs->owner_creds.head) {
132			lws_wifi_creds_t *c = lws_container_of(q,
133							       lws_wifi_creds_t,
134							       list);
135
136			if (!strcmp((const char *)&w[1], c->ssid) &&
137			    w->last_seen < least_recent) {
138				/*
139				 * Not <= so we stick with higher RSSI when
140				 * all 0
141				 */
142				pc = c;
143				pw = w;
144				least_recent = w->last_seen;
145			}
146
147		} lws_end_foreach_dll(q);
148
149	} lws_end_foreach_dll(p1);
150
151
152	if (least_recent != 0xffffffff) {
153		/*
154		 * We picked one to try... note what we're trying so we can
155		 * record it in settings as last successful
156		 */
157		lws_strncpy(wnd->current_attempt_ssid, (const char *)&pw[1],
158			    sizeof(wnd->current_attempt_ssid));
159		memcpy(wnd->current_attempt_bssid, pw->bssid, LWS_ETH_ALEN);
160		wnd->inst.ops->connect(&wnd->inst, pc->ssid, pc->passphrase,
161					pw->bssid);
162	} else {
163		/*
164		 * We couldn't see anyone we recognized on this scan, let's
165		 * rescan in a bit
166		 */
167
168		lwsl_notice("%s: nothing usable in scan, redoing in 3s\n", __func__);
169		lws_sul_schedule(cx, 0, &wnd->sul_scan, lws_netdev_wifi_scan,
170				 3 * LWS_US_PER_SEC);
171	}
172
173	if (!--netdevs->refcount_creds) {
174		lws_dll2_owner_clear(&netdevs->owner_creds);
175		lwsac_free(&netdevs->ac_creds);
176	}
177
178	return 0;
179}
180
181/*
182 * Initially our best bet is just try to reconnect to whatever we last
183 * succeeded to connect to
184 */
185
186int
187lws_netdev_wifi_redo_last(lws_netdev_instance_wifi_t *wnd)
188{
189	lws_netdevs_t *netdevs = lws_netdevs_from_ndi(&wnd->inst);
190	uint8_t buf[256], bssid[LWS_ETH_ALEN];
191	const char *ssid, *pp = "", *pb;
192	char setname[16], ssid_copy[33];
193	size_t l = sizeof(buf), al;
194	lws_wifi_creds_t *cred;
195
196	/*
197	 * Let's try to retreive the last successful connect info for this
198	 * netdev
199	 */
200
201	lws_snprintf(setname, sizeof(setname), "netdev.last.%s", wnd->inst.name);
202	if (lws_settings_plat_get(netdevs->si, setname, buf, &l))
203		return 1;
204
205	lwsl_notice("%s: last successful %s\n", __func__, buf);
206
207	ssid = lws_json_simple_find((const char *)buf, l, "\"ssid\":", &al);
208	if (!ssid || al > 32)
209		return 1;
210
211	memcpy(ssid_copy, ssid, al);
212	ssid_copy[al + 1] = '\0';
213
214	pb = lws_json_simple_find((const char *)buf, l, "\"bssid\":", &al);
215	if (!pb)
216		return 1;
217	lws_hex_to_byte_array(pb, bssid, sizeof(bssid));
218
219	/*
220	 * make sure we have our device's connection credentials at hand
221	 */
222
223	if (!netdevs->ac_creds &&
224	    lws_netdev_credentials_settings_get(netdevs))
225		return 1;
226	netdevs->refcount_creds++;
227
228	cred = lws_netdev_credentials_find(netdevs, ssid_copy, bssid);
229	if (cred)
230		pp = cred->passphrase;
231
232	lws_strncpy(wnd->current_attempt_ssid, ssid_copy,
233		    sizeof(wnd->current_attempt_ssid));
234	memcpy(wnd->current_attempt_bssid, bssid, LWS_ETH_ALEN);
235	wnd->inst.ops->connect(&wnd->inst, ssid_copy, pp, bssid);
236
237	if (!--netdevs->refcount_creds) {
238		lws_dll2_owner_clear(&netdevs->owner_creds);
239		lwsac_free(&netdevs->ac_creds);
240	}
241
242	return 0;
243}
244