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