modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
dirkx
Date:
Wed Jul 21 19:25:56 2010 +0000
Revision:
0:355018f44c9f

        

Who changed what in which revision?

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