2016-10-11 12:05:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
2016-04-29 01:15:18 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
2017-06-26 14:03:20 +00:00
|
|
|
#include <sys/poll.h>
|
2016-04-29 13:05:08 +00:00
|
|
|
#include <fcntl.h>
|
2016-04-29 01:15:18 +00:00
|
|
|
#include <assert.h>
|
2016-05-05 23:57:29 +00:00
|
|
|
#include <stdlib.h>
|
2017-08-14 09:05:19 +00:00
|
|
|
#include <string.h>
|
2016-04-29 01:15:18 +00:00
|
|
|
|
2017-12-14 23:06:09 +00:00
|
|
|
#include "usb.h"
|
|
|
|
#include "touch.h"
|
2017-04-21 16:12:33 +00:00
|
|
|
|
2017-11-08 17:07:33 +00:00
|
|
|
void __attribute__((noreturn)) __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func);
|
|
|
|
|
|
|
|
#define ensure(expr, msg) (((expr) == sectrue) ? (void)0 : __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
|
|
|
|
|
2017-06-27 11:49:48 +00:00
|
|
|
#define TREZOR_UDP_IFACE 0
|
2016-05-05 23:57:29 +00:00
|
|
|
#define TREZOR_UDP_PORT 21324
|
2016-04-29 01:15:18 +00:00
|
|
|
|
2017-06-26 14:03:20 +00:00
|
|
|
static int sock;
|
2016-04-29 01:15:18 +00:00
|
|
|
static struct sockaddr_in si_me, si_other;
|
|
|
|
static socklen_t slen = 0;
|
|
|
|
|
2017-10-20 15:18:55 +00:00
|
|
|
void usb_init(const usb_dev_info_t *dev_info) {
|
2017-06-26 14:03:20 +00:00
|
|
|
(void)dev_info;
|
2016-04-29 01:15:18 +00:00
|
|
|
|
2017-06-26 14:03:20 +00:00
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
2017-10-26 21:51:39 +00:00
|
|
|
ensure(sectrue * (sock >= 0), NULL);
|
2017-06-26 14:03:20 +00:00
|
|
|
|
|
|
|
fcntl(sock, F_SETFL, O_NONBLOCK);
|
2016-04-29 13:05:08 +00:00
|
|
|
|
2016-04-29 01:15:18 +00:00
|
|
|
si_me.sin_family = AF_INET;
|
2016-05-05 23:57:29 +00:00
|
|
|
const char *ip = getenv("TREZOR_UDP_IP");
|
|
|
|
if (ip) {
|
|
|
|
si_me.sin_addr.s_addr = inet_addr(ip);
|
|
|
|
} else {
|
|
|
|
si_me.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
}
|
|
|
|
const char *port = getenv("TREZOR_UDP_PORT");
|
|
|
|
if (port) {
|
|
|
|
si_me.sin_port = htons(atoi(port));
|
|
|
|
} else {
|
|
|
|
si_me.sin_port = htons(TREZOR_UDP_PORT);
|
|
|
|
}
|
2016-04-29 01:15:18 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
ensure(sectrue * (0 == bind(sock, (struct sockaddr*)&si_me, sizeof(si_me))), NULL);
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:25:24 +00:00
|
|
|
void usb_deinit(void) {
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:25:24 +00:00
|
|
|
void usb_start(void) {
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:25:24 +00:00
|
|
|
void usb_stop(void) {
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool usb_hid_add(const usb_hid_info_t *info) {
|
|
|
|
return sectrue;
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
2017-06-07 12:35:46 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool usb_vcp_add(const usb_vcp_info_t *info) {
|
|
|
|
return sectrue;
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool usb_hid_can_read(uint8_t iface_num) {
|
2017-06-27 11:49:48 +00:00
|
|
|
if (iface_num != TREZOR_UDP_IFACE) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-06-27 11:49:48 +00:00
|
|
|
}
|
2017-06-26 14:03:20 +00:00
|
|
|
struct pollfd fds[] = {
|
|
|
|
{ sock, POLLIN, 0 },
|
|
|
|
};
|
|
|
|
int r = poll(fds, 1, 0);
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (r > 0);
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool usb_hid_can_write(uint8_t iface_num) {
|
2017-06-27 11:49:48 +00:00
|
|
|
if (iface_num != TREZOR_UDP_IFACE) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-06-27 11:49:48 +00:00
|
|
|
}
|
2017-06-26 14:03:20 +00:00
|
|
|
struct pollfd fds[] = {
|
|
|
|
{ sock, POLLOUT, 0 },
|
|
|
|
};
|
|
|
|
int r = poll(fds, 1, 0);
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (r > 0);
|
2017-06-26 14:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len) {
|
2017-06-27 11:49:48 +00:00
|
|
|
if (iface_num != TREZOR_UDP_IFACE) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-29 01:15:18 +00:00
|
|
|
struct sockaddr_in si;
|
2016-05-18 17:27:42 +00:00
|
|
|
socklen_t sl = sizeof(si);
|
2017-06-26 14:03:20 +00:00
|
|
|
ssize_t r = recvfrom(sock, buf, len, MSG_DONTWAIT, (struct sockaddr *)&si, &sl);
|
2016-05-23 15:53:42 +00:00
|
|
|
if (r < 0) {
|
|
|
|
return r;
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
si_other = si;
|
|
|
|
slen = sl;
|
2017-06-26 14:03:20 +00:00
|
|
|
static const char *ping_req = "PINGPING";
|
|
|
|
static const char *ping_resp = "PONGPONG";
|
|
|
|
if (r == strlen(ping_req) && memcmp(ping_req, buf, strlen(ping_req)) == 0) {
|
|
|
|
usb_hid_write(0, (const uint8_t *)ping_resp, strlen(ping_resp));
|
2017-06-06 17:02:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-23 15:53:42 +00:00
|
|
|
return r;
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 14:03:20 +00:00
|
|
|
int usb_hid_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {
|
2017-06-27 11:49:48 +00:00
|
|
|
if (iface_num != TREZOR_UDP_IFACE) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-04 16:53:32 +00:00
|
|
|
ssize_t r = len;
|
|
|
|
if (slen > 0) {
|
2017-06-26 14:03:20 +00:00
|
|
|
r = sendto(sock, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen);
|
2016-04-29 01:15:18 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2017-06-26 14:03:20 +00:00
|
|
|
|
|
|
|
void pendsv_kbd_intr(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_hal_set_vcp_iface(int iface_num) {
|
|
|
|
}
|