mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-28 17:11:23 +02:00
When a packet > 57 bytes is sent, it must be chunked.
Once the packet is sent, a callback is triggered to send the next chunk. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -79,8 +79,33 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void hid_write_offset(uint16_t size, uint16_t offset) {
|
||||||
|
if (*usb_get_tx() != 0x81)
|
||||||
|
DEBUG_PAYLOAD(usb_get_tx()+offset, size);
|
||||||
|
usb_write_offset(size, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hid_write(uint16_t size) {
|
||||||
|
hid_write_offset(size, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t send_buffer_size = 0;
|
||||||
|
|
||||||
|
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len) {
|
||||||
|
uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4]+1;
|
||||||
|
if (send_buffer_size > 0)
|
||||||
|
{
|
||||||
|
u2f_resp->cid = u2f_req->cid;
|
||||||
|
u2f_resp->cont.seq = seq;
|
||||||
|
hid_write_offset(64, (uint8_t *)u2f_resp - (usb_get_tx()));
|
||||||
|
send_buffer_size -= MIN(64 - 5, send_buffer_size);
|
||||||
|
u2f_resp = (U2FHID_FRAME *)((uint8_t *)u2f_resp + 64 - 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int driver_write(const uint8_t *buffer, size_t buffer_size) {
|
int driver_write(const uint8_t *buffer, size_t buffer_size) {
|
||||||
return tud_hid_report(0, buffer, buffer_size);
|
int ret = tud_hid_report(0, buffer, buffer_size);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t driver_read(uint8_t *buffer, size_t buffer_size) {
|
size_t driver_read(uint8_t *buffer, size_t buffer_size) {
|
||||||
@@ -99,17 +124,8 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep
|
|||||||
usb_rx(buffer, bufsize);
|
usb_rx(buffer, bufsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hid_write_offset(uint16_t size, uint16_t offset) {
|
|
||||||
if (*usb_get_tx() != 0x81)
|
|
||||||
DEBUG_PAYLOAD(usb_get_tx()+offset, size);
|
|
||||||
usb_write_offset(size, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void hid_write(uint16_t size) {
|
|
||||||
hid_write_offset(size, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void u2f_error(uint8_t error) {
|
void u2f_error(uint8_t error) {
|
||||||
|
u2f_resp = (U2FHID_FRAME *)usb_get_tx();
|
||||||
u2f_resp->cid = u2f_req->cid;
|
u2f_resp->cid = u2f_req->cid;
|
||||||
u2f_resp->init.cmd = U2FHID_ERROR;
|
u2f_resp->init.cmd = U2FHID_ERROR;
|
||||||
u2f_resp->init.bcntl = 1;
|
u2f_resp->init.bcntl = 1;
|
||||||
@@ -129,10 +145,9 @@ int driver_process_usb_packet(uint16_t read) {
|
|||||||
{
|
{
|
||||||
printf("command %x\n", FRAME_CMD(u2f_req));
|
printf("command %x\n", FRAME_CMD(u2f_req));
|
||||||
printf("len %d\n", MSG_LEN(u2f_req));
|
printf("len %d\n", MSG_LEN(u2f_req));
|
||||||
msg_packet.len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
if (MSG_LEN(u2f_req) > 64 - 7)
|
if (MSG_LEN(u2f_req) > 64 - 7)
|
||||||
{
|
{
|
||||||
msg_packet.current_len = 0;
|
|
||||||
msg_packet.len = MSG_LEN(u2f_req);
|
msg_packet.len = MSG_LEN(u2f_req);
|
||||||
memcpy(msg_packet.data + msg_packet.current_len, u2f_req->init.data, 64-7);
|
memcpy(msg_packet.data + msg_packet.current_len, u2f_req->init.data, 64-7);
|
||||||
msg_packet.current_len += 64 - 7;
|
msg_packet.current_len += 64 - 7;
|
||||||
@@ -144,6 +159,7 @@ int driver_process_usb_packet(uint16_t read) {
|
|||||||
msg_packet.current_len += MIN(64 - 5, msg_packet.len - msg_packet.current_len);
|
msg_packet.current_len += MIN(64 - 5, msg_packet.len - msg_packet.current_len);
|
||||||
}
|
}
|
||||||
if (u2f_req->init.cmd == U2FHID_INIT) {
|
if (u2f_req->init.cmd == U2FHID_INIT) {
|
||||||
|
u2f_resp = (U2FHID_FRAME *)usb_get_tx();
|
||||||
U2FHID_INIT_REQ *req = (U2FHID_INIT_REQ *)u2f_req->init.data;
|
U2FHID_INIT_REQ *req = (U2FHID_INIT_REQ *)u2f_req->init.data;
|
||||||
U2FHID_INIT_RESP *resp = (U2FHID_INIT_RESP *)u2f_resp->init.data;
|
U2FHID_INIT_RESP *resp = (U2FHID_INIT_RESP *)u2f_resp->init.data;
|
||||||
memcpy(resp->nonce, req->nonce, sizeof(resp->nonce));
|
memcpy(resp->nonce, req->nonce, sizeof(resp->nonce));
|
||||||
@@ -158,6 +174,7 @@ int driver_process_usb_packet(uint16_t read) {
|
|||||||
u2f_resp->init.bcntl = 17;
|
u2f_resp->init.bcntl = 17;
|
||||||
u2f_resp->init.bcnth = 0;
|
u2f_resp->init.bcnth = 0;
|
||||||
hid_write(64);
|
hid_write(64);
|
||||||
|
current_app = apps[0].select_aid(&apps[0]);
|
||||||
card_start();
|
card_start();
|
||||||
DEBUG_PAYLOAD((uint8_t *)u2f_resp, u2f_resp->init.bcntl+7);
|
DEBUG_PAYLOAD((uint8_t *)u2f_resp, u2f_resp->init.bcntl+7);
|
||||||
}
|
}
|
||||||
@@ -165,6 +182,7 @@ int driver_process_usb_packet(uint16_t read) {
|
|||||||
if (MSG_LEN(u2f_req) != 0) {
|
if (MSG_LEN(u2f_req) != 0) {
|
||||||
u2f_error(ERR_INVALID_LEN);
|
u2f_error(ERR_INVALID_LEN);
|
||||||
}
|
}
|
||||||
|
u2f_resp = (U2FHID_FRAME *)usb_get_tx();
|
||||||
memcpy(u2f_resp, u2f_req, sizeof(U2FHID_FRAME));
|
memcpy(u2f_resp, u2f_req, sizeof(U2FHID_FRAME));
|
||||||
hid_write(64);
|
hid_write(64);
|
||||||
}
|
}
|
||||||
@@ -176,7 +194,8 @@ int driver_process_usb_packet(uint16_t read) {
|
|||||||
DEBUG_PAYLOAD(apdu.data, (int)apdu.nc);
|
DEBUG_PAYLOAD(apdu.data, (int)apdu.nc);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
u2f_error(ERR_INVALID_CMD);
|
if (msg_packet.len == 0)
|
||||||
|
u2f_error(ERR_INVALID_CMD);
|
||||||
}
|
}
|
||||||
// echo back anything we received from host
|
// echo back anything we received from host
|
||||||
//tud_hid_report(0, buffer, bufsize);
|
//tud_hid_report(0, buffer, bufsize);
|
||||||
@@ -208,15 +227,8 @@ void driver_exec_finished_cont(size_t size_next, size_t offset) {
|
|||||||
u2f_resp->init.bcnth = size_next >> 8;
|
u2f_resp->init.bcnth = size_next >> 8;
|
||||||
u2f_resp->init.bcntl = size_next & 0xff;
|
u2f_resp->init.bcntl = size_next & 0xff;
|
||||||
hid_write_offset(64, offset);
|
hid_write_offset(64, offset);
|
||||||
size_next -= MIN(64-7, size_next);
|
u2f_resp = (U2FHID_FRAME *)((uint8_t *)u2f_resp + 64 - 5);
|
||||||
u2f_resp += 64;
|
|
||||||
uint8_t seq = 0;
|
send_buffer_size = size_next;
|
||||||
while (size_next > 0)
|
send_buffer_size -= MIN(64-7, send_buffer_size);
|
||||||
{
|
|
||||||
u2f_resp->cid = u2f_req->cid;
|
|
||||||
u2f_resp->cont.seq = seq++;
|
|
||||||
hid_write_offset(64, (uint8_t *)u2f_resp-(usb_get_tx()+offset));
|
|
||||||
size_next -= MIN(64 - 5, size_next);
|
|
||||||
u2f_resp += 64;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user