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