From cdf448f6d42cbda7d5ced7661f6de22ffb96cc54 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Mon, 13 Oct 2008 16:00:39 +0000 Subject: [PATCH] libftdi: (tomj) reformat according to I2n style, added copyright header and more cosmetic changes --- ftdipp/ftdi.cpp | 353 +++++++++++++++++++++++++++++-------------------------- ftdipp/ftdi.hpp | 259 +++++++++++++++++++++------------------- src/ftdi.c | 2 +- 3 files changed, 323 insertions(+), 291 deletions(-) diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp index 0515f56..7c1ba60 100644 --- a/ftdipp/ftdi.cpp +++ b/ftdipp/ftdi.cpp @@ -1,3 +1,19 @@ +/*************************************************************************** + ftdi.cpp - C++ wraper for libftdi + ------------------- + begin : Mon Oct 13 2008 + copyright : (C) 2008 by Marek Vavruša + email : opensource@intra2net.com and marek@vavrusa.com + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License * + * version 2.1 as published by the Free Software Foundation; * + * * + ***************************************************************************/ + #include "ftdi.hpp" #include "ftdi.h" @@ -6,267 +22,268 @@ namespace Ftdi class Context::Private { - public: - Private() - : ftdi(0), dev(0), open(false) - {} - - bool open; - - struct ftdi_context* ftdi; - struct usb_device* dev; - - std::string vendor; - std::string description; - std::string serial; +public: + Private() + : ftdi(0), dev(0), open(false) + { + } + + bool open; + + struct ftdi_context* ftdi; + struct usb_device* dev; + + std::string vendor; + std::string description; + std::string serial; }; /*! \brief Constructor. */ Context::Context() - : d( new Private() ) + : d( new Private() ) { - d->ftdi = ftdi_new(); + d->ftdi = ftdi_new(); } /*! \brief Destructor. */ Context::~Context() { - if(d->open) - close(); - - ftdi_free(d->ftdi); - delete d; + if (d->open) + close(); + + ftdi_free(d->ftdi); + delete d; } bool Context::is_open() { - return d->open; + return d->open; } int Context::open(int vendor, int product, const std::string& description, const std::string& serial) { - int ret = 0; - if(description.empty() && serial.empty()) - ret = ftdi_usb_open(d->ftdi, vendor, product); - else - ret = ftdi_usb_open_desc(d->ftdi, vendor, product, description.c_str(), serial.c_str()); + int ret = 0; + + if (description.empty() && serial.empty()) + ret = ftdi_usb_open(d->ftdi, vendor, product); + else + ret = ftdi_usb_open_desc(d->ftdi, vendor, product, description.c_str(), serial.c_str()); - d->dev = usb_device(d->ftdi->usb_dev); + d->dev = usb_device(d->ftdi->usb_dev); - if((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0) - { - d->open = true; - get_strings(); - } + if ((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0) + { + d->open = true; + get_strings(); + } - return ret; + return ret; } int Context::open(struct usb_device *dev) { - int ret = 0; - - if(dev != 0) - d->dev = dev; + int ret = 0; + + if (dev != 0) + d->dev = dev; + + if (d->dev == 0) + return -1; - if(d->dev == 0) - return -1; - - if((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0) - { - d->open = true; - get_strings(); - } + if ((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0) + { + d->open = true; + get_strings(); + } - return ret; + return ret; } int Context::close() { - d->open = false; - return ftdi_usb_close(d->ftdi); + d->open = false; + return ftdi_usb_close(d->ftdi); } int Context::reset() { - return ftdi_usb_reset(d->ftdi); + return ftdi_usb_reset(d->ftdi); } int Context::flush(int mask) { - int ret = 1; - if(mask & Input) - ret &= ftdi_usb_purge_rx_buffer(d->ftdi); - if(mask & Output) - ret &= ftdi_usb_purge_tx_buffer(d->ftdi); + int ret = 1; - return ret; + if (mask & Input) + ret &= ftdi_usb_purge_rx_buffer(d->ftdi); + if (mask & Output) + ret &= ftdi_usb_purge_tx_buffer(d->ftdi); + + return ret; } int Context::set_interface(enum ftdi_interface interface) { - return ftdi_set_interface(d->ftdi, interface); + return ftdi_set_interface(d->ftdi, interface); } void Context::set_usb_device(struct usb_dev_handle *dev) { - ftdi_set_usbdev(d->ftdi, dev); - d->dev = usb_device(dev); + ftdi_set_usbdev(d->ftdi, dev); + d->dev = usb_device(dev); } int Context::set_baud_rate(int baudrate) { - return ftdi_set_baudrate(d->ftdi, baudrate); + return ftdi_set_baudrate(d->ftdi, baudrate); } int Context::set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) { - return ftdi_set_line_property(d->ftdi, bits, sbit, parity); + return ftdi_set_line_property(d->ftdi, bits, sbit, parity); } int Context::set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type) { - return ftdi_set_line_property2(d->ftdi, bits, sbit, parity, break_type); + return ftdi_set_line_property2(d->ftdi, bits, sbit, parity, break_type); } int Context::read(unsigned char *buf, int size) { - return ftdi_read_data(d->ftdi, buf, size); + return ftdi_read_data(d->ftdi, buf, size); } int Context::set_read_chunk_size(unsigned int chunksize) { - return ftdi_read_data_set_chunksize(d->ftdi, chunksize); + return ftdi_read_data_set_chunksize(d->ftdi, chunksize); } int Context::read_chunk_size() { - unsigned chunk = -1; - if(ftdi_read_data_get_chunksize(d->ftdi, &chunk) < 0) - return -1; - - return chunk; -} + unsigned chunk = -1; + if (ftdi_read_data_get_chunksize(d->ftdi, &chunk) < 0) + return -1; + return chunk; +} int Context::write(unsigned char *buf, int size) { - return ftdi_write_data(d->ftdi, buf, size); + return ftdi_write_data(d->ftdi, buf, size); } int Context::set_write_chunk_size(unsigned int chunksize) { - return ftdi_write_data_set_chunksize(d->ftdi, chunksize); + return ftdi_write_data_set_chunksize(d->ftdi, chunksize); } int Context::write_chunk_size() { - unsigned chunk = -1; - if(ftdi_write_data_get_chunksize(d->ftdi, &chunk) < 0) - return -1; + unsigned chunk = -1; + if (ftdi_write_data_get_chunksize(d->ftdi, &chunk) < 0) + return -1; - return chunk; + return chunk; } int Context::set_flow_control(int flowctrl) { - return ftdi_setflowctrl(d->ftdi, flowctrl); + return ftdi_setflowctrl(d->ftdi, flowctrl); } int Context::set_modem_control(int mask) { - int dtr = 0, rts = 0; - - if(mask & Dtr) - dtr = 1; - if(mask & Rts) - rts = 1; + int dtr = 0, rts = 0; + + if (mask & Dtr) + dtr = 1; + if (mask & Rts) + rts = 1; - return ftdi_setdtr_rts(d->ftdi, dtr, rts); + return ftdi_setdtr_rts(d->ftdi, dtr, rts); } int Context::set_dtr(bool state) { - return ftdi_setdtr(d->ftdi, state); + return ftdi_setdtr(d->ftdi, state); } int Context::set_rts(bool state) { - return ftdi_setrts(d->ftdi, state); + return ftdi_setrts(d->ftdi, state); } int Context::set_latency(unsigned char latency) { - return ftdi_set_latency_timer(d->ftdi, latency); + return ftdi_set_latency_timer(d->ftdi, latency); } unsigned Context::latency() { - unsigned char latency = 0; - ftdi_get_latency_timer(d->ftdi, &latency); - return latency; + unsigned char latency = 0; + ftdi_get_latency_timer(d->ftdi, &latency); + return latency; } unsigned short Context::poll_modem_status() { - unsigned short status = 0; - ftdi_poll_modem_status(d->ftdi, &status); - return status; + unsigned short status = 0; + ftdi_poll_modem_status(d->ftdi, &status); + return status; } - int Context::set_event_char(unsigned char eventch, unsigned char enable) { - return ftdi_set_event_char(d->ftdi, eventch, enable); + return ftdi_set_event_char(d->ftdi, eventch, enable); } int Context::set_error_char(unsigned char errorch, unsigned char enable) { - return ftdi_set_error_char(d->ftdi, errorch, enable); + return ftdi_set_error_char(d->ftdi, errorch, enable); } int Context::bitbang_enable(unsigned char bitmask) { - return ftdi_enable_bitbang(d->ftdi, bitmask); + return ftdi_enable_bitbang(d->ftdi, bitmask); } int Context::bitbang_disable() { - return ftdi_disable_bitbang(d->ftdi); + return ftdi_disable_bitbang(d->ftdi); } int Context::set_bitmode(unsigned char bitmask, unsigned char mode) { - return ftdi_set_bitmode(d->ftdi, bitmask, mode); + return ftdi_set_bitmode(d->ftdi, bitmask, mode); } int Context::read_pins(unsigned char *pins) { - return ftdi_read_pins(d->ftdi, pins); + return ftdi_read_pins(d->ftdi, pins); } char* Context::error_string() { - return ftdi_get_error_string(d->ftdi); + return ftdi_get_error_string(d->ftdi); } int Context::get_strings() { - // Prepare buffers - char vendor[512], desc[512], serial[512]; - - int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor, 512, desc, 512, serial, 512); + // Prepare buffers + char vendor[512], desc[512], serial[512]; + + int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor, 512, desc, 512, serial, 512); + + if (ret < 0) + return -1; - if(ret < 0) - return -1; - - d->vendor = vendor; - d->description = desc; - d->serial = serial; + d->vendor = vendor; + d->description = desc; + d->serial = serial; - return 1; + return 1; } /*! \fn vendor @@ -276,148 +293,148 @@ int Context::get_strings() */ const std::string& Context::vendor() { - return d->vendor; + return d->vendor; } const std::string& Context::description() { - return d->description; + return d->description; } const std::string& Context::serial() { - return d->serial; + return d->serial; } void Context::set_context(struct ftdi_context* context) { - ftdi_free(d->ftdi); - d->ftdi = context; + ftdi_free(d->ftdi); + d->ftdi = context; } void Context::set_usb_device(struct usb_device *dev) { - d->dev = dev; + d->dev = dev; } struct ftdi_context* Context::context() { - return d->ftdi; + return d->ftdi; } class Eeprom::Private { - public: - Private() - : context(0) - {} +public: + Private() + : context(0) + {} - struct ftdi_eeprom eeprom; - struct ftdi_context* context; + struct ftdi_eeprom eeprom; + struct ftdi_context* context; }; Eeprom::Eeprom(Context* parent) - : d ( new Private() ) + : d ( new Private() ) { - d->context = parent->context(); + d->context = parent->context(); } Eeprom::~Eeprom() { - delete d; + delete d; } void Eeprom::init_defaults() { - return ftdi_eeprom_initdefaults(&d->eeprom); + return ftdi_eeprom_initdefaults(&d->eeprom); } void Eeprom::set_size(int size) { - return ftdi_eeprom_setsize(d->context, &d->eeprom, size); + return ftdi_eeprom_setsize(d->context, &d->eeprom, size); } int Eeprom::size(unsigned char *eeprom, int maxsize) { - return ftdi_read_eeprom_getsize(d->context, eeprom, maxsize); + return ftdi_read_eeprom_getsize(d->context, eeprom, maxsize); } int Eeprom::chip_id(unsigned int *chipid) { - return ftdi_read_chipid(d->context, chipid); + return ftdi_read_chipid(d->context, chipid); } int Eeprom::build(unsigned char *output) { - return ftdi_eeprom_build(&d->eeprom, output); + return ftdi_eeprom_build(&d->eeprom, output); } int Eeprom::read(unsigned char *eeprom) { - return ftdi_read_eeprom(d->context, eeprom); + return ftdi_read_eeprom(d->context, eeprom); } int Eeprom::write(unsigned char *eeprom) { - return ftdi_write_eeprom(d->context, eeprom); + return ftdi_write_eeprom(d->context, eeprom); } int Eeprom::erase() { - return ftdi_erase_eeprom(d->context); + return ftdi_erase_eeprom(d->context); } class List::Private { - public: - Private() - : list(0) - {} +public: + Private() + : list(0) + {} - struct ftdi_device_list* list; + struct ftdi_device_list* list; }; List::List(struct ftdi_device_list* devlist) - : ListBase(), d( new Private() ) + : ListBase(), d( new Private() ) { - if(devlist != 0) - { - // Iterate list - Context* c = 0; - for(d->list = devlist; d->list != 0; d->list = d->list->next) - { - c = new Context(); - c->set_usb_device(d->list->dev); - push_back(c); - } + if (devlist != 0) + { + // Iterate list + Context* c = 0; + for (d->list = devlist; d->list != 0; d->list = d->list->next) + { + c = new Context(); + c->set_usb_device(d->list->dev); + push_back(c); + } - // Store pointer - d->list = devlist; - } + // Store pointer + d->list = devlist; + } } List::~List() { - // Deallocate instances - for(iterator it = begin(); it != end(); it++) - delete *it; + // Deallocate instances + for (iterator it = begin(); it != end(); it++) + delete *it; - // Clear list - clear(); - ftdi_list_free(&d->list); + // Clear list + clear(); + ftdi_list_free(&d->list); - // Delete d-ptr - delete d; + // Delete d-ptr + delete d; } List* List::find_all(int vendor, int product) { - struct ftdi_device_list* dlist = 0; - struct ftdi_context ftdi; - ftdi_init(&ftdi); - ftdi_usb_find_all(&ftdi, &dlist, vendor, product); - ftdi_deinit(&ftdi); - return new List(dlist); + struct ftdi_device_list* dlist = 0; + struct ftdi_context ftdi; + ftdi_init(&ftdi); + ftdi_usb_find_all(&ftdi, &dlist, vendor, product); + ftdi_deinit(&ftdi); + return new List(dlist); } } diff --git a/ftdipp/ftdi.hpp b/ftdipp/ftdi.hpp index 7b6a9a0..77aee85 100644 --- a/ftdipp/ftdi.hpp +++ b/ftdipp/ftdi.hpp @@ -1,5 +1,21 @@ -#ifndef FTDICPP_H -#define FTDICPP_H +/*************************************************************************** + ftdi.hpp - C++ wrapper for libftdi + ------------------- + begin : Mon Oct 13 2008 + copyright : (C) 2008 by Marek Vavruša + email : opensource@intra2net.com and marek@vavrusa.com + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License * + * version 2.1 as published by the Free Software Foundation; * + * * + ***************************************************************************/ + +#ifndef __libftdi_hpp__ +#define __libftdi_hpp__ #include #include @@ -17,125 +33,124 @@ class Eeprom; */ class Context { - /* Friends */ - friend class Eeprom; - friend class List; - - public: - - /*! \brief Direction flags for flush(). - */ - enum Direction - { - Input, - Output - }; - - /*! \brief Modem control flags. - */ - enum ModemCtl - { - Dtr, - Rts - }; - - /* Constructor, Destructor */ - Context(); - ~Context(); - - /* Properties */ - Eeprom* eeprom(); - const std::string& vendor(); - const std::string& description(); - const std::string& serial(); - - /* Device manipulators */ - bool is_open(); - int open(struct usb_device *dev = 0); - int open(int vendor, int product, const std::string& description = std::string(), const std::string& serial = std::string()); - int close(); - int reset(); - int flush(int mask = Input|Output); - int set_interface(enum ftdi_interface interface); - void set_usb_device(struct usb_dev_handle *dev); - - /* Line manipulators */ - int set_baud_rate(int baudrate); - int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity); - int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type); - - /* I/O */ - int read(unsigned char *buf, int size); - int write(unsigned char *buf, int size); - int set_read_chunk_size(unsigned int chunksize); - int set_write_chunk_size(unsigned int chunksize); - int read_chunk_size(); - int write_chunk_size(); - - /* Async IO - TODO: should wrap? - int writeAsync(unsigned char *buf, int size); - void asyncComplete(int wait_for_more); - */ - - /* Flow control */ - int set_event_char(unsigned char eventch, unsigned char enable); - int set_error_char(unsigned char errorch, unsigned char enable); - int set_flow_control(int flowctrl); - int set_modem_control(int mask = Dtr|Rts); - int set_latency(unsigned char latency); - int set_dtr(bool state); - int set_rts(bool state); - - unsigned short poll_modem_status(); - unsigned latency(); - - /* BitBang mode */ - int set_bitmode(unsigned char bitmask, unsigned char mode); - int bitbang_enable(unsigned char bitmask); - int bitbang_disable(); - int read_pins(unsigned char *pins); - - /* Misc */ - char* error_string(); - - protected: - int get_strings(); - - /* Properties */ - struct ftdi_context* context(); - void set_context(struct ftdi_context* context); - void set_usb_device(struct usb_device *dev); - - private: - class Private; - Private *d; - - /* Disable copy constructor */ - Context(const Context &) {} - Context& operator=(const Context &) {} + /* Friends */ + friend class Eeprom; + friend class List; + +public: + /*! \brief Direction flags for flush(). + */ + enum Direction + { + Input, + Output + }; + + /*! \brief Modem control flags. + */ + enum ModemCtl + { + Dtr, + Rts + }; + + /* Constructor, Destructor */ + Context(); + ~Context(); + + /* Properties */ + Eeprom* eeprom(); + const std::string& vendor(); + const std::string& description(); + const std::string& serial(); + + /* Device manipulators */ + bool is_open(); + int open(struct usb_device *dev = 0); + int open(int vendor, int product, const std::string& description = std::string(), const std::string& serial = std::string()); + int close(); + int reset(); + int flush(int mask = Input|Output); + int set_interface(enum ftdi_interface interface); + void set_usb_device(struct usb_dev_handle *dev); + + /* Line manipulators */ + int set_baud_rate(int baudrate); + int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity); + int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type); + + /* I/O */ + int read(unsigned char *buf, int size); + int write(unsigned char *buf, int size); + int set_read_chunk_size(unsigned int chunksize); + int set_write_chunk_size(unsigned int chunksize); + int read_chunk_size(); + int write_chunk_size(); + + /* Async IO + TODO: should wrap? + int writeAsync(unsigned char *buf, int size); + void asyncComplete(int wait_for_more); + */ + + /* Flow control */ + int set_event_char(unsigned char eventch, unsigned char enable); + int set_error_char(unsigned char errorch, unsigned char enable); + int set_flow_control(int flowctrl); + int set_modem_control(int mask = Dtr|Rts); + int set_latency(unsigned char latency); + int set_dtr(bool state); + int set_rts(bool state); + + unsigned short poll_modem_status(); + unsigned latency(); + + /* BitBang mode */ + int set_bitmode(unsigned char bitmask, unsigned char mode); + int bitbang_enable(unsigned char bitmask); + int bitbang_disable(); + int read_pins(unsigned char *pins); + + /* Misc */ + char* error_string(); + +protected: + int get_strings(); + + /* Properties */ + struct ftdi_context* context(); + void set_context(struct ftdi_context* context); + void set_usb_device(struct usb_device *dev); + +private: + class Private; + Private *d; + + /* Disable copy constructor */ + Context(const Context &) {} + Context& operator=(const Context &) {} }; /*! \brief Device EEPROM. */ class Eeprom { - public: - Eeprom(Context* parent); - ~Eeprom(); - - void init_defaults(); - void set_size(int size); - int size(unsigned char *eeprom, int maxsize); - int chip_id(unsigned int *chipid); - int build(unsigned char *output); - int read(unsigned char *eeprom); - int write(unsigned char *eeprom); - int erase(); - - private: - class Private; - Private *d; +public: + Eeprom(Context* parent); + ~Eeprom(); + + void init_defaults(); + void set_size(int size); + int size(unsigned char *eeprom, int maxsize); + int chip_id(unsigned int *chipid); + int build(unsigned char *output); + int read(unsigned char *eeprom); + int write(unsigned char *eeprom); + int erase(); + +private: + class Private; + Private *d; }; typedef std::list ListBase; @@ -144,15 +159,15 @@ typedef std::list ListBase; */ class List : public ListBase { - public: - List(struct ftdi_device_list* devlist = 0); - ~List(); +public: + List(struct ftdi_device_list* devlist = 0); + ~List(); - static List* find_all(int vendor, int product); + static List* find_all(int vendor, int product); - private: - class Private; - Private *d; +private: + class Private; + Private *d; }; } diff --git a/src/ftdi.c b/src/ftdi.c index a08abbf..b770e72 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -119,7 +119,7 @@ struct ftdi_context *ftdi_new() if (ftdi_init(ftdi) != 0) { free(ftdi); - return NULL; + return NULL; } return ftdi; -- 1.7.1