-
Notifications
You must be signed in to change notification settings - Fork 0
/
unchan.cpp
184 lines (147 loc) · 5 KB
/
unchan.cpp
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
#include <libusb-1.0/libusb.h>
#define __INTERNAL_UNCHAN_IMPL
#include "common.hpp"
#include "visualizer.hpp"
/*
* Development Note
*
* - Fl_Widget constructor does not copy label, for dynamic string use
* copy_label().
*
*/
class DevicePanel {
Fl_Group *group;
Fl_Int_Input *txtReq, *txtLen, *txtVal, *txtIdx;
libusb_device_handle *dev;
unsigned char rbuf[256];
VisualizerPanel vizPanel;
public:
DevicePanel(libusb_device_handle *h) : dev{h}
{
group = new Fl_Group(40, 60, 520, 300);
group->box(FL_BORDER_BOX);
group->color(fl_lighter(fl_lighter(fl_lighter(FL_BLUE))));
int left = group->x() + 10 + 120;
txtReq = new Fl_Int_Input(left, group->y() + 10,
100, 30,
"bRequest (HEX):");
txtVal = new Fl_Int_Input(left, txtReq->y() + txtReq->h() + 10,
100, 30,
"wValue:");
txtIdx = new Fl_Int_Input(left, txtVal->y() + txtVal->h() + 10,
100, 30,
"wIndex:");
txtLen = new Fl_Int_Input(left, txtIdx->y() + txtIdx->h() + 10,
100, 30,
"wLength:");
txtVal->value("0");
txtIdx->value("0");
auto btnSend = new Fl_Button(txtLen->x() + txtLen->w() + 10, txtLen->y(),
100, 30,
"send");
btnSend->callback(ButtonSend, REFWRAP(*this));
vizPanel.Init();
group->end();
}
~DevicePanel()
{
libusb_close(dev);
}
static void ButtonSend(Fl_Widget *w, void *p)
{
DevicePanel& d = UNWRAP(p, DevicePanel);
unsigned int bRequest, wValue, wIndex, wLength;
std::istringstream i(d.txtReq->value());
i >> std::hex >> bRequest;
i = std::istringstream(d.txtVal->value());
i >> std::dec >> wValue;
i = std::istringstream(d.txtIdx->value());
i >> std::dec >> wIndex;
i = std::istringstream(d.txtLen->value());
i >> std::dec >> wLength;
int rx = libusb_control_transfer(d.dev,
LIBUSB_ENDPOINT_IN |
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_DEVICE,
(uint8_t) bRequest,
wValue,
wIndex,
d.rbuf,
(uint16_t) wLength,
200);
d.vizPanel.Set((const char *) d.rbuf, rx);
}
};
class DeviceList {
Fl_Choice *combo;
Fl_Button *button;
libusb_device **list;
public:
DeviceList() : combo{NULL}, list{NULL}
{
combo = new Fl_Choice(100, 10, 105, 30, "device:");
button = new Fl_Button(combo->x() + combo->w() + 10, combo->y(),
60, combo->h(),
"open");
button->callback(ButtonOpen, REFWRAP(*this));
}
~DeviceList()
{
if (list)
libusb_free_device_list(list, 1);
}
void Load()
{
if (list)
libusb_free_device_list(list, 1);
combo->clear();
ssize_t cnt = libusb_get_device_list(NULL, &list);
for (ssize_t i = 0; i < cnt; i++) {
unsigned char buf[128] = {};
libusb_device_descriptor desc;
libusb_get_device_descriptor(list[i], &desc);
libusb_device_handle *h = NULL;
libusb_open(list[i], &h);
if (h) {
libusb_get_string_descriptor_ascii(h, desc.iProduct, buf, 128);
libusb_close(h);
}
std::ostringstream o;
o << std::hex << std::setfill('0');
o << std::setw(4) << desc.idVendor;
o << ':';
o << std::setw(4) << desc.idProduct;
o << ' ' << buf << std::endl;
combo->add(o.str().c_str());
}
}
static void ButtonOpen(Fl_Widget *w, void *p)
{
DeviceList& d = UNWRAP(p, DeviceList);
Fl_Choice *c = d.combo;
if (c->value() == -1)
return;
libusb_device_descriptor desc;
libusb_get_device_descriptor(d.list[c->value()], &desc);
libusb_device_handle *h;
libusb_open(d.list[c->value()], &h);
libusb_claim_interface(h, 0);
d.combo->parent()->begin();
if (h)
new DevicePanel(h);
d.combo->parent()->end();
d.combo->parent()->redraw();
}
};
int main(int argc, char **argv)
{
libusb_init(NULL);
auto w = new Fl_Window(600, 400, "USB Un-chan!");
auto devList = DeviceList();
devList.Load();
w->end();
w->show(argc, argv);
int ret = Fl::run();
libusb_exit(NULL);
return ret;
}