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