Clone of official tools

Committer:
screamer
Date:
Thu May 19 19:44:41 2016 +0100
Revision:
0:66f3b5499f7f
Child:
43:2a7da56ebd24
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:66f3b5499f7f 1 """
screamer 0:66f3b5499f7f 2 mbed SDK
screamer 0:66f3b5499f7f 3 Copyright (c) 2011-2013 ARM Limited
screamer 0:66f3b5499f7f 4
screamer 0:66f3b5499f7f 5 Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:66f3b5499f7f 6 you may not use this file except in compliance with the License.
screamer 0:66f3b5499f7f 7 You may obtain a copy of the License at
screamer 0:66f3b5499f7f 8
screamer 0:66f3b5499f7f 9 http://www.apache.org/licenses/LICENSE-2.0
screamer 0:66f3b5499f7f 10
screamer 0:66f3b5499f7f 11 Unless required by applicable law or agreed to in writing, software
screamer 0:66f3b5499f7f 12 distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:66f3b5499f7f 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:66f3b5499f7f 14 See the License for the specific language governing permissions and
screamer 0:66f3b5499f7f 15 limitations under the License.
screamer 0:66f3b5499f7f 16 """
screamer 0:66f3b5499f7f 17
screamer 0:66f3b5499f7f 18 import re
screamer 0:66f3b5499f7f 19 import sys
screamer 0:66f3b5499f7f 20 import uuid
screamer 0:66f3b5499f7f 21 from sys import stdout
screamer 0:66f3b5499f7f 22 from socket import socket, AF_INET, SOCK_DGRAM
screamer 0:66f3b5499f7f 23
screamer 0:66f3b5499f7f 24 class UDPEchoServerTest():
screamer 0:66f3b5499f7f 25 ECHO_SERVER_ADDRESS = ""
screamer 0:66f3b5499f7f 26 ECHO_PORT = 0
screamer 0:66f3b5499f7f 27 s = None # Socket
screamer 0:66f3b5499f7f 28
screamer 0:66f3b5499f7f 29 PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
screamer 0:66f3b5499f7f 30 re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
screamer 0:66f3b5499f7f 31
screamer 0:66f3b5499f7f 32 def test(self, selftest):
screamer 0:66f3b5499f7f 33 result = True
screamer 0:66f3b5499f7f 34 serial_ip_msg = selftest.mbed.serial_readline()
screamer 0:66f3b5499f7f 35 if serial_ip_msg is None:
screamer 0:66f3b5499f7f 36 return selftest.RESULT_IO_SERIAL
screamer 0:66f3b5499f7f 37 selftest.notify(serial_ip_msg)
screamer 0:66f3b5499f7f 38 # Searching for IP address and port prompted by server
screamer 0:66f3b5499f7f 39 m = self.re_detect_server_ip.search(serial_ip_msg)
screamer 0:66f3b5499f7f 40 if m and len(m.groups()):
screamer 0:66f3b5499f7f 41 self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
screamer 0:66f3b5499f7f 42 self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
screamer 0:66f3b5499f7f 43 selftest.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT))
screamer 0:66f3b5499f7f 44
screamer 0:66f3b5499f7f 45 # We assume this test fails so can't send 'error' message to server
screamer 0:66f3b5499f7f 46 try:
screamer 0:66f3b5499f7f 47 self.s = socket(AF_INET, SOCK_DGRAM)
screamer 0:66f3b5499f7f 48 except Exception, e:
screamer 0:66f3b5499f7f 49 self.s = None
screamer 0:66f3b5499f7f 50 selftest.notify("HOST: Socket error: %s"% e)
screamer 0:66f3b5499f7f 51 return selftest.RESULT_ERROR
screamer 0:66f3b5499f7f 52
screamer 0:66f3b5499f7f 53 for i in range(0, 100):
screamer 0:66f3b5499f7f 54 TEST_STRING = str(uuid.uuid4())
screamer 0:66f3b5499f7f 55 self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
screamer 0:66f3b5499f7f 56 data = self.s.recv(len(TEST_STRING))
screamer 0:66f3b5499f7f 57 received_str = repr(data)[1:-1]
screamer 0:66f3b5499f7f 58 if TEST_STRING != received_str:
screamer 0:66f3b5499f7f 59 result = False
screamer 0:66f3b5499f7f 60 break
screamer 0:66f3b5499f7f 61 sys.stdout.write('.')
screamer 0:66f3b5499f7f 62 stdout.flush()
screamer 0:66f3b5499f7f 63 else:
screamer 0:66f3b5499f7f 64 result = False
screamer 0:66f3b5499f7f 65
screamer 0:66f3b5499f7f 66 if self.s is not None:
screamer 0:66f3b5499f7f 67 self.s.close()
screamer 0:66f3b5499f7f 68 return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE