Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of eth_v13 by
TCPSocket.cpp
- Committer:
- hggerdd
- Date:
- 2014-03-21
- Revision:
- 3:79dc3337d9da
- Parent:
- 2:8f5bacfef390
File content as of revision 3:79dc3337d9da:
#include "TCPSocket.h"
#include "ipaddr.h"
#include "w5100.h"
#include "mbed.h"
extern Serial pc;
TCPSocket::TCPSocket(int newSocket)
{
_socket = newSocket;
}
TCPSocket::TCPSocket()
{
}
void TCPSocket::setSocket(int socket)
{
pc.printf("setSocket() = %d \n", _socket);
_socket = socket;
}
void TCPSocket::bind(int port)
{
pc.printf("bind()= %d \n", _socket);
_port = port;
W5100.writeSnMR(_socket, SnMR::TCP); // set TCP mode
W5100.writeSnPORT(_socket, _port);
}
void TCPSocket::listen()
{
pc.printf("listen()= %d \n", _socket);
W5100.execCmdSn(_socket, Sock_OPEN); // set OPEN command
W5100.execCmdSn(_socket, Sock_LISTEN); // listen
}
int TCPSocket::send(const char* buf, int len)
{
if (len > 0) {
W5100.send_data_processing(_socket, (uint8_t*)buf, len);
W5100.execCmdSn(_socket, Sock_SEND);
}
return len;
}
int TCPSocket::recv(char* buf, int len)
{
int size = W5100.getRXReceivedSize(_socket);
if (size > len) {
size = len;
}
if (size > 0) {
W5100.recv_data_processing(_socket, (uint8_t*)buf, size);
W5100.execCmdSn(_socket, Sock_RECV);
}
return size;
}
void TCPSocket::poll()
{
// status abfragen
uint8_t Sn_SR = W5100.readSnSR(_socket);
pc.printf("(sock) SnSR = 0x%x\n", Sn_SR);
switch(Sn_SR) {
case SnSR::CLOSED:
listen(); // wenn Socket geschlossen, dann öffnen
break;
case SnSR::LISTEN: // weiter warten
break;
case SnSR::ESTABLISHED: // Kontrolle, und App abarbeiten
_callbackHandler.call();
break;
case SnSR::FIN_WAIT: // für die folgenden Fälle den Port schließen
case SnSR::CLOSING:
case SnSR::TIME_WAIT:
case SnSR::CLOSE_WAIT:
case SnSR::LAST_ACK:
close();
break;
default: // Sonst --> nichts machen
break;
}
}
void TCPSocket::close()
{
if (_socket != (-1)) {
W5100.execCmdSn(_socket, Sock_DISCON);
W5100.execCmdSn(_socket, Sock_CLOSE);
}
}
void TCPSocket::setOnEvent(void (*pMethod)(void))
{
m_pCb = pMethod;
pointertype = 0;
}
int TCPSocket::getRxSize()
{
return W5100.readSnRX_RSR(_socket);
}
