Skip to content

Commit

Permalink
Merge pull request usb-tools#3 from BenGardiner/working_button_injector
Browse files Browse the repository at this point in the history
Working button injector
  • Loading branch information
pojungc committed Nov 4, 2015
2 parents a11f321 + d4f7620 commit 650b639
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/bindings/python/USBKeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from keymap import get_keycode
from evdev import InputDevice, categorize, ecodes
from select import select
import os

class USBKeyboardInterface(USBInterface):
name = "USB keyboard interface"
Expand Down Expand Up @@ -48,42 +49,70 @@ def __init__(self, verbose=0, text=None):
descriptors
)

self.devices = map(InputDevice, ('/dev/input/event1', '/dev/input/event1'))
self.devices = map(InputDevice, ('/dev/input/event0', '/dev/input/event0'))
self.devices = {dev.fd: dev for dev in self.devices}
for dev in self.devices.values(): print(dev)
self.current_keys = [0]

#clear all the LEDs
for ledpath in os.listdir('/sys/class/leds/'):
contents = open('/sys/class/leds/' + ledpath + '/trigger', 'w')
contents.write('none\n');
contents.close()
contents = open('/sys/class/leds/' + ledpath + '/brightness', 'w')
contents.write('0\n')
contents.close()

#TODO
#self.button1_rate_led = open('/sys/class/leds/switch:green:led_A/brightness', 'w')
self.button1_rate_led = open('/sqays/class/leds/beaglebone:green:usr3/brightness', 'w')
self.button1_status_led = open('/sys/class/leds/beaglebone:green:usr2/brightness', 'w')
self.button2_status_led = open('/sys/class/leds/beaglebone:green:mmc0/brightness', 'w')
#TODO
#self.button2_rate_led = open('/sys/class/leds/switch:green:led_B/brightness', 'w')
self.button2_rate_led = open('/sys/class/leds/beaglebone:green:heartbeat/brightness', 'w')

def handle_buffer_available(self):
r, w, x = select(self.devices, [], [])
for fd in r:
for event in self.devices[fd].read():
if event.type != ecodes.EV_KEY:
return
if event.code == 3 and event.value != 1:
print("TODO: do a reset of the rate limiter")

if event.code != 1 and event.code != 2:
return
print(event)


if event.value == 1: #key pressed
if 0 in self.current_keys:
self.current_keys.remove(0)
if event.code == 1:
if not 0x4 in self.current_keys:
self.current_keys.append(0x4)
self.button1_status_led.write('255\n')
self.button1_status_led.flush()
elif event.code == 2:
if not 0x14 in self.current_keys:
self.current_keys.append(0x14)

self.button2_status_led.write('255\n')
self.button2_status_led.flush()

else: #key released
if event.code == 1:
if 0x4 in self.current_keys:
self.current_keys.remove(0x4)
self.button1_status_led.write('0\n')
self.button1_status_led.flush()
elif event.code == 2:
if 0x14 in self.current_keys:
self.current_keys.remove(0x14)

self.button2_status_led.write('0\n')
self.button2_status_led.flush()

if not self.current_keys:
self.current_keys = [0]

self.endpoint.send(bytes([0,0] + self.current_keys + [0]*(6-len(self.current_keys)) ))

def type_letter(self, keycode, modifiers=0):
Expand Down
17 changes: 17 additions & 0 deletions src/tools/inject-reset-button
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python
import sys, os

path='/dev/input/event0'
os.chmod(path, 666)
event_file = open(path,'w')

#NB: valid layout on BBB -- not on x64
#int timestamp (1), int timestamp (2), short type, short code, int value -- all LSB

#press key 3
event_file.write("\x00"*4+"\x00"*4+"\x01\x00"+"\x03\x00"+"\x01\x00\x00\x00")
#release key 3
event_file.write("\x00"*4+"\x00"*4+"\x01\x00"+"\x03\x00"+"\x00\x00\x00\x00")
#send syn report
event_file.write("\x00"*4+"\x00"*4+"\x00\x00"+"\x00\x00"+"\x00\x00\x00\x00")

2 changes: 2 additions & 0 deletions src/tools/uinputme.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ main(void)
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, 2))
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, 3))
die("error: ioctl");

memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinputme");
Expand Down

0 comments on commit 650b639

Please sign in to comment.