OpenBK7231T_App icon indicating copy to clipboard operation
OpenBK7231T_App copied to clipboard

Faster wifi connecting (up to 2sec restart -> connected)

Open NonPIayerCharacter opened this issue 1 year ago • 2 comments

BK7231 only (N tested, don't have T). Originally just used hardcoded values for myself. After wifi first connected to ap, calculate PSK, then store it, bssid, channel and security type to LFS. After reboot, these values will be used, allowing to drastically reduce connection time (WIFI_STA_CONNECTED in 1.5-2s after force reboot and before even Info:MAIN:Time 1). Will mark as draft, feel free to adopt, as i have no desire to further fix/improve it. Currently have no idea, how it will react, if anything on ap side changed.

NonPIayerCharacter avatar Jul 27 '24 05:07 NonPIayerCharacter

@NonPIayerCharacter

I tried this PR but when I hardcoded my values the N module I use for testing froze on boot. Can you elaborate a bit more on the usage so we can test this a bit more because it has great potential?

Thanks.

DeDaMrAzR avatar Sep 30 '24 23:09 DeDaMrAzR

@DeDaMrAzR pushed an update, now it stores data to cfg, making it easier to repair (just erase cfg). In essence, on first successful connection, it calculates PSK (i was unable to get already calculated one), then saves all the required data for bk_wlan_start_sta_adv (channel, security, psk, bssid). Another problem is, that if any saved data is incorrect (like channel == 0), then no wifi event is sent, making it appear dead.

NonPIayerCharacter avatar Oct 01 '24 12:10 NonPIayerCharacter

I've been testing this on my BK7231N door sensor. It seems to have problems when switching the ssid. I have to reset the flash point to the new ssid and then it will work.

protectivedad avatar Jan 07 '25 14:01 protectivedad

I've done a bit more testing. When the SSID is changed the device takes 2+ minutes to finally decide how to connect to the wifi. It then never seems to quick connect. Or at least it takes the 'normal' amount of time to connect to the wifi. Some things I've noticed:

  1. If it is a static IP I don't think the PSK creationg routine ever gets run. This was causing a lot of issues that I couldn't figure out what was going on.
  2. There are times on a new flash (upgrade) or change (ssid) where the residual configuration will cause the advanced connection to fail silently and it will never try again. This is what is causing the hanging. The easiest way I can think of to fix this is to clear the PSK on a change to the SSID. The PSK has to always be 64 characters long you might want to trigger a reset if it isn't. That way you can catch some of the "moving" settings on firmware upgrades.
  3. There are times on a ssid change that the connection will fail over and over and will eventually find its way back after a few minutes. I think the best way to fix that is on a authentication failure don't increment the fails counter just set it to 5 so the system can reset right away.
  4. If you set the SSID it won't use the BSSID.

I made a bunch of these changes but I don't know the system well enough and I am having a very hard time with the OTA. It takes five, six even a dozen times for the firmware update to work.

protectivedad avatar Jan 08 '25 15:01 protectivedad

  • Reworked, now it requires a flag and will connect normally if device is rebooted 3-4 times (3 failed boots).
  • Implemented fast connect to Realteks.

Both on Beken (with new sdk) and Realteks it won't work if AP security mode is WPA3 or mixed WPA3/WPA2.

  • Added support for different flash sizes on RTL8720D. If 2mb - ota is disabled and lfs size is 0x10000. If more - lfs is expanded to the end of the flash (this is done for esp partition too).
  • Powersave 2 for RTL8720D
  • Fast connect data on BK is in g_cfg, on RTL - in separate easyflash env blob.
  • MQTT discovery diagnostic section now requires either flag 3 or flag 10.
  • MQTT and QuickTick timers now use rtos_delay_milliseconds, instead of vTaskDelay
  • WDT is now configured before starting wifi connection.
  • Alternative to #1533, Powersave on BK can now have 2 args, arg 1 is rf sleep, arg 2 is mcu sleep. If only 1 argument is supplied, then old behaviour is used. If you decide to merge #1533, then i will remove this.
  • Startup delay is reduced to 100ms after extended_app_waiting_for_launch for BK, and use it for new sdk too. That seems enough, failures to start tcp server on BK7238 seems to be fixed. Also, without that delay on 7238, if enhanced fast connect and static ip were used, it crashed.

+fixed some warnings

NonPIayerCharacter avatar Mar 28 '25 15:03 NonPIayerCharacter

Thanks ^^ I will have a look. I downloaded the PR yesterday, rebase it on main and correct some code. You have done more ^^

I need to check if that works cause I have a new device which need fast connection on reboot otherwise MCU is shutdown 5 s after sending some needed data.

psolyca avatar Mar 28 '25 20:03 psolyca

Checked BK7231T. It works and connects. But, for some reason, it sends two WIFI_STA_DISCONNECTED events in the process. The same happen on N, but not with new sdk. BK7231T:

Info:MAIN:Main_Init_After_Delay done
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTING - 1
ssid<edited>, 1
ht in scan
scan_start_req_handler
found scan rst rssi -73 < -50
dis ht_support
me_set_ps_disable:840 0 0 0 609301 845064
sm_auth_send:1
sm_auth_handler
ht NOT in assoc req
sm_assoc_rsp_handler
rc_init: station_id=0 format_mod=0 pre_type=0 short_gi=618029 max_bw=845575
rc_init: nss_max=0 mcs_max=0 r_idx_min=255 r_idx_max=618071 no_samples=845642
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_DISCONNECTED - 2
__l2_packet_send: ret 0
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_DISCONNECTED - 2
__l2_packet_send: ret 0
sta_mgmt_add_key
ctrl_port_hdl:1
me_set_ps_disable:840 0 0 0 609301 845064

configuring interface mlan (with DHCP client)WARN: TCPIP mutex is NOT locked (1) caller 51567

BK7231N standard:

Info:MAIN:Main_Init_After_Delay done
Debug:CMD:loglevel set 6
wpa_supplicant_scan
Cancelling scan request
wpa_driver_associate
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTING - 1
ssid:<edited>, 1
ht in scan
scan_start_req_handler
found scan rst rssi -37 > -50
found scan rst rssi -37 > -50
found scan rst rssi -34 > -50
me_set_ps_disable:840 0 0 1 0 3
sm_auth_send:1
sm_auth_handler
ht in assoc req
sm_assoc_rsp_handler
rc_init: station_id=0 format_mod=2 pre_type=0 short_gi=1 max_bw=0
rc_init: nss_max=0 mcs_max=7 r_idx_min=12 r_idx_max=12 no_samples=10
---------SM_CONNECT_IND_ok
wpa_driver_new dtim period:2

new ie: 0 : <edited> 
new ie: 1 : <edited> 
new ie: 3 : <edited> 
new ie: 30 : <edited> 
new ie: 2d : <edited> 

a get_scan_rst_null
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_DISCONNECTED - 2
RL_STATUS_STA_SCAN_VAIN
Cancelling scan request
__l2_packet_send: ret 0
get_scan_rst_null
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_DISCONNECTED - 2
RL_STATUS_STA_SCAN_VAIN
__l2_packet_send: ret 0
hapd_intf_add_key CCMP
add sta_mgmt_get_sta
sta:0, vif:0, key:0
sta_mgmt_add_key
add hw key idx:24
hapd_intf_add_key CCMP
add is_broadcast_ether_addr
sta:255, vif:0, key:1
add hw key idx:1
ctrl_port_hdl:1
me_set_ps_disable:840 0 0 0 0 6
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTED - 4
sta_ip_start

BK7231N new sdk:

Info:MAIN:Main_Init_After_Delay done
Info:HTTP:TCP server listening
Debug:CMD:loglevel set 6
wpa_driver_scan_cb
wpa rx E SCAN_RESULTS
Scan completed in 0.100000 seconds
wpa_get_scan_rst:1
cipher2security 2 2 16 16
cipher2security 2 2 16 16
wpa_supplicant_connect
Cancelling scan request
wpa S: SCANNING -> ASSOCIATING
wpa_driver_associate: auth_alg 0x1
found scan rst rssi -39 > -50 0
sta_mgmt_register: mac <edited>
sm_auth_send:1
Info:MAIN:Main_OnWiFiStatusChangsm_auth_handler
ht in assoc req
e - WIFI_STA_CONNECTING - 1sm_assoc_rsp_handler
rc_init: station_id=0 format_mod=2 pre_type=0 short_gi=1 max_bw=0
rc_init: nss_max=0 mcs_max=7 r_idx_min=12 r_idx_max=12 no_samples=10
mm_set_vif_state_req_handler
chan_bcn_detect_start
---------SM_CONNECT_IND_ok, aid 2, bssid <edited>
Not associated - Delay processing of received EAPOL frame (state=ASSOCIATING bssid=00:00:00:00:00:00 )
wpa rx E ASSOC
wpa S: ASSOCIATING -> ASSOCIATED
Cancelling scan request
wpa S: ASSOCIATED -> 4WAY_HANDSHAKE
WPA: TK <edited>

wpa S: 4WAY_HANDSHAKE -> 4WAY_HANDSHAKE
hapd_intf_add_key CCMP
add sta_mgmt_get_sta
sta:0, vif:0, key:0
sta_mgmt_add_key
add hw key idx:24
wpa S: 4WAY_HANDSHAKE -> GROUP_HANDSHAKE
WPA: GTK <edited>
hapd_intfnew dtim period:2

new ie: 0 : <edited>
new ie: 1 : <edited>
new ie: 3 : <edited> 
new ie: 30 : <edited>
new ie: 2d : <edited>

_add_key CCMP
add is_broadcast_ether_addr
sta:255, vif:0, key:1
add hw key idx:1
wpa S: GROUP_HANDSHAKE -> COMPLETED
ctrl_port_hdl:1
me_set_ps_disable:978 0 0 0 0 1
dis set ps 1!!
WLAN_EVENT_CONNECTED
sta_ip_start

configuring interface mlan (with Static IP)WARN: TCPIP mutex is NOT locked (1) caller 94F43
WARN: TCPIP mutex is NOT locked (1) caller 94F43
Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTED - 4

NonPIayerCharacter avatar Mar 29 '25 05:03 NonPIayerCharacter

I have downloaded the artifact of this PR so it's the "old" SDK. BTW, it's correctly working on my 7231N (CB3S module). Now my data have times to send MQTT values ^^ I will update my teardown https://www.elektroda.com/rtvforum/topic4111095.html

psolyca avatar Mar 29 '25 21:03 psolyca

Hoo did not report back but the 3db9887 commit is working great on my device ^^

psolyca avatar Apr 15 '25 07:04 psolyca

Perfection.

openshwprojects avatar Apr 15 '25 14:04 openshwprojects

:tada: This PR is included in version 1.18.84 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Apr 16 '25 07:04 github-actions[bot]