Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:850eacf3e945 1
RodColeman 0:850eacf3e945 2 /*
RodColeman 0:850eacf3e945 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
RodColeman 0:850eacf3e945 4
RodColeman 0:850eacf3e945 5 Permission is hereby granted, free of charge, to any person obtaining a copy
RodColeman 0:850eacf3e945 6 of this software and associated documentation files (the "Software"), to deal
RodColeman 0:850eacf3e945 7 in the Software without restriction, including without limitation the rights
RodColeman 0:850eacf3e945 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RodColeman 0:850eacf3e945 9 copies of the Software, and to permit persons to whom the Software is
RodColeman 0:850eacf3e945 10 furnished to do so, subject to the following conditions:
RodColeman 0:850eacf3e945 11
RodColeman 0:850eacf3e945 12 The above copyright notice and this permission notice shall be included in
RodColeman 0:850eacf3e945 13 all copies or substantial portions of the Software.
RodColeman 0:850eacf3e945 14
RodColeman 0:850eacf3e945 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RodColeman 0:850eacf3e945 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RodColeman 0:850eacf3e945 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RodColeman 0:850eacf3e945 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RodColeman 0:850eacf3e945 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RodColeman 0:850eacf3e945 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RodColeman 0:850eacf3e945 21 THE SOFTWARE.
RodColeman 0:850eacf3e945 22 */
RodColeman 0:850eacf3e945 23
RodColeman 0:850eacf3e945 24 /*
RodColeman 0:850eacf3e945 25
RodColeman 0:850eacf3e945 26 This is a wrapper around Serial if for lwIP (acts as a serial driver)
RodColeman 0:850eacf3e945 27
RodColeman 0:850eacf3e945 28 See sio.h for functions to be implemented
RodColeman 0:850eacf3e945 29
RodColeman 0:850eacf3e945 30 sio_fd_t is a void* defined type, we use it as a SerialBuf ptr
RodColeman 0:850eacf3e945 31
RodColeman 0:850eacf3e945 32
RodColeman 0:850eacf3e945 33
RodColeman 0:850eacf3e945 34 */
RodColeman 0:850eacf3e945 35
RodColeman 0:850eacf3e945 36
RodColeman 0:850eacf3e945 37 #include "netCfg.h"
RodColeman 0:850eacf3e945 38 #if NET_PPP
RodColeman 0:850eacf3e945 39
RodColeman 0:850eacf3e945 40 //#define MAX_SERIAL_PORTS 8
RodColeman 0:850eacf3e945 41
RodColeman 0:850eacf3e945 42 #include "lwip/sio.h"
RodColeman 0:850eacf3e945 43 #include "mbed.h"
RodColeman 0:850eacf3e945 44 //#include "sioMgr.h"
RodColeman 0:850eacf3e945 45 #include "drv/serial/buf/SerialBuf.h"
RodColeman 0:850eacf3e945 46
RodColeman 0:850eacf3e945 47 //#define __DEBUG
RodColeman 0:850eacf3e945 48 #include "dbg/dbg.h"
RodColeman 0:850eacf3e945 49
RodColeman 0:850eacf3e945 50 //extern "C" {
RodColeman 0:850eacf3e945 51
RodColeman 0:850eacf3e945 52 /**
RodColeman 0:850eacf3e945 53 * Opens a serial device for communication.
RodColeman 0:850eacf3e945 54 *
RodColeman 0:850eacf3e945 55 * @param devnum device number
RodColeman 0:850eacf3e945 56 * @return handle to serial device if successful, NULL otherwise
RodColeman 0:850eacf3e945 57 */
RodColeman 0:850eacf3e945 58 sio_fd_t sio_open(u8_t devnum)
RodColeman 0:850eacf3e945 59 {
RodColeman 0:850eacf3e945 60 #if 0
RodColeman 0:850eacf3e945 61 SerialBuf* pIf = SioMgr::getIf(devnum);
RodColeman 0:850eacf3e945 62 if(pIf == NULL)
RodColeman 0:850eacf3e945 63 return NULL;
RodColeman 0:850eacf3e945 64
RodColeman 0:850eacf3e945 65 //Got a SerialBuf* object
RodColeman 0:850eacf3e945 66 //WARN: It HAS to be initialised (instanciated + attached to a Serial obj)
RodColeman 0:850eacf3e945 67
RodColeman 0:850eacf3e945 68 return (sio_fd_t) pIf;
RodColeman 0:850eacf3e945 69 #endif
RodColeman 0:850eacf3e945 70 return NULL;
RodColeman 0:850eacf3e945 71 }
RodColeman 0:850eacf3e945 72
RodColeman 0:850eacf3e945 73 /**
RodColeman 0:850eacf3e945 74 * Sends a single character to the serial device.
RodColeman 0:850eacf3e945 75 *
RodColeman 0:850eacf3e945 76 * @param c character to send
RodColeman 0:850eacf3e945 77 * @param fd serial device handle
RodColeman 0:850eacf3e945 78 *
RodColeman 0:850eacf3e945 79 * @note This function will block until the character can be sent.
RodColeman 0:850eacf3e945 80 */
RodColeman 0:850eacf3e945 81 void sio_send(u8_t c, sio_fd_t fd)
RodColeman 0:850eacf3e945 82 {
RodColeman 0:850eacf3e945 83 SerialBuf* pIf = (SerialBuf*) fd;
RodColeman 0:850eacf3e945 84 //while(!pIf->writeable());
RodColeman 0:850eacf3e945 85 pIf->putc( (char) c );
RodColeman 0:850eacf3e945 86 }
RodColeman 0:850eacf3e945 87
RodColeman 0:850eacf3e945 88 /**
RodColeman 0:850eacf3e945 89 * Receives a single character from the serial device.
RodColeman 0:850eacf3e945 90 *
RodColeman 0:850eacf3e945 91 * @param fd serial device handle
RodColeman 0:850eacf3e945 92 *
RodColeman 0:850eacf3e945 93 * @note This function will block until a character is received.
RodColeman 0:850eacf3e945 94 */
RodColeman 0:850eacf3e945 95 u8_t sio_recv(sio_fd_t fd)
RodColeman 0:850eacf3e945 96 {
RodColeman 0:850eacf3e945 97 SerialBuf* pIf = (SerialBuf*) fd;
RodColeman 0:850eacf3e945 98 pIf->setReadMode(false);
RodColeman 0:850eacf3e945 99 while(!pIf->readable());
RodColeman 0:850eacf3e945 100 return (u8_t) pIf->getc();
RodColeman 0:850eacf3e945 101 }
RodColeman 0:850eacf3e945 102
RodColeman 0:850eacf3e945 103 /**
RodColeman 0:850eacf3e945 104 * Reads from the serial device.
RodColeman 0:850eacf3e945 105 *
RodColeman 0:850eacf3e945 106 * @param fd serial device handle
RodColeman 0:850eacf3e945 107 * @param data pointer to data buffer for receiving
RodColeman 0:850eacf3e945 108 * @param len maximum length (in bytes) of data to receive
RodColeman 0:850eacf3e945 109 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
RodColeman 0:850eacf3e945 110 *
RodColeman 0:850eacf3e945 111 * @note This function will block until data can be received. The blocking
RodColeman 0:850eacf3e945 112 * can be cancelled by calling sio_read_abort().
RodColeman 0:850eacf3e945 113 */
RodColeman 0:850eacf3e945 114 static volatile bool m_abort = false;
RodColeman 0:850eacf3e945 115 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
RodColeman 0:850eacf3e945 116 {
RodColeman 0:850eacf3e945 117 u32_t recvd = 0; //bytes received
RodColeman 0:850eacf3e945 118 SerialBuf* pIf = (SerialBuf*) fd;
RodColeman 0:850eacf3e945 119 pIf->setReadMode(false);
RodColeman 0:850eacf3e945 120 while(!m_abort && len)
RodColeman 0:850eacf3e945 121 {
RodColeman 0:850eacf3e945 122 while(!pIf->readable());
RodColeman 0:850eacf3e945 123 *data = (u8_t) pIf->getc();
RodColeman 0:850eacf3e945 124 data++;
RodColeman 0:850eacf3e945 125 len--;
RodColeman 0:850eacf3e945 126 recvd++;
RodColeman 0:850eacf3e945 127 }
RodColeman 0:850eacf3e945 128 m_abort = false;
RodColeman 0:850eacf3e945 129 return recvd;
RodColeman 0:850eacf3e945 130 }
RodColeman 0:850eacf3e945 131
RodColeman 0:850eacf3e945 132 /**
RodColeman 0:850eacf3e945 133 * Tries to read from the serial device. Same as sio_read but returns
RodColeman 0:850eacf3e945 134 * immediately if no data is available and never blocks.
RodColeman 0:850eacf3e945 135 *
RodColeman 0:850eacf3e945 136 * @param fd serial device handle
RodColeman 0:850eacf3e945 137 * @param data pointer to data buffer for receiving
RodColeman 0:850eacf3e945 138 * @param len maximum length (in bytes) of data to receive
RodColeman 0:850eacf3e945 139 * @return number of bytes actually received
RodColeman 0:850eacf3e945 140 */
RodColeman 0:850eacf3e945 141 u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
RodColeman 0:850eacf3e945 142 {
RodColeman 0:850eacf3e945 143 u32_t recvd = 0; //bytes received
RodColeman 0:850eacf3e945 144 SerialBuf* pIf = (SerialBuf*) fd;
RodColeman 0:850eacf3e945 145 pIf->setReadMode(false);
RodColeman 0:850eacf3e945 146 while(len)
RodColeman 0:850eacf3e945 147 {
RodColeman 0:850eacf3e945 148 /* if(!pIf->readable())
RodColeman 0:850eacf3e945 149 {
RodColeman 0:850eacf3e945 150 wait_ms(4);
RodColeman 0:850eacf3e945 151 }*/
RodColeman 0:850eacf3e945 152 if(!pIf->readable())
RodColeman 0:850eacf3e945 153 {
RodColeman 0:850eacf3e945 154 return recvd;
RodColeman 0:850eacf3e945 155 }
RodColeman 0:850eacf3e945 156 *data = (u8_t) pIf->getc();
RodColeman 0:850eacf3e945 157 data++;
RodColeman 0:850eacf3e945 158 len--;
RodColeman 0:850eacf3e945 159 recvd++;
RodColeman 0:850eacf3e945 160 }
RodColeman 0:850eacf3e945 161 return recvd;
RodColeman 0:850eacf3e945 162 }
RodColeman 0:850eacf3e945 163
RodColeman 0:850eacf3e945 164 /**
RodColeman 0:850eacf3e945 165 * Writes to the serial device.
RodColeman 0:850eacf3e945 166 *
RodColeman 0:850eacf3e945 167 * @param fd serial device handle
RodColeman 0:850eacf3e945 168 * @param data pointer to data to send
RodColeman 0:850eacf3e945 169 * @param len length (in bytes) of data to send
RodColeman 0:850eacf3e945 170 * @return number of bytes actually sent
RodColeman 0:850eacf3e945 171 *
RodColeman 0:850eacf3e945 172 * @note This function will block until all data can be sent.
RodColeman 0:850eacf3e945 173 */
RodColeman 0:850eacf3e945 174 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
RodColeman 0:850eacf3e945 175 {
RodColeman 0:850eacf3e945 176 u32_t sent = 0; //bytes sent
RodColeman 0:850eacf3e945 177 SerialBuf* pIf = (SerialBuf*) fd;
RodColeman 0:850eacf3e945 178 while(len)
RodColeman 0:850eacf3e945 179 {
RodColeman 0:850eacf3e945 180 while(!pIf->writeable());
RodColeman 0:850eacf3e945 181 pIf->putc(*data);
RodColeman 0:850eacf3e945 182 data++;
RodColeman 0:850eacf3e945 183 len--;
RodColeman 0:850eacf3e945 184 sent++;
RodColeman 0:850eacf3e945 185 }
RodColeman 0:850eacf3e945 186 return sent; //Well, this is bound to be len if no interrupt mechanism
RodColeman 0:850eacf3e945 187 }
RodColeman 0:850eacf3e945 188
RodColeman 0:850eacf3e945 189 /**
RodColeman 0:850eacf3e945 190 * Aborts a blocking sio_read() call.
RodColeman 0:850eacf3e945 191 *
RodColeman 0:850eacf3e945 192 * @param fd serial device handle
RodColeman 0:850eacf3e945 193 */
RodColeman 0:850eacf3e945 194 void sio_read_abort(sio_fd_t fd)
RodColeman 0:850eacf3e945 195 {
RodColeman 0:850eacf3e945 196 m_abort = true;
RodColeman 0:850eacf3e945 197 }
RodColeman 0:850eacf3e945 198
RodColeman 0:850eacf3e945 199 //}
RodColeman 0:850eacf3e945 200
RodColeman 0:850eacf3e945 201 #endif
RodColeman 0:850eacf3e945 202