Implementation of 3G USB Modem Huawei E372
PPPIPInterface.cpp@2:61ac95f0af72, 2015-04-14 (annotated)
- Committer:
- clemounet
- Date:
- Tue Apr 14 13:27:07 2015 +0000
- Revision:
- 2:61ac95f0af72
- Parent:
- 1:fbf17fb09581
.up (working)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
clemounet | 1:fbf17fb09581 | 1 | /* PPPIPInterface.cpp */ |
clemounet | 1:fbf17fb09581 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
clemounet | 1:fbf17fb09581 | 3 | * |
clemounet | 1:fbf17fb09581 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
clemounet | 1:fbf17fb09581 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
clemounet | 1:fbf17fb09581 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
clemounet | 1:fbf17fb09581 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
clemounet | 1:fbf17fb09581 | 8 | * furnished to do so, subject to the following conditions: |
clemounet | 1:fbf17fb09581 | 9 | * |
clemounet | 1:fbf17fb09581 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
clemounet | 1:fbf17fb09581 | 11 | * substantial portions of the Software. |
clemounet | 1:fbf17fb09581 | 12 | * |
clemounet | 1:fbf17fb09581 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
clemounet | 1:fbf17fb09581 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
clemounet | 1:fbf17fb09581 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
clemounet | 1:fbf17fb09581 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
clemounet | 1:fbf17fb09581 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
clemounet | 1:fbf17fb09581 | 18 | */ |
clemounet | 2:61ac95f0af72 | 19 | |
clemounet | 2:61ac95f0af72 | 20 | #define __DEBUG__ 3 |
clemounet | 1:fbf17fb09581 | 21 | #ifndef __MODULE__ |
clemounet | 1:fbf17fb09581 | 22 | #define __MODULE__ "PPPIPInterface.cpp" |
clemounet | 1:fbf17fb09581 | 23 | #endif |
clemounet | 2:61ac95f0af72 | 24 | #include "MyDebug.h" |
clemounet | 1:fbf17fb09581 | 25 | |
clemounet | 2:61ac95f0af72 | 26 | #include "core/errors.h" |
clemounet | 1:fbf17fb09581 | 27 | #include "core/fwk.h" |
clemounet | 2:61ac95f0af72 | 28 | #include "core/IOStream.h" |
clemounet | 1:fbf17fb09581 | 29 | #include "rtos.h" |
clemounet | 1:fbf17fb09581 | 30 | |
clemounet | 1:fbf17fb09581 | 31 | #include <cstdio> |
clemounet | 1:fbf17fb09581 | 32 | using std::sscanf; |
clemounet | 1:fbf17fb09581 | 33 | |
clemounet | 1:fbf17fb09581 | 34 | #include "PPPIPInterface.h" |
clemounet | 2:61ac95f0af72 | 35 | #include "USBSerialStream.h" |
clemounet | 2:61ac95f0af72 | 36 | |
clemounet | 2:61ac95f0af72 | 37 | #define PPP_SERIAL_PORT 0 |
clemounet | 1:fbf17fb09581 | 38 | |
clemounet | 1:fbf17fb09581 | 39 | #define MSISDN "*99#" |
clemounet | 1:fbf17fb09581 | 40 | #define CONNECT_CMD "ATD " MSISDN "\x0D" |
clemounet | 1:fbf17fb09581 | 41 | #define EXPECTED_RESP CONNECT_CMD "\x0D" "\x0A" "CONNECT" "\x0D" "\x0A" |
clemounet | 1:fbf17fb09581 | 42 | #define EXPECTED_RESP_DATARATE CONNECT_CMD "\x0D" "\x0A" "CONNECT %d" "\x0D" "\x0A" |
clemounet | 1:fbf17fb09581 | 43 | #define EXPECTED_RESP_MIN_LEN 20 |
clemounet | 1:fbf17fb09581 | 44 | #define OK_RESP "\x0D" "\x0A" "OK" "\x0D" "\x0A" |
clemounet | 1:fbf17fb09581 | 45 | #define ESCAPE_SEQ "+++" |
clemounet | 1:fbf17fb09581 | 46 | #define HANGUP_CMD "ATH" "\x0D" |
clemounet | 1:fbf17fb09581 | 47 | #define NO_CARRIER_RESP "\x0D" "\x0A" "NO CARRIER" "\x0D" "\x0A" |
clemounet | 2:61ac95f0af72 | 48 | |
clemounet | 1:fbf17fb09581 | 49 | extern "C" { |
clemounet | 1:fbf17fb09581 | 50 | #include "lwip/ip_addr.h" |
clemounet | 1:fbf17fb09581 | 51 | #include "lwip/inet.h" |
clemounet | 1:fbf17fb09581 | 52 | #include "lwip/err.h" |
clemounet | 2:61ac95f0af72 | 53 | //#include "lwip/dns.h" |
clemounet | 2:61ac95f0af72 | 54 | #include "lwip/tcpip.h" |
clemounet | 1:fbf17fb09581 | 55 | #include "netif/ppp/ppp.h" |
clemounet | 1:fbf17fb09581 | 56 | } |
clemounet | 1:fbf17fb09581 | 57 | |
clemounet | 2:61ac95f0af72 | 58 | PPPIPInterface::PPPIPInterface(USBSerialStream* pStream): |
clemounet | 2:61ac95f0af72 | 59 | pppStream(pStream), |
clemounet | 2:61ac95f0af72 | 60 | pppSession(0), |
clemounet | 2:61ac95f0af72 | 61 | ipInitiated(false), |
clemounet | 2:61ac95f0af72 | 62 | pppInitiated(false), |
clemounet | 2:61ac95f0af72 | 63 | connected(false) {} |
clemounet | 2:61ac95f0af72 | 64 | PPPIPInterface::~PPPIPInterface() { } |
clemounet | 1:fbf17fb09581 | 65 | |
clemounet | 2:61ac95f0af72 | 66 | void PPPIPInterface::stackInits(const char* user, const char* pw) { |
clemounet | 2:61ac95f0af72 | 67 | // LwIP Stuffs |
clemounet | 2:61ac95f0af72 | 68 | if(!ipInitiated) { |
clemounet | 2:61ac95f0af72 | 69 | DBG("LwIP: Init Stack"); |
clemounet | 2:61ac95f0af72 | 70 | tcpip_init(PPPIPInterface::tcpipInitDoneCb, this); |
clemounet | 2:61ac95f0af72 | 71 | DBG("LwIP: Wait for setup"); |
clemounet | 2:61ac95f0af72 | 72 | while (!ipInitiated) |
clemounet | 2:61ac95f0af72 | 73 | Thread::wait(1); |
clemounet | 2:61ac95f0af72 | 74 | DBG("LwIP: setup done"); |
clemounet | 2:61ac95f0af72 | 75 | } |
clemounet | 2:61ac95f0af72 | 76 | // PPP Stuffs |
clemounet | 2:61ac95f0af72 | 77 | if(!pppInitiated) { |
clemounet | 2:61ac95f0af72 | 78 | pppInit(); |
clemounet | 2:61ac95f0af72 | 79 | pppSetAuth(PPPAUTHTYPE_ANY, user, pw); |
clemounet | 2:61ac95f0af72 | 80 | pppInitiated = true; |
clemounet | 2:61ac95f0af72 | 81 | } |
clemounet | 1:fbf17fb09581 | 82 | } |
clemounet | 1:fbf17fb09581 | 83 | |
clemounet | 2:61ac95f0af72 | 84 | int PPPIPInterface::dial(void) { |
clemounet | 2:61ac95f0af72 | 85 | int ret; |
clemounet | 2:61ac95f0af72 | 86 | char buf[32]; |
clemounet | 2:61ac95f0af72 | 87 | size_t len; |
clemounet | 2:61ac95f0af72 | 88 | |
clemounet | 2:61ac95f0af72 | 89 | DBG("Sending %s", CONNECT_CMD); |
clemounet | 2:61ac95f0af72 | 90 | ret = pppStream->write((uint8_t*)CONNECT_CMD, strlen(CONNECT_CMD), osWaitForever); |
clemounet | 2:61ac95f0af72 | 91 | if( ret != OK ) |
clemounet | 2:61ac95f0af72 | 92 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 93 | DBG("Expect %s", EXPECTED_RESP); |
clemounet | 2:61ac95f0af72 | 94 | |
clemounet | 2:61ac95f0af72 | 95 | /* |
clemounet | 2:61ac95f0af72 | 96 | size_t readLen; |
clemounet | 2:61ac95f0af72 | 97 | ret = pppStream->read((uint8_t*)buf, &readLen, EXPECTED_RESP_MIN_LEN, 10000); |
clemounet | 2:61ac95f0af72 | 98 | if( ret != OK ) |
clemounet | 2:61ac95f0af72 | 99 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 100 | |
clemounet | 2:61ac95f0af72 | 101 | DBG("Readed %d chars", readLen); |
clemounet | 2:61ac95f0af72 | 102 | DBG_MEMDUMP("PPPAT",buf,readLen); |
clemounet | 2:61ac95f0af72 | 103 | */ |
clemounet | 2:61ac95f0af72 | 104 | |
clemounet | 2:61ac95f0af72 | 105 | len = 0; |
clemounet | 2:61ac95f0af72 | 106 | size_t readLen; |
clemounet | 2:61ac95f0af72 | 107 | ret = pppStream->read((uint8_t*)buf + len, &readLen, EXPECTED_RESP_MIN_LEN, 10000); |
clemounet | 2:61ac95f0af72 | 108 | if( ret != OK ) |
clemounet | 2:61ac95f0af72 | 109 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 110 | |
clemounet | 2:61ac95f0af72 | 111 | len += readLen; |
clemounet | 2:61ac95f0af72 | 112 | while( (len < EXPECTED_RESP_MIN_LEN) || (buf[len-1] != LF) ) { |
clemounet | 2:61ac95f0af72 | 113 | ret = pppStream->read((uint8_t*)buf + len, &readLen, 1, 10000); |
clemounet | 2:61ac95f0af72 | 114 | if( ret != OK ) |
clemounet | 2:61ac95f0af72 | 115 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 116 | len += readLen; |
clemounet | 2:61ac95f0af72 | 117 | } |
clemounet | 2:61ac95f0af72 | 118 | |
clemounet | 2:61ac95f0af72 | 119 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 120 | |
clemounet | 2:61ac95f0af72 | 121 | DBG("Got %s[len %d]", buf, len); |
clemounet | 2:61ac95f0af72 | 122 | |
clemounet | 2:61ac95f0af72 | 123 | int datarate = 0; |
clemounet | 2:61ac95f0af72 | 124 | if( (sscanf( buf, EXPECTED_RESP_DATARATE, &datarate ) != 1) && (strcmp(EXPECTED_RESP, buf) != 0) ) { |
clemounet | 2:61ac95f0af72 | 125 | //Discard buffer |
clemounet | 2:61ac95f0af72 | 126 | do { //Clear buf |
clemounet | 2:61ac95f0af72 | 127 | ret = pppStream->read((uint8_t*)buf, &len, 32, 0); |
clemounet | 2:61ac95f0af72 | 128 | DBG("Got %s[len %d]", buf, len); |
clemounet | 2:61ac95f0af72 | 129 | } while( (ret == OK) && (len > 0) ); |
clemounet | 2:61ac95f0af72 | 130 | return NET_CONN; |
clemounet | 2:61ac95f0af72 | 131 | } |
clemounet | 2:61ac95f0af72 | 132 | |
clemounet | 2:61ac95f0af72 | 133 | DBG("Transport link open"); |
clemounet | 2:61ac95f0af72 | 134 | if(datarate != 0) |
clemounet | 2:61ac95f0af72 | 135 | DBG("Datarate: %d bps", datarate); |
clemounet | 2:61ac95f0af72 | 136 | return OK; |
clemounet | 1:fbf17fb09581 | 137 | } |
clemounet | 1:fbf17fb09581 | 138 | |
clemounet | 2:61ac95f0af72 | 139 | int PPPIPInterface::escape(void) { |
clemounet | 2:61ac95f0af72 | 140 | DBG("Sending %s", ESCAPE_SEQ); |
clemounet | 2:61ac95f0af72 | 141 | Thread::wait(1000); |
clemounet | 2:61ac95f0af72 | 142 | int ret = pppStream->write((uint8_t*)ESCAPE_SEQ, strlen(ESCAPE_SEQ), osWaitForever); |
clemounet | 2:61ac95f0af72 | 143 | Thread::wait(1000); |
clemounet | 2:61ac95f0af72 | 144 | if( ret != OK ) |
clemounet | 2:61ac95f0af72 | 145 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 146 | return OK; |
clemounet | 2:61ac95f0af72 | 147 | } |
clemounet | 2:61ac95f0af72 | 148 | |
clemounet | 2:61ac95f0af72 | 149 | int PPPIPInterface::connect(const char* user, const char* pw) { |
clemounet | 2:61ac95f0af72 | 150 | |
clemounet | 2:61ac95f0af72 | 151 | DBG("Trying to connect with PPP"); |
clemounet | 2:61ac95f0af72 | 152 | |
clemounet | 2:61ac95f0af72 | 153 | // Init the lWIP Stacks |
clemounet | 2:61ac95f0af72 | 154 | stackInits(user,pw); |
clemounet | 2:61ac95f0af72 | 155 | |
clemounet | 2:61ac95f0af72 | 156 | // Clear the line |
clemounet | 2:61ac95f0af72 | 157 | cleanupLink(); |
clemounet | 2:61ac95f0af72 | 158 | |
clemounet | 2:61ac95f0af72 | 159 | // Do the dialing |
clemounet | 2:61ac95f0af72 | 160 | dial(); |
clemounet | 2:61ac95f0af72 | 161 | |
clemounet | 2:61ac95f0af72 | 162 | // Launch the ppp session |
clemounet | 2:61ac95f0af72 | 163 | pppSession = pppOverSerialOpen(this, PPPIPInterface::linkStatusCb, this); |
clemounet | 2:61ac95f0af72 | 164 | DBG("PPP over serial opening returned = %d, pppSession"); |
clemounet | 2:61ac95f0af72 | 165 | if (pppSession >= 0) { |
clemounet | 2:61ac95f0af72 | 166 | // the thread was successfully started. |
clemounet | 2:61ac95f0af72 | 167 | int retries = 600; // Approx (600*100ms) = 60s |
clemounet | 2:61ac95f0af72 | 168 | while (!connected && (retries-- > 0)) { |
clemounet | 2:61ac95f0af72 | 169 | Thread::wait(100); |
clemounet | 2:61ac95f0af72 | 170 | } |
clemounet | 2:61ac95f0af72 | 171 | |
clemounet | 2:61ac95f0af72 | 172 | if (retries!=0) { |
clemounet | 2:61ac95f0af72 | 173 | // We are connected on lwIP over PPP! |
clemounet | 2:61ac95f0af72 | 174 | DBG("Got Connected"); |
clemounet | 2:61ac95f0af72 | 175 | return OK; |
clemounet | 2:61ac95f0af72 | 176 | } else { |
clemounet | 2:61ac95f0af72 | 177 | DBG("Got a Timeout"); |
clemounet | 2:61ac95f0af72 | 178 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 179 | } |
clemounet | 2:61ac95f0af72 | 180 | }else { |
clemounet | 2:61ac95f0af72 | 181 | DBG("Could not get ppp session"); |
clemounet | 2:61ac95f0af72 | 182 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 183 | } |
clemounet | 2:61ac95f0af72 | 184 | } |
clemounet | 2:61ac95f0af72 | 185 | |
clemounet | 2:61ac95f0af72 | 186 | int PPPIPInterface::disconnect() { |
clemounet | 2:61ac95f0af72 | 187 | DBG("Do something on ppp stack and close the session"); |
clemounet | 2:61ac95f0af72 | 188 | pppClose(pppSession); |
clemounet | 2:61ac95f0af72 | 189 | escape(); |
clemounet | 2:61ac95f0af72 | 190 | cleanupLink(); |
clemounet | 2:61ac95f0af72 | 191 | return OK; |
clemounet | 1:fbf17fb09581 | 192 | } |
clemounet | 1:fbf17fb09581 | 193 | |
clemounet | 2:61ac95f0af72 | 194 | void PPPIPInterface::setConnected(bool val) { |
clemounet | 2:61ac95f0af72 | 195 | connected = val; |
clemounet | 2:61ac95f0af72 | 196 | } |
clemounet | 2:61ac95f0af72 | 197 | |
clemounet | 2:61ac95f0af72 | 198 | bool PPPIPInterface::isConnected(void) { |
clemounet | 2:61ac95f0af72 | 199 | return connected; |
clemounet | 1:fbf17fb09581 | 200 | } |
clemounet | 1:fbf17fb09581 | 201 | |
clemounet | 2:61ac95f0af72 | 202 | int PPPIPInterface::cleanupLink() { |
clemounet | 2:61ac95f0af72 | 203 | int ret; |
clemounet | 2:61ac95f0af72 | 204 | char buf[32]; |
clemounet | 2:61ac95f0af72 | 205 | size_t len; |
clemounet | 2:61ac95f0af72 | 206 | |
clemounet | 2:61ac95f0af72 | 207 | do { //Clear buf |
clemounet | 2:61ac95f0af72 | 208 | ret = pppStream->read((uint8_t*)buf, &len, 32, 100); |
clemounet | 2:61ac95f0af72 | 209 | if(ret == OK) { |
clemounet | 2:61ac95f0af72 | 210 | buf[len] = '\0'; |
clemounet | 2:61ac95f0af72 | 211 | DBG("Got %s", buf); |
clemounet | 2:61ac95f0af72 | 212 | } |
clemounet | 2:61ac95f0af72 | 213 | } while( (ret == OK) && (len > 0) ); |
clemounet | 2:61ac95f0af72 | 214 | |
clemounet | 2:61ac95f0af72 | 215 | DBG("Sending %s", HANGUP_CMD); |
clemounet | 2:61ac95f0af72 | 216 | |
clemounet | 2:61ac95f0af72 | 217 | // Here we could need to pass the ATStream to hangup |
clemounet | 2:61ac95f0af72 | 218 | IOStream *hangupPort = pppStream; |
clemounet | 2:61ac95f0af72 | 219 | ret = hangupPort->write((uint8_t*)HANGUP_CMD, strlen(HANGUP_CMD), osWaitForever); |
clemounet | 2:61ac95f0af72 | 220 | if( ret != OK ) |
clemounet | 1:fbf17fb09581 | 221 | return NET_UNKNOWN; |
clemounet | 2:61ac95f0af72 | 222 | |
clemounet | 2:61ac95f0af72 | 223 | size_t readLen; |
clemounet | 2:61ac95f0af72 | 224 | |
clemounet | 2:61ac95f0af72 | 225 | //Hangup |
clemounet | 2:61ac95f0af72 | 226 | DBG("Expect %s", HANGUP_CMD); |
clemounet | 2:61ac95f0af72 | 227 | |
clemounet | 2:61ac95f0af72 | 228 | len = 0; |
clemounet | 2:61ac95f0af72 | 229 | while( len < strlen(HANGUP_CMD) ) { |
clemounet | 2:61ac95f0af72 | 230 | ret = hangupPort->read((uint8_t*)buf + len, &readLen, strlen(HANGUP_CMD) - len, 100); |
clemounet | 2:61ac95f0af72 | 231 | if( ret != OK ) |
clemounet | 2:61ac95f0af72 | 232 | break; |
clemounet | 2:61ac95f0af72 | 233 | len += readLen; |
clemounet | 2:61ac95f0af72 | 234 | ///// |
clemounet | 2:61ac95f0af72 | 235 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 236 | DBG("Got %s", buf); |
clemounet | 2:61ac95f0af72 | 237 | } |
clemounet | 2:61ac95f0af72 | 238 | |
clemounet | 2:61ac95f0af72 | 239 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 240 | |
clemounet | 2:61ac95f0af72 | 241 | DBG("Got %s[len %d]", buf, len); |
clemounet | 2:61ac95f0af72 | 242 | |
clemounet | 2:61ac95f0af72 | 243 | //OK response |
clemounet | 2:61ac95f0af72 | 244 | DBG("Expect %s", OK_RESP); |
clemounet | 2:61ac95f0af72 | 245 | |
clemounet | 2:61ac95f0af72 | 246 | len = 0; |
clemounet | 2:61ac95f0af72 | 247 | while( len < strlen(OK_RESP) ) { |
clemounet | 2:61ac95f0af72 | 248 | ret = hangupPort->read((uint8_t*)buf + len, &readLen, strlen(OK_RESP) - len, 100); |
clemounet | 2:61ac95f0af72 | 249 | if( ret != OK ) { |
clemounet | 2:61ac95f0af72 | 250 | break; |
clemounet | 2:61ac95f0af72 | 251 | } |
clemounet | 2:61ac95f0af72 | 252 | len += readLen; |
clemounet | 2:61ac95f0af72 | 253 | ///// |
clemounet | 2:61ac95f0af72 | 254 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 255 | DBG("Got %s", buf); |
clemounet | 2:61ac95f0af72 | 256 | } |
clemounet | 2:61ac95f0af72 | 257 | |
clemounet | 2:61ac95f0af72 | 258 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 259 | |
clemounet | 2:61ac95f0af72 | 260 | DBG("Got %s[len %d]", buf, len); |
clemounet | 2:61ac95f0af72 | 261 | |
clemounet | 2:61ac95f0af72 | 262 | //NO CARRIER event |
clemounet | 2:61ac95f0af72 | 263 | DBG("Expect %s", NO_CARRIER_RESP); |
clemounet | 2:61ac95f0af72 | 264 | |
clemounet | 2:61ac95f0af72 | 265 | len = 0; |
clemounet | 2:61ac95f0af72 | 266 | while( len < strlen(NO_CARRIER_RESP) ) { |
clemounet | 2:61ac95f0af72 | 267 | ret = pppStream->read((uint8_t*)buf + len, &readLen, strlen(NO_CARRIER_RESP) - len, 100); |
clemounet | 2:61ac95f0af72 | 268 | if( ret != OK ) { |
clemounet | 2:61ac95f0af72 | 269 | break; |
clemounet | 2:61ac95f0af72 | 270 | } |
clemounet | 2:61ac95f0af72 | 271 | len += readLen; |
clemounet | 2:61ac95f0af72 | 272 | ///// |
clemounet | 2:61ac95f0af72 | 273 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 274 | DBG("Got %s", buf); |
clemounet | 1:fbf17fb09581 | 275 | } |
clemounet | 2:61ac95f0af72 | 276 | |
clemounet | 2:61ac95f0af72 | 277 | buf[len]=0; |
clemounet | 2:61ac95f0af72 | 278 | |
clemounet | 2:61ac95f0af72 | 279 | DBG("Got %s[len %d]", buf, len); |
clemounet | 2:61ac95f0af72 | 280 | |
clemounet | 2:61ac95f0af72 | 281 | do { //Clear buf |
clemounet | 2:61ac95f0af72 | 282 | ret = pppStream->read((uint8_t*)buf, &len, 32, 100); |
clemounet | 2:61ac95f0af72 | 283 | if(ret == OK) { |
clemounet | 2:61ac95f0af72 | 284 | buf[len] = '\0'; |
clemounet | 2:61ac95f0af72 | 285 | DBG("Got %s", buf); |
clemounet | 2:61ac95f0af72 | 286 | } |
clemounet | 2:61ac95f0af72 | 287 | } while( (ret == OK) && (len > 0) ); |
clemounet | 2:61ac95f0af72 | 288 | |
clemounet | 2:61ac95f0af72 | 289 | return OK; |
clemounet | 2:61ac95f0af72 | 290 | } |
clemounet | 2:61ac95f0af72 | 291 | |
clemounet | 2:61ac95f0af72 | 292 | // IP Stuffs |
clemounet | 2:61ac95f0af72 | 293 | char *PPPIPInterface::getIPAddress(void) { |
clemounet | 2:61ac95f0af72 | 294 | return ipAddress; |
clemounet | 2:61ac95f0af72 | 295 | } |
clemounet | 2:61ac95f0af72 | 296 | |
clemounet | 2:61ac95f0af72 | 297 | void PPPIPInterface::setIPAddress(char *ip) { |
clemounet | 2:61ac95f0af72 | 298 | strcpy(ipAddress,ip); |
clemounet | 2:61ac95f0af72 | 299 | } |
clemounet | 2:61ac95f0af72 | 300 | |
clemounet | 2:61ac95f0af72 | 301 | // PPP Stuffs |
clemounet | 2:61ac95f0af72 | 302 | extern "C" { |
clemounet | 2:61ac95f0af72 | 303 | u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len) { |
clemounet | 2:61ac95f0af72 | 304 | //DBG("LEN %d",len); |
clemounet | 2:61ac95f0af72 | 305 | //DBG_MEMDUMP("IN",(const char*)data,len); |
clemounet | 2:61ac95f0af72 | 306 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
clemounet | 2:61ac95f0af72 | 307 | USBSerialStream *s = pIf->pppStream; |
clemounet | 2:61ac95f0af72 | 308 | int ret = s->write(data, len, osWaitForever); //Blocks until all data is sent or an error happens |
clemounet | 2:61ac95f0af72 | 309 | //DBG("sio_write OUT"); |
clemounet | 2:61ac95f0af72 | 310 | if(ret != OK) { |
clemounet | 2:61ac95f0af72 | 311 | return 0; |
clemounet | 2:61ac95f0af72 | 312 | } |
clemounet | 2:61ac95f0af72 | 313 | return len; |
clemounet | 2:61ac95f0af72 | 314 | } |
clemounet | 2:61ac95f0af72 | 315 | |
clemounet | 2:61ac95f0af72 | 316 | u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) { |
clemounet | 2:61ac95f0af72 | 317 | //DBG("sio_read"); |
clemounet | 2:61ac95f0af72 | 318 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
clemounet | 2:61ac95f0af72 | 319 | size_t readLen; |
clemounet | 2:61ac95f0af72 | 320 | int ret = pIf->pppStream->read(data, &readLen, len, osWaitForever); //Blocks until some data is received or an error happens |
clemounet | 2:61ac95f0af72 | 321 | if(ret != OK) { |
clemounet | 2:61ac95f0af72 | 322 | return 0; |
clemounet | 2:61ac95f0af72 | 323 | } |
clemounet | 2:61ac95f0af72 | 324 | return readLen; |
clemounet | 2:61ac95f0af72 | 325 | } |
clemounet | 2:61ac95f0af72 | 326 | |
clemounet | 2:61ac95f0af72 | 327 | void sio_read_abort(sio_fd_t fd) { |
clemounet | 2:61ac95f0af72 | 328 | //DBG("sio_read_abort"); |
clemounet | 2:61ac95f0af72 | 329 | PPPIPInterface* pIf = (PPPIPInterface*)fd; |
clemounet | 2:61ac95f0af72 | 330 | pIf->pppStream->abortRead(); |
clemounet | 2:61ac95f0af72 | 331 | } |
clemounet | 1:fbf17fb09581 | 332 | } |
clemounet | 1:fbf17fb09581 | 333 | |
clemounet | 1:fbf17fb09581 | 334 | |
clemounet | 2:61ac95f0af72 | 335 | // LwIP Callbacks |
clemounet | 2:61ac95f0af72 | 336 | void PPPIPInterface::tcpipInitDoneCb(void *ctx) { |
clemounet | 2:61ac95f0af72 | 337 | PPPIPInterface *pIf = (PPPIPInterface*)ctx; |
clemounet | 2:61ac95f0af72 | 338 | pIf->ipInitiated = true; |
clemounet | 1:fbf17fb09581 | 339 | } |
clemounet | 1:fbf17fb09581 | 340 | |
clemounet | 2:61ac95f0af72 | 341 | void PPPIPInterface::linkStatusCb(void *ctx, int errCode, void *arg) { |
clemounet | 2:61ac95f0af72 | 342 | PPPIPInterface* pIf = (PPPIPInterface*)ctx; |
clemounet | 2:61ac95f0af72 | 343 | struct ppp_addrs* addrs = (struct ppp_addrs*) arg; |
clemounet | 1:fbf17fb09581 | 344 | |
clemounet | 2:61ac95f0af72 | 345 | switch(errCode) { |
clemounet | 2:61ac95f0af72 | 346 | case PPPERR_NONE: |
clemounet | 2:61ac95f0af72 | 347 | WARN("Connected via PPP."); |
clemounet | 2:61ac95f0af72 | 348 | DBG("Local IP address: %s", inet_ntoa(addrs->our_ipaddr)); |
clemounet | 2:61ac95f0af72 | 349 | DBG("Netmask: %s", inet_ntoa(addrs->netmask)); |
clemounet | 2:61ac95f0af72 | 350 | DBG("Remote IP address: %s", inet_ntoa(addrs->his_ipaddr)); |
clemounet | 2:61ac95f0af72 | 351 | DBG("Primary DNS: %s", inet_ntoa(addrs->dns1)); |
clemounet | 2:61ac95f0af72 | 352 | DBG("Secondary DNS: %s", inet_ntoa(addrs->dns2)); |
clemounet | 2:61ac95f0af72 | 353 | //Setup DNS |
clemounet | 2:61ac95f0af72 | 354 | if (addrs->dns1.addr != 0) { |
clemounet | 2:61ac95f0af72 | 355 | //dns_setserver(0, (struct ip_addr*)&(addrs->dns1)); |
clemounet | 2:61ac95f0af72 | 356 | } |
clemounet | 2:61ac95f0af72 | 357 | if (addrs->dns2.addr != 0) { |
clemounet | 2:61ac95f0af72 | 358 | //dns_setserver(1, (struct ip_addr*)&(addrs->dns1)); |
clemounet | 2:61ac95f0af72 | 359 | } |
clemounet | 1:fbf17fb09581 | 360 | |
clemounet | 2:61ac95f0af72 | 361 | pIf->setConnected(true); |
clemounet | 2:61ac95f0af72 | 362 | pIf->setIPAddress(inet_ntoa(addrs->our_ipaddr)); |
clemounet | 2:61ac95f0af72 | 363 | break; |
clemounet | 2:61ac95f0af72 | 364 | case PPPERR_CONNECT: //Connection lost |
clemounet | 2:61ac95f0af72 | 365 | WARN("Connection lost/terminated"); |
clemounet | 2:61ac95f0af72 | 366 | pIf->setConnected(false); |
clemounet | 2:61ac95f0af72 | 367 | break; |
clemounet | 2:61ac95f0af72 | 368 | case PPPERR_AUTHFAIL: //Authentication failed |
clemounet | 2:61ac95f0af72 | 369 | WARN("Authentication failed"); |
clemounet | 2:61ac95f0af72 | 370 | pIf->setConnected(false); |
clemounet | 2:61ac95f0af72 | 371 | break; |
clemounet | 2:61ac95f0af72 | 372 | case PPPERR_PROTOCOL: //Protocol error |
clemounet | 2:61ac95f0af72 | 373 | WARN("Protocol error"); |
clemounet | 2:61ac95f0af72 | 374 | pIf->setConnected(false); |
clemounet | 2:61ac95f0af72 | 375 | break; |
clemounet | 2:61ac95f0af72 | 376 | case PPPERR_USER: |
clemounet | 2:61ac95f0af72 | 377 | WARN("Disconnected by user"); |
clemounet | 2:61ac95f0af72 | 378 | pIf->setConnected(false); |
clemounet | 2:61ac95f0af72 | 379 | break; |
clemounet | 2:61ac95f0af72 | 380 | default: |
clemounet | 2:61ac95f0af72 | 381 | WARN("Unknown error (%d)", errCode); |
clemounet | 2:61ac95f0af72 | 382 | pIf->setConnected(false); |
clemounet | 2:61ac95f0af72 | 383 | break; |
clemounet | 2:61ac95f0af72 | 384 | } |
clemounet | 1:fbf17fb09581 | 385 | |
clemounet | 2:61ac95f0af72 | 386 | //pIf->m_linkStatusSphre.wait(0); //If previous event has not been handled, "delete" it now |
clemounet | 2:61ac95f0af72 | 387 | //pIf->m_pppErrCode = errCode; |
clemounet | 2:61ac95f0af72 | 388 | //pIf->m_linkStatusSphre.release(); |
clemounet | 1:fbf17fb09581 | 389 | } |