NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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