example_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # This example code is in the Public Domain (or CC0 licensed, at your option.)
  2. # Unless required by applicable law or agreed to in writing, this
  3. # software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  4. # CONDITIONS OF ANY KIND, either express or implied.
  5. # -*- coding: utf-8 -*-
  6. from __future__ import print_function
  7. from __future__ import unicode_literals
  8. import os
  9. import sys
  10. import re
  11. import socket
  12. import ttfw_idf
  13. # ----------- Config ----------
  14. PORT = 3333
  15. INTERFACE = 'eth0'
  16. # -------------------------------
  17. def udp_client(address, payload):
  18. for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC,
  19. socket.SOCK_DGRAM, 0, socket.AI_PASSIVE):
  20. family_addr, socktype, proto, canonname, addr = res
  21. try:
  22. sock = socket.socket(family_addr, socket.SOCK_DGRAM)
  23. sock.settimeout(20.0)
  24. except socket.error as msg:
  25. print('Could not create socket')
  26. print(os.strerror(msg.errno))
  27. raise
  28. try:
  29. sock.sendto(payload.encode(), addr)
  30. reply, addr = sock.recvfrom(128)
  31. if not reply:
  32. return
  33. print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + str(reply))
  34. except socket.timeout:
  35. print('Socket operation timeout')
  36. return str(None)
  37. except socket.error as msg:
  38. print('Error while sending or receiving data from the socket')
  39. print(os.strerror(msg.errno))
  40. sock.close()
  41. raise
  42. return reply.decode()
  43. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  44. def test_examples_protocol_socket_udpserver(env, extra_data):
  45. MESSAGE = "Data to ESP"
  46. MAX_RETRIES = 3
  47. """
  48. steps:
  49. 1. join AP
  50. 2. have the board connect to the server
  51. 3. send and receive data
  52. """
  53. dut1 = env.get_dut("udp_server", "examples/protocols/sockets/udp_server", dut_class=ttfw_idf.ESP32DUT)
  54. # check and log bin size
  55. binary_file = os.path.join(dut1.app.binary_path, "udp_server.bin")
  56. bin_size = os.path.getsize(binary_file)
  57. ttfw_idf.log_performance("udp_server_bin_size", "{}KB".format(bin_size // 1024))
  58. ttfw_idf.check_performance("udp_server_bin_size", bin_size // 1024, dut1.TARGET)
  59. # start test
  60. dut1.start_app()
  61. ipv4 = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)[0]
  62. ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form)
  63. ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0]
  64. print("Connected with IPv4={} and IPv6={}".format(ipv4, ipv6))
  65. dut1.expect(re.compile(r'Waiting for data'), timeout=10)
  66. # test IPv4
  67. for _ in range(MAX_RETRIES):
  68. print('Testing UDP on IPv4...')
  69. received = udp_client(ipv4, MESSAGE)
  70. if received == MESSAGE:
  71. print('OK')
  72. break
  73. else:
  74. raise ValueError('IPv4: Did not receive UDP message after {} retries'.format(MAX_RETRIES))
  75. dut1.expect(MESSAGE)
  76. # test IPv6
  77. for _ in range(MAX_RETRIES):
  78. print('Testing UDP on IPv6...')
  79. received = udp_client('{}%{}'.format(ipv6, INTERFACE), MESSAGE)
  80. if received == MESSAGE:
  81. print('OK')
  82. break
  83. else:
  84. raise ValueError('IPv6: Did not receive UDP message after {} retries'.format(MAX_RETRIES))
  85. dut1.expect(MESSAGE)
  86. if __name__ == '__main__':
  87. if sys.argv[2:]: # if two arguments provided:
  88. # Usage: example_test.py <server_address> <message_to_send_to_server>
  89. udp_client(sys.argv[1], sys.argv[2])
  90. else: # otherwise run standard example test as in the CI
  91. test_examples_protocol_socket_udpserver()