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.

Dependencies:   mbed

Committer:
pehrhovey
Date:
Wed Mar 17 03:17:38 2010 +0000
Revision:
0:439354122597

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:439354122597 1 #include "lwip/arch.h"
pehrhovey 0:439354122597 2
pehrhovey 0:439354122597 3 #include "dns.h"
pehrhovey 0:439354122597 4
pehrhovey 0:439354122597 5 #include "TCPConnection.h"
pehrhovey 0:439354122597 6 #include "TCPListener.h"
pehrhovey 0:439354122597 7 #include "NetServer.h"
pehrhovey 0:439354122597 8
pehrhovey 0:439354122597 9 using namespace std;
pehrhovey 0:439354122597 10 using namespace mbed;
pehrhovey 0:439354122597 11
pehrhovey 0:439354122597 12 void TCPConnection::dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg) {
pehrhovey 0:439354122597 13 TCPConnection *connection = static_cast<TCPConnection *>(arg);
pehrhovey 0:439354122597 14 if(connection) {
pehrhovey 0:439354122597 15 (connection->dnsreply)(name, ipaddr);
pehrhovey 0:439354122597 16 }
pehrhovey 0:439354122597 17 }
pehrhovey 0:439354122597 18
pehrhovey 0:439354122597 19 err_t TCPConnection::connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) {
pehrhovey 0:439354122597 20 TCPConnection *connection = static_cast<TCPConnection *>(arg);
pehrhovey 0:439354122597 21 LWIP_UNUSED_ARG(pcb);
pehrhovey 0:439354122597 22 if(connection) {
pehrhovey 0:439354122597 23 return (connection->connected)(err);
pehrhovey 0:439354122597 24 }
pehrhovey 0:439354122597 25 return ERR_OK;
pehrhovey 0:439354122597 26 }
pehrhovey 0:439354122597 27
pehrhovey 0:439354122597 28 err_t TCPConnection::sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space) {
pehrhovey 0:439354122597 29 TCPConnection *connection = static_cast<TCPConnection *>(arg);
pehrhovey 0:439354122597 30 LWIP_UNUSED_ARG(pcb);
pehrhovey 0:439354122597 31 if(connection) {
pehrhovey 0:439354122597 32 return (connection->sent)(space);
pehrhovey 0:439354122597 33 }
pehrhovey 0:439354122597 34 return ERR_OK;
pehrhovey 0:439354122597 35 }
pehrhovey 0:439354122597 36
pehrhovey 0:439354122597 37 err_t TCPConnection::recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
pehrhovey 0:439354122597 38 TCPConnection *connection = static_cast<TCPConnection *>(arg);
pehrhovey 0:439354122597 39 LWIP_UNUSED_ARG(pcb);
pehrhovey 0:439354122597 40 if(connection) {
pehrhovey 0:439354122597 41 return (connection->recv)(p, err);
pehrhovey 0:439354122597 42 }
pehrhovey 0:439354122597 43 return ERR_OK;
pehrhovey 0:439354122597 44 }
pehrhovey 0:439354122597 45
pehrhovey 0:439354122597 46 err_t TCPConnection::poll_callback(void *arg, struct tcp_pcb *pcb) {
pehrhovey 0:439354122597 47 TCPConnection *connection = static_cast<TCPConnection *>(arg);
pehrhovey 0:439354122597 48 LWIP_UNUSED_ARG(pcb);
pehrhovey 0:439354122597 49 if(connection) {
pehrhovey 0:439354122597 50 return (connection->poll)();
pehrhovey 0:439354122597 51 }
pehrhovey 0:439354122597 52 return ERR_OK;
pehrhovey 0:439354122597 53 }
pehrhovey 0:439354122597 54
pehrhovey 0:439354122597 55 void TCPConnection::error_callback(void *arg, err_t erra) {
pehrhovey 0:439354122597 56 TCPConnection *connection = static_cast<TCPConnection *>(arg);
pehrhovey 0:439354122597 57 if(connection) {
pehrhovey 0:439354122597 58 (connection->err)(erra);
pehrhovey 0:439354122597 59 }
pehrhovey 0:439354122597 60 }
pehrhovey 0:439354122597 61
pehrhovey 0:439354122597 62 TCPConnection::TCPConnection() : _parent(NULL), _port(0) {
pehrhovey 0:439354122597 63 }
pehrhovey 0:439354122597 64
pehrhovey 0:439354122597 65 TCPConnection::TCPConnection(struct ip_addr ip, u16_t port) : _parent(NULL) {
pehrhovey 0:439354122597 66 this->_ipaddr = ip;
pehrhovey 0:439354122597 67 this->_port = port;
pehrhovey 0:439354122597 68 }
pehrhovey 0:439354122597 69
pehrhovey 0:439354122597 70 TCPConnection::TCPConnection(TCPListener *parent, struct tcp_pcb *pcb)
pehrhovey 0:439354122597 71 : TCPItem(pcb), _parent(parent), _ipaddr(pcb->remote_ip), _port(pcb->remote_port) {
pehrhovey 0:439354122597 72 tcp_arg(this->_pcb, static_cast<void *>(this));
pehrhovey 0:439354122597 73 connected(ERR_OK);
pehrhovey 0:439354122597 74 }
pehrhovey 0:439354122597 75
pehrhovey 0:439354122597 76 TCPConnection::~TCPConnection() {
pehrhovey 0:439354122597 77 }
pehrhovey 0:439354122597 78
pehrhovey 0:439354122597 79 err_t TCPConnection::write(void *msg, u16_t len, u8_t flags) const {
pehrhovey 0:439354122597 80 return tcp_write(this->_pcb, msg, len, flags);
pehrhovey 0:439354122597 81 }
pehrhovey 0:439354122597 82
pehrhovey 0:439354122597 83 void TCPConnection::recved(u32_t len) const {
pehrhovey 0:439354122597 84 tcp_recved(this->_pcb, len);
pehrhovey 0:439354122597 85 }
pehrhovey 0:439354122597 86
pehrhovey 0:439354122597 87 err_t TCPConnection::connected(err_t err) {
pehrhovey 0:439354122597 88 tcp_recv(this->_pcb, TCPConnection::recv_callback);
pehrhovey 0:439354122597 89 tcp_sent(this->_pcb, TCPConnection::sent_callback);
pehrhovey 0:439354122597 90 tcp_poll(this->_pcb, TCPConnection::poll_callback, 1); // addjust time (in twice a sec)
pehrhovey 0:439354122597 91 tcp_err(this->_pcb, TCPConnection::error_callback);
pehrhovey 0:439354122597 92 return ERR_OK;
pehrhovey 0:439354122597 93 }
pehrhovey 0:439354122597 94
pehrhovey 0:439354122597 95 void TCPConnection::connect() {
pehrhovey 0:439354122597 96 NetServer::ready();
pehrhovey 0:439354122597 97 open();
pehrhovey 0:439354122597 98 tcp_connect(this->_pcb, &this->_ipaddr, this->_port, TCPConnection::connected_callback);
pehrhovey 0:439354122597 99 }
pehrhovey 0:439354122597 100
pehrhovey 0:439354122597 101 err_t TCPConnection::dnsrequest(const char *hostname, struct ip_addr *addr) const {
pehrhovey 0:439354122597 102 return dns_gethostbyname(hostname, addr, TCPConnection::dnsreply_callback, (void *)this);
pehrhovey 0:439354122597 103 }