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_client_auto.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 sys
  15. import socket
  16. from sys import stdout
  17. from SocketServer import BaseRequestHandler, UDPServer
  18. class UDPEchoClient_Handler(BaseRequestHandler):
  19. def handle(self):
  20. """ One handle per connection
  21. """
  22. data, socket = self.request
  23. socket.sendto(data, self.client_address)
  24. if '{{end}}' in data:
  25. print
  26. print data
  27. else:
  28. sys.stdout.write('.')
  29. stdout.flush()
  30. class UDPEchoClientTest():
  31. def send_server_ip_port(self, selftest, ip_address, port_no):
  32. c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
  33. if c is None:
  34. selftest.print_result(selftest.RESULT_IO_SERIAL)
  35. return
  36. selftest.notify(c.strip())
  37. selftest.notify("HOST: Sending server IP Address to target...")
  38. connection_str = ip_address + ":" + str(port_no) + "\n"
  39. selftest.mbed.serial_write(connection_str)
  40. c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
  41. if c is None:
  42. self.print_result(selftest.RESULT_IO_SERIAL)
  43. return
  44. selftest.notify(c.strip())
  45. return selftest.RESULT_PASSIVE
  46. def test(self, selftest):
  47. # We need to discover SERVEP_IP and set up SERVER_PORT
  48. # Note: Port 7 is Echo Protocol:
  49. #
  50. # Port number rationale:
  51. #
  52. # The Echo Protocol is a service in the Internet Protocol Suite defined
  53. # in RFC 862. It was originally proposed for testing and measurement
  54. # of round-trip times[citation needed] in IP networks.
  55. #
  56. # A host may connect to a server that supports the Echo Protocol using
  57. # the Transmission Control Protocol (TCP) or the User Datagram Protocol
  58. # (UDP) on the well-known port number 7. The server sends back an
  59. # identical copy of the data it received.
  60. SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
  61. SERVER_PORT = 7
  62. # Returning none will suppress host test from printing success code
  63. server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler)
  64. print "HOST: Listening for UDP connections..."
  65. self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
  66. server.serve_forever()