PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libftdi/libftdi-0.18/ftdipp/ftdi.cpp

http://hiinks.googlecode.com/
C++ | 640 lines | 420 code | 110 blank | 110 comment | 24 complexity | f4f029f65823dc9fd0e558abf20e4936 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. /***************************************************************************
  2. ftdi.cpp - C++ wraper for libftdi
  3. -------------------
  4. begin : Mon Oct 13 2008
  5. copyright : (C) 2008 by Marek Vavruša
  6. email : opensource@intra2net.com and marek@vavrusa.com
  7. ***************************************************************************/
  8. /*
  9. Copyright (C) 2008 by Marek Vavruša
  10. The software in this package is distributed under the GNU General
  11. Public License version 2 (with a special exception described below).
  12. A copy of GNU General Public License (GPL) is included in this distribution,
  13. in the file COPYING.GPL.
  14. As a special exception, if other files instantiate templates or use macros
  15. or inline functions from this file, or you compile this file and link it
  16. with other works to produce a work based on this file, this file
  17. does not by itself cause the resulting work to be covered
  18. by the GNU General Public License.
  19. However the source code for this file must still be made available
  20. in accordance with section (3) of the GNU General Public License.
  21. This exception does not invalidate any other reasons why a work based
  22. on this file might be covered by the GNU General Public License.
  23. */
  24. #include "ftdi.hpp"
  25. #include "ftdi.h"
  26. namespace Ftdi
  27. {
  28. class Context::Private
  29. {
  30. public:
  31. Private()
  32. : ftdi(0), dev(0), open(false)
  33. {
  34. ftdi = ftdi_new();
  35. }
  36. ~Private()
  37. {
  38. if (open)
  39. ftdi_usb_close(ftdi);
  40. ftdi_free(ftdi);
  41. }
  42. bool open;
  43. struct ftdi_context* ftdi;
  44. struct usb_device* dev;
  45. std::string vendor;
  46. std::string description;
  47. std::string serial;
  48. };
  49. /*! \brief Constructor.
  50. */
  51. Context::Context()
  52. : d( new Private() )
  53. {
  54. }
  55. /*! \brief Destructor.
  56. */
  57. Context::~Context()
  58. {
  59. }
  60. bool Context::is_open()
  61. {
  62. return d->open;
  63. }
  64. int Context::open(int vendor, int product)
  65. {
  66. // Open device
  67. int ret = ftdi_usb_open(d->ftdi, vendor, product);
  68. if (ret < 0)
  69. return ret;
  70. return get_strings_and_reopen();
  71. }
  72. int Context::open(int vendor, int product, const std::string& description, const std::string& serial, unsigned int index)
  73. {
  74. // translate empty strings to NULL
  75. // -> do not use them to find the device (vs. require an empty string to be set in the EEPROM)
  76. const char* c_description=NULL;
  77. const char* c_serial=NULL;
  78. if (!description.empty())
  79. c_description=description.c_str();
  80. if (!serial.empty())
  81. c_serial=serial.c_str();
  82. int ret = ftdi_usb_open_desc_index(d->ftdi, vendor, product, c_description, c_serial, index);
  83. if (ret < 0)
  84. return ret;
  85. return get_strings_and_reopen();
  86. }
  87. int Context::open(const std::string& description)
  88. {
  89. int ret = ftdi_usb_open_string(d->ftdi, description.c_str());
  90. if (ret < 0)
  91. return ret;
  92. return get_strings_and_reopen();
  93. }
  94. int Context::open(struct usb_device *dev)
  95. {
  96. if (dev != 0)
  97. d->dev = dev;
  98. if (d->dev == 0)
  99. return -1;
  100. return get_strings_and_reopen();
  101. }
  102. int Context::close()
  103. {
  104. d->open = false;
  105. return ftdi_usb_close(d->ftdi);
  106. }
  107. int Context::reset()
  108. {
  109. return ftdi_usb_reset(d->ftdi);
  110. }
  111. int Context::flush(int mask)
  112. {
  113. int ret = 1;
  114. if (mask & Input)
  115. ret &= ftdi_usb_purge_rx_buffer(d->ftdi);
  116. if (mask & Output)
  117. ret &= ftdi_usb_purge_tx_buffer(d->ftdi);
  118. return ret;
  119. }
  120. int Context::set_interface(enum ftdi_interface interface)
  121. {
  122. return ftdi_set_interface(d->ftdi, interface);
  123. }
  124. void Context::set_usb_device(struct usb_dev_handle *dev)
  125. {
  126. ftdi_set_usbdev(d->ftdi, dev);
  127. d->dev = usb_device(dev);
  128. }
  129. int Context::set_baud_rate(int baudrate)
  130. {
  131. return ftdi_set_baudrate(d->ftdi, baudrate);
  132. }
  133. int Context::set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
  134. {
  135. return ftdi_set_line_property(d->ftdi, bits, sbit, parity);
  136. }
  137. 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)
  138. {
  139. return ftdi_set_line_property2(d->ftdi, bits, sbit, parity, break_type);
  140. }
  141. int Context::read(unsigned char *buf, int size)
  142. {
  143. return ftdi_read_data(d->ftdi, buf, size);
  144. }
  145. int Context::set_read_chunk_size(unsigned int chunksize)
  146. {
  147. return ftdi_read_data_set_chunksize(d->ftdi, chunksize);
  148. }
  149. int Context::read_chunk_size()
  150. {
  151. unsigned chunk = -1;
  152. if (ftdi_read_data_get_chunksize(d->ftdi, &chunk) < 0)
  153. return -1;
  154. return chunk;
  155. }
  156. int Context::write(unsigned char *buf, int size)
  157. {
  158. return ftdi_write_data(d->ftdi, buf, size);
  159. }
  160. int Context::set_write_chunk_size(unsigned int chunksize)
  161. {
  162. return ftdi_write_data_set_chunksize(d->ftdi, chunksize);
  163. }
  164. int Context::write_chunk_size()
  165. {
  166. unsigned chunk = -1;
  167. if (ftdi_write_data_get_chunksize(d->ftdi, &chunk) < 0)
  168. return -1;
  169. return chunk;
  170. }
  171. int Context::set_flow_control(int flowctrl)
  172. {
  173. return ftdi_setflowctrl(d->ftdi, flowctrl);
  174. }
  175. int Context::set_modem_control(int mask)
  176. {
  177. int dtr = 0, rts = 0;
  178. if (mask & Dtr)
  179. dtr = 1;
  180. if (mask & Rts)
  181. rts = 1;
  182. return ftdi_setdtr_rts(d->ftdi, dtr, rts);
  183. }
  184. int Context::set_dtr(bool state)
  185. {
  186. return ftdi_setdtr(d->ftdi, state);
  187. }
  188. int Context::set_rts(bool state)
  189. {
  190. return ftdi_setrts(d->ftdi, state);
  191. }
  192. int Context::set_latency(unsigned char latency)
  193. {
  194. return ftdi_set_latency_timer(d->ftdi, latency);
  195. }
  196. unsigned Context::latency()
  197. {
  198. unsigned char latency = 0;
  199. ftdi_get_latency_timer(d->ftdi, &latency);
  200. return latency;
  201. }
  202. unsigned short Context::poll_modem_status()
  203. {
  204. unsigned short status = 0;
  205. ftdi_poll_modem_status(d->ftdi, &status);
  206. return status;
  207. }
  208. int Context::set_event_char(unsigned char eventch, unsigned char enable)
  209. {
  210. return ftdi_set_event_char(d->ftdi, eventch, enable);
  211. }
  212. int Context::set_error_char(unsigned char errorch, unsigned char enable)
  213. {
  214. return ftdi_set_error_char(d->ftdi, errorch, enable);
  215. }
  216. int Context::bitbang_enable(unsigned char bitmask)
  217. {
  218. return ftdi_set_bitmode(d->ftdi, bitmask, BITMODE_BITBANG);
  219. }
  220. int Context::bitbang_disable()
  221. {
  222. return ftdi_disable_bitbang(d->ftdi);
  223. }
  224. int Context::set_bitmode(unsigned char bitmask, unsigned char mode)
  225. {
  226. return set_bitmode(bitmask, mode);
  227. }
  228. int Context::set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode)
  229. {
  230. return ftdi_set_bitmode(d->ftdi, bitmask, mode);
  231. }
  232. int Context::read_pins(unsigned char *pins)
  233. {
  234. return ftdi_read_pins(d->ftdi, pins);
  235. }
  236. char* Context::error_string()
  237. {
  238. return ftdi_get_error_string(d->ftdi);
  239. }
  240. int Context::get_strings()
  241. {
  242. // Prepare buffers
  243. char vendor[512], desc[512], serial[512];
  244. int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor, 512, desc, 512, serial, 512);
  245. if (ret < 0)
  246. return -1;
  247. d->vendor = vendor;
  248. d->description = desc;
  249. d->serial = serial;
  250. return 1;
  251. }
  252. int Context::get_strings_and_reopen()
  253. {
  254. // Get device strings (closes device)
  255. int ret=get_strings();
  256. if (ret < 0)
  257. {
  258. d->open = 0;
  259. return ret;
  260. }
  261. // Reattach device
  262. ret = ftdi_usb_open_dev(d->ftdi, d->dev);
  263. d->open = (ret >= 0);
  264. return ret;
  265. }
  266. /*! \brief Device strings properties.
  267. */
  268. const std::string& Context::vendor()
  269. {
  270. return d->vendor;
  271. }
  272. /*! \brief Device strings properties.
  273. */
  274. const std::string& Context::description()
  275. {
  276. return d->description;
  277. }
  278. /*! \brief Device strings properties.
  279. */
  280. const std::string& Context::serial()
  281. {
  282. return d->serial;
  283. }
  284. void Context::set_context(struct ftdi_context* context)
  285. {
  286. ftdi_free(d->ftdi);
  287. d->ftdi = context;
  288. }
  289. void Context::set_usb_device(struct usb_device *dev)
  290. {
  291. d->dev = dev;
  292. }
  293. struct ftdi_context* Context::context()
  294. {
  295. return d->ftdi;
  296. }
  297. class Eeprom::Private
  298. {
  299. public:
  300. Private()
  301. : context(0)
  302. {}
  303. struct ftdi_eeprom eeprom;
  304. struct ftdi_context* context;
  305. };
  306. Eeprom::Eeprom(Context* parent)
  307. : d ( new Private() )
  308. {
  309. d->context = parent->context();
  310. }
  311. Eeprom::~Eeprom()
  312. {
  313. }
  314. void Eeprom::init_defaults()
  315. {
  316. return ftdi_eeprom_initdefaults(&d->eeprom);
  317. }
  318. void Eeprom::set_size(int size)
  319. {
  320. return ftdi_eeprom_setsize(d->context, &d->eeprom, size);
  321. }
  322. int Eeprom::size(unsigned char *eeprom, int maxsize)
  323. {
  324. return ftdi_read_eeprom_getsize(d->context, eeprom, maxsize);
  325. }
  326. int Eeprom::chip_id(unsigned int *chipid)
  327. {
  328. return ftdi_read_chipid(d->context, chipid);
  329. }
  330. int Eeprom::build(unsigned char *output)
  331. {
  332. return ftdi_eeprom_build(&d->eeprom, output);
  333. }
  334. int Eeprom::read(unsigned char *eeprom)
  335. {
  336. return ftdi_read_eeprom(d->context, eeprom);
  337. }
  338. int Eeprom::write(unsigned char *eeprom)
  339. {
  340. return ftdi_write_eeprom(d->context, eeprom);
  341. }
  342. int Eeprom::read_location(int eeprom_addr, unsigned short *eeprom_val)
  343. {
  344. return ftdi_read_eeprom_location(d->context, eeprom_addr, eeprom_val);
  345. }
  346. int Eeprom::write_location(int eeprom_addr, unsigned short eeprom_val)
  347. {
  348. return ftdi_write_eeprom_location(d->context, eeprom_addr, eeprom_val);
  349. }
  350. int Eeprom::erase()
  351. {
  352. return ftdi_erase_eeprom(d->context);
  353. }
  354. class List::Private
  355. {
  356. public:
  357. Private(struct ftdi_device_list* _devlist)
  358. : devlist(_devlist)
  359. {}
  360. ~Private()
  361. {
  362. if(devlist)
  363. ftdi_list_free(&devlist);
  364. }
  365. std::list<Context> list;
  366. struct ftdi_device_list* devlist;
  367. };
  368. List::List(struct ftdi_device_list* devlist)
  369. : d( new Private(devlist) )
  370. {
  371. if (devlist != 0)
  372. {
  373. // Iterate list
  374. for (; devlist != 0; devlist = devlist->next)
  375. {
  376. Context c;
  377. c.set_usb_device(devlist->dev);
  378. c.get_strings();
  379. d->list.push_back(c);
  380. }
  381. }
  382. }
  383. List::~List()
  384. {
  385. }
  386. /**
  387. * Return begin iterator for accessing the contained list elements
  388. * @return Iterator
  389. */
  390. List::iterator List::begin()
  391. {
  392. return d->list.begin();
  393. }
  394. /**
  395. * Return end iterator for accessing the contained list elements
  396. * @return Iterator
  397. */
  398. List::iterator List::end()
  399. {
  400. return d->list.end();
  401. }
  402. /**
  403. * Return begin iterator for accessing the contained list elements
  404. * @return Const iterator
  405. */
  406. List::const_iterator List::begin() const
  407. {
  408. return d->list.begin();
  409. }
  410. /**
  411. * Return end iterator for accessing the contained list elements
  412. * @return Const iterator
  413. */
  414. List::const_iterator List::end() const
  415. {
  416. return d->list.end();
  417. }
  418. /**
  419. * Return begin reverse iterator for accessing the contained list elements
  420. * @return Reverse iterator
  421. */
  422. List::reverse_iterator List::rbegin()
  423. {
  424. return d->list.rbegin();
  425. }
  426. /**
  427. * Return end reverse iterator for accessing the contained list elements
  428. * @return Reverse iterator
  429. */
  430. List::reverse_iterator List::rend()
  431. {
  432. return d->list.rend();
  433. }
  434. /**
  435. * Return begin reverse iterator for accessing the contained list elements
  436. * @return Const reverse iterator
  437. */
  438. List::const_reverse_iterator List::rbegin() const
  439. {
  440. return d->list.rbegin();
  441. }
  442. /**
  443. * Return end reverse iterator for accessing the contained list elements
  444. * @return Const reverse iterator
  445. */
  446. List::const_reverse_iterator List::rend() const
  447. {
  448. return d->list.rend();
  449. }
  450. /**
  451. * Get number of elements stored in the list
  452. * @return Number of elements
  453. */
  454. List::ListType::size_type List::size() const
  455. {
  456. return d->list.size();
  457. }
  458. /**
  459. * Check if list is empty
  460. * @return True if empty, false otherwise
  461. */
  462. bool List::empty() const
  463. {
  464. return d->list.empty();
  465. }
  466. /**
  467. * Removes all elements. Invalidates all iterators.
  468. * Do it in a non-throwing way and also make
  469. * sure we really free the allocated memory.
  470. */
  471. void List::clear()
  472. {
  473. ListType().swap(d->list);
  474. // Free device list
  475. if (d->devlist)
  476. {
  477. ftdi_list_free(&d->devlist);
  478. d->devlist = 0;
  479. }
  480. }
  481. /**
  482. * Appends a copy of the element as the new last element.
  483. * @param element Value to copy and append
  484. */
  485. void List::push_back(const Context& element)
  486. {
  487. d->list.push_back(element);
  488. }
  489. /**
  490. * Adds a copy of the element as the new first element.
  491. * @param element Value to copy and add
  492. */
  493. void List::push_front(const Context& element)
  494. {
  495. d->list.push_front(element);
  496. }
  497. /**
  498. * Erase one element pointed by iterator
  499. * @param pos Element to erase
  500. * @return Position of the following element (or end())
  501. */
  502. List::iterator List::erase(iterator pos)
  503. {
  504. return d->list.erase(pos);
  505. }
  506. /**
  507. * Erase a range of elements
  508. * @param beg Begin of range
  509. * @param end End of range
  510. * @return Position of the element after the erased range (or end())
  511. */
  512. List::iterator List::erase(iterator beg, iterator end)
  513. {
  514. return d->list.erase(beg, end);
  515. }
  516. List* List::find_all(int vendor, int product)
  517. {
  518. struct ftdi_device_list* dlist = 0;
  519. struct ftdi_context ftdi;
  520. ftdi_init(&ftdi);
  521. ftdi_usb_find_all(&ftdi, &dlist, vendor, product);
  522. ftdi_deinit(&ftdi);
  523. return new List(dlist);
  524. }
  525. }