Debian Codegraph @

module/_blueman.pyx

raw
1 #!/usr/bin/env python3
2 # coding=utf-8
3 #cython: language_level=3
4
5 cdef extern from "malloc.h":
6 cdef void free(void *ptr)
7
8 cdef extern from "string.h":
9 cdef char* strerror(int errnum)
10
11 cdef extern from "bluetooth/bluetooth.h":
12 ctypedef struct bdaddr_t:
13 unsigned char b[6]
14
15 int ba2str(bdaddr_t *ba, char *str)
16
17 cdef extern from "bluetooth/hci.h":
18 cdef struct hci_dev_stats:
19 unsigned long int err_rx
20 unsigned long int err_tx
21 unsigned long int cmd_tx
22 unsigned long int evt_rx
23 unsigned long int acl_tx
24 unsigned long int acl_rx
25 unsigned long int sco_tx
26 unsigned long int sco_rx
27 unsigned long int byte_rx
28 unsigned long int byte_tx
29
30
31 cdef struct hci_dev_info:
32 unsigned int dev_id
33 char name[8]
34
35 bdaddr_t bdaddr
36
37 unsigned long int flags
38 unsigned char type
39
40 unsigned char features[8]
41
42 unsigned long int pkt_type
43 unsigned long int link_policy
44 unsigned long int link_mode
45
46 unsigned int acl_mtu
47 unsigned int acl_pkts
48 unsigned int sco_mtu
49 unsigned int sco_pkts
50
51 hci_dev_stats stat
52
53
54
55 cdef extern from "bluetooth/hci_lib.h":
56 cdef int hci_devinfo(int dev_id, hci_dev_info *di)
57
58
59 cdef extern from "bluetooth/rfcomm.h":
60
61 cdef struct rfcomm_dev_info:
62 short id
63 unsigned int flags
64 unsigned short state
65 bdaddr_t src
66 bdaddr_t dst
67 unsigned char channel
68
69 cdef struct rfcomm_dev_list_req:
70 unsigned short dev_num
71 rfcomm_dev_info dev_info[0]
72
73
74 cdef extern from "libblueman.h":
75 cdef struct conn_info_handles:
76 unsigned int handle
77 int dd
78
79
80 cdef int connection_init(int dev_id, char *addr, conn_info_handles *ci)
81 cdef int connection_get_rssi(conn_info_handles *ci, signed char *ret_rssi)
82 cdef int connection_get_tpl(conn_info_handles *ci, signed char *ret_tpl, unsigned char type)
83 cdef int connection_close(conn_info_handles *ci)
84 cdef int c_get_rfcomm_channel "get_rfcomm_channel" (unsigned short service_class, char* btd_addr)
85 cdef int get_rfcomm_list(rfcomm_dev_list_req **ret)
86 cdef int c_create_rfcomm_device "create_rfcomm_device" (char *local_address, char *remote_address, int channel)
87 cdef int c_release_rfcomm_device "release_rfcomm_device" (int id)
88 cdef int _create_bridge(char* name)
89 cdef int _destroy_bridge(char* name)
90
91
92 class RFCOMMError(Exception):
93 pass
94
95 ERR = {
96 -1:"Can't allocate memory",
97 -2:"HCI device open failed",
98 -3:"Not connected",
99 -4:"Get connection info failed",
100 -5:"Read RSSI failed",
101 -6:"Read transmit power level request failed",
102 -8:"Getting rfcomm list failed",
103 -9:"ERR_SOCKET_FAILED",
104 -12: "Can't bind RFCOMM socket",
105 -13: "Can't connect RFCOMM socket",
106 -14: "Can't create RFCOMM TTY",
107 -15: "Can't release RFCOMM TTY"
108 }
109
110 RFCOMM_STATES = [
111 "unknown",
112 "connected",
113 "clean",
114 "bound",
115 "listening",
116 "connecting",
117 "connecting",
118 "config",
119 "disconnecting",
120 "closed"
121 ]
122 RFCOMM_REUSE_DLC = 0
123 RFCOMM_RELEASE_ONHUP = 1
124 RFCOMM_HANGUP_NOW = 2
125 RFCOMM_TTY_ATTACHED = 3
126
127 def get_rfcomm_channel(uuid, py_bdaddr):
128 if py_bdaddr is None:
129 return
130
131 py_bytes = py_bdaddr.encode('UTF-8')
132 cdef char* bdaddr = py_bytes
133 return c_get_rfcomm_channel(uuid, bdaddr)
134
135 def rfcomm_list():
136 cdef rfcomm_dev_list_req *dl
137
138 cdef char src[18]
139 cdef char dst[18]
140
141
142 res = get_rfcomm_list(&dl)
143 if res < 0:
144 raise RFCOMMError(ERR[res])
145
146 devs = []
147 for 0 <= i < dl.dev_num:
148 ba2str(&dl.dev_info[i].src, src)
149 ba2str(&dl.dev_info[i].dst, dst)
150
151 devs.append(
152 { "id": dl.dev_info[i].id,
153 "channel": dl.dev_info[i].channel,
154 "flags": dl.dev_info[i].flags,
155 "state": RFCOMM_STATES[dl.dev_info[i].state],
156 "src": src.decode("UTF-8"),
157 "dst": dst.decode("UTF-8")
158 })
159
160 free(dl)
161
162 return devs
163
164
165 def create_rfcomm_device(py_local_address, py_remote_address, channel):
166 py_bytes_local_address = py_local_address.encode('UTF-8')
167 py_bytes_remote_address = py_remote_address.encode('UTF-8')
168 cdef char* local_address = py_bytes_local_address
169 cdef char* remote_address = py_bytes_remote_address
170 ret = c_create_rfcomm_device(local_address, remote_address, channel)
171 if ret < 0:
172 raise RFCOMMError(ERR[ret])
173 return ret
174
175
176 def release_rfcomm_device(id):
177 ret = c_release_rfcomm_device(id)
178 if ret < 0:
179 raise RFCOMMError(ERR[ret])
180 return ret
181
182
183 class BridgeException(Exception):
184 def __init__(self, errno):
185 self.errno = errno
186
187 def __str__(self):
188 return strerror(self.errno)
189
190
191
192 def create_bridge(py_name="pan1"):
193 py_bytes_name = py_name.encode("UTF-8")
194 cdef char* name = py_bytes_name
195
196 err = _create_bridge(name)
197 if err < 0:
198 raise BridgeException(-err)
199
200 def destroy_bridge(py_name="pan1"):
201 py_bytes_name = py_name.encode("UTF-8")
202 cdef char* name = py_bytes_name
203
204 err = _destroy_bridge(name)
205 if err < 0:
206 raise BridgeException(-err)
207
208
209 class ConnInfoReadError(Exception):
210 pass
211
212 cdef class conn_info:
213 cdef conn_info_handles ci
214 cdef int hci
215 cdef char* addr
216 cdef public bint failed
217
218 def __init__(self, py_addr, py_hci_name="hci0"):
219 self.failed = False
220 py_bytes_addr = py_addr.encode("UTF-8")
221 cdef char* addr = py_bytes_addr
222
223 py_bytes_hci_name = py_hci_name.encode("UTF-8")
224 cdef char* hci_name = py_bytes_hci_name
225
226
227 self.hci = int(hci_name[3:])
228 self.addr = addr
229
230 def init(self):
231 res = connection_init(self.hci, self.addr, & self.ci)
232 if res < 0:
233 self.failed = True
234 raise ConnInfoReadError(ERR[res])
235
236 def deinit(self):
237 if self.failed:
238 return
239 connection_close(&self.ci)
240
241 def get_rssi(self):
242 cdef signed char rssi
243 res = connection_get_rssi(&self.ci, &rssi)
244 if res < 0:
245 raise ConnInfoReadError(ERR[res])
246
247 return rssi
248
249 def get_tpl(self, tp=0):
250 cdef signed char tpl
251 res = connection_get_tpl(&self.ci, &tpl, tp)
252 if res < 0:
253 raise ConnInfoReadError(ERR[res])
254
255 return tpl
256
257 def device_info(py_hci_name="hci0"):
258 py_bytes_hci_name = py_hci_name.encode("UTF-8")
259 cdef char* hci_name = py_bytes_hci_name
260
261 cdef hci_dev_info di
262 cdef int ret
263
264 dev_id = int(hci_name[3:])
265
266 res = hci_devinfo(dev_id, &di)
267
268 cdef char addr[32]
269 ba2str(&di.bdaddr, addr)
270
271 feats = []
272 for 0 <= i < 8:
273 feats.append(di.features[i])
274
275 x = [("err_rx", di.stat.err_rx),
276 ("err_tx",di.stat.err_tx),
277 ("cmd_tx",di.stat.cmd_tx),
278 ("evt_rx",di.stat.evt_rx),
279 ("acl_tx",di.stat.acl_tx),
280 ("acl_rx",di.stat.acl_rx),
281 ("sco_tx",di.stat.sco_tx),
282 ("sco_rx",di.stat.sco_rx),
283 ("byte_rx",di.stat.byte_rx),
284 ("byte_tx",di.stat.byte_tx)]
285
286 z = [("dev_id", di.dev_id),
287 ("name", di.name.decode("UTF-8")),
288 ("bdaddr",addr.decode("UTF-8")),
289 ("flags",di.flags),
290 ("type",di.type),
291 ("features",feats),
292 ("pkt_type",di.pkt_type),
293 ("link_policy",di.link_policy),
294 ("link_mode",di.link_mode),
295 ("acl_mtu",di.acl_mtu),
296 ("acl_pkts",di.acl_pkts),
297 ("sco_mtu",di.sco_pkts),
298 ("stat", dict(x))]
299
300 return dict(z)