Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.
Dependencies: ChaNFSSD TFTPServer RMFWeb
NetServices/if/ppp/PPPNetIf.cpp@1:809b59c7a800, 2014-06-16 (annotated)
- Committer:
- ED7418
- Date:
- Mon Jun 16 07:40:08 2014 +0000
- Revision:
- 1:809b59c7a800
- Parent:
- 0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ED7418 | 0:51f1ef89ec7b | 1 | |
ED7418 | 0:51f1ef89ec7b | 2 | /* |
ED7418 | 0:51f1ef89ec7b | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
ED7418 | 0:51f1ef89ec7b | 4 | |
ED7418 | 0:51f1ef89ec7b | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
ED7418 | 0:51f1ef89ec7b | 6 | of this software and associated documentation files (the "Software"), to deal |
ED7418 | 0:51f1ef89ec7b | 7 | in the Software without restriction, including without limitation the rights |
ED7418 | 0:51f1ef89ec7b | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ED7418 | 0:51f1ef89ec7b | 9 | copies of the Software, and to permit persons to whom the Software is |
ED7418 | 0:51f1ef89ec7b | 10 | furnished to do so, subject to the following conditions: |
ED7418 | 0:51f1ef89ec7b | 11 | |
ED7418 | 0:51f1ef89ec7b | 12 | The above copyright notice and this permission notice shall be included in |
ED7418 | 0:51f1ef89ec7b | 13 | all copies or substantial portions of the Software. |
ED7418 | 0:51f1ef89ec7b | 14 | |
ED7418 | 0:51f1ef89ec7b | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ED7418 | 0:51f1ef89ec7b | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ED7418 | 0:51f1ef89ec7b | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ED7418 | 0:51f1ef89ec7b | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ED7418 | 0:51f1ef89ec7b | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ED7418 | 0:51f1ef89ec7b | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ED7418 | 0:51f1ef89ec7b | 21 | THE SOFTWARE. |
ED7418 | 0:51f1ef89ec7b | 22 | */ |
ED7418 | 0:51f1ef89ec7b | 23 | |
ED7418 | 0:51f1ef89ec7b | 24 | #include "PPPNetIf.h" |
ED7418 | 0:51f1ef89ec7b | 25 | #include "mbed.h" |
ED7418 | 0:51f1ef89ec7b | 26 | #include "ppp/ppp.h" |
ED7418 | 0:51f1ef89ec7b | 27 | #include "lwip/init.h" |
ED7418 | 0:51f1ef89ec7b | 28 | #include "lwip/sio.h" |
ED7418 | 0:51f1ef89ec7b | 29 | |
ED7418 | 0:51f1ef89ec7b | 30 | #define __DEBUG |
ED7418 | 0:51f1ef89ec7b | 31 | #include "dbg/dbg.h" |
ED7418 | 0:51f1ef89ec7b | 32 | |
ED7418 | 0:51f1ef89ec7b | 33 | #include "netCfg.h" |
ED7418 | 0:51f1ef89ec7b | 34 | #if NET_PPP |
ED7418 | 0:51f1ef89ec7b | 35 | |
ED7418 | 0:51f1ef89ec7b | 36 | #define PPP_TIMEOUT 60000 |
ED7418 | 0:51f1ef89ec7b | 37 | |
ED7418 | 0:51f1ef89ec7b | 38 | #define BUF_SIZE 256 |
ED7418 | 0:51f1ef89ec7b | 39 | |
ED7418 | 0:51f1ef89ec7b | 40 | PPPNetIf::PPPNetIf(GPRSModem* pIf) : LwipNetIf(), m_pIf(pIf),/* m_open(false),*/ m_connected(false), m_status(PPP_DISCONNECTED), m_fd(0) //, m_id(0) |
ED7418 | 0:51f1ef89ec7b | 41 | { |
ED7418 | 0:51f1ef89ec7b | 42 | //FIXME: Check static refcount |
ED7418 | 0:51f1ef89ec7b | 43 | m_buf = new uint8_t[BUF_SIZE]; |
ED7418 | 0:51f1ef89ec7b | 44 | } |
ED7418 | 0:51f1ef89ec7b | 45 | |
ED7418 | 0:51f1ef89ec7b | 46 | PPPNetIf::~PPPNetIf() |
ED7418 | 0:51f1ef89ec7b | 47 | { |
ED7418 | 0:51f1ef89ec7b | 48 | delete[] m_buf; |
ED7418 | 0:51f1ef89ec7b | 49 | } |
ED7418 | 0:51f1ef89ec7b | 50 | |
ED7418 | 0:51f1ef89ec7b | 51 | #if 0 |
ED7418 | 0:51f1ef89ec7b | 52 | PPPErr PPPNetIf::open(Serial* pSerial) |
ED7418 | 0:51f1ef89ec7b | 53 | { |
ED7418 | 0:51f1ef89ec7b | 54 | GPRSErr err = m_pIf->open(pSerial); |
ED7418 | 0:51f1ef89ec7b | 55 | if(err) |
ED7418 | 0:51f1ef89ec7b | 56 | return PPP_MODEM; |
ED7418 | 0:51f1ef89ec7b | 57 | m_open = true; |
ED7418 | 0:51f1ef89ec7b | 58 | #if 0 |
ED7418 | 0:51f1ef89ec7b | 59 | m_id = sioMgr::registerSerialIf(this); |
ED7418 | 0:51f1ef89ec7b | 60 | if(!m_id) |
ED7418 | 0:51f1ef89ec7b | 61 | { |
ED7418 | 0:51f1ef89ec7b | 62 | close(); |
ED7418 | 0:51f1ef89ec7b | 63 | return PPP_CLOSED; |
ED7418 | 0:51f1ef89ec7b | 64 | } |
ED7418 | 0:51f1ef89ec7b | 65 | #endif |
ED7418 | 0:51f1ef89ec7b | 66 | return PPP_OK; |
ED7418 | 0:51f1ef89ec7b | 67 | } |
ED7418 | 0:51f1ef89ec7b | 68 | #endif |
ED7418 | 0:51f1ef89ec7b | 69 | |
ED7418 | 0:51f1ef89ec7b | 70 | |
ED7418 | 0:51f1ef89ec7b | 71 | PPPErr PPPNetIf::GPRSConnect(const char* apn, const char* userId, const char* password) //Connect using GPRS |
ED7418 | 0:51f1ef89ec7b | 72 | { |
ED7418 | 0:51f1ef89ec7b | 73 | LwipNetIf::init(); |
ED7418 | 0:51f1ef89ec7b | 74 | pppInit(); |
ED7418 | 0:51f1ef89ec7b | 75 | //TODO: Tell ATIf that we get ownership of the serial port |
ED7418 | 0:51f1ef89ec7b | 76 | |
ED7418 | 0:51f1ef89ec7b | 77 | GPRSErr gprsErr; |
ED7418 | 0:51f1ef89ec7b | 78 | gprsErr = m_pIf->connect(apn); |
ED7418 | 0:51f1ef89ec7b | 79 | if(gprsErr) |
ED7418 | 0:51f1ef89ec7b | 80 | return PPP_NETWORK; |
ED7418 | 0:51f1ef89ec7b | 81 | |
ED7418 | 0:51f1ef89ec7b | 82 | DBG("PPPNetIf: If Connected.\n"); |
ED7418 | 0:51f1ef89ec7b | 83 | |
ED7418 | 0:51f1ef89ec7b | 84 | if( userId == NULL ) |
ED7418 | 0:51f1ef89ec7b | 85 | pppSetAuth(PPPAUTHTYPE_NONE, NULL, NULL); |
ED7418 | 0:51f1ef89ec7b | 86 | else |
ED7418 | 0:51f1ef89ec7b | 87 | pppSetAuth(PPPAUTHTYPE_PAP, userId, password); //TODO: Allow CHAP as well |
ED7418 | 0:51f1ef89ec7b | 88 | |
ED7418 | 0:51f1ef89ec7b | 89 | DBG("PPPNetIf: Set Auth.\n"); |
ED7418 | 0:51f1ef89ec7b | 90 | |
ED7418 | 0:51f1ef89ec7b | 91 | //wait(1.); |
ED7418 | 0:51f1ef89ec7b | 92 | |
ED7418 | 0:51f1ef89ec7b | 93 | //m_pIf->flushBuffer(); //Flush buffer before passing serial port to PPP |
ED7418 | 0:51f1ef89ec7b | 94 | |
ED7418 | 0:51f1ef89ec7b | 95 | m_status = PPP_CONNECTING; |
ED7418 | 0:51f1ef89ec7b | 96 | DBG("m_pIf = %p\n", m_pIf); |
ED7418 | 0:51f1ef89ec7b | 97 | int res = pppOverSerialOpen((void*)m_pIf, sPppCallback, (void*)this); |
ED7418 | 0:51f1ef89ec7b | 98 | DBG("PPP connected\n"); |
ED7418 | 0:51f1ef89ec7b | 99 | if(res<0) |
ED7418 | 0:51f1ef89ec7b | 100 | { |
ED7418 | 0:51f1ef89ec7b | 101 | disconnect(); |
ED7418 | 0:51f1ef89ec7b | 102 | return PPP_PROTOCOL; |
ED7418 | 0:51f1ef89ec7b | 103 | } |
ED7418 | 0:51f1ef89ec7b | 104 | |
ED7418 | 0:51f1ef89ec7b | 105 | DBG("PPPNetIf: PPP Started with res = %d.\n", res); |
ED7418 | 0:51f1ef89ec7b | 106 | |
ED7418 | 0:51f1ef89ec7b | 107 | m_fd = res; |
ED7418 | 0:51f1ef89ec7b | 108 | m_connected = true; |
ED7418 | 0:51f1ef89ec7b | 109 | Timer t; |
ED7418 | 0:51f1ef89ec7b | 110 | t.start(); |
ED7418 | 0:51f1ef89ec7b | 111 | while( m_status == PPP_CONNECTING ) //Wait for callback |
ED7418 | 0:51f1ef89ec7b | 112 | { |
ED7418 | 0:51f1ef89ec7b | 113 | poll(); |
ED7418 | 0:51f1ef89ec7b | 114 | if(t.read_ms()>PPP_TIMEOUT) |
ED7418 | 0:51f1ef89ec7b | 115 | { |
ED7418 | 0:51f1ef89ec7b | 116 | DBG("PPPNetIf: Timeout.\n"); |
ED7418 | 0:51f1ef89ec7b | 117 | disconnect(); |
ED7418 | 0:51f1ef89ec7b | 118 | return PPP_PROTOCOL; |
ED7418 | 0:51f1ef89ec7b | 119 | } |
ED7418 | 0:51f1ef89ec7b | 120 | } |
ED7418 | 0:51f1ef89ec7b | 121 | |
ED7418 | 0:51f1ef89ec7b | 122 | DBG("PPPNetIf: Callback returned.\n"); |
ED7418 | 0:51f1ef89ec7b | 123 | |
ED7418 | 0:51f1ef89ec7b | 124 | if( m_status == PPP_DISCONNECTED ) |
ED7418 | 0:51f1ef89ec7b | 125 | { |
ED7418 | 0:51f1ef89ec7b | 126 | disconnect(); |
ED7418 | 0:51f1ef89ec7b | 127 | return PPP_PROTOCOL; |
ED7418 | 0:51f1ef89ec7b | 128 | } |
ED7418 | 0:51f1ef89ec7b | 129 | |
ED7418 | 0:51f1ef89ec7b | 130 | return PPP_OK; |
ED7418 | 0:51f1ef89ec7b | 131 | |
ED7418 | 0:51f1ef89ec7b | 132 | } |
ED7418 | 0:51f1ef89ec7b | 133 | |
ED7418 | 0:51f1ef89ec7b | 134 | PPPErr PPPNetIf::ATConnect(const char* number) //Connect using a "classic" voice modem or GSM |
ED7418 | 0:51f1ef89ec7b | 135 | { |
ED7418 | 0:51f1ef89ec7b | 136 | //TODO: IMPL |
ED7418 | 0:51f1ef89ec7b | 137 | return PPP_MODEM; |
ED7418 | 0:51f1ef89ec7b | 138 | } |
ED7418 | 0:51f1ef89ec7b | 139 | |
ED7418 | 0:51f1ef89ec7b | 140 | PPPErr PPPNetIf::disconnect() |
ED7418 | 0:51f1ef89ec7b | 141 | { |
ED7418 | 0:51f1ef89ec7b | 142 | if(m_fd) |
ED7418 | 0:51f1ef89ec7b | 143 | pppClose(m_fd); //0 if ok, else should gen a WARN |
ED7418 | 0:51f1ef89ec7b | 144 | m_connected = false; |
ED7418 | 0:51f1ef89ec7b | 145 | |
ED7418 | 0:51f1ef89ec7b | 146 | m_pIf->flushBuffer(); |
ED7418 | 0:51f1ef89ec7b | 147 | m_pIf->printf("+++\r\n"); |
ED7418 | 0:51f1ef89ec7b | 148 | wait(.5); |
ED7418 | 0:51f1ef89ec7b | 149 | m_pIf->flushBuffer(); |
ED7418 | 0:51f1ef89ec7b | 150 | |
ED7418 | 0:51f1ef89ec7b | 151 | GPRSErr gprsErr; |
ED7418 | 0:51f1ef89ec7b | 152 | gprsErr = m_pIf->disconnect(); |
ED7418 | 0:51f1ef89ec7b | 153 | if(gprsErr) |
ED7418 | 0:51f1ef89ec7b | 154 | return PPP_NETWORK; |
ED7418 | 0:51f1ef89ec7b | 155 | |
ED7418 | 0:51f1ef89ec7b | 156 | return PPP_OK; |
ED7418 | 0:51f1ef89ec7b | 157 | } |
ED7418 | 0:51f1ef89ec7b | 158 | |
ED7418 | 0:51f1ef89ec7b | 159 | #if 0 |
ED7418 | 0:51f1ef89ec7b | 160 | PPPErr PPPNetIf::close() |
ED7418 | 0:51f1ef89ec7b | 161 | { |
ED7418 | 0:51f1ef89ec7b | 162 | GPRSErr err = m_pIf->close(); |
ED7418 | 0:51f1ef89ec7b | 163 | if(err) |
ED7418 | 0:51f1ef89ec7b | 164 | return PPP_MODEM; |
ED7418 | 0:51f1ef89ec7b | 165 | m_open = false; |
ED7418 | 0:51f1ef89ec7b | 166 | return PPP_OK; |
ED7418 | 0:51f1ef89ec7b | 167 | } |
ED7418 | 0:51f1ef89ec7b | 168 | #endif |
ED7418 | 0:51f1ef89ec7b | 169 | |
ED7418 | 0:51f1ef89ec7b | 170 | |
ED7418 | 0:51f1ef89ec7b | 171 | #if 0 |
ED7418 | 0:51f1ef89ec7b | 172 | //We have to use : |
ED7418 | 0:51f1ef89ec7b | 173 | |
ED7418 | 0:51f1ef89ec7b | 174 | /** Pass received raw characters to PPPoS to be decoded. This function is |
ED7418 | 0:51f1ef89ec7b | 175 | * thread-safe and can be called from a dedicated RX-thread or from a main-loop. |
ED7418 | 0:51f1ef89ec7b | 176 | * |
ED7418 | 0:51f1ef89ec7b | 177 | * @param pd PPP descriptor index, returned by pppOpen() |
ED7418 | 0:51f1ef89ec7b | 178 | * @param data received data |
ED7418 | 0:51f1ef89ec7b | 179 | * @param len length of received data |
ED7418 | 0:51f1ef89ec7b | 180 | */ |
ED7418 | 0:51f1ef89ec7b | 181 | void |
ED7418 | 0:51f1ef89ec7b | 182 | pppos_input(int pd, u_char* data, int len) |
ED7418 | 0:51f1ef89ec7b | 183 | { |
ED7418 | 0:51f1ef89ec7b | 184 | pppInProc(&pppControl[pd].rx, data, len); |
ED7418 | 0:51f1ef89ec7b | 185 | } |
ED7418 | 0:51f1ef89ec7b | 186 | #endif |
ED7418 | 0:51f1ef89ec7b | 187 | |
ED7418 | 0:51f1ef89ec7b | 188 | void PPPNetIf::poll() |
ED7418 | 0:51f1ef89ec7b | 189 | { |
ED7418 | 0:51f1ef89ec7b | 190 | if(!m_connected) |
ED7418 | 0:51f1ef89ec7b | 191 | return; |
ED7418 | 0:51f1ef89ec7b | 192 | LwipNetIf::poll(); |
ED7418 | 0:51f1ef89ec7b | 193 | //static u8_t buf[128]; |
ED7418 | 0:51f1ef89ec7b | 194 | int len; |
ED7418 | 0:51f1ef89ec7b | 195 | do |
ED7418 | 0:51f1ef89ec7b | 196 | { |
ED7418 | 0:51f1ef89ec7b | 197 | len = sio_tryread((sio_fd_t) m_pIf, m_buf, BUF_SIZE); |
ED7418 | 0:51f1ef89ec7b | 198 | if(len > 0) |
ED7418 | 0:51f1ef89ec7b | 199 | pppos_input(m_fd, m_buf, len); |
ED7418 | 0:51f1ef89ec7b | 200 | } while(len>0); |
ED7418 | 0:51f1ef89ec7b | 201 | } |
ED7418 | 0:51f1ef89ec7b | 202 | |
ED7418 | 0:51f1ef89ec7b | 203 | //Link Callback |
ED7418 | 0:51f1ef89ec7b | 204 | void PPPNetIf::pppCallback(int errCode, void *arg) |
ED7418 | 0:51f1ef89ec7b | 205 | { |
ED7418 | 0:51f1ef89ec7b | 206 | switch ( errCode ) |
ED7418 | 0:51f1ef89ec7b | 207 | { |
ED7418 | 0:51f1ef89ec7b | 208 | //No error |
ED7418 | 0:51f1ef89ec7b | 209 | case PPPERR_NONE: |
ED7418 | 0:51f1ef89ec7b | 210 | { |
ED7418 | 0:51f1ef89ec7b | 211 | struct ppp_addrs* addrs = (struct ppp_addrs*) arg; |
ED7418 | 0:51f1ef89ec7b | 212 | m_ip = IpAddr(&(addrs->our_ipaddr)); //Set IP |
ED7418 | 0:51f1ef89ec7b | 213 | } |
ED7418 | 0:51f1ef89ec7b | 214 | m_status = PPP_CONNECTED; |
ED7418 | 0:51f1ef89ec7b | 215 | break; |
ED7418 | 0:51f1ef89ec7b | 216 | default: |
ED7418 | 0:51f1ef89ec7b | 217 | //Disconnected |
ED7418 | 0:51f1ef89ec7b | 218 | DBG("PPPNetIf: Callback errCode = %d.\n", errCode); |
ED7418 | 0:51f1ef89ec7b | 219 | m_status = PPP_DISCONNECTED; |
ED7418 | 0:51f1ef89ec7b | 220 | break; |
ED7418 | 0:51f1ef89ec7b | 221 | } |
ED7418 | 0:51f1ef89ec7b | 222 | } |
ED7418 | 0:51f1ef89ec7b | 223 | |
ED7418 | 0:51f1ef89ec7b | 224 | #endif |