Version of easy-connect with the u-blox cellular platforms C027 and C030 added.
esp8266-driver/TESTS/net/host_tests/udp_shotgun.py@6:304d3ba87a01, 2017-11-03 (annotated)
- Committer:
- RobMeades
- Date:
- Fri Nov 03 13:01:23 2017 +0000
- Revision:
- 6:304d3ba87a01
- Parent:
- 0:19aa55d66228
Add comment concerning N2XX baud rate.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-ublox | 0:19aa55d66228 | 1 | """ |
group-ublox | 0:19aa55d66228 | 2 | mbed SDK |
group-ublox | 0:19aa55d66228 | 3 | Copyright (c) 2011-2013 ARM Limited |
group-ublox | 0:19aa55d66228 | 4 | |
group-ublox | 0:19aa55d66228 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
group-ublox | 0:19aa55d66228 | 6 | you may not use this file except in compliance with the License. |
group-ublox | 0:19aa55d66228 | 7 | You may obtain a copy of the License at |
group-ublox | 0:19aa55d66228 | 8 | |
group-ublox | 0:19aa55d66228 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
group-ublox | 0:19aa55d66228 | 10 | |
group-ublox | 0:19aa55d66228 | 11 | Unless required by applicable law or agreed to in writing, software |
group-ublox | 0:19aa55d66228 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
group-ublox | 0:19aa55d66228 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-ublox | 0:19aa55d66228 | 14 | See the License for the specific language governing permissions and |
group-ublox | 0:19aa55d66228 | 15 | limitations under the License. |
group-ublox | 0:19aa55d66228 | 16 | """ |
group-ublox | 0:19aa55d66228 | 17 | |
group-ublox | 0:19aa55d66228 | 18 | import sys |
group-ublox | 0:19aa55d66228 | 19 | import socket |
group-ublox | 0:19aa55d66228 | 20 | import json |
group-ublox | 0:19aa55d66228 | 21 | import random |
group-ublox | 0:19aa55d66228 | 22 | import itertools |
group-ublox | 0:19aa55d66228 | 23 | import time |
group-ublox | 0:19aa55d66228 | 24 | from sys import stdout |
group-ublox | 0:19aa55d66228 | 25 | from threading import Thread |
group-ublox | 0:19aa55d66228 | 26 | from SocketServer import BaseRequestHandler, UDPServer |
group-ublox | 0:19aa55d66228 | 27 | from mbed_host_tests import BaseHostTest, event_callback |
group-ublox | 0:19aa55d66228 | 28 | |
group-ublox | 0:19aa55d66228 | 29 | |
group-ublox | 0:19aa55d66228 | 30 | class UDPEchoClientHandler(BaseRequestHandler): |
group-ublox | 0:19aa55d66228 | 31 | def handle(self): |
group-ublox | 0:19aa55d66228 | 32 | """ UDP packet handler. Responds with multiple simultaneous packets |
group-ublox | 0:19aa55d66228 | 33 | """ |
group-ublox | 0:19aa55d66228 | 34 | data, sock = self.request |
group-ublox | 0:19aa55d66228 | 35 | pattern = [ord(d) << 4 for d in data] |
group-ublox | 0:19aa55d66228 | 36 | |
group-ublox | 0:19aa55d66228 | 37 | # Each byte in request indicates size of packet to recieve |
group-ublox | 0:19aa55d66228 | 38 | # Each packet size is shifted over by 4 to fit in a byte, which |
group-ublox | 0:19aa55d66228 | 39 | # avoids any issues with endianess or decoding |
group-ublox | 0:19aa55d66228 | 40 | for packet in pattern: |
group-ublox | 0:19aa55d66228 | 41 | data = [random.randint(0, 255) for _ in range(packet-1)] |
group-ublox | 0:19aa55d66228 | 42 | data.append(reduce(lambda a,b: a^b, data)) |
group-ublox | 0:19aa55d66228 | 43 | data = ''.join(map(chr, data)) |
group-ublox | 0:19aa55d66228 | 44 | sock.sendto(data, self.client_address) |
group-ublox | 0:19aa55d66228 | 45 | |
group-ublox | 0:19aa55d66228 | 46 | # Sleep a tiny bit to compensate for local network |
group-ublox | 0:19aa55d66228 | 47 | time.sleep(0.01) |
group-ublox | 0:19aa55d66228 | 48 | |
group-ublox | 0:19aa55d66228 | 49 | |
group-ublox | 0:19aa55d66228 | 50 | class UDPEchoClientTest(BaseHostTest): |
group-ublox | 0:19aa55d66228 | 51 | def __init__(self): |
group-ublox | 0:19aa55d66228 | 52 | """ |
group-ublox | 0:19aa55d66228 | 53 | Initialise test parameters. |
group-ublox | 0:19aa55d66228 | 54 | |
group-ublox | 0:19aa55d66228 | 55 | :return: |
group-ublox | 0:19aa55d66228 | 56 | """ |
group-ublox | 0:19aa55d66228 | 57 | BaseHostTest.__init__(self) |
group-ublox | 0:19aa55d66228 | 58 | self.SERVER_IP = None # Will be determined after knowing the target IP |
group-ublox | 0:19aa55d66228 | 59 | self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port |
group-ublox | 0:19aa55d66228 | 60 | self.server = None |
group-ublox | 0:19aa55d66228 | 61 | self.server_thread = None |
group-ublox | 0:19aa55d66228 | 62 | self.target_ip = None |
group-ublox | 0:19aa55d66228 | 63 | |
group-ublox | 0:19aa55d66228 | 64 | @staticmethod |
group-ublox | 0:19aa55d66228 | 65 | def find_interface_to_target_addr(target_ip): |
group-ublox | 0:19aa55d66228 | 66 | """ |
group-ublox | 0:19aa55d66228 | 67 | Finds IP address of the interface through which it is connected to the target. |
group-ublox | 0:19aa55d66228 | 68 | |
group-ublox | 0:19aa55d66228 | 69 | :return: |
group-ublox | 0:19aa55d66228 | 70 | """ |
group-ublox | 0:19aa55d66228 | 71 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
group-ublox | 0:19aa55d66228 | 72 | try: |
group-ublox | 0:19aa55d66228 | 73 | s.connect((target_ip, 0)) # Target IP, any port |
group-ublox | 0:19aa55d66228 | 74 | except socket.error: |
group-ublox | 0:19aa55d66228 | 75 | s.connect((target_ip, 8000)) # Target IP, 'random' port |
group-ublox | 0:19aa55d66228 | 76 | ip = s.getsockname()[0] |
group-ublox | 0:19aa55d66228 | 77 | s.close() |
group-ublox | 0:19aa55d66228 | 78 | return ip |
group-ublox | 0:19aa55d66228 | 79 | |
group-ublox | 0:19aa55d66228 | 80 | def setup_udp_server(self): |
group-ublox | 0:19aa55d66228 | 81 | """ |
group-ublox | 0:19aa55d66228 | 82 | sets up a UDP server for target to connect and send test data. |
group-ublox | 0:19aa55d66228 | 83 | |
group-ublox | 0:19aa55d66228 | 84 | :return: |
group-ublox | 0:19aa55d66228 | 85 | """ |
group-ublox | 0:19aa55d66228 | 86 | # !NOTE: There should mechanism to assert in the host test |
group-ublox | 0:19aa55d66228 | 87 | if self.SERVER_IP is None: |
group-ublox | 0:19aa55d66228 | 88 | self.log("setup_udp_server() called before determining server IP!") |
group-ublox | 0:19aa55d66228 | 89 | self.notify_complete(False) |
group-ublox | 0:19aa55d66228 | 90 | |
group-ublox | 0:19aa55d66228 | 91 | # Returning none will suppress host test from printing success code |
group-ublox | 0:19aa55d66228 | 92 | self.server = UDPServer((self.SERVER_IP, self.SERVER_PORT), UDPEchoClientHandler) |
group-ublox | 0:19aa55d66228 | 93 | ip, port = self.server.server_address |
group-ublox | 0:19aa55d66228 | 94 | self.SERVER_PORT = port |
group-ublox | 0:19aa55d66228 | 95 | self.server.allow_reuse_address = True |
group-ublox | 0:19aa55d66228 | 96 | self.log("HOST: Listening for UDP packets: " + self.SERVER_IP + ":" + str(self.SERVER_PORT)) |
group-ublox | 0:19aa55d66228 | 97 | self.server_thread = Thread(target=UDPEchoClientTest.server_thread_func, args=(self,)) |
group-ublox | 0:19aa55d66228 | 98 | self.server_thread.start() |
group-ublox | 0:19aa55d66228 | 99 | |
group-ublox | 0:19aa55d66228 | 100 | @staticmethod |
group-ublox | 0:19aa55d66228 | 101 | def server_thread_func(this): |
group-ublox | 0:19aa55d66228 | 102 | """ |
group-ublox | 0:19aa55d66228 | 103 | Thread function to run TCP server forever. |
group-ublox | 0:19aa55d66228 | 104 | |
group-ublox | 0:19aa55d66228 | 105 | :param this: |
group-ublox | 0:19aa55d66228 | 106 | :return: |
group-ublox | 0:19aa55d66228 | 107 | """ |
group-ublox | 0:19aa55d66228 | 108 | this.server.serve_forever() |
group-ublox | 0:19aa55d66228 | 109 | |
group-ublox | 0:19aa55d66228 | 110 | @event_callback("target_ip") |
group-ublox | 0:19aa55d66228 | 111 | def _callback_target_ip(self, key, value, timestamp): |
group-ublox | 0:19aa55d66228 | 112 | """ |
group-ublox | 0:19aa55d66228 | 113 | Callback to handle reception of target's IP address. |
group-ublox | 0:19aa55d66228 | 114 | |
group-ublox | 0:19aa55d66228 | 115 | :param key: |
group-ublox | 0:19aa55d66228 | 116 | :param value: |
group-ublox | 0:19aa55d66228 | 117 | :param timestamp: |
group-ublox | 0:19aa55d66228 | 118 | :return: |
group-ublox | 0:19aa55d66228 | 119 | """ |
group-ublox | 0:19aa55d66228 | 120 | self.target_ip = value |
group-ublox | 0:19aa55d66228 | 121 | self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip) |
group-ublox | 0:19aa55d66228 | 122 | self.setup_udp_server() |
group-ublox | 0:19aa55d66228 | 123 | |
group-ublox | 0:19aa55d66228 | 124 | @event_callback("host_ip") |
group-ublox | 0:19aa55d66228 | 125 | def _callback_host_ip(self, key, value, timestamp): |
group-ublox | 0:19aa55d66228 | 126 | """ |
group-ublox | 0:19aa55d66228 | 127 | Callback for request for host IP Addr |
group-ublox | 0:19aa55d66228 | 128 | |
group-ublox | 0:19aa55d66228 | 129 | """ |
group-ublox | 0:19aa55d66228 | 130 | self.send_kv("host_ip", self.SERVER_IP) |
group-ublox | 0:19aa55d66228 | 131 | |
group-ublox | 0:19aa55d66228 | 132 | @event_callback("host_port") |
group-ublox | 0:19aa55d66228 | 133 | def _callback_host_port(self, key, value, timestamp): |
group-ublox | 0:19aa55d66228 | 134 | """ |
group-ublox | 0:19aa55d66228 | 135 | Callback for request for host port |
group-ublox | 0:19aa55d66228 | 136 | """ |
group-ublox | 0:19aa55d66228 | 137 | self.send_kv("host_port", self.SERVER_PORT) |
group-ublox | 0:19aa55d66228 | 138 | |
group-ublox | 0:19aa55d66228 | 139 | def teardown(self): |
group-ublox | 0:19aa55d66228 | 140 | if self.server: |
group-ublox | 0:19aa55d66228 | 141 | self.server.shutdown() |
group-ublox | 0:19aa55d66228 | 142 | self.server_thread.join() |