Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

udpecho_server_auto.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. mbed SDK
  3. Copyright (c) 2011-2013 ARM Limited
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. import re
  15. import sys
  16. import uuid
  17. from sys import stdout
  18. from socket import socket, AF_INET, SOCK_DGRAM
  19. class UDPEchoServerTest():
  20. ECHO_SERVER_ADDRESS = ""
  21. ECHO_PORT = 0
  22. s = None # Socket
  23. PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
  24. re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
  25. def test(self, selftest):
  26. result = True
  27. serial_ip_msg = selftest.mbed.serial_readline()
  28. if serial_ip_msg is None:
  29. return selftest.RESULT_IO_SERIAL
  30. selftest.notify(serial_ip_msg)
  31. # Searching for IP address and port prompted by server
  32. m = self.re_detect_server_ip.search(serial_ip_msg)
  33. if m and len(m.groups()):
  34. self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
  35. self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
  36. selftest.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT))
  37. # We assume this test fails so can't send 'error' message to server
  38. try:
  39. self.s = socket(AF_INET, SOCK_DGRAM)
  40. except Exception, e:
  41. self.s = None
  42. selftest.notify("HOST: Socket error: %s"% e)
  43. return selftest.RESULT_ERROR
  44. for i in range(0, 100):
  45. TEST_STRING = str(uuid.uuid4())
  46. self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
  47. data = self.s.recv(len(TEST_STRING))
  48. received_str = repr(data)[1:-1]
  49. if TEST_STRING != received_str:
  50. result = False
  51. break
  52. sys.stdout.write('.')
  53. stdout.flush()
  54. else:
  55. result = False
  56. if self.s is not None:
  57. self.s.close()
  58. return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE