Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:615f90842ce8 1 # Copyright 2015 ARM Limited, All rights reserved
MACRUM 0:615f90842ce8 2 #
MACRUM 0:615f90842ce8 3 # Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:615f90842ce8 4 # you may not use this file except in compliance with the License.
MACRUM 0:615f90842ce8 5 # You may obtain a copy of the License at
MACRUM 0:615f90842ce8 6 #
MACRUM 0:615f90842ce8 7 # http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:615f90842ce8 8 #
MACRUM 0:615f90842ce8 9 # Unless required by applicable law or agreed to in writing, software
MACRUM 0:615f90842ce8 10 # distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:615f90842ce8 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:615f90842ce8 12 # See the License for the specific language governing permissions and
MACRUM 0:615f90842ce8 13 # limitations under the License.
MACRUM 0:615f90842ce8 14
MACRUM 0:615f90842ce8 15 import sys
MACRUM 0:615f90842ce8 16 import select
MACRUM 0:615f90842ce8 17 import socket
MACRUM 0:615f90842ce8 18 import logging
MACRUM 0:615f90842ce8 19 from threading import Thread
MACRUM 0:615f90842ce8 20 from sys import stdout
MACRUM 0:615f90842ce8 21 from SocketServer import BaseRequestHandler, TCPServer
MACRUM 0:615f90842ce8 22 from mbed_host_tests import BaseHostTest, event_callback
MACRUM 0:615f90842ce8 23
MACRUM 0:615f90842ce8 24
MACRUM 0:615f90842ce8 25 class TCPEchoClientHandler(BaseRequestHandler):
MACRUM 0:615f90842ce8 26 def handle(self):
MACRUM 0:615f90842ce8 27 """
MACRUM 0:615f90842ce8 28 Handles a connection. Test starts by client(i.e. mbed) connecting to server.
MACRUM 0:615f90842ce8 29 This connection handler receives data and echoes back to the client util
MACRUM 0:615f90842ce8 30 {{end}} is received. Then it sits on recv() for client to terminate the
MACRUM 0:615f90842ce8 31 connection.
MACRUM 0:615f90842ce8 32
MACRUM 0:615f90842ce8 33 Note: reason for not echoing data back after receiving {{end}} is that send
MACRUM 0:615f90842ce8 34 fails raising a SocketError as client closes connection.
MACRUM 0:615f90842ce8 35 """
MACRUM 0:615f90842ce8 36 while self.server.isrunning():
MACRUM 0:615f90842ce8 37 try:
MACRUM 0:615f90842ce8 38 data = self.recv()
MACRUM 0:615f90842ce8 39 if not data: break
MACRUM 0:615f90842ce8 40 except Exception as e:
MACRUM 0:615f90842ce8 41 break
MACRUM 0:615f90842ce8 42
MACRUM 0:615f90842ce8 43 try:
MACRUM 0:615f90842ce8 44 # echo data back to the client
MACRUM 0:615f90842ce8 45 self.send(data)
MACRUM 0:615f90842ce8 46 except Exception as e:
MACRUM 0:615f90842ce8 47 break
MACRUM 0:615f90842ce8 48
MACRUM 0:615f90842ce8 49 def recv(self):
MACRUM 0:615f90842ce8 50 """
MACRUM 0:615f90842ce8 51 Try to receive until server is shutdown
MACRUM 0:615f90842ce8 52 """
MACRUM 0:615f90842ce8 53 while self.server.isrunning():
MACRUM 0:615f90842ce8 54 rl, wl, xl = select.select([self.request], [], [], 1)
MACRUM 0:615f90842ce8 55 if len(rl):
MACRUM 0:615f90842ce8 56 return self.request.recv(1024)
MACRUM 0:615f90842ce8 57
MACRUM 0:615f90842ce8 58 def send(self, data):
MACRUM 0:615f90842ce8 59 """
MACRUM 0:615f90842ce8 60 Try to send until server is shutdown
MACRUM 0:615f90842ce8 61 """
MACRUM 0:615f90842ce8 62 while self.server.isrunning():
MACRUM 0:615f90842ce8 63 rl, wl, xl = select.select([], [self.request], [], 1)
MACRUM 0:615f90842ce8 64 if len(wl):
MACRUM 0:615f90842ce8 65 self.request.sendall(data)
MACRUM 0:615f90842ce8 66 break
MACRUM 0:615f90842ce8 67
MACRUM 0:615f90842ce8 68
MACRUM 0:615f90842ce8 69 class TCPServerWrapper(TCPServer):
MACRUM 0:615f90842ce8 70 """
MACRUM 0:615f90842ce8 71 Wrapper over TCP server to implement server initiated shutdown.
MACRUM 0:615f90842ce8 72 Adds a flag:= running that a request handler can check and come out of
MACRUM 0:615f90842ce8 73 recv loop when shutdown is called.
MACRUM 0:615f90842ce8 74 """
MACRUM 0:615f90842ce8 75
MACRUM 0:615f90842ce8 76 def __init__(self, addr, request_handler):
MACRUM 0:615f90842ce8 77 # hmm, TCPServer is not sub-classed from object!
MACRUM 0:615f90842ce8 78 if issubclass(TCPServer, object):
MACRUM 0:615f90842ce8 79 super(TCPServerWrapper, self).__init__(addr, request_handler)
MACRUM 0:615f90842ce8 80 else:
MACRUM 0:615f90842ce8 81 TCPServer.__init__(self, addr, request_handler)
MACRUM 0:615f90842ce8 82 self.running = False
MACRUM 0:615f90842ce8 83
MACRUM 0:615f90842ce8 84 def serve_forever(self):
MACRUM 0:615f90842ce8 85 self.running = True
MACRUM 0:615f90842ce8 86 if issubclass(TCPServer, object):
MACRUM 0:615f90842ce8 87 super(TCPServerWrapper, self).serve_forever()
MACRUM 0:615f90842ce8 88 else:
MACRUM 0:615f90842ce8 89 TCPServer.serve_forever(self)
MACRUM 0:615f90842ce8 90
MACRUM 0:615f90842ce8 91 def shutdown(self):
MACRUM 0:615f90842ce8 92 self.running = False
MACRUM 0:615f90842ce8 93 if issubclass(TCPServer, object):
MACRUM 0:615f90842ce8 94 super(TCPServerWrapper, self).shutdown()
MACRUM 0:615f90842ce8 95 else:
MACRUM 0:615f90842ce8 96 TCPServer.shutdown(self)
MACRUM 0:615f90842ce8 97
MACRUM 0:615f90842ce8 98 def isrunning(self):
MACRUM 0:615f90842ce8 99 return self.running
MACRUM 0:615f90842ce8 100
MACRUM 0:615f90842ce8 101
MACRUM 0:615f90842ce8 102 class TCPEchoClientTest(BaseHostTest):
MACRUM 0:615f90842ce8 103
MACRUM 0:615f90842ce8 104 def __init__(self):
MACRUM 0:615f90842ce8 105 """
MACRUM 0:615f90842ce8 106 Initialise test parameters.
MACRUM 0:615f90842ce8 107
MACRUM 0:615f90842ce8 108 :return:
MACRUM 0:615f90842ce8 109 """
MACRUM 0:615f90842ce8 110 BaseHostTest.__init__(self)
MACRUM 0:615f90842ce8 111 self.SERVER_IP = None # Will be determined after knowing the target IP
MACRUM 0:615f90842ce8 112 self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port
MACRUM 0:615f90842ce8 113 self.server = None
MACRUM 0:615f90842ce8 114 self.server_thread = None
MACRUM 0:615f90842ce8 115 self.target_ip = None
MACRUM 0:615f90842ce8 116
MACRUM 0:615f90842ce8 117 @staticmethod
MACRUM 0:615f90842ce8 118 def find_interface_to_target_addr(target_ip):
MACRUM 0:615f90842ce8 119 """
MACRUM 0:615f90842ce8 120 Finds IP address of the interface through which it is connected to the target.
MACRUM 0:615f90842ce8 121
MACRUM 0:615f90842ce8 122 :return:
MACRUM 0:615f90842ce8 123 """
MACRUM 0:615f90842ce8 124 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
MACRUM 0:615f90842ce8 125 try:
MACRUM 0:615f90842ce8 126 s.connect((target_ip, 0)) # Target IP, any port
MACRUM 0:615f90842ce8 127 except socket.error:
MACRUM 0:615f90842ce8 128 s.connect((target_ip, 8000)) # Target IP, 'random' port
MACRUM 0:615f90842ce8 129 ip = s.getsockname()[0]
MACRUM 0:615f90842ce8 130 s.close()
MACRUM 0:615f90842ce8 131 return ip
MACRUM 0:615f90842ce8 132
MACRUM 0:615f90842ce8 133 def setup_tcp_server(self):
MACRUM 0:615f90842ce8 134 """
MACRUM 0:615f90842ce8 135 sets up a TCP server for target to connect and send test data.
MACRUM 0:615f90842ce8 136
MACRUM 0:615f90842ce8 137 :return:
MACRUM 0:615f90842ce8 138 """
MACRUM 0:615f90842ce8 139 # !NOTE: There should mechanism to assert in the host test
MACRUM 0:615f90842ce8 140 if self.SERVER_IP is None:
MACRUM 0:615f90842ce8 141 self.log("setup_tcp_server() called before determining server IP!")
MACRUM 0:615f90842ce8 142 self.notify_complete(False)
MACRUM 0:615f90842ce8 143
MACRUM 0:615f90842ce8 144 # Returning none will suppress host test from printing success code
MACRUM 0:615f90842ce8 145 self.server = TCPServerWrapper((self.SERVER_IP, self.SERVER_PORT), TCPEchoClientHandler)
MACRUM 0:615f90842ce8 146 ip, port = self.server.server_address
MACRUM 0:615f90842ce8 147 self.SERVER_PORT = port
MACRUM 0:615f90842ce8 148 self.server.allow_reuse_address = True
MACRUM 0:615f90842ce8 149 self.log("HOST: Listening for TCP connections: " + self.SERVER_IP + ":" + str(self.SERVER_PORT))
MACRUM 0:615f90842ce8 150 self.server_thread = Thread(target=TCPEchoClientTest.server_thread_func, args=(self,))
MACRUM 0:615f90842ce8 151 self.server_thread.start()
MACRUM 0:615f90842ce8 152
MACRUM 0:615f90842ce8 153 @staticmethod
MACRUM 0:615f90842ce8 154 def server_thread_func(this):
MACRUM 0:615f90842ce8 155 """
MACRUM 0:615f90842ce8 156 Thread function to run TCP server forever.
MACRUM 0:615f90842ce8 157
MACRUM 0:615f90842ce8 158 :param this:
MACRUM 0:615f90842ce8 159 :return:
MACRUM 0:615f90842ce8 160 """
MACRUM 0:615f90842ce8 161 this.server.serve_forever()
MACRUM 0:615f90842ce8 162
MACRUM 0:615f90842ce8 163 @event_callback("target_ip")
MACRUM 0:615f90842ce8 164 def _callback_target_ip(self, key, value, timestamp):
MACRUM 0:615f90842ce8 165 """
MACRUM 0:615f90842ce8 166 Callback to handle reception of target's IP address.
MACRUM 0:615f90842ce8 167
MACRUM 0:615f90842ce8 168 :param key:
MACRUM 0:615f90842ce8 169 :param value:
MACRUM 0:615f90842ce8 170 :param timestamp:
MACRUM 0:615f90842ce8 171 :return:
MACRUM 0:615f90842ce8 172 """
MACRUM 0:615f90842ce8 173 self.target_ip = value
MACRUM 0:615f90842ce8 174 self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip)
MACRUM 0:615f90842ce8 175 self.setup_tcp_server()
MACRUM 0:615f90842ce8 176
MACRUM 0:615f90842ce8 177 @event_callback("host_ip")
MACRUM 0:615f90842ce8 178 def _callback_host_ip(self, key, value, timestamp):
MACRUM 0:615f90842ce8 179 """
MACRUM 0:615f90842ce8 180 Callback for request for host IP Addr
MACRUM 0:615f90842ce8 181
MACRUM 0:615f90842ce8 182 """
MACRUM 0:615f90842ce8 183 self.send_kv("host_ip", self.SERVER_IP)
MACRUM 0:615f90842ce8 184
MACRUM 0:615f90842ce8 185 @event_callback("host_port")
MACRUM 0:615f90842ce8 186 def _callback_host_port(self, key, value, timestamp):
MACRUM 0:615f90842ce8 187 """
MACRUM 0:615f90842ce8 188 Callback for request for host port
MACRUM 0:615f90842ce8 189 """
MACRUM 0:615f90842ce8 190 self.send_kv("host_port", self.SERVER_PORT)
MACRUM 0:615f90842ce8 191
MACRUM 0:615f90842ce8 192 def teardown(self):
MACRUM 0:615f90842ce8 193 if self.server:
MACRUM 0:615f90842ce8 194 self.server.shutdown()
MACRUM 0:615f90842ce8 195 self.server_thread.join()