Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Wed Dec 15 18:01:30 2010 +0000
Revision:
7:4e2468d7d5cb
Parent:
0:ac1725ba162c

        

Who changed what in which revision?

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