Skip to content

Commit 9c72c71

Browse files
author
danicampora
committed
cc3200: WLAN class can retrieve the existing instance.
1 parent f4c50f1 commit 9c72c71

4 files changed

Lines changed: 69 additions & 33 deletions

File tree

cc3200/mods/modwlan.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
829829
mp_arg_val_t args[MP_ARRAY_SIZE(wlan_init_args)];
830830
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), wlan_init_args, args);
831831

832-
// check the peripheral id
833-
if (args[0].u_int != 0) {
834-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
835-
}
836-
837832
// setup the object
838833
wlan_obj_t *self = &wlan_obj;
839834
self->base.type = (mp_obj_t)&mod_network_nic_type_wlan;
840835

841-
// start the peripheral
842-
wlan_init_helper(self, &args[1]);
843-
844-
// pass it to the sleep module
836+
// give it to the sleep module
845837
pyb_sleep_set_wlan_obj(self);
846838

839+
if (n_args > 1 || n_kw > 0) {
840+
// check the peripheral id
841+
if (args[0].u_int != 0) {
842+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
843+
}
844+
// start the peripheral
845+
wlan_init_helper(self, &args[1]);
846+
}
847+
847848
return (mp_obj_t)self;
848849
}
849850

docs/library/network.rst

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,22 @@ For example::
312312
Constructors
313313
------------
314314

315-
.. class:: WLAN(..)
315+
.. class:: WLAN(id=0, ...)
316316

317317
Create a WLAN object, and optionally configure it. See ``init`` for params of configuration.
318318

319+
.. note::
320+
321+
The ``WLAN`` constructor is special in the sense that if no arguments besides the id are given,
322+
it will return the already exisiting ``WLAN`` instance without re-configuring it. This is
323+
because ``WLAN`` is a system feature of the WiPy. If the already existing instance is not
324+
initialized it will do the same as the other constructors an will initialize it with default
325+
values.
326+
319327
Methods
320328
-------
321329

322-
.. method:: wlan.init(mode, \*, ssid, security, key, channel, antenna)
330+
.. method:: wlan.init(mode, \*, ssid, auth, channel, antenna)
323331

324332
Set or get the WiFi network processor configuration.
325333

@@ -329,34 +337,33 @@ For example::
329337
- ``ssid`` is a string with the ssid name. Only needed when mode is ``WLAN.AP``.
330338
- ``auth`` is a tuple with (sec, key). Security can be ``None``, ``WLAN.WEP``,
331339
``WLAN.WPA`` or ``WLAN.WPA2``. The key is a string with the network password.
332-
If ``security`` is ``WLAN.WEP`` the key must be a string representing hexadecimal
340+
If ``sec`` is ``WLAN.WEP`` the key must be a string representing hexadecimal
333341
values (e.g. 'ABC1DE45BF'). Only needed when mode is ``WLAN.AP``.
334342
- ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``.
335343
- ``antenna`` selects between the internal and the external antenna. Can be either
336-
``WLAN.INT_ANT`` or ``WLAN.EXTERNAL``.
344+
``WLAN.INT_ANT`` or ``WLAN.EXT_ANT``.
337345

338346
For example, you can do::
339347

340348
# create and configure as an access point
341-
nic.iwconfig(mode=WLAN.AP, ssid='wipy-wlan', security=WLAN.WPA2, key='www.wipy.io', channel=7, antenna=WLAN.INTERNAL)
349+
wlan.init(mode=WLAN.AP, ssid='wipy-wlan', auth=(WLAN.WPA2,'www.wipy.io'), channel=7, antenna=WLAN.INT_ANT)
342350

343351
or::
344352

345353
# configure as an station
346-
nic.iwconfig(mode=WLAN.STA)
347-
348-
With no arguments given, the current configuration is returned as a namedtuple that looks like this:
349-
``(mode=2, ssid='wipy-wlan', security=2, key='www.wipy.io', channel=5, antenna=0)``
354+
wlan.init(mode=WLAN.STA)
350355

351-
.. method:: wlan.connect(ssid, \*, auth=None, key=None, bssid=None, timeout=5000)
356+
.. method:: wlan.connect(ssid, \*, auth=None, bssid=None, timeout=5000)
352357

353358
Connect to a wifi access point using the given SSID, and other security
354359
parameters.
355360

356-
- ``key`` is always a string, but if ``security`` is ``WLAN.WEP`` the key must be a string
357-
representing hexadecimal values (e.g. 'ABC1DE45BF').
358-
- ``bssid`` is the MAC address of the AP to connect to. Useful when there are several APs
359-
with the same ssid.
361+
- ``auth`` is a tuple with (sec, key). Security can be ``None``, ``WLAN.WEP``,
362+
``WLAN.WPA`` or ``WLAN.WPA2``. The key is a string with the network password.
363+
If ``sec`` is ``WLAN.WEP`` the key must be a string representing hexadecimal
364+
values (e.g. 'ABC1DE45BF'). Only needed when mode is ``WLAN.AP``
365+
- ``bssid`` is the MAC address of the AP to connect to. Useful when there are several
366+
APs with the same ssid.
360367
- ``timeout`` is the maximum time in milliseconds to wait for the connection to succeed.
361368

362369
.. method:: wlan.scan()
@@ -373,16 +380,36 @@ For example::
373380
In case of STA mode, returns ``True`` if connected to a wifi access point and has a valid IP address.
374381
In AP mode returns ``True`` when a station is connected. Returns ``False`` otherwise.
375382

376-
.. method:: wlan.ifconfig(if_id, config=['dhcp' or configtuple])
383+
.. method:: wlan.ifconfig(if_id=0, config=['dhcp' or configtuple])
377384

378-
With no parameters given eturns a 4-tuple of ``(ip, subnet mask, gateway, DNS server)``.
385+
With no parameters given eturns a 4-tuple of ``(ip, subnet_mask, gateway, DNS_server)``.
379386

380387
if ``'dhcp'`` is passed as a parameter then the DHCP client is enabled and the IP params
381388
are negotiated with the AP.
382389

383390
if the 4-tuple config is given then a static IP is configured. For example::
384391

385-
nic.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
392+
wlan.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
393+
394+
.. method:: wlan.mode([mode])
395+
396+
Get or set the WLAN mode.
397+
398+
.. method:: wlan.ssid([ssid])
399+
400+
Get or set the SSID when in AP mode.
401+
402+
.. method:: wlan.auth([auth])
403+
404+
Get or set the authentication type when in AP mode.
405+
406+
.. method:: wlan.channel([channel])
407+
408+
Get or set the channel (only applicable in AP mode).
409+
410+
.. method:: wlan.antenna([antenna])
411+
412+
Get or set the antenna type (external or internal).
386413

387414
.. method:: wlan.mac([mac_addr])
388415

@@ -396,18 +423,15 @@ For example::
396423
- ``handler`` is the function that gets called when the irq is triggered.
397424
- ``wake`` must be ``machine.SLEEP``.
398425

399-
Returns a callback object.
426+
Returns a irq object.
400427

401428
Constants
402429
---------
403-
404-
.. data:: WLAN.STA
405-
406-
WiFi station mode
407430

431+
.. data:: WLAN.STA
408432
.. data:: WLAN.AP
409433

410-
WiFi access point mode
434+
selects the WLAN mode
411435

412436
.. data:: WLAN.WEP
413437
.. data:: WLAN.WPA

tests/wipy/wlan/wlan.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def wait_for_connection(wifi, timeout=10):
2222
print('Connection failed!')
2323

2424

25-
wifi = WLAN()
25+
wifi = WLAN(0, WLAN.STA)
2626
print(wifi.mode() == WLAN.STA)
2727
print(wifi.antenna() == WLAN.INT_ANT)
2828

@@ -103,7 +103,15 @@ def wait_for_connection(wifi, timeout=10):
103103

104104
# test init again
105105
wifi.init(WLAN.AP, ssid='www.wipy.io', auth=None, channel=5, antenna=WLAN.INT_ANT)
106+
print(wifi.mode() == WLAN.AP)
107+
108+
# get the current instance without re-init
109+
wifi = WLAN()
110+
print(wifi.mode() == WLAN.AP)
111+
wifi = WLAN(0)
112+
print(wifi.mode() == WLAN.AP)
106113

114+
# test the MAC address length
107115
print(len(wifi.mac()) == 6)
108116

109117
# next ones MUST raise

tests/wipy/wlan/wlan.py.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ True
3737
True
3838
True
3939
True
40+
True
41+
True
42+
True
4043
Exception
4144
Exception
4245
Exception

0 commit comments

Comments
 (0)