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, TCPServer
dkato 0:f782d9c66c49 22
dkato 0:f782d9c66c49 23 class TCPEchoClient_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 print "HOST: Connection received...",
dkato 0:f782d9c66c49 28 count = 1;
dkato 0:f782d9c66c49 29 while True:
dkato 0:f782d9c66c49 30 data = self.request.recv(1024)
dkato 0:f782d9c66c49 31 if not data: break
dkato 0:f782d9c66c49 32 self.request.sendall(data)
dkato 0:f782d9c66c49 33 if '{{end}}' in str(data):
dkato 0:f782d9c66c49 34 print
dkato 0:f782d9c66c49 35 print str(data)
dkato 0:f782d9c66c49 36 else:
dkato 0:f782d9c66c49 37 if not count % 10:
dkato 0:f782d9c66c49 38 sys.stdout.write('.')
dkato 0:f782d9c66c49 39 count += 1
dkato 0:f782d9c66c49 40 stdout.flush()
dkato 0:f782d9c66c49 41
dkato 0:f782d9c66c49 42 class TCPEchoClientTest():
dkato 0:f782d9c66c49 43 def send_server_ip_port(self, selftest, ip_address, port_no):
dkato 0:f782d9c66c49 44 """ Set up network host. Reset target and and send server IP via serial to Mbed
dkato 0:f782d9c66c49 45 """
dkato 0:f782d9c66c49 46 c = selftest.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...'
dkato 0:f782d9c66c49 47 if c is None:
dkato 0:f782d9c66c49 48 self.print_result(selftest.RESULT_IO_SERIAL)
dkato 0:f782d9c66c49 49 return
dkato 0:f782d9c66c49 50
dkato 0:f782d9c66c49 51 selftest.notify(c.strip())
dkato 0:f782d9c66c49 52 selftest.notify("HOST: Sending server IP Address to target...")
dkato 0:f782d9c66c49 53
dkato 0:f782d9c66c49 54 connection_str = ip_address + ":" + str(port_no) + "\n"
dkato 0:f782d9c66c49 55 selftest.mbed.serial_write(connection_str)
dkato 0:f782d9c66c49 56 selftest.notify(connection_str)
dkato 0:f782d9c66c49 57
dkato 0:f782d9c66c49 58 # Two more strings about connection should be sent by MBED
dkato 0:f782d9c66c49 59 for i in range(0, 2):
dkato 0:f782d9c66c49 60 c = selftest.mbed.serial_readline()
dkato 0:f782d9c66c49 61 if c is None:
dkato 0:f782d9c66c49 62 selftest.print_result(self.RESULT_IO_SERIAL)
dkato 0:f782d9c66c49 63 return
dkato 0:f782d9c66c49 64 selftest.notify(c.strip())
dkato 0:f782d9c66c49 65
dkato 0:f782d9c66c49 66 def test(self, selftest):
dkato 0:f782d9c66c49 67 # We need to discover SERVEP_IP and set up SERVER_PORT
dkato 0:f782d9c66c49 68 # Note: Port 7 is Echo Protocol:
dkato 0:f782d9c66c49 69 #
dkato 0:f782d9c66c49 70 # Port number rationale:
dkato 0:f782d9c66c49 71 #
dkato 0:f782d9c66c49 72 # The Echo Protocol is a service in the Internet Protocol Suite defined
dkato 0:f782d9c66c49 73 # in RFC 862. It was originally proposed for testing and measurement
dkato 0:f782d9c66c49 74 # of round-trip times[citation needed] in IP networks.
dkato 0:f782d9c66c49 75 #
dkato 0:f782d9c66c49 76 # A host may connect to a server that supports the Echo Protocol using
dkato 0:f782d9c66c49 77 # the Transmission Control Protocol (TCP) or the User Datagram Protocol
dkato 0:f782d9c66c49 78 # (UDP) on the well-known port number 7. The server sends back an
dkato 0:f782d9c66c49 79 # identical copy of the data it received.
dkato 0:f782d9c66c49 80 SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
dkato 0:f782d9c66c49 81 SERVER_PORT = 7
dkato 0:f782d9c66c49 82
dkato 0:f782d9c66c49 83 # Returning none will suppress host test from printing success code
dkato 0:f782d9c66c49 84 server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
dkato 0:f782d9c66c49 85 print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT)
dkato 0:f782d9c66c49 86 self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
dkato 0:f782d9c66c49 87 server.serve_forever()