add initial framework

This commit is contained in:
Conor Patrick 2018-04-28 13:40:28 -04:00
parent 0d0eff153e
commit 999ea34634
9 changed files with 212 additions and 0 deletions

2
.gitignore vendored
View File

@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
*.swp

10
Makefile Normal file
View File

@ -0,0 +1,10 @@
src = $(wildcard *.c)
obj = $(src:.c=.o)
LDFLAGS = -Wl,--gc-sections ./tinycbor/lib/libtinycbor.a
CFLAGS = -O2 -fdata-sections -ffunction-sections -I./tinycbor/src
name = main
$(name): $(obj)
$(CC) $(LDFLAGS) -o $@ $(obj) $(LDFLAGS)

51
main.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "cbor.h"
#include "usbhid.h"
#include "util.h"
void check_ret(CborError ret)
{
if (ret != CborNoError)
{
printf("CborError: %d\n", ret);
exit(1);
}
}
int main(int argc, char * argv[])
{
CborError ret;
uint8_t buf[16];
memset(buf,0,sizeof(buf));
CborEncoder encoder;
cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
ret = cbor_encode_int(&encoder, 55);
check_ret(ret);
dump_hex(buf,sizeof(buf));
printf("init usbhid\n");
usbhid_init();
uint8_t hidmsg[64];
memset(hidmsg,0,sizeof(hidmsg));
printf("recv'ing hid msg \n");
while(1)
{
usbhid_recv(hidmsg);
printf(">> "); dump_hex(hidmsg,sizeof(hidmsg));
printf("<< "); dump_hex(hidmsg,sizeof(hidmsg));
usbhid_send(hidmsg);
}
usbhid_close();
printf("done\n");
return 0;
}

67
udp_bridge.c Normal file
View File

@ -0,0 +1,67 @@
/*
* 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 <netinet/in.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int udp_server()
{
int fd;
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror( "socket failed" );
return 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;
}
void udp_recv(int fd, uint8_t * buf, int size)
{
int length = recvfrom( fd, buf, size, 0, NULL, 0 );
if ( length < 0 ) {
perror( "recvfrom failed" );
exit(1);
}
}
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 Normal file
View File

@ -0,0 +1,14 @@
#ifndef _UDP_BRIDGE_H
#define _UDP_BRIDGE_H
int udp_server();
// Recv from anyone
void 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

33
usbhid.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdint.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
void usbhid_recv(uint8_t * msg)
{
udp_recv(serverfd, msg, HID_MESSAGE_SIZE);
}
// 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);
}

16
usbhid.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _USBHID_H
#define _USBHID_H
// HID message size in bytes
#define HID_MESSAGE_SIZE 64
void usbhid_init();
void usbhid_recv(uint8_t * msg);
void usbhid_send(uint8_t * msg);
void usbhid_close();
#endif

12
util.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
void dump_hex(uint8_t * buf, int size)
{
while(size--)
{
printf("%02x ", *buf++);
}
printf("\n");
}

7
util.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _UTIL_H
#define _UTIL_H
void dump_hex(uint8_t * buf, int size);
#endif