Committer:
zoot661
Date:
Tue May 29 09:49:18 2012 +0000
Revision:
0:f993b6d8b1d8

        

Who changed what in which revision?

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