major refactoring/simplifying
This commit is contained in:
parent
f681d7a19a
commit
7a12eea133
2
ctap.c
2
ctap.c
@ -1068,7 +1068,7 @@ void ctap_response_init(CTAP_RESPONSE * resp)
|
||||
}
|
||||
|
||||
|
||||
uint8_t ctap_handle_packet(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp)
|
||||
uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp)
|
||||
{
|
||||
CborEncoder encoder;
|
||||
uint8_t status = 0;
|
||||
|
2
ctap.h
2
ctap.h
@ -235,7 +235,7 @@ typedef struct
|
||||
|
||||
void ctap_response_init(CTAP_RESPONSE * resp);
|
||||
|
||||
uint8_t ctap_handle_packet(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp);
|
||||
uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp);
|
||||
|
||||
// Encodes R,S signature to 2 der sequence of two integers. Sigder must be at least 72 bytes.
|
||||
// @return length of der signature
|
||||
|
@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Device specific functionality defined here
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cbor.h"
|
||||
|
||||
#include "util.h"
|
||||
#include "usbhid.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
void ctaphid_write_block(uint8_t * data)
|
||||
{
|
||||
printf("<< "); dump_hex(data, 64);
|
||||
usbhid_send(data);
|
||||
}
|
||||
|
||||
|
||||
int ctap_user_presence_test()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ctap_user_verification(uint8_t arg)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
uint32_t ctap_atomic_count(int sel)
|
||||
{
|
||||
static uint32_t counter1 = 25;
|
||||
static uint32_t counter2 = 25;
|
||||
/*return 713;*/
|
||||
if (sel == 0)
|
||||
{
|
||||
printf1(TAG_RED,"counter1: %d\n", counter1);
|
||||
return counter1++;
|
||||
}
|
||||
else
|
||||
{
|
||||
return counter2++;
|
||||
}
|
||||
}
|
||||
|
||||
int ctap_generate_rng(uint8_t * dst, size_t num)
|
||||
{
|
||||
FILE * urand = fopen("/dev/urandom","r");
|
||||
if (urand == NULL)
|
||||
{
|
||||
perror("fopen");
|
||||
exit(1);
|
||||
}
|
||||
fread(dst, 1, num, urand);
|
||||
fclose(urand);
|
||||
|
||||
/*memset(dst,0xaa,num);*/
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
14
ctaphid.c
14
ctaphid.c
@ -1,8 +1,8 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "device.h"
|
||||
#include "ctaphid.h"
|
||||
#include "ctap.h"
|
||||
#include "u2f.h"
|
||||
@ -159,11 +159,6 @@ static int is_cont_pkt(CTAPHID_PACKET * pkt)
|
||||
}
|
||||
|
||||
|
||||
static int is_timed_out()
|
||||
{
|
||||
return (millis() - active_cid_timestamp > 500);
|
||||
}
|
||||
|
||||
static int buffer_packet(CTAPHID_PACKET * pkt)
|
||||
{
|
||||
if (pkt->pkt.init.cmd & TYPE_INIT)
|
||||
@ -301,7 +296,6 @@ static void ctaphid_write(CTAPHID_WRITE_BUFFER * wb, void * _data, int len)
|
||||
|
||||
static void ctaphid_send_error(uint32_t cid, uint8_t error)
|
||||
{
|
||||
uint8_t buf[HID_MESSAGE_SIZE];
|
||||
CTAPHID_WRITE_BUFFER wb;
|
||||
ctaphid_write_buffer_init(&wb);
|
||||
|
||||
@ -335,7 +329,7 @@ static void send_init_response(uint32_t oldcid, uint32_t newcid, uint8_t * nonce
|
||||
}
|
||||
|
||||
|
||||
void u2f_hid_check_timeouts()
|
||||
void ctaphid_check_timeouts()
|
||||
{
|
||||
uint8_t i;
|
||||
for(i = 0; i < CID_MAX; i++)
|
||||
@ -412,6 +406,8 @@ void ctaphid_handle_packet(uint8_t * pkt_raw)
|
||||
return;
|
||||
}
|
||||
send_init_response(oldcid, newcid, pkt->pkt.init.payload);
|
||||
cid_del(newcid);
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
@ -538,7 +534,7 @@ void ctaphid_handle_packet(uint8_t * pkt_raw)
|
||||
}
|
||||
|
||||
ctap_response_init(&ctap_resp);
|
||||
status = ctap_handle_packet(ctap_buffer, buffer_len(), &ctap_resp);
|
||||
status = ctap_request(ctap_buffer, buffer_len(), &ctap_resp);
|
||||
|
||||
ctaphid_write_buffer_init(&wb);
|
||||
wb.cid = active_cid;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef _CTAPHID_H_H
|
||||
#define _CTAPHID_H_H
|
||||
|
||||
#include "usbhid.h"
|
||||
#include "device.h"
|
||||
#include "ctap_errors.h"
|
||||
|
||||
#define TYPE_INIT 0x80
|
||||
@ -72,7 +72,7 @@ void ctaphid_init();
|
||||
|
||||
void ctaphid_handle_packet(uint8_t * pkt_raw);
|
||||
|
||||
void u2f_hid_check_timeouts();
|
||||
void ctaphid_check_timeouts();
|
||||
|
||||
|
||||
#define ctaphid_packet_len(pkt) ((uint16_t)((pkt)->pkt.init.bcnth << 8) | ((pkt)->pkt.init.bcntl))
|
||||
|
211
device.c
Normal file
211
device.c
Normal file
@ -0,0 +1,211 @@
|
||||
#include <sys/time.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "device.h"
|
||||
#include "cbor.h"
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
|
||||
int udp_server()
|
||||
{
|
||||
int fd;
|
||||
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
|
||||
perror( "socket failed" );
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct timeval read_timeout;
|
||||
read_timeout.tv_sec = 0;
|
||||
read_timeout.tv_usec = 10;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof(struct timeval)) != 0)
|
||||
{
|
||||
perror( "setsockopt" );
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct sockaddr_in serveraddr;
|
||||
memset( &serveraddr, 0, sizeof(serveraddr) );
|
||||
serveraddr.sin_family = AF_INET;
|
||||
serveraddr.sin_port = htons( 8111 );
|
||||
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
|
||||
|
||||
if ( bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
|
||||
perror( "bind failed" );
|
||||
exit(1);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
int udp_recv(int fd, uint8_t * buf, int size)
|
||||
{
|
||||
|
||||
fd_set input;
|
||||
FD_ZERO(&input);
|
||||
FD_SET(fd, &input);
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 100;
|
||||
int n = select(fd + 1, &input, NULL, NULL, &timeout);
|
||||
if (n == -1) {
|
||||
perror("select\n");
|
||||
exit(1);
|
||||
} else if (n == 0)
|
||||
return 0;
|
||||
if (!FD_ISSET(fd, &input))
|
||||
{
|
||||
|
||||
}
|
||||
int length = recvfrom( fd, buf, size, 0, NULL, 0 );
|
||||
if ( length < 0 ) {
|
||||
perror( "recvfrom failed" );
|
||||
exit(1);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
void udp_send(int fd, uint8_t * buf, int size)
|
||||
{
|
||||
struct sockaddr_in serveraddr;
|
||||
memset( &serveraddr, 0, sizeof(serveraddr) );
|
||||
serveraddr.sin_family = AF_INET;
|
||||
serveraddr.sin_port = htons( 7112 );
|
||||
serveraddr.sin_addr.s_addr = htonl( 0x7f000001 ); // (127.0.0.1)
|
||||
|
||||
if (sendto( fd, buf, size, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
|
||||
perror( "sendto failed" );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void udp_close(int fd)
|
||||
{
|
||||
close(fd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint64_t millis()
|
||||
{
|
||||
struct timeval te;
|
||||
gettimeofday(&te, NULL); // get current time
|
||||
uint64_t milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
|
||||
return milliseconds;
|
||||
}
|
||||
|
||||
|
||||
static int serverfd = 0;
|
||||
|
||||
void usbhid_init()
|
||||
{
|
||||
// just bridge to UDP for now for pure software testing
|
||||
serverfd = udp_server();
|
||||
}
|
||||
|
||||
// Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing
|
||||
int usbhid_recv(uint8_t * msg)
|
||||
{
|
||||
int l = udp_recv(serverfd, msg, HID_MESSAGE_SIZE);
|
||||
/*if (l && l != HID_MESSAGE_SIZE)*/
|
||||
/*{*/
|
||||
/*printf("Error, recv'd message of wrong size %d", l);*/
|
||||
/*exit(1);*/
|
||||
/*}*/
|
||||
return l;
|
||||
}
|
||||
|
||||
// Send 64 byte USB HID message
|
||||
void usbhid_send(uint8_t * msg)
|
||||
{
|
||||
udp_send(serverfd, msg, HID_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
void usbhid_close()
|
||||
{
|
||||
udp_close(serverfd);
|
||||
}
|
||||
|
||||
void device_init()
|
||||
{
|
||||
usbhid_init();
|
||||
}
|
||||
|
||||
|
||||
void main_loop_delay()
|
||||
{
|
||||
struct timespec ts;
|
||||
ts.tv_sec = 0;
|
||||
ts.tv_nsec = 1000*1000*25;
|
||||
nanosleep(&ts,NULL);
|
||||
}
|
||||
|
||||
|
||||
void heartbeat()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ctaphid_write_block(uint8_t * data)
|
||||
{
|
||||
printf("<< "); dump_hex(data, 64);
|
||||
usbhid_send(data);
|
||||
}
|
||||
|
||||
|
||||
int ctap_user_presence_test()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ctap_user_verification(uint8_t arg)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
uint32_t ctap_atomic_count(int sel)
|
||||
{
|
||||
static uint32_t counter1 = 25;
|
||||
static uint32_t counter2 = 25;
|
||||
/*return 713;*/
|
||||
if (sel == 0)
|
||||
{
|
||||
printf1(TAG_RED,"counter1: %d\n", counter1);
|
||||
return counter1++;
|
||||
}
|
||||
else
|
||||
{
|
||||
return counter2++;
|
||||
}
|
||||
}
|
||||
|
||||
int ctap_generate_rng(uint8_t * dst, size_t num)
|
||||
{
|
||||
FILE * urand = fopen("/dev/urandom","r");
|
||||
if (urand == NULL)
|
||||
{
|
||||
perror("fopen");
|
||||
exit(1);
|
||||
}
|
||||
fread(dst, 1, num, urand);
|
||||
fclose(urand);
|
||||
|
||||
/*memset(dst,0xaa,num);*/
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
#ifndef _USBHID_H
|
||||
#define _USBHID_H
|
||||
#ifndef _DEVICE_H
|
||||
#define _DEVICE_H
|
||||
|
||||
void device_init();
|
||||
|
||||
uint64_t millis();
|
||||
|
||||
// HID message size in bytes
|
||||
#define HID_MESSAGE_SIZE 64
|
||||
@ -13,4 +16,8 @@ void usbhid_send(uint8_t * msg);
|
||||
|
||||
void usbhid_close();
|
||||
|
||||
void main_loop_delay();
|
||||
|
||||
void heartbeat();
|
||||
|
||||
#endif
|
7
log.c
7
log.c
@ -29,6 +29,12 @@ struct logtag tagtable[] = {
|
||||
{TAG_RED,"\x1b[31mDEBUG\x1b[0m"},
|
||||
};
|
||||
|
||||
|
||||
__attribute__((weak)) void set_logging_tag(uint32_t tag)
|
||||
{
|
||||
// nothing
|
||||
}
|
||||
|
||||
void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...)
|
||||
{
|
||||
int i;
|
||||
@ -51,6 +57,7 @@ void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...)
|
||||
printf("INVALID LOG TAG\n");
|
||||
exit(1);
|
||||
}
|
||||
set_logging_tag(tag);
|
||||
#ifdef ENABLE_FILE_LOGGING
|
||||
if (tag & TAG_FILENO)
|
||||
{
|
||||
|
1
log.h
1
log.h
@ -7,6 +7,7 @@
|
||||
void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...);
|
||||
void LOG_HEX(uint32_t tag, uint8_t * data, int length);
|
||||
void set_logging_mask(uint32_t mask);
|
||||
void set_logging_tag(uint32_t tag);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
|
40
main.c
40
main.c
@ -3,25 +3,21 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "cbor.h"
|
||||
#include "usbhid.h"
|
||||
#include "device.h"
|
||||
#include "ctaphid.h"
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
#include "ctap.h"
|
||||
|
||||
|
||||
static void check_ret(CborError ret)
|
||||
{
|
||||
if (ret != CborNoError)
|
||||
{
|
||||
printf("CborError: %d\n", ret);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef TEST
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
int count = 0, beat = 0;
|
||||
uint64_t t1 = 0;
|
||||
uint8_t hidmsg[64];
|
||||
|
||||
set_logging_mask(
|
||||
TAG_MC |
|
||||
TAG_GA |
|
||||
@ -35,21 +31,28 @@ int main(int argc, char * argv[])
|
||||
TAG_ERR
|
||||
);
|
||||
|
||||
printf("init usbhid\n");
|
||||
usbhid_init();
|
||||
printf("init device\n");
|
||||
device_init();
|
||||
|
||||
printf("init ctaphid\n");
|
||||
ctaphid_init();
|
||||
|
||||
printf("init ctap\n");
|
||||
ctap_init();
|
||||
|
||||
int count = 0;
|
||||
uint8_t hidmsg[64];
|
||||
memset(hidmsg,0,sizeof(hidmsg));
|
||||
|
||||
printf("recv'ing hid msg \n");
|
||||
|
||||
while(1)
|
||||
{
|
||||
if (millis() - t1 > 100)
|
||||
{
|
||||
/*printf("heartbeat %ld\n", beat++);*/
|
||||
heartbeat();
|
||||
t1 = millis();
|
||||
}
|
||||
|
||||
if (usbhid_recv(hidmsg) > 0)
|
||||
{
|
||||
printf("%d>> ",count++); dump_hex(hidmsg,sizeof(hidmsg));
|
||||
@ -57,12 +60,17 @@ int main(int argc, char * argv[])
|
||||
ctaphid_handle_packet(hidmsg);
|
||||
memset(hidmsg, 0, sizeof(hidmsg));
|
||||
}
|
||||
u2f_hid_check_timeouts();
|
||||
else
|
||||
{
|
||||
main_loop_delay();
|
||||
}
|
||||
ctaphid_check_timeouts();
|
||||
}
|
||||
|
||||
|
||||
// Should never get here
|
||||
usbhid_close();
|
||||
printf("done\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -11,8 +11,14 @@ $(OUTPUT_DIRECTORY)/nrf52840_xxaa.out: \
|
||||
# Source files common to all targets
|
||||
SRC_FILES += \
|
||||
$(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52840.S \
|
||||
$(PROJ_DIR)/main.c \
|
||||
$(PROJ_DIR)/../main.c \
|
||||
$(PROJ_DIR)/usb.c \
|
||||
$(PROJ_DIR)/retarget.c \
|
||||
$(PROJ_DIR)/device.c \
|
||||
$(PROJ_DIR)/../util.c \
|
||||
$(PROJ_DIR)/../log.c \
|
||||
$(PROJ_DIR)/../stubs.c \
|
||||
$(PROJ_DIR)/../ctaphid.c \
|
||||
$(SDK_ROOT)/components/boards/boards.c \
|
||||
$(SDK_ROOT)/components/libraries/util/app_error.c \
|
||||
$(SDK_ROOT)/components/libraries/util/app_error_handler_gcc.c \
|
||||
@ -82,6 +88,8 @@ INC_FOLDERS += \
|
||||
$(SDK_ROOT)/modules/nrfx \
|
||||
$(SDK_ROOT)/external/segger_rtt \
|
||||
$(PROJ_DIR) \
|
||||
$(PROJ_DIR)/.. \
|
||||
$(PROJ_DIR)/../tinycbor/src \
|
||||
$(SDK_ROOT)/components/libraries/util \
|
||||
$(SDK_ROOT)/integration/nrfx/legacy \
|
||||
$(SDK_ROOT)/modules/nrfx/drivers/include \
|
||||
@ -138,13 +146,16 @@ OPT = -O3 -g3
|
||||
# C flags common to all targets
|
||||
CFLAGS += $(OPT)
|
||||
CFLAGS += -DBOARD_PCA10056
|
||||
CFLAGS += -DNRF52
|
||||
#CFLAGS += -DBSP_DEFINES_ONLY
|
||||
CFLAGS += -DCONFIG_GPIO_AS_PINRESET
|
||||
CFLAGS += -DFLOAT_ABI_HARD
|
||||
CFLAGS += -DNRF52840_XXAA
|
||||
#CFLAGS += -DSTUB_CTAPHID
|
||||
CFLAGS += -DSTUB_CTAP
|
||||
CFLAGS += -mcpu=cortex-m4
|
||||
CFLAGS += -mthumb -mabi=aapcs
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wall -Wno-format
|
||||
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
# keep every function in a separate section, this allows linker to discard unused ones
|
||||
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
|
||||
|
@ -37,6 +37,9 @@
|
||||
|
||||
#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE
|
||||
#define SEGGER_RTT_CONFIG_DEFAULT_MODE 2
|
||||
|
||||
#define APP_FIFO_ENABLED 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
259
nrf52840/device.c
Normal file
259
nrf52840/device.c
Normal file
@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Device specific functionality here
|
||||
* */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nrf.h"
|
||||
#include "nrf_error.h"
|
||||
#include "nrf_drv_power.h"
|
||||
#include "nrf_strerror.h"
|
||||
#include "nrf_drv_rtc.h"
|
||||
#include "nrf_drv_clock.h"
|
||||
#include "nrf_drv_usbd.h"
|
||||
#include "nrf_gpio.h"
|
||||
#include "bsp.h"
|
||||
|
||||
#include "app_error.h"
|
||||
#include "app_fifo.h"
|
||||
|
||||
#include "util.h"
|
||||
#include "usb.h"
|
||||
#include "device.h"
|
||||
#include "cbor.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
|
||||
extern int _SEGGER_TERM;
|
||||
|
||||
|
||||
void set_output_terminal(uint32_t term)
|
||||
{
|
||||
_SEGGER_TERM = term;
|
||||
}
|
||||
|
||||
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
|
||||
{
|
||||
error_info_t * e = (error_info_t *)info;
|
||||
printf("Error: %d: %s at %d:%s\n",e->err_code, nrf_strerror_get(e->err_code), e->line_num, e->p_file_name);
|
||||
while(1)
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
void log_resetreason(void)
|
||||
{
|
||||
/* Reset reason */
|
||||
uint32_t rr = nrf_power_resetreas_get();
|
||||
printf("Reset reasons:\n");
|
||||
if (0 == rr)
|
||||
{
|
||||
printf("- NONE\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_RESETPIN_MASK))
|
||||
{
|
||||
printf("- RESETPIN\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_DOG_MASK ))
|
||||
{
|
||||
printf("- DOG\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_SREQ_MASK ))
|
||||
{
|
||||
printf("- SREQ\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_LOCKUP_MASK ))
|
||||
{
|
||||
printf("- LOCKUP\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_OFF_MASK ))
|
||||
{
|
||||
printf("- OFF\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_LPCOMP_MASK ))
|
||||
{
|
||||
printf("- LPCOMP\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_DIF_MASK ))
|
||||
{
|
||||
printf("- DIF\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_NFC_MASK ))
|
||||
{
|
||||
printf("- NFC\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_VBUS_MASK ))
|
||||
{
|
||||
printf("- VBUS\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
|
||||
uint64_t millis()
|
||||
{
|
||||
return (uint64_t)nrf_drv_rtc_counter_get(&rtc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void rtc_config(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
//Initialize RTC instance
|
||||
nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
|
||||
config.prescaler = 32;
|
||||
err_code = nrf_drv_rtc_init(&rtc, &config, NULL);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
//Power on RTC instance
|
||||
nrf_drv_rtc_enable(&rtc);
|
||||
}
|
||||
|
||||
static void init_power_clock(void)
|
||||
{
|
||||
ret_code_t ret;
|
||||
/* Initializing power and clock */
|
||||
ret = nrf_drv_clock_init();
|
||||
APP_ERROR_CHECK(ret);
|
||||
ret = nrf_drv_power_init(NULL);
|
||||
APP_ERROR_CHECK(ret);
|
||||
nrf_drv_clock_hfclk_request(NULL);
|
||||
nrf_drv_clock_lfclk_request(NULL);
|
||||
while (!(nrf_drv_clock_hfclk_is_running() &&
|
||||
nrf_drv_clock_lfclk_is_running()))
|
||||
{
|
||||
/* Just waiting */
|
||||
}
|
||||
}
|
||||
|
||||
void device_init()
|
||||
{
|
||||
if ((nrf_power_resetreas_get() & NRF_POWER_RESETREAS_RESETPIN_MASK) == 0)
|
||||
{
|
||||
// Hard reset. this is for engineering A sample to work for USB ...
|
||||
nrf_power_resetreas_clear(nrf_power_resetreas_get());
|
||||
nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0,31));
|
||||
nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0,31));
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
nrf_power_resetreas_clear(nrf_power_resetreas_get());
|
||||
|
||||
set_output_terminal(0);
|
||||
init_power_clock();
|
||||
rtc_config();
|
||||
usbhid_init();
|
||||
|
||||
srand(millis());
|
||||
|
||||
nrf_gpio_cfg_output(LED_1);
|
||||
nrf_gpio_cfg_output(LED_2);
|
||||
nrf_gpio_cfg_output(LED_3);
|
||||
nrf_gpio_cfg_output(LED_4);
|
||||
|
||||
nrf_gpio_pin_toggle(LED_2);
|
||||
nrf_gpio_pin_toggle(LED_3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static uint8_t fifo_buf[1024];
|
||||
app_fifo_t USBHID_RECV_FIFO;
|
||||
|
||||
void usbhid_init()
|
||||
{
|
||||
app_fifo_init(&USBHID_RECV_FIFO, fifo_buf, sizeof(fifo_buf));
|
||||
usb_init();
|
||||
}
|
||||
|
||||
// Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing
|
||||
int usbhid_recv(uint8_t * msg)
|
||||
{
|
||||
uint32_t size = 64;
|
||||
app_fifo_read(&USBHID_RECV_FIFO, msg, &size);
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
// Send 64 byte USB HID message
|
||||
void usbhid_send(uint8_t * msg)
|
||||
{
|
||||
static nrf_drv_usbd_transfer_t transfer;
|
||||
transfer.p_data.tx = msg;
|
||||
transfer.size = 64;
|
||||
|
||||
nrf_drv_usbd_ep_transfer(
|
||||
NRF_DRV_USBD_EPIN1,
|
||||
&transfer);
|
||||
}
|
||||
|
||||
void usbhid_close()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void main_loop_delay()
|
||||
{
|
||||
// no delay on embedded system
|
||||
}
|
||||
|
||||
void heartbeat()
|
||||
{
|
||||
nrf_gpio_pin_toggle(LED_1);
|
||||
nrf_gpio_pin_toggle(LED_2);
|
||||
nrf_gpio_pin_toggle(LED_3);
|
||||
nrf_gpio_pin_toggle(LED_4);
|
||||
}
|
||||
|
||||
|
||||
void ctaphid_write_block(uint8_t * data)
|
||||
{
|
||||
printf("<< "); dump_hex(data, 64);
|
||||
usbhid_send(data);
|
||||
}
|
||||
|
||||
|
||||
int ctap_user_presence_test()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ctap_user_verification(uint8_t arg)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
uint32_t ctap_atomic_count(int sel)
|
||||
{
|
||||
static uint32_t counter1 = 25;
|
||||
static uint32_t counter2 = 25;
|
||||
/*return 713;*/
|
||||
if (sel == 0)
|
||||
{
|
||||
printf1(TAG_RED,"counter1: %d\n", counter1);
|
||||
return counter1++;
|
||||
}
|
||||
else
|
||||
{
|
||||
return counter2++;
|
||||
}
|
||||
}
|
||||
|
||||
int ctap_generate_rng(uint8_t * dst, size_t num)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < num; i++)
|
||||
{
|
||||
*dst++ = (uint8_t)rand();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
140
nrf52840/main.c
140
nrf52840/main.c
@ -50,140 +50,52 @@
|
||||
#define DEBUG
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nrf.h"
|
||||
#include "nrf_gpio.h"
|
||||
#include "nrf_delay.h"
|
||||
#include "nrf_drv_clock.h"
|
||||
#include "nrf_drv_power.h"
|
||||
#include "nrf_strerror.h"
|
||||
#include "nrf_drv_rtc.h"
|
||||
#include "nrf_drv_clock.h"
|
||||
|
||||
#include "SEGGER_RTT.h"
|
||||
#include "boards.h"
|
||||
|
||||
#include "usb.h"
|
||||
|
||||
|
||||
#define COMPARE_COUNTERTIME (3UL) /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
|
||||
|
||||
#ifdef BSP_LED_0
|
||||
#define TICK_EVENT_OUTPUT BSP_LED_0 /**< Pin number for indicating tick event. */
|
||||
#endif
|
||||
#ifndef TICK_EVENT_OUTPUT
|
||||
#error "Please indicate output pin"
|
||||
#endif
|
||||
#ifdef BSP_LED_1
|
||||
#define COMPARE_EVENT_OUTPUT BSP_LED_1 /**< Pin number for indicating compare event. */
|
||||
#endif
|
||||
#ifndef COMPARE_EVENT_OUTPUT
|
||||
#error "Please indicate output pin"
|
||||
#endif
|
||||
|
||||
const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
|
||||
|
||||
#define printf(fmt,...) SEGGER_RTT_printf(0, fmt, ##__VA_ARGS__);
|
||||
|
||||
|
||||
/*lint -save -e14 */
|
||||
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
|
||||
{
|
||||
error_info_t * e = (error_info_t *)info;
|
||||
SEGGER_RTT_printf(0, "Error: %d: %s at %d:%s\n",e->err_code, nrf_strerror_get(e->err_code), e->line_num, e->p_file_name);
|
||||
while(1)
|
||||
;
|
||||
}
|
||||
|
||||
void log_resetreason(void);
|
||||
|
||||
|
||||
|
||||
static void init_power_clock(void)
|
||||
{
|
||||
ret_code_t ret;
|
||||
/* Initializing power and clock */
|
||||
ret = nrf_drv_clock_init();
|
||||
APP_ERROR_CHECK(ret);
|
||||
ret = nrf_drv_power_init(NULL);
|
||||
APP_ERROR_CHECK(ret);
|
||||
nrf_drv_clock_hfclk_request(NULL);
|
||||
nrf_drv_clock_lfclk_request(NULL);
|
||||
while (!(nrf_drv_clock_hfclk_is_running() &&
|
||||
nrf_drv_clock_lfclk_is_running()))
|
||||
{
|
||||
/* Just waiting */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void rtc_config(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
//Initialize RTC instance
|
||||
nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
|
||||
config.prescaler = 32;
|
||||
err_code = nrf_drv_rtc_init(&rtc, &config, NULL);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
//Enable tick event & interrupt
|
||||
/*nrf_drv_rtc_tick_enable(&rtc,true);*/
|
||||
|
||||
/*//Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds*/
|
||||
/*err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);*/
|
||||
/*APP_ERROR_CHECK(err_code);*/
|
||||
|
||||
//Power on RTC instance
|
||||
nrf_drv_rtc_enable(&rtc);
|
||||
}
|
||||
#include "device.h"
|
||||
#include "time.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* Configure board. */
|
||||
/*bsp_board_init(BSP_INIT_LEDS);*/
|
||||
/*lfclk_config();*/
|
||||
/*SEGGER_RTT_printf(0, "Hello FIDO2\n");*/
|
||||
uint8_t hidmsg[64];
|
||||
uint32_t count = 0;
|
||||
uint32_t beat = 0;
|
||||
uint32_t t1 = 0;
|
||||
|
||||
uint32_t count;
|
||||
int i = 0;
|
||||
|
||||
printf("hello FIDO2\r\n");
|
||||
log_resetreason();
|
||||
if ((nrf_power_resetreas_get() & NRF_POWER_RESETREAS_RESETPIN_MASK) == 0)
|
||||
device_init();
|
||||
|
||||
while(1)
|
||||
{
|
||||
// Hard reset. this is for engineering A sample to work for USB ...
|
||||
nrf_power_resetreas_clear(nrf_power_resetreas_get());
|
||||
nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0,31));
|
||||
nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0,31));
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
nrf_power_resetreas_clear(nrf_power_resetreas_get());
|
||||
|
||||
|
||||
init_power_clock();
|
||||
rtc_config();
|
||||
usb_init();
|
||||
|
||||
while (1)
|
||||
if (millis() - t1 > 1000)
|
||||
{
|
||||
i++;
|
||||
for (int i = 0; i < LEDS_NUMBER; i++)
|
||||
{
|
||||
bsp_board_led_invert(i);
|
||||
nrf_delay_ms(25);
|
||||
printf("heartbeat %ld\n", beat++);
|
||||
t1 = millis();
|
||||
}
|
||||
count = nrf_drv_rtc_counter_get(&rtc);
|
||||
|
||||
printf("toggle\r\n");
|
||||
SEGGER_RTT_SetTerminal(0);
|
||||
SEGGER_RTT_printf(0, "Hello World %d!\n", count);
|
||||
SEGGER_RTT_SetTerminal(1);
|
||||
SEGGER_RTT_printf(0, "Hello World %d!\n", i);
|
||||
if (usbhid_recv(hidmsg) > 0)
|
||||
{
|
||||
printf("%d>> ",count++); dump_hex(hidmsg,sizeof(hidmsg));
|
||||
|
||||
/*ctaphid_handle_packet(hidmsg);*/
|
||||
memset(hidmsg, 0, sizeof(hidmsg));
|
||||
}
|
||||
|
||||
/*u2f_hid_check_timeouts();*/
|
||||
|
||||
main_loop_delay();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
73
nrf52840/retarget.c
Normal file
73
nrf52840/retarget.c
Normal file
@ -0,0 +1,73 @@
|
||||
#include "nrf.h"
|
||||
#include "SEGGER_RTT.h"
|
||||
|
||||
int _SEGGER_TERM = 0;
|
||||
|
||||
void set_logging_tag(uint32_t tag)
|
||||
{
|
||||
int term = 0;
|
||||
while (tag)
|
||||
{
|
||||
if (tag & 1)
|
||||
break;
|
||||
term++;
|
||||
tag = tag >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__CC_ARM)
|
||||
int fgetc(FILE * p_file)
|
||||
{
|
||||
return '0';
|
||||
}
|
||||
|
||||
int fputc(int ch, FILE * p_file)
|
||||
{
|
||||
SEGGER_RTT_PutChar(_SEGGER_TERM, ch);
|
||||
return ch;
|
||||
}
|
||||
|
||||
#elif defined(__GNUC__) && defined(__SES_ARM)
|
||||
|
||||
int __getchar(FILE * p_file)
|
||||
{
|
||||
return '0';
|
||||
}
|
||||
|
||||
int __putchar(int ch, FILE * p_file)
|
||||
{
|
||||
SEGGER_RTT_PutChar(_SEGGER_TERM, ch);
|
||||
return ch;
|
||||
}
|
||||
#elif defined(__GNUC__) && !defined(__SES_ARM)
|
||||
|
||||
int _write(int file, const char * p_char, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
UNUSED_PARAMETER(file);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
SEGGER_RTT_PutChar(_SEGGER_TERM, *p_char++);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int _read(int file, char * p_char, int len)
|
||||
{
|
||||
*p_char = '0';
|
||||
/*UNUSED_PARAMETER(file);*/
|
||||
/*while (app_uart_get((uint8_t *)p_char) == NRF_ERROR_NOT_FOUND)*/
|
||||
/*{*/
|
||||
/*// No implementation needed.*/
|
||||
/*}*/
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else
|
||||
/*#elif defined(__ICCARM__)*/
|
||||
#error "No read/write for printing implemented for compiler"
|
||||
#endif
|
||||
|
@ -6844,7 +6844,7 @@
|
||||
// <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module
|
||||
//==========================================================
|
||||
#ifndef NRF_QUEUE_ENABLED
|
||||
#define NRF_QUEUE_ENABLED 1
|
||||
#define NRF_QUEUE_ENABLED 0
|
||||
#endif
|
||||
// <q> NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module
|
||||
|
||||
|
@ -52,12 +52,13 @@
|
||||
#include "nrf_log_default_backends.h"
|
||||
#include "app_timer.h"
|
||||
#include "app_error.h"
|
||||
#include "app_fifo.h"
|
||||
#include "bsp.h"
|
||||
#include "bsp_cli.h"
|
||||
#include "nrf_cli.h"
|
||||
|
||||
#include "SEGGER_RTT.h"
|
||||
#define printf(fmt,...) SEGGER_RTT_printf(0, fmt, ##__VA_ARGS__);
|
||||
#include "util.h"
|
||||
#include "usb.h"
|
||||
|
||||
#define BTN_DATA_SEND 0
|
||||
#define BTN_DATA_KEY_RELEASE (bsp_event_t)(BSP_EVENT_KEY_LAST + 1)
|
||||
@ -805,20 +806,12 @@ static void usbd_setup_SetProtocol(
|
||||
|
||||
/** @} */ /* End of processing setup requests functions */
|
||||
|
||||
void dump_hex(uint8_t * buf, int size)
|
||||
{
|
||||
while(size--)
|
||||
{
|
||||
printf("%02x ", *buf++);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
|
||||
{
|
||||
static uint8_t buf[64];
|
||||
uint32_t size;
|
||||
nrf_drv_usbd_transfer_t transfer;
|
||||
memset(&transfer, 0, sizeof(nrf_drv_usbd_transfer_t));
|
||||
|
||||
@ -861,7 +854,14 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
|
||||
{
|
||||
case NRF_USBD_EP_OK:
|
||||
/*printf("NRF_USBD_EP_OK\n");*/
|
||||
printf(">> ");dump_hex(buf,64);
|
||||
/*printf(">> "); dump_hex(buf,64);*/
|
||||
size = 64;
|
||||
app_fifo_write(&USBHID_RECV_FIFO, buf, &size);
|
||||
if (size != 64)
|
||||
{
|
||||
printf("Error, USB FIFO is full\n");
|
||||
APP_ERROR_CHECK(NRF_ERROR_NO_MEM);
|
||||
}
|
||||
break;
|
||||
case NRF_USBD_EP_WAITING:
|
||||
/*printf("NRF_USBD_EP_WAITING\n");*/
|
||||
@ -1038,54 +1038,6 @@ static void power_usb_event_handler(nrf_drv_power_usb_evt_t event)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void log_resetreason(void)
|
||||
{
|
||||
/* Reset reason */
|
||||
uint32_t rr = nrf_power_resetreas_get();
|
||||
printf("Reset reasons:\n");
|
||||
if (0 == rr)
|
||||
{
|
||||
printf("- NONE\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_RESETPIN_MASK))
|
||||
{
|
||||
printf("- RESETPIN\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_DOG_MASK ))
|
||||
{
|
||||
printf("- DOG\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_SREQ_MASK ))
|
||||
{
|
||||
printf("- SREQ\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_LOCKUP_MASK ))
|
||||
{
|
||||
printf("- LOCKUP\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_OFF_MASK ))
|
||||
{
|
||||
printf("- OFF\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_LPCOMP_MASK ))
|
||||
{
|
||||
printf("- LPCOMP\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_DIF_MASK ))
|
||||
{
|
||||
printf("- DIF\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_NFC_MASK ))
|
||||
{
|
||||
printf("- NFC\n");
|
||||
}
|
||||
if (0 != (rr & NRF_POWER_RESETREAS_VBUS_MASK ))
|
||||
{
|
||||
printf("- VBUS\n");
|
||||
}
|
||||
}
|
||||
|
||||
void usb_init(void)
|
||||
{
|
||||
ret_code_t ret;
|
||||
|
@ -1,6 +1,10 @@
|
||||
#ifndef _USB_H
|
||||
#define _USB_H
|
||||
|
||||
#include "app_fifo.h"
|
||||
|
||||
void usb_init(void);
|
||||
|
||||
extern app_fifo_t USBHID_RECV_FIFO;
|
||||
|
||||
#endif
|
||||
|
1
nrf52840/usbhid.c
Normal file
1
nrf52840/usbhid.c
Normal file
@ -0,0 +1 @@
|
||||
|
56
stubs.c
Normal file
56
stubs.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include "device.h"
|
||||
#include "util.h"
|
||||
#include "ctap.h"
|
||||
#include "u2f.h"
|
||||
|
||||
#if defined(STUB_CTAPHID) || defined(STUB_CTAP)
|
||||
|
||||
|
||||
|
||||
void ctap_init()
|
||||
{
|
||||
printf("STUB: ctap_init\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(STUB_CTAPHID)
|
||||
void ctaphid_init()
|
||||
{
|
||||
printf("STUB: ctaphid_init\n");
|
||||
}
|
||||
void ctaphid_handle_packet(uint8_t * hidmsg)
|
||||
{
|
||||
printf("STUB: ctaphid_handle_packet\n");
|
||||
}
|
||||
|
||||
void ctaphid_check_timeouts()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef STUB_CTAP
|
||||
|
||||
void ctap_reset_state()
|
||||
{
|
||||
printf("STUB: ctap_reset_state\n");
|
||||
}
|
||||
|
||||
void ctap_response_init(CTAP_RESPONSE * resp)
|
||||
{
|
||||
}
|
||||
|
||||
void u2f_request(struct u2f_request_apdu* req, CTAP_RESPONSE * resp)
|
||||
{
|
||||
printf("STUB: u2f_request\n");
|
||||
}
|
||||
|
||||
uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp)
|
||||
{
|
||||
printf("STUB: ctap_request\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
10
time.c
10
time.c
@ -1,10 +0,0 @@
|
||||
#include <sys/time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
uint64_t millis()
|
||||
{
|
||||
struct timeval te;
|
||||
gettimeofday(&te, NULL); // get current time
|
||||
uint64_t milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
|
||||
return milliseconds;
|
||||
}
|
97
udp_bridge.c
97
udp_bridge.c
@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Used as a bridge for USBHID protocol for FIDO 2.0 and U2F to ease firmware development and testing.
|
||||
*
|
||||
* Client FIDO 2.0, U2F software should bind to UDP port 7112 to send/recv USBHID messages from.
|
||||
*
|
||||
* */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
int udp_server()
|
||||
{
|
||||
int fd;
|
||||
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
|
||||
perror( "socket failed" );
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct timeval read_timeout;
|
||||
read_timeout.tv_sec = 0;
|
||||
read_timeout.tv_usec = 10;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof(struct timeval)) != 0)
|
||||
{
|
||||
perror( "setsockopt" );
|
||||
exit(1);
|
||||
}
|
||||
/*fcntl(fd, F_SETFL, fcntl(fd,F_GETFL, 0)|O_NONBLOCK);*/
|
||||
|
||||
struct sockaddr_in serveraddr;
|
||||
memset( &serveraddr, 0, sizeof(serveraddr) );
|
||||
serveraddr.sin_family = AF_INET;
|
||||
serveraddr.sin_port = htons( 8111 );
|
||||
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
|
||||
|
||||
if ( bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
|
||||
perror( "bind failed" );
|
||||
exit(1);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
int udp_recv(int fd, uint8_t * buf, int size)
|
||||
{
|
||||
|
||||
fd_set input;
|
||||
FD_ZERO(&input);
|
||||
FD_SET(fd, &input);
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 100000;
|
||||
int n = select(fd + 1, &input, NULL, NULL, &timeout);
|
||||
if (n == -1) {
|
||||
perror("select\n");
|
||||
exit(1);
|
||||
} else if (n == 0)
|
||||
return 0;
|
||||
if (!FD_ISSET(fd, &input))
|
||||
{
|
||||
|
||||
}
|
||||
int length = recvfrom( fd, buf, size, 0, NULL, 0 );
|
||||
if ( length < 0 ) {
|
||||
perror( "recvfrom failed" );
|
||||
exit(1);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
void udp_send(int fd, uint8_t * buf, int size)
|
||||
{
|
||||
struct sockaddr_in serveraddr;
|
||||
memset( &serveraddr, 0, sizeof(serveraddr) );
|
||||
serveraddr.sin_family = AF_INET;
|
||||
serveraddr.sin_port = htons( 7112 );
|
||||
serveraddr.sin_addr.s_addr = htonl( 0x7f000001 ); // (127.0.0.1)
|
||||
|
||||
if (sendto( fd, buf, size, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
|
||||
perror( "sendto failed" );
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void udp_close(int fd)
|
||||
{
|
||||
close(fd);
|
||||
}
|
||||
|
||||
|
14
udp_bridge.h
14
udp_bridge.h
@ -1,14 +0,0 @@
|
||||
#ifndef _UDP_BRIDGE_H
|
||||
#define _UDP_BRIDGE_H
|
||||
|
||||
int udp_server();
|
||||
|
||||
// Recv from anyone
|
||||
int udp_recv(int fd, uint8_t * buf, int size);
|
||||
|
||||
// Send to 127.0.0.1:7112
|
||||
void udp_send(int fd, uint8_t * buf, int size);
|
||||
|
||||
void udp_close(int fd);
|
||||
|
||||
#endif
|
40
usbhid.c
40
usbhid.c
@ -1,40 +0,0 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "usbhid.h"
|
||||
#include "udp_bridge.h"
|
||||
|
||||
|
||||
static int serverfd = 0;
|
||||
|
||||
void usbhid_init()
|
||||
{
|
||||
// just bridge to UDP for now for pure software testing
|
||||
serverfd = udp_server();
|
||||
}
|
||||
|
||||
// Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing
|
||||
int usbhid_recv(uint8_t * msg)
|
||||
{
|
||||
int l = udp_recv(serverfd, msg, HID_MESSAGE_SIZE);
|
||||
/*if (l && l != HID_MESSAGE_SIZE)*/
|
||||
/*{*/
|
||||
/*printf("Error, recv'd message of wrong size %d", l);*/
|
||||
/*exit(1);*/
|
||||
/*}*/
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
// Send 64 byte USB HID message
|
||||
void usbhid_send(uint8_t * msg)
|
||||
{
|
||||
udp_send(serverfd, msg, HID_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
void usbhid_close()
|
||||
{
|
||||
udp_close(serverfd);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user