I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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