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