]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/host_tests/udpecho_client_auto.py
Change .gitignore for ChibiOS
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / workspace_tools / host_tests / udpecho_client_auto.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17
18 import sys
19 import socket
20 from sys import stdout
21 from SocketServer import BaseRequestHandler, UDPServer
22
23 class UDPEchoClient_Handler(BaseRequestHandler):
24 def handle(self):
25 """ One handle per connection
26 """
27 data, socket = self.request
28 socket.sendto(data, self.client_address)
29 if '{{end}}' in data:
30 print
31 print data
32 else:
33 sys.stdout.write('.')
34 stdout.flush()
35
36 class UDPEchoClientTest():
37
38 def send_server_ip_port(self, selftest, ip_address, port_no):
39 c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
40 if c is None:
41 selftest.print_result(selftest.RESULT_IO_SERIAL)
42 return
43 selftest.notify(c.strip())
44
45 selftest.notify("HOST: Sending server IP Address to target...")
46 connection_str = ip_address + ":" + str(port_no) + "\n"
47 selftest.mbed.serial_write(connection_str)
48
49 c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
50 if c is None:
51 self.print_result(selftest.RESULT_IO_SERIAL)
52 return
53 selftest.notify(c.strip())
54 return selftest.RESULT_PASSIVE
55
56 def test(self, selftest):
57 # We need to discover SERVEP_IP and set up SERVER_PORT
58 # Note: Port 7 is Echo Protocol:
59 #
60 # Port number rationale:
61 #
62 # The Echo Protocol is a service in the Internet Protocol Suite defined
63 # in RFC 862. It was originally proposed for testing and measurement
64 # of round-trip times[citation needed] in IP networks.
65 #
66 # A host may connect to a server that supports the Echo Protocol using
67 # the Transmission Control Protocol (TCP) or the User Datagram Protocol
68 # (UDP) on the well-known port number 7. The server sends back an
69 # identical copy of the data it received.
70 SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
71 SERVER_PORT = 7
72
73 # Returning none will suppress host test from printing success code
74 server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler)
75 print "HOST: Listening for UDP connections..."
76 self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
77 server.serve_forever()
Imprint / Impressum