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

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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