mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:f782d9c66c49 1 """
dkato 0:f782d9c66c49 2 mbed SDK
dkato 0:f782d9c66c49 3 Copyright (c) 2011-2013 ARM Limited
dkato 0:f782d9c66c49 4
dkato 0:f782d9c66c49 5 Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:f782d9c66c49 6 you may not use this file except in compliance with the License.
dkato 0:f782d9c66c49 7 You may obtain a copy of the License at
dkato 0:f782d9c66c49 8
dkato 0:f782d9c66c49 9 http://www.apache.org/licenses/LICENSE-2.0
dkato 0:f782d9c66c49 10
dkato 0:f782d9c66c49 11 Unless required by applicable law or agreed to in writing, software
dkato 0:f782d9c66c49 12 distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:f782d9c66c49 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:f782d9c66c49 14 See the License for the specific language governing permissions and
dkato 0:f782d9c66c49 15 limitations under the License.
dkato 0:f782d9c66c49 16 """
dkato 0:f782d9c66c49 17
dkato 0:f782d9c66c49 18 import sys
dkato 0:f782d9c66c49 19 import socket
dkato 0:f782d9c66c49 20 from sys import stdout
dkato 0:f782d9c66c49 21 from SocketServer import BaseRequestHandler, UDPServer
dkato 0:f782d9c66c49 22
dkato 0:f782d9c66c49 23 class UDPEchoClient_Handler(BaseRequestHandler):
dkato 0:f782d9c66c49 24 def handle(self):
dkato 0:f782d9c66c49 25 """ One handle per connection
dkato 0:f782d9c66c49 26 """
dkato 0:f782d9c66c49 27 data, socket = self.request
dkato 0:f782d9c66c49 28 socket.sendto(data, self.client_address)
dkato 0:f782d9c66c49 29 if '{{end}}' in data:
dkato 0:f782d9c66c49 30 print
dkato 0:f782d9c66c49 31 print data
dkato 0:f782d9c66c49 32 else:
dkato 0:f782d9c66c49 33 sys.stdout.write('.')
dkato 0:f782d9c66c49 34 stdout.flush()
dkato 0:f782d9c66c49 35
dkato 0:f782d9c66c49 36 class UDPEchoClientTest():
dkato 0:f782d9c66c49 37
dkato 0:f782d9c66c49 38 def send_server_ip_port(self, selftest, ip_address, port_no):
dkato 0:f782d9c66c49 39 c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
dkato 0:f782d9c66c49 40 if c is None:
dkato 0:f782d9c66c49 41 selftest.print_result(selftest.RESULT_IO_SERIAL)
dkato 0:f782d9c66c49 42 return
dkato 0:f782d9c66c49 43 selftest.notify(c.strip())
dkato 0:f782d9c66c49 44
dkato 0:f782d9c66c49 45 selftest.notify("HOST: Sending server IP Address to target...")
dkato 0:f782d9c66c49 46 connection_str = ip_address + ":" + str(port_no) + "\n"
dkato 0:f782d9c66c49 47 selftest.mbed.serial_write(connection_str)
dkato 0:f782d9c66c49 48
dkato 0:f782d9c66c49 49 c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
dkato 0:f782d9c66c49 50 if c is None:
dkato 0:f782d9c66c49 51 self.print_result(selftest.RESULT_IO_SERIAL)
dkato 0:f782d9c66c49 52 return
dkato 0:f782d9c66c49 53 selftest.notify(c.strip())
dkato 0:f782d9c66c49 54 return selftest.RESULT_PASSIVE
dkato 0:f782d9c66c49 55
dkato 0:f782d9c66c49 56 def test(self, selftest):
dkato 0:f782d9c66c49 57 # We need to discover SERVEP_IP and set up SERVER_PORT
dkato 0:f782d9c66c49 58 # Note: Port 7 is Echo Protocol:
dkato 0:f782d9c66c49 59 #
dkato 0:f782d9c66c49 60 # Port number rationale:
dkato 0:f782d9c66c49 61 #
dkato 0:f782d9c66c49 62 # The Echo Protocol is a service in the Internet Protocol Suite defined
dkato 0:f782d9c66c49 63 # in RFC 862. It was originally proposed for testing and measurement
dkato 0:f782d9c66c49 64 # of round-trip times[citation needed] in IP networks.
dkato 0:f782d9c66c49 65 #
dkato 0:f782d9c66c49 66 # A host may connect to a server that supports the Echo Protocol using
dkato 0:f782d9c66c49 67 # the Transmission Control Protocol (TCP) or the User Datagram Protocol
dkato 0:f782d9c66c49 68 # (UDP) on the well-known port number 7. The server sends back an
dkato 0:f782d9c66c49 69 # identical copy of the data it received.
dkato 0:f782d9c66c49 70 SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
dkato 0:f782d9c66c49 71 SERVER_PORT = 7
dkato 0:f782d9c66c49 72
dkato 0:f782d9c66c49 73 # Returning none will suppress host test from printing success code
dkato 0:f782d9c66c49 74 server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler)
dkato 0:f782d9c66c49 75 print "HOST: Listening for UDP connections..."
dkato 0:f782d9c66c49 76 self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
dkato 0:f782d9c66c49 77 server.serve_forever()