Skip to content

Networking: Add network constants to WLAN classes. #14015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2024

Conversation

iabdalkader
Copy link
Contributor

No description provided.

Copy link

github-actions bot commented Mar 3, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link

codecov bot commented Mar 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.39%. Comparing base (db1b5df) to head (e8dd519).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14015   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         161      161           
  Lines       21200    21200           
=======================================
  Hits        20860    20860           
  Misses        340      340           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iabdalkader iabdalkader changed the title extmod/network_cyw43: Add network security type constants. cyw43: Add network security type constants, and configure default security mode. Mar 3, 2024
@iabdalkader iabdalkader force-pushed the cyw43_network_sec branch 2 times, most recently from 59a005b to a1a45c0 Compare March 4, 2024 13:46
@dpgeorge
Copy link
Member

I just noticed that esp8266 and esp32 have these constants named AUTH_OPEN and AUTH_WPA_PSK. Furthermore, they live in the network module, not the network.WLAN class.

We'll need to make the cyw43 (and nina, esp-hosted) drivers match that.

@iabdalkader
Copy link
Contributor Author

We've had those for a few years now, and there's many scripts out there that use them, if we must use the prefix AUTH_ can we add aliases so we don't break scripts ?

    // Network is not secured.
    { MP_ROM_QSTR(MP_QSTR_OPEN),                MP_ROM_INT(NINA_SEC_OPEN) },
    // Security type WEP (40 or 104).
    { MP_ROM_QSTR(MP_QSTR_WEP),                 MP_ROM_INT(NINA_SEC_WEP) },
    // Network secured with WPA/WPA2 personal(PSK).
    { MP_ROM_QSTR(MP_QSTR_WPA_PSK),             MP_ROM_INT(NINA_SEC_WPA_PSK) },

@dpgeorge
Copy link
Member

You can keep the existing constants in the nina/esp-hosted WLAN objects, for backwards compatibility.

But adding new ones, they should go in network and be prefixed with AUTH_.

@iabdalkader
Copy link
Contributor Author

But adding new ones, they should go in network and be prefixed with AUTH_.

That would still break the networking scripts that have been using those constants, because the same scripts run on CYW43, Nina and ESP hosted.

@dpgeorge
Copy link
Member

That would still break the networking scripts that have been using those constants, because the same scripts run on CYW43, Nina and ESP hosted.

But cyw43 doesn't currently have any constants, so those scripts don't work on cyw43 at the moment.

Is it possible to update the networking scripts to use network.AUTH_xxx, and then require a firmware update to use these new scripts?

@iabdalkader
Copy link
Contributor Author

But cyw43 doesn't currently have any constants, so those scripts don't work on cyw43 at the moment.

That true, this PR is to make CYW43 compatible with the existing networking scripts by adding the missing constants. If we add different constants, existing scripts won't run and we'll need to change every other interface to match. Right now Nina, ESP hosted use those constants, and one more driver that's not in MicroPython (a WINC1500 very similar to Nina driver in structure).

Is it possible to update the networking scripts to use network.AUTH_xxx, and then require a firmware update to use these new scripts?

I will have to change 3 network interfaces and some examples but it will still break user code.

@dpgeorge
Copy link
Member

it will still break user code

How will it break user code if the old constants are left as they are?

You could also update the network scripts to try network.AUTH_xxx first, and fall back to network.WLAN.xxx.

@dpgeorge
Copy link
Member

Do the network scripts currently work on ARDUINO_NANO_ESP32?

@iabdalkader
Copy link
Contributor Author

How will it break user code if the old constants are left as they are?

The networking scripts expect those constants to be in network.WLAN.xxx so they will not run on CYW43. The goal is to have a common set of networking scripts, which we have right now.

Note if we add constants network.AUTH_xxx they will be enums, and then network interface drivers will need to be changed to map those constants to internal values, right ? It's a good idea, I like it better than duplicating the standard constants, but I'm just pointing out that it will require changes to all drivers.

