Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.
Diff: lwip/TCPConnection.cpp
- Revision:
- 0:439354122597
diff -r 000000000000 -r 439354122597 lwip/TCPConnection.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwip/TCPConnection.cpp Wed Mar 17 03:17:38 2010 +0000 @@ -0,0 +1,103 @@ +#include "lwip/arch.h" + +#include "dns.h" + +#include "TCPConnection.h" +#include "TCPListener.h" +#include "NetServer.h" + +using namespace std; +using namespace mbed; + +void TCPConnection::dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg) { + TCPConnection *connection = static_cast<TCPConnection *>(arg); + if(connection) { + (connection->dnsreply)(name, ipaddr); + } +} + +err_t TCPConnection::connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) { + TCPConnection *connection = static_cast<TCPConnection *>(arg); + LWIP_UNUSED_ARG(pcb); + if(connection) { + return (connection->connected)(err); + } + return ERR_OK; +} + +err_t TCPConnection::sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space) { + TCPConnection *connection = static_cast<TCPConnection *>(arg); + LWIP_UNUSED_ARG(pcb); + if(connection) { + return (connection->sent)(space); + } + return ERR_OK; +} + +err_t TCPConnection::recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { + TCPConnection *connection = static_cast<TCPConnection *>(arg); + LWIP_UNUSED_ARG(pcb); + if(connection) { + return (connection->recv)(p, err); + } + return ERR_OK; +} + +err_t TCPConnection::poll_callback(void *arg, struct tcp_pcb *pcb) { + TCPConnection *connection = static_cast<TCPConnection *>(arg); + LWIP_UNUSED_ARG(pcb); + if(connection) { + return (connection->poll)(); + } + return ERR_OK; +} + +void TCPConnection::error_callback(void *arg, err_t erra) { + TCPConnection *connection = static_cast<TCPConnection *>(arg); + if(connection) { + (connection->err)(erra); + } +} + +TCPConnection::TCPConnection() : _parent(NULL), _port(0) { +} + +TCPConnection::TCPConnection(struct ip_addr ip, u16_t port) : _parent(NULL) { + this->_ipaddr = ip; + this->_port = port; +} + +TCPConnection::TCPConnection(TCPListener *parent, struct tcp_pcb *pcb) + : TCPItem(pcb), _parent(parent), _ipaddr(pcb->remote_ip), _port(pcb->remote_port) { + tcp_arg(this->_pcb, static_cast<void *>(this)); + connected(ERR_OK); +} + +TCPConnection::~TCPConnection() { +} + +err_t TCPConnection::write(void *msg, u16_t len, u8_t flags) const { + return tcp_write(this->_pcb, msg, len, flags); +} + +void TCPConnection::recved(u32_t len) const { + tcp_recved(this->_pcb, len); +} + +err_t TCPConnection::connected(err_t err) { + tcp_recv(this->_pcb, TCPConnection::recv_callback); + tcp_sent(this->_pcb, TCPConnection::sent_callback); + tcp_poll(this->_pcb, TCPConnection::poll_callback, 1); // addjust time (in twice a sec) + tcp_err(this->_pcb, TCPConnection::error_callback); + return ERR_OK; +} + +void TCPConnection::connect() { + NetServer::ready(); + open(); + tcp_connect(this->_pcb, &this->_ipaddr, this->_port, TCPConnection::connected_callback); +} + +err_t TCPConnection::dnsrequest(const char *hostname, struct ip_addr *addr) const { + return dns_gethostbyname(hostname, addr, TCPConnection::dnsreply_callback, (void *)this); +}