Changes to support Vodafone K4606
Dependencies: Socket USBHostWANDongle lwip-sys lwip
Fork of VodafoneUSBModem by
ip/PPPIPInterface.cpp@18:1789a11d1892, 2012-07-30 (annotated)
- Committer:
- donatien
- Date:
- Mon Jul 30 13:51:51 2012 +0000
- Revision:
- 18:1789a11d1892
- Parent:
- 12:66dc2c8eba2d
- Child:
- 22:06fb2a93a1f6
Vodafone K3772-Z support!
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 | 12:66dc2c8eba2d | 24 | #define __DEBUG__ 0 //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 | 18:1789a11d1892 | 34 | #define MSISDN "*99#" |
donatien | 18:1789a11d1892 | 35 | |
donatien | 18:1789a11d1892 | 36 | #define CONNECT_CMD "ATD " MSISDN "\x0D" |
donatien | 18:1789a11d1892 | 37 | #define EXPECTED_RESP CONNECT_CMD "\x0D" "\x0A" "CONNECT" "\x0D" "\x0A" |
donatien | 18:1789a11d1892 | 38 | #define EXPECTED_RESP_DATARATE CONNECT_CMD "\x0D" "\x0A" "CONNECT %d" "\x0D" "\x0A" |
donatien | 18:1789a11d1892 | 39 | #define EXPECTED_RESP_MIN_LEN 20 |
donatien | 18:1789a11d1892 | 40 | #define OK_RESP "\x0D" "\x0A" "OK" "\x0D" "\x0A" |
donatien | 18:1789a11d1892 | 41 | #define ESCAPE_SEQ "+++" |
donatien | 18:1789a11d1892 | 42 | #define HANGUP_CMD "ATH" "\x0D" |
donatien | 18:1789a11d1892 | 43 | #define NO_CARRIER_RESP "\x0D" "\x0A" "NO CARRIER" "\x0D" "\x0A" |
donatien | 8:04b6a042595f | 44 | extern "C" { |
donatien | 8:04b6a042595f | 45 | #include "lwip/ip_addr.h" |
donatien | 8:04b6a042595f | 46 | #include "lwip/inet.h" |
donatien | 8:04b6a042595f | 47 | #include "netif/ppp/ppp.h" |
donatien | 8:04b6a042595f | 48 | } |
donatien | 8:04b6a042595f | 49 | |
donatien | 8:04b6a042595f | 50 | PPPIPInterface::PPPIPInterface(IOStream* pStream) : LwIPInterface(), m_linkStatusSphre(1), m_pppErrCode(0), m_pStream(pStream), m_streamAvail(true), m_pppd(-1) |
donatien | 8:04b6a042595f | 51 | { |
donatien | 8:04b6a042595f | 52 | m_linkStatusSphre.wait(); |
donatien | 8:04b6a042595f | 53 | } |
donatien | 8:04b6a042595f | 54 | |
donatien | 8:04b6a042595f | 55 | /*virtual*/ PPPIPInterface::~PPPIPInterface() |
donatien | 8:04b6a042595f | 56 | { |
donatien | 8:04b6a042595f | 57 | |
donatien | 8:04b6a042595f | 58 | } |
donatien | 8:04b6a042595f | 59 | |
donatien | 8:04b6a042595f | 60 | /*virtual*/ int PPPIPInterface::init() //Init PPP-specific stuff, create the right bindings, etc |
donatien | 8:04b6a042595f | 61 | { |
donatien | 8:04b6a042595f | 62 | DBG("Initializing LwIP"); |
donatien | 8:04b6a042595f | 63 | LwIPInterface::init(); //Init LwIP, NOT including PPP |
donatien | 8:04b6a042595f | 64 | DBG("Initializing PPP"); |
donatien | 8:04b6a042595f | 65 | pppInit(); |
donatien | 8:04b6a042595f | 66 | DBG("Done"); |
donatien | 8:04b6a042595f | 67 | return OK; |
donatien | 8:04b6a042595f | 68 | } |
donatien | 8:04b6a042595f | 69 | |
donatien | 8:04b6a042595f | 70 | int PPPIPInterface::setup(const char* user, const char* pw) |
donatien | 8:04b6a042595f | 71 | { |
donatien | 8:04b6a042595f | 72 | DBG("Configuring PPP authentication method"); |
donatien | 8:04b6a042595f | 73 | pppSetAuth(PPPAUTHTYPE_ANY, user, pw); |
donatien | 8:04b6a042595f | 74 | DBG("Done"); |
donatien | 8:04b6a042595f | 75 | return OK; |
donatien | 8:04b6a042595f | 76 | } |
donatien | 8:04b6a042595f | 77 | |
donatien | 12:66dc2c8eba2d | 78 | /*virtual*/ int PPPIPInterface::connect() |
donatien | 8:04b6a042595f | 79 | { |
donatien | 9:3f077dde13c9 | 80 | int ret; |
donatien | 9:3f077dde13c9 | 81 | char buf[32]; |
donatien | 9:3f077dde13c9 | 82 | size_t len; |
donatien | 8:04b6a042595f | 83 | DBG("Trying to connect with PPP"); |
donatien | 9:3f077dde13c9 | 84 | |
donatien | 18:1789a11d1892 | 85 | cleanupLink(); |
donatien | 9:3f077dde13c9 | 86 | |
donatien | 12:66dc2c8eba2d | 87 | DBG("Sending %s", CONNECT_CMD); |
donatien | 9:3f077dde13c9 | 88 | |
donatien | 12:66dc2c8eba2d | 89 | ret = m_pStream->write((uint8_t*)CONNECT_CMD, strlen(CONNECT_CMD), osWaitForever); |
donatien | 9:3f077dde13c9 | 90 | if( ret != OK ) |
donatien | 9:3f077dde13c9 | 91 | { |
donatien | 9:3f077dde13c9 | 92 | return NET_UNKNOWN; |
donatien | 9:3f077dde13c9 | 93 | } |
donatien | 9:3f077dde13c9 | 94 | |
donatien | 18:1789a11d1892 | 95 | DBG("Expect %s", EXPECTED_RESP); |
donatien | 12:66dc2c8eba2d | 96 | |
donatien | 12:66dc2c8eba2d | 97 | len = 0; |
donatien | 18:1789a11d1892 | 98 | size_t readLen; |
donatien | 18:1789a11d1892 | 99 | ret = m_pStream->read((uint8_t*)buf + len, &readLen, EXPECTED_RESP_MIN_LEN, 10000); |
donatien | 18:1789a11d1892 | 100 | if( ret != OK ) |
donatien | 9:3f077dde13c9 | 101 | { |
donatien | 18:1789a11d1892 | 102 | return NET_UNKNOWN; |
donatien | 18:1789a11d1892 | 103 | } |
donatien | 18:1789a11d1892 | 104 | len += readLen; |
donatien | 18:1789a11d1892 | 105 | while( (len < EXPECTED_RESP_MIN_LEN) || (buf[len-1] != LF) ) |
donatien | 18:1789a11d1892 | 106 | { |
donatien | 18:1789a11d1892 | 107 | ret = m_pStream->read((uint8_t*)buf + len, &readLen, 1, 10000); |
donatien | 12:66dc2c8eba2d | 108 | if( ret != OK ) |
donatien | 12:66dc2c8eba2d | 109 | { |
donatien | 18:1789a11d1892 | 110 | return NET_UNKNOWN; |
donatien | 12:66dc2c8eba2d | 111 | } |
donatien | 12:66dc2c8eba2d | 112 | len += readLen; |
donatien | 9:3f077dde13c9 | 113 | } |
donatien | 9:3f077dde13c9 | 114 | |
donatien | 12:66dc2c8eba2d | 115 | buf[len]=0; |
donatien | 12:66dc2c8eba2d | 116 | |
donatien | 12:66dc2c8eba2d | 117 | DBG("Got %s[len %d]", buf, len); |
donatien | 12:66dc2c8eba2d | 118 | |
donatien | 18:1789a11d1892 | 119 | int datarate = 0; |
donatien | 18:1789a11d1892 | 120 | if( (sscanf( buf, EXPECTED_RESP_DATARATE, &datarate ) != 1) && (strcmp(EXPECTED_RESP, buf) != 0) ) |
donatien | 9:3f077dde13c9 | 121 | { |
donatien | 9:3f077dde13c9 | 122 | //Discard buffer |
donatien | 9:3f077dde13c9 | 123 | do //Clear buf |
donatien | 9:3f077dde13c9 | 124 | { |
donatien | 12:66dc2c8eba2d | 125 | ret = m_pStream->read((uint8_t*)buf, &len, 32, 0); |
donatien | 9:3f077dde13c9 | 126 | } while( (ret == OK) && (len > 0) ); |
donatien | 9:3f077dde13c9 | 127 | return NET_CONN; |
donatien | 9:3f077dde13c9 | 128 | } |
donatien | 9:3f077dde13c9 | 129 | |
donatien | 9:3f077dde13c9 | 130 | DBG("Transport link open"); |
donatien | 18:1789a11d1892 | 131 | if(datarate != 0) |
donatien | 18:1789a11d1892 | 132 | { |
donatien | 18:1789a11d1892 | 133 | DBG("Datarate: %d bps", datarate); |
donatien | 18:1789a11d1892 | 134 | } |
donatien | 8:04b6a042595f | 135 | m_linkStatusSphre.wait(0); |
donatien | 8:04b6a042595f | 136 | if((m_pppd != -1) && (m_pppErrCode == 0)) //Already connected |
donatien | 8:04b6a042595f | 137 | { |
donatien | 8:04b6a042595f | 138 | return NET_INVALID; |
donatien | 8:04b6a042595f | 139 | } |
donatien | 12:66dc2c8eba2d | 140 | |
donatien | 12:66dc2c8eba2d | 141 | ret = pppOverSerialOpen(this, PPPIPInterface::linkStatusCb, this); |
donatien | 8:04b6a042595f | 142 | if(ret < 0) |
donatien | 8:04b6a042595f | 143 | { |
donatien | 8:04b6a042595f | 144 | switch(ret) |
donatien | 8:04b6a042595f | 145 | { |
donatien | 8:04b6a042595f | 146 | case PPPERR_OPEN: |
donatien | 8:04b6a042595f | 147 | default: |
donatien | 8:04b6a042595f | 148 | return NET_FULL; //All available resources are already used |
donatien | 8:04b6a042595f | 149 | } |
donatien | 8:04b6a042595f | 150 | } |
donatien | 8:04b6a042595f | 151 | m_pppd = ret; //PPP descriptor |
donatien | 8:04b6a042595f | 152 | m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there |
donatien | 8:04b6a042595f | 153 | if(m_pppErrCode != PPPERR_NONE) |
donatien | 8:04b6a042595f | 154 | { |
donatien | 8:04b6a042595f | 155 | m_pppd = -1; |
donatien | 8:04b6a042595f | 156 | } |
donatien | 8:04b6a042595f | 157 | switch(m_pppErrCode) |
donatien | 8:04b6a042595f | 158 | { |
donatien | 8:04b6a042595f | 159 | case PPPERR_NONE: //Connected OK |
donatien | 8:04b6a042595f | 160 | return OK; |
donatien | 8:04b6a042595f | 161 | case PPPERR_CONNECT: //Connection lost |
donatien | 8:04b6a042595f | 162 | return NET_INTERRUPTED; |
donatien | 8:04b6a042595f | 163 | case PPPERR_AUTHFAIL: //Authentication failed |
donatien | 8:04b6a042595f | 164 | return NET_AUTH; |
donatien | 8:04b6a042595f | 165 | case PPPERR_PROTOCOL: //Protocol error |
donatien | 8:04b6a042595f | 166 | return NET_PROTOCOL; |
donatien | 8:04b6a042595f | 167 | default: |
donatien | 8:04b6a042595f | 168 | return NET_UNKNOWN; |
donatien | 8:04b6a042595f | 169 | } |
donatien | 8:04b6a042595f | 170 | } |
donatien | 8:04b6a042595f | 171 | |
donatien | 8:04b6a042595f | 172 | /*virtual*/ int PPPIPInterface::disconnect() |
donatien | 8:04b6a042595f | 173 | { |
donatien | 8:04b6a042595f | 174 | int ret = m_linkStatusSphre.wait(0); |
donatien | 8:04b6a042595f | 175 | if(ret > 0) //Already disconnected? |
donatien | 8:04b6a042595f | 176 | { |
donatien | 8:04b6a042595f | 177 | m_pppd = -1; //Discard PPP descriptor |
donatien | 8:04b6a042595f | 178 | switch(m_pppErrCode) |
donatien | 8:04b6a042595f | 179 | { |
donatien | 8:04b6a042595f | 180 | case PPPERR_CONNECT: //Connection terminated |
donatien | 8:04b6a042595f | 181 | case PPPERR_AUTHFAIL: //Authentication failed |
donatien | 8:04b6a042595f | 182 | case PPPERR_PROTOCOL: //Protocol error |
donatien | 8:04b6a042595f | 183 | case PPPERR_USER: |
donatien | 8:04b6a042595f | 184 | return OK; |
donatien | 8:04b6a042595f | 185 | default: |
donatien | 8:04b6a042595f | 186 | return NET_UNKNOWN; |
donatien | 8:04b6a042595f | 187 | } |
donatien | 8:04b6a042595f | 188 | } |
donatien | 8:04b6a042595f | 189 | else |
donatien | 8:04b6a042595f | 190 | { |
donatien | 8:04b6a042595f | 191 | if(m_pppd == -1) |
donatien | 8:04b6a042595f | 192 | { |
donatien | 8:04b6a042595f | 193 | return NET_INVALID; |
donatien | 8:04b6a042595f | 194 | } |
donatien | 8:04b6a042595f | 195 | pppClose(m_pppd); |
donatien | 8:04b6a042595f | 196 | do |
donatien | 8:04b6a042595f | 197 | { |
donatien | 8:04b6a042595f | 198 | m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there |
donatien | 8:04b6a042595f | 199 | DBG("Received PPP err code %d", m_pppErrCode); |
donatien | 8:04b6a042595f | 200 | } while(m_pppErrCode != PPPERR_USER); |
donatien | 8:04b6a042595f | 201 | m_pppd = -1; //Discard PPP descriptor |
donatien | 8:04b6a042595f | 202 | } |
donatien | 18:1789a11d1892 | 203 | |
donatien | 18:1789a11d1892 | 204 | DBG("Sending %s", ESCAPE_SEQ); |
donatien | 18:1789a11d1892 | 205 | |
donatien | 18:1789a11d1892 | 206 | ret = m_pStream->write((uint8_t*)ESCAPE_SEQ, strlen(ESCAPE_SEQ), osWaitForever); |
donatien | 18:1789a11d1892 | 207 | if( ret != OK ) |
donatien | 18:1789a11d1892 | 208 | { |
donatien | 18:1789a11d1892 | 209 | return NET_UNKNOWN; |
donatien | 18:1789a11d1892 | 210 | } |
donatien | 18:1789a11d1892 | 211 | |
donatien | 18:1789a11d1892 | 212 | cleanupLink(); |
donatien | 18:1789a11d1892 | 213 | |
donatien | 18:1789a11d1892 | 214 | return OK; |
donatien | 18:1789a11d1892 | 215 | } |
donatien | 18:1789a11d1892 | 216 | |
donatien | 18:1789a11d1892 | 217 | |
donatien | 18:1789a11d1892 | 218 | int PPPIPInterface::cleanupLink() |
donatien | 18:1789a11d1892 | 219 | { |
donatien | 18:1789a11d1892 | 220 | int ret; |
donatien | 18:1789a11d1892 | 221 | char buf[32]; |
donatien | 18:1789a11d1892 | 222 | size_t len; |
donatien | 18:1789a11d1892 | 223 | |
donatien | 18:1789a11d1892 | 224 | do //Clear buf |
donatien | 18:1789a11d1892 | 225 | { |
donatien | 18:1789a11d1892 | 226 | ret = m_pStream->read((uint8_t*)buf, &len, 32, 100); |
donatien | 18:1789a11d1892 | 227 | if(ret == OK) |
donatien | 18:1789a11d1892 | 228 | { |
donatien | 18:1789a11d1892 | 229 | buf[len] = '\0'; |
donatien | 18:1789a11d1892 | 230 | DBG("Got %s", buf); |
donatien | 18:1789a11d1892 | 231 | } |
donatien | 18:1789a11d1892 | 232 | } while( (ret == OK) && (len > 0) ); |
donatien | 18:1789a11d1892 | 233 | |
donatien | 18:1789a11d1892 | 234 | DBG("Sending %s", HANGUP_CMD); |
donatien | 18:1789a11d1892 | 235 | |
donatien | 18:1789a11d1892 | 236 | ret = m_pStream->write((uint8_t*)HANGUP_CMD, strlen(HANGUP_CMD), osWaitForever); |
donatien | 18:1789a11d1892 | 237 | if( ret != OK ) |
donatien | 18:1789a11d1892 | 238 | { |
donatien | 18:1789a11d1892 | 239 | return NET_UNKNOWN; |
donatien | 18:1789a11d1892 | 240 | } |
donatien | 18:1789a11d1892 | 241 | |
donatien | 18:1789a11d1892 | 242 | size_t readLen; |
donatien | 18:1789a11d1892 | 243 | |
donatien | 18:1789a11d1892 | 244 | //Hangup |
donatien | 18:1789a11d1892 | 245 | DBG("Expect %s", HANGUP_CMD); |
donatien | 18:1789a11d1892 | 246 | |
donatien | 18:1789a11d1892 | 247 | len = 0; |
donatien | 18:1789a11d1892 | 248 | while( len < strlen(HANGUP_CMD) ) |
donatien | 18:1789a11d1892 | 249 | { |
donatien | 18:1789a11d1892 | 250 | ret = m_pStream->read((uint8_t*)buf + len, &readLen, strlen(HANGUP_CMD) - len, 100); |
donatien | 18:1789a11d1892 | 251 | if( ret != OK ) |
donatien | 18:1789a11d1892 | 252 | { |
donatien | 18:1789a11d1892 | 253 | break; |
donatien | 18:1789a11d1892 | 254 | } |
donatien | 18:1789a11d1892 | 255 | len += readLen; |
donatien | 18:1789a11d1892 | 256 | ///// |
donatien | 18:1789a11d1892 | 257 | buf[len]=0; |
donatien | 18:1789a11d1892 | 258 | DBG("Got %s", buf); |
donatien | 18:1789a11d1892 | 259 | } |
donatien | 18:1789a11d1892 | 260 | |
donatien | 18:1789a11d1892 | 261 | buf[len]=0; |
donatien | 18:1789a11d1892 | 262 | |
donatien | 18:1789a11d1892 | 263 | DBG("Got %s[len %d]", buf, len); |
donatien | 18:1789a11d1892 | 264 | |
donatien | 18:1789a11d1892 | 265 | //OK response |
donatien | 18:1789a11d1892 | 266 | DBG("Expect %s", OK_RESP); |
donatien | 18:1789a11d1892 | 267 | |
donatien | 18:1789a11d1892 | 268 | len = 0; |
donatien | 18:1789a11d1892 | 269 | while( len < strlen(OK_RESP) ) |
donatien | 18:1789a11d1892 | 270 | { |
donatien | 18:1789a11d1892 | 271 | ret = m_pStream->read((uint8_t*)buf + len, &readLen, strlen(OK_RESP) - len, 100); |
donatien | 18:1789a11d1892 | 272 | if( ret != OK ) |
donatien | 18:1789a11d1892 | 273 | { |
donatien | 18:1789a11d1892 | 274 | break; |
donatien | 18:1789a11d1892 | 275 | } |
donatien | 18:1789a11d1892 | 276 | len += readLen; |
donatien | 18:1789a11d1892 | 277 | ///// |
donatien | 18:1789a11d1892 | 278 | buf[len]=0; |
donatien | 18:1789a11d1892 | 279 | DBG("Got %s", buf); |
donatien | 18:1789a11d1892 | 280 | } |
donatien | 18:1789a11d1892 | 281 | |
donatien | 18:1789a11d1892 | 282 | buf[len]=0; |
donatien | 18:1789a11d1892 | 283 | |
donatien | 18:1789a11d1892 | 284 | DBG("Got %s[len %d]", buf, len); |
donatien | 18:1789a11d1892 | 285 | |
donatien | 18:1789a11d1892 | 286 | //NO CARRIER event |
donatien | 18:1789a11d1892 | 287 | DBG("Expect %s", NO_CARRIER_RESP); |
donatien | 18:1789a11d1892 | 288 | |
donatien | 18:1789a11d1892 | 289 | len = 0; |
donatien | 18:1789a11d1892 | 290 | while( len < strlen(NO_CARRIER_RESP) ) |
donatien | 18:1789a11d1892 | 291 | { |
donatien | 18:1789a11d1892 | 292 | ret = m_pStream->read((uint8_t*)buf + len, &readLen, strlen(NO_CARRIER_RESP) - len, 100); |
donatien | 18:1789a11d1892 | 293 | if( ret != OK ) |
donatien | 18:1789a11d1892 | 294 | { |
donatien | 18:1789a11d1892 | 295 | break; |
donatien | 18:1789a11d1892 | 296 | } |
donatien | 18:1789a11d1892 | 297 | len += readLen; |
donatien | 18:1789a11d1892 | 298 | ///// |
donatien | 18:1789a11d1892 | 299 | buf[len]=0; |
donatien | 18:1789a11d1892 | 300 | DBG("Got %s", buf); |
donatien | 18:1789a11d1892 | 301 | } |
donatien | 18:1789a11d1892 | 302 | |
donatien | 18:1789a11d1892 | 303 | buf[len]=0; |
donatien | 18:1789a11d1892 | 304 | |
donatien | 18:1789a11d1892 | 305 | DBG("Got %s[len %d]", buf, len); |
donatien | 18:1789a11d1892 | 306 | |
donatien | 18:1789a11d1892 | 307 | do //Clear buf |
donatien | 18:1789a11d1892 | 308 | { |
donatien | 18:1789a11d1892 | 309 | ret = m_pStream->read((uint8_t*)buf, &len, 32, 100); |
donatien | 18:1789a11d1892 | 310 | if(ret == OK) |
donatien | 18:1789a11d1892 | 311 | { |
donatien | 18:1789a11d1892 | 312 | buf[len] = '\0'; |
donatien | 18:1789a11d1892 | 313 | DBG("Got %s", buf); |
donatien | 18:1789a11d1892 | 314 | } |
donatien | 18:1789a11d1892 | 315 | } while( (ret == OK) && (len > 0) ); |
donatien | 18:1789a11d1892 | 316 | |
donatien | 18:1789a11d1892 | 317 | |
donatien | 8:04b6a042595f | 318 | return OK; |
donatien | 8:04b6a042595f | 319 | } |
donatien | 8:04b6a042595f | 320 | |
donatien | 8:04b6a042595f | 321 | /*static*/ void PPPIPInterface::linkStatusCb(void *ctx, int errCode, void *arg) //PPP link status |
donatien | 8:04b6a042595f | 322 | { |
donatien | 8:04b6a042595f | 323 | PPPIPInterface* pIf = (PPPIPInterface*)ctx; |
donatien | 8:04b6a042595f | 324 | struct ppp_addrs* addrs = (struct ppp_addrs*) arg; |
donatien | 8:04b6a042595f | 325 | |
donatien | 8:04b6a042595f | 326 | switch(errCode) |
donatien | 8:04b6a042595f | 327 | { |
donatien | 8:04b6a042595f | 328 | case PPPERR_NONE: |
donatien | 8:04b6a042595f | 329 | WARN("Connected via PPP."); |
donatien | 8:04b6a042595f | 330 | DBG("Local IP address: %s", inet_ntoa(addrs->our_ipaddr)); |
donatien | 8:04b6a042595f | 331 | DBG("Netmask: %s", inet_ntoa(addrs->netmask)); |
donatien | 8:04b6a042595f | 332 | DBG("Remote IP address: %s", inet_ntoa(addrs->his_ipaddr)); |
donatien | 8:04b6a042595f | 333 | DBG("Primary DNS: %s", inet_ntoa(addrs->dns1)); |
donatien | 8:04b6a042595f | 334 | DBG("Secondary DNS: %s", inet_ntoa(addrs->dns2)); |
donatien | 10:21a6f09d5631 | 335 | pIf->setConnected(true); |
donatien | 10:21a6f09d5631 | 336 | pIf->setIPAddress(inet_ntoa(addrs->our_ipaddr)); |
donatien | 8:04b6a042595f | 337 | break; |
donatien | 8:04b6a042595f | 338 | case PPPERR_CONNECT: //Connection lost |
donatien | 8:04b6a042595f | 339 | WARN("Connection lost/terminated"); |
donatien | 10:21a6f09d5631 | 340 | pIf->setConnected(false); |
donatien | 8:04b6a042595f | 341 | break; |
donatien | 8:04b6a042595f | 342 | case PPPERR_AUTHFAIL: //Authentication failed |
donatien | 8:04b6a042595f | 343 | WARN("Authentication failed"); |
donatien | 10:21a6f09d5631 | 344 | pIf->setConnected(false); |
donatien | 8:04b6a042595f | 345 | break; |
donatien | 8:04b6a042595f | 346 | case PPPERR_PROTOCOL: //Protocol error |
donatien | 8:04b6a042595f | 347 | WARN("Protocol error"); |
donatien | 10:21a6f09d5631 | 348 | pIf->setConnected(false); |
donatien | 8:04b6a042595f | 349 | break; |
donatien | 8:04b6a042595f | 350 | case PPPERR_USER: |
donatien | 8:04b6a042595f | 351 | WARN("Disconnected by user"); |
donatien | 10:21a6f09d5631 | 352 | pIf->setConnected(false); |
donatien | 8:04b6a042595f | 353 | break; |
donatien | 8:04b6a042595f | 354 | default: |
donatien | 8:04b6a042595f | 355 | WARN("Unknown error (%d)", errCode); |
donatien | 10:21a6f09d5631 | 356 | pIf->setConnected(false); |
donatien | 8:04b6a042595f | 357 | break; |
donatien | 8:04b6a042595f | 358 | } |
donatien | 8:04b6a042595f | 359 | |
donatien | 8:04b6a042595f | 360 | pIf->m_linkStatusSphre.wait(0); //If previous event has not been handled, "delete" it now |
donatien | 8:04b6a042595f | 361 | pIf->m_pppErrCode = errCode; |
donatien | 8:04b6a042595f | 362 | pIf->m_linkStatusSphre.release(); |
donatien | 8:04b6a042595f | 363 | } |
donatien | 8:04b6a042595f | 364 | |
donatien | 8:04b6a042595f | 365 | //LwIP PPP implementation |
donatien | 8:04b6a042595f | 366 | extern "C" |
donatien | 8:04b6a042595f | 367 | { |
donatien | 8:04b6a042595f | 368 | |
donatien | 8:04b6a042595f | 369 | /** |
donatien | 8:04b6a042595f | 370 | * Writes to the serial device. |
donatien | 8:04b6a042595f | 371 | * |
donatien | 8:04b6a042595f | 372 | * @param fd serial device handle |
donatien | 8:04b6a042595f | 373 | * @param data pointer to data to send |
donatien | 8:04b6a042595f | 374 | * @param len length (in bytes) of data to send |
donatien | 8:04b6a042595f | 375 | * @return number of bytes actually sent |
donatien | 8:04b6a042595f | 376 | * |
donatien | 8:04b6a042595f | 377 | * @note This function will block until all data can be sent. |
donatien | 8:04b6a042595f | 378 | */ |
donatien | 8:04b6a042595f | 379 | u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len) |
donatien | 8:04b6a042595f | 380 | { |
donatien | 8:04b6a042595f | 381 | DBG("sio_write"); |
donatien | 8:04b6a042595f | 382 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
donatien | 8:04b6a042595f | 383 | int ret; |
donatien | 8:04b6a042595f | 384 | if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further |
donatien | 8:04b6a042595f | 385 | { |
donatien | 8:04b6a042595f | 386 | return 0; |
donatien | 8:04b6a042595f | 387 | } |
donatien | 8:04b6a042595f | 388 | ret = pIf->m_pStream->write(data, len, osWaitForever); //Blocks until all data is sent or an error happens |
donatien | 8:04b6a042595f | 389 | if(ret != OK) |
donatien | 8:04b6a042595f | 390 | { |
donatien | 8:04b6a042595f | 391 | return 0; |
donatien | 8:04b6a042595f | 392 | } |
donatien | 8:04b6a042595f | 393 | return len; |
donatien | 8:04b6a042595f | 394 | } |
donatien | 8:04b6a042595f | 395 | |
donatien | 8:04b6a042595f | 396 | /** |
donatien | 8:04b6a042595f | 397 | * Reads from the serial device. |
donatien | 8:04b6a042595f | 398 | * |
donatien | 8:04b6a042595f | 399 | * @param fd serial device handle |
donatien | 8:04b6a042595f | 400 | * @param data pointer to data buffer for receiving |
donatien | 8:04b6a042595f | 401 | * @param len maximum length (in bytes) of data to receive |
donatien | 8:04b6a042595f | 402 | * @return number of bytes actually received - may be 0 if aborted by sio_read_abort |
donatien | 8:04b6a042595f | 403 | * |
donatien | 8:04b6a042595f | 404 | * @note This function will block until data can be received. The blocking |
donatien | 8:04b6a042595f | 405 | * can be cancelled by calling sio_read_abort(). |
donatien | 8:04b6a042595f | 406 | */ |
donatien | 8:04b6a042595f | 407 | u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) |
donatien | 8:04b6a042595f | 408 | { |
donatien | 8:04b6a042595f | 409 | DBG("sio_read"); |
donatien | 8:04b6a042595f | 410 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
donatien | 8:04b6a042595f | 411 | int ret; |
donatien | 8:04b6a042595f | 412 | size_t readLen; |
donatien | 8:04b6a042595f | 413 | if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further |
donatien | 8:04b6a042595f | 414 | { |
donatien | 8:04b6a042595f | 415 | WARN("EXIT NOT AVAIL"); |
donatien | 8:04b6a042595f | 416 | return 0; |
donatien | 8:04b6a042595f | 417 | } |
donatien | 8:04b6a042595f | 418 | ret = pIf->m_pStream->read(data, &readLen, len, osWaitForever); //Blocks until some data is received or an error happens |
donatien | 8:04b6a042595f | 419 | if(ret != OK) |
donatien | 8:04b6a042595f | 420 | { |
donatien | 8:04b6a042595f | 421 | return 0; |
donatien | 8:04b6a042595f | 422 | } |
donatien | 8:04b6a042595f | 423 | DBG("ret"); |
donatien | 8:04b6a042595f | 424 | return readLen; |
donatien | 8:04b6a042595f | 425 | } |
donatien | 8:04b6a042595f | 426 | |
donatien | 8:04b6a042595f | 427 | /** |
donatien | 8:04b6a042595f | 428 | * Aborts a blocking sio_read() call. |
donatien | 8:04b6a042595f | 429 | * |
donatien | 8:04b6a042595f | 430 | * @param fd serial device handle |
donatien | 8:04b6a042595f | 431 | */ |
donatien | 8:04b6a042595f | 432 | void sio_read_abort(sio_fd_t fd) |
donatien | 8:04b6a042595f | 433 | { |
donatien | 8:04b6a042595f | 434 | DBG("sio_read_abort"); |
donatien | 8:04b6a042595f | 435 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
donatien | 8:04b6a042595f | 436 | if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further |
donatien | 8:04b6a042595f | 437 | { |
donatien | 8:04b6a042595f | 438 | return; |
donatien | 8:04b6a042595f | 439 | } |
donatien | 8:04b6a042595f | 440 | pIf->m_pStream->abortRead(); |
donatien | 8:04b6a042595f | 441 | DBG("ret"); |
donatien | 8:04b6a042595f | 442 | } |
donatien | 8:04b6a042595f | 443 | |
donatien | 8:04b6a042595f | 444 | } |
donatien | 8:04b6a042595f | 445 |