]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/workspace_tools/host_tests/tcpecho_client_auto.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / workspace_tools / host_tests / tcpecho_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, TCPServer
22
23 class TCPEchoClient_Handler(BaseRequestHandler):
24 def handle(self):
25 """ One handle per connection
26 """
27 print "HOST: Connection received...",
28 count = 1;
29 while True:
30 data = self.request.recv(1024)
31 if not data: break
32 self.request.sendall(data)
33 if '{{end}}' in str(data):
34 print
35 print str(data)
36 else:
37 if not count % 10:
38 sys.stdout.write('.')
39 count += 1
40 stdout.flush()
41
42 class TCPEchoClientTest():
43 def send_server_ip_port(self, selftest, ip_address, port_no):
44 """ Set up network host. Reset target and and send server IP via serial to Mbed
45 """
46 c = selftest.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...'
47 if c is None:
48 self.print_result(selftest.RESULT_IO_SERIAL)
49 return
50
51 selftest.notify(c.strip())
52 selftest.notify("HOST: Sending server IP Address to target...")
53
54 connection_str = ip_address + ":" + str(port_no) + "\n"
55 selftest.mbed.serial_write(connection_str)
56 selftest.notify(connection_str)
57
58 # Two more strings about connection should be sent by MBED
59 for i in range(0, 2):
60 c = selftest.mbed.serial_readline()
61 if c is None:
62 selftest.print_result(self.RESULT_IO_SERIAL)
63 return
64 selftest.notify(c.strip())
65
66 def test(self, selftest):
67 # We need to discover SERVEP_IP and set up SERVER_PORT
68 # Note: Port 7 is Echo Protocol:
69 #
70 # Port number rationale:
71 #
72 # The Echo Protocol is a service in the Internet Protocol Suite defined
73 # in RFC 862. It was originally proposed for testing and measurement
74 # of round-trip times[citation needed] in IP networks.
75 #
76 # A host may connect to a server that supports the Echo Protocol using
77 # the Transmission Control Protocol (TCP) or the User Datagram Protocol
78 # (UDP) on the well-known port number 7. The server sends back an
79 # identical copy of the data it received.
80 SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
81 SERVER_PORT = 7
82
83 # Returning none will suppress host test from printing success code
84 server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
85 print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT)
86 self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
87 server.serve_forever()
Imprint / Impressum