example_test.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, unicode_literals
  7. import os
  8. import re
  9. import socket
  10. import sys
  11. import ttfw_idf
  12. # ----------- Config ----------
  13. PORT = 3333
  14. INTERFACE = 'eth0'
  15. # -------------------------------
  16. def udp_client(address, payload):
  17. for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC,
  18. socket.SOCK_DGRAM, 0, socket.AI_PASSIVE):
  19. family_addr, socktype, proto, canonname, addr = res
  20. try:
  21. sock = socket.socket(family_addr, socket.SOCK_DGRAM)
  22. sock.settimeout(60.0)
  23. except socket.error as msg:
  24. print('Could not create socket: ' + str(msg[0]) + ': ' + msg[1])
  25. raise
  26. try:
  27. sock.sendto(payload.encode(), addr)
  28. reply, addr = sock.recvfrom(128)
  29. if not reply:
  30. return
  31. print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + str(reply))
  32. except socket.error as msg:
  33. print('Error Code : ' + str(msg[0]) + ' Message: ' + msg[1])
  34. sock.close()
  35. raise
  36. return reply.decode()
  37. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols')
  38. def test_examples_protocol_socket_udpserver(env, extra_data):
  39. MESSAGE = 'Data to ESP'
  40. """
  41. steps:
  42. 1. join AP
  43. 2. have the board connect to the server
  44. 3. send and receive data
  45. """
  46. dut1 = env.get_dut('udp_server', 'examples/protocols/sockets/udp_server', dut_class=ttfw_idf.ESP32DUT)
  47. # check and log bin size
  48. binary_file = os.path.join(dut1.app.binary_path, 'udp_server.bin')
  49. bin_size = os.path.getsize(binary_file)
  50. ttfw_idf.log_performance('udp_server_bin_size', '{}KB'.format(bin_size // 1024))
  51. # start test
  52. dut1.start_app()
  53. ipv4 = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)[0]
  54. 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)
  55. ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0]
  56. print('Connected with IPv4={} and IPv6={}'.format(ipv4, ipv6))
  57. # test IPv4
  58. received = udp_client(ipv4, MESSAGE)
  59. if not received == MESSAGE:
  60. raise
  61. dut1.expect(MESSAGE)
  62. # test IPv6
  63. received = udp_client('{}%{}'.format(ipv6, INTERFACE), MESSAGE)
  64. if not received == MESSAGE:
  65. raise
  66. dut1.expect(MESSAGE)
  67. if __name__ == '__main__':
  68. if sys.argv[2:]: # if two arguments provided:
  69. # Usage: example_test.py <server_address> <message_to_send_to_server>
  70. udp_client(sys.argv[1], sys.argv[2])
  71. else: # otherwise run standard example test as in the CI
  72. test_examples_protocol_socket_udpserver()