use ref_name as release name
[mfgtools.git] / libuuu / trans.h
blob3f137d494f3a467606aae2f363b1e00658c20eb6
1 /*
2 * Copyright 2018 NXP.
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.
31 #pragma once
33 #include <atomic>
34 #include <string>
35 #include <vector>
37 class TransBase
39 public:
40 TransBase() = default;
41 TransBase(const TransBase&) = delete;
42 TransBase& operator=(const TransBase&) = delete;
43 virtual ~TransBase();
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);
52 protected:
53 void * m_devhandle = nullptr;
56 class EPInfo
58 public:
59 constexpr EPInfo() = default;
60 constexpr EPInfo(int a, int size) : addr{a}, package_size{size} {}
61 int addr = 0;
62 int package_size = 64;
65 class USBTrans : public TransBase
67 public:
68 int open(void *p) override;
69 int close() override;
71 protected:
72 std::vector<EPInfo> m_EPs;
74 class HIDTrans : public USBTrans
76 public:
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;
85 private:
86 int m_outEP = 0;
87 const int m_read_timeout = 1000;
88 int m_set_report = 9;
91 class BulkTrans : public USBTrans
93 public:
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;
101 private:
102 size_t m_MaxTransPreRequest = 0x100000;
103 int m_b_send_zero = 0;
104 EPInfo m_ep_in;
105 EPInfo m_ep_out;
106 uint64_t m_timeout = 2000;
109 int polling_usb(std::atomic<int>& bexit);