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