Changes to support Vodafone K4606
Dependencies: Socket USBHostWANDongle lwip-sys lwip
Fork of VodafoneUSBModem by
ip/PPPIPInterface.cpp@8:04b6a042595f, 2012-06-26 (annotated)
- Committer:
- donatien
- Date:
- Tue Jun 26 13:44:59 2012 +0000
- Revision:
- 8:04b6a042595f
- Child:
- 9:3f077dde13c9
- Child:
- 10:21a6f09d5631
Bleeding edge - test with two different interfaces
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 8:04b6a042595f | 1 | /* PPPIPInterface.cpp */ |
donatien | 8:04b6a042595f | 2 | /* |
donatien | 8:04b6a042595f | 3 | Copyright (C) 2012 ARM Limited. |
donatien | 8:04b6a042595f | 4 | |
donatien | 8:04b6a042595f | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
donatien | 8:04b6a042595f | 6 | this software and associated documentation files (the "Software"), to deal in |
donatien | 8:04b6a042595f | 7 | the Software without restriction, including without limitation the rights to |
donatien | 8:04b6a042595f | 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
donatien | 8:04b6a042595f | 9 | of the Software, and to permit persons to whom the Software is furnished to do |
donatien | 8:04b6a042595f | 10 | so, subject to the following conditions: |
donatien | 8:04b6a042595f | 11 | |
donatien | 8:04b6a042595f | 12 | The above copyright notice and this permission notice shall be included in all |
donatien | 8:04b6a042595f | 13 | copies or substantial portions of the Software. |
donatien | 8:04b6a042595f | 14 | |
donatien | 8:04b6a042595f | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 8:04b6a042595f | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 8:04b6a042595f | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 8:04b6a042595f | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 8:04b6a042595f | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 8:04b6a042595f | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
donatien | 8:04b6a042595f | 21 | SOFTWARE. |
donatien | 8:04b6a042595f | 22 | */ |
donatien | 8:04b6a042595f | 23 | |
donatien | 8:04b6a042595f | 24 | #define __DEBUG__ 2 //Maximum verbosity |
donatien | 8:04b6a042595f | 25 | #ifndef __MODULE__ |
donatien | 8:04b6a042595f | 26 | #define __MODULE__ "PPPIPInterface.cpp" |
donatien | 8:04b6a042595f | 27 | #endif |
donatien | 8:04b6a042595f | 28 | |
donatien | 8:04b6a042595f | 29 | #include "core/fwk.h" |
donatien | 8:04b6a042595f | 30 | #include "rtos.h" |
donatien | 8:04b6a042595f | 31 | |
donatien | 8:04b6a042595f | 32 | #include "PPPIPInterface.h" |
donatien | 8:04b6a042595f | 33 | |
donatien | 8:04b6a042595f | 34 | extern "C" { |
donatien | 8:04b6a042595f | 35 | #include "lwip/ip_addr.h" |
donatien | 8:04b6a042595f | 36 | #include "lwip/inet.h" |
donatien | 8:04b6a042595f | 37 | #include "netif/ppp/ppp.h" |
donatien | 8:04b6a042595f | 38 | } |
donatien | 8:04b6a042595f | 39 | |
donatien | 8:04b6a042595f | 40 | #if NET_PPP |
donatien | 8:04b6a042595f | 41 | |
donatien | 8:04b6a042595f | 42 | PPPIPInterface::PPPIPInterface(IOStream* pStream) : LwIPInterface(), m_linkStatusSphre(1), m_pppErrCode(0), m_pStream(pStream), m_streamAvail(true), m_pppd(-1) |
donatien | 8:04b6a042595f | 43 | { |
donatien | 8:04b6a042595f | 44 | m_linkStatusSphre.wait(); |
donatien | 8:04b6a042595f | 45 | } |
donatien | 8:04b6a042595f | 46 | |
donatien | 8:04b6a042595f | 47 | /*virtual*/ PPPIPInterface::~PPPIPInterface() |
donatien | 8:04b6a042595f | 48 | { |
donatien | 8:04b6a042595f | 49 | |
donatien | 8:04b6a042595f | 50 | } |
donatien | 8:04b6a042595f | 51 | |
donatien | 8:04b6a042595f | 52 | /*virtual*/ int PPPIPInterface::init() //Init PPP-specific stuff, create the right bindings, etc |
donatien | 8:04b6a042595f | 53 | { |
donatien | 8:04b6a042595f | 54 | DBG("Initializing LwIP"); |
donatien | 8:04b6a042595f | 55 | LwIPInterface::init(); //Init LwIP, NOT including PPP |
donatien | 8:04b6a042595f | 56 | DBG("Initializing PPP"); |
donatien | 8:04b6a042595f | 57 | pppInit(); |
donatien | 8:04b6a042595f | 58 | DBG("Done"); |
donatien | 8:04b6a042595f | 59 | return OK; |
donatien | 8:04b6a042595f | 60 | } |
donatien | 8:04b6a042595f | 61 | |
donatien | 8:04b6a042595f | 62 | int PPPIPInterface::setup(const char* user, const char* pw) |
donatien | 8:04b6a042595f | 63 | { |
donatien | 8:04b6a042595f | 64 | DBG("Configuring PPP authentication method"); |
donatien | 8:04b6a042595f | 65 | pppSetAuth(PPPAUTHTYPE_ANY, user, pw); |
donatien | 8:04b6a042595f | 66 | DBG("Done"); |
donatien | 8:04b6a042595f | 67 | return OK; |
donatien | 8:04b6a042595f | 68 | } |
donatien | 8:04b6a042595f | 69 | |
donatien | 8:04b6a042595f | 70 | /*virtual*/ int PPPIPInterface::connect() |
donatien | 8:04b6a042595f | 71 | { |
donatien | 8:04b6a042595f | 72 | DBG("Trying to connect with PPP"); |
donatien | 8:04b6a042595f | 73 | m_linkStatusSphre.wait(0); |
donatien | 8:04b6a042595f | 74 | if((m_pppd != -1) && (m_pppErrCode == 0)) //Already connected |
donatien | 8:04b6a042595f | 75 | { |
donatien | 8:04b6a042595f | 76 | return NET_INVALID; |
donatien | 8:04b6a042595f | 77 | } |
donatien | 8:04b6a042595f | 78 | int ret = pppOverSerialOpen(this, PPPIPInterface::linkStatusCb, this); |
donatien | 8:04b6a042595f | 79 | if(ret < 0) |
donatien | 8:04b6a042595f | 80 | { |
donatien | 8:04b6a042595f | 81 | switch(ret) |
donatien | 8:04b6a042595f | 82 | { |
donatien | 8:04b6a042595f | 83 | case PPPERR_OPEN: |
donatien | 8:04b6a042595f | 84 | default: |
donatien | 8:04b6a042595f | 85 | return NET_FULL; //All available resources are already used |
donatien | 8:04b6a042595f | 86 | } |
donatien | 8:04b6a042595f | 87 | } |
donatien | 8:04b6a042595f | 88 | m_pppd = ret; //PPP descriptor |
donatien | 8:04b6a042595f | 89 | m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there |
donatien | 8:04b6a042595f | 90 | if(m_pppErrCode != PPPERR_NONE) |
donatien | 8:04b6a042595f | 91 | { |
donatien | 8:04b6a042595f | 92 | m_pppd = -1; |
donatien | 8:04b6a042595f | 93 | } |
donatien | 8:04b6a042595f | 94 | switch(m_pppErrCode) |
donatien | 8:04b6a042595f | 95 | { |
donatien | 8:04b6a042595f | 96 | case PPPERR_NONE: //Connected OK |
donatien | 8:04b6a042595f | 97 | return OK; |
donatien | 8:04b6a042595f | 98 | case PPPERR_CONNECT: //Connection lost |
donatien | 8:04b6a042595f | 99 | return NET_INTERRUPTED; |
donatien | 8:04b6a042595f | 100 | case PPPERR_AUTHFAIL: //Authentication failed |
donatien | 8:04b6a042595f | 101 | return NET_AUTH; |
donatien | 8:04b6a042595f | 102 | case PPPERR_PROTOCOL: //Protocol error |
donatien | 8:04b6a042595f | 103 | return NET_PROTOCOL; |
donatien | 8:04b6a042595f | 104 | default: |
donatien | 8:04b6a042595f | 105 | return NET_UNKNOWN; |
donatien | 8:04b6a042595f | 106 | } |
donatien | 8:04b6a042595f | 107 | } |
donatien | 8:04b6a042595f | 108 | |
donatien | 8:04b6a042595f | 109 | /*virtual*/ int PPPIPInterface::disconnect() |
donatien | 8:04b6a042595f | 110 | { |
donatien | 8:04b6a042595f | 111 | int ret = m_linkStatusSphre.wait(0); |
donatien | 8:04b6a042595f | 112 | if(ret > 0) //Already disconnected? |
donatien | 8:04b6a042595f | 113 | { |
donatien | 8:04b6a042595f | 114 | m_pppd = -1; //Discard PPP descriptor |
donatien | 8:04b6a042595f | 115 | switch(m_pppErrCode) |
donatien | 8:04b6a042595f | 116 | { |
donatien | 8:04b6a042595f | 117 | case PPPERR_CONNECT: //Connection terminated |
donatien | 8:04b6a042595f | 118 | case PPPERR_AUTHFAIL: //Authentication failed |
donatien | 8:04b6a042595f | 119 | case PPPERR_PROTOCOL: //Protocol error |
donatien | 8:04b6a042595f | 120 | case PPPERR_USER: |
donatien | 8:04b6a042595f | 121 | return OK; |
donatien | 8:04b6a042595f | 122 | default: |
donatien | 8:04b6a042595f | 123 | return NET_UNKNOWN; |
donatien | 8:04b6a042595f | 124 | } |
donatien | 8:04b6a042595f | 125 | } |
donatien | 8:04b6a042595f | 126 | else |
donatien | 8:04b6a042595f | 127 | { |
donatien | 8:04b6a042595f | 128 | if(m_pppd == -1) |
donatien | 8:04b6a042595f | 129 | { |
donatien | 8:04b6a042595f | 130 | return NET_INVALID; |
donatien | 8:04b6a042595f | 131 | } |
donatien | 8:04b6a042595f | 132 | pppClose(m_pppd); |
donatien | 8:04b6a042595f | 133 | do |
donatien | 8:04b6a042595f | 134 | { |
donatien | 8:04b6a042595f | 135 | m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there |
donatien | 8:04b6a042595f | 136 | DBG("Received PPP err code %d", m_pppErrCode); |
donatien | 8:04b6a042595f | 137 | } while(m_pppErrCode != PPPERR_USER); |
donatien | 8:04b6a042595f | 138 | m_pppd = -1; //Discard PPP descriptor |
donatien | 8:04b6a042595f | 139 | } |
donatien | 8:04b6a042595f | 140 | return OK; |
donatien | 8:04b6a042595f | 141 | } |
donatien | 8:04b6a042595f | 142 | |
donatien | 8:04b6a042595f | 143 | /*static*/ void PPPIPInterface::linkStatusCb(void *ctx, int errCode, void *arg) //PPP link status |
donatien | 8:04b6a042595f | 144 | { |
donatien | 8:04b6a042595f | 145 | PPPIPInterface* pIf = (PPPIPInterface*)ctx; |
donatien | 8:04b6a042595f | 146 | struct ppp_addrs* addrs = (struct ppp_addrs*) arg; |
donatien | 8:04b6a042595f | 147 | |
donatien | 8:04b6a042595f | 148 | switch(errCode) |
donatien | 8:04b6a042595f | 149 | { |
donatien | 8:04b6a042595f | 150 | case PPPERR_NONE: |
donatien | 8:04b6a042595f | 151 | WARN("Connected via PPP."); |
donatien | 8:04b6a042595f | 152 | DBG("Local IP address: %s", inet_ntoa(addrs->our_ipaddr)); |
donatien | 8:04b6a042595f | 153 | DBG("Netmask: %s", inet_ntoa(addrs->netmask)); |
donatien | 8:04b6a042595f | 154 | DBG("Remote IP address: %s", inet_ntoa(addrs->his_ipaddr)); |
donatien | 8:04b6a042595f | 155 | DBG("Primary DNS: %s", inet_ntoa(addrs->dns1)); |
donatien | 8:04b6a042595f | 156 | DBG("Secondary DNS: %s", inet_ntoa(addrs->dns2)); |
donatien | 8:04b6a042595f | 157 | setConnected(true); |
donatien | 8:04b6a042595f | 158 | setIPAddress(inet_ntoa(addrs->our_ipaddr)); |
donatien | 8:04b6a042595f | 159 | break; |
donatien | 8:04b6a042595f | 160 | case PPPERR_CONNECT: //Connection lost |
donatien | 8:04b6a042595f | 161 | WARN("Connection lost/terminated"); |
donatien | 8:04b6a042595f | 162 | setConnected(false); |
donatien | 8:04b6a042595f | 163 | break; |
donatien | 8:04b6a042595f | 164 | case PPPERR_AUTHFAIL: //Authentication failed |
donatien | 8:04b6a042595f | 165 | WARN("Authentication failed"); |
donatien | 8:04b6a042595f | 166 | setConnected(false); |
donatien | 8:04b6a042595f | 167 | break; |
donatien | 8:04b6a042595f | 168 | case PPPERR_PROTOCOL: //Protocol error |
donatien | 8:04b6a042595f | 169 | WARN("Protocol error"); |
donatien | 8:04b6a042595f | 170 | setConnected(false); |
donatien | 8:04b6a042595f | 171 | break; |
donatien | 8:04b6a042595f | 172 | case PPPERR_USER: |
donatien | 8:04b6a042595f | 173 | WARN("Disconnected by user"); |
donatien | 8:04b6a042595f | 174 | setConnected(false); |
donatien | 8:04b6a042595f | 175 | break; |
donatien | 8:04b6a042595f | 176 | default: |
donatien | 8:04b6a042595f | 177 | WARN("Unknown error (%d)", errCode); |
donatien | 8:04b6a042595f | 178 | setConnected(false); |
donatien | 8:04b6a042595f | 179 | break; |
donatien | 8:04b6a042595f | 180 | } |
donatien | 8:04b6a042595f | 181 | |
donatien | 8:04b6a042595f | 182 | pIf->m_linkStatusSphre.wait(0); //If previous event has not been handled, "delete" it now |
donatien | 8:04b6a042595f | 183 | pIf->m_pppErrCode = errCode; |
donatien | 8:04b6a042595f | 184 | pIf->m_linkStatusSphre.release(); |
donatien | 8:04b6a042595f | 185 | } |
donatien | 8:04b6a042595f | 186 | |
donatien | 8:04b6a042595f | 187 | //LwIP PPP implementation |
donatien | 8:04b6a042595f | 188 | extern "C" |
donatien | 8:04b6a042595f | 189 | { |
donatien | 8:04b6a042595f | 190 | |
donatien | 8:04b6a042595f | 191 | /** |
donatien | 8:04b6a042595f | 192 | * Writes to the serial device. |
donatien | 8:04b6a042595f | 193 | * |
donatien | 8:04b6a042595f | 194 | * @param fd serial device handle |
donatien | 8:04b6a042595f | 195 | * @param data pointer to data to send |
donatien | 8:04b6a042595f | 196 | * @param len length (in bytes) of data to send |
donatien | 8:04b6a042595f | 197 | * @return number of bytes actually sent |
donatien | 8:04b6a042595f | 198 | * |
donatien | 8:04b6a042595f | 199 | * @note This function will block until all data can be sent. |
donatien | 8:04b6a042595f | 200 | */ |
donatien | 8:04b6a042595f | 201 | u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len) |
donatien | 8:04b6a042595f | 202 | { |
donatien | 8:04b6a042595f | 203 | DBG("sio_write"); |
donatien | 8:04b6a042595f | 204 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
donatien | 8:04b6a042595f | 205 | int ret; |
donatien | 8:04b6a042595f | 206 | if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further |
donatien | 8:04b6a042595f | 207 | { |
donatien | 8:04b6a042595f | 208 | return 0; |
donatien | 8:04b6a042595f | 209 | } |
donatien | 8:04b6a042595f | 210 | ret = pIf->m_pStream->write(data, len, osWaitForever); //Blocks until all data is sent or an error happens |
donatien | 8:04b6a042595f | 211 | if(ret != OK) |
donatien | 8:04b6a042595f | 212 | { |
donatien | 8:04b6a042595f | 213 | return 0; |
donatien | 8:04b6a042595f | 214 | } |
donatien | 8:04b6a042595f | 215 | return len; |
donatien | 8:04b6a042595f | 216 | } |
donatien | 8:04b6a042595f | 217 | |
donatien | 8:04b6a042595f | 218 | /** |
donatien | 8:04b6a042595f | 219 | * Reads from the serial device. |
donatien | 8:04b6a042595f | 220 | * |
donatien | 8:04b6a042595f | 221 | * @param fd serial device handle |
donatien | 8:04b6a042595f | 222 | * @param data pointer to data buffer for receiving |
donatien | 8:04b6a042595f | 223 | * @param len maximum length (in bytes) of data to receive |
donatien | 8:04b6a042595f | 224 | * @return number of bytes actually received - may be 0 if aborted by sio_read_abort |
donatien | 8:04b6a042595f | 225 | * |
donatien | 8:04b6a042595f | 226 | * @note This function will block until data can be received. The blocking |
donatien | 8:04b6a042595f | 227 | * can be cancelled by calling sio_read_abort(). |
donatien | 8:04b6a042595f | 228 | */ |
donatien | 8:04b6a042595f | 229 | u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) |
donatien | 8:04b6a042595f | 230 | { |
donatien | 8:04b6a042595f | 231 | DBG("sio_read"); |
donatien | 8:04b6a042595f | 232 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
donatien | 8:04b6a042595f | 233 | int ret; |
donatien | 8:04b6a042595f | 234 | size_t readLen; |
donatien | 8:04b6a042595f | 235 | if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further |
donatien | 8:04b6a042595f | 236 | { |
donatien | 8:04b6a042595f | 237 | WARN("EXIT NOT AVAIL"); |
donatien | 8:04b6a042595f | 238 | return 0; |
donatien | 8:04b6a042595f | 239 | } |
donatien | 8:04b6a042595f | 240 | ret = pIf->m_pStream->read(data, &readLen, len, osWaitForever); //Blocks until some data is received or an error happens |
donatien | 8:04b6a042595f | 241 | if(ret != OK) |
donatien | 8:04b6a042595f | 242 | { |
donatien | 8:04b6a042595f | 243 | return 0; |
donatien | 8:04b6a042595f | 244 | } |
donatien | 8:04b6a042595f | 245 | DBG("ret"); |
donatien | 8:04b6a042595f | 246 | return readLen; |
donatien | 8:04b6a042595f | 247 | } |
donatien | 8:04b6a042595f | 248 | |
donatien | 8:04b6a042595f | 249 | /** |
donatien | 8:04b6a042595f | 250 | * Aborts a blocking sio_read() call. |
donatien | 8:04b6a042595f | 251 | * |
donatien | 8:04b6a042595f | 252 | * @param fd serial device handle |
donatien | 8:04b6a042595f | 253 | */ |
donatien | 8:04b6a042595f | 254 | void sio_read_abort(sio_fd_t fd) |
donatien | 8:04b6a042595f | 255 | { |
donatien | 8:04b6a042595f | 256 | DBG("sio_read_abort"); |
donatien | 8:04b6a042595f | 257 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
donatien | 8:04b6a042595f | 258 | if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further |
donatien | 8:04b6a042595f | 259 | { |
donatien | 8:04b6a042595f | 260 | return; |
donatien | 8:04b6a042595f | 261 | } |
donatien | 8:04b6a042595f | 262 | pIf->m_pStream->abortRead(); |
donatien | 8:04b6a042595f | 263 | DBG("ret"); |
donatien | 8:04b6a042595f | 264 | } |
donatien | 8:04b6a042595f | 265 | |
donatien | 8:04b6a042595f | 266 | } |
donatien | 8:04b6a042595f | 267 | |
donatien | 8:04b6a042595f | 268 | #endif |
donatien | 8:04b6a042595f | 269 |