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.

tcpecho_server_auto.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import socket
  18. from sys import stdout
  19. class TCPEchoServerTest():
  20. ECHO_SERVER_ADDRESS = ""
  21. ECHO_PORT = 0
  22. ECHO_LOOPs = 100
  23. s = None # Socket
  24. PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
  25. re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
  26. def test(self, selftest):
  27. result = False
  28. c = selftest.mbed.serial_readline()
  29. if c is None:
  30. return selftest.RESULT_IO_SERIAL
  31. selftest.notify(c)
  32. m = self.re_detect_server_ip.search(c)
  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: TCP 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.socket(socket.AF_INET, socket.SOCK_STREAM)
  40. self.s.connect((self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
  41. except Exception, e:
  42. self.s = None
  43. selftest.notify("HOST: Socket error: %s"% e)
  44. return selftest.RESULT_ERROR
  45. print 'HOST: Sending %d echo strings...'% self.ECHO_LOOPs,
  46. for i in range(0, self.ECHO_LOOPs):
  47. TEST_STRING = str(uuid.uuid4())
  48. try:
  49. self.s.sendall(TEST_STRING)
  50. data = self.s.recv(128)
  51. except Exception, e:
  52. self.s = None
  53. selftest.notify("HOST: Socket error: %s"% e)
  54. return selftest.RESULT_ERROR
  55. received_str = repr(data)[1:-1]
  56. if TEST_STRING == received_str: # We need to cut not needed single quotes from the string
  57. sys.stdout.write('.')
  58. stdout.flush()
  59. result = True
  60. else:
  61. print "Expected: "
  62. print "'%s'"% TEST_STRING
  63. print "received: "
  64. print "'%s'"% received_str
  65. result = False
  66. break
  67. if self.s is not None:
  68. self.s.close()
  69. else:
  70. selftest.notify("HOST: TCP Server not found")
  71. result = False
  72. return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE