Added implementation for TCP and UDP client

pull/216/head
Przemek Wirkus 2014-03-17 17:29:18 +00:00
parent 5ad9d33664
commit b345a189cf
6 changed files with 215 additions and 72 deletions

View File

@ -1,31 +1,55 @@
#include "mbed.h"
#include "EthernetInterface.h"
const char* ECHO_SERVER_ADDRESS = "192.168.0.51";
const int ECHO_PORT = 7;
struct s_ip_address
{
int ip_1;
int ip_2;
int ip_3;
int ip_4;
};
int main() {
char buffer[256] = {0};
char out_buffer[] = "Hello World\n";
char out_success[] = "{{success}}\n{{end}}\n";
char out_failure[] = "{{failure}}\n{{end}}\n";
s_ip_address ip_addr = {0, 0, 0, 0};
int port = 0;
printf("TCPCllient waiting for server IP and port...\r\n");
scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
printf("Address received:%d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);
EthernetInterface eth;
eth.init(); //Use DHCP
eth.connect();
printf("IP Address is %s\n", eth.getIPAddress());
printf("TCPClient IP Address is %s\r\n", eth.getIPAddress());
sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);
TCPSocketConnection socket;
while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) {
printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT);
while (socket.connect(buffer, port) < 0) {
printf("TCPCllient unable to connect to %s:%d\r\n", buffer, port);
wait(1);
}
char hello[] = "Hello World\n";
socket.send_all(hello, sizeof(hello) - 1);
char buf[256];
int n = socket.receive(buf, 256);
buf[n] = '\0';
printf("%s", buf);
socket.send_all(out_buffer, sizeof(out_buffer) - 1);
int n = socket.receive(buffer, sizeof(buffer));
if (n > 0)
{
buffer[n] = '\0';
printf("%s", buffer);
if (strncmp(out_buffer, buffer, sizeof(out_buffer) - 1) == 0) {
socket.send_all(out_success, sizeof(out_success) - 1);
}
}
socket.send_all(out_failure, sizeof(out_failure) - 1);
socket.close();
eth.disconnect();
while(true) {}
return 0;
}

View File

@ -2,54 +2,64 @@
#include "rtos.h"
#include "EthernetInterface.h"
const char* ECHO_SERVER_ADDRESS = "10.2.200.94";
const int ECHO_SERVER_PORT = 7195;
const char* LOCAL_IP_ADDRESS = "10.2.200.70";
const char* NETWORK_MASK = "255.255.255.0";
const char* GATEWAY = "10.2.200.1";
char out_buffer[] = "Hello World\n";
char in_buffer[256];
#define CHECK(RC, STEP) if (RC < 0) error(STEP": %d\n", RC)
struct s_ip_address
{
int ip_1;
int ip_2;
int ip_3;
int ip_4;
};
int main() {
int rc = 0;
char buffer[256] = {0};
char out_buffer[] = "Hello World\n";
char out_success[] = "{{success}}\n{{end}}\n";
char out_failure[] = "{{failure}}\n{{end}}\n";
s_ip_address ip_addr = {0, 0, 0, 0};
int port = 0;
printf("UDPCllient waiting for server IP and port...\r\n");
scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
printf("Address received:%d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);
EthernetInterface eth;
rc = eth.init(); //Use DHCP
// rc = eth.init(LOCAL_IP_ADDRESS, NETWORK_MASK, GATEWAY);
int rc = eth.init(); //Use DHCP
CHECK(rc, "eth init");
rc = eth.connect();
CHECK(rc, "connect");
printf("IP: %s\n", eth.getIPAddress());
UDPSocket sock;
rc = sock.init();
CHECK(rc, "sock init");
printf("UDPClient IP Address is %s\r\n", eth.getIPAddress());
sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);
Endpoint echo_server;
rc = echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
rc = echo_server.set_address(buffer, port);
CHECK(rc, "set_address");
rc = sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
CHECK(rc, "sendTo");
int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
int n = sock.receiveFrom(echo_server, buffer, sizeof(buffer));
CHECK(n, "receiveFrom");
in_buffer[n] = '\0';
printf("%s\n", in_buffer);
sock.close();
eth.disconnect();
DigitalOut led(LED1);
while (true) {
led = !led;
Thread::wait(1000);
if (n > 0)
{
buffer[n] = '\0';
printf("%s", buffer);
if (strncmp(out_buffer, buffer, sizeof(out_buffer) - 1) == 0) {
sock.sendTo(echo_server, out_success, sizeof(out_success) - 1);
}
}
sock.sendTo(echo_server, out_failure, sizeof(out_failure) - 1);
sock.close();
eth.disconnect();
return 0;
}

View File

@ -14,48 +14,48 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from optparse import OptionParser
from serial import Serial
from time import sleep
from sys import stdout
class Mbed:
"""
Base class for a host driven test
"""
def __init__(self):
parser = OptionParser()
parser.add_option("-m", "--micro", dest="micro",
help="The target microcontroller ", metavar="MICRO")
parser.add_option("-p", "--port", dest="port",
help="The serial port of the target mbed (ie: COM3)", metavar="PORT")
parser.add_option("-d", "--disk", dest="disk",
help="The target disk path", metavar="DISK_PATH")
parser.add_option("-t", "--timeout", dest="timeout",
help="Timeout", metavar="TIMEOUT")
parser.add_option("-e", "--extra", dest="extra",
help="Extra serial port (used by some tests)", metavar="EXTRA")
(self.options, _) = parser.parse_args()
if self.options.port is None:
raise Exception("The serial port of the target mbed have to be provided as command line arguments")
self.port = self.options.port
self.disk = self.options.disk
self.extra_port = self.options.extra
self.extra_serial = None
self.serial = None
self.timeout = 10 if self.options.timeout is None else self.options.timeout
print 'Mbed: "%s" "%s"' % (self.port, self.disk)
def init_serial(self, baud=9600, extra_baud=9600):
self.serial = Serial(self.port, timeout = 1)
self.serial.setBaudrate(baud)
@ -63,12 +63,12 @@ class Mbed:
self.extra_serial = Serial(self.extra_port, timeout = 1)
self.extra_serial.setBaudrate(extra_baud)
self.flush()
def reset(self):
self.serial.sendBreak()
# Give time to wait for the image loading
sleep(2)
def flush(self):
self.serial.flushInput()
self.serial.flushOutput()
@ -79,7 +79,7 @@ class Mbed:
class Test:
def __init__(self):
self.mbed = Mbed()
def run(self):
try:
result = self.test()
@ -87,11 +87,11 @@ class Test:
except Exception, e:
print str(e)
self.print_result("error")
def notify(self, message):
print message
stdout.flush()
def print_result(self, result):
self.notify("\n{%s}\n{end}" % result)

View File

@ -0,0 +1,60 @@
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from SocketServer import BaseRequestHandler, TCPServer
import socket
from host_test import Test, DefaultTest
from sys import stdout
SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
SERVER_PORT = 7
class TCPEchoClientTest(Test):
def __init__(self):
Test.__init__(self)
self.mbed.init_serial()
def send_server_ip_port(self, ip_address, port_no):
print "Resetting target..."
self.mbed.reset()
print "Sending server IP Address to target..."
connection_str = ip_address + ":" + str(port_no) + "\n"
self.mbed.serial.write(connection_str)
class TCPEchoClient_Handler(BaseRequestHandler):
def print_result(self, result):
print "\n{%s}\n{end}" % result
def handle(self):
""" One handle per connection """
print "connection received"
while True:
data = self.request.recv(1024)
if not data: break
self.request.sendall(data)
print "echo: " + repr(data)
stdout.flush()
server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
print "listening for connections: " + SERVER_IP + ":" + str(SERVER_PORT)
mbed_test = TCPEchoClientTest();
mbed_test.send_server_ip_port(SERVER_IP, SERVER_PORT)
server.serve_forever()

View File

@ -0,0 +1,58 @@
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from SocketServer import BaseRequestHandler, UDPServer
from host_test import Test, DefaultTest
import socket
from sys import stdout
SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
SERVER_PORT = 7
class UDPEchoClientTest(Test):
def __init__(self):
Test.__init__(self)
self.mbed.init_serial()
def send_server_ip_port(self, ip_address, port_no):
print "Resetting target..."
self.mbed.reset()
print "Sending server IP Address to target..."
connection_str = ip_address + ":" + str(port_no) + "\n"
self.mbed.serial.write(connection_str)
class UDPEchoClient_Handler(BaseRequestHandler):
def print_result(self, result):
print "\n{%s}\n{end}" % result
def handle(self):
""" One handle per connection """
print "connection received"
data, socket = self.request
print "client: ", self.client_address
print "data: ", data
socket.sendto(data, self.client_address)
stdout.flush()
server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler)
print "listening for connections"
mbed_test = UDPEchoClientTest();
mbed_test.send_server_ip_port(SERVER_IP, SERVER_PORT)
server.serve_forever()

View File

@ -258,15 +258,6 @@ class SingleTestRunner(object):
line = ''
output = []
while (time() - start) < duration:
# Give the client a way to interrupt the test
"""
try:
c = client.recv(1)
if c == '!':
break
except Exception, _:
pass
"""
try:
c = obs.queue.get(block=True, timeout=1)
except Empty, _: