Compare commits

...

28 Commits
main ... dev

Author SHA1 Message Date
qiurui dda746410c add close socket 2024-05-13 15:01:34 +08:00
qiurui 5b69abe0bc add SO_REUSEADDR 2024-05-13 14:53:26 +08:00
qiurui e6a27c75b9 fix aarch64 build 2024-05-13 09:52:07 +08:00
qiurui 646ac54e74 fix aarch64 build 2024-05-13 01:43:53 +08:00
qiurui 0b73e8b0f3 add tcp establish check 2024-05-13 01:27:53 +08:00
qiurui b5051b1b25 buffer not bzero problem 2024-04-13 16:10:09 +08:00
qiurui f02d4681ce buffer not bzero problem 2024-04-13 15:51:50 +08:00
qiurui 1e35c147c3 remove python 2024-04-13 00:15:11 +08:00
qiurui f5b28ba3ba add open UDC 2024-04-13 00:11:06 +08:00
qiurui 8fa5fd620c fix client problem and update train txt 2024-04-13 00:10:09 +08:00
qiurui 51746e2962 fix client problem and update train txt 2024-04-13 00:09:47 +08:00
qiurui 4dcb7e38b4 update service file 2024-04-13 00:03:31 +08:00
qiurui 0098709cf5 fix service problem 2024-04-12 14:40:25 +08:00
qiurui 218080b2bb change service to alywas 2024-04-12 13:47:49 +08:00
qiurui e10b0380dc change service to hid_gadget_test_select 2024-04-12 13:40:51 +08:00
qiurui a919ad455b add C server 2024-04-12 13:39:00 +08:00
qiurui be85e0aef1 add sig and update file.txt 2024-04-12 10:16:00 +08:00
qiurui b9abda9c68 add number 2024-04-12 07:46:26 +08:00
qiurui 3b36c226f7 add socket plugin test 2024-04-11 22:11:31 +08:00
qiurui e599114267 add hid-gadget test 2024-04-11 22:10:28 +08:00
qiurui d2e9ff74b4 add C test code and C client runtime 2024-04-11 22:09:31 +08:00
qiurui 779596f308 fix problems 2024-04-10 22:20:14 +08:00
qiurui 8653de3af3 commit for test 2024-04-10 20:37:54 +08:00
qiurui 992f38cc35 commit for test 2024-04-10 20:37:37 +08:00
qiurui ea3e3dcbea add catch expect and sys.exit(1) 2024-04-10 20:32:39 +08:00
qiurui f4c73b84d8 change i range 2024-04-10 17:50:01 +08:00
qiurui 2660d7bad9 add test code 2024-04-10 17:39:13 +08:00
qiurui f7d0bcd4ff test for flush() 2024-04-10 17:38:22 +08:00
17 changed files with 1660 additions and 49 deletions

View File

@ -1,16 +1,15 @@
[Unit]
Description=keyboard-hook
After=network-online.target keyboard-hook.service
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
Restart=on-failure
Restart=always
RestartSec=5s
ExecStart=/usr/bin/python /usr/bin/server.py
StartLimitBurst=0
ExecStart=/usr/bin/hid_gadget_test_select /dev/hidg0 keyboard > /dev/null 2>&1
[Install]
WantedBy=multi-user.target

BIN
test/C/client Executable file

Binary file not shown.

89
test/C/client-test.c Executable file
View File

