-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinux.c
208 lines (164 loc) · 4.69 KB
/
linux.c
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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include <pthread.h>
#include <ncurses.h>
#define OPENIBOOTCMD_DUMPBUFFER 0
#define OPENIBOOTCMD_DUMPBUFFER_LEN 1
#define OPENIBOOTCMD_DUMPBUFFER_GOAHEAD 2
#define OPENIBOOTCMD_SENDCOMMAND 3
#define OPENIBOOTCMD_SENDCOMMAND_GOAHEAD 4
typedef struct OpenIBootCmd {
uint32_t command;
uint32_t dataLen;
} __attribute__ ((__packed__)) OpenIBootCmd;
usb_dev_handle* device;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
FILE* outputFile = NULL;
volatile size_t readIntoOutput = 0;
volatile int InterestWrite = 0;
#define USB_BYTES_AT_A_TIME 512
void* doOutput(void* threadid) {
OpenIBootCmd cmd;
char* buffer;
int totalLen = 0;
while(1) {
if(InterestWrite)
{
sched_yield();
continue;
}
pthread_mutex_lock(&lock);
cmd.command = OPENIBOOTCMD_DUMPBUFFER;
cmd.dataLen = 0;
usb_interrupt_write(device, 4, (char*) (&cmd), sizeof(OpenIBootCmd), 1000);
if(usb_interrupt_read(device, 3, (char*) (&cmd), sizeof(OpenIBootCmd), 1000) < 0) {
endwin();
exit(0);
}
totalLen = cmd.dataLen;
while(totalLen > 0) {
buffer = (char*) malloc(totalLen + 1);
cmd.command = OPENIBOOTCMD_DUMPBUFFER_GOAHEAD;
cmd.dataLen = totalLen;
usb_interrupt_write(device, 4, (char*) (&cmd), sizeof(OpenIBootCmd), 1000);
int read = 0;
while(read < totalLen) {
int left = (totalLen - read);
size_t toRead = (left > USB_BYTES_AT_A_TIME) ? USB_BYTES_AT_A_TIME : left;
int hasRead;
hasRead = usb_bulk_read(device, 1, buffer + read, toRead, 1000);
read += hasRead;
}
int discarded = 0;
if(readIntoOutput > 0) {
if(readIntoOutput <= read) {
fwrite(buffer, 1, readIntoOutput, outputFile);
discarded += readIntoOutput;
readIntoOutput = 0;
fclose(outputFile);
} else {
fwrite(buffer, 1, read, outputFile);
discarded += read;
readIntoOutput -= read;
}
}
*(buffer + read) = '\0';
printf("%s", buffer + discarded); fflush(stdout);
free(buffer);
cmd.command = OPENIBOOTCMD_DUMPBUFFER;
cmd.dataLen = 0;
usb_interrupt_write(device, 4, (char*) (&cmd), sizeof(OpenIBootCmd), 1000);
usb_interrupt_read(device, 3, (char*) (&cmd), sizeof(OpenIBootCmd), 1000);
totalLen = cmd.dataLen;
}
pthread_mutex_unlock(&lock);
sched_yield();
}
pthread_exit(NULL);
}
#define MAX_TO_SEND 512
void sendBuffer(char* buffer, size_t size) {
OpenIBootCmd cmd;
cmd.command = OPENIBOOTCMD_SENDCOMMAND;
cmd.dataLen = size;
usb_interrupt_write(device, 4, (char*) (&cmd), sizeof(OpenIBootCmd), 1000);
while(1) {
usb_interrupt_read(device, 3, (char*) (&cmd), sizeof(OpenIBootCmd), 1000);
if(cmd.command == OPENIBOOTCMD_SENDCOMMAND_GOAHEAD)
break;
}
int toSend = 0;
while(size > 0) {
if(size <= MAX_TO_SEND)
toSend = size;
else
toSend = MAX_TO_SEND;
usb_bulk_write(device, 2, buffer, toSend, 1000);
buffer += toSend;
size -= toSend;
}
}
void* doInput(void* threadid) {
while(1) {
int ch = getch();
char theChar = ch;
InterestWrite = 1;
pthread_mutex_lock(&lock);
sendBuffer(&theChar, 1);
pthread_mutex_unlock(&lock);
InterestWrite = 0;
sched_yield();
}
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
usb_init();
usb_find_busses();
usb_find_devices();
struct usb_bus *busses;
struct usb_bus *bus;
struct usb_device *dev;
busses = usb_get_busses();
int found = 0;
int c, i, a;
for(bus = busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
/* Check if this device is a printer */
if (dev->descriptor.idVendor != 0x05ac || dev->descriptor.idProduct != 0x1280) {
continue;
}
/* Loop through all of the interfaces */
for (i = 0; i < dev->config[0].bNumInterfaces; i++) {
for (a = 0; a < dev->config[0].interface[i].num_altsetting; a++) {
if(dev->config[0].interface[i].altsetting[a].bInterfaceClass == 0xFF
&& dev->config[0].interface[i].altsetting[a].bInterfaceSubClass == 0xFF
&& dev->config[0].interface[i].altsetting[a].bInterfaceProtocol == 0x51) {
goto done;
}
}
}
}
}
return 1;
done:
initscr();
noecho();
device = usb_open(dev);
if(!device) {
return 2;
}
if(usb_claim_interface(device, i) != 0) {
return 3;
}
pthread_t inputThread;
pthread_t outputThread;
printf("Client connected: !<filename>[@<address>] to send a file, ~<filename>[@<address>]:<len> to receive a file\n");
printf("---------------------------------------------------------------------------------------------------------\n");
pthread_create(&outputThread, NULL, doOutput, NULL);
pthread_create(&inputThread, NULL, doInput, NULL);
pthread_exit(NULL);
usb_release_interface(device, i);
usb_close(device);
}