Do the network scripts currently work on ARDUINO_NANO_ESP32?

No they don't but we don't support any ESP boards.

@dpgeorge
Copy link
Member

Just discussing this with @jimmo and we may actually want to go the other way: move everything out of network and into individual classes. Ie, this PR is correct (although I still think the constants need to be named AUTH_xxx).

Also then need to move things like network.PHY_xxx to network.LAN.PHY_xxx, etc.

@jimmo
Copy link
Member

jimmo commented Mar 14, 2024

The main justification for the above is the way constants work elsewhere, e.g. Pin.X rather than on machine directly. We should do the same for network.

@iabdalkader
Copy link
Contributor Author

There's one more issue with AUTH_xxx it doesn't match the keyword arg, security=AUTH_xxx should be SEC_ if we must have a prefix.

@iabdalkader
Copy link
Contributor Author

Also then need to move things like network.PHY_xxx to network.LAN.PHY_xxx, etc.

The main justification for the above is the way constants work elsewhere, e.g. Pin.X rather than on machine directly. We should do the same for network.

machine groups many modules, each one has its own constants, that aren't shared with any other module. network's constants on the other hand are common, standard constants that can be shared between all networking interfaces, and will result in less duplicate constants. I assume this change would also include networking.STA/AP_IF ? This is a very breaking change, every script assumes those are accessible from network. I don't think this is a good change, those constants should all be in network.

@robert-hh
Copy link
Contributor

I assume this change would also include networking.STA/AP_IF ? This is a very breaking change, every script assumes those are accessible from network

It happens to me regularly that I type WLAN.STA_IF, because I expect it there. Just to remember that it's network.STA_IF, which seem not to be natural. So maybe there can be a period where both variants exist.

@iabdalkader
Copy link
Contributor Author

It happens to me regularly that I type WLAN.STA_IF, because I expect it there

If all constants are moved to one module, regardless of what that module is, instead of being split between network and WLAN, there should be less confusion about this. However I still think they should be in network, because this way they can be shared, instead of having to add the same constants to every network interface. Note that the ports in question, esp32 and esp8266, already have them in network via MICROPY_PY_NETWORK_MODULE_GLOBALS_INCLUDEFILE. I guess this config option, and include files, will not be needed either.

@iabdalkader iabdalkader changed the title cyw43: Add network security type constants, and configure default security mode. Networking: Add network constants to WLAN classes. Mar 14, 2024
@dpgeorge
Copy link
Member

Note that stm32 and mimxrt already have network.LAN.PHY_xxx constants. It makes sense to have these PHY constants in the LAN class because:

  1. Other LAN drivers may have a different set of PHYs that they support, and hence different constants.
  2. Other LAN drivers may have different values for the PHY constants.

Similarly for network.WLAN, if you have multiple WLAN drivers compiled in then each one may have a different set of constants (different AUTH modes available, and they may have different values for their constants).

@iabdalkader
Copy link
Contributor Author

Similarly for network.WLAN, if you have multiple WLAN drivers compiled in then each one may have a different set of constants (different AUTH modes available, and they may have different values for their constants).

It's true some drivers will not support all modes, and constants have different values, but that's not a big problem we can use an enum and map to the actual value in drivers and fail if security mode is not supported.

I've pushed a change to move the constants to WLAN, they have to be duplicated in every driver. Note I used AUTH_ but this doesn't match the keyword arg, it should be SEC_.

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Mar 15, 2024
@dpgeorge
Copy link
Member

I think we should put the constants on the class. It keeps the constants grouped together where they belong and with the class that they are used for.

There aren't going to be that many cases where there are common constants, eg WLAN and ETH have pretty much nothing in common.

Note I used AUTH_ but this doesn't match the keyword arg, it should be SEC_.

Yes, I agree, the constants should start with SEC_. I think we should do that here.

I assume this change would also include networking.STA/AP_IF ?

