SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lorcansmith 0:2a53a4c3238c 1
lorcansmith 0:2a53a4c3238c 2 /*
lorcansmith 0:2a53a4c3238c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
lorcansmith 0:2a53a4c3238c 4
lorcansmith 0:2a53a4c3238c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
lorcansmith 0:2a53a4c3238c 6 of this software and associated documentation files (the "Software"), to deal
lorcansmith 0:2a53a4c3238c 7 in the Software without restriction, including without limitation the rights
lorcansmith 0:2a53a4c3238c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
lorcansmith 0:2a53a4c3238c 9 copies of the Software, and to permit persons to whom the Software is
lorcansmith 0:2a53a4c3238c 10 furnished to do so, subject to the following conditions:
lorcansmith 0:2a53a4c3238c 11
lorcansmith 0:2a53a4c3238c 12 The above copyright notice and this permission notice shall be included in
lorcansmith 0:2a53a4c3238c 13 all copies or substantial portions of the Software.
lorcansmith 0:2a53a4c3238c 14
lorcansmith 0:2a53a4c3238c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
lorcansmith 0:2a53a4c3238c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
lorcansmith 0:2a53a4c3238c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
lorcansmith 0:2a53a4c3238c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
lorcansmith 0:2a53a4c3238c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lorcansmith 0:2a53a4c3238c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
lorcansmith 0:2a53a4c3238c 21 THE SOFTWARE.
lorcansmith 0:2a53a4c3238c 22 */
lorcansmith 0:2a53a4c3238c 23
lorcansmith 0:2a53a4c3238c 24 #include "lwip/ip_addr.h"
lorcansmith 0:2a53a4c3238c 25 #include "lwipNetUdpSocket.h"
lorcansmith 0:2a53a4c3238c 26 #include "lwip/udp.h"
lorcansmith 0:2a53a4c3238c 27 #include "lwip/igmp.h"
lorcansmith 0:2a53a4c3238c 28
lorcansmith 0:2a53a4c3238c 29
lorcansmith 0:2a53a4c3238c 30 //#define __DEBUG
lorcansmith 0:2a53a4c3238c 31 #include "dbg/dbg.h"
lorcansmith 0:2a53a4c3238c 32
lorcansmith 0:2a53a4c3238c 33 #include "netCfg.h"
lorcansmith 0:2a53a4c3238c 34 #if NET_LWIP_STACK
lorcansmith 0:2a53a4c3238c 35
lorcansmith 0:2a53a4c3238c 36 LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt(), m_multicastGroup() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
lorcansmith 0:2a53a4c3238c 37 {
lorcansmith 0:2a53a4c3238c 38 DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb);
lorcansmith 0:2a53a4c3238c 39 if(!m_pPcb)
lorcansmith 0:2a53a4c3238c 40 m_pPcb = udp_new();
lorcansmith 0:2a53a4c3238c 41 if(m_pPcb)
lorcansmith 0:2a53a4c3238c 42 {
lorcansmith 0:2a53a4c3238c 43 //Setup callback
lorcansmith 0:2a53a4c3238c 44 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
lorcansmith 0:2a53a4c3238c 45 }
lorcansmith 0:2a53a4c3238c 46 }
lorcansmith 0:2a53a4c3238c 47
lorcansmith 0:2a53a4c3238c 48 LwipNetUdpSocket::~LwipNetUdpSocket()
lorcansmith 0:2a53a4c3238c 49 {
lorcansmith 0:2a53a4c3238c 50 close();
lorcansmith 0:2a53a4c3238c 51 }
lorcansmith 0:2a53a4c3238c 52
lorcansmith 0:2a53a4c3238c 53 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
lorcansmith 0:2a53a4c3238c 54 {
lorcansmith 0:2a53a4c3238c 55 err_t err;
lorcansmith 0:2a53a4c3238c 56
lorcansmith 0:2a53a4c3238c 57 if(!m_pPcb)
lorcansmith 0:2a53a4c3238c 58 return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
lorcansmith 0:2a53a4c3238c 59
lorcansmith 0:2a53a4c3238c 60 #if LWIP_IGMP //Multicast support enabled
lorcansmith 0:2a53a4c3238c 61 if(me.getIp().isMulticast())
lorcansmith 0:2a53a4c3238c 62 {
lorcansmith 0:2a53a4c3238c 63 DBG("This is a multicast addr, joining multicast group\n");
lorcansmith 0:2a53a4c3238c 64 m_multicastGroup = me.getIp();
lorcansmith 0:2a53a4c3238c 65 err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
lorcansmith 0:2a53a4c3238c 66 if(err)
lorcansmith 0:2a53a4c3238c 67 return NETUDPSOCKET_IF; //Could not find or create group
lorcansmith 0:2a53a4c3238c 68 }
lorcansmith 0:2a53a4c3238c 69 #endif
lorcansmith 0:2a53a4c3238c 70
lorcansmith 0:2a53a4c3238c 71 err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
lorcansmith 0:2a53a4c3238c 72 if(err)
lorcansmith 0:2a53a4c3238c 73 return NETUDPSOCKET_INUSE;
lorcansmith 0:2a53a4c3238c 74
lorcansmith 0:2a53a4c3238c 75 //Setup callback
lorcansmith 0:2a53a4c3238c 76 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
lorcansmith 0:2a53a4c3238c 77
lorcansmith 0:2a53a4c3238c 78 return NETUDPSOCKET_OK;
lorcansmith 0:2a53a4c3238c 79 }
lorcansmith 0:2a53a4c3238c 80
lorcansmith 0:2a53a4c3238c 81 #define MAX(a,b) ((a>b)?a:b)
lorcansmith 0:2a53a4c3238c 82 #define MIN(a,b) ((a<b)?a:b)
lorcansmith 0:2a53a4c3238c 83
lorcansmith 0:2a53a4c3238c 84 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost)
lorcansmith 0:2a53a4c3238c 85 {
lorcansmith 0:2a53a4c3238c 86 if( !m_pPcb ) //Pcb doesn't exist (anymore)
lorcansmith 0:2a53a4c3238c 87 return NETUDPSOCKET_MEM;
lorcansmith 0:2a53a4c3238c 88 pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
lorcansmith 0:2a53a4c3238c 89 if( !p )
lorcansmith 0:2a53a4c3238c 90 return NETUDPSOCKET_MEM;
lorcansmith 0:2a53a4c3238c 91 char* pBuf = (char*) buf;
lorcansmith 0:2a53a4c3238c 92 pbuf* q = p;
lorcansmith 0:2a53a4c3238c 93 do
lorcansmith 0:2a53a4c3238c 94 {
lorcansmith 0:2a53a4c3238c 95 memcpy (q->payload, (void*)pBuf, q->len);
lorcansmith 0:2a53a4c3238c 96 pBuf += q->len;
lorcansmith 0:2a53a4c3238c 97 q = q->next;
lorcansmith 0:2a53a4c3238c 98 } while(q != NULL);
lorcansmith 0:2a53a4c3238c 99
lorcansmith 0:2a53a4c3238c 100 err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() );
lorcansmith 0:2a53a4c3238c 101 pbuf_free( p );
lorcansmith 0:2a53a4c3238c 102 if(err)
lorcansmith 0:2a53a4c3238c 103 return NETUDPSOCKET_SETUP; //Connection problem
lorcansmith 0:2a53a4c3238c 104 DBG("%d bytes sent in UDP Socket.\n", len);
lorcansmith 0:2a53a4c3238c 105 return len;
lorcansmith 0:2a53a4c3238c 106 }
lorcansmith 0:2a53a4c3238c 107
lorcansmith 0:2a53a4c3238c 108 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost)
lorcansmith 0:2a53a4c3238c 109 {
lorcansmith 0:2a53a4c3238c 110 if( !m_pPcb ) //Pcb doesn't exist (anymore)
lorcansmith 0:2a53a4c3238c 111 return NETUDPSOCKET_MEM;
lorcansmith 0:2a53a4c3238c 112 int inLen = 0;
lorcansmith 0:2a53a4c3238c 113 int cpyLen = 0;
lorcansmith 0:2a53a4c3238c 114
lorcansmith 0:2a53a4c3238c 115 static int rmgLen = 0;
lorcansmith 0:2a53a4c3238c 116 //Contains the remaining len in this pbuf
lorcansmith 0:2a53a4c3238c 117
lorcansmith 0:2a53a4c3238c 118 if( m_lInPkt.empty() )
lorcansmith 0:2a53a4c3238c 119 return 0;
lorcansmith 0:2a53a4c3238c 120
lorcansmith 0:2a53a4c3238c 121 pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf;
lorcansmith 0:2a53a4c3238c 122
lorcansmith 0:2a53a4c3238c 123 if(pHost)
lorcansmith 0:2a53a4c3238c 124 *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port );
lorcansmith 0:2a53a4c3238c 125
lorcansmith 0:2a53a4c3238c 126 if( !pBuf )
lorcansmith 0:2a53a4c3238c 127 {
lorcansmith 0:2a53a4c3238c 128 rmgLen = 0;
lorcansmith 0:2a53a4c3238c 129 return 0;
lorcansmith 0:2a53a4c3238c 130 }
lorcansmith 0:2a53a4c3238c 131
lorcansmith 0:2a53a4c3238c 132 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
lorcansmith 0:2a53a4c3238c 133 {
lorcansmith 0:2a53a4c3238c 134 rmgLen = pBuf->len;
lorcansmith 0:2a53a4c3238c 135 }
lorcansmith 0:2a53a4c3238c 136
lorcansmith 0:2a53a4c3238c 137 while ( inLen < len )
lorcansmith 0:2a53a4c3238c 138 {
lorcansmith 0:2a53a4c3238c 139 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
lorcansmith 0:2a53a4c3238c 140 memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen);
lorcansmith 0:2a53a4c3238c 141 inLen += cpyLen;
lorcansmith 0:2a53a4c3238c 142 buf += cpyLen;
lorcansmith 0:2a53a4c3238c 143
lorcansmith 0:2a53a4c3238c 144 rmgLen = rmgLen - cpyLen; //Update rmgLen
lorcansmith 0:2a53a4c3238c 145
lorcansmith 0:2a53a4c3238c 146 if( rmgLen > 0 )
lorcansmith 0:2a53a4c3238c 147 {
lorcansmith 0:2a53a4c3238c 148 //We did not read this pbuf completely, so let's save it's pos & return
lorcansmith 0:2a53a4c3238c 149 break;
lorcansmith 0:2a53a4c3238c 150 }
lorcansmith 0:2a53a4c3238c 151
lorcansmith 0:2a53a4c3238c 152 if(pBuf->next)
lorcansmith 0:2a53a4c3238c 153 {
lorcansmith 0:2a53a4c3238c 154 pbuf* pNextPBuf = pBuf->next;
lorcansmith 0:2a53a4c3238c 155 pBuf->next = NULL; //So that it is not freed as well
lorcansmith 0:2a53a4c3238c 156 //We get the reference to pNextPBuf from m_pReadPbuf
lorcansmith 0:2a53a4c3238c 157 pbuf_free((pbuf*)pBuf);
lorcansmith 0:2a53a4c3238c 158 pBuf = pNextPBuf;
lorcansmith 0:2a53a4c3238c 159 rmgLen = pBuf->len;
lorcansmith 0:2a53a4c3238c 160 }
lorcansmith 0:2a53a4c3238c 161 else
lorcansmith 0:2a53a4c3238c 162 {
lorcansmith 0:2a53a4c3238c 163 pbuf_free((pbuf*)pBuf);
lorcansmith 0:2a53a4c3238c 164 pBuf = NULL;
lorcansmith 0:2a53a4c3238c 165 rmgLen = 0;
lorcansmith 0:2a53a4c3238c 166 m_lInPkt.pop_front();
lorcansmith 0:2a53a4c3238c 167 break; //No more data to read
lorcansmith 0:2a53a4c3238c 168 }
lorcansmith 0:2a53a4c3238c 169 }
lorcansmith 0:2a53a4c3238c 170
lorcansmith 0:2a53a4c3238c 171 return inLen;
lorcansmith 0:2a53a4c3238c 172 }
lorcansmith 0:2a53a4c3238c 173
lorcansmith 0:2a53a4c3238c 174 NetUdpSocketErr LwipNetUdpSocket::close()
lorcansmith 0:2a53a4c3238c 175 {
lorcansmith 0:2a53a4c3238c 176 DBG("LwipNetUdpSocket::close() : Closing...\n");
lorcansmith 0:2a53a4c3238c 177
lorcansmith 0:2a53a4c3238c 178 if(m_closed)
lorcansmith 0:2a53a4c3238c 179 return NETUDPSOCKET_OK; //Already being closed
lorcansmith 0:2a53a4c3238c 180 m_closed = true;
lorcansmith 0:2a53a4c3238c 181
lorcansmith 0:2a53a4c3238c 182 if( !m_pPcb ) //Pcb doesn't exist (anymore)
lorcansmith 0:2a53a4c3238c 183 return NETUDPSOCKET_MEM;
lorcansmith 0:2a53a4c3238c 184
lorcansmith 0:2a53a4c3238c 185 DBG("LwipNetUdpSocket::close() : Cleanup...\n");
lorcansmith 0:2a53a4c3238c 186
lorcansmith 0:2a53a4c3238c 187 //Cleanup incoming data
lorcansmith 0:2a53a4c3238c 188 cleanUp();
lorcansmith 0:2a53a4c3238c 189
lorcansmith 0:2a53a4c3238c 190
lorcansmith 0:2a53a4c3238c 191 DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n");
lorcansmith 0:2a53a4c3238c 192 udp_remove( (udp_pcb*) m_pPcb);
lorcansmith 0:2a53a4c3238c 193
lorcansmith 0:2a53a4c3238c 194 m_pPcb = NULL;
lorcansmith 0:2a53a4c3238c 195 return NETUDPSOCKET_OK;
lorcansmith 0:2a53a4c3238c 196 }
lorcansmith 0:2a53a4c3238c 197
lorcansmith 0:2a53a4c3238c 198 NetUdpSocketErr LwipNetUdpSocket::poll()
lorcansmith 0:2a53a4c3238c 199 {
lorcansmith 0:2a53a4c3238c 200 NetUdpSocket::flushEvents();
lorcansmith 0:2a53a4c3238c 201 return NETUDPSOCKET_OK;
lorcansmith 0:2a53a4c3238c 202 }
lorcansmith 0:2a53a4c3238c 203
lorcansmith 0:2a53a4c3238c 204 // Callbacks events
lorcansmith 0:2a53a4c3238c 205
lorcansmith 0:2a53a4c3238c 206 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
lorcansmith 0:2a53a4c3238c 207 {
lorcansmith 0:2a53a4c3238c 208 DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len);
lorcansmith 0:2a53a4c3238c 209 list<InPacket>::iterator it;
lorcansmith 0:2a53a4c3238c 210 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
lorcansmith 0:2a53a4c3238c 211 {
lorcansmith 0:2a53a4c3238c 212 if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) )
lorcansmith 0:2a53a4c3238c 213 {
lorcansmith 0:2a53a4c3238c 214 //Let's tail this packet to the previous one
lorcansmith 0:2a53a4c3238c 215 pbuf_cat((pbuf*)((*it).pBuf), p);
lorcansmith 0:2a53a4c3238c 216 //No need to queue an event in that case since the read buf has not been processed yet
lorcansmith 0:2a53a4c3238c 217 return;
lorcansmith 0:2a53a4c3238c 218 }
lorcansmith 0:2a53a4c3238c 219 }
lorcansmith 0:2a53a4c3238c 220
lorcansmith 0:2a53a4c3238c 221 //New host, add a packet to the queue
lorcansmith 0:2a53a4c3238c 222 InPacket pkt;
lorcansmith 0:2a53a4c3238c 223 pkt.pBuf = p;
lorcansmith 0:2a53a4c3238c 224 pkt.addr = *addr;
lorcansmith 0:2a53a4c3238c 225 pkt.port = port;
lorcansmith 0:2a53a4c3238c 226 m_lInPkt.push_back(pkt);
lorcansmith 0:2a53a4c3238c 227
lorcansmith 0:2a53a4c3238c 228 queueEvent(NETUDPSOCKET_READABLE);
lorcansmith 0:2a53a4c3238c 229 }
lorcansmith 0:2a53a4c3238c 230
lorcansmith 0:2a53a4c3238c 231 void LwipNetUdpSocket::cleanUp() //Flush input buffer
lorcansmith 0:2a53a4c3238c 232 {
lorcansmith 0:2a53a4c3238c 233 //Ensure that further error won't be followed to this inst (which can be destroyed)
lorcansmith 0:2a53a4c3238c 234 if( m_pPcb )
lorcansmith 0:2a53a4c3238c 235 {
lorcansmith 0:2a53a4c3238c 236 udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
lorcansmith 0:2a53a4c3238c 237 }
lorcansmith 0:2a53a4c3238c 238
lorcansmith 0:2a53a4c3238c 239 //Leaving multicast group(Ok because LwIP has a refscount for multicast group)
lorcansmith 0:2a53a4c3238c 240 #if LWIP_IGMP //Multicast support enabled
lorcansmith 0:2a53a4c3238c 241 if(m_multicastGroup.isMulticast())
lorcansmith 0:2a53a4c3238c 242 {
lorcansmith 0:2a53a4c3238c 243 igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
lorcansmith 0:2a53a4c3238c 244 m_multicastGroup = IpAddr();
lorcansmith 0:2a53a4c3238c 245 }
lorcansmith 0:2a53a4c3238c 246 #endif
lorcansmith 0:2a53a4c3238c 247
lorcansmith 0:2a53a4c3238c 248 list<InPacket>::iterator it;
lorcansmith 0:2a53a4c3238c 249 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
lorcansmith 0:2a53a4c3238c 250 {
lorcansmith 0:2a53a4c3238c 251 //Free buf
lorcansmith 0:2a53a4c3238c 252 pbuf_free((pbuf*)((*it).pBuf));
lorcansmith 0:2a53a4c3238c 253 }
lorcansmith 0:2a53a4c3238c 254 m_lInPkt.clear();
lorcansmith 0:2a53a4c3238c 255 }
lorcansmith 0:2a53a4c3238c 256
lorcansmith 0:2a53a4c3238c 257 // Static callback from LwIp
lorcansmith 0:2a53a4c3238c 258
lorcansmith 0:2a53a4c3238c 259 void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
lorcansmith 0:2a53a4c3238c 260 {
lorcansmith 0:2a53a4c3238c 261 LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg;
lorcansmith 0:2a53a4c3238c 262 return pMe->recvCb( pcb, p, addr, port );
lorcansmith 0:2a53a4c3238c 263 }
lorcansmith 0:2a53a4c3238c 264
lorcansmith 0:2a53a4c3238c 265 #endif