forked from mon/Arduino-USB-Rename
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_rename.cpp
62 lines (54 loc) · 1.83 KB
/
usb_rename.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
#include "usb_rename.h"
static
bool SendControl(u8 d)
{
return USB_SendControl(0, &d, 1) == 1;
}
// Send a USB descriptor string. The string is stored in PROGMEM as a
// plain ASCII string but is sent out as UTF-16 with the correct 2-byte
// prefix
// taken from Arduino's USBCore.cpp
static bool USB_SendStringDescriptor(const char*string_P, size_t string_len, uint8_t flags) {
SendControl(2 + string_len * 2);
SendControl(3);
bool pgm = flags & TRANSFER_PGM;
for(size_t i = 0; i < string_len; i++) {
bool r = SendControl(pgm ? pgm_read_byte(&string_P[i]) : (u8)string_P[i]);
r &= SendControl(0); // high byte
if(!r) {
return false;
}
}
return true;
}
USBRename::USBRename(
const char *product_name,
const char *manufacturer_name,
const char *serial_num) :
manufacturer_name(manufacturer_name),
product_name(product_name),
serial_num(serial_num),
PluggableUSBModule(1, 1, epType) {
PluggableUSB().plug(this);
}
int USBRename::getInterface(uint8_t* interfaceCount) {
return 0;
}
int USBRename::getDescriptor(USBSetup& setup)
{
if(setup.wValueH != USB_STRING_DESCRIPTOR_TYPE) {
return 0;
}
if(setup.wValueL == IMANUFACTURER && manufacturer_name) {
return USB_SendStringDescriptor(manufacturer_name, strlen(manufacturer_name), 0);
} else if (setup.wValueL == IPRODUCT && product_name) {
return USB_SendStringDescriptor(product_name, strlen(product_name), 0);
} else if (setup.wValueL == ISERIAL && serial_num) {
return USB_SendStringDescriptor(serial_num, strlen(serial_num), 0);
}
return 0;
}
bool USBRename::setup(USBSetup& setup)
{
return false;
}