Dependencies:   mbed

Dependents:   TCP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwipNetUdpSocket.h Source File

lwipNetUdpSocket.h

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef LWIPNETUDPSOCKET_H
00025 #define LWIPNETUDPSOCKET_H
00026 
00027 #define NET_LWIP_STACK 1
00028 //#include "lwip/ip_addr.h"
00029 #include "if/net/netudpsocket.h"
00030 #include "LwipNetIf.h"
00031 
00032 #include "stdint.h"
00033 
00034 #include <list>
00035 using std::list;
00036 
00037 //Implements NetUdpSockets over lwIP raw API
00038 
00039 struct udp_pcb; //Represents a Udp Connection, "Protocol Control Block", see rawapi.txt & udp.h
00040 struct pbuf; //Lwip Buffer Container
00041 typedef struct ip_addr ip_addr_t;
00042 
00043 //typedef signed char err_t;
00044 typedef uint16_t u16_t;
00045 
00046 class LwipNetUdpSocket: public NetUdpSocket
00047 {
00048 public:
00049   LwipNetUdpSocket(udp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
00050   virtual ~LwipNetUdpSocket();
00051   
00052   virtual NetUdpSocketErr bind(const Host& me);
00053   
00054   virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost);
00055   virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
00056 
00057   virtual NetUdpSocketErr close();
00058 
00059   virtual NetUdpSocketErr poll();
00060   
00061 protected:
00062   volatile udp_pcb* m_pPcb;
00063   
00064   //Event callback from lwIp
00065   void recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port);
00066   
00067 private:
00068   void cleanUp(); //Flush input buffer
00069   struct InPacket
00070   {
00071     volatile pbuf* pBuf;
00072     ip_addr_t addr;
00073     u16_t port;
00074   };
00075   
00076   list<InPacket> m_lInPkt;
00077   IpAddr m_multicastGroup;
00078   
00079   //Static callback : Transforms into a C++ callback
00080   static void sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
00081   
00082 };
00083 
00084 #endif