Yes... I think we should also move those constants to their class. And while we are moving them, we can rename them to IF_STA and IF_AP, to have a common prefix, to match all the other groups of constants.

Of course we would retain the old constants for backwards compatibility (eg until MicroPython 2.0).

// Network secured with WPA/WPA2 personal(PSK).
{ MP_ROM_QSTR(MP_QSTR_WPA_PSK), MP_ROM_INT(NINA_SEC_WPA_PSK) },
{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA_PSK), MP_ROM_INT(NINA_SEC_WPA_PSK) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this mixed mode? SEC_WPA_WPA2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When starting the AP mode, it looks like only WEP is supported (in our driver, nina_start_ap only supports the open and WEP constants).

But the command we send to the Nina doesn't actually specify the mode. Our driver and the Arduino driver use the same command (0x19, which they call SET_AP_PASSPHRASE_CMD and document as WPA, and we call NINA_CMD_START_AP_WEP).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our driver and the Arduino driver use the same command

No we use different commands: https://github.com/micropython/micropython/blob/master/drivers/ninaw10/nina_wifi_drv.c#L402
It's seems that WPA is also supported in AP mode, in the firmware, but there's no command to actually connect use it. Maybe I will push a firmware update later to fix it.

Is this mixed mode? SEC_WPA_WPA2.

@dpgeorge The security mode is not set in station mode, it seems to connect to all modes, except for deprecated/unsecure ones like WEP.

@iabdalkader
Copy link
Contributor Author

Of course we would retain the old constants for backwards compatibility (eg until MicroPython 2.0).

Does this also mean that we should keep both AUTH_ and SEC_ ?

@iabdalkader iabdalkader force-pushed the cyw43_network_sec branch 3 times, most recently from b2fe570 to fb7a597 Compare March 21, 2024 15:12
@iabdalkader
Copy link
Contributor Author

Updated. I've kept the existing constants to maintain backwards compatibility. As for esp ports, the constants in network.XXX are the same, included via the config file, so it should still be backwards compatible.

The default CYW43 WiFi AP settings were missing the security mode, leaving
the AP in open mode by default.  That's changed by this commit to use
WPA/WPA2 by default.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Other constants such as `machine.Pin.OUT` are defined on the class that
uses them, rather than at the module level.  This commit makes that the
case for WLAN network interfaces, adding IF_xxx and SEC_xxx constants.

The SEC_xxx constants are named as such to match the `security` keyword
that they are used with.  And the IF_xxx constants have IF as a prefix so
they are grouped together as names.

This scheme of putting constants on the class means that only the available
features (eg security configurations) are made available as constants.  It
also means that different network interfaces (eg WLAN vs LAN) can keep
their own specific constants within their class, such as PHY_xxx for LAN.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Following the same change to extmod network interfaces.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Following the same change to extmod network interfaces.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
@dpgeorge dpgeorge force-pushed the cyw43_network_sec branch from 08a6b3e to e8dd519 Compare March 28, 2024 02:02
@dpgeorge dpgeorge merged commit e8dd519 into micropython:master Mar 28, 2024
61 checks passed
@iabdalkader iabdalkader deleted the cyw43_network_sec branch March 28, 2024 07:17
@mattytrentini
Copy link
Contributor

Documentation, folks! As far as I can tell these are not documented anywhere.

I'm happy to add documentation but it's not clear to me what is port-specific and what is general...

This is a real example: I was trying to create a simple, secured AP on ESP32 recently and couldn't figure out the appropriate configuration. Old forum posts with now-obsolete items make things worse - so I think we need to add a little more to the docs.

@iabdalkader
Copy link
Contributor Author

I'm happy to add documentation but it's not clear to me what is port-specific and what is general...

When ports register network interfaces they either uses network.LAN or network.WAN, with the exception of things like WIZNET5K which uses its own name. So I think the constants should be documented here https://docs.micropython.org/en/latest/library/network.WLAN.html with notes that some modes might not be supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants