wlan.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. '''
  2. WLAN test for the CC3200 based boards.
  3. '''
  4. from network import WLAN
  5. import os
  6. import time
  7. import testconfig
  8. mch = os.uname().machine
  9. if not 'LaunchPad' in mch and not 'WiPy' in mch:
  10. raise Exception('Board not supported!')
  11. def wait_for_connection(wifi, timeout=10):
  12. while not wifi.isconnected() and timeout > 0:
  13. time.sleep(1)
  14. timeout -= 1
  15. if wifi.isconnected():
  16. print('Connected')
  17. else:
  18. print('Connection failed!')
  19. wifi = WLAN(0, WLAN.STA)
  20. print(wifi.mode() == WLAN.STA)
  21. print(wifi.antenna() == WLAN.INT_ANT)
  22. wifi = WLAN(mode=WLAN.AP)
  23. print(wifi.mode() == WLAN.AP)
  24. print(wifi.channel() == 1)
  25. print(wifi.auth() == None)
  26. print(wifi.antenna() == WLAN.INT_ANT)
  27. wifi = WLAN(0, mode=WLAN.AP, ssid='test-wlan', auth=(WLAN.WPA, '123456abc'), channel=7)
  28. print(wifi.mode() == WLAN.AP)
  29. print(wifi.channel() == 7)
  30. print(wifi.ssid() == 'test-wlan')
  31. print(wifi.auth() == (WLAN.WPA, '123456abc'))
  32. print(wifi.antenna() == WLAN.INT_ANT)
  33. wifi = WLAN(mode=WLAN.STA)
  34. print(wifi.mode() == WLAN.STA)
  35. time.sleep(5) # this ensures a full network scan
  36. scan_r = wifi.scan()
  37. print(len(scan_r) > 3)
  38. for net in scan_r:
  39. if net.ssid == testconfig.wlan_ssid:
  40. # test that the scan results contains the desired params
  41. print(len(net.bssid) == 6)
  42. print(net.channel == None)
  43. print(net.sec == testconfig.wlan_auth[0])
  44. print(net.rssi < 0)
  45. print('Network found')
  46. break
  47. wifi.mode(WLAN.STA)
  48. print(wifi.mode() == WLAN.STA)
  49. wifi.channel(7)
  50. print(wifi.channel() == 7)
  51. wifi.ssid('t-wlan')
  52. print(wifi.ssid() == 't-wlan')
  53. wifi.auth(None)
  54. print(wifi.auth() == None)
  55. wifi.auth((WLAN.WEP, '11223344556677889900'))
  56. print(wifi.auth() == (WLAN.WEP, '11223344556677889900'))
  57. wifi.antenna(WLAN.INT_ANT)
  58. print(wifi.antenna() == WLAN.INT_ANT)
  59. wifi.antenna(WLAN.EXT_ANT)
  60. print(wifi.antenna() == WLAN.EXT_ANT)
  61. time.sleep(2) # this ensures a full network scan
  62. scan_r = wifi.scan()
  63. print(len(scan_r) > 3)
  64. for net in scan_r:
  65. if net.ssid == testconfig.wlan_ssid:
  66. print('Network found')
  67. break
  68. wifi.antenna(WLAN.INT_ANT)
  69. wifi.mode(WLAN.STA)
  70. print(wifi.mode() == WLAN.STA)
  71. wifi.connect(testconfig.wlan_ssid, auth=testconfig.wlan_auth, timeout=10000)
  72. wait_for_connection(wifi)
  73. wifi.ifconfig(config='dhcp')
  74. wait_for_connection(wifi)
  75. print('0.0.0.0' not in wifi.ifconfig())
  76. wifi.ifconfig(0, ('192.168.178.109', '255.255.255.0', '192.168.178.1', '8.8.8.8'))
  77. wait_for_connection(wifi)
  78. print(wifi.ifconfig(0) == ('192.168.178.109', '255.255.255.0', '192.168.178.1', '8.8.8.8'))
  79. wait_for_connection(wifi)
  80. print(wifi.isconnected() == True)
  81. wifi.disconnect()
  82. print(wifi.isconnected() == False)
  83. t0 = time.ticks_ms()
  84. wifi.connect(testconfig.wlan_ssid, auth=testconfig.wlan_auth, timeout=0)
  85. print(time.ticks_ms() - t0 < 500)
  86. wifi.disconnect()
  87. print(wifi.isconnected() == False)
  88. # test init again
  89. wifi.init(WLAN.AP, ssid='www.wipy.io', auth=None, channel=5, antenna=WLAN.INT_ANT)
  90. print(wifi.mode() == WLAN.AP)
  91. # get the current instance without re-init
  92. wifi = WLAN()
  93. print(wifi.mode() == WLAN.AP)
  94. wifi = WLAN(0)
  95. print(wifi.mode() == WLAN.AP)
  96. # test the MAC address length
  97. print(len(wifi.mac()) == 6)
  98. # next ones MUST raise
  99. try:
  100. wifi.init(mode=12345)
  101. except:
  102. print('Exception')
  103. try:
  104. wifi.init(1, mode=WLAN.AP)
  105. except:
  106. print('Exception')
  107. try:
  108. wifi.init(mode=WLAN.AP, ssid=None)
  109. except:
  110. print('Exception')
  111. try:
  112. wifi = WLAN(mode=WLAN.AP, channel=12)
  113. except:
  114. print('Exception')
  115. try:
  116. wifi.antenna(2)
  117. except:
  118. print('Exception')
  119. try:
  120. wifi.mode(10)
  121. except:
  122. print('Exception')
  123. try:
  124. wifi.ssid('11111sdfasdfasdfasdf564sdf654asdfasdf123451245ssdgfsdf1111111111111111111111111234123412341234asdfasdf')
  125. except:
  126. print('Exception')
  127. try:
  128. wifi.auth((0))
  129. except:
  130. print('Exception')
  131. try:
  132. wifi.auth((0, None))
  133. except:
  134. print('Exception')
  135. try:
  136. wifi.auth((10, 10))
  137. except:
  138. print('Exception')
  139. try:
  140. wifi.channel(0)
  141. except:
  142. print('Exception')
  143. try:
  144. wifi.ifconfig(1, 'dhcp')
  145. except:
  146. print('Exception')
  147. try:
  148. wifi.ifconfig(config=())
  149. except:
  150. print('Exception')