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 mbed official

/media/uploads/phsfan/sma01_003.png

Committer:
donatien
Date:
Fri Jul 06 08:56:32 2012 +0000
Revision:
10:21a6f09d5631
Parent:
8:04b6a042595f
Child:
11:565b2ec40dea
Dual serial ports 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 PPPIPInterface::PPPIPInterface(IOStream* pStream) : LwIPInterface(), m_linkStatusSphre(1), m_pppErrCode(0), m_pStream(pStream), m_streamAvail(true), m_pppd(-1)
donatien 8:04b6a042595f 41 {
donatien 8:04b6a042595f 42 m_linkStatusSphre.wait();
donatien 8:04b6a042595f 43 }
donatien 8:04b6a042595f 44
donatien 8:04b6a042595f 45 /*virtual*/ PPPIPInterface::~PPPIPInterface()
donatien 8:04b6a042595f 46 {
donatien 8:04b6a042595f 47
donatien 8:04b6a042595f 48 }
donatien 8:04b6a042595f 49
donatien 8:04b6a042595f 50 /*virtual*/ int PPPIPInterface::init() //Init PPP-specific stuff, create the right bindings, etc
donatien 8:04b6a042595f 51 {
donatien 8:04b6a042595f 52 DBG("Initializing LwIP");
donatien 8:04b6a042595f 53 LwIPInterface::init(); //Init LwIP, NOT including PPP
donatien 8:04b6a042595f 54 DBG("Initializing PPP");
donatien 8:04b6a042595f 55 pppInit();
donatien 8:04b6a042595f 56 DBG("Done");
donatien 8:04b6a042595f 57 return OK;
donatien 8:04b6a042595f 58 }
donatien 8:04b6a042595f 59
donatien 8:04b6a042595f 60 int PPPIPInterface::setup(const char* user, const char* pw)
donatien 8:04b6a042595f 61 {
donatien 8:04b6a042595f 62 DBG("Configuring PPP authentication method");
donatien 8:04b6a042595f 63 pppSetAuth(PPPAUTHTYPE_ANY, user, pw);
donatien 8:04b6a042595f 64 DBG("Done");
donatien 8:04b6a042595f 65 return OK;
donatien 8:04b6a042595f 66 }
donatien 8:04b6a042595f 67
donatien 8:04b6a042595f 68 /*virtual*/ int PPPIPInterface::connect()
donatien 8:04b6a042595f 69 {
donatien 8:04b6a042595f 70 DBG("Trying to connect with PPP");
donatien 8:04b6a042595f 71 m_linkStatusSphre.wait(0);
donatien 8:04b6a042595f 72 if((m_pppd != -1) && (m_pppErrCode == 0)) //Already connected
donatien 8:04b6a042595f 73 {
donatien 8:04b6a042595f 74 return NET_INVALID;
donatien 8:04b6a042595f 75 }
donatien 8:04b6a042595f 76 int ret = pppOverSerialOpen(this, PPPIPInterface::linkStatusCb, this);
donatien 8:04b6a042595f 77 if(ret < 0)
donatien 8:04b6a042595f 78 {
donatien 8:04b6a042595f 79 switch(ret)
donatien 8:04b6a042595f 80 {
donatien 8:04b6a042595f 81 case PPPERR_OPEN:
donatien 8:04b6a042595f 82 default:
donatien 8:04b6a042595f 83 return NET_FULL; //All available resources are already used
donatien 8:04b6a042595f 84 }
donatien 8:04b6a042595f 85 }
donatien 8:04b6a042595f 86 m_pppd = ret; //PPP descriptor
donatien 8:04b6a042595f 87 m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there
donatien 8:04b6a042595f 88 if(m_pppErrCode != PPPERR_NONE)
donatien 8:04b6a042595f 89 {
donatien 8:04b6a042595f 90 m_pppd = -1;
donatien 8:04b6a042595f 91 }
donatien 8:04b6a042595f 92 switch(m_pppErrCode)
donatien 8:04b6a042595f 93 {
donatien 8:04b6a042595f 94 case PPPERR_NONE: //Connected OK
donatien 8:04b6a042595f 95 return OK;
donatien 8:04b6a042595f 96 case PPPERR_CONNECT: //Connection lost
donatien 8:04b6a042595f 97 return NET_INTERRUPTED;
donatien 8:04b6a042595f 98 case PPPERR_AUTHFAIL: //Authentication failed
donatien 8:04b6a042595f 99 return NET_AUTH;
donatien 8:04b6a042595f 100 case PPPERR_PROTOCOL: //Protocol error
donatien 8:04b6a042595f 101 return NET_PROTOCOL;
donatien 8:04b6a042595f 102 default:
donatien 8:04b6a042595f 103 return NET_UNKNOWN;
donatien 8:04b6a042595f 104 }
donatien 8:04b6a042595f 105 }
donatien 8:04b6a042595f 106
donatien 8:04b6a042595f 107 /*virtual*/ int PPPIPInterface::disconnect()
donatien 8:04b6a042595f 108 {
donatien 8:04b6a042595f 109 int ret = m_linkStatusSphre.wait(0);
donatien 8:04b6a042595f 110 if(ret > 0) //Already disconnected?
donatien 8:04b6a042595f 111 {
donatien 8:04b6a042595f 112 m_pppd = -1; //Discard PPP descriptor
donatien 8:04b6a042595f 113 switch(m_pppErrCode)
donatien 8:04b6a042595f 114 {
donatien 8:04b6a042595f 115 case PPPERR_CONNECT: //Connection terminated
donatien 8:04b6a042595f 116 case PPPERR_AUTHFAIL: //Authentication failed
donatien 8:04b6a042595f 117 case PPPERR_PROTOCOL: //Protocol error
donatien 8:04b6a042595f 118 case PPPERR_USER:
donatien 8:04b6a042595f 119 return OK;
donatien 8:04b6a042595f 120 default:
donatien 8:04b6a042595f 121 return NET_UNKNOWN;
donatien 8:04b6a042595f 122 }
donatien 8:04b6a042595f 123 }
donatien 8:04b6a042595f 124 else
donatien 8:04b6a042595f 125 {
donatien 8:04b6a042595f 126 if(m_pppd == -1)
donatien 8:04b6a042595f 127 {
donatien 8:04b6a042595f 128 return NET_INVALID;
donatien 8:04b6a042595f 129 }
donatien 8:04b6a042595f 130 pppClose(m_pppd);
donatien 8:04b6a042595f 131 do
donatien 8:04b6a042595f 132 {
donatien 8:04b6a042595f 133 m_linkStatusSphre.wait(); //Block indefinitely; there should be a timeout there
donatien 8:04b6a042595f 134 DBG("Received PPP err code %d", m_pppErrCode);
donatien 8:04b6a042595f 135 } while(m_pppErrCode != PPPERR_USER);
donatien 8:04b6a042595f 136 m_pppd = -1; //Discard PPP descriptor
donatien 8:04b6a042595f 137 }
donatien 8:04b6a042595f 138 return OK;
donatien 8:04b6a042595f 139 }
donatien 8:04b6a042595f 140
donatien 8:04b6a042595f 141 /*static*/ void PPPIPInterface::linkStatusCb(void *ctx, int errCode, void *arg) //PPP link status
donatien 8:04b6a042595f 142 {
donatien 8:04b6a042595f 143 PPPIPInterface* pIf = (PPPIPInterface*)ctx;
donatien 8:04b6a042595f 144 struct ppp_addrs* addrs = (struct ppp_addrs*) arg;
donatien 8:04b6a042595f 145
donatien 8:04b6a042595f 146 switch(errCode)
donatien 8:04b6a042595f 147 {
donatien 8:04b6a042595f 148 case PPPERR_NONE:
donatien 8:04b6a042595f 149 WARN("Connected via PPP.");
donatien 8:04b6a042595f 150 DBG("Local IP address: %s", inet_ntoa(addrs->our_ipaddr));
donatien 8:04b6a042595f 151 DBG("Netmask: %s", inet_ntoa(addrs->netmask));
donatien 8:04b6a042595f 152 DBG("Remote IP address: %s", inet_ntoa(addrs->his_ipaddr));
donatien 8:04b6a042595f 153 DBG("Primary DNS: %s", inet_ntoa(addrs->dns1));
donatien 8:04b6a042595f 154 DBG("Secondary DNS: %s", inet_ntoa(addrs->dns2));
donatien 10:21a6f09d5631 155 pIf->setConnected(true);
donatien 10:21a6f09d5631 156 pIf->setIPAddress(inet_ntoa(addrs->our_ipaddr));
donatien 8:04b6a042595f 157 break;
donatien 8:04b6a042595f 158 case PPPERR_CONNECT: //Connection lost
donatien 8:04b6a042595f 159 WARN("Connection lost/terminated");
donatien 10:21a6f09d5631 160 pIf->setConnected(false);
donatien 8:04b6a042595f 161 break;
donatien 8:04b6a042595f 162 case PPPERR_AUTHFAIL: //Authentication failed
donatien 8:04b6a042595f 163 WARN("Authentication failed");
donatien 10:21a6f09d5631 164 pIf->setConnected(false);
donatien 8:04b6a042595f 165 break;
donatien 8:04b6a042595f 166 case PPPERR_PROTOCOL: //Protocol error
donatien 8:04b6a042595f 167 WARN("Protocol error");
donatien 10:21a6f09d5631 168 pIf->setConnected(false);
donatien 8:04b6a042595f 169 break;
donatien 8:04b6a042595f 170 case PPPERR_USER:
donatien 8:04b6a042595f 171 WARN("Disconnected by user");
donatien 10:21a6f09d5631 172 pIf->setConnected(false);
donatien 8:04b6a042595f 173 break;
donatien 8:04b6a042595f 174 default:
donatien 8:04b6a042595f 175 WARN("Unknown error (%d)", errCode);
donatien 10:21a6f09d5631 176 pIf->setConnected(false);
donatien 8:04b6a042595f 177 break;
donatien 8:04b6a042595f 178 }
donatien 8:04b6a042595f 179
donatien 8:04b6a042595f 180 pIf->m_linkStatusSphre.wait(0); //If previous event has not been handled, "delete" it now
donatien 8:04b6a042595f 181 pIf->m_pppErrCode = errCode;
donatien 8:04b6a042595f 182 pIf->m_linkStatusSphre.release();
donatien 8:04b6a042595f 183 }
donatien 8:04b6a042595f 184
donatien 8:04b6a042595f 185 //LwIP PPP implementation
donatien 8:04b6a042595f 186 extern "C"
donatien 8:04b6a042595f 187 {
donatien 8:04b6a042595f 188
donatien 8:04b6a042595f 189 /**
donatien 8:04b6a042595f 190 * Writes to the serial device.
donatien 8:04b6a042595f 191 *
donatien 8:04b6a042595f 192 * @param fd serial device handle
donatien 8:04b6a042595f 193 * @param data pointer to data to send
donatien 8:04b6a042595f 194 * @param len length (in bytes) of data to send
donatien 8:04b6a042595f 195 * @return number of bytes actually sent
donatien 8:04b6a042595f 196 *
donatien 8:04b6a042595f 197 * @note This function will block until all data can be sent.
donatien 8:04b6a042595f 198 */
donatien 8:04b6a042595f 199 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
donatien 8:04b6a042595f 200 {
donatien 8:04b6a042595f 201 DBG("sio_write");
donatien 8:04b6a042595f 202 PPPIPInterface* pIf = (PPPIPInterface*)fd;
donatien 8:04b6a042595f 203 int ret;
donatien 8:04b6a042595f 204 if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further
donatien 8:04b6a042595f 205 {
donatien 8:04b6a042595f 206 return 0;
donatien 8:04b6a042595f 207 }
donatien 8:04b6a042595f 208 ret = pIf->m_pStream->write(data, len, osWaitForever); //Blocks until all data is sent or an error happens
donatien 8:04b6a042595f 209 if(ret != OK)
donatien 8:04b6a042595f 210 {
donatien 8:04b6a042595f 211 return 0;
donatien 8:04b6a042595f 212 }
donatien 8:04b6a042595f 213 return len;
donatien 8:04b6a042595f 214 }
donatien 8:04b6a042595f 215
donatien 8:04b6a042595f 216 /**
donatien 8:04b6a042595f 217 * Reads from the serial device.
donatien 8:04b6a042595f 218 *
donatien 8:04b6a042595f 219 * @param fd serial device handle
donatien 8:04b6a042595f 220 * @param data pointer to data buffer for receiving
donatien 8:04b6a042595f 221 * @param len maximum length (in bytes) of data to receive
donatien 8:04b6a042595f 222 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
donatien 8:04b6a042595f 223 *
donatien 8:04b6a042595f 224 * @note This function will block until data can be received. The blocking
donatien 8:04b6a042595f 225 * can be cancelled by calling sio_read_abort().
donatien 8:04b6a042595f 226 */
donatien 8:04b6a042595f 227 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
donatien 8:04b6a042595f 228 {
donatien 8:04b6a042595f 229 DBG("sio_read");
donatien 8:04b6a042595f 230 PPPIPInterface* pIf = (PPPIPInterface*)fd;
donatien 8:04b6a042595f 231 int ret;
donatien 8:04b6a042595f 232 size_t readLen;
donatien 8:04b6a042595f 233 if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further
donatien 8:04b6a042595f 234 {
donatien 8:04b6a042595f 235 WARN("EXIT NOT AVAIL");
donatien 8:04b6a042595f 236 return 0;
donatien 8:04b6a042595f 237 }
donatien 8:04b6a042595f 238 ret = pIf->m_pStream->read(data, &readLen, len, osWaitForever); //Blocks until some data is received or an error happens
donatien 8:04b6a042595f 239 if(ret != OK)
donatien 8:04b6a042595f 240 {
donatien 8:04b6a042595f 241 return 0;
donatien 8:04b6a042595f 242 }
donatien 8:04b6a042595f 243 DBG("ret");
donatien 8:04b6a042595f 244 return readLen;
donatien 8:04b6a042595f 245 }
donatien 8:04b6a042595f 246
donatien 8:04b6a042595f 247 /**
donatien 8:04b6a042595f 248 * Aborts a blocking sio_read() call.
donatien 8:04b6a042595f 249 *
donatien 8:04b6a042595f 250 * @param fd serial device handle
donatien 8:04b6a042595f 251 */
donatien 8:04b6a042595f 252 void sio_read_abort(sio_fd_t fd)
donatien 8:04b6a042595f 253 {
donatien 8:04b6a042595f 254 DBG("sio_read_abort");
donatien 8:04b6a042595f 255 PPPIPInterface* pIf = (PPPIPInterface*)fd;
donatien 8:04b6a042595f 256 if(!pIf->m_streamAvail) //If stream is not available (it is a shared resource) don't go further
donatien 8:04b6a042595f 257 {
donatien 8:04b6a042595f 258 return;
donatien 8:04b6a042595f 259 }
donatien 8:04b6a042595f 260 pIf->m_pStream->abortRead();
donatien 8:04b6a042595f 261 DBG("ret");
donatien 8:04b6a042595f 262 }
donatien 8:04b6a042595f 263
donatien 8:04b6a042595f 264 }
donatien 8:04b6a042595f 265