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 /*
tax 0:66300c77c6e9 25
tax 0:66300c77c6e9 26 This is a wrapper around Serial if for lwIP (acts as a serial driver)
tax 0:66300c77c6e9 27
tax 0:66300c77c6e9 28 See sio.h for functions to be implemented
tax 0:66300c77c6e9 29
tax 0:66300c77c6e9 30 sio_fd_t is a void* defined type, we use it as a SerialBuf ptr
tax 0:66300c77c6e9 31
tax 0:66300c77c6e9 32
tax 0:66300c77c6e9 33
tax 0:66300c77c6e9 34 */
tax 0:66300c77c6e9 35
tax 0:66300c77c6e9 36
tax 0:66300c77c6e9 37 #include "netCfg.h"
tax 0:66300c77c6e9 38 #if NET_PPP
tax 0:66300c77c6e9 39
tax 0:66300c77c6e9 40 //#define MAX_SERIAL_PORTS 8
tax 0:66300c77c6e9 41
tax 0:66300c77c6e9 42 #include "lwip/sio.h"
tax 0:66300c77c6e9 43 #include "mbed.h"
tax 0:66300c77c6e9 44 //#include "sioMgr.h"
tax 0:66300c77c6e9 45 #include "drv/serial/buf/SerialBuf.h"
tax 0:66300c77c6e9 46
tax 0:66300c77c6e9 47 //#define __DEBUG
tax 0:66300c77c6e9 48 #include "dbg/dbg.h"
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 //extern "C" {
tax 0:66300c77c6e9 51
tax 0:66300c77c6e9 52 /**
tax 0:66300c77c6e9 53 * Opens a serial device for communication.
tax 0:66300c77c6e9 54 *
tax 0:66300c77c6e9 55 * @param devnum device number
tax 0:66300c77c6e9 56 * @return handle to serial device if successful, NULL otherwise
tax 0:66300c77c6e9 57 */
tax 0:66300c77c6e9 58 sio_fd_t sio_open(u8_t devnum)
tax 0:66300c77c6e9 59 {
tax 0:66300c77c6e9 60 #if 0
tax 0:66300c77c6e9 61 SerialBuf* pIf = SioMgr::getIf(devnum);
tax 0:66300c77c6e9 62 if(pIf == NULL)
tax 0:66300c77c6e9 63 return NULL;
tax 0:66300c77c6e9 64
tax 0:66300c77c6e9 65 //Got a SerialBuf* object
tax 0:66300c77c6e9 66 //WARN: It HAS to be initialised (instanciated + attached to a Serial obj)
tax 0:66300c77c6e9 67
tax 0:66300c77c6e9 68 return (sio_fd_t) pIf;
tax 0:66300c77c6e9 69 #endif
tax 0:66300c77c6e9 70 return NULL;
tax 0:66300c77c6e9 71 }
tax 0:66300c77c6e9 72
tax 0:66300c77c6e9 73 /**
tax 0:66300c77c6e9 74 * Sends a single character to the serial device.
tax 0:66300c77c6e9 75 *
tax 0:66300c77c6e9 76 * @param c character to send
tax 0:66300c77c6e9 77 * @param fd serial device handle
tax 0:66300c77c6e9 78 *
tax 0:66300c77c6e9 79 * @note This function will block until the character can be sent.
tax 0:66300c77c6e9 80 */
tax 0:66300c77c6e9 81 void sio_send(u8_t c, sio_fd_t fd)
tax 0:66300c77c6e9 82 {
tax 0:66300c77c6e9 83 SerialBuf* pIf = (SerialBuf*) fd;
tax 0:66300c77c6e9 84 //while(!pIf->writeable());
tax 0:66300c77c6e9 85 pIf->putc( (char) c );
tax 0:66300c77c6e9 86 }
tax 0:66300c77c6e9 87
tax 0:66300c77c6e9 88 /**
tax 0:66300c77c6e9 89 * Receives a single character from the serial device.
tax 0:66300c77c6e9 90 *
tax 0:66300c77c6e9 91 * @param fd serial device handle
tax 0:66300c77c6e9 92 *
tax 0:66300c77c6e9 93 * @note This function will block until a character is received.
tax 0:66300c77c6e9 94 */
tax 0:66300c77c6e9 95 u8_t sio_recv(sio_fd_t fd)
tax 0:66300c77c6e9 96 {
tax 0:66300c77c6e9 97 SerialBuf* pIf = (SerialBuf*) fd;
tax 0:66300c77c6e9 98 pIf->setReadMode(false);
tax 0:66300c77c6e9 99 while(!pIf->readable());
tax 0:66300c77c6e9 100 return (u8_t) pIf->getc();
tax 0:66300c77c6e9 101 }
tax 0:66300c77c6e9 102
tax 0:66300c77c6e9 103 /**
tax 0:66300c77c6e9 104 * Reads from the serial device.
tax 0:66300c77c6e9 105 *
tax 0:66300c77c6e9 106 * @param fd serial device handle
tax 0:66300c77c6e9 107 * @param data pointer to data buffer for receiving
tax 0:66300c77c6e9 108 * @param len maximum length (in bytes) of data to receive
tax 0:66300c77c6e9 109 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
tax 0:66300c77c6e9 110 *
tax 0:66300c77c6e9 111 * @note This function will block until data can be received. The blocking
tax 0:66300c77c6e9 112 * can be cancelled by calling sio_read_abort().
tax 0:66300c77c6e9 113 */
tax 0:66300c77c6e9 114 static volatile bool m_abort = false;
tax 0:66300c77c6e9 115 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
tax 0:66300c77c6e9 116 {
tax 0:66300c77c6e9 117 u32_t recvd = 0; //bytes received
tax 0:66300c77c6e9 118 SerialBuf* pIf = (SerialBuf*) fd;
tax 0:66300c77c6e9 119 pIf->setReadMode(false);
tax 0:66300c77c6e9 120 while(!m_abort && len)
tax 0:66300c77c6e9 121 {
tax 0:66300c77c6e9 122 while(!pIf->readable());
tax 0:66300c77c6e9 123 *data = (u8_t) pIf->getc();
tax 0:66300c77c6e9 124 data++;
tax 0:66300c77c6e9 125 len--;
tax 0:66300c77c6e9 126 recvd++;
tax 0:66300c77c6e9 127 }
tax 0:66300c77c6e9 128 m_abort = false;
tax 0:66300c77c6e9 129 return recvd;
tax 0:66300c77c6e9 130 }
tax 0:66300c77c6e9 131
tax 0:66300c77c6e9 132 /**
tax 0:66300c77c6e9 133 * Tries to read from the serial device. Same as sio_read but returns
tax 0:66300c77c6e9 134 * immediately if no data is available and never blocks.
tax 0:66300c77c6e9 135 *
tax 0:66300c77c6e9 136 * @param fd serial device handle
tax 0:66300c77c6e9 137 * @param data pointer to data buffer for receiving
tax 0:66300c77c6e9 138 * @param len maximum length (in bytes) of data to receive
tax 0:66300c77c6e9 139 * @return number of bytes actually received
tax 0:66300c77c6e9 140 */
tax 0:66300c77c6e9 141 u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
tax 0:66300c77c6e9 142 {
tax 0:66300c77c6e9 143 u32_t recvd = 0; //bytes received
tax 0:66300c77c6e9 144 SerialBuf* pIf = (SerialBuf*) fd;
tax 0:66300c77c6e9 145 pIf->setReadMode(false);
tax 0:66300c77c6e9 146 while(len)
tax 0:66300c77c6e9 147 {
tax 0:66300c77c6e9 148 /* if(!pIf->readable())
tax 0:66300c77c6e9 149 {
tax 0:66300c77c6e9 150 wait_ms(4);
tax 0:66300c77c6e9 151 }*/
tax 0:66300c77c6e9 152 if(!pIf->readable())
tax 0:66300c77c6e9 153 {
tax 0:66300c77c6e9 154 return recvd;
tax 0:66300c77c6e9 155 }
tax 0:66300c77c6e9 156 *data = (u8_t) pIf->getc();
tax 0:66300c77c6e9 157 data++;
tax 0:66300c77c6e9 158 len--;
tax 0:66300c77c6e9 159 recvd++;
tax 0:66300c77c6e9 160 }
tax 0:66300c77c6e9 161 return recvd;
tax 0:66300c77c6e9 162 }
tax 0:66300c77c6e9 163
tax 0:66300c77c6e9 164 /**
tax 0:66300c77c6e9 165 * Writes to the serial device.
tax 0:66300c77c6e9 166 *
tax 0:66300c77c6e9 167 * @param fd serial device handle
tax 0:66300c77c6e9 168 * @param data pointer to data to send
tax 0:66300c77c6e9 169 * @param len length (in bytes) of data to send
tax 0:66300c77c6e9 170 * @return number of bytes actually sent
tax 0:66300c77c6e9 171 *
tax 0:66300c77c6e9 172 * @note This function will block until all data can be sent.
tax 0:66300c77c6e9 173 */
tax 0:66300c77c6e9 174 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
tax 0:66300c77c6e9 175 {
tax 0:66300c77c6e9 176 u32_t sent = 0; //bytes sent
tax 0:66300c77c6e9 177 SerialBuf* pIf = (SerialBuf*) fd;
tax 0:66300c77c6e9 178 while(len)
tax 0:66300c77c6e9 179 {
tax 0:66300c77c6e9 180 while(!pIf->writeable());
tax 0:66300c77c6e9 181 pIf->putc(*data);
tax 0:66300c77c6e9 182 data++;
tax 0:66300c77c6e9 183 len--;
tax 0:66300c77c6e9 184 sent++;
tax 0:66300c77c6e9 185 }
tax 0:66300c77c6e9 186 return sent; //Well, this is bound to be len if no interrupt mechanism
tax 0:66300c77c6e9 187 }
tax 0:66300c77c6e9 188
tax 0:66300c77c6e9 189 /**
tax 0:66300c77c6e9 190 * Aborts a blocking sio_read() call.
tax 0:66300c77c6e9 191 *
tax 0:66300c77c6e9 192 * @param fd serial device handle
tax 0:66300c77c6e9 193 */
tax 0:66300c77c6e9 194 void sio_read_abort(sio_fd_t fd)
tax 0:66300c77c6e9 195 {
tax 0:66300c77c6e9 196 m_abort = true;
tax 0:66300c77c6e9 197 }
tax 0:66300c77c6e9 198
tax 0:66300c77c6e9 199 //}
tax 0:66300c77c6e9 200
tax 0:66300c77c6e9 201 #endif
tax 0:66300c77c6e9 202