Darran Shepherd
/
AutoIpNetStack
Net stack with AutoIP enabled
Embed:
(wiki syntax)
Show/hide line numbers
sio.cpp
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 /* 00025 00026 This is a wrapper around Serial if for lwIP (acts as a serial driver) 00027 00028 See sio.h for functions to be implemented 00029 00030 sio_fd_t is a void* defined type, we use it as a SerialBuf ptr 00031 00032 00033 00034 */ 00035 00036 00037 #include "netCfg.h" 00038 #if NET_PPP 00039 00040 00041 #define MAX_SERIAL_PORTS 8 00042 00043 #include "lwip/sio.h" 00044 #include "mbed.h" 00045 #include "sioMgr.h" 00046 00047 //extern "C" { 00048 00049 /** 00050 * Opens a serial device for communication. 00051 * 00052 * @param devnum device number 00053 * @return handle to serial device if successful, NULL otherwise 00054 */ 00055 sio_fd_t sio_open(u8_t devnum) 00056 { 00057 SerialBuf* pIf = SioMgr::getIf(devnum); 00058 if(pIf == NULL) 00059 return NULL; 00060 00061 //Got a SerialBuf* object 00062 //WARN: It HAS to be initialised (instanciated + attached to a Serial obj) 00063 00064 return (sio_fd_t*) pIf; 00065 } 00066 00067 /** 00068 * Sends a single character to the serial device. 00069 * 00070 * @param c character to send 00071 * @param fd serial device handle 00072 * 00073 * @note This function will block until the character can be sent. 00074 */ 00075 void sio_send(u8_t c, sio_fd_t fd) 00076 { 00077 SerialBuf* pIf = (SerialBuf*) fd; 00078 //while(!pIf->writeable()); 00079 pIf->putc( (char) c ); 00080 } 00081 00082 /** 00083 * Receives a single character from the serial device. 00084 * 00085 * @param fd serial device handle 00086 * 00087 * @note This function will block until a character is received. 00088 */ 00089 u8_t sio_recv(sio_fd_t fd) 00090 { 00091 SerialBuf* pIf = (SerialBuf*) fd; 00092 pIf->setReadMode(false); 00093 while(!pIf->readable()); 00094 return (u8_t) pIf->getc(); 00095 } 00096 00097 /** 00098 * Reads from the serial device. 00099 * 00100 * @param fd serial device handle 00101 * @param data pointer to data buffer for receiving 00102 * @param len maximum length (in bytes) of data to receive 00103 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort 00104 * 00105 * @note This function will block until data can be received. The blocking 00106 * can be cancelled by calling sio_read_abort(). 00107 */ 00108 static volatile bool m_abort = false; 00109 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len) 00110 { 00111 u32_t recvd = 0; //bytes received 00112 SerialBuf* pIf = (SerialBuf*) fd; 00113 pIf->setReadMode(false); 00114 while(!m_abort && len) 00115 { 00116 while(!pIf->readable()); 00117 *data = (u8_t) pIf->getc(); 00118 data++; 00119 len--; 00120 recvd++; 00121 } 00122 m_abort = false; 00123 return recvd; 00124 } 00125 00126 /** 00127 * Tries to read from the serial device. Same as sio_read but returns 00128 * immediately if no data is available and never blocks. 00129 * 00130 * @param fd serial device handle 00131 * @param data pointer to data buffer for receiving 00132 * @param len maximum length (in bytes) of data to receive 00133 * @return number of bytes actually received 00134 */ 00135 u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len) 00136 { 00137 u32_t recvd = 0; //bytes received 00138 SerialBuf* pIf = (SerialBuf*) fd; 00139 pIf->setReadMode(false); 00140 while(len) 00141 { 00142 /* if(!pIf->readable()) 00143 { 00144 wait_ms(4); 00145 }*/ 00146 if(!pIf->readable()) 00147 { 00148 return recvd; 00149 } 00150 *data = (u8_t) pIf->getc(); 00151 data++; 00152 len--; 00153 recvd++; 00154 } 00155 return recvd; 00156 } 00157 00158 /** 00159 * Writes to the serial device. 00160 * 00161 * @param fd serial device handle 00162 * @param data pointer to data to send 00163 * @param len length (in bytes) of data to send 00164 * @return number of bytes actually sent 00165 * 00166 * @note This function will block until all data can be sent. 00167 */ 00168 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len) 00169 { 00170 u32_t sent = 0; //bytes sent 00171 SerialBuf* pIf = (SerialBuf*) fd; 00172 while(len) 00173 { 00174 //while(!pIf->writeable()); 00175 pIf->putc(*data); 00176 data++; 00177 len--; 00178 sent++; 00179 } 00180 return sent; //Well, this is bound to be len if no interrupt mechanism 00181 } 00182 00183 /** 00184 * Aborts a blocking sio_read() call. 00185 * 00186 * @param fd serial device handle 00187 */ 00188 void sio_read_abort(sio_fd_t fd) 00189 { 00190 m_abort = true; 00191 } 00192 00193 //} 00194 00195 #endif 00196
Generated on Tue Jul 12 2022 15:37:05 by 1.7.2