|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import bluetooth |
| 4 | +import dbus |
| 5 | +import dbus.mainloop.glib |
| 6 | +from gi.repository import GObject as gobject |
| 7 | +from queue import * |
| 8 | +import sys |
| 9 | +import time |
| 10 | +from threading import Thread |
| 11 | + |
| 12 | +from bluefang import agents |
| 13 | +from bluefang import servicerecords |
| 14 | + |
| 15 | +BLUEZ_SERVICE = "org.bluez" |
| 16 | +BLUEZ_ADAPTER = BLUEZ_SERVICE + ".Adapter1" |
| 17 | +BLUEZ_AGENT_MANAGER = BLUEZ_SERVICE + ".AgentManager1" |
| 18 | +BLUEZ_DEVICE = BLUEZ_SERVICE + ".Device1" |
| 19 | +BLUEZ_PROFILE_MANAGER = BLUEZ_SERVICE + ".ProfileManager1" |
| 20 | + |
| 21 | +HID_UUID = "00001124-0000-1000-8000-00805f9b34fb" |
| 22 | +HID_CONTROL_PSM = 17 |
| 23 | +HID_INTERRUPT_PSM = 19 |
| 24 | + |
| 25 | +q = Queue() |
| 26 | + |
| 27 | +class Bluefang(): |
| 28 | + def __init__(self): |
| 29 | + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 30 | + self.bus = dbus.SystemBus() |
| 31 | + self.agent = agents.BluefangAgent() |
| 32 | + |
| 33 | + def info(self): |
| 34 | + manager = dbus.Interface(self.bus.get_object(BLUEZ_SERVICE, "/"), "org.freedesktop.DBus.ObjectManager") |
| 35 | + device = manager.GetManagedObjects()['/org/bluez/hci0'] # Requires correct permissions in /etc/dbus-1/system-local.conf |
| 36 | + #TODO Pull device name dynamically |
| 37 | + adapter = device[BLUEZ_ADAPTER] |
| 38 | + |
| 39 | + print(adapter) |
| 40 | + return adapter |
| 41 | + |
| 42 | + def connect(self, deviceAddress): |
| 43 | + """ |
| 44 | + Attempt to connect to the given Device. |
| 45 | + """ |
| 46 | + |
| 47 | + socket = bluetooth.BluetoothSocket(bluetooth.L2CAP) |
| 48 | + print("Attempting to connect to device %s" % deviceAddress) |
| 49 | + socket.connect((deviceAddress, 0x1001)) |
| 50 | + print("Connected! Spawning thread") |
| 51 | + connection = L2CAPWorker(socket, deviceAddress) |
| 52 | + connection.daemon = True |
| 53 | + connection.start() |
| 54 | + # Device Connect |
| 55 | + #devicePath = '/org/bluez/hci0/dev_' + deviceAddress.replace(':', '_') |
| 56 | + #device = dbus.Interface(self.bus.get_object(BLUEZ_SERVICE, devicePath), BLUEZ_DEVICE) |
| 57 | + #device.Connect() |
| 58 | + |
| 59 | + def start_server(self): |
| 60 | + control_server_socket = bluetooth.BluetoothSocket(bluetooth.L2CAP) |
| 61 | + control_server_socket.bind(("", HID_CONTROL_PSM)) |
| 62 | + bluetooth.set_l2cap_mtu(control_server_socket, 64) |
| 63 | + control_server_socket.listen(1) |
| 64 | + |
| 65 | + interrupt_server_socket = bluetooth.BluetoothSocket(bluetooth.L2CAP) |
| 66 | + interrupt_server_socket.bind(("", HID_INTERRUPT_PSM)) |
| 67 | + bluetooth.set_l2cap_mtu(interrupt_server_socket, 64) |
| 68 | + interrupt_server_socket.listen(1) |
| 69 | + |
| 70 | + print("Listening on both PSMs") |
| 71 | + |
| 72 | + control_socket, control_address = control_server_socket.accept() |
| 73 | + interrupt_socket, interrupt_address = interrupt_server_socket.accept() |
| 74 | + |
| 75 | + print("Spawned control and interrupt connection threads") |
| 76 | + |
| 77 | + control = L2CAPWorker(control_socket, control_address, 'receive') |
| 78 | + control.daemon = True |
| 79 | + control.start() |
| 80 | + |
| 81 | + interrupt = L2CAPWorker(interrupt_socket, interrupt_address, 'send') |
| 82 | + interrupt.daemon = True |
| 83 | + interrupt.start() |
| 84 | + |
| 85 | + while 1: |
| 86 | + command = input("Enter a command: ") |
| 87 | + q.put(command) |
| 88 | + |
| 89 | + |
| 90 | + def disconnect(self, blah): |
| 91 | + print("DISCONNECT %s" % blah) #TODO implement this |
| 92 | + |
| 93 | + def discoverable(self, state): |
| 94 | + deviceManager = dbus.Interface(self.bus.get_object(BLUEZ_SERVICE, "/org/bluez/hci0"), 'org.freedesktop.DBus.Properties') |
| 95 | + if state == "on": |
| 96 | + deviceManager.Set(BLUEZ_ADAPTER, 'Discoverable', True) |
| 97 | + elif state == "off": |
| 98 | + deviceManager.Set(BLUEZ_ADAPTER, 'Discoverable', True) |
| 99 | + else: |
| 100 | + raise Error("Unsupported state %s. Supported states: on, off" % state) |
| 101 | + |
| 102 | + def pair(self): |
| 103 | + self.agent.registerAsDefault() |
| 104 | + self.agent.startPairing() |
| 105 | + mainloop = gobject.MainLoop() |
| 106 | + mainloop.run() |
| 107 | + |
| 108 | + def registerProfile(self, profilePath): |
| 109 | + print("REGISTER %s" % profilePath) |
| 110 | + profileManager = dbus.Interface(self.bus.get_object(BLUEZ_SERVICE, "/org/bluez"), BLUEZ_PROFILE_MANAGER) |
| 111 | + profileManager.RegisterProfile( |
| 112 | + profilePath, |
| 113 | + HID_UUID, |
| 114 | + { |
| 115 | + "Name": "Omnihub HID", |
| 116 | + "AutoConnect": False, |
| 117 | + "ServiceRecord": servicerecords.HID_PROFILE |
| 118 | + } |
| 119 | + ) |
| 120 | + #TODO this doesn't register from CLI because the profile is unregistered when program exits |
| 121 | + |
| 122 | + def unregisterProfile(self, profilePath): |
| 123 | + print("UNREGISTER %s" % profilePath) |
| 124 | + profileManager = dbus.Interface(self.bus.get_object(BLUEZ_SERVICE, "/org/bluez"), BLUEZ_PROFILE_MANAGER) |
| 125 | + profileManager.UnregisterProfile(profilePath) |
| 126 | + #TODO handle the 'does not exist' error more gracefully? |
| 127 | + |
| 128 | + def sendHIDMessage(self, msg): |
| 129 | + print("SEND MESSAGE %s" % msg) |
| 130 | + |
| 131 | + def isConnectionEstablished(self): |
| 132 | + print("Connection established?") |
| 133 | + return True |
| 134 | + |
| 135 | +class L2CAPWorker(Thread): |
| 136 | + def __init__(self, socket, address, sendOrReceive): |
| 137 | + Thread.__init__(self) |
| 138 | + self.socket = socket |
| 139 | + self.address = address |
| 140 | + self.sendOrReceive = sendOrReceive |
| 141 | + |
| 142 | + def run(self): |
| 143 | + size = 500 |
| 144 | + try: |
| 145 | + if self.sendOrReceive == 'send': |
| 146 | + print("Sending on address: ${0}".format(self.address)) |
| 147 | + while True: |
| 148 | + command = q.get() |
| 149 | + print("Received command: " + command) |
| 150 | + if command == "left": |
| 151 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 152 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 153 | + elif command == "right": |
| 154 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 155 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 156 | + elif command == "up": |
| 157 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 158 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 159 | + elif command == "down": |
| 160 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 161 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 162 | + elif command == "enter": |
| 163 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 164 | + self.socket.send(bytes(bytearray((0xA1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)))) |
| 165 | + else: |
| 166 | + print("Unknown command: " + command) |
| 167 | + q.task_done() |
| 168 | + |
| 169 | + #self.socket.send(chr(0xA1) + chr(0x00) + chr(0x00) + chr(0x00) + chr(0x00)) |
| 170 | + #self.socket.send(chr(0xA1) + chr(0x54)) #up |
| 171 | + #self.socket.send(chr(0xA1) + chr(0x55)) #right |
| 172 | + #self.socket.send(chr(0xA1) + chr(0x56)) #down |
| 173 | + #self.socket.send(chr(0xA1) + chr(0x57)) #left |
| 174 | + else: |
| 175 | + print("Receiving on address: ${0}".format(self.address)) |
| 176 | + while 1: |
| 177 | + data = self.socket.recv(size) |
| 178 | + if data: |
| 179 | + print("Received data:") |
| 180 | + print(':'.join(hex(x) for x in data)) |
| 181 | + if data[0] == 0x71: |
| 182 | + print("Server wants to use Report Protocol Mode. Acknowledging.") |
| 183 | + self.socket.send(chr(0x00)) # Acknowledge that we will use this protocol mode |
| 184 | + self.socket.send(chr(0xA1) + chr(0x04)) |
| 185 | + finally: |
| 186 | + print("Closing socket for address: ${0}".format(self.address)) |
| 187 | + #client.close() |
| 188 | + self.socket.close() |
| 189 | + |
| 190 | +""" |
| 191 | +Every message should have the following: |
| 192 | +
|
| 193 | +L2CAP Length (2 bytes) |
| 194 | +L2CAP CID (2 bytes) |
| 195 | +HIDP Header (1 byte) |
| 196 | +HID Payload (variable) |
| 197 | +""" |
0 commit comments