Changes to support Vodafone K4606

Dependencies:   Socket USBHostWANDongle lwip-sys lwip

Fork of VodafoneUSBModem by mbed official

Committer:
donatien
Date:
Wed Jul 11 12:44:33 2012 +0000
Revision:
9:3f077dde13c9
Parent:
8:04b6a042595f
Child:
11:565b2ec40dea
Send AT Connect command in PPPIPInterface, tweaks in VodafoneK3770 for dual serial port support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 8:04b6a042595f 1 /* PPPIPInterface.cpp */
donatien 8:04b6a042595f 2 /*
donatien 8:04b6a042595f 3 Copyright (C) 2012 ARM Limited.
donatien 8:04b6a042595f 4
donatien 8:04b6a042595f 5 Permission is hereby granted, free of charge, to any person obtaining a copy of
donatien 8:04b6a042595f 6 this software and associated documentation files (the "Software"), to deal in
donatien 8:04b6a042595f 7 the Software without restriction, including without limitation the rights to
donatien 8:04b6a042595f 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
donatien 8:04b6a042595f 9 of the Software, and to permit persons to whom the Software is furnished to do
donatien 8:04b6a042595f 10 so, subject to the following conditions:
donatien 8:04b6a042595f 11
donatien 8:04b6a042595f 12 The above copyright notice and this permission notice shall be included in all
donatien 8:04b6a042595f 13 copies or substantial portions of the Software.
donatien 8:04b6a042595f 14
donatien 8:04b6a042595f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
donatien 8:04b6a042595f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
donatien 8:04b6a042595f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
donatien 8:04b6a042595f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
donatien 8:04b6a042595f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 8:04b6a042595f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
donatien 8:04b6a042595f 21 SOFTWARE.
donatien 8:04b6a042595f 22 */
donatien 8:04b6a042595f 23
donatien 8:04b6a042595f 24 #define __DEBUG__ 2 //Maximum verbosity
donatien 8:04b6a042595f 25 #ifndef __MODULE__
donatien 8:04b6a042595f 26 #define __MODULE__ "PPPIPInterface.cpp"
donatien 8:04b6a042595f 27 #endif
donatien 8:04b6a042595f 28
donatien 8:04b6a042595f 29 #include "core/fwk.h"
donatien 8:04b6a042595f 30 #include "rtos.h"
donatien 8:04b6a042595f 31
donatien 8:04b6a042595f 32 #include "PPPIPInterface.h"
donatien 8:04b6a042595f 33
donatien 8:04b6a042595f 34 extern "C" {
donatien 8:04b6a042595f 35 #include "lwip/ip_addr.h"
donatien 8:04b6a042595f 36 #include "lwip/inet.h"
donatien 8:04b6a042595f 37 #include "netif/ppp/ppp.h"
donatien 8:04b6a042595f 38 }
donatien 8:04b6a042595f 39
donatien 8:04b6a042595f 40 #if NET_PPP
donatien 8:04b6a042595f 41
donatien 8:04b6a042595f 42 PPPIPInterface::PPPIPInterface(IOStream* pStream) : LwIPInterface(), m_linkStatusSphre(1), m_pppErrCode(0), m_pStream(pStream), m_streamAvail(true), m_pppd(-1)
donatien 8:04b6a042595f 43 {
donatien 8:04b6a042595f 44 m_linkStatusSphre.wait();
donatien 8:04b6a042595f 45 }
donatien 8:04b6a042595f 46
donatien 8:04b6a042595f 47 /*virtual*/ PPPIPInterface::~PPPIPInterface()
donatien 8:04b6a042595f 48 {
donatien 8:04b6a042595f 49
donatien 8:04b6a042595f 50 }
donatien 8:04b6a042595f 51
donatien 8:04b6a042595f 52 /*virtual*/ int PPPIPInterface::init() //Init PPP-specific stuff, create the right bindings, etc
donatien 8:04b6a042595f 53 {
donatien 8:04b6a042595f 54 DBG("Initializing LwIP");
donatien 8:04b6a042595f 55 LwIPInterface::init(); //Init LwIP, NOT including PPP
donatien 8:04b6a042595f 56 DBG("Initializing PPP");
donatien 8:04b6a042595f 57 pppInit();
donatien 8:04b6a042595f 58 DBG("Done");
donatien 8:04b6a042595f 59 return OK;
donatien 8:04b6a042595f 60 }
donatien 8:04b6a042595f 61
donatien 8:04b6a042595f 62 int PPPIPInterface::setup(const char* user, const char* pw)
donatien 8:04b6a042595f 63 {
donatien 8:04b6a042595f 64 DBG("Configuring PPP authentication method");
donatien 8:04b6a042595f 65 pppSetAuth(PPPAUTHTYPE_ANY, user, pw);
donatien 8:04b6a042595f 66 DBG("Done");
donatien 8:04b6a042595f 67 return OK;
donatien 8:04b6a042595f 68 }
donatien 8:04b6a042595f 69
donatien 9:3f077dde13c9 70 /*virtual*/ int PPPIPInterface::connect(const char* msisdn)
donatien 8:04b6a042595f 71 {
donatien 9:3f077dde13c9 72 int ret;
donatien 9:3f077dde13c9 73 char buf[32];
donatien 9:3f077dde13c9 74 size_t len;
donatien 8:04b6a042595f 75 DBG("Trying to connect with PPP");
donatien 9:3f077dde13c9 76
donatien 9:3f077dde13c9 77 do //Clear buf
donatien 9:3f077dde13c9 78 {
donatien 9:3f077dde13c9 79 ret = m_pStream->read(buf, &len, 32, 0) > 0
donatien 9:3f077dde13c9 80 } while( (ret == OK) && (len > 0) );
donatien 9:3f077dde13c9 81
donatien 9:3f077dde13c9 82 sprintf(buf, "ATD %16s\x0D");
donatien 9:3f077dde13c9 83
donatien 9:3f077dde13c9 84 ret = m_pStream->write(buf, strlen(buf), osWaitForever);
donatien 9:3f077dde13c9 85 if( ret != OK )
donatien 9:3f077dde13c9 86 {
donatien 9:3f077dde13c9 87 return NET_UNKNOWN;
donatien 9:3f077dde13c9 88 }
donatien 9:3f077dde13c9 89
donatien 9:3f077dde13c9 90 const char expected = "\x0D\0x0ACONNECT\x0D\0x0A";
donatien 9:3f077dde13c9 91
donatien 9:3f077dde13c9 92 ret = m_pStream->read(buf, strlen(expected), &len, 3000);
donatien 9:3f077dde13c9 93 if( ret != OK )
donatien 9:3f077dde13c9 94 {
donatien 9:3f077dde13c9 95 return NET_UNKNOWN;
donatien 9:3f077dde13c9 96 }
donatien 9:3f077dde13c9 97
donatien 9:3f077dde13c9 98 if( (len < strlen(expected)) || (memcmp(expected, buf) != 0) )
donatien 9:3f077dde13c9 99 {
donatien 9:3f077dde13c9 100 //Discard buffer
donatien 9:3f077dde13c9 101 do //Clear buf
donatien 9:3f077dde13c9 102 {
donatien 9:3f077dde13c9 103 ret = m_pStream->read(buf, &len, 32, 0) > 0
donatien 9:3f077dde13c9 104 } while( (ret == OK) && (len > 0) );
donatien 9:3f077dde13c9 105 return NET_CONN;
donatien 9:3f077dde13c9 106 }
donatien 9:3f077dde13c9 107
donatien 9:3f077dde13c9 108 DBG("Transport link open");
donatien 8:04b6a042595f 109 m_linkStatusSphre.wait(0);
donatien 8:04b6a042595f 110 if((m_pppd != -1) && (m_pppErrCode == 0)) //Already connected
donatien 8:04b6a042595f 111 {
donatien 8:04b6a042595f 112 return NET_INVALID;
donatien 8:04b6a042595f 113 }
donatien 8:04b6a042595f 114 int ret = pppOverSerialOpen(this, PPPIPInterface::linkStatusCb, this);
donatien 8:04b6a042595f 115 if(ret < 0)
donatien 8:04b6a042595f 116 {
donatien 8:04b6a042595f 117 switch(ret)
donatien 8:04b6a042595f 118 {
donatien 8:04b6a042595f 119 case PPPERR_OPEN:
donatien 8:04b6a042595f 120 default:
donatien 8:04b6a042595f 121 return NET_FULL; //All available resources are already used
donatien 8:04b6a042595f 122 }
donatien 8:04b6a042595f 123 }
donatien 8:04b6a042595f 124 m_pppd = ret; //PPP descriptor
donatien 8:04b6a042595f 125 m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there
donatien 8:04b6a042595f 126 if(m_pppErrCode != PPPERR_NONE)
donatien 8:04b6a042595f 127 {
donatien 8:04b6a042595f 128 m_pppd = -1;
donatien 8:04b6a042595f 129 }
donatien 8:04b6a042595f 130 switch(m_pppErrCode)
donatien 8:04b6a042595f 131 {
donatien 8:04b6a042595f 132 case PPPERR_NONE: //Connected OK
donatien 8:04b6a042595f 133 return OK;
donatien 8:04b6a042595f 134 case PPPERR_CONNECT: //Connection lost
donatien 8:04b6a042595f 135 return NET_INTERRUPTED;
donatien 8:04b6a042595f 136 case PPPERR_AUTHFAIL: //Authentication failed
donatien 8:04b6a042595f 137 return NET_AUTH;
donatien 8:04b6a042595f 138 case PPPERR_PROTOCOL: //Protocol error
donatien 8:04b6a042595f 139 return NET_PROTOCOL;
donatien 8:04b6a042595f 140 default:
donatien 8:04b6a042595f 141 return NET_UNKNOWN;
donatien 8:04b6a042595f 142 }
donatien 8:04b6a042595f 143 }
donatien 8:04b6a042595f 144
donatien 8:04b6a042595f 145 /*virtual*/ int PPPIPInterface::disconnect()
donatien 8:04b6a042595f 146 {
donatien 8:04b6a042595f 147 int ret = m_linkStatusSphre.wait(0);
donatien 8:04b6a042595f 148 if(ret > 0) //Already disconnected?
donatien 8:04b6a042595f 149 {
donatien 8:04b6a042595f 150 m_pppd = -1; //Discard PPP descriptor
donatien 8:04b6a042595f 151 switch(m_pppErrCode)
donatien 8:04b6a042595f 152 {
donatien 8:04b6a042595f 153 case PPPERR_CONNECT: //Connection terminated
donatien 8:04b6a042595f 154 case PPPERR_AUTHFAIL: //Authentication failed
donatien 8:04b6a042595f 155 case PPPERR_PROTOCOL: //Protocol error
donatien 8:04b6a042595f 156 case PPPERR_USER:
donatien 8:04b6a042595f 157 return OK;
donatien 8:04b6a042595f 158 default:
donatien 8:04b6a042595f 159 return NET_UNKNOWN;
donatien 8:04b6a042595f 160 }
donatien 8:04b6a042595f 161 }
donatien 8:04b6a042595f 162 else
donatien 8:04b6a042595f 163 {
donatien 8:04b6a042595f 164 if(m_pppd == -1)
donatien 8:04b6a042595f 165 {
donatien 8:04b6a042595f 166 return NET_INVALID;
donatien 8:04b6a042595f 167 }
donatien 8:04b6a042595f 168 pppClose(m_pppd);
donatien 8:04b6a042595f 169 do
donatien 8:04b6a042595f 170 {
donatien 8:04b6a042595f 171 m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there
donatien 8:04b6a042595f 172 DBG("Received PPP err code %d", m_pppErrCode);
donatien 8:04b6a042595f 173 } while(m_pppErrCode != PPPERR_USER);
donatien 8:04b6a042595f 174 m_pppd = -1; //Discard PPP descriptor
donatien 8:04b6a042595f 175 }
donatien 8:04b6a042595f 176 return OK;
donatien 8:04b6a042595f 177 }
donatien 8:04b6a042595f 178
donatien 8:04b6a042595f 179 /*static*/ void PPPIPInterface::linkStatusCb(void *ctx, int errCode, void *arg) //PPP link status
donatien 8:04b6a042595f 180 {
donatien 8:04b6a042595f 181 PPPIPInterface* pIf = (PPPIPInterface*)ctx;
donatien 8:04b6a042595f 182 struct ppp_addrs* addrs = (struct ppp_addrs*) arg;
donatien 8:04b6a042595f 183
donatien 8:04b6a042595f 184 switch(errCode)
donatien 8:04b6a042595f 185 {
donatien 8:04b6a042595f 186 case PPPERR_NONE:
donatien 8:04b6a042595f 187 WARN("Connected via PPP.");
donatien 8:04b6a042595f 188 DBG("Local IP address: %s", inet_ntoa(addrs->our_ipaddr));
donatien 8:04b6a042595f 189 DBG("Netmask: %s", inet_ntoa(addrs->netmask));
donatien 8:04b6a042595f 190 DBG("Remote IP address: %s", inet_ntoa(addrs->his_ipaddr));
donatien 8:04b6a042595f 191 DBG("Primary DNS: %s", inet_ntoa(addrs->dns1));
donatien 8:04b6a042595f 192 DBG("Secondary DNS: %s", inet_ntoa(addrs->dns2));
donatien 8:04b6a042595f 193 setConnected(true);
donatien 8:04b6a042595f 194 setIPAddress(inet_ntoa(addrs->our_ipaddr));
donatien 8:04b6a042595f 195 break;
donatien 8:04b6a042595f 196 case PPPERR_CONNECT: //Connection lost
donatien 8:04b6a042595f 197 WARN("Connection lost/terminated");
donatien 8:04b6a042595f 198 setConnected(false);
donatien 8:04b6a042595f 199 break;
donatien 8:04b6a042595f 200 case PPPERR_AUTHFAIL: //Authentication failed
donatien 8:04b6a042595f 201 WARN("Authentication failed");
donatien 8:04b6a042595f 202 setConnected(false);
donatien 8:04b6a042595f 203 break;
donatien 8:04b6a042595f 204 case PPPERR_PROTOCOL: //Protocol error
donatien 8:04b6a042595f 205 WARN("Protocol error");
donatien 8:04b6a042595f 206 setConnected(false);
donatien 8:04b6a042595f 207 break;
donatien 8:04b6a042595f 208 case PPPERR_USER:
donatien 8:04b6a042595f 209 WARN("Disconnected by user");
donatien 8:04b6a042595f 210 setConnected(false);
donatien 8:04b6a042595f 211 break;
donatien 8:04b6a042595f 212 default:
donatien 8:04b6a042595f 213 WARN("Unknown error (%d)", errCode);
donatien 8:04b6a042595f 214 setConnected(false);
donatien 8:04b6a042595f 215 break;
donatien 8:04b6a042595f 216 }
donatien 8:04b6a042595f 217
donatien 8:04b6a042595f 218 pIf->m_linkStatusSphre.wait(0); //If previous event has not been handled, "delete" it now
donatien 8:04b6a042595f 219 pIf->m_pppErrCode = errCode;
donatien 8:04b6a042595f 220 pIf->m_linkStatusSphre.release();
donatien 8:04b6a042595f 221 }
donatien 8:04b6a042595f 222
donatien 8:04b6a042595f 223 //LwIP PPP implementation
donatien 8:04b6a042595f 224 extern "C"
donatien 8:04b6a042595f 225 {
donatien 8:04b6a042595f 226
donatien 8:04b6a042595f 227 /**
donatien 8:04b6a042595f 228 * Writes to the serial device.
donatien 8:04b6a042595f 229 *
donatien 8:04b6a042595f 230 * @param fd serial device handle
donatien 8:04b6a042595f 231 * @param data pointer to data to send
donatien 8:04b6a042595f 232 * @param len length (in bytes) of data to send
donatien 8:04b6a042595f 233 * @return number of bytes actually sent
donatien 8:04b6a042595f 234 *
donatien 8:04b6a042595f 235 * @note This function will block until all data can be sent.
donatien 8:04b6a042595f 236 */
donatien 8:04b6a042595f 237 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
donatien 8:04b6a042595f 238 {
donatien 8:04b6a042595f 239 DBG("sio_write");
donatien 8:04b6a042595f 240 PPPIPInterface* pIf = (PPPIPInterface*)fd;
donatien 8:04b6a042595f 241 int ret;
donatien 8:04b6a042595f 242 if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further
donatien 8:04b6a042595f 243 {
donatien 8:04b6a042595f 244 return 0;
donatien 8:04b6a042595f 245 }
donatien 8:04b6a042595f 246 ret = pIf->m_pStream->write(data, len, osWaitForever); //Blocks until all data is sent or an error happens
donatien 8:04b6a042595f 247 if(ret != OK)
donatien 8:04b6a042595f 248 {
donatien 8:04b6a042595f 249 return 0;
donatien 8:04b6a042595f 250 }
donatien 8:04b6a042595f 251 return len;
donatien 8:04b6a042595f 252 }
donatien 8:04b6a042595f 253
donatien 8:04b6a042595f 254 /**
donatien 8:04b6a042595f 255 * Reads from the serial device.
donatien 8:04b6a042595f 256 *
donatien 8:04b6a042595f 257 * @param fd serial device handle
donatien 8:04b6a042595f 258 * @param data pointer to data buffer for receiving
donatien 8:04b6a042595f 259 * @param len maximum length (in bytes) of data to receive
donatien 8:04b6a042595f 260 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
donatien 8:04b6a042595f 261 *
donatien 8:04b6a042595f 262 * @note This function will block until data can be received. The blocking
donatien 8:04b6a042595f 263 * can be cancelled by calling sio_read_abort().
donatien 8:04b6a042595f 264 */
donatien 8:04b6a042595f 265 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
donatien 8:04b6a042595f 266 {
donatien 8:04b6a042595f 267 DBG("sio_read");
donatien 8:04b6a042595f 268 PPPIPInterface* pIf = (PPPIPInterface*)fd;
donatien 8:04b6a042595f 269 int ret;
donatien 8:04b6a042595f 270 size_t readLen;
donatien 8:04b6a042595f 271 if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further
donatien 8:04b6a042595f 272 {
donatien 8:04b6a042595f 273 WARN("EXIT NOT AVAIL");
donatien 8:04b6a042595f 274 return 0;
donatien 8:04b6a042595f 275 }
donatien 8:04b6a042595f 276 ret = pIf->m_pStream->read(data, &readLen, len, osWaitForever); //Blocks until some data is received or an error happens
donatien 8:04b6a042595f 277 if(ret != OK)
donatien 8:04b6a042595f 278 {
donatien 8:04b6a042595f 279 return 0;
donatien 8:04b6a042595f 280 }
donatien 8:04b6a042595f 281 DBG("ret");
donatien 8:04b6a042595f 282 return readLen;
donatien 8:04b6a042595f 283 }
donatien 8:04b6a042595f 284
donatien 8:04b6a042595f 285 /**
donatien 8:04b6a042595f 286 * Aborts a blocking sio_read() call.
donatien 8:04b6a042595f 287 *
donatien 8:04b6a042595f 288 * @param fd serial device handle
donatien 8:04b6a042595f 289 */
donatien 8:04b6a042595f 290 void sio_read_abort(sio_fd_t fd)
donatien 8:04b6a042595f 291 {
donatien 8:04b6a042595f 292 DBG("sio_read_abort");
donatien 8:04b6a042595f 293 PPPIPInterface* pIf = (PPPIPInterface*)fd;
donatien 8:04b6a042595f 294 if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further
donatien 8:04b6a042595f 295 {
donatien 8:04b6a042595f 296 return;
donatien 8:04b6a042595f 297 }
donatien 8:04b6a042595f 298 pIf->m_pStream->abortRead();
donatien 8:04b6a042595f 299 DBG("ret");
donatien 8:04b6a042595f 300 }
donatien 8:04b6a042595f 301
donatien 8:04b6a042595f 302 }
donatien 8:04b6a042595f 303
donatien 8:04b6a042595f 304 #endif
donatien 8:04b6a042595f 305