This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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