File: ftdi.cpp

package info (click to toggle)
ponyprog 3.1.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,904 kB
  • sloc: cpp: 35,932; python: 981; sh: 565; xml: 67; makefile: 45; ansic: 38
file content (704 lines) | stat: -rw-r--r-- 14,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/***************************************************************************
                          ftdi.cpp  -  C++ wrapper for libftdi
                             -------------------
    begin                : Mon Oct 13 2008
    copyright            : (C) 2008-2020 by Marek Vavruša / libftdi developers
    email                : opensource@intra2net.com and marek@vavrusa.com
 ***************************************************************************/
/*
Copyright (C) 2008-2017 by Marek Vavruša / libftdi developers

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 <libusb.h>
#define _FTDI_DISABLE_DEPRECATED
#include "ftdi.hpp"
//#include "ftdi_i.h"
#include "ftdi.h"

namespace Ftdi
{

class Context::Private
{
public:
    Private()
            : open(false), ftdi(0), dev(0)
    {
        ftdi = ftdi_new();
    }

    ~Private()
    {
        if (open)
            ftdi_usb_close(ftdi);

        ftdi_free(ftdi);
    }

    bool open;

    struct ftdi_context* ftdi;
    struct libusb_device* dev;

    std::string vendor;
    std::string description;
    std::string serial;
};

/*! \brief Constructor.
 */
Context::Context()
        : d( new Private() )
{
}

/*! \brief Destructor.
 */
Context::~Context()
{
}

bool Context::is_open()
{
    return d->open;
}

int Context::open(int vendor, int product)
{
    // Open device
    int ret = ftdi_usb_open(d->ftdi, vendor, product);

    if (ret < 0)
       return ret;

    return get_strings_and_reopen(false,false,false);
}

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();

    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(false,!description.empty(),!serial.empty());
}

int Context::open(const std::string& description)
{
    int ret = ftdi_usb_open_string(d->ftdi, description.c_str());

    if (ret < 0)
       return ret;

    return get_strings_and_reopen(false,true,false);
}

int Context::open(struct libusb_device *dev)
{
    if (dev != 0)
        d->dev = dev;

    if (d->dev == 0)
        return -1;

    return get_strings_and_reopen();
}

int Context::close()
{
    d->open = false;
    d->dev = 0;
    return ftdi_usb_close(d->ftdi);
}

int Context::reset()
{
    return ftdi_usb_reset(d->ftdi);
}

int Context::flush(int mask)
{
    int ret;

    switch (mask & (Input | Output)) {
    case Input:
        ret = ftdi_usb_purge_rx_buffer(d->ftdi);
        break;

    case Output:
        ret = ftdi_usb_purge_tx_buffer(d->ftdi);
        break;

    case Input | Output:
        ret = ftdi_usb_purge_buffers(d->ftdi);
        break;

    default:
        // Emulate behavior of previous version.
        ret = 1;
        break;
    }

    return ret;
}

int Context::tcflush(int mask)
{
    int ret;

    switch (mask & (Input | Output)) {
    case Input:
        ret = ftdi_tciflush(d->ftdi);
        break;

    case Output:
        ret = ftdi_tcoflush(d->ftdi);
        break;

    case Input | Output:
        ret = ftdi_tcioflush(d->ftdi);
        break;

    default:
        // Emulate behavior of previous version.
        ret = 1;
        break;
    }

    return ret;
}

int Context::set_interface(enum ftdi_interface interface)
{
    return ftdi_set_interface(d->ftdi, interface);
}

void Context::set_usb_device(struct libusb_device_handle *dev)
{
    ftdi_set_usbdev(d->ftdi, dev);
    d->dev = libusb_get_device(dev);
}

int Context::set_baud_rate(int 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);
}

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);
}

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);
}

int Context::set_read_chunk_size(unsigned int 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;
}

int Context::write(const unsigned char *buf, int 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);
}

int Context::write_chunk_size()
{
    unsigned chunk = -1;
    if (ftdi_write_data_get_chunksize(d->ftdi, &chunk) < 0)
        return -1;

    return chunk;
}

int Context::set_flow_control(int 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;

    return ftdi_setdtr_rts(d->ftdi, dtr, rts);
}

int Context::set_dtr(bool state)
{
    return ftdi_setdtr(d->ftdi, state);
}

int Context::set_rts(bool state)
{
    return ftdi_setrts(d->ftdi, state);
}

int Context::set_latency(unsigned char 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 short Context::poll_modem_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);
}

