Appel de bluetoothctl depuis Python

Startseite

Nachricht beantworten
Autor: Frédéric
Datum:  
To: Guilde
Betreff: Appel de bluetoothctl depuis Python
Hello,

J'essaye d'automatiser l'utilisation de bluetoothctl (le seul truc qui
marche !) avec un script Python ; l'idée est de tenter une connexion au
gamepad en tache de fond, jusqu'à ce que celui-ci soit visible (tout ce
bastringue sera embarqué sur le RPi pour piloter un hexapode, donc sans
clavier ni écran).

bluetoothctl est un shell, dans lequel on tape des commandes comme
'connect', 'info', 'disconnect'... Ça marche très bien en interactif. Par
contre, si je fais ça en Python, j'obtiens :

>>> import subprocess
>>> p = subprocess.Popen(["bluetoothctl"],

                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

>>> stdout, stderr = p.communicate("connect 00:90:E0:C4:F9:47")
>>> print stdout

[NEW] Controller B8:27:EB:01:18:BD raspberrypi [default]
[NEW] Device 00:90:E0:C4:F9:47 ipega Extending Game controller
[bluetooth]# connect 00:90:E0:C4:F9:47
Attempting to connect to 00:90:E0:C4:F9:47
[DEL] Controller B8:27:EB:01:18:BD raspberrypi [default]

>>> print stderr


(bluetoothctl:8850): GLib-CRITICAL **: Source ID 23 was not found when
                                       attempting to remove it


À ce niveau, le process bluetoothctl est crashé :o(

J'arrive à faire ce que je veux en relançant l'appel à Popen à chaque fois
que je veux faire quelque chose (les crashs surviennent après que la
commande soit exécutée, qu'elle réussisse ou pas), mais bon, j'aimerais
comprendre. Je pense que c'est la façon dont Python communique avec le
process fils (via communicate()), mais je ne comprend pas pourquoi.

J'ai tenté un appel via strace, mais ça ne m'apprend pas grand chose.
J'ai attaché le log à partir du moment où bluetoothctl reçoit une
commande via communicate(). Si quelqu'un voit quelque chose de pertinent...

Merci de vos lumières.

-- 
    Frédéric

read(0, "", 1) = 0
write(1, "connect 00:90:E0:C4:F9:47", 25) = 25
write(1, "\n", 1)                       = 1
rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e03dd0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, 8) = 0
rt_sigaction(SIGWINCH, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x76ca26b0}, {sa_handler=0x76e032b4, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x76ca26b0}, 8) = 0
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\0\0\0\0\21\0\0\0x\0\0\0\1\1o\0%\0\0\0/org/blu"..., iov_len=136}, {iov_base="", iov_len=0}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 136
write(1, "Attempting to connect to 00:90:E"..., 43) = 43
ioctl(0, TIOCGWINSZ, 0x7ea5afbc)        = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(0, TCGETS, 0x7ea5af8c)            = -1 ENOTTY (Inappropriate ioctl for device)
write(1, "\33[0;94m[bluetooth]\33[0m# ", 24) = 24
poll([{fd=0, events=POLLIN}, {fd=3, events=POLLIN}, {fd=4, events=POLLIN}, {fd=5, events=POLLIN}], 4, 300000) = 1 ([{fd=0, revents=POLLHUP}])
write(3, "\1\0\0\0\0\0\0\0", 8)         = 8
futex(0x100814, FUTEX_WAKE_PRIVATE, 2147483647) = 0
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1A\0\0\0\22\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="<\0\0\0type='signal',sender='org.bl"..., iov_len=65}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 209
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\235\0\0\0\23\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\230\0\0\0type='signal',sender='org.bl"..., iov_len=157}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 301
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\21\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\22\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\237\0\0\0\24\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\232\0\0\0type='signal',sender='org.bl"..., iov_len=159}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 303
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\23\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\236\0\0\0\25\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\231\0\0\0type='signal',sender='org.bl"..., iov_len=158}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 302
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\24\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "\r\33[K", 4)                  = 4
write(1, "[\33[0;91mDEL\33[0m] Controller B8:2"..., 68) = 68
write(1, "\33[0;94m[bluetooth]\33[0m# ", 24) = 24
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\235\0\0\0\26\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\230\0\0\0type='signal',sender='org.bl"..., iov_len=157}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 301
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\25\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\241\0\0\0\27\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\234\0\0\0type='signal',sender='org.bl"..., iov_len=161}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 305
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\26\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\233\0\0\0\30\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\226\0\0\0type='signal',sender='org.bl"..., iov_len=155}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 299
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\27\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\243\0\0\0\31\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\236\0\0\0type='signal',sender='org.bl"..., iov_len=163}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 307
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\30\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\262\0\0\0\32\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\255\0\0\0type='signal',sender='org.bl"..., iov_len=178}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 322
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\31\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\261\0\0\0\33\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\254\0\0\0type='signal',sender='org.bl"..., iov_len=177}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 321
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\32\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
getpeername(2, 0x7ea5af24, [128])       = -1 ENOTSOCK (Socket operation on non-socket)
futex(0x76f84ccc, FUTEX_WAKE_PRIVATE, 2147483647) = 0
ioctl(2, TCGETS, 0x7ea5af1c)            = -1 ENOTTY (Inappropriate ioctl for device)
getpid()                                = 8931
open("/usr/lib/arm-linux-gnueabihf/charset.alias", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
write(2, "\n(bluetoothctl:8931): GLib-CRITI"..., 96
(bluetoothctl:8931): GLib-CRITICAL **: Source ID 23 was not found when attempting to remove it
) = 96
write(1, "\r\rWaiting to connect to bluetoot"..., 37) = 37
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1v\0\0\0\34\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="q\0\0\0type='signal',sender='org.bl"..., iov_len=118}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 262
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\33\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1x\0\0\0\35\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="s\0\0\0type='signal',sender='org.bl"..., iov_len=120}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 264
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\34\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1\231\0\0\0\36\0\0\0\177\0\0\0\1\1o\0\25\0\0\0/org/fre"..., iov_len=144}, {iov_base="\224\0\0\0type='signal',sender='org.fr"..., iov_len=153}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 297
poll([{fd=4, events=POLLIN}], 1, 25000) = 1 ([{fd=4, revents=POLLIN}])
recvmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\0\0\0\0\35\0\0\0005\0\0\0\6\1s\0\6\0\0\0:1.727\0\0"..., iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 72
recvmsg(4, {msg_namelen=0}, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable)
close(5)                                = 0
write(1, "\r\33[K", 4)                  = 4
exit_group(0)                           = ?
+++ exited with 0 +++