You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
2.9 KiB
145 lines
2.9 KiB
8 years ago
|
/*
|
||
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
||
|
*
|
||
|
* Licensed under TREZOR License
|
||
|
* see LICENSE file for details
|
||
|
*/
|
||
|
|
||
9 years ago
|
#include <arpa/inet.h>
|
||
|
#include <sys/socket.h>
|
||
7 years ago
|
#include <sys/poll.h>
|
||
9 years ago
|
#include <fcntl.h>
|
||
9 years ago
|
#include <assert.h>
|
||
8 years ago
|
#include <stdlib.h>
|
||
7 years ago
|
#include <string.h>
|
||
9 years ago
|
|
||
7 years ago
|
#include "../../trezorhal/usb.h"
|
||
|
#include "../../trezorhal/touch.h"
|
||
8 years ago
|
|
||
7 years ago
|
#define TREZOR_UDP_IFACE 0
|
||
8 years ago
|
#define TREZOR_UDP_PORT 21324
|
||
9 years ago
|
|
||
7 years ago
|
static int sock;
|
||
9 years ago
|
static struct sockaddr_in si_me, si_other;
|
||
|
static socklen_t slen = 0;
|
||
|
|
||
7 years ago
|
int usb_init(const usb_dev_info_t *dev_info) {
|
||
|
(void)dev_info;
|
||
9 years ago
|
|
||
7 years ago
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||
|
if (sock < 0) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
fcntl(sock, F_SETFL, O_NONBLOCK);
|
||
9 years ago
|
|
||
9 years ago
|
si_me.sin_family = AF_INET;
|
||
8 years ago
|
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);
|
||
|
}
|
||
9 years ago
|
|
||
7 years ago
|
int b = bind(sock, (struct sockaddr*)&si_me, sizeof(si_me));
|
||
|
if (b < 0) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int usb_deinit(void) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int usb_start(void) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int usb_stop(void) {
|
||
|
return 0;
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
int usb_hid_add(const usb_hid_info_t *info) {
|
||
|
return 0;
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
int usb_vcp_add(const usb_vcp_info_t *info) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int usb_hid_can_read(uint8_t iface_num) {
|
||
7 years ago
|
if (iface_num != TREZOR_UDP_IFACE) {
|
||
|
return 0;
|
||
|
}
|
||
7 years ago
|
struct pollfd fds[] = {
|
||
|
{ sock, POLLIN, 0 },
|
||
|
};
|
||
|
int r = poll(fds, 1, 0);
|
||
|
if (r > 0) {
|
||
|
return 1;
|
||
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int usb_hid_can_write(uint8_t iface_num) {
|
||
7 years ago
|
if (iface_num != TREZOR_UDP_IFACE) {
|
||
|
return 0;
|
||
|
}
|
||
7 years ago
|
struct pollfd fds[] = {
|
||
|
{ sock, POLLOUT, 0 },
|
||
|
};
|
||
|
int r = poll(fds, 1, 0);
|
||
|
if (r > 0) {
|
||
|
return 1;
|
||
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len) {
|
||
7 years ago
|
if (iface_num != TREZOR_UDP_IFACE) {
|
||
|
return 0;
|
||
|
}
|
||
9 years ago
|
struct sockaddr_in si;
|
||
8 years ago
|
socklen_t sl = sizeof(si);
|
||
7 years ago
|
ssize_t r = recvfrom(sock, buf, len, MSG_DONTWAIT, (struct sockaddr *)&si, &sl);
|
||
8 years ago
|
if (r < 0) {
|
||
|
return r;
|
||
9 years ago
|
}
|
||
|
si_other = si;
|
||
|
slen = sl;
|
||
7 years ago
|
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));
|
||
7 years ago
|
return 0;
|
||
|
}
|
||
8 years ago
|
return r;
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
int usb_hid_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {
|
||
7 years ago
|
if (iface_num != TREZOR_UDP_IFACE) {
|
||
|
return 0;
|
||
|
}
|
||
8 years ago
|
ssize_t r = len;
|
||
|
if (slen > 0) {
|
||
7 years ago
|
r = sendto(sock, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen);
|
||
9 years ago
|
}
|
||
|
return r;
|
||
|
}
|
||
7 years ago
|
|
||
|
void pendsv_kbd_intr(void) {
|
||
|
}
|
||
|
|
||
|
void mp_hal_set_vcp_iface(int iface_num) {
|
||
|
}
|