[cosmetics] update date in GPL header
[vuplus_xbmc] / tools / EventClients / lib / python / bt / hid.py
1 # -*- coding: utf-8 -*-
2 #   Copyright (C) 2008-2013 Team XBMC
3 #
4 #   This program is free software; you can redistribute it and/or modify
5 #   it under the terms of the GNU General Public License as published by
6 #   the Free Software Foundation; either version 2 of the License, or
7 #   (at your option) any later version.
8 #
9 #   This program is distributed in the hope that it will be useful,
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #   GNU General Public License for more details.
13 #
14 #   You should have received a copy of the GNU General Public License along
15 #   with this program; if not, write to the Free Software Foundation, Inc.,
16 #   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 from bluetooth import *
19 import fcntl
20 import bluetooth._bluetooth as _bt
21 import array
22
23 class HID:
24     def __init__(self, bdaddress=None):
25         self.cport = 0x11  # HID's control PSM
26         self.iport = 0x13  # HID' interrupt PSM
27         self.backlog = 1
28
29         self.address = ""
30         if bdaddress:
31             self.address = bdaddress
32
33         # create the HID control socket
34         self.csock = BluetoothSocket( L2CAP )
35         self.csock.bind((self.address, self.cport))
36         set_l2cap_mtu(self.csock, 64)
37         self.csock.settimeout(2)
38         self.csock.listen(self.backlog)
39
40         # create the HID interrupt socket
41         self.isock = BluetoothSocket( L2CAP )
42         self.isock.bind((self.address, self.iport))
43         set_l2cap_mtu(self.isock, 64)
44         self.isock.settimeout(2)
45         self.isock.listen(self.backlog)
46
47         self.connected = False
48
49
50     def listen(self):
51         try:
52             (self.client_csock, self.caddress) = self.csock.accept()
53             print "Accepted Control connection from %s" % self.caddress[0]
54             (self.client_isock, self.iaddress) = self.isock.accept()
55             print "Accepted Interrupt connection from %s" % self.iaddress[0]
56             self.connected = True
57             return True
58         except Exception, e:
59             self.connected = False
60             return False
61
62     def get_local_address(self):
63         hci = BluetoothSocket( HCI )
64         fd  = hci.fileno()
65         buf = array.array('B', [0] * 96)
66         fcntl.ioctl(fd, _bt.HCIGETDEVINFO, buf, 1)
67         data = struct.unpack_from("H8s6B", buf.tostring())
68         return data[2:8][::-1]
69
70     def get_control_socket(self):
71         if self.connected:
72             return (self.client_csock, self.caddress)
73         else:
74             return None
75
76
77     def get_interrupt_socket(self):
78         if self.connected:
79             return (self.client_isock, self.iaddress)
80         else:
81             return None