mbed-os
Fork of mbed-os by
tools/host_tests/udp_link_layer_auto.py@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | """ |
elessair | 0:f269e3021894 | 2 | mbed SDK |
elessair | 0:f269e3021894 | 3 | Copyright (c) 2011-2013 ARM Limited |
elessair | 0:f269e3021894 | 4 | |
elessair | 0:f269e3021894 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 6 | you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 7 | You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 8 | |
elessair | 0:f269e3021894 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 10 | |
elessair | 0:f269e3021894 | 11 | Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 14 | See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 15 | limitations under the License. |
elessair | 0:f269e3021894 | 16 | """ |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | """ |
elessair | 0:f269e3021894 | 19 | How to use: |
elessair | 0:f269e3021894 | 20 | make.py -m LPC1768 -t ARM -d E:\ -n NET_14 |
elessair | 0:f269e3021894 | 21 | udp_link_layer_auto.py -p COM20 -d E:\ -t 10 |
elessair | 0:f269e3021894 | 22 | """ |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | import re |
elessair | 0:f269e3021894 | 25 | import uuid |
elessair | 0:f269e3021894 | 26 | import socket |
elessair | 0:f269e3021894 | 27 | import thread |
elessair | 0:f269e3021894 | 28 | from sys import stdout |
elessair | 0:f269e3021894 | 29 | from time import time, sleep |
elessair | 0:f269e3021894 | 30 | from host_test import DefaultTest |
elessair | 0:f269e3021894 | 31 | from SocketServer import BaseRequestHandler, UDPServer |
elessair | 0:f269e3021894 | 32 | |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | # Received datagrams (with time) |
elessair | 0:f269e3021894 | 35 | dict_udp_recv_datagrams = dict() |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | # Sent datagrams (with time) |
elessair | 0:f269e3021894 | 38 | dict_udp_sent_datagrams = dict() |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | class UDPEchoClient_Handler(BaseRequestHandler): |
elessair | 0:f269e3021894 | 42 | def handle(self): |
elessair | 0:f269e3021894 | 43 | """ One handle per connection |
elessair | 0:f269e3021894 | 44 | """ |
elessair | 0:f269e3021894 | 45 | _data, _socket = self.request |
elessair | 0:f269e3021894 | 46 | # Process received datagram |
elessair | 0:f269e3021894 | 47 | data_str = repr(_data)[1:-1] |
elessair | 0:f269e3021894 | 48 | dict_udp_recv_datagrams[data_str] = time() |
elessair | 0:f269e3021894 | 49 | |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | def udp_packet_recv(threadName, server_ip, server_port): |
elessair | 0:f269e3021894 | 52 | """ This function will receive packet stream from mbed device |
elessair | 0:f269e3021894 | 53 | """ |
elessair | 0:f269e3021894 | 54 | server = UDPServer((server_ip, server_port), UDPEchoClient_Handler) |
elessair | 0:f269e3021894 | 55 | print "[UDP_COUNTER] Listening for connections... %s:%d"% (server_ip, server_port) |
elessair | 0:f269e3021894 | 56 | server.serve_forever() |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | |
elessair | 0:f269e3021894 | 59 | class UDPEchoServerTest(DefaultTest): |
elessair | 0:f269e3021894 | 60 | ECHO_SERVER_ADDRESS = "" # UDP IP of datagram bursts |
elessair | 0:f269e3021894 | 61 | ECHO_PORT = 0 # UDP port for datagram bursts |
elessair | 0:f269e3021894 | 62 | CONTROL_PORT = 23 # TCP port used to get stats from mbed device, e.g. counters |
elessair | 0:f269e3021894 | 63 | s = None # Socket |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | TEST_PACKET_COUNT = 1000 # how many packets should be send |
elessair | 0:f269e3021894 | 66 | TEST_STRESS_FACTOR = 0.001 # stress factor: 10 ms |
elessair | 0:f269e3021894 | 67 | PACKET_SATURATION_RATIO = 29.9 # Acceptable packet transmission in % |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)" |
elessair | 0:f269e3021894 | 70 | re_detect_server_ip = re.compile(PATTERN_SERVER_IP) |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | def get_control_data(self, command="stat\n"): |
elessair | 0:f269e3021894 | 73 | BUFFER_SIZE = 256 |
elessair | 0:f269e3021894 | 74 | try: |
elessair | 0:f269e3021894 | 75 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
elessair | 0:f269e3021894 | 76 | s.connect((self.ECHO_SERVER_ADDRESS, self.CONTROL_PORT)) |
elessair | 0:f269e3021894 | 77 | except Exception, e: |
elessair | 0:f269e3021894 | 78 | data = None |
elessair | 0:f269e3021894 | 79 | s.send(command) |
elessair | 0:f269e3021894 | 80 | data = s.recv(BUFFER_SIZE) |
elessair | 0:f269e3021894 | 81 | s.close() |
elessair | 0:f269e3021894 | 82 | return data |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | def test(self): |
elessair | 0:f269e3021894 | 85 | serial_ip_msg = self.mbed.serial_readline() |
elessair | 0:f269e3021894 | 86 | if serial_ip_msg is None: |
elessair | 0:f269e3021894 | 87 | return self.RESULT_IO_SERIAL |
elessair | 0:f269e3021894 | 88 | stdout.write(serial_ip_msg) |
elessair | 0:f269e3021894 | 89 | stdout.flush() |
elessair | 0:f269e3021894 | 90 | # Searching for IP address and port prompted by server |
elessair | 0:f269e3021894 | 91 | m = self.re_detect_server_ip.search(serial_ip_msg) |
elessair | 0:f269e3021894 | 92 | if m and len(m.groups()): |
elessair | 0:f269e3021894 | 93 | self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4]) |
elessair | 0:f269e3021894 | 94 | self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method |
elessair | 0:f269e3021894 | 95 | self.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT)) |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | # Open client socket to burst datagrams to UDP server in mbed |
elessair | 0:f269e3021894 | 98 | try: |
elessair | 0:f269e3021894 | 99 | self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
elessair | 0:f269e3021894 | 100 | except Exception, e: |
elessair | 0:f269e3021894 | 101 | self.s = None |
elessair | 0:f269e3021894 | 102 | self.notify("HOST: Error: %s"% e) |
elessair | 0:f269e3021894 | 103 | return self.RESULT_ERROR |
elessair | 0:f269e3021894 | 104 | |
elessair | 0:f269e3021894 | 105 | # UDP replied receiver works in background to get echoed datagrams |
elessair | 0:f269e3021894 | 106 | SERVER_IP = str(socket.gethostbyname(socket.getfqdn())) |
elessair | 0:f269e3021894 | 107 | SERVER_PORT = self.ECHO_PORT + 1 |
elessair | 0:f269e3021894 | 108 | thread.start_new_thread(udp_packet_recv, ("Thread-udp-recv", SERVER_IP, SERVER_PORT)) |
elessair | 0:f269e3021894 | 109 | sleep(0.5) |
elessair | 0:f269e3021894 | 110 | |
elessair | 0:f269e3021894 | 111 | # Burst part |
elessair | 0:f269e3021894 | 112 | for no in range(self.TEST_PACKET_COUNT): |
elessair | 0:f269e3021894 | 113 | TEST_STRING = str(uuid.uuid4()) |
elessair | 0:f269e3021894 | 114 | payload = str(no) + "__" + TEST_STRING |
elessair | 0:f269e3021894 | 115 | self.s.sendto(payload, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT)) |
elessair | 0:f269e3021894 | 116 | dict_udp_sent_datagrams[payload] = time() |
elessair | 0:f269e3021894 | 117 | sleep(self.TEST_STRESS_FACTOR) |
elessair | 0:f269e3021894 | 118 | |
elessair | 0:f269e3021894 | 119 | if self.s is not None: |
elessair | 0:f269e3021894 | 120 | self.s.close() |
elessair | 0:f269e3021894 | 121 | |
elessair | 0:f269e3021894 | 122 | # Wait 5 seconds for packets to come |
elessair | 0:f269e3021894 | 123 | result = True |
elessair | 0:f269e3021894 | 124 | self.notify("HOST: Test Summary:") |
elessair | 0:f269e3021894 | 125 | for d in range(5): |
elessair | 0:f269e3021894 | 126 | sleep(1.0) |
elessair | 0:f269e3021894 | 127 | summary_datagram_success = (float(len(dict_udp_recv_datagrams)) / float(self.TEST_PACKET_COUNT)) * 100.0 |
elessair | 0:f269e3021894 | 128 | self.notify("HOST: Datagrams received after +%d sec: %.3f%% (%d / %d), stress=%.3f ms"% (d, |
elessair | 0:f269e3021894 | 129 | summary_datagram_success, |
elessair | 0:f269e3021894 | 130 | len(dict_udp_recv_datagrams), |
elessair | 0:f269e3021894 | 131 | self.TEST_PACKET_COUNT, |
elessair | 0:f269e3021894 | 132 | self.TEST_STRESS_FACTOR)) |
elessair | 0:f269e3021894 | 133 | result = result and (summary_datagram_success >= self.PACKET_SATURATION_RATIO) |
elessair | 0:f269e3021894 | 134 | stdout.flush() |
elessair | 0:f269e3021894 | 135 | |
elessair | 0:f269e3021894 | 136 | # Getting control data from test |
elessair | 0:f269e3021894 | 137 | self.notify("...") |
elessair | 0:f269e3021894 | 138 | self.notify("HOST: Mbed Summary:") |
elessair | 0:f269e3021894 | 139 | mbed_stats = self.get_control_data() |
elessair | 0:f269e3021894 | 140 | self.notify(mbed_stats) |
elessair | 0:f269e3021894 | 141 | return self.RESULT_SUCCESS if result else self.RESULT_FAILURE |
elessair | 0:f269e3021894 | 142 | |
elessair | 0:f269e3021894 | 143 | |
elessair | 0:f269e3021894 | 144 | if __name__ == '__main__': |
elessair | 0:f269e3021894 | 145 | UDPEchoServerTest().run() |