@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error( const char *msg )
{
perror( msg );
exit( 0 );
}
int main( int argc, char *argv[] )
{
int sockfd, portno;
ssize_t n;
struct sockaddr_in serv_addr;
struct hostent *server;
const size_t buffer_length = 256;
char buffer[ buffer_length ];
if( argc < 3 )
{
fprintf( stderr, "usage %s hostname port\n", argv[ 0 ] );
exit( 0 );
}
portno = atoi( argv[ 2 ] );
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
if( sockfd < 0 )
{
error("ERROR opening socket");
}
server = gethostbyname( argv[ 1 ] );
if( server == NULL )
{
fprintf( stderr, "ERROR, no such host\n" );
exit( 0 );
}
bzero( (char *)&serv_addr, sizeof( serv_addr ) );
serv_addr.sin_family = AF_INET;
bcopy( (char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, (size_t)server->h_length );
serv_addr.sin_port = htons(portno);
if( connect( sockfd, (struct sockaddr *)&serv_addr, sizeof( serv_addr ) ) < 0 )
{
error("ERROR connecting");
}
FILE *fp;
while(1)
{
fp = fopen("file.txt" , "r");
if(fp == NULL) {
perror("打开文件时发生错误");
return(-1);
}
bzero( buffer, buffer_length );
int i = 0;
while (1)
{
if (fgets(buffer,buffer_length - 1, fp) == NULL) break;
i++;
char string[255];
sprintf(string , "%s\n", buffer);
printf("%3d: %s\n", i, string);
n = write( sockfd, buffer, strlen( buffer ) );
if( n < 0 )
{
error("ERROR writing to socket");
}
bzero( buffer, buffer_length );
n = read( sockfd, buffer, buffer_length - 1 );
if( n < 0 )
{
error( "ERROR reading from socket" );
}
sleep(1);
}
fclose(fp);
}
close( sockfd );
return 0;
}

13
test/C/file-linux.txt Executable file
View File

@ -0,0 +1,13 @@
a b c d e
a b c d --left-shift --kp-enter
--1 --2 --3 --4 --5
--6 --7 --8 --9 --0
--1 --2 --3 --4 --5 --left-shift
--6 --7 --8 --9 --0 --left-shift
--kp-enter
--- --= --[ --] --fx
--; --' --` --, --. --/
--- --= --[ --] --fx --left-shift
--; --' --` --, --. --/ --left-shift
--kp-enter
--1 --2 --3 --4 --left-shift --kp-enter

29
test/C/file-win.txt Executable file
View File

@ -0,0 +1,29 @@
--kp-enter
a
b
c
d
e
f
g
h
i
j
k --left-shift
--1
--2
--3
--4
--5
--6
--7
--8
--9
--0 --left-shift
--kp-enter
--left-ctrl a
--del
--left-alt q
f
n --left-shift
--kp-enter

15
test/C/file.txt Normal file
View File

@ -0,0 +1,15 @@
--kp-enter
a b c d e
a b c d --left-shift --kp-enter
--1 --2 --3 --4 --5
--6 --7 --8 --9 --0
--1 --2 --3 --4 --5 --left-shift
--6 --7 --8 --9 --0 --left-shift
--kp-enter
--- --= --[ --] --fx
--; --' --` --, --. --/
--- --= --[ --] --fx --left-shift
--; --' --` --, --. --/ --left-shift
--kp-enter
--left-ctrl a
--del

View File

@ -0,0 +1,454 @@
/* hid_gadget_test */
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 1024
struct options {
const char *opt;
unsigned char val;
};
static struct options kmod[] = {
{.opt = "--left-ctrl", .val = 0x01},
{.opt = "--right-ctrl", .val = 0x10},
{.opt = "--left-shift", .val = 0x02},
{.opt = "--right-shift", .val = 0x20},
{.opt = "--left-alt", .val = 0x04},
{.opt = "--right-alt", .val = 0x40},
{.opt = "--left-meta", .val = 0x08},
{.opt = "--right-meta", .val = 0x80},
{.opt = NULL}
};
static struct options kval[] = {
{.opt = "--return", .val = 0x28},
{.opt = "--esc", .val = 0x29},
{.opt = "--bckspc", .val = 0x2a},
{.opt = "--tab", .val = 0x2b},
{.opt = "--spacebar", .val = 0x2c},
{.opt = "---", .val = 0x2d},
{.opt = "--=", .val = 0x2e},
{.opt = "--[", .val = 0x2f},
{.opt = "--]", .val = 0x30},
{.opt = "--fx", .val = 0x31},
{.opt = "--;", .val = 0x33},
{.opt = "--'", .val = 0x34},
{.opt = "--`", .val = 0x35},
{.opt = "--,", .val = 0x36},
{.opt = "--.", .val = 0x37},
{.opt = "--/", .val = 0x38},
{.opt = "--caps-lock", .val = 0x39},
{.opt = "--f1", .val = 0x3a},
{.opt = "--f2", .val = 0x3b},
{.opt = "--f3", .val = 0x3c},
{.opt = "--f4", .val = 0x3d},
{.opt = "--f5", .val = 0x3e},
{.opt = "--f6", .val = 0x3f},
{.opt = "--f7", .val = 0x40},
{.opt = "--f8", .val = 0x41},
{.opt = "--f9", .val = 0x42},
{.opt = "--f10", .val = 0x43},
{.opt = "--f11", .val = 0x44},
{.opt = "--f12", .val = 0x45},
{.opt = "--insert", .val = 0x49},
{.opt = "--home", .val = 0x4a},
{.opt = "--pageup", .val = 0x4b},
{.opt = "--del", .val = 0x4c},
{.opt = "--end", .val = 0x4d},
{.opt = "--pagedown", .val = 0x4e},
{.opt = "--right", .val = 0x4f},
{.opt = "--left", .val = 0x50},
{.opt = "--down", .val = 0x51},
{.opt = "--kp-enter", .val = 0x58},
{.opt = "--up", .val = 0x52},
{.opt = "--num-lock", .val = 0x53},
{.opt = "--1", .val = 0x1e},
{.opt = "--2", .val = 0x1f},
{.opt = "--3", .val = 0x20},
{.opt = "--4", .val = 0x21},
{.opt = "--5", .val = 0x22},
{.opt = "--6", .val = 0x23},
{.opt = "--7", .val = 0x24},
{.opt = "--8", .val = 0x25},
{.opt = "--9", .val = 0x26},
{.opt = "--0", .val = 0x27},
{.opt = NULL}
};
int keyboard_fill_report(char report[8], char buf[SIZE], int *hold)
{
char *tok = strtok(buf, " ");
int key = 0;
int i = 0;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
if (strcmp(tok, "--hold") == 0) {
*hold = 1;
continue;
}
if (key < 6) {
for (i = 0; kval[i].opt != NULL; i++)
if (strcmp(tok, kval[i].opt) == 0) {
report[2 + key++] = kval[i].val;
break;
}
if (kval[i].opt != NULL)
continue;
}
if (key < 6)
if (islower(tok[0])) {
report[2 + key++] = (tok[0] - ('a' - 0x04));
continue;
}
for (i = 0; kmod[i].opt != NULL; i++)
if (strcmp(tok, kmod[i].opt) == 0) {
report[0] = report[0] | kmod[i].val;
break;
}
if (kmod[i].opt != NULL)
continue;
if (key < 6)
fprintf(stderr, "unknown option: %s\n", tok);
}
return 8;
}
static struct options mmod[] = {
{.opt = "--b1", .val = 0x01},
{.opt = "--b2", .val = 0x02},
{.opt = "--b3", .val = 0x04},
{.opt = NULL}
};
int mouse_fill_report(char report[8], char buf[SIZE], int *hold)
{
char *tok = strtok(buf, " ");
int mvt = 0;
int i = 0;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
if (strcmp(tok, "--hold") == 0) {
*hold = 1;
continue;
}
for (i = 0; mmod[i].opt != NULL; i++)
if (strcmp(tok, mmod[i].opt) == 0) {
report[0] = report[0] | mmod[i].val;
break;
}
if (mmod[i].opt != NULL)
continue;
if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
errno = 0;
report[1 + mvt++] = (char)strtol(tok, NULL, 0);
if (errno != 0) {
fprintf(stderr, "Bad value:'%s'\n", tok);
report[1 + mvt--] = 0;
}
continue;
}
fprintf(stderr, "unknown option: %s\n", tok);
}
return 3;
}
static struct options jmod[] = {
{.opt = "--b1", .val = 0x10},
{.opt = "--b2", .val = 0x20},
{.opt = "--b3", .val = 0x40},
{.opt = "--b4", .val = 0x80},
{.opt = "--hat1", .val = 0x00},
{.opt = "--hat2", .val = 0x01},
{.opt = "--hat3", .val = 0x02},
{.opt = "--hat4", .val = 0x03},
{.opt = "--hatneutral", .val = 0x04},
{.opt = NULL}
};
int joystick_fill_report(char report[8], char buf[SIZE], int *hold)
{
char *tok = strtok(buf, " ");
int mvt = 0;
int i = 0;
*hold = 1;
/* set default hat position: neutral */
report[3] = 0x04;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
for (i = 0; jmod[i].opt != NULL; i++)
if (strcmp(tok, jmod[i].opt) == 0) {
report[3] = (report[3] & 0xF0) | jmod[i].val;
break;
}
if (jmod[i].opt != NULL)
continue;
if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
errno = 0;
report[mvt++] = (char)strtol(tok, NULL, 0);
if (errno != 0) {
fprintf(stderr, "Bad value:'%s'\n", tok);
report[mvt--] = 0;
}
continue;
}
fprintf(stderr, "unknown option: %s\n", tok);
}
return 4;
}
void print_options(char c)
{
int i = 0;
if (c == 'k') {
printf(" keyboard options:\n"
" --hold\n");
for (i = 0; kmod[i].opt != NULL; i++)
printf("\t\t%s\n", kmod[i].opt);
printf("\n keyboard values:\n"
" [a-z] or\n");
for (i = 0; kval[i].opt != NULL; i++)
printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
printf("\n");
} else if (c == 'm') {
printf(" mouse options:\n"
" --hold\n");
for (i = 0; mmod[i].opt != NULL; i++)
printf("\t\t%s\n", mmod[i].opt);
printf("\n mouse values:\n"
" Two signed numbers\n"
"--quit to close\n");
} else {
printf(" joystick options:\n");
for (i = 0; jmod[i].opt != NULL; i++)
printf("\t\t%s\n", jmod[i].opt);
printf("\n joystick values:\n"
" three signed numbers\n"
"--quit to close\n");
}
}
int main(int argc, const char *argv[])
{
char *ip = "0.0.0.0";
int port = 50009;
int e;
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
char buffer[SIZE];
const char *filename = NULL;
int fd = 0;
char buf[SIZE];
int cmd_len;
char report[8];
int to_send = 8;
int hold = 0;
fd_set rfds;
int retval, i;
if (argc < 3) {
fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
argv[0]);
return 1;
}
if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
return 2;
filename = argv[1];
server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server_fd<0)
{
perror("[-]Error in socket");
exit(1);
}
printf("[+]Server socket created. \n");
/* Set the option active */
int optval = 1;
socklen_t optlen = sizeof(optval);
if(setsockopt(server_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
perror("[-]Error in TCP_KEEPCNT");
exit(1);
}
printf("SO_KEEPALIVE set on socket\n");
if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, optlen) < 0) {
perror("[-]Error in SO_REUSEADDR");
exit(1);
}
printf("SO_REUSEADDR set on socket\n");
/* Check the status again */
if(getsockopt(server_fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, &optlen) < 0) {
perror("[-]Error in TCP_KEEPCNT");
exit(1);
}
printf("TCP_KEEPIDLE is %d\n", optval );
/* Check the status again */
if(getsockopt(server_fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, &optlen) < 0) {
perror("[-]Error in TCP_KEEPCNT");
exit(1);
}
printf("TCP_KEEPCNT is %d\n", optval);
/* Check the status again */
if(getsockopt(server_fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, &optlen) < 0) {
perror("getsockopt()");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("TCP_KEEPINTVL is %d\n", optval );
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
e = bind(server_fd,(struct sockaddr*)&server_addr, sizeof(server_addr));
if(e<0)
{
perror("[-]Error in Binding");
exit(1);
}
printf("[+]Binding Successfull.\n");
e = listen(server_fd, 10);
if(e==0)
{
printf("[+]Listening...\n");
}
else
{
perror("[-]Error in Binding");
exit(1);
}
socklen_t addr_size = sizeof(client_addr);
client_fd = accept(server_fd,(struct sockaddr*)&client_addr, &addr_size);
if(client_fd < 0){
perror("[-]Error in Accepting");
exit(1);
}else{
printf("[+]Connection accepted\n");
system("ls /sys/class/udc > /sys/kernel/config/usb_gadget/g1/UDC");
}
if ((fd = open(filename, O_RDWR, 0666)) == -1) {
perror(filename);
return 3;
}
print_options(argv[2][0]);
while (42) {
FD_ZERO(&rfds);
FD_SET(client_fd, &rfds);
FD_SET(fd, &rfds);
retval = select(fd + 1, &rfds, NULL, NULL, NULL);
if (retval == -1 && errno == EINTR)
continue;
if (retval < 0) {
perror("select()");
return 4;
}
if (FD_ISSET(fd, &rfds)) {
cmd_len = read(fd, buf, SIZE - 1);
printf("recv report:");
for (i = 0; i < cmd_len; i++)
printf(" %02x", buf[i]);
printf("\n");
}
if (FD_ISSET(client_fd, &rfds)) {
memset(report, 0x0, sizeof(report));
bzero(buf,SIZE);
cmd_len = read(client_fd, buf, SIZE - 1);
if (cmd_len == 0)
break;
if(buf[cmd_len - 1] == '\n')
{
buf[cmd_len - 1] = '\0';
}
fprintf(stdout, "buffer set: %s\n", buf);
fprintf(stdout, "cmd_len set: %d\n", cmd_len);
hold = 0;
memset(report, 0x0, sizeof(report));
const char *message = "receive";
send(client_fd, message, strlen(message), 0);
if (argv[2][0] == 'k')
to_send = keyboard_fill_report(report, buf, &hold);
else if (argv[2][0] == 'm')
to_send = mouse_fill_report(report, buf, &hold);
else
to_send = joystick_fill_report(report, buf, &hold);
if (to_send == -1)
break;
if (write(fd, report, to_send) != to_send) {
perror(filename);
return 5;
}
if (!hold) {
memset(report, 0x0, sizeof(report));
if (write(fd, report, to_send) != to_send) {
perror(filename);
return 6;
}
}
}
}
system("echo > /sys/kernel/config/usb_gadget/g1/UDC");
close(fd);
close(client_fd);
close(server_fd);
return 0;
}

