2017-12-13 18:38:51 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2017-12-13 18:38:51 +00:00
|
|
|
void usbPoll(void) {
|
2019-03-29 16:10:31 +00:00
|
|
|
emulatorPoll();
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static uint8_t buffer[64];
|
2018-04-04 14:03:12 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int iface = 0;
|
|
|
|
if (emulatorSocketRead(&iface, buffer, sizeof(buffer)) > 0) {
|
|
|
|
if (!tiny) {
|
|
|
|
msg_read_common(_ISDBG, buffer, sizeof(buffer));
|
|
|
|
} else {
|
|
|
|
msg_read_tiny(buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
const uint8_t *data = msg_out_data();
|
|
|
|
if (data != NULL) {
|
|
|
|
emulatorSocketWrite(0, data, 64);
|
|
|
|
}
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2018-04-04 14:03:12 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
data = msg_debug_out_data();
|
|
|
|
if (data != NULL) {
|
|
|
|
emulatorSocketWrite(1, data, 64);
|
|
|
|
}
|
2018-04-04 14:03:12 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void usbSleep(uint32_t millis) {
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t start = timer_ms();
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
while ((timer_ms() - start) < millis) {
|
|
|
|
usbPoll();
|
|
|
|
}
|
2017-12-13 18:38:51 +00:00
|
|
|
}
|