4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
40 TransBase() = default;
41 TransBase(const TransBase
&) = delete;
42 TransBase
& operator=(const TransBase
&) = delete;
45 virtual int open(void *) { return 0; }
46 virtual int close() { return 0; }
47 virtual int write(void *buff
, size_t size
) = 0;
48 virtual int read(void *buff
, size_t size
, size_t *return_size
) = 0;
49 int write(std::vector
<uint8_t> & buff
) { return write(buff
.data(), buff
.size()); }
50 int read(std::vector
<uint8_t> &buff
);
53 void * m_devhandle
= nullptr;
59 constexpr EPInfo() = default;
60 constexpr EPInfo(int a
, int size
) : addr
{a
}, package_size
{size
} {}
62 int package_size
= 64;
65 class USBTrans
: public TransBase
68 int open(void *p
) override
;
72 std::vector
<EPInfo
> m_EPs
;
74 class HIDTrans
: public USBTrans
77 HIDTrans(int read_timeout
= 1000) : m_read_timeout
{read_timeout
} {}
78 ~HIDTrans() override
{ if (m_devhandle
) close(); m_devhandle
= nullptr; }
80 int open(void *p
) override
;
81 void set_hid_out_ep(int ep
) noexcept
{ m_outEP
= ep
; }
82 int write(void *buff
, size_t size
) override
;
83 int read(void *buff
, size_t size
, size_t *return_size
) override
;
87 const int m_read_timeout
= 1000;
91 class BulkTrans
: public USBTrans
94 BulkTrans(uint64_t timeout
= 2000) : m_timeout
{timeout
} {}
95 ~BulkTrans() override
{ if (m_devhandle
) close(); m_devhandle
= nullptr; }
97 int open(void *p
) override
;
98 int write(void *buff
, size_t size
) override
;
99 int read(void *buff
, size_t size
, size_t *return_size
) override
;
102 size_t m_MaxTransPreRequest
= 0x100000;
103 int m_b_send_zero
= 0;
106 uint64_t m_timeout
= 2000;
109 int polling_usb(std::atomic
<int>& bexit
);