445
test/C/hid_gadget_test_select.c Executable file
View File

@ -0,0 +1,445 @@
/* hid_gadget_test */
//aarch64-linux-gnu-gcc -o usr/bin/hid_gadget_test_select-tcpinfo test/C/hid_gadget_test_select.c
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/wait.h>
#include <pthread.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 1024
struct options {
const char *opt;
unsigned char val;
};
static struct options kmod[] = {
{.opt = "--left-ctrl", .val = 0x01},
{.opt = "--right-ctrl", .val = 0x10},
{.opt = "--left-shift", .val = 0x02},
{.opt = "--right-shift", .val = 0x20},
{.opt = "--left-alt", .val = 0x04},
{.opt = "--right-alt", .val = 0x40},
{.opt = "--left-meta", .val = 0x08},
{.opt = "--right-meta", .val = 0x80},
{.opt = NULL}
};
static struct options kval[] = {
{.opt = "--return", .val = 0x28},
{.opt = "--esc", .val = 0x29},
{.opt = "--bckspc", .val = 0x2a},
{.opt = "--tab", .val = 0x2b},
{.opt = "--spacebar", .val = 0x2c},
{.opt = "---", .val = 0x2d},
{.opt = "--=", .val = 0x2e},
{.opt = "--[", .val = 0x2f},
{.opt = "--]", .val = 0x30},
{.opt = "--fx", .val = 0x31},
{.opt = "--;", .val = 0x33},
{.opt = "--'", .val = 0x34},
{.opt = "--`", .val = 0x35},
{.opt = "--,", .val = 0x36},
{.opt = "--.", .val = 0x37},
{.opt = "--/", .val = 0x38},
{.opt = "--caps-lock", .val = 0x39},
{.opt = "--f1", .val = 0x3a},
{.opt = "--f2", .val = 0x3b},
{.opt = "--f3", .val = 0x3c},
{.opt = "--f4", .val = 0x3d},
{.opt = "--f5", .val = 0x3e},
{.opt = "--f6", .val = 0x3f},
{.opt = "--f7", .val = 0x40},
{.opt = "--f8", .val = 0x41},
{.opt = "--f9", .val = 0x42},
{.opt = "--f10", .val = 0x43},
{.opt = "--f11", .val = 0x44},
{.opt = "--f12", .val = 0x45},
{.opt = "--insert", .val = 0x49},
{.opt = "--home", .val = 0x4a},
{.opt = "--pageup", .val = 0x4b},
{.opt = "--del", .val = 0x4c},
{.opt = "--end", .val = 0x4d},
{.opt = "--pagedown", .val = 0x4e},
{.opt = "--right", .val = 0x4f},
{.opt = "--left", .val = 0x50},
{.opt = "--down", .val = 0x51},
{.opt = "--kp-enter", .val = 0x58},
{.opt = "--up", .val = 0x52},
{.opt = "--num-lock", .val = 0x53},
{.opt = "--1", .val = 0x1e},
{.opt = "--2", .val = 0x1f},
{.opt = "--3", .val = 0x20},
{.opt = "--4", .val = 0x21},
{.opt = "--5", .val = 0x22},
{.opt = "--6", .val = 0x23},
{.opt = "--7", .val = 0x24},
{.opt = "--8", .val = 0x25},
{.opt = "--9", .val = 0x26},
{.opt = "--0", .val = 0x27},
{.opt = NULL}
};
int keyboard_fill_report(char report[8], char buf[SIZE], int *hold)
{
char *tok = strtok(buf, " ");
int key = 0;
int i = 0;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
if (strcmp(tok, "--hold") == 0) {
*hold = 1;
continue;
}
if (key < 6) {
for (i = 0; kval[i].opt != NULL; i++)
if (strcmp(tok, kval[i].opt) == 0) {
report[2 + key++] = kval[i].val;
break;
}
if (kval[i].opt != NULL)
continue;
}
if (key < 6)
if (islower(tok[0])) {
report[2 + key++] = (tok[0] - ('a' - 0x04));
continue;
}
for (i = 0; kmod[i].opt != NULL; i++)
if (strcmp(tok, kmod[i].opt) == 0) {
report[0] = report[0] | kmod[i].val;
break;
}
if (kmod[i].opt != NULL)
continue;
if (key < 6)
fprintf(stderr, "unknown option: %s\n", tok);
}
return 8;
}
static struct options mmod[] = {
{.opt = "--b1", .val = 0x01},
{.opt = "--b2", .val = 0x02},
{.opt = "--b3", .val = 0x04},
{.opt = NULL}
};
int mouse_fill_report(char report[8], char buf[SIZE], int *hold)
{
char *tok = strtok(buf, " ");
int mvt = 0;
int i = 0;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
if (strcmp(tok, "--hold") == 0) {
*hold = 1;
continue;
}
for (i = 0; mmod[i].opt != NULL; i++)
if (strcmp(tok, mmod[i].opt) == 0) {
report[0] = report[0] | mmod[i].val;
break;
}
if (mmod[i].opt != NULL)
continue;
if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
errno = 0;
report[1 + mvt++] = (char)strtol(tok, NULL, 0);
if (errno != 0) {
fprintf(stderr, "Bad value:'%s'\n", tok);
report[1 + mvt--] = 0;
}
continue;
}
fprintf(stderr, "unknown option: %s\n", tok);
}
return 3;
}
static struct options jmod[] = {
{.opt = "--b1", .val = 0x10},
{.opt = "--b2", .val = 0x20},
{.opt = "--b3", .val = 0x40},
{.opt = "--b4", .val = 0x80},
{.opt = "--hat1", .val = 0x00},
{.opt = "--hat2", .val = 0x01},
{.opt = "--hat3", .val = 0x02},
{.opt = "--hat4", .val = 0x03},
{.opt = "--hatneutral", .val = 0x04},
{.opt = NULL}
};
int joystick_fill_report(char report[8], char buf[SIZE], int *hold)
{
char *tok = strtok(buf, " ");
int mvt = 0;
int i = 0;
*hold = 1;
/* set default hat position: neutral */
report[3] = 0x04;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
for (i = 0; jmod[i].opt != NULL; i++)
if (strcmp(tok, jmod[i].opt) == 0) {
report[3] = (report[3] & 0xF0) | jmod[i].val;
break;
}
if (jmod[i].opt != NULL)
continue;
if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
errno = 0;
report[mvt++] = (char)strtol(tok, NULL, 0);
if (errno != 0) {
fprintf(stderr, "Bad value:'%s'\n", tok);
report[mvt--] = 0;
}
continue;
}
fprintf(stderr, "unknown option: %s\n", tok);
}
return 4;
}
void print_options(char c)
{
int i = 0;
if (c == 'k') {
printf(" keyboard options:\n"
" --hold\n");
for (i = 0; kmod[i].opt != NULL; i++)
printf("\t\t%s\n", kmod[i].opt);
printf("\n keyboard values:\n"
" [a-z] or\n");
for (i = 0; kval[i].opt != NULL; i++)
printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
printf("\n");
} else if (c == 'm') {
printf(" mouse options:\n"
" --hold\n");
for (i = 0; mmod[i].opt != NULL; i++)
printf("\t\t%s\n", mmod[i].opt);
printf("\n mouse values:\n"
" Two signed numbers\n"
"--quit to close\n");
} else {
printf(" joystick options:\n");
for (i = 0; jmod[i].opt != NULL; i++)
printf("\t\t%s\n", jmod[i].opt);
printf("\n joystick values:\n"
" three signed numbers\n"
"--quit to close\n");
}
}
int main(int argc, const char *argv[])
{
char *ip = "0.0.0.0";
int port = 50009;
int e;
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
struct tcp_info tcp_info;
char buffer[SIZE];
const char *filename = NULL;
int fd = 0;
char buf[SIZE];
int cmd_len;
char report[8];
int to_send = 8;
int hold = 0;
fd_set rfds;
int retval, i;
if (argc < 3) {
fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
argv[0]);
return 1;
}
if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
return 2;
filename = argv[1];
server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server_fd<0)
{
perror("[-]Error in socket");
exit(1);
}
printf("[+]Server socket created. \n");
/* Set the option active */
int optval = 1;
socklen_t optlen = sizeof(optval);
if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, optlen) < 0) {
perror("[-]Error in SO_REUSEADDR");
exit(1);
}
printf("SO_REUSEADDR set on socket\n");
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
e = bind(server_fd,(struct sockaddr*)&server_addr, sizeof(server_addr));
if(e<0)
{
perror("[-]Error in Binding");
exit(1);
}
printf("[+]Binding Successfull.\n");
e = listen(server_fd, 10);
if(e==0)
{
printf("[+]Listening...\n");
}
else
{
perror("[-]Error in Binding");
exit(1);
}
socklen_t addr_size = sizeof(client_addr);
client_fd = accept(server_fd,(struct sockaddr*)&client_addr, &addr_size);
if(client_fd < 0){
perror("[-]Error in Accepting");
exit(1);
}else{
printf("[+]Connection accepted\n");
system("ls /sys/class/udc > /sys/kernel/config/usb_gadget/g1/UDC");
}
if ((fd = open(filename, O_RDWR, 0666)) == -1) {
perror(filename);
return 3;
}
print_options(argv[2][0]);
while (42) {
FD_ZERO(&rfds);
FD_SET(client_fd, &rfds);
FD_SET(fd, &rfds);
retval = select(fd + 1, &rfds, NULL, NULL, NULL);
if (retval == -1 && errno == EINTR)
continue;
if (retval < 0) {
perror("select()");
return 4;
}
if (FD_ISSET(fd, &rfds)) {
cmd_len = read(fd, buf, SIZE - 1);
printf("recv report:");
for (i = 0; i < cmd_len; i++)
printf(" %02x", buf[i]);
printf("\n");
}
if (FD_ISSET(client_fd, &rfds)) {
memset(report, 0x0, sizeof(report));
bzero(buf,SIZE);
cmd_len = read(client_fd, buf, SIZE - 1);
int error = sizeof(tcp_info);
getsockopt(client_fd, IPPROTO_TCP, TCP_INFO, &tcp_info, (socklen_t *)&error);
if(tcp_info.tcpi_state == TCP_ESTABLISHED)
{
printf("connect ok \r\n");
}
else
{
printf("connect error\r\n");
system("echo > /sys/kernel/config/usb_gadget/g1/UDC");
close(fd);
close(client_fd);
return 7;
}
if (cmd_len == 0)
break;
if(buf[cmd_len - 1] == '\n')
{
buf[cmd_len - 1] = '\0';
}
fprintf(stdout, "buffer set: %s\n", buf);
fprintf(stdout, "cmd_len set: %d\n", cmd_len);
hold = 0;
memset(report, 0x0, sizeof(report));
const char *message = "receive";
send(client_fd, message, strlen(message), 0);
if (argv[2][0] == 'k')
to_send = keyboard_fill_report(report, buf, &hold);
else if (argv[2][0] == 'm')
to_send = mouse_fill_report(report, buf, &hold);
else
to_send = joystick_fill_report(report, buf, &hold);
if (to_send == -1)
break;
if (write(fd, report, to_send) != to_send) {
perror(filename);
return 5;
}
if (!hold) {
memset(report, 0x0, sizeof(report));
if (write(fd, report, to_send) != to_send) {
perror(filename);
return 6;
}
}
}
}
system("echo > /sys/kernel/config/usb_gadget/g1/UDC");
close(fd);
close(client_fd);
close(server_fd);
return 0;
}

