📄 fakehid.c
字号:
/* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2003-2007 Marcel Holtmann <marcel@holtmann.org> * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#define _GNU_SOURCE#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <signal.h>#include <sys/poll.h>#include <sys/ioctl.h>#include <sys/socket.h>#include <bluetooth/bluetooth.h>#include <bluetooth/rfcomm.h>#include <bluetooth/hidp.h>#include "hidd.h"#include "uinput.h"#include <math.h>#ifdef NEED_PPOLL#include "ppoll.h"#endiftypedef struct{ unsigned short pressure; unsigned short x; unsigned short y; unsigned short pad;}TS_RET;static volatile sig_atomic_t __io_canceled = 0;static void sig_hup(int sig){}static void sig_term(int sig){ __io_canceled = 1;}static void send_event(int fd, uint16_t type, uint16_t code, int32_t value){ struct uinput_event event; int len; if (fd <= fileno(stderr)) return; memset(&event, 0, sizeof(event)); event.type = type; event.code = code; event.value = value; len = write(fd, &event, sizeof(event));}static int uinput_create(char *name, int keyboard, int mouse){ struct uinput_dev dev; int fd, aux; fd = open("/dev/uinput", O_RDWR); if (fd < 0) { fd = open("/dev/input/uinput", O_RDWR); if (fd < 0) { fd = open("/dev/misc/uinput", O_RDWR); if (fd < 0) { fprintf(stderr, "Can't open input device: %s (%d)\n", strerror(errno), errno); return -1; } } } memset(&dev, 0, sizeof(dev)); if (name) strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE); dev.id.bustype = BUS_BLUETOOTH; dev.id.vendor = 0x0000; dev.id.product = 0x0000; dev.id.version = 0x0000; if (write(fd, &dev, sizeof(dev)) < 0) { fprintf(stderr, "Can't write device information: %s (%d)\n", strerror(errno), errno); close(fd); return -1; } if (mouse) { ioctl(fd, UI_SET_EVBIT, EV_REL); for (aux = REL_X; aux <= REL_MISC; aux++) ioctl(fd, UI_SET_RELBIT, aux); } if (mouse) { ioctl(fd, UI_SET_EVBIT, EV_KEY); for (aux = BTN_LEFT; aux <= BTN_BACK; aux++) ioctl(fd, UI_SET_KEYBIT, aux); } ioctl(fd, UI_DEV_CREATE); return fd;}//static int rfcomm_connect( bdaddr_t *src, bdaddr_t *dst, uint8_t channel)static int rfcomm_connect( bdaddr_t *dst){ struct sockaddr_rc addr; bdaddr_t src; //bdaddr_t dst; uint8_t channel=1; int sk; // str2ba( "00:13:EF:F0:CF:15\0",&src); // str2ba( "00:13:EF:F0:CF:02\0",&dst); sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); if (sk < 0) { fprintf(stderr, "Can't create socket: %s (%d)\n", strerror(errno), errno); return -1; } memset(&addr, 0, sizeof(addr)); addr.rc_family = AF_BLUETOOTH; addr.rc_channel = channel; bacpy(&addr.rc_bdaddr, dst); /******* if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { fprintf(stderr, "Can't bind socket: %s (%d)\n", strerror(errno), errno); close(sk); return -1; } ********/ memset(&addr, 0, sizeof(addr)); addr.rc_family = AF_BLUETOOTH; bacpy(&addr.rc_bdaddr, dst); addr.rc_channel = channel; if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { fprintf(stderr, "Can't connect: %s (%d)\n", strerror(errno), errno); close(sk); return -1; } return sk;}/*********************************************************************************static void func(int fd){}static void back(int fd){}static void next(int fd){}static void button(int fd, unsigned int button, int is_press){ switch (button) { case 1: send_event(fd, EV_KEY, BTN_LEFT, is_press); break; case 3: send_event(fd, EV_KEY, BTN_RIGHT, is_press); break; } send_event(fd, EV_SYN, SYN_REPORT, 0);}static void move(int fd, unsigned int direction){ double angle; int32_t x, y; angle = (direction * 22.5) * 3.1415926 / 180; x = (int) ( sin(angle) * 8); y = (int) ( cos(angle) * -8); send_event(fd, EV_REL, REL_X, x); send_event(fd, EV_REL, REL_Y, y); send_event(fd, EV_SYN, SYN_REPORT, 0);}*************************************************************************/static inline void epox_decode(int fd, TS_RET event){ int32_t x,y; x = (int) event.x; y = (int) event.y; send_event (fd, EV_REL, REL_X, x); send_event (fd, EV_REL, REL_Y, y); send_event (fd, EV_SYN, SYN_REPORT, 0);}int main ( int argc, char * argv[] ){ unsigned char buf[16]; uint8_t channel =0; struct sigaction sa; struct pollfd p; sigset_t sigs; char addr[18]; int i, fd, sk, len; bdaddr_t src, dst; //char bdsrc[20]={0}; char bddst[20]={0}; TS_RET userdata[200]={0}; strcpy ( bddst, "00:13:EF:F0:CF:02\0" ); //str2ba ( bdsrc, &src); str2ba ( bddst, &dst); //sk = rfcomm_connect( &src, &dst, channel ); sk = rfcomm_connect( &dst ); if (sk < 0) return -1; fd = uinput_create("Bluetooth Presenter", 0, 1); if (fd < 0) { close(sk); return -1; } ba2str( &dst, addr); printf("Connected to %s on channel\n", addr); printf("Press CTRL-C for hangup\n"); memset(&sa, 0, sizeof(sa)); sa.sa_flags = SA_NOCLDSTOP; sa.sa_handler = SIG_IGN; sigaction(SIGCHLD, &sa, NULL); sigaction(SIGPIPE, &sa, NULL); sa.sa_handler = sig_term; sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); sa.sa_handler = sig_hup; sigaction(SIGHUP, &sa, NULL); sigfillset(&sigs); sigdelset(&sigs, SIGCHLD); sigdelset(&sigs, SIGPIPE); sigdelset(&sigs, SIGTERM); sigdelset(&sigs, SIGINT); sigdelset(&sigs, SIGHUP); p.fd = sk; p.events = POLLIN | POLLERR | POLLHUP; while (!__io_canceled) { p.revents = 0; if (ppoll(&p, 1, NULL, &sigs) < 1) continue; len=read(sk, userdata, sizeof(userdata)); if (len < 0) break; for (i = 0; i < len; i++) epox_decode(fd, userdata[i]); } printf("Disconnected\n"); ioctl(fd, UI_DEV_DESTROY); close(fd); close(sk); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -