/*
    Cross-platform serial / RS232 library
    Version 0.21, 11/10/2015
    -> All platforms header
    -> rs232.h

    The MIT License (MIT)

    Copyright (c) 2013-2015 Fr�d�ric Meslin, Florent Touchard
    Email: fredericmeslin@hotmail.com
    Website: www.fredslab.net
    Twitter: @marzacdev

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#ifndef RS232_H
#define RS232_H

#ifdef __cplusplus
extern "C" {
#endif

    #include <stdint.h>
    #include <stdlib.h>

/*****************************************************************************/
    /* Doxywizard specific */
    /**
    * \mainpage RS232
    * \section intro_sec C / C++ RS232 cross-platform serial library
    * <b>Version 0.21 - 11/10/2015</b>
    *
    * Supported platforms:  
    * - Windows (XP / Win7, possibly 8 and 10)  
    * - Linux  
    * - MacOS X  
    *
    * Copyright (c) 2013-2015 Fr&eacute;d&eacute;ric Meslin, Florent Touchard <br>
    * Email: fredericmeslin@hotmail.com <br>
    * Website: www.fredslab.net <br>
    * Twitter: \@marzacdev <br>
    */

#define PARITY_NONE          0x00000000          //see comOpen()
#define PARITY_EVEN          0x10000000          //see comOpen()
#define PARITY_ODD           0x20000000          //see comOpen()
#define PARITY_SPACE         0x30000000          //see comOpen()
#define PARITY_MARK          0x40000000          //see comOpen()

typedef struct comPortList_t * comPortList;
typedef struct comPort_t * comPort;

/*****************************************************************************/
    /**
     * \fn int comEnumerate()
     * \brief Enumerate available serial ports (Serial, USB serial, Bluetooth serial)
     * \return number of enumerated ports
     */
    int comEnumerate(comPortList * portList);
    
    /**
     * \fn comFreePortList()
     * @param portList frees a port list generated by comEnumerate
     */
    void comFreePortList(comPortList portList);
    
    /**
     * \fn const char * comGetPortName(int index)
     * \brief Get port user-friendly name
     * \param[in] an Iterator over portList
     * \return null terminated port name which is next in portlist
     */    
    const char * comGetNextPortName(comPortList * portListIter);
    
    /**
     * \fn const char * comGetPortName(int index)
     * \brief Get port user-friendly name
     * \param[in] an Iterator over portList
     * \return null terminated full name which is next in portlist
     */
    const char * comGetNextFullName(comPortList * portListIter);
    
/*****************************************************************************/
    /**
     * \fn int comOpen(int index, int baudrate)
     * \brief Try to open a port at a specific baudrate
     * \brief (No parity, single stop bit, no hardware flow control)
     * \param[in] index port index
     * \param[in] baudrate port baudrate
     * \param[in] parity (i.E. PARITY_NONE).
     * \return comPort object on success otherwise NULL
     */        
    comPort comOpen(const char * portName, int baudrate, int parity);
    
    /**
     * \fn void comClose(int index)
     * \brief Close an opened port
     * \param[in] index port index
     */            
    void comClose(comPort port);

/*****************************************************************************/
    /**
     * \fn int comWrite(int index, const char * buffer, size_t len)
     * \brief Write data to the port (non-blocking)
     * \param[in] index port index
     * \param[in] buffer pointer to transmit buffer
     * \param[in] len length of transmit buffer in bytes
     * \return number of bytes transferred
     */            
    int comWrite(comPort port, const char * buffer, size_t len);
    
    /**
     * \fn int comRead(int index, const char * buffer, size_t len)
     * \brief Read data from the port (non-blocking)
     * \param[in] index port index
     * \param[out] buffer pointer to receive buffer
     * \param[in] len length of receive buffer in bytes
     * \return number of bytes transferred
     */                
    int comRead(comPort port, char * buffer, size_t len);

    
    /**
     * \brief A blocking version of comRead().
     * \param[in] index port index
     * \param[in] buffer pointer to receive buffer
     * \param[out] len length of receive buffer in bytes
     * \param[in] timeout maximum number of ms to wait until 'len' bytes are received.
     * \param[in] interbyte_timeout_ms maximum time in ms to wait between individual bytes (Windows only).
     * \return number of bytes transferred
     */
    int comReadBlocking(comPort port, char * buffer, size_t len, unsigned timeout, unsigned interbyte_timeout_ms);
    
    
    /**
     * \brief Set DTR Line
     * \param[in] index port index
     * \param[in] new state of DTR line
     * \return 0 on failure otherwise 1
     */
    int comSetDtr(comPort port, int state);
    
    
    /**
     * \brief Set RTS Line
     * \param[in] index port index
     * \param[in] new state of RTS line
     * \return 0 on failure otherwise 1
     */
    int comSetRts(comPort port, int state);
    
    
#ifdef __cplusplus
}
#endif

#endif // RS232_H