View File

@ -0,0 +1,330 @@
/* hid_gadget_test */
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUF_LEN 512
struct options {
const char *opt;
unsigned char val;
};
static struct options kmod[] = {
{.opt = "--left-ctrl", .val = 0x01},
{.opt = "--right-ctrl", .val = 0x10},
{.opt = "--left-shift", .val = 0x02},
{.opt = "--right-shift", .val = 0x20},
{.opt = "--left-alt", .val = 0x04},
{.opt = "--right-alt", .val = 0x40},
{.opt = "--left-meta", .val = 0x08},
{.opt = "--right-meta", .val = 0x80},
{.opt = NULL}
};
static struct options kval[] = {
{.opt = "--return", .val = 0x28},
{.opt = "--esc", .val = 0x29},
{.opt = "--bckspc", .val = 0x2a},
{.opt = "--tab", .val = 0x2b},
{.opt = "--spacebar", .val = 0x2c},
{.opt = "--caps-lock", .val = 0x39},
{.opt = "--f1", .val = 0x3a},
{.opt = "--f2", .val = 0x3b},
{.opt = "--f3", .val = 0x3c},
{.opt = "--f4", .val = 0x3d},
{.opt = "--f5", .val = 0x3e},
{.opt = "--f6", .val = 0x3f},
{.opt = "--f7", .val = 0x40},
{.opt = "--f8", .val = 0x41},
{.opt = "--f9", .val = 0x42},
{.opt = "--f10", .val = 0x43},
{.opt = "--f11", .val = 0x44},
{.opt = "--f12", .val = 0x45},
{.opt = "--insert", .val = 0x49},
{.opt = "--home", .val = 0x4a},
{.opt = "--pageup", .val = 0x4b},
{.opt = "--del", .val = 0x4c},
{.opt = "--end", .val = 0x4d},
{.opt = "--pagedown", .val = 0x4e},
{.opt = "--right", .val = 0x4f},
{.opt = "--left", .val = 0x50},
{.opt = "--down", .val = 0x51},
{.opt = "--kp-enter", .val = 0x58},
{.opt = "--up", .val = 0x52},
{.opt = "--num-lock", .val = 0x53},
{.opt = NULL}
};
int keyboard_fill_report(char report[8], char buf[BUF_LEN], int *hold)
{
char *tok = strtok(buf, " ");
int key = 0;
int i = 0;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
if (strcmp(tok, "--hold") == 0) {
*hold = 1;
continue;
}
if (key < 6) {
for (i = 0; kval[i].opt != NULL; i++)
if (strcmp(tok, kval[i].opt) == 0) {
report[2 + key++] = kval[i].val;
break;
}
if (kval[i].opt != NULL)
continue;
}
if (key < 6)
if (islower(tok[0])) {
report[2 + key++] = (tok[0] - ('a' - 0x04));
continue;
}
for (i = 0; kmod[i].opt != NULL; i++)
if (strcmp(tok, kmod[i].opt) == 0) {
report[0] = report[0] | kmod[i].val;
break;
}
if (kmod[i].opt != NULL)
continue;
if (key < 6)
fprintf(stderr, "unknown option: %s\n", tok);
}
return 8;
}
static struct options mmod[] = {
{.opt = "--b1", .val = 0x01},
{.opt = "--b2", .val = 0x02},
{.opt = "--b3", .val = 0x04},
{.opt = NULL}
};
int mouse_fill_report(char report[8], char buf[BUF_LEN], int *hold)
{
char *tok = strtok(buf, " ");
int mvt = 0;
int i = 0;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
if (strcmp(tok, "--hold") == 0) {
*hold = 1;
continue;
}
for (i = 0; mmod[i].opt != NULL; i++)
if (strcmp(tok, mmod[i].opt) == 0) {
report[0] = report[0] | mmod[i].val;
break;
}
if (mmod[i].opt != NULL)
continue;
if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
errno = 0;
report[1 + mvt++] = (char)strtol(tok, NULL, 0);
if (errno != 0) {
fprintf(stderr, "Bad value:'%s'\n", tok);
report[1 + mvt--] = 0;
}
continue;
}
fprintf(stderr, "unknown option: %s\n", tok);
}
return 3;
}
static struct options jmod[] = {
{.opt = "--b1", .val = 0x10},
{.opt = "--b2", .val = 0x20},
{.opt = "--b3", .val = 0x40},
{.opt = "--b4", .val = 0x80},
{.opt = "--hat1", .val = 0x00},
{.opt = "--hat2", .val = 0x01},
{.opt = "--hat3", .val = 0x02},
{.opt = "--hat4", .val = 0x03},
{.opt = "--hatneutral", .val = 0x04},
{.opt = NULL}
};
int joystick_fill_report(char report[8], char buf[BUF_LEN], int *hold)
{
char *tok = strtok(buf, " ");
int mvt = 0;
int i = 0;
*hold = 1;
/* set default hat position: neutral */
report[3] = 0x04;
for (; tok != NULL; tok = strtok(NULL, " ")) {
if (strcmp(tok, "--quit") == 0)
return -1;
for (i = 0; jmod[i].opt != NULL; i++)
if (strcmp(tok, jmod[i].opt) == 0) {
report[3] = (report[3] & 0xF0) | jmod[i].val;
break;
}
if (jmod[i].opt != NULL)
continue;
if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
errno = 0;
report[mvt++] = (char)strtol(tok, NULL, 0);
if (errno != 0) {
fprintf(stderr, "Bad value:'%s'\n", tok);
report[mvt--] = 0;
}
continue;
}
fprintf(stderr, "unknown option: %s\n", tok);
}
return 4;
}
void print_options(char c)
{
int i = 0;
if (c == 'k') {
printf(" keyboard options:\n"
" --hold\n");
for (i = 0; kmod[i].opt != NULL; i++)
printf("\t\t%s\n", kmod[i].opt);
printf("\n keyboard values:\n"
" [a-z] or\n");
for (i = 0; kval[i].opt != NULL; i++)
printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
printf("\n");
} else if (c == 'm') {
printf(" mouse options:\n"
" --hold\n");
for (i = 0; mmod[i].opt != NULL; i++)
printf("\t\t%s\n", mmod[i].opt);
printf("\n mouse values:\n"
" Two signed numbers\n"
"--quit to close\n");
} else {
printf(" joystick options:\n");
for (i = 0; jmod[i].opt != NULL; i++)
printf("\t\t%s\n", jmod[i].opt);
printf("\n joystick values:\n"
" three signed numbers\n"
"--quit to close\n");
}
}
int main(int argc, const char *argv[])
{
const char *filename = NULL;
int fd = 0;
char buf[BUF_LEN];
int cmd_len;
char report[8];
int to_send = 8;
int hold = 0;
fd_set rfds;
int retval, i;
if (argc < 3) {
fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
argv[0]);
return 1;
}
if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
return 2;
filename = argv[1];
if ((fd = open(filename, O_RDWR, 0666)) == -1) {
perror(filename);
return 3;
}
print_options(argv[2][0]);
while (42) {
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
FD_SET(fd, &rfds);
retval = select(fd + 1, &rfds, NULL, NULL, NULL);
if (retval == -1 && errno == EINTR)
continue;
if (retval < 0) {
perror("select()");
return 4;
}
if (FD_ISSET(fd, &rfds)) {
cmd_len = read(fd, buf, BUF_LEN - 1);
printf("recv report:");
for (i = 0; i < cmd_len; i++)
printf(" %02x", buf[i]);
printf("\n");
}
if (FD_ISSET(STDIN_FILENO, &rfds)) {
memset(report, 0x0, sizeof(report));
cmd_len = read(STDIN_FILENO, buf, BUF_LEN - 1);
if (cmd_len == 0)
break;
buf[cmd_len - 1] = '\0';
hold = 0;
memset(report, 0x0, sizeof(report));
if (argv[2][0] == 'k')
to_send = keyboard_fill_report(report, buf, &hold);
else if (argv[2][0] == 'm')
to_send = mouse_fill_report(report, buf, &hold);
else
to_send = joystick_fill_report(report, buf, &hold);
if (to_send == -1)
break;
if (write(fd, report, to_send) != to_send) {
perror(filename);
return 5;
}
if (!hold) {
memset(report, 0x0, sizeof(report));
if (write(fd, report, to_send) != to_send) {
perror(filename);
return 6;
}
}
}
}
close(fd);
return 0;
}

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
import time
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
while True:
for i in range(4,35):
write_report(NULL_CHAR*2+chr(i)+NULL_CHAR*5)
i = i+1
print(i)
if i == 35:
print(i)
i = 4
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
print(i)
# Release all keys
write_report(NULL_CHAR*8)
time.sleep(0.2)

