]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/workspace_tools/host_tests/udpecho_server_auto.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / workspace_tools / host_tests / udpecho_server_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 re
19 import sys
20 import uuid
21 from sys import stdout
22 from socket import socket, AF_INET, SOCK_DGRAM
23
24 class UDPEchoServerTest():
25 ECHO_SERVER_ADDRESS = ""
26 ECHO_PORT = 0
27 s = None # Socket
28
29 PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
30 re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
31
32 def test(self, selftest):
33 result = True
34 serial_ip_msg = selftest.mbed.serial_readline()
35 if serial_ip_msg is None:
36 return selftest.RESULT_IO_SERIAL
37 selftest.notify(serial_ip_msg)
38 # Searching for IP address and port prompted by server
39 m = self.re_detect_server_ip.search(serial_ip_msg)
40 if m and len(m.groups()):
41 self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
42 self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
43 selftest.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT))
44
45 # We assume this test fails so can't send 'error' message to server
46 try:
47 self.s = socket(AF_INET, SOCK_DGRAM)
48 except Exception, e:
49 self.s = None
50 selftest.notify("HOST: Socket error: %s"% e)
51 return selftest.RESULT_ERROR
52
53 for i in range(0, 100):
54 TEST_STRING = str(uuid.uuid4())
55 self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
56 data = self.s.recv(len(TEST_STRING))
57 received_str = repr(data)[1:-1]
58 if TEST_STRING != received_str:
59 result = False
60 break
61 sys.stdout.write('.')
62 stdout.flush()
63 else:
64 result = False
65
66 if self.s is not None:
67 self.s.close()
68 return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE
Imprint / Impressum