X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=ftdipp%2Fftdi.cpp;h=d9b6fbf229b097e64dd5f60cc85fcfcd8876aefe;hp=827bca1efccc51fca5933e7e66e369972ded0b7a;hb=dcd7e8a307f75474afb23bd5da7048db91faeaa1;hpb=22d12cda4aed8d25fb79588a7261ca489db5ecd7 diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp index 827bca1..d9b6fbf 100644 --- a/ftdipp/ftdi.cpp +++ b/ftdipp/ftdi.cpp @@ -2,19 +2,33 @@ ftdi.cpp - C++ wraper for libftdi ------------------- begin : Mon Oct 13 2008 - copyright : (C) 2008 by Marek Vavruša + copyright : (C) 2008-2014 by Marek Vavruša / libftdi developers email : opensource@intra2net.com and marek@vavrusa.com ***************************************************************************/ +/* +Copyright (C) 2008-2014 by Marek Vavruša / libftdi developers -/*************************************************************************** - * * - * 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; * - * * - ***************************************************************************/ +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ +#include #include "ftdi.hpp" +#include "ftdi_i.h" #include "ftdi.h" namespace Ftdi @@ -24,7 +38,7 @@ class Context::Private { public: Private() - : ftdi(0), dev(0), open(false) + : open(false), ftdi(0), dev(0) { ftdi = ftdi_new(); } @@ -40,7 +54,7 @@ public: bool open; struct ftdi_context* ftdi; - struct usb_device* dev; + struct libusb_device* dev; std::string vendor; std::string description; @@ -65,48 +79,61 @@ bool Context::is_open() return d->open; } -int Context::open(int vendor, int product, const std::string& description, const std::string& serial) +int Context::open(int vendor, int product) { - int ret = 0; + // Open device + int ret = ftdi_usb_open(d->ftdi, vendor, product); - 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()); + if (ret < 0) + return ret; - d->dev = usb_device(d->ftdi->usb_dev); + return get_strings_and_reopen(); +} - if ((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0) - { - d->open = true; - get_strings(); - } +int Context::open(int vendor, int product, const std::string& description, const std::string& serial, unsigned int index) +{ + // translate empty strings to NULL + // -> do not use them to find the device (vs. require an empty string to be set in the EEPROM) + const char* c_description=NULL; + const char* c_serial=NULL; + if (!description.empty()) + c_description=description.c_str(); + if (!serial.empty()) + c_serial=serial.c_str(); - return ret; + int ret = ftdi_usb_open_desc_index(d->ftdi, vendor, product, c_description, c_serial, index); + + if (ret < 0) + return ret; + + return get_strings_and_reopen(); } -int Context::open(struct usb_device *dev) +int Context::open(const std::string& description) { - int ret = 0; + int ret = ftdi_usb_open_string(d->ftdi, description.c_str()); + + if (ret < 0) + return ret; + + return get_strings_and_reopen(); +} +int Context::open(struct libusb_device *dev) +{ if (dev != 0) d->dev = dev; if (d->dev == 0) return -1; - if ((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0) - { - d->open = true; - get_strings(); - } - - return ret; + return get_strings_and_reopen(); } int Context::close() { d->open = false; + d->dev = 0; return ftdi_usb_close(d->ftdi); } @@ -132,10 +159,10 @@ int Context::set_interface(enum ftdi_interface interface) return ftdi_set_interface(d->ftdi, interface); } -void Context::set_usb_device(struct usb_dev_handle *dev) +void Context::set_usb_device(struct libusb_device_handle *dev) { ftdi_set_usbdev(d->ftdi, dev); - d->dev = usb_device(dev); + d->dev = libusb_get_device(dev); } int Context::set_baud_rate(int baudrate) @@ -153,6 +180,26 @@ int Context::set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type return ftdi_set_line_property2(d->ftdi, bits, sbit, parity, break_type); } +int Context::get_usb_read_timeout() const +{ + return d->ftdi->usb_read_timeout; +} + +void Context::set_usb_read_timeout(int usb_read_timeout) +{ + d->ftdi->usb_read_timeout = usb_read_timeout; +} + +int Context::get_usb_write_timeout() const +{ + return d->ftdi->usb_write_timeout; +} + +void Context::set_usb_write_timeout(int usb_write_timeout) +{ + d->ftdi->usb_write_timeout = usb_write_timeout; +} + int Context::read(unsigned char *buf, int size) { return ftdi_read_data(d->ftdi, buf, size); @@ -247,19 +294,19 @@ int Context::set_error_char(unsigned char errorch, unsigned char enable) return ftdi_set_error_char(d->ftdi, errorch, enable); } -int Context::bitbang_enable(unsigned char bitmask) +int Context::set_bitmode(unsigned char bitmask, unsigned char mode) { - return ftdi_enable_bitbang(d->ftdi, bitmask); + return ftdi_set_bitmode(d->ftdi, bitmask, mode); } -int Context::bitbang_disable() +int Context::set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode) { - return ftdi_disable_bitbang(d->ftdi); + return ftdi_set_bitmode(d->ftdi, bitmask, mode); } -int Context::set_bitmode(unsigned char bitmask, unsigned char mode) +int Context::bitbang_disable() { - return ftdi_set_bitmode(d->ftdi, bitmask, mode); + return ftdi_disable_bitbang(d->ftdi); } int Context::read_pins(unsigned char *pins) @@ -289,21 +336,44 @@ int Context::get_strings() return 1; } -/*! \fn vendor - * \fn description - * \fn serial - * \brief Device strings properties. +int Context::get_strings_and_reopen() +{ + if ( d->dev == 0 ) + { + d->dev = libusb_get_device(d->ftdi->usb_dev); + } + + // Get device strings (closes device) + int ret=get_strings(); + if (ret < 0) + { + d->open = 0; + return ret; + } + + // Reattach device + ret = ftdi_usb_open_dev(d->ftdi, d->dev); + d->open = (ret >= 0); + + return ret; +} + +/*! \brief Device strings properties. */ const std::string& Context::vendor() { return d->vendor; } +/*! \brief Device strings properties. + */ const std::string& Context::description() { return d->description; } +/*! \brief Device strings properties. + */ const std::string& Context::serial() { return d->serial; @@ -315,7 +385,7 @@ void Context::set_context(struct ftdi_context* context) d->ftdi = context; } -void Context::set_usb_device(struct usb_device *dev) +void Context::set_usb_device(struct libusb_device *dev) { d->dev = dev; } @@ -346,39 +416,39 @@ Eeprom::~Eeprom() { } -void Eeprom::init_defaults() +int Eeprom::init_defaults(char* manufacturer, char *product, char * serial) { - return ftdi_eeprom_initdefaults(&d->eeprom); + return ftdi_eeprom_initdefaults(d->context, manufacturer, product, serial); } -void Eeprom::set_size(int size) +int Eeprom::chip_id(unsigned int *chipid) { - return ftdi_eeprom_setsize(d->context, &d->eeprom, size); + return ftdi_read_chipid(d->context, chipid); } -int Eeprom::size(unsigned char *eeprom, int maxsize) +int Eeprom::build(unsigned char *output) { - return ftdi_read_eeprom_getsize(d->context, eeprom, maxsize); + return ftdi_eeprom_build(d->context); } -int Eeprom::chip_id(unsigned int *chipid) +int Eeprom::read(unsigned char *eeprom) { - return ftdi_read_chipid(d->context, chipid); + return ftdi_read_eeprom(d->context); } -int Eeprom::build(unsigned char *output) +int Eeprom::write(unsigned char *eeprom) { - return ftdi_eeprom_build(&d->eeprom, output); + return ftdi_write_eeprom(d->context); } -int Eeprom::read(unsigned char *eeprom) +int Eeprom::read_location(int eeprom_addr, unsigned short *eeprom_val) { - return ftdi_read_eeprom(d->context, eeprom); + return ftdi_read_eeprom_location(d->context, eeprom_addr, eeprom_val); } -int Eeprom::write(unsigned char *eeprom) +int Eeprom::write_location(int eeprom_addr, unsigned short eeprom_val) { - return ftdi_write_eeprom(d->context, eeprom); + return ftdi_write_eeprom_location(d->context, eeprom_addr, eeprom_val); } int Eeprom::erase() @@ -389,30 +459,32 @@ int Eeprom::erase() class List::Private { public: - Private(struct ftdi_device_list* devlist) - : list(devlist) + Private(struct ftdi_device_list* _devlist) + : devlist(_devlist) {} ~Private() { - ftdi_list_free(&list); + if(devlist) + ftdi_list_free(&devlist); } - struct ftdi_device_list* list; + std::list list; + struct ftdi_device_list* devlist; }; List::List(struct ftdi_device_list* devlist) - : ListBase(), d( new Private(devlist) ) + : d( new Private(devlist) ) { if (devlist != 0) { // Iterate list - for (d->list = devlist; d->list != 0; d->list = d->list->next) + for (; devlist != 0; devlist = devlist->next) { Context c; - c.set_usb_device(d->list->dev); + c.set_usb_device(devlist->dev); c.get_strings(); - push_back(c); + d->list.push_back(c); } } } @@ -421,13 +493,157 @@ List::~List() { } -List* List::find_all(int vendor, int product) +/** +* Return begin iterator for accessing the contained list elements +* @return Iterator +*/ +List::iterator List::begin() +{ + return d->list.begin(); +} + +/** +* Return end iterator for accessing the contained list elements +* @return Iterator +*/ +List::iterator List::end() +{ + return d->list.end(); +} + +/** +* Return begin iterator for accessing the contained list elements +* @return Const iterator +*/ +List::const_iterator List::begin() const +{ + return d->list.begin(); +} + +/** +* Return end iterator for accessing the contained list elements +* @return Const iterator +*/ +List::const_iterator List::end() const +{ + return d->list.end(); +} + +/** +* Return begin reverse iterator for accessing the contained list elements +* @return Reverse iterator +*/ +List::reverse_iterator List::rbegin() +{ + return d->list.rbegin(); +} + +/** +* Return end reverse iterator for accessing the contained list elements +* @return Reverse iterator +*/ +List::reverse_iterator List::rend() +{ + return d->list.rend(); +} + +/** +* Return begin reverse iterator for accessing the contained list elements +* @return Const reverse iterator +*/ +List::const_reverse_iterator List::rbegin() const +{ + return d->list.rbegin(); +} + +/** +* Return end reverse iterator for accessing the contained list elements +* @return Const reverse iterator +*/ +List::const_reverse_iterator List::rend() const +{ + return d->list.rend(); + +} + +/** +* Get number of elements stored in the list +* @return Number of elements +*/ +List::ListType::size_type List::size() const +{ + return d->list.size(); +} + +/** +* Check if list is empty +* @return True if empty, false otherwise +*/ +bool List::empty() const +{ + return d->list.empty(); +} + +/** + * Removes all elements. Invalidates all iterators. + * Do it in a non-throwing way and also make + * sure we really free the allocated memory. + */ +void List::clear() +{ + ListType().swap(d->list); + + // Free device list + if (d->devlist) + { + ftdi_list_free(&d->devlist); + d->devlist = 0; + } +} + +/** + * Appends a copy of the element as the new last element. + * @param element Value to copy and append +*/ +void List::push_back(const Context& element) +{ + d->list.push_back(element); +} + +/** + * Adds a copy of the element as the new first element. + * @param element Value to copy and add +*/ +void List::push_front(const Context& element) +{ + d->list.push_front(element); +} + +/** + * Erase one element pointed by iterator + * @param pos Element to erase + * @return Position of the following element (or end()) +*/ +List::iterator List::erase(iterator pos) +{ + return d->list.erase(pos); +} + +/** + * Erase a range of elements + * @param beg Begin of range + * @param end End of range + * @return Position of the element after the erased range (or end()) +*/ +List::iterator List::erase(iterator beg, iterator end) +{ + return d->list.erase(beg, end); +} + +List* List::find_all(Context &context, 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); + ftdi_usb_find_all(context.context(), &dlist, vendor, product); return new List(dlist); }