Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sherckuith 0:479ce5546098 1
sherckuith 0:479ce5546098 2 /*
sherckuith 0:479ce5546098 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
sherckuith 0:479ce5546098 4
sherckuith 0:479ce5546098 5 Permission is hereby granted, free of charge, to any person obtaining a copy
sherckuith 0:479ce5546098 6 of this software and associated documentation files (the "Software"), to deal
sherckuith 0:479ce5546098 7 in the Software without restriction, including without limitation the rights
sherckuith 0:479ce5546098 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sherckuith 0:479ce5546098 9 copies of the Software, and to permit persons to whom the Software is
sherckuith 0:479ce5546098 10 furnished to do so, subject to the following conditions:
sherckuith 0:479ce5546098 11
sherckuith 0:479ce5546098 12 The above copyright notice and this permission notice shall be included in
sherckuith 0:479ce5546098 13 all copies or substantial portions of the Software.
sherckuith 0:479ce5546098 14
sherckuith 0:479ce5546098 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sherckuith 0:479ce5546098 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sherckuith 0:479ce5546098 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sherckuith 0:479ce5546098 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sherckuith 0:479ce5546098 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sherckuith 0:479ce5546098 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
sherckuith 0:479ce5546098 21 THE SOFTWARE.
sherckuith 0:479ce5546098 22 */
sherckuith 0:479ce5546098 23
sherckuith 0:479ce5546098 24 #ifndef LWIPNETTCPSOCKET_H
sherckuith 0:479ce5546098 25 #define LWIPNETTCPSOCKET_H
sherckuith 0:479ce5546098 26
sherckuith 0:479ce5546098 27 #define NET_LWIP_STACK 1
sherckuith 0:479ce5546098 28 #include "if/net/nettcpsocket.h"
sherckuith 0:479ce5546098 29 #include "LwipNetIf.h"
sherckuith 0:479ce5546098 30
sherckuith 0:479ce5546098 31 #include "stdint.h"
sherckuith 0:479ce5546098 32
sherckuith 0:479ce5546098 33 //Implements NetTcpSockets over lwIP raw API
sherckuith 0:479ce5546098 34
sherckuith 0:479ce5546098 35 struct tcp_pcb; //Represents a Tcp Connection, "Protocol Control Block", see rawapi.txt & tcp.h
sherckuith 0:479ce5546098 36 struct pbuf; //Lwip Buffer Container
sherckuith 0:479ce5546098 37
sherckuith 0:479ce5546098 38 typedef signed char err_t;
sherckuith 0:479ce5546098 39 typedef uint16_t u16_t;
sherckuith 0:479ce5546098 40
sherckuith 0:479ce5546098 41 class LwipNetTcpSocket: public NetTcpSocket
sherckuith 0:479ce5546098 42 {
sherckuith 0:479ce5546098 43 public:
sherckuith 0:479ce5546098 44 LwipNetTcpSocket(tcp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
sherckuith 0:479ce5546098 45 virtual ~LwipNetTcpSocket();
sherckuith 0:479ce5546098 46
sherckuith 0:479ce5546098 47 virtual NetTcpSocketErr bind(const Host& me);
sherckuith 0:479ce5546098 48 virtual NetTcpSocketErr listen();
sherckuith 0:479ce5546098 49 virtual NetTcpSocketErr connect(const Host& host);
sherckuith 0:479ce5546098 50 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket);
sherckuith 0:479ce5546098 51
sherckuith 0:479ce5546098 52 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len);
sherckuith 0:479ce5546098 53 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len);
sherckuith 0:479ce5546098 54
sherckuith 0:479ce5546098 55 virtual NetTcpSocketErr close();
sherckuith 0:479ce5546098 56
sherckuith 0:479ce5546098 57 virtual NetTcpSocketErr poll();
sherckuith 0:479ce5546098 58
sherckuith 0:479ce5546098 59 protected:
sherckuith 0:479ce5546098 60 volatile tcp_pcb* m_pPcb;
sherckuith 0:479ce5546098 61
sherckuith 0:479ce5546098 62 //Events callbacks from lwIp
sherckuith 0:479ce5546098 63 err_t acceptCb(tcp_pcb* newpcb, err_t err);
sherckuith 0:479ce5546098 64 err_t connectedCb(tcp_pcb* tpcb, err_t err);
sherckuith 0:479ce5546098 65
sherckuith 0:479ce5546098 66 void errCb(err_t err);
sherckuith 0:479ce5546098 67
sherckuith 0:479ce5546098 68 err_t sentCb(tcp_pcb* tpcb, u16_t len);
sherckuith 0:479ce5546098 69 err_t recvCb(tcp_pcb* tpcb, pbuf *p, err_t err);
sherckuith 0:479ce5546098 70
sherckuith 0:479ce5546098 71 private:
sherckuith 0:479ce5546098 72 void cleanUp(); //Flush input buffer
sherckuith 0:479ce5546098 73
sherckuith 0:479ce5546098 74 // queue<tcp_pcb*> m_lpInPcb; //Incoming connections that have not been accepted yet
sherckuith 0:479ce5546098 75 queue<LwipNetTcpSocket*> m_lpInNetTcpSocket; //Incoming connections that have not been accepted yet
sherckuith 0:479ce5546098 76
sherckuith 0:479ce5546098 77 volatile pbuf* m_pReadPbuf; //Ptr to read buffer
sherckuith 0:479ce5546098 78
sherckuith 0:479ce5546098 79 //Static callbacks : Transforms into a C++ callback
sherckuith 0:479ce5546098 80 static err_t sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err);
sherckuith 0:479ce5546098 81 static err_t sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err);
sherckuith 0:479ce5546098 82
sherckuith 0:479ce5546098 83 static void sErrCb(void *arg, err_t err);
sherckuith 0:479ce5546098 84
sherckuith 0:479ce5546098 85 static err_t sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len);
sherckuith 0:479ce5546098 86 static err_t sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
sherckuith 0:479ce5546098 87
sherckuith 0:479ce5546098 88 };
sherckuith 0:479ce5546098 89
sherckuith 0:479ce5546098 90 #endif