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 "PPPNetIf.h"
lorcansmith 0:2a53a4c3238c 25 #include "mbed.h"
lorcansmith 0:2a53a4c3238c 26 #include "ppp/ppp.h"
lorcansmith 0:2a53a4c3238c 27 #include "lwip/init.h"
lorcansmith 0:2a53a4c3238c 28 #include "lwip/sio.h"
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_PPP
lorcansmith 0:2a53a4c3238c 35
lorcansmith 0:2a53a4c3238c 36 #define PPP_TIMEOUT 60000
lorcansmith 0:2a53a4c3238c 37
lorcansmith 0:2a53a4c3238c 38 #define BUF_SIZE 256
lorcansmith 0:2a53a4c3238c 39
lorcansmith 0:2a53a4c3238c 40 PPPNetIf::PPPNetIf(GPRSModem* pIf) : LwipNetIf(), m_pIf(pIf),/* m_open(false),*/ m_connected(false), m_status(PPP_DISCONNECTED), m_fd(0) //, m_id(0)
lorcansmith 0:2a53a4c3238c 41 {
lorcansmith 0:2a53a4c3238c 42 //FIXME: Check static refcount
lorcansmith 0:2a53a4c3238c 43 m_buf = new uint8_t[BUF_SIZE];
lorcansmith 0:2a53a4c3238c 44 }
lorcansmith 0:2a53a4c3238c 45
lorcansmith 0:2a53a4c3238c 46 PPPNetIf::~PPPNetIf()
lorcansmith 0:2a53a4c3238c 47 {
lorcansmith 0:2a53a4c3238c 48 delete[] m_buf;
lorcansmith 0:2a53a4c3238c 49 }
lorcansmith 0:2a53a4c3238c 50
lorcansmith 0:2a53a4c3238c 51 #if 0
lorcansmith 0:2a53a4c3238c 52 PPPErr PPPNetIf::open(Serial* pSerial)
lorcansmith 0:2a53a4c3238c 53 {
lorcansmith 0:2a53a4c3238c 54 GPRSErr err = m_pIf->open(pSerial);
lorcansmith 0:2a53a4c3238c 55 if(err)
lorcansmith 0:2a53a4c3238c 56 return PPP_MODEM;
lorcansmith 0:2a53a4c3238c 57 m_open = true;
lorcansmith 0:2a53a4c3238c 58 #if 0
lorcansmith 0:2a53a4c3238c 59 m_id = sioMgr::registerSerialIf(this);
lorcansmith 0:2a53a4c3238c 60 if(!m_id)
lorcansmith 0:2a53a4c3238c 61 {
lorcansmith 0:2a53a4c3238c 62 close();
lorcansmith 0:2a53a4c3238c 63 return PPP_CLOSED;
lorcansmith 0:2a53a4c3238c 64 }
lorcansmith 0:2a53a4c3238c 65 #endif
lorcansmith 0:2a53a4c3238c 66 return PPP_OK;
lorcansmith 0:2a53a4c3238c 67 }
lorcansmith 0:2a53a4c3238c 68 #endif
lorcansmith 0:2a53a4c3238c 69
lorcansmith 0:2a53a4c3238c 70
lorcansmith 0:2a53a4c3238c 71 PPPErr PPPNetIf::GPRSConnect(const char* apn, const char* userId, const char* password) //Connect using GPRS
lorcansmith 0:2a53a4c3238c 72 {
lorcansmith 0:2a53a4c3238c 73 LwipNetIf::init();
lorcansmith 0:2a53a4c3238c 74 pppInit();
lorcansmith 0:2a53a4c3238c 75 //TODO: Tell ATIf that we get ownership of the serial port
lorcansmith 0:2a53a4c3238c 76
lorcansmith 0:2a53a4c3238c 77 GPRSErr gprsErr;
lorcansmith 0:2a53a4c3238c 78 gprsErr = m_pIf->connect(apn);
lorcansmith 0:2a53a4c3238c 79 if(gprsErr)
lorcansmith 0:2a53a4c3238c 80 return PPP_NETWORK;
lorcansmith 0:2a53a4c3238c 81
lorcansmith 0:2a53a4c3238c 82 DBG("PPPNetIf: If Connected.\n");
lorcansmith 0:2a53a4c3238c 83
lorcansmith 0:2a53a4c3238c 84 if( userId == NULL )
lorcansmith 0:2a53a4c3238c 85 pppSetAuth(PPPAUTHTYPE_NONE, NULL, NULL);
lorcansmith 0:2a53a4c3238c 86 else
lorcansmith 0:2a53a4c3238c 87 pppSetAuth(PPPAUTHTYPE_PAP, userId, password); //TODO: Allow CHAP as well
lorcansmith 0:2a53a4c3238c 88
lorcansmith 0:2a53a4c3238c 89 DBG("PPPNetIf: Set Auth.\n");
lorcansmith 0:2a53a4c3238c 90
lorcansmith 0:2a53a4c3238c 91 //wait(1.);
lorcansmith 0:2a53a4c3238c 92
lorcansmith 0:2a53a4c3238c 93 //m_pIf->flushBuffer(); //Flush buffer before passing serial port to PPP
lorcansmith 0:2a53a4c3238c 94
lorcansmith 0:2a53a4c3238c 95 m_status = PPP_CONNECTING;
lorcansmith 0:2a53a4c3238c 96 DBG("m_pIf = %p\n", m_pIf);
lorcansmith 0:2a53a4c3238c 97 int res = pppOverSerialOpen((void*)m_pIf, sPppCallback, (void*)this);
lorcansmith 0:2a53a4c3238c 98 DBG("PPP connected\n");
lorcansmith 0:2a53a4c3238c 99 if(res<0)
lorcansmith 0:2a53a4c3238c 100 {
lorcansmith 0:2a53a4c3238c 101 disconnect();
lorcansmith 0:2a53a4c3238c 102 return PPP_PROTOCOL;
lorcansmith 0:2a53a4c3238c 103 }
lorcansmith 0:2a53a4c3238c 104
lorcansmith 0:2a53a4c3238c 105 DBG("PPPNetIf: PPP Started with res = %d.\n", res);
lorcansmith 0:2a53a4c3238c 106
lorcansmith 0:2a53a4c3238c 107 m_fd = res;
lorcansmith 0:2a53a4c3238c 108 m_connected = true;
lorcansmith 0:2a53a4c3238c 109 Timer t;
lorcansmith 0:2a53a4c3238c 110 t.start();
lorcansmith 0:2a53a4c3238c 111 while( m_status == PPP_CONNECTING ) //Wait for callback
lorcansmith 0:2a53a4c3238c 112 {
lorcansmith 0:2a53a4c3238c 113 poll();
lorcansmith 0:2a53a4c3238c 114 if(t.read_ms()>PPP_TIMEOUT)
lorcansmith 0:2a53a4c3238c 115 {
lorcansmith 0:2a53a4c3238c 116 DBG("PPPNetIf: Timeout.\n");
lorcansmith 0:2a53a4c3238c 117 disconnect();
lorcansmith 0:2a53a4c3238c 118 return PPP_PROTOCOL;
lorcansmith 0:2a53a4c3238c 119 }
lorcansmith 0:2a53a4c3238c 120 }
lorcansmith 0:2a53a4c3238c 121
lorcansmith 0:2a53a4c3238c 122 DBG("PPPNetIf: Callback returned.\n");
lorcansmith 0:2a53a4c3238c 123
lorcansmith 0:2a53a4c3238c 124 if( m_status == PPP_DISCONNECTED )
lorcansmith 0:2a53a4c3238c 125 {
lorcansmith 0:2a53a4c3238c 126 disconnect();
lorcansmith 0:2a53a4c3238c 127 return PPP_PROTOCOL;
lorcansmith 0:2a53a4c3238c 128 }
lorcansmith 0:2a53a4c3238c 129
lorcansmith 0:2a53a4c3238c 130 return PPP_OK;
lorcansmith 0:2a53a4c3238c 131
lorcansmith 0:2a53a4c3238c 132 }
lorcansmith 0:2a53a4c3238c 133
lorcansmith 0:2a53a4c3238c 134 PPPErr PPPNetIf::ATConnect(const char* number) //Connect using a "classic" voice modem or GSM
lorcansmith 0:2a53a4c3238c 135 {
lorcansmith 0:2a53a4c3238c 136 //TODO: IMPL
lorcansmith 0:2a53a4c3238c 137 return PPP_MODEM;
lorcansmith 0:2a53a4c3238c 138 }
lorcansmith 0:2a53a4c3238c 139
lorcansmith 0:2a53a4c3238c 140 PPPErr PPPNetIf::disconnect()
lorcansmith 0:2a53a4c3238c 141 {
lorcansmith 0:2a53a4c3238c 142 if(m_fd)
lorcansmith 0:2a53a4c3238c 143 pppClose(m_fd); //0 if ok, else should gen a WARN
lorcansmith 0:2a53a4c3238c 144 m_connected = false;
lorcansmith 0:2a53a4c3238c 145
lorcansmith 0:2a53a4c3238c 146 m_pIf->flushBuffer();
lorcansmith 0:2a53a4c3238c 147 m_pIf->printf("+++\r\n");
lorcansmith 0:2a53a4c3238c 148 wait(.5);
lorcansmith 0:2a53a4c3238c 149 m_pIf->flushBuffer();
lorcansmith 0:2a53a4c3238c 150
lorcansmith 0:2a53a4c3238c 151 GPRSErr gprsErr;
lorcansmith 0:2a53a4c3238c 152 gprsErr = m_pIf->disconnect();
lorcansmith 0:2a53a4c3238c 153 if(gprsErr)
lorcansmith 0:2a53a4c3238c 154 return PPP_NETWORK;
lorcansmith 0:2a53a4c3238c 155
lorcansmith 0:2a53a4c3238c 156 return PPP_OK;
lorcansmith 0:2a53a4c3238c 157 }
lorcansmith 0:2a53a4c3238c 158
lorcansmith 0:2a53a4c3238c 159 #if 0
lorcansmith 0:2a53a4c3238c 160 PPPErr PPPNetIf::close()
lorcansmith 0:2a53a4c3238c 161 {
lorcansmith 0:2a53a4c3238c 162 GPRSErr err = m_pIf->close();
lorcansmith 0:2a53a4c3238c 163 if(err)
lorcansmith 0:2a53a4c3238c 164 return PPP_MODEM;
lorcansmith 0:2a53a4c3238c 165 m_open = false;
lorcansmith 0:2a53a4c3238c 166 return PPP_OK;
lorcansmith 0:2a53a4c3238c 167 }
lorcansmith 0:2a53a4c3238c 168 #endif
lorcansmith 0:2a53a4c3238c 169
lorcansmith 0:2a53a4c3238c 170
lorcansmith 0:2a53a4c3238c 171 #if 0
lorcansmith 0:2a53a4c3238c 172 //We have to use :
lorcansmith 0:2a53a4c3238c 173
lorcansmith 0:2a53a4c3238c 174 /** Pass received raw characters to PPPoS to be decoded. This function is
lorcansmith 0:2a53a4c3238c 175 * thread-safe and can be called from a dedicated RX-thread or from a main-loop.
lorcansmith 0:2a53a4c3238c 176 *
lorcansmith 0:2a53a4c3238c 177 * @param pd PPP descriptor index, returned by pppOpen()
lorcansmith 0:2a53a4c3238c 178 * @param data received data
lorcansmith 0:2a53a4c3238c 179 * @param len length of received data
lorcansmith 0:2a53a4c3238c 180 */
lorcansmith 0:2a53a4c3238c 181 void
lorcansmith 0:2a53a4c3238c 182 pppos_input(int pd, u_char* data, int len)
lorcansmith 0:2a53a4c3238c 183 {
lorcansmith 0:2a53a4c3238c 184 pppInProc(&pppControl[pd].rx, data, len);
lorcansmith 0:2a53a4c3238c 185 }
lorcansmith 0:2a53a4c3238c 186 #endif
lorcansmith 0:2a53a4c3238c 187
lorcansmith 0:2a53a4c3238c 188 void PPPNetIf::poll()
lorcansmith 0:2a53a4c3238c 189 {
lorcansmith 0:2a53a4c3238c 190 if(!m_connected)
lorcansmith 0:2a53a4c3238c 191 return;
lorcansmith 0:2a53a4c3238c 192 LwipNetIf::poll();
lorcansmith 0:2a53a4c3238c 193 //static u8_t buf[128];
lorcansmith 0:2a53a4c3238c 194 int len;
lorcansmith 0:2a53a4c3238c 195 do
lorcansmith 0:2a53a4c3238c 196 {
lorcansmith 0:2a53a4c3238c 197 len = sio_tryread((sio_fd_t) m_pIf, m_buf, BUF_SIZE);
lorcansmith 0:2a53a4c3238c 198 if(len > 0)
lorcansmith 0:2a53a4c3238c 199 pppos_input(m_fd, m_buf, len);
lorcansmith 0:2a53a4c3238c 200 } while(len>0);
lorcansmith 0:2a53a4c3238c 201 }
lorcansmith 0:2a53a4c3238c 202
lorcansmith 0:2a53a4c3238c 203 //Link Callback
lorcansmith 0:2a53a4c3238c 204 void PPPNetIf::pppCallback(int errCode, void *arg)
lorcansmith 0:2a53a4c3238c 205 {
lorcansmith 0:2a53a4c3238c 206 switch ( errCode )
lorcansmith 0:2a53a4c3238c 207 {
lorcansmith 0:2a53a4c3238c 208 //No error
lorcansmith 0:2a53a4c3238c 209 case PPPERR_NONE:
lorcansmith 0:2a53a4c3238c 210 {
lorcansmith 0:2a53a4c3238c 211 struct ppp_addrs* addrs = (struct ppp_addrs*) arg;
lorcansmith 0:2a53a4c3238c 212 m_ip = IpAddr(&(addrs->our_ipaddr)); //Set IP
lorcansmith 0:2a53a4c3238c 213 }
lorcansmith 0:2a53a4c3238c 214 m_status = PPP_CONNECTED;
lorcansmith 0:2a53a4c3238c 215 break;
lorcansmith 0:2a53a4c3238c 216 default:
lorcansmith 0:2a53a4c3238c 217 //Disconnected
lorcansmith 0:2a53a4c3238c 218 DBG("PPPNetIf: Callback errCode = %d.\n", errCode);
lorcansmith 0:2a53a4c3238c 219 m_status = PPP_DISCONNECTED;
lorcansmith 0:2a53a4c3238c 220 break;
lorcansmith 0:2a53a4c3238c 221 }
lorcansmith 0:2a53a4c3238c 222 }
lorcansmith 0:2a53a4c3238c 223
lorcansmith 0:2a53a4c3238c 224 #endif