Description
We need a standard way of using internet modules on the pyboard that is powerful, extensible and easy to use. Powerful means being able to attach an ethernet and wifi module at the same time (and possibly multiple ones of these); extensible means we should be able to add new drivers without changing existing ones; easy to use means it should behave as expected for someone familiar with the Python socket module. Note that higher level abstractions (like http servers) can be built upon a proper functioning socket interface.
Let's first briefly review how things are done in Python and on a POSIX system. Python has the socket module which allows you to make socket objects, and do things like gethostbyname:
import socket
ip = socket.gethostbyname('micropython.org')
s = socket.socket(socket.AF_INET)
This is just a wrapper for the underlying POSIX calls.
The above calls don't know anything about the underlying internet device (be it eth0 or wlan0 for example). That, and the configuration of an IP address, is left up to the operating system.
On the pyboard, uPy is the operating system, and so we need a way (in Python) to initialise and configure the eth/wlan drivers, and also a way to select which one we want to use for, eg, a gethostbyname call.
A scenario might be: I have a pyboard with a CC3000 (wifi) and a WIZnet (eth) module connected. I need to initialise them on the correct pins, then perhaps run DHCP on the CC3000, but set a static IP for the WIZnet. I want the CC3000 to be a server and the WIZ to be a client, and I need to open sockets on both.
My proposed solution is to have a "meta socket" module which contains a class for each driver that is available. To initialise a driver, you make a new instance of the associated class. The instance object then behaves like a CPython socket module, and calling socket on this class makes a socket associated with that driver. Each driver has its own gethostbytname, etc.
Initialised drivers also register themselves with the "meta socket" module, so, for example, you can get a list of all initialised internet drivers (something like ifconfig on linux).
I thought about splitting into eth and wlan modules, but I think it makes sense to put all drivers into one module, since the application shouldn't care if a connection is wired or wireless.
Adding a new driver would be as simple as writing a new class and putting it into the "meta socket" module.
I propose calling the "meta socket" module network
. An example use would be:
import network
cc3k = network.CC3k(pyb.SPI(1), pyb.Pin.board.Y5, pyb.Pin.borad.Y6)
cc3k.init('myssid', key='secret')
# cc3k now acts like a socket module
cc3k.gethostbyname('micropython.org')
s = cc3k.socket(cc3k.AF_INET, cc3k.SOCK_STREAM)
wiz = network.WIZnet5k(...)
wiz.init(...)
# wiz also acts like a socket module
network.iflist() # will return [('wlan0', cc3k), ('eth0', wiz)]
Comments, thoughts, suggestions?