phs fan / AbitModemInterface

Dependencies:   Socket lwip-sys lwip

Fork of AbitUSBModem by phs fan

Committer:
phsfan
Date:
Mon May 11 14:07:07 2015 +0000
Revision:
100:dbd92e9515ef
Parent:
98:1b851249d70b
ports to APM-002 (Abit PHS Module)

Who changed what in which revision?

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