From 9c72c71c05541b946feffb4d43fba6a5c513c9a7 Mon Sep 17 00:00:00 2001 From: danicampora Date: Mon, 19 Oct 2015 20:53:02 +0200 Subject: [PATCH] cc3200: WLAN class can retrieve the existing instance. --- cc3200/mods/modwlan.c | 19 +++++----- docs/library/network.rst | 70 +++++++++++++++++++++++++------------ tests/wipy/wlan/wlan.py | 10 +++++- tests/wipy/wlan/wlan.py.exp | 3 ++ 4 files changed, 69 insertions(+), 33 deletions(-) diff --git a/cc3200/mods/modwlan.c b/cc3200/mods/modwlan.c index 8db89b376a..920b0d4729 100644 --- a/cc3200/mods/modwlan.c +++ b/cc3200/mods/modwlan.c @@ -829,21 +829,22 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k mp_arg_val_t args[MP_ARRAY_SIZE(wlan_init_args)]; mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), wlan_init_args, args); - // check the peripheral id - if (args[0].u_int != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); - } - // setup the object wlan_obj_t *self = &wlan_obj; self->base.type = (mp_obj_t)&mod_network_nic_type_wlan; - // start the peripheral - wlan_init_helper(self, &args[1]); - - // pass it to the sleep module + // give it to the sleep module pyb_sleep_set_wlan_obj(self); + if (n_args > 1 || n_kw > 0) { + // check the peripheral id + if (args[0].u_int != 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); + } + // start the peripheral + wlan_init_helper(self, &args[1]); + } + return (mp_obj_t)self; } diff --git a/docs/library/network.rst b/docs/library/network.rst index 794ce70c87..c0115864d3 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -312,14 +312,22 @@ For example:: Constructors ------------ - .. class:: WLAN(..) + .. class:: WLAN(id=0, ...) Create a WLAN object, and optionally configure it. See ``init`` for params of configuration. + .. note:: + + The ``WLAN`` constructor is special in the sense that if no arguments besides the id are given, + it will return the already exisiting ``WLAN`` instance without re-configuring it. This is + because ``WLAN`` is a system feature of the WiPy. If the already existing instance is not + initialized it will do the same as the other constructors an will initialize it with default + values. + Methods ------- - .. method:: wlan.init(mode, \*, ssid, security, key, channel, antenna) + .. method:: wlan.init(mode, \*, ssid, auth, channel, antenna) Set or get the WiFi network processor configuration. @@ -329,34 +337,33 @@ For example:: - ``ssid`` is a string with the ssid name. Only needed when mode is ``WLAN.AP``. - ``auth`` is a tuple with (sec, key). Security can be ``None``, ``WLAN.WEP``, ``WLAN.WPA`` or ``WLAN.WPA2``. The key is a string with the network password. - If ``security`` is ``WLAN.WEP`` the key must be a string representing hexadecimal + If ``sec`` is ``WLAN.WEP`` the key must be a string representing hexadecimal values (e.g. 'ABC1DE45BF'). Only needed when mode is ``WLAN.AP``. - ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``. - ``antenna`` selects between the internal and the external antenna. Can be either - ``WLAN.INT_ANT`` or ``WLAN.EXTERNAL``. + ``WLAN.INT_ANT`` or ``WLAN.EXT_ANT``. For example, you can do:: # create and configure as an access point - nic.iwconfig(mode=WLAN.AP, ssid='wipy-wlan', security=WLAN.WPA2, key='www.wipy.io', channel=7, antenna=WLAN.INTERNAL) + wlan.init(mode=WLAN.AP, ssid='wipy-wlan', auth=(WLAN.WPA2,'www.wipy.io'), channel=7, antenna=WLAN.INT_ANT) or:: # configure as an station - nic.iwconfig(mode=WLAN.STA) + wlan.init(mode=WLAN.STA) - With no arguments given, the current configuration is returned as a namedtuple that looks like this: - ``(mode=2, ssid='wipy-wlan', security=2, key='www.wipy.io', channel=5, antenna=0)`` - - .. method:: wlan.connect(ssid, \*, auth=None, key=None, bssid=None, timeout=5000) + .. method:: wlan.connect(ssid, \*, auth=None, bssid=None, timeout=5000) Connect to a wifi access point using the given SSID, and other security parameters. - - ``key`` is always a string, but if ``security`` is ``WLAN.WEP`` the key must be a string - representing hexadecimal values (e.g. 'ABC1DE45BF'). - - ``bssid`` is the MAC address of the AP to connect to. Useful when there are several APs - with the same ssid. + - ``auth`` is a tuple with (sec, key). Security can be ``None``, ``WLAN.WEP``, + ``WLAN.WPA`` or ``WLAN.WPA2``. The key is a string with the network password. + If ``sec`` is ``WLAN.WEP`` the key must be a string representing hexadecimal + values (e.g. 'ABC1DE45BF'). Only needed when mode is ``WLAN.AP`` + - ``bssid`` is the MAC address of the AP to connect to. Useful when there are several + APs with the same ssid. - ``timeout`` is the maximum time in milliseconds to wait for the connection to succeed. .. method:: wlan.scan() @@ -373,16 +380,36 @@ For example:: In case of STA mode, returns ``True`` if connected to a wifi access point and has a valid IP address. In AP mode returns ``True`` when a station is connected. Returns ``False`` otherwise. - .. method:: wlan.ifconfig(if_id, config=['dhcp' or configtuple]) + .. method:: wlan.ifconfig(if_id=0, config=['dhcp' or configtuple]) - With no parameters given eturns a 4-tuple of ``(ip, subnet mask, gateway, DNS server)``. + With no parameters given eturns a 4-tuple of ``(ip, subnet_mask, gateway, DNS_server)``. if ``'dhcp'`` is passed as a parameter then the DHCP client is enabled and the IP params are negotiated with the AP. if the 4-tuple config is given then a static IP is configured. For example:: - nic.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')) + wlan.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')) + + .. method:: wlan.mode([mode]) + + Get or set the WLAN mode. + + .. method:: wlan.ssid([ssid]) + + Get or set the SSID when in AP mode. + + .. method:: wlan.auth([auth]) + + Get or set the authentication type when in AP mode. + + .. method:: wlan.channel([channel]) + + Get or set the channel (only applicable in AP mode). + + .. method:: wlan.antenna([antenna]) + + Get or set the antenna type (external or internal). .. method:: wlan.mac([mac_addr]) @@ -396,18 +423,15 @@ For example:: - ``handler`` is the function that gets called when the irq is triggered. - ``wake`` must be ``machine.SLEEP``. - Returns a callback object. + Returns a irq object. Constants --------- - + .. data:: WLAN.STA - - WiFi station mode - .. data:: WLAN.AP - WiFi access point mode + selects the WLAN mode .. data:: WLAN.WEP .. data:: WLAN.WPA diff --git a/tests/wipy/wlan/wlan.py b/tests/wipy/wlan/wlan.py index 72a20d4fa8..49e2e4af6a 100644 --- a/tests/wipy/wlan/wlan.py +++ b/tests/wipy/wlan/wlan.py @@ -22,7 +22,7 @@ def wait_for_connection(wifi, timeout=10): print('Connection failed!') -wifi = WLAN() +wifi = WLAN(0, WLAN.STA) print(wifi.mode() == WLAN.STA) print(wifi.antenna() == WLAN.INT_ANT) @@ -103,7 +103,15 @@ print(wifi.isconnected() == False) # test init again wifi.init(WLAN.AP, ssid='www.wipy.io', auth=None, channel=5, antenna=WLAN.INT_ANT) +print(wifi.mode() == WLAN.AP) +# get the current instance without re-init +wifi = WLAN() +print(wifi.mode() == WLAN.AP) +wifi = WLAN(0) +print(wifi.mode() == WLAN.AP) + +# test the MAC address length print(len(wifi.mac()) == 6) # next ones MUST raise diff --git a/tests/wipy/wlan/wlan.py.exp b/tests/wipy/wlan/wlan.py.exp index 41253eab7c..2bb3537a22 100644 --- a/tests/wipy/wlan/wlan.py.exp +++ b/tests/wipy/wlan/wlan.py.exp @@ -37,6 +37,9 @@ True True True True +True +True +True Exception Exception Exception