First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 09:11:35 2010 +0000
Revision:
0:55a05330f8cc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darran 0:55a05330f8cc 1
darran 0:55a05330f8cc 2 /*
darran 0:55a05330f8cc 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
darran 0:55a05330f8cc 4
darran 0:55a05330f8cc 5 Permission is hereby granted, free of charge, to any person obtaining a copy
darran 0:55a05330f8cc 6 of this software and associated documentation files (the "Software"), to deal
darran 0:55a05330f8cc 7 in the Software without restriction, including without limitation the rights
darran 0:55a05330f8cc 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
darran 0:55a05330f8cc 9 copies of the Software, and to permit persons to whom the Software is
darran 0:55a05330f8cc 10 furnished to do so, subject to the following conditions:
darran 0:55a05330f8cc 11
darran 0:55a05330f8cc 12 The above copyright notice and this permission notice shall be included in
darran 0:55a05330f8cc 13 all copies or substantial portions of the Software.
darran 0:55a05330f8cc 14
darran 0:55a05330f8cc 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
darran 0:55a05330f8cc 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
darran 0:55a05330f8cc 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
darran 0:55a05330f8cc 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
darran 0:55a05330f8cc 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
darran 0:55a05330f8cc 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
darran 0:55a05330f8cc 21 THE SOFTWARE.
darran 0:55a05330f8cc 22 */
darran 0:55a05330f8cc 23
darran 0:55a05330f8cc 24 #include "PPPNetIf.h"
darran 0:55a05330f8cc 25 #include "mbed.h"
darran 0:55a05330f8cc 26 #include "ppp/ppp.h"
darran 0:55a05330f8cc 27 #include "lwip/init.h"
darran 0:55a05330f8cc 28 #include "lwip/sio.h"
darran 0:55a05330f8cc 29
darran 0:55a05330f8cc 30 #define __DEBUG
darran 0:55a05330f8cc 31 #include "dbg/dbg.h"
darran 0:55a05330f8cc 32
darran 0:55a05330f8cc 33 #include "netCfg.h"
darran 0:55a05330f8cc 34 #if NET_PPP
darran 0:55a05330f8cc 35
darran 0:55a05330f8cc 36 #define PPP_TIMEOUT 60000
darran 0:55a05330f8cc 37
darran 0:55a05330f8cc 38 #define BUF_SIZE 128
darran 0:55a05330f8cc 39
darran 0:55a05330f8cc 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)
darran 0:55a05330f8cc 41 {
darran 0:55a05330f8cc 42 //FIXME: Check static refcount
darran 0:55a05330f8cc 43 pppInit();
darran 0:55a05330f8cc 44 m_buf = new uint8_t[BUF_SIZE];
darran 0:55a05330f8cc 45 }
darran 0:55a05330f8cc 46
darran 0:55a05330f8cc 47 PPPNetIf::~PPPNetIf()
darran 0:55a05330f8cc 48 {
darran 0:55a05330f8cc 49 delete[] m_buf;
darran 0:55a05330f8cc 50 }
darran 0:55a05330f8cc 51
darran 0:55a05330f8cc 52 #if 0
darran 0:55a05330f8cc 53 PPPErr PPPNetIf::open(Serial* pSerial)
darran 0:55a05330f8cc 54 {
darran 0:55a05330f8cc 55 GPRSErr err = m_pIf->open(pSerial);
darran 0:55a05330f8cc 56 if(err)
darran 0:55a05330f8cc 57 return PPP_MODEM;
darran 0:55a05330f8cc 58 m_open = true;
darran 0:55a05330f8cc 59 #if 0
darran 0:55a05330f8cc 60 m_id = sioMgr::registerSerialIf(this);
darran 0:55a05330f8cc 61 if(!m_id)
darran 0:55a05330f8cc 62 {
darran 0:55a05330f8cc 63 close();
darran 0:55a05330f8cc 64 return PPP_CLOSED;
darran 0:55a05330f8cc 65 }
darran 0:55a05330f8cc 66 #endif
darran 0:55a05330f8cc 67 return PPP_OK;
darran 0:55a05330f8cc 68 }
darran 0:55a05330f8cc 69 #endif
darran 0:55a05330f8cc 70
darran 0:55a05330f8cc 71
darran 0:55a05330f8cc 72 PPPErr PPPNetIf::GPRSConnect(const char* apn, const char* userId, const char* password) //Connect using GPRS
darran 0:55a05330f8cc 73 {
darran 0:55a05330f8cc 74 LwipNetIf::init();
darran 0:55a05330f8cc 75 //TODO: Tell ATIf that we get ownership of the serial port
darran 0:55a05330f8cc 76
darran 0:55a05330f8cc 77 GPRSErr gprsErr;
darran 0:55a05330f8cc 78 gprsErr = m_pIf->connect(apn);
darran 0:55a05330f8cc 79 if(gprsErr)
darran 0:55a05330f8cc 80 return PPP_NETWORK;
darran 0:55a05330f8cc 81
darran 0:55a05330f8cc 82 DBG("\r\nPPPNetIf: If Connected.\r\n");
darran 0:55a05330f8cc 83
darran 0:55a05330f8cc 84 if( userId == NULL )
darran 0:55a05330f8cc 85 pppSetAuth(PPPAUTHTYPE_NONE, NULL, NULL);
darran 0:55a05330f8cc 86 else
darran 0:55a05330f8cc 87 pppSetAuth(PPPAUTHTYPE_PAP, userId, password); //TODO: Allow CHAP as well
darran 0:55a05330f8cc 88
darran 0:55a05330f8cc 89 DBG("\r\nPPPNetIf: Set Auth.\r\n");
darran 0:55a05330f8cc 90
darran 0:55a05330f8cc 91 m_pIf->flushBuffer(); //Flush buffer before passing serial port to PPP
darran 0:55a05330f8cc 92
darran 0:55a05330f8cc 93 m_status = PPP_CONNECTING;
darran 0:55a05330f8cc 94 int res = pppOverSerialOpen((void*)m_pIf, sPppCallback, (void*)this);
darran 0:55a05330f8cc 95 if(res<0)
darran 0:55a05330f8cc 96 {
darran 0:55a05330f8cc 97 disconnect();
darran 0:55a05330f8cc 98 return PPP_PROTOCOL;
darran 0:55a05330f8cc 99 }
darran 0:55a05330f8cc 100
darran 0:55a05330f8cc 101 DBG("\r\nPPPNetIf: PPP Started with res = %d.\r\n", res);
darran 0:55a05330f8cc 102
darran 0:55a05330f8cc 103 m_fd = res;
darran 0:55a05330f8cc 104 m_connected = true;
darran 0:55a05330f8cc 105 Timer t;
darran 0:55a05330f8cc 106 t.start();
darran 0:55a05330f8cc 107 while( m_status == PPP_CONNECTING ) //Wait for callback
darran 0:55a05330f8cc 108 {
darran 0:55a05330f8cc 109 poll();
darran 0:55a05330f8cc 110 if(t.read_ms()>PPP_TIMEOUT)
darran 0:55a05330f8cc 111 {
darran 0:55a05330f8cc 112 DBG("\r\nPPPNetIf: Timeout.\r\n");
darran 0:55a05330f8cc 113 disconnect();
darran 0:55a05330f8cc 114 return PPP_PROTOCOL;
darran 0:55a05330f8cc 115 }
darran 0:55a05330f8cc 116 }
darran 0:55a05330f8cc 117
darran 0:55a05330f8cc 118 DBG("\r\nPPPNetIf: Callback returned.\r\n");
darran 0:55a05330f8cc 119
darran 0:55a05330f8cc 120 if( m_status == PPP_DISCONNECTED )
darran 0:55a05330f8cc 121 {
darran 0:55a05330f8cc 122 disconnect();
darran 0:55a05330f8cc 123 return PPP_PROTOCOL;
darran 0:55a05330f8cc 124 }
darran 0:55a05330f8cc 125
darran 0:55a05330f8cc 126 return PPP_OK;
darran 0:55a05330f8cc 127
darran 0:55a05330f8cc 128 }
darran 0:55a05330f8cc 129
darran 0:55a05330f8cc 130 PPPErr PPPNetIf::ATConnect(const char* number) //Connect using a "classic" voice modem or GSM
darran 0:55a05330f8cc 131 {
darran 0:55a05330f8cc 132 //TODO: IMPL
darran 0:55a05330f8cc 133 return PPP_MODEM;
darran 0:55a05330f8cc 134 }
darran 0:55a05330f8cc 135
darran 0:55a05330f8cc 136 PPPErr PPPNetIf::disconnect()
darran 0:55a05330f8cc 137 {
darran 0:55a05330f8cc 138 pppClose(m_fd); //0 if ok, else should gen a WARN
darran 0:55a05330f8cc 139 m_connected = false;
darran 0:55a05330f8cc 140
darran 0:55a05330f8cc 141 m_pIf->flushBuffer();
darran 0:55a05330f8cc 142 m_pIf->printf("+++");
darran 0:55a05330f8cc 143 wait(.1);
darran 0:55a05330f8cc 144 m_pIf->flushBuffer();
darran 0:55a05330f8cc 145
darran 0:55a05330f8cc 146 GPRSErr gprsErr;
darran 0:55a05330f8cc 147 gprsErr = m_pIf->disconnect();
darran 0:55a05330f8cc 148 if(gprsErr)
darran 0:55a05330f8cc 149 return PPP_NETWORK;
darran 0:55a05330f8cc 150
darran 0:55a05330f8cc 151 return PPP_OK;
darran 0:55a05330f8cc 152 }
darran 0:55a05330f8cc 153
darran 0:55a05330f8cc 154 #if 0
darran 0:55a05330f8cc 155 PPPErr PPPNetIf::close()
darran 0:55a05330f8cc 156 {
darran 0:55a05330f8cc 157 GPRSErr err = m_pIf->close();
darran 0:55a05330f8cc 158 if(err)
darran 0:55a05330f8cc 159 return PPP_MODEM;
darran 0:55a05330f8cc 160 m_open = false;
darran 0:55a05330f8cc 161 return PPP_OK;
darran 0:55a05330f8cc 162 }
darran 0:55a05330f8cc 163 #endif
darran 0:55a05330f8cc 164
darran 0:55a05330f8cc 165
darran 0:55a05330f8cc 166 #if 0
darran 0:55a05330f8cc 167 //We have to use :
darran 0:55a05330f8cc 168
darran 0:55a05330f8cc 169 /** Pass received raw characters to PPPoS to be decoded. This function is
darran 0:55a05330f8cc 170 * thread-safe and can be called from a dedicated RX-thread or from a main-loop.
darran 0:55a05330f8cc 171 *
darran 0:55a05330f8cc 172 * @param pd PPP descriptor index, returned by pppOpen()
darran 0:55a05330f8cc 173 * @param data received data
darran 0:55a05330f8cc 174 * @param len length of received data
darran 0:55a05330f8cc 175 */
darran 0:55a05330f8cc 176 void
darran 0:55a05330f8cc 177 pppos_input(int pd, u_char* data, int len)
darran 0:55a05330f8cc 178 {
darran 0:55a05330f8cc 179 pppInProc(&pppControl[pd].rx, data, len);
darran 0:55a05330f8cc 180 }
darran 0:55a05330f8cc 181 #endif
darran 0:55a05330f8cc 182
darran 0:55a05330f8cc 183 void PPPNetIf::poll()
darran 0:55a05330f8cc 184 {
darran 0:55a05330f8cc 185 if(!m_connected)
darran 0:55a05330f8cc 186 return;
darran 0:55a05330f8cc 187 LwipNetIf::poll();
darran 0:55a05330f8cc 188 //static u8_t buf[128];
darran 0:55a05330f8cc 189 int len = sio_tryread((sio_fd_t) m_pIf, m_buf, BUF_SIZE);
darran 0:55a05330f8cc 190 if(len > 0)
darran 0:55a05330f8cc 191 pppos_input(m_fd, m_buf, len);
darran 0:55a05330f8cc 192 }
darran 0:55a05330f8cc 193
darran 0:55a05330f8cc 194 //Link Callback
darran 0:55a05330f8cc 195 void PPPNetIf::pppCallback(int errCode, void *arg)
darran 0:55a05330f8cc 196 {
darran 0:55a05330f8cc 197 //DBG("\r\nPPPNetIf: Callback errCode = %d.\r\n", errCode);
darran 0:55a05330f8cc 198 switch ( errCode )
darran 0:55a05330f8cc 199 {
darran 0:55a05330f8cc 200 //No error
darran 0:55a05330f8cc 201 case PPPERR_NONE:
darran 0:55a05330f8cc 202 m_status = PPP_CONNECTED;
darran 0:55a05330f8cc 203 break;
darran 0:55a05330f8cc 204 default:
darran 0:55a05330f8cc 205 //Disconnected
darran 0:55a05330f8cc 206 m_status = PPP_DISCONNECTED;
darran 0:55a05330f8cc 207 break;
darran 0:55a05330f8cc 208 }
darran 0:55a05330f8cc 209 }
darran 0:55a05330f8cc 210
darran 0:55a05330f8cc 211 #endif