int Context::set_error_char(unsigned char errorch, unsigned char enable)
{
    return ftdi_set_error_char(d->ftdi, errorch, enable);
}

int Context::set_bitmode(unsigned char bitmask, unsigned char mode)
{
    return ftdi_set_bitmode(d->ftdi, bitmask, mode);
}

int Context::set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode)
{
    return ftdi_set_bitmode(d->ftdi, bitmask, mode);
}

int Context::bitbang_disable()
{
    return ftdi_disable_bitbang(d->ftdi);
}

int Context::read_pins(unsigned char *pins)
{
    return ftdi_read_pins(d->ftdi, pins);
}

const char* Context::error_string()
{
    return ftdi_get_error_string(d->ftdi);
}

int Context::get_strings(bool vendor, bool description, bool serial)
{
    // Prepare buffers
    char ivendor[512], idesc[512], iserial[512];

    int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor?ivendor:NULL, 512, description?idesc:NULL, 512, serial?iserial:NULL, 512);

    if (ret < 0)
        return -1;

    d->vendor = ivendor;
    d->description = idesc;
    d->serial = iserial;

    return 1;
}

int Context::get_strings_and_reopen(bool vendor, bool description, bool serial)
{
    int ret = 0;

    if(vendor || description || serial)
    {
        if (d->dev == 0)
        {
            d->dev = libusb_get_device(d->ftdi->usb_dev);
        }

        // Get device strings (closes device)
        ret=get_strings(vendor, description, serial);
        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()
{
    if(d->vendor.empty())
        get_strings_and_reopen(true,false,false);
    return d->vendor;
}

/*! \brief Device strings properties.
 */
const std::string& Context::description()
{
    if(d->description.empty())
        get_strings_and_reopen(false,true,false);
    return d->description;
}

/*! \brief Device strings properties.
 */
const std::string& Context::serial()
{
    if(d->serial.empty())
        get_strings_and_reopen(false,false,true);
    return d->serial;
}

void Context::set_context(struct ftdi_context* context)
{
    ftdi_free(d->ftdi);
    d->ftdi = context;
}

void Context::set_usb_device(struct libusb_device *dev)
{
    d->dev = dev;
}

struct ftdi_context* Context::context()
{
    return d->ftdi;
}

/**
class Eeprom::Private
{
public:
    Private()
            : context(0)
    {}

    struct ftdi_eeprom eeprom;
    struct ftdi_context* context;
};

Eeprom::Eeprom(Context* parent)
        : d ( new Private() )
{
    d->context = parent->context();
}

Eeprom::~Eeprom()
{
}

int Eeprom::init_defaults(char* manufacturer, char *product, char * serial)
{
    return ftdi_eeprom_initdefaults(d->context, manufacturer, product, serial);
}

int Eeprom::chip_id(unsigned int *chipid)
{
    return ftdi_read_chipid(d->context, chipid);
}

int Eeprom::build(unsigned char *output)
{
    return ftdi_eeprom_build(d->context);
}

int Eeprom::read(unsigned char *eeprom)
{
    return ftdi_read_eeprom(d->context);
}

int Eeprom::write(unsigned char *eeprom)
{
    return ftdi_write_eeprom(d->context);
}

int Eeprom::read_location(int eeprom_addr, unsigned short *eeprom_val)
{
    return ftdi_read_eeprom_location(d->context, eeprom_addr, eeprom_val);
}

int Eeprom::write_location(int eeprom_addr, unsigned short eeprom_val)
{
    return ftdi_write_eeprom_location(d->context, eeprom_addr, eeprom_val);
}

int Eeprom::erase()
{
    return ftdi_erase_eeprom(d->context);
}
**/

class List::Private
{
public:
    Private(struct ftdi_device_list* _devlist)
            : devlist(_devlist)
    {}

    ~Private()
    {
        if(devlist)
            ftdi_list_free(&devlist);
    }

    std::list<Context> list;
    struct ftdi_device_list* devlist;
};

List::List(struct ftdi_device_list* devlist)
        : d( new Private(devlist) )
{
    if (devlist != 0)
    {
        // Iterate list
        for (; devlist != 0; devlist = devlist->next)
        {
            Context c;
            c.set_usb_device(devlist->dev);
            c.get_strings();
            d->list.push_back(c);
        }
    }
}

List::~List()
{
}

/**
* 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;
    ftdi_usb_find_all(context.context(), &dlist, vendor, product);
    return new List(dlist);
}

}