logo elektroda
logo elektroda
X
logo elektroda

Optimal Flash Storage for Last Used SSID in SSID2 Mod

XJ_ 480 18
ADVERTISEMENT
  • #1 21312648
    XJ_
    Level 11  
    I want to enhance SSID2 mod and store last used SSID in flash, so the next reboot will try to connect to last used SSID.

    Where is the right place to store "Permanent" flash value? Or is there some other place to store values like this?
    One byte is enough - I need to store last used SSID + some flags. But 2 bytes will be better.
  • ADVERTISEMENT
  • #2 21312761
    p.kaczmarek2
    Moderator Smart Home
    If you want to store something that rarely changes, then you can try to use dummy bytes at mainConfig_t :
    https://github.com/search?q=repo%3Aopenshwpro...FOpenBK7231T_App%20mainConfig_t&type=code
    However, main config should only change when users changes something, not by itself... it's not optimized against flash wear.

    So it sounds like you rather should use flashvars:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/hal/hal_flashVars.h
    Flashvars are optimized and reduce number of flash erase operations, as you can see here:
    https://github.com/openshwprojects/OpenBK7231...ob/main/src/hal/bk7231/hal_flashVars_bk7231.c
    Quote:

    This module saves variable data to a flash region in an erase effient way.

    Design:
    variables to be small - we want as many writes between erases as possible.
    sector will be all FF (erased), and data added into it.
    reading consists of searching the first non FF byte from the end, then
    reading the preceding bytes as the variables.
    last byte of data is len (!== 0xFF!)

    Still, the less flashvars size is, the better for us... and we can't add anything to flashvars without breaking compatibility.

    So my final suggestion is simple:
    Code: C / C++
    Log in, to see the code

    MAX_RETAIN_CHANNELS is currently 12, but I can't imagine someone having to retain 12 channel states between reboot. So, maybe, you can just use the last entry of savedValues array? It should work out of the box, without any serious changes, and it will not break compatibility with older versions.

    API:
    Code: C / C++
    Log in, to see the code
    Helpful post? Buy me a coffee.
  • #3 21312930
    XJ_
    Level 11  
    p.kaczmarek2 wrote:
    I can't imagine someone having to retain 12 channel states between reboot.


    So, I do change MAX_RETAIN_CHANNELS to 11 and I'll use freed space for SSID [to avoid conflict]

       MAX_RETAIN_CHANNELS=11;   //20241121 MAX_RETAIN_CHANNELS changed from  12 to 11
    
       ...
    
       short savedValues[MAX_RETAIN_CHANNELS]; 
       // offset 26
       short ssid2flags
       // offset 28


    OK?
  • #4 21312956
    p.kaczmarek2
    Moderator Smart Home
    I am not sure. I think it's better to keep MAX_RETAIN_CHANNELS at 12 and just use last slot for your purpose as well.

    This is because, if I understand correctly, you will use last channel only if there is SSID2 defined? Well, many people don't use secondary SSID, and reducing MAX_RETAIN_CHANNELS will still take one channel from them...

    Just maybe document it somewhere, so one one gets suprised in the rare scenario when someone tries to use all 12 save channels AND SSID state save at the same time..
    Helpful post? Buy me a coffee.
  • Helpful post
    #5 21320298
    XJ_
    Level 11  
    The SSID retain modification is done and as a PR for anyone who wants to try it. Mod description:

    I have added new commands to allow preserving the last used SSID. The last used SSID is stored in the retained channel. By default, the feature is disabled.

    To enable this feature, the channel that will be used to store the last SSID must be set using the setStartupSSIDChannel command in early.bat. The recommended value is 7, values 8-12 are already used for LED brightness and so on.
    It has to be in early.bat. Autoexec.bat is processed after the wifi data is loaded.

    To disable this feature, set setStartupSSIDChannel -1

    There is also a setStartupSSID command that will force the SSID to be used on the next reboot. 0=SSID1, 1=SSID2.

    [early.bat]
    setStartupSSIDChannel 7

    https://github.com/openshwprojects/OpenBK7231T_App/pull/1440

    New Commands:
    setStartupSSIDChannel [-1 or RetainChannelIndex]
    Description: Sets retain channel number to store last used SSID, 0..MAX_RETAIN_CHANNELS-1, -1 to disable. Suggested channel number is 7 (MAXMAX_RETAIN_CHANNELS-5), because channels MAXMAX_RETAIN_CHANNELS-4 . .MAXMAX_RETAIN_CHANNELS-1 are used for LED brightness etc.

    Examples:
    - to enable SSID retain function on channel 7 - It has to be in early.bat.
      setStartupSSIDChannel 7

    - to disable SSID retain function (or just remove activation in early.bat)
      setStartupSSIDChannel -1


    setStartupSSID [0/1]
    Description: Sets startup SSID - 0=SSID1 1=SSID2 - which SSID will be used after reboot. The value is stored in flash, so it will be so until next change or until the module connects to another SSID. For this to work, setStartupSSIDChannel and SSID2 must be set.
    Examples:
    - to force use SSID1 at startup
      setStartupSSID 0

    - to force use SSID2 at startup (SSID2 must be set)
      setStartupSSID 1
  • #6 21321497
    p.kaczmarek2
    Moderator Smart Home
    It seems like a nice change. I've reviewed the PR and I can accept it on my side.

    @XJ_ , do you consider it ready to merge?

    @divadiow ,@max4elektroda ,@insmod any other things to watch for, or shall I merge it already?

    PS: Please link to this discussion topic here:
    Screenshot showing a section of code with edits in a configuration file.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #7 21321621
    XJ_
    Level 11  
    p.kaczmarek2 wrote:
    do you consider it ready to merge?

    Checked and used by me, but as usual only on BK7231N. Changing the default state doesn't actually change the standard behavior, so it's ok for merging.

    Added after 16 [minutes]:

    p.kaczmarek2 wrote:
    Please link to this discussion topic here:

    I added a description of the commands.
  • #8 21321675
    p.kaczmarek2
    Moderator Smart Home
    Helpful post? Buy me a coffee.
  • #9 21321904
    max4elektroda
    Level 20  
    p.kaczmarek2 wrote:
    any other things to watch for
    I can surely do some tests - but not before the weekend.

    But maybe you allow some feedback, to what I understud from reading here and the code.
    Goal is to allow the last active SSID used as starting SSID on next device start?
    I really think this is a great idea!

    If we can agree on making a constraint about the channel used for this feature (like "use channel 7"), I think this great feature can be simplified (regarding the usage):
    If there is no (more) need to set the retaining channel with a command, it's only to decide wether to retain or not.
    If I'm right, this could also be a simplified to just a tick box on the SSID config page.
    Or do I miss(understand) something?
  • ADVERTISEMENT
  • #10 21328184
    max4elektroda
    Level 20  
    I tried to code my idea in PR #1448.

    Basic idea is to store everything to channel 7.
    There probably is some room for improvement, but it's a working approach - I really like the idea and think there might be more users, if they don't need to generate/modify autoexec.bat or use commands ;-)

    Screenshot of a WiFi connection settings interface with options for selecting SSID and network authentication.
  • #11 21328234
    XJ_
    Level 11  
    max4elektroda wrote:
    generate/modify autoexec.bat or use commands

    I originally wanted this too, and after a discussion with @p.kaczmarek2 and considering that it is not specified anywhere which channel can be used (some use LED...) and @p.kaczmarek2 did not want to block the channel it is for, this solution came out of it.
    I didn't look at the code, wihtout early.bat, are you using channel 7 hard-coded?

    Added after 5 [minutes]:

    max4elektroda wrote:
    my idea in PR #1448.

    I truly appreciate it; excellent job!

    Added after 6 [minutes]:

    max4elektroda wrote:
    If we can agree on making a constraint about the channel used for this feature (like "use channel 7"), I think this great feature can be simplified (regarding the usage):
    If there is no (more) need to set the retaining channel with a command, it's only to decide wether to retain or not.
    If I'm right, this could also be a simplified to just a tick box on the SSID config page.

    Sorry, I did not read that first. If the channel 7 will be reserved using new flag "Enable SSID retain /Channel 7 reserved for SSID/", it will be simple then.
  • ADVERTISEMENT
  • #12 21328257
    max4elektroda
    Level 20  
    XJ_ wrote:
    channel 7 hard-coded

    Yes, for now. but it's just using a #define now - could be changed to a variable, too.

    Problem is what you also addressed - to tell the device to use the setting on startup.
  • #13 21328299
    XJ_
    Level 11  
    >>21328257
    Then maybe we'll reopen the discussion about dedicated channel 7 for SSID (and maybe some other things later, 1 channel=16bit, right?), for now we only need 3 bits. What do you think, @p.kaczmarek2 ?
  • #14 21328314
    max4elektroda
    Level 20  
    In fact, that raises the question, if/where the channel "assignment" is made and documented. With other words, can we "test" for a "free" channel?
    One idea was some kind of "magic word" in the channel - but that's not too reliable on a two byte variable.
  • #15 21328326
    XJ_
    Level 11  
    >>21328314
    max4elektroda wrote:
    can we "test" for a "free" channel

    I found channel 7 by going through the code, but I'm not actually sure if channel 7 is free. Hence the early.bat command version.
    A retail channel definition/specification should probably be created.

    Added after 38 [minutes]:

    And what about reversing it? Change the access, block channel 7 as default for SSID and allow channel 7 to be released for other things with the command setStartupSSIDChannel -1.
    You ( @max4elektroda ) would change #define back to variable and that's it.
    And possibility to change channel used for SSID will be also retained.

    Added after 8 [minutes]:

    And even better, how about thinking about it globally, if someone want to use a retain channel, he must reserve it in early.bat using command ReserveRetainChannel ...

    Added after 1 [minutes]:

    Therefore, it will be possible to verify availability before use.
  • #16 21328481
    max4elektroda
    Level 20  
    I think we need some input from @p.kaczmarek2 on what to do. There also was a general decision about the LED related channels.
    So why not do the same for SSID?
    And I could also live with the idea of undoing/changing the reservation in early.bat
  • #17 21328484
    XJ_
    Level 11  
    >>21328184
    What is the reason for using the "Magic number 42"? Aren't these 6 bits unnecessarily occupied, given the already small retain space?
  • #18 21328525
    max4elektroda
    Level 20  
    It was just an idea to make sure not to set a value if this channel was used differently. As I said, this was just a first try to code the idea. If we can reserve some bits in a defined place, it's not needed at all.
    And the answer to life, the universe and everything seemed a good choice for a small magic ;-)
  • #19 21328544
    XJ_
    Level 11  
    max4elektroda wrote:
    good choice for a small magic

    And... one Magic to rule them all ... ;-)

Topic summary

The discussion revolves around enhancing the SSID2 mod to store the last used SSID in flash memory for automatic reconnection upon reboot. Participants suggest using the `flashvars` structure for optimized flash memory usage, as it minimizes wear from frequent writes. A modification was proposed to utilize a specific channel (recommended as channel 7) for retaining the last SSID, with commands to enable or disable this feature. The implementation details include setting the channel in the `early.bat` file and ensuring compatibility with existing configurations. The conversation also touches on the need for documentation regarding channel assignments and the potential for a more user-friendly interface for managing SSID retention.
Summary generated by the language model.
ADVERTISEMENT