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 "SerialBuf.h"
lorcansmith 0:2a53a4c3238c 25 #include "mbed.h"
lorcansmith 0:2a53a4c3238c 26
lorcansmith 0:2a53a4c3238c 27 //#define __DEBUG
lorcansmith 0:2a53a4c3238c 28 #include "dbg/dbg.h"
lorcansmith 0:2a53a4c3238c 29
lorcansmith 0:2a53a4c3238c 30 #include "netCfg.h"
lorcansmith 0:2a53a4c3238c 31 #if NET_GPRS
lorcansmith 0:2a53a4c3238c 32
lorcansmith 0:2a53a4c3238c 33 #if NET_USB_SERIAL
lorcansmith 0:2a53a4c3238c 34 #define m_pStream( a ) (m_pSerial?m_pSerial->a:m_pUsbSerial->a)
lorcansmith 0:2a53a4c3238c 35 #else
lorcansmith 0:2a53a4c3238c 36 #define m_pStream( a ) (m_pSerial->a)
lorcansmith 0:2a53a4c3238c 37 #endif
lorcansmith 0:2a53a4c3238c 38
lorcansmith 0:2a53a4c3238c 39 //Circular buf
lorcansmith 0:2a53a4c3238c 40
lorcansmith 0:2a53a4c3238c 41 SerialCircularBuf::SerialCircularBuf(int len) : m_readMode(false)
lorcansmith 0:2a53a4c3238c 42 {
lorcansmith 0:2a53a4c3238c 43 m_buf = new char[len];
lorcansmith 0:2a53a4c3238c 44 m_len = len;
lorcansmith 0:2a53a4c3238c 45 m_pReadStart = m_pRead = m_buf;
lorcansmith 0:2a53a4c3238c 46 m_pWrite = m_buf;
lorcansmith 0:2a53a4c3238c 47 }
lorcansmith 0:2a53a4c3238c 48
lorcansmith 0:2a53a4c3238c 49 SerialCircularBuf::~SerialCircularBuf()
lorcansmith 0:2a53a4c3238c 50 {
lorcansmith 0:2a53a4c3238c 51 if(m_buf)
lorcansmith 0:2a53a4c3238c 52 delete[] m_buf;
lorcansmith 0:2a53a4c3238c 53 }
lorcansmith 0:2a53a4c3238c 54
lorcansmith 0:2a53a4c3238c 55 int SerialCircularBuf::room() //Return room available in buf
lorcansmith 0:2a53a4c3238c 56 {
lorcansmith 0:2a53a4c3238c 57 //return m_len - len() - 1; //-1 is to avoid loop
lorcansmith 0:2a53a4c3238c 58 if ( m_pReadStart > m_pWrite )
lorcansmith 0:2a53a4c3238c 59 return ( m_pReadStart - m_pWrite - 1 );
lorcansmith 0:2a53a4c3238c 60 else
lorcansmith 0:2a53a4c3238c 61 return m_len - ( m_pWrite - m_pReadStart ) - 1;
lorcansmith 0:2a53a4c3238c 62 }
lorcansmith 0:2a53a4c3238c 63
lorcansmith 0:2a53a4c3238c 64 int SerialCircularBuf::len() //Return chars length in buf
lorcansmith 0:2a53a4c3238c 65 {
lorcansmith 0:2a53a4c3238c 66 if ( m_pWrite >= m_pRead )
lorcansmith 0:2a53a4c3238c 67 return ( m_pWrite - m_pRead );
lorcansmith 0:2a53a4c3238c 68 else
lorcansmith 0:2a53a4c3238c 69 return m_len - ( m_pRead - m_pWrite ); // = ( ( m_buf + m_len) - m_pRead ) + ( m_pWrite - m_buf )
lorcansmith 0:2a53a4c3238c 70 }
lorcansmith 0:2a53a4c3238c 71
lorcansmith 0:2a53a4c3238c 72 void SerialCircularBuf::write(char c)
lorcansmith 0:2a53a4c3238c 73 {
lorcansmith 0:2a53a4c3238c 74 #if 0
lorcansmith 0:2a53a4c3238c 75 if(!room())
lorcansmith 0:2a53a4c3238c 76 return;
lorcansmith 0:2a53a4c3238c 77 #endif
lorcansmith 0:2a53a4c3238c 78 //WARN: Must call room() before
lorcansmith 0:2a53a4c3238c 79 *m_pWrite = c;
lorcansmith 0:2a53a4c3238c 80 m_pWrite++;
lorcansmith 0:2a53a4c3238c 81 if(m_pWrite>=m_buf+m_len)
lorcansmith 0:2a53a4c3238c 82 m_pWrite=m_buf;
lorcansmith 0:2a53a4c3238c 83 }
lorcansmith 0:2a53a4c3238c 84 char SerialCircularBuf::read()
lorcansmith 0:2a53a4c3238c 85 {
lorcansmith 0:2a53a4c3238c 86 #if 0
lorcansmith 0:2a53a4c3238c 87 if(!len())
lorcansmith 0:2a53a4c3238c 88 return 0;
lorcansmith 0:2a53a4c3238c 89 #endif
lorcansmith 0:2a53a4c3238c 90 //WARN: Must call len() before
lorcansmith 0:2a53a4c3238c 91 char c = *m_pRead;
lorcansmith 0:2a53a4c3238c 92 m_pRead++;
lorcansmith 0:2a53a4c3238c 93
lorcansmith 0:2a53a4c3238c 94 if(m_pRead>=m_buf+m_len)
lorcansmith 0:2a53a4c3238c 95 m_pRead=m_buf;
lorcansmith 0:2a53a4c3238c 96
lorcansmith 0:2a53a4c3238c 97 if(!m_readMode) //If readmode=false, trash this char
lorcansmith 0:2a53a4c3238c 98 m_pReadStart=m_pRead;
lorcansmith 0:2a53a4c3238c 99
lorcansmith 0:2a53a4c3238c 100 return c;
lorcansmith 0:2a53a4c3238c 101 }
lorcansmith 0:2a53a4c3238c 102
lorcansmith 0:2a53a4c3238c 103 void SerialCircularBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
lorcansmith 0:2a53a4c3238c 104 {
lorcansmith 0:2a53a4c3238c 105 if(m_readMode == true && readMode == false)
lorcansmith 0:2a53a4c3238c 106 {
lorcansmith 0:2a53a4c3238c 107 //Trash bytes that have been read
lorcansmith 0:2a53a4c3238c 108 flushRead();
lorcansmith 0:2a53a4c3238c 109 }
lorcansmith 0:2a53a4c3238c 110 m_readMode = readMode;
lorcansmith 0:2a53a4c3238c 111 }
lorcansmith 0:2a53a4c3238c 112
lorcansmith 0:2a53a4c3238c 113 void SerialCircularBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true)
lorcansmith 0:2a53a4c3238c 114 {
lorcansmith 0:2a53a4c3238c 115 m_pReadStart = m_pRead;
lorcansmith 0:2a53a4c3238c 116 }
lorcansmith 0:2a53a4c3238c 117
lorcansmith 0:2a53a4c3238c 118 void SerialCircularBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true)
lorcansmith 0:2a53a4c3238c 119 {
lorcansmith 0:2a53a4c3238c 120 m_pRead = m_pReadStart;
lorcansmith 0:2a53a4c3238c 121 }
lorcansmith 0:2a53a4c3238c 122
lorcansmith 0:2a53a4c3238c 123 //SerialBuf
lorcansmith 0:2a53a4c3238c 124
lorcansmith 0:2a53a4c3238c 125 SerialBuf::SerialBuf(int len) : m_rxBuf(len), m_txBuf(len), m_pSerial(NULL) //Buffer length
lorcansmith 0:2a53a4c3238c 126 #if NET_USB_SERIAL
lorcansmith 0:2a53a4c3238c 127 , m_pUsbSerial(NULL)
lorcansmith 0:2a53a4c3238c 128 #endif
lorcansmith 0:2a53a4c3238c 129 {
lorcansmith 0:2a53a4c3238c 130 DBG("New Serial buf@%p\n", this);
lorcansmith 0:2a53a4c3238c 131 }
lorcansmith 0:2a53a4c3238c 132
lorcansmith 0:2a53a4c3238c 133 SerialBuf::~SerialBuf()
lorcansmith 0:2a53a4c3238c 134 {
lorcansmith 0:2a53a4c3238c 135
lorcansmith 0:2a53a4c3238c 136 }
lorcansmith 0:2a53a4c3238c 137
lorcansmith 0:2a53a4c3238c 138 void SerialBuf::attach(Serial* pSerial)
lorcansmith 0:2a53a4c3238c 139 {
lorcansmith 0:2a53a4c3238c 140 DBG("Serial buf@%p in attach\n", this);
lorcansmith 0:2a53a4c3238c 141 m_pSerial = pSerial;
lorcansmith 0:2a53a4c3238c 142 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, Serial::RxIrq);
lorcansmith 0:2a53a4c3238c 143 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, Serial::TxIrq);
lorcansmith 0:2a53a4c3238c 144 onRxInterrupt(); //Read data
lorcansmith 0:2a53a4c3238c 145 }
lorcansmith 0:2a53a4c3238c 146
lorcansmith 0:2a53a4c3238c 147 void SerialBuf::detach()
lorcansmith 0:2a53a4c3238c 148 {
lorcansmith 0:2a53a4c3238c 149 if(m_pSerial)
lorcansmith 0:2a53a4c3238c 150 {
lorcansmith 0:2a53a4c3238c 151 m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::RxIrq);
lorcansmith 0:2a53a4c3238c 152 m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::TxIrq);
lorcansmith 0:2a53a4c3238c 153 m_pSerial = NULL;
lorcansmith 0:2a53a4c3238c 154 }
lorcansmith 0:2a53a4c3238c 155 #if NET_USB_SERIAL
lorcansmith 0:2a53a4c3238c 156 else if(m_pUsbSerial)
lorcansmith 0:2a53a4c3238c 157 {
lorcansmith 0:2a53a4c3238c 158 m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::RxIrq);
lorcansmith 0:2a53a4c3238c 159 m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::TxIrq);
lorcansmith 0:2a53a4c3238c 160 m_pUsbSerial = NULL;
lorcansmith 0:2a53a4c3238c 161 }
lorcansmith 0:2a53a4c3238c 162 #endif
lorcansmith 0:2a53a4c3238c 163 }
lorcansmith 0:2a53a4c3238c 164
lorcansmith 0:2a53a4c3238c 165 #if NET_USB_SERIAL
lorcansmith 0:2a53a4c3238c 166 void SerialBuf::attach(UsbSerial* pUsbSerial)
lorcansmith 0:2a53a4c3238c 167 {
lorcansmith 0:2a53a4c3238c 168 m_pUsbSerial = pUsbSerial;
lorcansmith 0:2a53a4c3238c 169 m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, UsbSerial::RxIrq);
lorcansmith 0:2a53a4c3238c 170 m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, UsbSerial::TxIrq);
lorcansmith 0:2a53a4c3238c 171 onRxInterrupt(); //Read data
lorcansmith 0:2a53a4c3238c 172 }
lorcansmith 0:2a53a4c3238c 173 #endif
lorcansmith 0:2a53a4c3238c 174
lorcansmith 0:2a53a4c3238c 175 char SerialBuf::getc()
lorcansmith 0:2a53a4c3238c 176 {
lorcansmith 0:2a53a4c3238c 177 while(!readable());
lorcansmith 0:2a53a4c3238c 178 char c = m_rxBuf.read();
lorcansmith 0:2a53a4c3238c 179 return c;
lorcansmith 0:2a53a4c3238c 180 }
lorcansmith 0:2a53a4c3238c 181
lorcansmith 0:2a53a4c3238c 182 void SerialBuf::putc(char c)
lorcansmith 0:2a53a4c3238c 183 {
lorcansmith 0:2a53a4c3238c 184 while(!writeable());
lorcansmith 0:2a53a4c3238c 185 m_txBuf.write(c);
lorcansmith 0:2a53a4c3238c 186 onTxInterrupt();
lorcansmith 0:2a53a4c3238c 187 }
lorcansmith 0:2a53a4c3238c 188
lorcansmith 0:2a53a4c3238c 189 bool SerialBuf::readable()
lorcansmith 0:2a53a4c3238c 190 {
lorcansmith 0:2a53a4c3238c 191 if( !m_rxBuf.len() ) //Fill buf if possible
lorcansmith 0:2a53a4c3238c 192 onRxInterrupt();
lorcansmith 0:2a53a4c3238c 193 return (m_rxBuf.len() > 0);
lorcansmith 0:2a53a4c3238c 194 }
lorcansmith 0:2a53a4c3238c 195
lorcansmith 0:2a53a4c3238c 196 bool SerialBuf::writeable()
lorcansmith 0:2a53a4c3238c 197 {
lorcansmith 0:2a53a4c3238c 198 if( !m_txBuf.room() ) //Free buf is possible
lorcansmith 0:2a53a4c3238c 199 onTxInterrupt();
lorcansmith 0:2a53a4c3238c 200 return (m_txBuf.room() > 0);
lorcansmith 0:2a53a4c3238c 201 }
lorcansmith 0:2a53a4c3238c 202
lorcansmith 0:2a53a4c3238c 203 void SerialBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
lorcansmith 0:2a53a4c3238c 204 {
lorcansmith 0:2a53a4c3238c 205 m_rxBuf.setReadMode(readMode);
lorcansmith 0:2a53a4c3238c 206 }
lorcansmith 0:2a53a4c3238c 207
lorcansmith 0:2a53a4c3238c 208 void SerialBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true)
lorcansmith 0:2a53a4c3238c 209 {
lorcansmith 0:2a53a4c3238c 210 m_rxBuf.flushRead();
lorcansmith 0:2a53a4c3238c 211 }
lorcansmith 0:2a53a4c3238c 212
lorcansmith 0:2a53a4c3238c 213 void SerialBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true)
lorcansmith 0:2a53a4c3238c 214 {
lorcansmith 0:2a53a4c3238c 215 m_rxBuf.resetRead();
lorcansmith 0:2a53a4c3238c 216 }
lorcansmith 0:2a53a4c3238c 217
lorcansmith 0:2a53a4c3238c 218 void SerialBuf::onRxInterrupt() //Callback from m_pSerial
lorcansmith 0:2a53a4c3238c 219 {
lorcansmith 0:2a53a4c3238c 220 while( m_rxBuf.room() && m_pStream(readable()) )
lorcansmith 0:2a53a4c3238c 221 {
lorcansmith 0:2a53a4c3238c 222 m_rxBuf.write(m_pStream(getc()));
lorcansmith 0:2a53a4c3238c 223 }
lorcansmith 0:2a53a4c3238c 224 }
lorcansmith 0:2a53a4c3238c 225
lorcansmith 0:2a53a4c3238c 226 void SerialBuf::onTxInterrupt() //Callback from m_pSerial
lorcansmith 0:2a53a4c3238c 227 {
lorcansmith 0:2a53a4c3238c 228 while( m_txBuf.len() && m_pStream(writeable()) )
lorcansmith 0:2a53a4c3238c 229 {
lorcansmith 0:2a53a4c3238c 230 m_pStream(putc(m_txBuf.read()));
lorcansmith 0:2a53a4c3238c 231 }
lorcansmith 0:2a53a4c3238c 232 }
lorcansmith 0:2a53a4c3238c 233
lorcansmith 0:2a53a4c3238c 234
lorcansmith 0:2a53a4c3238c 235 #endif