248
test/scan-code.csv Normal file
View File

@ -0,0 +1,248 @@
HID Usage,HID Usage Name,LKC,Linux Key Code Name,Version,AKC,Android Key Code Name,Notes
0x07 0x0001,Keyboard Error Roll Over,,,,,,
0x07 0x0002,Keyboard POST Fail,,,,,,
0x07 0x0003,Keyboard Error Undefined,,,,,,
0x07 0x0004,Keyboard a and A,0x001e,KEY_A,1.6,0x001d,KEYCODE_A,1
0x07 0x0005,Keyboard b and B,0x0030,KEY_B,1.6,0x001e,KEYCODE_B,1
0x07 0x0006,Keyboard c and C,0x002e,KEY_C,1.6,0x001f,KEYCODE_C,1
0x07 0x0007,Keyboard d and D,0x0020,KEY_D,1.6,0x0020,KEYCODE_D,1
0x07 0x0008,Keyboard e and E,0x0012,KEY_E,1.6,0x0021,KEYCODE_E,1
0x07 0x0009,Keyboard f and F,0x0021,KEY_F,1.6,0x0022,KEYCODE_F,1
0x07 0x000a,Keyboard g and G,0x0022,KEY_G,1.6,0x0023,KEYCODE_G,1
0x07 0x000b,Keyboard h and H,0x0023,KEY_H,1.6,0x0024,KEYCODE_H,1
0x07 0x000c,Keyboard i and I,0x0017,KEY_I,1.6,0x0025,KEYCODE_I,1
0x07 0x000d,Keyboard j and J,0x0024,KEY_J,1.6,0x0026,KEYCODE_J,1
0x07 0x000e,Keyboard k and K,0x0025,KEY_K,1.6,0x0027,KEYCODE_K,1
0x07 0x000f,Keyboard l and L,0x0026,KEY_L,1.6,0x0028,KEYCODE_L,1
0x07 0x0010,Keyboard m and M,0x0032,KEY_M,1.6,0x0029,KEYCODE_M,1
0x07 0x0011,Keyboard n and N,0x0031,KEY_N,1.6,0x002a,KEYCODE_N,1
0x07 0x0012,Keyboard o and O,0x0018,KEY_O,1.6,0x002b,KEYCODE_O,1
0x07 0x0013,Keyboard p and P,0x0019,KEY_P,1.6,0x002c,KEYCODE_P,1
0x07 0x0014,Keyboard q and Q,0x0010,KEY_Q,1.6,0x002d,KEYCODE_Q,1
0x07 0x0015,Keyboard r and R,0x0013,KEY_R,1.6,0x002e,KEYCODE_R,1
0x07 0x0016,Keyboard s and S,0x001f,KEY_S,1.6,0x002f,KEYCODE_S,1
0x07 0x0017,Keyboard t and T,0x0014,KEY_T,1.6,0x0030,KEYCODE_T,1
0x07 0x0018,Keyboard u and U,0x0016,KEY_U,1.6,0x0031,KEYCODE_U,1
0x07 0x0019,Keyboard v and V,0x002f,KEY_V,1.6,0x0032,KEYCODE_V,1
0x07 0x001a,Keyboard w and W,0x0011,KEY_W,1.6,0x0033,KEYCODE_W,1
0x07 0x001b,Keyboard x and X,0x002d,KEY_X,1.6,0x0034,KEYCODE_X,1
0x07 0x001c,Keyboard y and Y,0x0015,KEY_Y,1.6,0x0035,KEYCODE_Y,1
0x07 0x001d,Keyboard z and Z,0x002c,KEY_Z,1.6,0x0036,KEYCODE_Z,1
0x07 0x001e,Keyboard 1 and !,0x0002,KEY_1,1.6,0x0008,KEYCODE_1,1
0x07 0x001f,Keyboard 2 and @,0x0003,KEY_2,1.6,0x0009,KEYCODE_2,1
0x07 0x0020,Keyboard 3 and #,0x0004,KEY_3,1.6,0x000a,KEYCODE_3,1
0x07 0x0021,Keyboard 4 and $,0x0005,KEY_4,1.6,0x000b,KEYCODE_4,1
0x07 0x0022,Keyboard 5 and %,0x0006,KEY_5,1.6,0x000c,KEYCODE_5,1
0x07 0x0023,Keyboard 6 and ^,0x0007,KEY_6,1.6,0x000d,KEYCODE_6,1
0x07 0x0024,Keyboard 7 and &,0x0008,KEY_7,1.6,0x000e,KEYCODE_7,1
0x07 0x0025,Keyboard 8 and *,0x0009,KEY_8,1.6,0x000f,KEYCODE_8,1
0x07 0x0026,Keyboard 9 and (,0x000a,KEY_9,1.6,0x0010,KEYCODE_9,1
0x07 0x0027,Keyboard 0 and ),0x000b,KEY_0,1.6,0x0007,KEYCODE_0,1
0x07 0x0028,Keyboard Return (ENTER),0x001c,KEY_ENTER,1.6,0x0042,KEYCODE_ENTER,1
0x07 0x0029,Keyboard ESCAPE,0x0001,KEY_ESC,3,0x006f,KEYCODE_ESCAPE,
"""""","""""","""""","""""",2.3,0x0004,KEYCODE_BACK,
0x07 0x002a,Keyboard DELETE (Backspace),0x000e,KEY_BACKSPACE,1.6,0x0043,KEYCODE_DEL,
0x07 0x002b,Keyboard Tab,0x000f,KEY_TAB,1.6,0x003d,KEYCODE_TAB,
0x07 0x002c,Keyboard Spacebar,0x0039,KEY_SPACE,1.6,0x003e,KEYCODE_SPACE,
0x07 0x002d,Keyboard - and _,0x000c,KEY_MINUS,1.6,0x0045,KEYCODE_MINUS,1
0x07 0x002e,Keyboard = and +,0x000d,KEY_EQUAL,1.6,0x0046,KEYCODE_EQUALS,1
0x07 0x002f,Keyboard [ and {,0x001a,KEY_LEFTBRACE,1.6,0x0047,KEYCODE_LEFT_BRACKET,1
0x07 0x0030,Keyboard ] and },0x001b,KEY_RIGHTBRACE,1.6,0x0048,KEYCODE_RIGHT_BRACKET,1
0x07 0x0031,Keyboard \ and |,0x002b,KEY_BACKSLASH,1.6,0x0049,KEYCODE_BACKSLASH,1
0x07 0x0032,Keyboard Non-US # and ~,0x002b,KEY_BACKSLASH,1.6,0x0049,KEYCODE_BACKSLASH,1
0x07 0x0033,Keyboard ; and :,0x0027,KEY_SEMICOLON,1.6,0x004a,KEYCODE_SEMICOLON,1
0x07 0x0034,Keyboard ' and ",0x0028,KEY_APOSTROPHE,1.6,0x004b,KEYCODE_APOSTROPHE,1
0x07 0x0035,Keyboard ` and ~,0x0029,KEY_GRAVE,3,0x0044,KEYCODE_GRAVE,1
0x07 0x0036,"Keyboard , and <",0x0033,KEY_COMMA,1.6,0x0037,KEYCODE_COMMA,1
0x07 0x0037,Keyboard . and >,0x0034,KEY_DOT,1.6,0x0038,KEYCODE_PERIOD,1
0x07 0x0038,Keyboard / and ?,0x0035,KEY_SLASH,1.6,0x004c,KEYCODE_SLASH,1
0x07 0x0039,Keyboard Caps Lock,0x003a,KEY_CAPSLOCK,3,0x0073,KEYCODE_CAPS_LOCK,
0x07 0x003a,Keyboard F1,0x003b,KEY_F1,3,0x0083,KEYCODE_F1,
"""""","""""","""""","""""",1.6,0x0052,KEYCODE_MENU,
0x07 0x003b,Keyboard F2,0x003c,KEY_F2,3,0x0084,KEYCODE_F2,
"""""","""""","""""","""""",1.6,0x0002,KEYCODE_SOFT_RIGHT,
0x07 0x003c,Keyboard F3,0x003d,KEY_F3,3,0x0085,KEYCODE_F3,
"""""","""""","""""","""""",1.6,0x0005,KEYCODE_CALL,
0x07 0x003d,Keyboard F4,0x003e,KEY_F4,3,0x0086,KEYCODE_F4,
"""""","""""","""""","""""",1.6,0x0006,KEYCODE_ENDCALL,
0x07 0x003e,Keyboard F5,0x003f,KEY_F5,3,0x0087,KEYCODE_F5,
0x07 0x003f,Keyboard F6,0x0040,KEY_F6,3,0x0088,KEYCODE_F6,
0x07 0x0040,Keyboard F7,0x0041,KEY_F7,3,0x0089,KEYCODE_F7,
0x07 0x0041,Keyboard F8,0x0042,KEY_F8,3,0x008a,KEYCODE_F8,
0x07 0x0042,Keyboard F9,0x0043,KEY_F9,3,0x008b,KEYCODE_F9,
0x07 0x0043,Keyboard F10,0x0044,KEY_F10,3,0x008c,KEYCODE_F10,
"""""","""""","""""","""""",2.3,0x0052,KEYCODE_MENU,
0x07 0x0044,Keyboard F11,0x0057,KEY_F11,3,0x008d,KEYCODE_F11,
0x07 0x0045,Keyboard F12,0x0058,KEY_F12,3,0x008e,KEYCODE_F12,
0x07 0x0046,Keyboard Print Screen,0x0063,KEY_SYSRQ,3,0x0078,KEYCODE_SYSRQ,
0x07 0x0047,Keyboard Scroll Lock,0x0046,KEY_SCROLLLOCK,3,0x0074,KEYCODE_SCROLL_LOCK,
0x07 0x0048,Keyboard Pause,0x0077,KEY_PAUSE,3,0x0079,KEYCODE_BREAK,
0x07 0x0049,Keyboard Insert,0x006e,KEY_INSERT,3,0x007c,KEYCODE_INSERT,
0x07 0x004a,Keyboard Home,0x0066,KEY_HOME,3,0x007a,KEYCODE_MOVE_HOME,
"""""","""""","""""","""""",1.6,0x0003,KEYCODE_HOME,
0x07 0x004b,Keyboard Page Up,0x0068,KEY_PAGEUP,3,0x005c,KEYCODE_PAGE_UP,
0x07 0x004c,Keyboard Delete Forward,0x006f,KEY_DELETE,3,0x0070,KEYCODE_FORWARD_DEL,
0x07 0x004d,Keyboard End,0x006b,KEY_END,3,0x007b,KEYCODE_MOVE_END,
"""""","""""","""""","""""",1.6,0x0006,KEYCODE_ENDCALL,
0x07 0x004e,Keyboard Page Down,0x006d,KEY_PAGEDOWN,3,0x005d,KEYCODE_PAGE_DOWN,
0x07 0x004f,Keyboard Right Arrow,0x006a,KEY_RIGHT,1.6,0x0016,KEYCODE_DPAD_RIGHT,
0x07 0x0050,Keyboard Left Arrow,0x0069,KEY_LEFT,1.6,0x0015,KEYCODE_DPAD_LEFT,
0x07 0x0051,Keyboard Down Arrow,0x006c,KEY_DOWN,1.6,0x0014,KEYCODE_DPAD_DOWN,
0x07 0x0052,Keyboard Up Arrow,0x0067,KEY_UP,1.6,0x0013,KEYCODE_DPAD_UP,
0x07 0x0053,Keyboard Num Lock and Clear,0x0045,KEY_NUMLOCK,3,0x008f,KEYCODE_NUM_LOCK,
0x07 0x0054,Keypad /,0x0062,KEY_KPSLASH,3,0x009a,KEYCODE_NUMPAD_DIVIDE,
0x07 0x0055,Keypad *,0x0037,KEY_KPASTERISK,3,0x009b,KEYCODE_NUMPAD_MULTIPLY,
0x07 0x0056,Keypad -,0x004a,KEY_KPMINUS,3,0x009c,KEYCODE_NUMPAD_SUBTRACT,
0x07 0x0057,Keypad +,0x004e,KEY_KPPLUS,3,0x009d,KEYCODE_NUMPAD_ADD,
0x07 0x0058,Keypad ENTER,0x0060,KEY_KPENTER,3,0x00a0,KEYCODE_NUMPAD_ENTER,
0x07 0x0059,Keypad 1 and End,0x004f,KEY_KP1,3,0x0091,KEYCODE_NUMPAD_1,
0x07 0x005a,Keypad 2 and Down Arrow,0x0050,KEY_KP2,3,0x0092,KEYCODE_NUMPAD_2,
0x07 0x005b,Keypad 3 and PageDn,0x0051,KEY_KP3,3,0x0093,KEYCODE_NUMPAD_3,
0x07 0x005c,Keypad 4 and Left Arrow,0x004b,KEY_KP4,3,0x0094,KEYCODE_NUMPAD_4,
0x07 0x005d,Keypad 5,0x004c,KEY_KP5,3,0x0095,KEYCODE_NUMPAD_5,
0x07 0x005e,Keypad 6 and Right Arrow,0x004d,KEY_KP6,3,0x0096,KEYCODE_NUMPAD_6,
0x07 0x005f,Keypad 7 and Home,0x0047,KEY_KP7,3,0x0097,KEYCODE_NUMPAD_7,
0x07 0x0060,Keypad 8 and Up Arrow,0x0048,KEY_KP8,3,0x0098,KEYCODE_NUMPAD_8,
0x07 0x0061,Keypad 9 and Page Up,0x0049,KEY_KP9,3,0x0099,KEYCODE_NUMPAD_9,
0x07 0x0062,Keypad 0 and Insert,0x0052,KEY_KP0,3,0x0090,KEYCODE_NUMPAD_0,
0x07 0x0063,Keypad . and Delete,0x0053,KEY_KPDOT,3,0x009e,KEYCODE_NUMPAD_DOT,
0x07 0x0064,Keyboard Non-US \ and |,0x0056,KEY_102ND,4,0x0049,KEYCODE_BACKSLASH,1
0x07 0x0065,Keyboard Application,0x007f,KEY_COMPOSE,3,0x0052,KEYCODE_MENU,
"""""","""""","""""","""""",1.6,0x0054,KEYCODE_SEARCH,
0x07 0x0066,Keyboard Power,0x0074,KEY_POWER,1.6,0x001a,KEYCODE_POWER,
0x07 0x0067,Keypad =,0x0075,KEY_KPEQUAL,3,0x00a1,KEYCODE_NUMPAD_EQUALS,
0x07 0x0068,Keyboard F13,0x00b7,KEY_F13,,,,
0x07 0x0069,Keyboard F14,0x00b8,KEY_F14,,,,
0x07 0x006a,Keyboard F15,0x00b9,KEY_F15,,,,
0x07 0x006b,Keyboard F16,0x00ba,KEY_F16,,,,
0x07 0x006c,Keyboard F17,0x00bb,KEY_F17,,,,
0x07 0x006d,Keyboard F18,0x00bc,KEY_F18,,,,
0x07 0x006e,Keyboard F19,0x00bd,KEY_F19,,,,
0x07 0x006f,Keyboard F20,0x00be,KEY_F20,,,,
0x07 0x0070,Keyboard F21,0x00bf,KEY_F21,,,,
0x07 0x0071,Keyboard F22,0x00c0,KEY_F22,,,,
0x07 0x0072,Keyboard F23,0x00c1,KEY_F23,,,,
0x07 0x0073,Keyboard F24,0x00c2,KEY_F24,,,,
0x07 0x0074,Keyboard Execute,0x0086,KEY_OPEN,,,,
0x07 0x0075,Keyboard Help,0x008a,KEY_HELP,,,,
0x07 0x0076,Keyboard Menu,0x0082,KEY_PROPS,,,,
0x07 0x0077,Keyboard Select,0x0084,KEY_FRONT,,,,
0x07 0x0078,Keyboard Stop,0x0080,KEY_STOP,3,0x0056,KEYCODE_MEDIA_STOP,
0x07 0x0079,Keyboard Again,0x0081,KEY_AGAIN,,,,
0x07 0x007a,Keyboard Undo,0x0083,KEY_UNDO,,,,
0x07 0x007b,Keyboard Cut,0x0089,KEY_CUT,,,,
0x07 0x007c,Keyboard Copy,0x0085,KEY_COPY,,,,
0x07 0x007d,Keyboard Paste,0x0087,KEY_PASTE,,,,
0x07 0x007e,Keyboard Find,0x0088,KEY_FIND,,,,
0x07 0x007f,Keyboard Mute,0x0071,KEY_MUTE,3,0x00a4,KEYCODE_VOLUME_MUTE,
0x07 0x0080,Keyboard Volume Up,0x0073,KEY_VOLUMEUP,1.6,0x0018,KEYCODE_VOLUME_UP,
0x07 0x0081,Keyboard Volume Down,0x0072,KEY_VOLUMEDOWN,1.6,0x0019,KEYCODE_VOLUME_DOWN,
0x07 0x0082,Keyboard Locking Caps Lock,,,,,,
0x07 0x0083,Keyboard Locking Num Lock,,,,,,
0x07 0x0084,Keyboard Locking Scroll Lock,,,,,,
0x07 0x0085,Keypad Comma,0x0079,KEY_KPCOMMA,3,0x009f,KEYCODE_NUMPAD_COMMA,
0x07 0x0086,Keypad Equal Sign,,,,,,
0x07 0x0087,Keyboard International1,0x0059,KEY_RO,,,,
0x07 0x0088,Keyboard International2,0x005d,KEY_KATAKANAHIRAGANA,,,,
0x07 0x0089,Keyboard International3,0x007c,KEY_YEN,,,,
0x07 0x008a,Keyboard International4,0x005c,KEY_HENKAN,,,,
0x07 0x008b,Keyboard International5,0x005e,KEY_MUHENKAN,,,,
0x07 0x008c,Keyboard International6,0x005f,KEY_KPJPCOMMA,,,,
0x07 0x008d,Keyboard International7,,,,,,
0x07 0x008e,Keyboard International8,,,,,,
0x07 0x008f,Keyboard International9,,,,,,
0x07 0x0090,Keyboard LANG1,0x007a,KEY_HANGEUL,,,,
0x07 0x0091,Keyboard LANG2,0x007b,KEY_HANJA,,,,
0x07 0x0092,Keyboard LANG3,0x005a,KEY_KATAKANA,,,,
0x07 0x0093,Keyboard LANG4,0x005b,KEY_HIRAGANA,,,,
0x07 0x0094,Keyboard LANG5,0x0055,KEY_ZENKAKUHANKAKU,,,,
0x07 0x0095,Keyboard LANG6,,,,,,
0x07 0x0096,Keyboard LANG7,,,,,,
0x07 0x0097,Keyboard LANG8,,,,,,
0x07 0x0098,Keyboard LANG9,,,,,,
0x07 0x0099,Keyboard Alternate Erase,,,,,,
0x07 0x009a,Keyboard SysReq/Attention,,,,,,
0x07 0x009b,Keyboard Cancel,,,,,,
0x07 0x009c,Keyboard Clear,,,,,,
0x07 0x009d,Keyboard Prior,,,,,,
0x07 0x009e,Keyboard Return,,,,,,
0x07 0x009f,Keyboard Separator,,,,,,
0x07 0x00a0,Keyboard Out,,,,,,
0x07 0x00a1,Keyboard Oper,,,,,,
0x07 0x00a2,Keyboard Clear/Again,,,,,,
0x07 0x00a3,Keyboard CrSel/Props,,,,,,
0x07 0x00a4,Keyboard ExSel,,,,,,
0x07 0x00b0,Keypad 00,,,,,,
0x07 0x00b1,Keypad 000,,,,,,
0x07 0x00b2,Thousands Separator,,,,,,
0x07 0x00b3,Decimal Separator,,,,,,
0x07 0x00b4,Currency Unit,,,,,,
0x07 0x00b5,Currency Sub-unit,,,,,,
0x07 0x00b6,Keypad (,0x00b3,KEY_KPLEFTPAREN,3,0x00a2,KEYCODE_NUMPAD_LEFT_PAREN,
0x07 0x00b7,Keypad ),0x00b4,KEY_KPRIGHTPAREN,3,0x00a3,KEYCODE_NUMPAD_RIGHT_PAREN,
0x07 0x00b8,Keypad {,,,,,,
0x07 0x00b9,Keypad },,,,,,
0x07 0x00ba,Keypad Tab,,,,,,
0x07 0x00bb,Keypad Backspace,,,,,,
0x07 0x00bc,Keypad A,,,,,,
0x07 0x00bd,Keypad B,,,,,,
0x07 0x00be,Keypad C,,,,,,
0x07 0x00bf,Keypad D,,,,,,
0x07 0x00c0,Keypad E,,,,,,
0x07 0x00c1,Keypad F,,,,,,
0x07 0x00c2,Keypad XOR,,,,,,
0x07 0x00c3,Keypad ^,,,,,,
0x07 0x00c4,Keypad %,,,,,,
0x07 0x00c5,Keypad <,,,,,,
0x07 0x00c6,Keypad >,,,,,,
0x07 0x00c7,Keypad &,,,,,,
0x07 0x00c8,Keypad &&,,,,,,
0x07 0x00c9,Keypad |,,,,,,
0x07 0x00ca,Keypad ||,,,,,,
0x07 0x00cb,Keypad :,,,,,,
0x07 0x00cc,Keypad #,,,,,,
0x07 0x00cd,Keypad Space,,,,,,
0x07 0x00ce,Keypad @,,,,,,
0x07 0x00cf,Keypad !,,,,,,
0x07 0x00d0,Keypad Memory Store,,,,,,
0x07 0x00d1,Keypad Memory Recall,,,,,,
0x07 0x00d2,Keypad Memory Clear,,,,,,
0x07 0x00d3,Keypad Memory Add,,,,,,
0x07 0x00d4,Keypad Memory Subtract,,,,,,
0x07 0x00d5,Keypad Memory Multiply,,,,,,
0x07 0x00d6,Keypad Memory Divide,,,,,,
0x07 0x00d7,Keypad +/-,,,,,,
0x07 0x00d8,Keypad Clear,,,,,,
0x07 0x00d9,Keypad Clear Entry,,,,,,
0x07 0x00da,Keypad Binary,,,,,,
0x07 0x00db,Keypad Octal,,,,,,
0x07 0x00dc,Keypad Decimal,,,,,,
0x07 0x00dd,Keypad Hexadecimal,,,,,,
0x07 0x00e0,Keyboard Left Control,0x001d,KEY_LEFTCTRL,3,0x0071,KEYCODE_CTRL_LEFT,
0x07 0x00e1,Keyboard Left Shift,0x002a,KEY_LEFTSHIFT,1.6,0x003b,KEYCODE_SHIFT_LEFT,
0x07 0x00e2,Keyboard Left Alt,0x0038,KEY_LEFTALT,1.6,0x0039,KEYCODE_ALT_LEFT,
0x07 0x00e3,Keyboard Left GUI,0x007d,KEY_LEFTMETA,3,0x0075,KEYCODE_META_LEFT,
0x07 0x00e4,Keyboard Right Control,0x0061,KEY_RIGHTCTRL,3,0x0072,KEYCODE_CTRL_RIGHT,
0x07 0x00e5,Keyboard Right Shift,0x0036,KEY_RIGHTSHIFT,1.6,0x003c,KEYCODE_SHIFT_RIGHT,
0x07 0x00e6,Keyboard Right Alt,0x0064,KEY_RIGHTALT,1.6,0x003a,KEYCODE_ALT_RIGHT,
0x07 0x00e7,Keyboard Right GUI,0x007e,KEY_RIGHTMETA,3,0x0076,KEYCODE_META_RIGHT,
0x07 0x00e8,,0x00a4,KEY_PLAYPAUSE,3,0x0055,KEYCODE_MEDIA_PLAY_PAUSE,
0x07 0x00e9,,0x00a6,KEY_STOPCD,3,0x0056,KEYCODE_MEDIA_STOP,
0x07 0x00ea,,0x00a5,KEY_PREVIOUSSONG,3,0x0058,KEYCODE_MEDIA_PREVIOUS,
0x07 0x00eb,,0x00a3,KEY_NEXTSONG,3,0x0057,KEYCODE_MEDIA_NEXT,
0x07 0x00ec,,0x00a1,KEY_EJECTCD,3,0x0081,KEYCODE_MEDIA_EJECT,
0x07 0x00ed,,0x0073,KEY_VOLUMEUP,1.6,0x0018,KEYCODE_VOLUME_UP,
0x07 0x00ee,,0x0072,KEY_VOLUMEDOWN,1.6,0x0019,KEYCODE_VOLUME_DOWN,
0x07 0x00ef,,0x0071,KEY_MUTE,3,0x00a4,KEYCODE_VOLUME_MUTE,
0x07 0x00f0,,0x0096,KEY_WWW,1.6,0x0040,KEYCODE_EXPLORER,
0x07 0x00f1,,0x009e,KEY_BACK,1.6,0x0004,KEYCODE_BACK,
0x07 0x00f2,,0x009f,KEY_FORWARD,3,0x007d,KEYCODE_FORWARD,
0x07 0x00f3,,0x0080,KEY_STOP,3,0x0056,KEYCODE_MEDIA_STOP,
0x07 0x00f4,,0x0088,KEY_FIND,,,,
0x07 0x00f5,,0x00b1,KEY_SCROLLUP,3,0x005c,KEYCODE_PAGE_UP,
0x07 0x00f6,,0x00b2,KEY_SCROLLDOWN,3,0x005d,KEYCODE_PAGE_DOWN,
0x07 0x00f7,,0x00b0,KEY_EDIT,,,,
0x07 0x00f8,,0x008e,KEY_SLEEP,,,,
0x07 0x00f9,,0x0098,KEY_COFFEE,4,0x001a,KEYCODE_POWER,
0x07 0x00fa,,0x00ad,KEY_REFRESH,,,,
0x07 0x00fb,,0x008c,KEY_CALC,4.0.3,0x00d2,KEYCODE_CALCULATOR,
Can't render this file because it contains an unexpected character in line 54 and column 28.

View File

@ -0,0 +1,9 @@
import socket
solist=[x for x in dir(socket) if x.startswith('SO_')]
solist.sort()
for x in solist:
print(x)

0
test/test.txt Normal file
View File

BIN
usr/bin/hid_gadget_test_select Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,43 +0,0 @@
#coding:utf-8
import os
from socket import *
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0','rb+') as fd:
fd.write(report)
#''代表服务器为 localhost
myHost = ''
#在一个非保留端口号上进行监听
myPort = 50009
#设置一个TCP socket对象
sockobj = socket(AF_INET, SOCK_STREAM)
#绑定端口号
sockobj.bind((myHost, myPort))
#监听允许5个连结
sockobj.listen(5)
#直到进程结束时才结束循环
while True:
#等待客户端连接
connection, address = sockobj.accept( )
#连接是一个新的socket
print ('Server connected by', address)
while True:
#读取客户端套接字的下一行
data = connection.recv(1024)
#如果没有数量的话,那么跳出循环
if not data: break
if data == "hid open":
os.popen('ls /sys/class/udc | xargs echo > /sys/kernel/config/usb_gadget/g1/UDC')
print("hid open")
elif data == "hid close":
os.popen('echo > /sys/kernel/config/usb_gadget/g1/UDC')
print("hid close")
else:
write_report(data)
#发送一个回复至客户端
connection.send('server => ' + data)
#当socket关闭时eof
connection.close( )