Skip to content

Commit 9581b34

Browse files
bl602 fast connect (#1868)
1 parent c31cdd9 commit 9581b34

File tree

3 files changed

+137
-33
lines changed

3 files changed

+137
-33
lines changed

src/hal/bl602/hal_wifi_bl602.c

Lines changed: 133 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "../hal_wifi.h"
44
#include "../../new_common.h"
55
#include "../../new_cfg.h"
6+
#include "../../new_pins.h"
7+
#include "../../logging/logging.h"
68
#include <string.h>
79
#include <FreeRTOS.h>
810
#include <task.h>
@@ -14,30 +16,38 @@
1416
#include <wifi_mgmr_ext.h>
1517
#include <aos/kernel.h>
1618
#include <aos/yloop.h>
19+
#include <easyflash.h>
1720

1821
// lenght of "192.168.103.103" is 15 but we also need a NULL terminating character
19-
static char g_ipStr[32] = "unknown";
22+
static char g_ipStr[16] = "unknown";
23+
static char g_gwStr[16] = "unknown";
24+
static char g_maskStr[16] = "unknown";
25+
static char g_dnsStr[16] = "unknown";
2026
static int g_bAccessPointMode = 1;
2127
static void (*g_wifiStatusCallback)(int code);
2228
extern bool g_powersave;
29+
static obkFastConnectData_t fcdata = { 0 };
30+
extern uint16_t phy_channel_to_freq(uint8_t band, int channel);
2331

2432
void HAL_ConnectToWiFi(const char *ssid, const char *psk, obkStaticIP_t *ip)
2533
{
2634
wifi_interface_t wifi_interface;
27-
if (ip->localIPAddr[0] == 0) {
28-
wifi_mgmr_sta_ip_unset();
35+
if(ip->localIPAddr[0] == 0)
36+
{
37+
wifi_mgmr_sta_ip_unset();
2938
}
30-
else {
31-
wifi_mgmr_sta_ip_set(*(int*)ip->localIPAddr, *(int*)ip->netMask, *(int*)ip->gatewayIPAddr, *(int*)ip->dnsServerIpAddr, 0);
39+
else
40+
{
41+
wifi_mgmr_sta_ip_set(*(int*)ip->localIPAddr, *(int*)ip->netMask, *(int*)ip->gatewayIPAddr, *(int*)ip->dnsServerIpAddr, 0);
3242
}
3343
if(g_powersave) wifi_mgmr_sta_ps_exit();
3444
wifi_interface = wifi_mgmr_sta_enable();
3545

3646
// sending WIFI_CONNECT_PMF_CAPABLE is crucial here, without it, wpa3 or wpa2/3 mixed mode does not work and
37-
// connection is unstable, mqtt disconnects every few minutes
38-
wifi_mgmr_sta_connect_mid(wifi_interface, ssid, psk, NULL, NULL, 0, 0, ip->localIPAddr[0] == 0 ?1:0, WIFI_CONNECT_PMF_CAPABLE);
47+
// connection is unstable, mqtt disconnects every few minutes
48+
wifi_mgmr_sta_connect_mid(wifi_interface, (char*)ssid, (char*)psk, NULL, NULL, 0, 0, ip->localIPAddr[0] == 0 ? 1 : 0, WIFI_CONNECT_PMF_CAPABLE);
3949

40-
g_bAccessPointMode = 0;
50+
g_bAccessPointMode = 0;
4151
}
4252

4353
// BL_Err_Type EF_Ctrl_Write_MAC_Address_Opt(uint8_t slot,uint8_t mac[6],uint8_t program)
@@ -56,7 +66,7 @@ int WiFI_SetMacAddress(char *mac) {
5666
}
5767
void HAL_DisconnectFromWifi()
5868
{
59-
69+
wifi_mgmr_sta_disconnect();
6070
}
6171

6272
int HAL_SetupWiFiOpenAccessPoint(const char *ssid) {
@@ -68,7 +78,7 @@ int HAL_SetupWiFiOpenAccessPoint(const char *ssid) {
6878

6979
wifi_interface = wifi_mgmr_ap_enable();
7080
/*no password when only one param*/
71-
wifi_mgmr_ap_start(wifi_interface, ssid, hidden_ssid, NULL, 1);
81+
wifi_mgmr_ap_start(wifi_interface, (char*)ssid, hidden_ssid, NULL, 1);
7282

7383
g_bAccessPointMode = 1;
7484

@@ -129,10 +139,29 @@ static void event_cb_wifi_event(input_event_t *event, void *private_data)
129139
{
130140
printf("[APP] [EVT] GOT IP %lld\r\n", aos_now_ms());
131141
printf("[SYS] Memory left is %d Bytes\r\n", xPortGetFreeHeapSize());
132-
if(g_wifiStatusCallback!=0) {
133-
g_wifiStatusCallback(WIFI_STA_CONNECTED);
134-
}
135-
if(g_powersave) wifi_mgmr_sta_ps_enter(2);
142+
if(g_wifiStatusCallback!=0) {
143+
g_wifiStatusCallback(WIFI_STA_CONNECTED);
144+
}
145+
if(g_powersave) wifi_mgmr_sta_ps_enter(2);
146+
147+
if(CFG_HasFlag(OBK_FLAG_WIFI_ENHANCED_FAST_CONNECT))
148+
{
149+
wifi_mgmr_sta_connect_ind_stat_info_t info;
150+
wifi_mgmr_sta_connect_ind_stat_get(&info);
151+
ef_get_env_blob("fcdata", &fcdata, sizeof(obkFastConnectData_t), NULL);
152+
if(strcmp((const char*)info.passphr, fcdata.pwd) != 0 ||
153+
memcmp(&info.bssid, fcdata.bssid, 6) != 0 ||
154+
info.chan_id != fcdata.channel)
155+
{
156+
ADDLOG_INFO(LOG_FEATURE_GENERAL, "Saved fast connect data differ to current one, saving...");
157+
memset(&fcdata, 0, sizeof(obkFastConnectData_t));
158+
strcpy(fcdata.pwd, (const char*)info.passphr);
159+
fcdata.channel = info.chan_id;
160+
wifi_mgmr_psk_cal(info.passphr, info.ssid, strlen(info.ssid), fcdata.psk);
161+
memcpy(&fcdata.bssid, (char*)&info.bssid, sizeof(fcdata.bssid));
162+
ef_set_env_blob("fcdata", &fcdata, sizeof(obkFastConnectData_t));
163+
}
164+
}
136165
}
137166
break;
138167
case CODE_WIFI_ON_PROV_SSID:
@@ -225,30 +254,51 @@ int HAL_GetWifiStrength() {
225254
return rssi;
226255
}
227256

228-
const char *HAL_GetMyIPString() {
229-
uint32_t ip;
230-
uint32_t gw;
231-
uint32_t mask;
257+
const char *HAL_GetMyIPString()
258+
{
259+
uint32_t ip;
260+
uint32_t gw;
261+
uint32_t mask;
232262

233-
if(g_bAccessPointMode == 1) {
234-
wifi_mgmr_ap_ip_get(&ip, &gw, &mask);
235-
} else {
236-
wifi_mgmr_sta_ip_get(&ip, &gw, &mask);
237-
}
263+
if(g_bAccessPointMode == 1)
264+
{
265+
wifi_mgmr_ap_ip_get(&ip, &gw, &mask);
266+
}
267+
else
268+
{
269+
wifi_mgmr_sta_ip_get(&ip, &gw, &mask);
270+
}
238271

239-
strcpy(g_ipStr,inet_ntoa(ip));
272+
strcpy(g_ipStr,inet_ntoa(ip));
273+
strcpy(g_gwStr, inet_ntoa(gw));
274+
strcpy(g_maskStr, inet_ntoa(mask));
240275

241-
return g_ipStr;
276+
return g_ipStr;
242277
}
243278

244-
const char* HAL_GetMyGatewayString() {
245-
return "192.168.0.1";
279+
const char* HAL_GetMyGatewayString()
280+
{
281+
return g_gwStr;
246282
}
247-
const char* HAL_GetMyDNSString() {
248-
return "192.168.0.1";
283+
const char* HAL_GetMyDNSString()
284+
{
285+
uint32_t dns1;
286+
uint32_t dns2;
287+
288+
if(g_bAccessPointMode == 1)
289+
{
290+
return "none";
291+
}
292+
else
293+
{
294+
wifi_mgmr_sta_dns_get(&dns1, &dns2);
295+
}
296+
strcpy(g_dnsStr, inet_ntoa(dns1));
297+
return g_dnsStr;
249298
}
250-
const char* HAL_GetMyMaskString() {
251-
return "255.255.255.0";
299+
const char* HAL_GetMyMaskString()
300+
{
301+
return g_maskStr;
252302
}
253303

254304
void WiFI_GetMacAddress(char *mac) {
@@ -262,8 +312,59 @@ const char *HAL_GetMACStr(char *macstr) {
262312
} else {
263313
wifi_mgmr_sta_mac_get(mac);
264314
}
265-
sprintf(macstr,"%02X%02X%02X%02X%02X%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
315+
sprintf(macstr, MACSTR, MAC2STR(mac));
266316
return macstr;
267317
}
268318

319+
void HAL_FastConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticIP_t* ip)
320+
{
321+
int len = ef_get_env_blob("fcdata", &fcdata, sizeof(obkFastConnectData_t), NULL);
322+
if(len == sizeof(obkFastConnectData_t))
323+
{
324+
ADDLOG_INFO(LOG_FEATURE_GENERAL, "We have fast connection data, connecting...");
325+
if(ip->localIPAddr[0] == 0)
326+
{
327+
wifi_mgmr_sta_ip_unset();
328+
}
329+
else
330+
{
331+
wifi_mgmr_sta_ip_set(*(int*)ip->localIPAddr, *(int*)ip->netMask, *(int*)ip->gatewayIPAddr, *(int*)ip->dnsServerIpAddr, 0);
332+
}
333+
334+
struct ap_connect_adv ext_param = { 0 };
335+
char psk[65] = { 0 };
336+
memcpy(psk, fcdata.psk, 64);
337+
printf("strlen psk = %i\r\n", strlen(psk));
338+
ext_param.psk = psk;
339+
ext_param.ap_info.type = AP_INFO_TYPE_SUGGEST;
340+
ext_param.ap_info.time_to_live = 30;
341+
ext_param.ap_info.bssid = (uint8_t*)fcdata.bssid;
342+
ext_param.ap_info.band = 0;
343+
ext_param.ap_info.freq = phy_channel_to_freq(0, fcdata.channel);
344+
ext_param.ap_info.use_dhcp = ip->localIPAddr[0] == 0 ? 1 : 0;
345+
ext_param.flags = WIFI_CONNECT_PMF_CAPABLE | WIFI_CONNECT_STOP_SCAN_ALL_CHANNEL_IF_TARGET_AP_FOUND | WIFI_CONNECT_STOP_SCAN_CURRENT_CHANNEL_IF_TARGET_AP_FOUND;
346+
347+
if(g_powersave) wifi_mgmr_sta_ps_exit();
348+
wifi_interface_t wifi_interface;
349+
wifi_interface = wifi_mgmr_sta_enable();
350+
wifi_mgmr_sta_connect_ext(wifi_interface, (char*)oob_ssid, (char*)connect_key, &ext_param);
351+
g_bAccessPointMode = 0;
352+
return;
353+
}
354+
else if(len)
355+
{
356+
ADDLOG_INFO(LOG_FEATURE_GENERAL, "Fast connect data len (%i) != saved len (%i)", sizeof(obkFastConnectData_t), len);
357+
}
358+
else
359+
{
360+
ADDLOG_INFO(LOG_FEATURE_GENERAL, "Fast connect data is empty, connecting normally");
361+
}
362+
HAL_ConnectToWiFi(oob_ssid, connect_key, ip);
363+
}
364+
365+
void HAL_DisableEnhancedFastConnect()
366+
{
367+
ef_del_env("fcdata");
368+
}
369+
269370
#endif // PLATFORM_BL602

src/hal/hal_wifi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ typedef struct
3636
#else
3737
char psk[64];
3838
#endif
39+
#if PLATFORM_BL602
40+
char pwd[64 + 1];
41+
#endif
3942
} obkFastConnectData_t;
4043

4144
int HAL_SetupWiFiOpenAccessPoint(const char* ssid);

src/httpserver/http_fns.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3071,7 +3071,7 @@ const char* g_obk_flagNames[] = {
30713071
"[PWR] Invert AC dir",
30723072
"[HTTP] Hide ON/OFF for relays (only red/green buttons)",
30733073
"[MQTT] Never add GET suffix",
3074-
"[WiFi] (RTL/BK) Enhanced fast connect by saving AP data to flash (preferable with Flag 37 & static ip). Quick reset 3 times to connect normally",
3074+
"[WiFi] (RTL/BK/BL602) Enhanced fast connect by saving AP data to flash (preferable with Flag 37 & static ip). Quick reset 3 times to connect normally",
30753075
"error",
30763076
"error",
30773077
"error",

0 commit comments

Comments
 (0)