2017-12-13 18:38:51 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2017-12-13 18:38:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2022-02-08 15:54:32 +00:00
|
|
|
#include <unistd.h>
|
2017-12-13 18:38:51 +00:00
|
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "debug.h"
|
2017-12-13 18:38:51 +00:00
|
|
|
#include "messages.h"
|
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
static volatile char tiny = 0;
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void usbInit(void) { emulatorSocketInit(); }
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2018-04-04 14:03:12 +00:00
|
|
|
#if DEBUG_LINK
|
|
|
|
#define _ISDBG (((iface == 1) ? 'd' : 'n'))
|
|
|
|
#else
|
|
|
|
#define _ISDBG ('n')
|
|
|
|
#endif
|
|
|
|
|
2022-02-08 15:54:32 +00:00
|
|
|
void waitAndProcessUSBRequests(uint32_t millis) {
|
2019-03-29 16:10:31 +00:00
|
|
|
emulatorPoll();
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2021-02-09 18:07:48 +00:00
|
|
|
static uint8_t buffer[USB_PACKET_SIZE];
|
2018-04-04 14:03:12 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int iface = 0;
|
2021-07-30 13:09:29 +00:00
|
|
|
if (emulatorSocketRead(&iface, buffer, sizeof(buffer), millis) > 0) {
|
2019-03-29 16:10:31 +00:00
|
|
|
if (!tiny) {
|
2021-07-30 13:09:29 +00:00
|
|
|
do {
|
|
|
|
msg_read_common(_ISDBG, buffer, sizeof(buffer));
|
|
|
|
} while (emulatorSocketRead(&iface, buffer, sizeof(buffer), 0) > 0);
|
2019-03-29 16:10:31 +00:00
|
|
|
} else {
|
|
|
|
msg_read_tiny(buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2021-07-30 13:09:29 +00:00
|
|
|
const uint8_t *data;
|
|
|
|
while ((data = msg_out_data()) != NULL) {
|
2021-02-09 18:07:48 +00:00
|
|
|
emulatorSocketWrite(0, data, USB_PACKET_SIZE);
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2018-04-04 14:03:12 +00:00
|
|
|
#if DEBUG_LINK
|
2021-07-30 13:09:29 +00:00
|
|
|
while ((data = msg_debug_out_data()) != NULL) {
|
2021-02-09 18:07:48 +00:00
|
|
|
emulatorSocketWrite(1, data, USB_PACKET_SIZE);
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2018-04-04 14:03:12 +00:00
|
|
|
#endif
|
2017-12-13 18:38:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 15:54:32 +00:00
|
|
|
void usbPoll(void) { waitAndProcessUSBRequests(0); }
|
2021-07-30 13:09:29 +00:00
|
|
|
|
2017-12-13 18:38:51 +00:00
|
|
|
char usbTiny(char set) {
|
2019-03-29 16:10:31 +00:00
|
|
|
char old = tiny;
|
|
|
|
tiny = set;
|
|
|
|
return old;
|
2017-12-13 18:38:51 +00:00
|
|
|
}
|
2021-12-06 21:08:20 +00:00
|
|
|
|
2022-02-08 15:54:32 +00:00
|
|
|
void usbFlush(uint32_t millis) {
|
|
|
|
const uint8_t *data;
|
|
|
|
while ((data = msg_out_data()) != NULL) {
|
|
|
|
emulatorSocketWrite(0, data, USB_PACKET_SIZE);
|
|
|
|
}
|
|
|
|
usleep(millis * 1000);
|
|
|
|
}
|