From 7a12eea133850100891527edf94807ea123e841e Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 2 Jun 2018 14:44:12 -0400 Subject: [PATCH] major refactoring/simplifying --- ctap.c | 2 +- ctap.h | 2 +- ctap_device.c | 69 ----------- ctaphid.c | 14 +-- ctaphid.h | 4 +- device.c | 211 ++++++++++++++++++++++++++++++++++ usbhid.h => device.h | 11 +- log.c | 7 ++ log.h | 1 + main.c | 40 ++++--- nrf52840/Makefile | 15 ++- nrf52840/app_config.h | 3 + nrf52840/device.c | 259 ++++++++++++++++++++++++++++++++++++++++++ nrf52840/main.c | 140 +++++------------------ nrf52840/retarget.c | 73 ++++++++++++ nrf52840/sdk_config.h | 2 +- nrf52840/usb.c | 72 ++---------- nrf52840/usb.h | 4 + nrf52840/usbhid.c | 1 + stubs.c | 56 +++++++++ time.c | 10 -- time.h | 6 - udp_bridge.c | 97 ---------------- udp_bridge.h | 14 --- usbhid.c | 40 ------- util.h | 5 + 26 files changed, 714 insertions(+), 444 deletions(-) delete mode 100644 ctap_device.c create mode 100644 device.c rename usbhid.h => device.h (60%) create mode 100644 nrf52840/device.c create mode 100644 nrf52840/retarget.c create mode 100644 nrf52840/usbhid.c create mode 100644 stubs.c delete mode 100644 time.c delete mode 100644 time.h delete mode 100644 udp_bridge.c delete mode 100644 udp_bridge.h delete mode 100644 usbhid.c diff --git a/ctap.c b/ctap.c index 6bdfd77..3f980c1 100644 --- a/ctap.c +++ b/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; diff --git a/ctap.h b/ctap.h index dd3ba91..9fd4c73 100644 --- a/ctap.h +++ b/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 diff --git a/ctap_device.c b/ctap_device.c deleted file mode 100644 index 070ce95..0000000 --- a/ctap_device.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Device specific functionality defined here - */ - -#include -#include -#include - -#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; -} - - - - - diff --git a/ctaphid.c b/ctaphid.c index b056d67..835006b 100644 --- a/ctaphid.c +++ b/ctaphid.c @@ -1,8 +1,8 @@ -#include #include #include #include +#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; diff --git a/ctaphid.h b/ctaphid.h index 4ecd051..02c934c 100644 --- a/ctaphid.h +++ b/ctaphid.h @@ -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)) diff --git a/device.c b/device.c new file mode 100644 index 0000000..fac0168 --- /dev/null +++ b/device.c @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} + + + + + diff --git a/usbhid.h b/device.h similarity index 60% rename from usbhid.h rename to device.h index a0b5956..cb1e2d7 100644 --- a/usbhid.h +++ b/device.h @@ -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 diff --git a/log.c b/log.c index 4c26e60..cf4c485 100644 --- a/log.c +++ b/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) { diff --git a/log.h b/log.h index 6be59aa..8435ebf 100644 --- a/log.h +++ b/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 { diff --git a/main.c b/main.c index 81c0ed3..a767e03 100644 --- a/main.c +++ b/main.c @@ -3,25 +3,21 @@ #include #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 diff --git a/nrf52840/Makefile b/nrf52840/Makefile index 81b0711..28b09f6 100644 --- a/nrf52840/Makefile +++ b/nrf52840/Makefile @@ -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 diff --git a/nrf52840/app_config.h b/nrf52840/app_config.h index ecba132..786d1d6 100644 --- a/nrf52840/app_config.h +++ b/nrf52840/app_config.h @@ -37,6 +37,9 @@ #ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE #define SEGGER_RTT_CONFIG_DEFAULT_MODE 2 + +#define APP_FIFO_ENABLED 1 + #endif diff --git a/nrf52840/device.c b/nrf52840/device.c new file mode 100644 index 0000000..4d92af4 --- /dev/null +++ b/nrf52840/device.c @@ -0,0 +1,259 @@ +/* + * Device specific functionality here + * */ + +#include +#include +#include + +#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; +} + + diff --git a/nrf52840/main.c b/nrf52840/main.c index 43b82f3..f4c7703 100644 --- a/nrf52840/main.c +++ b/nrf52840/main.c @@ -50,140 +50,52 @@ #define DEBUG #include #include +#include #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) - { - i++; - for (int i = 0; i < LEDS_NUMBER; i++) + if (millis() - t1 > 1000) { - 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(); } + } diff --git a/nrf52840/retarget.c b/nrf52840/retarget.c new file mode 100644 index 0000000..2663db5 --- /dev/null +++ b/nrf52840/retarget.c @@ -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 + diff --git a/nrf52840/sdk_config.h b/nrf52840/sdk_config.h index 933c023..582e594 100644 --- a/nrf52840/sdk_config.h +++ b/nrf52840/sdk_config.h @@ -6844,7 +6844,7 @@ // NRF_QUEUE_ENABLED - nrf_queue - Queue module //========================================================== #ifndef NRF_QUEUE_ENABLED -#define NRF_QUEUE_ENABLED 1 +#define NRF_QUEUE_ENABLED 0 #endif // NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module diff --git a/nrf52840/usb.c b/nrf52840/usb.c index ad2e349..9dc4a9b 100644 --- a/nrf52840/usb.c +++ b/nrf52840/usb.c @@ -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; diff --git a/nrf52840/usb.h b/nrf52840/usb.h index ec74a2c..5c0b3f5 100644 --- a/nrf52840/usb.h +++ b/nrf52840/usb.h @@ -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 diff --git a/nrf52840/usbhid.c b/nrf52840/usbhid.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/nrf52840/usbhid.c @@ -0,0 +1 @@ + diff --git a/stubs.c b/stubs.c new file mode 100644 index 0000000..cdd73f4 --- /dev/null +++ b/stubs.c @@ -0,0 +1,56 @@ +#include +#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 diff --git a/time.c b/time.c deleted file mode 100644 index a77e4bd..0000000 --- a/time.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -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; -} diff --git a/time.h b/time.h deleted file mode 100644 index 41b45e4..0000000 --- a/time.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _TIME_H -#define _TIME_H - -uint64_t millis(); - -#endif diff --git a/udp_bridge.c b/udp_bridge.c deleted file mode 100644 index 4d7a949..0000000 --- a/udp_bridge.c +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include - - -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); -} - - diff --git a/udp_bridge.h b/udp_bridge.h deleted file mode 100644 index 448f2ac..0000000 --- a/udp_bridge.h +++ /dev/null @@ -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 diff --git a/usbhid.c b/usbhid.c deleted file mode 100644 index e5f561b..0000000 --- a/usbhid.c +++ /dev/null @@ -1,40 +0,0 @@ - -#include -#include -#include - -#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); -} diff --git a/util.h b/util.h index d347e5a..9f010f3 100644 --- a/util.h +++ b/util.h @@ -4,7 +4,12 @@ void dump_hex(uint8_t * buf, int size); +#ifndef MIN #define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef MAX #define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#endif #endif