R&Y: Added DAY RTA Tapp Card and Additional BKK BEM Stored Value Card AIDs to `aid_de...
[RRG-proxmark3.git] / tools / btaddon / hc06_factory.py
blob093f253225b887da66022a2225b9063002d77e1a
1 #!/usr/bin/env python3
3 import time
4 import sys
6 try:
7 import serial
8 except ModuleNotFoundError:
9 print("Please install pyserial module first.")
10 sys.exit(1)
12 name = b'PM3_RDV4.0'
13 pin = b'1234'
14 role = b'M'
15 role = b'S'
17 ser = None
19 baud2id = {
20 9600:b'4',
21 19200:b'5',
22 38400:b'6',
23 57600:b'7',
24 115200:b'8',
25 230400:b'9',
26 460800:b'A',
27 921600:b'B',
28 1382400:b'C'
31 p2c={
32 serial.PARITY_NONE:b'N',
33 serial.PARITY_ODD: b'O',
34 serial.PARITY_EVEN:b'E'
37 def send(cmd, timeout=1):
38 print("<<" + cmd.decode('utf8'))
39 ser.write(cmd)
40 out = b''
41 wait = timeout
42 ti = time.perf_counter()
43 while True:
44 time.sleep(0.05)
45 if (ser.in_waiting > 0):
46 # When receiving data, reset timer and shorten timeout
47 ti = time.perf_counter()
48 wait = 0.05
49 out += ser.read(1)
50 continue
51 # We stop either after 1s if no data or 50ms after last data received
52 if time.perf_counter() - ti > wait:
53 break
54 if out != b'':
55 print(">>" + out.decode('utf8'))
56 return out
58 def usart_bt_testcomm(baudrate, parity):
59 print("Configuring UART: %i 8%s1" % (baudrate, p2c[parity].decode('utf8')))
60 global ser
61 ser = serial.Serial(
62 port='/dev/ttyUSB0',
63 baudrate=baudrate,
64 parity=parity,
65 stopbits=serial.STOPBITS_ONE,
66 bytesize=serial.EIGHTBITS
68 ser.isOpen()
69 resp=send(b'AT')
70 if resp != b'OK':
71 ser.close()
72 return resp == b'OK'
74 if __name__ == '__main__':
75 print("WARNING: process only if strictly needed!")
76 print("This requires HC-06 dongle turned ON and NOT connected!")
77 if input("Is the HC-06 dongle LED blinking? (Say 'n' if you want to abort) [y/n] ") != 'y':
78 print("Aborting.")
79 exit(1)
81 print("\nTrying to detect current settings... Please be patient.")
83 if not usart_bt_testcomm(115200, serial.PARITY_NONE):
84 brs = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400]
85 ps = [ serial.PARITY_NONE, serial.PARITY_ODD, serial.PARITY_EVEN ]
86 ibr = 0
87 ip = 0
88 for p, b in [(i, j) for i in ps for j in brs]:
89 if usart_bt_testcomm(b, p):
90 break
91 else:
92 print("Sorry, add-on not found. Abort.")
93 exit(1)
95 print("Reconfiguring add-on to default settings.")
97 resp=send(b'AT+VERSION')
98 # Change name:
99 resp=send(b'AT+NAME%s' % name)
100 # Change BT PIN:
101 resp=send(b'AT+PIN%s' % pin)
102 # Change BT ROLE:
103 resp=send(b'AT+ROLE=%s' % role)
104 # Change BT Parity N:
105 resp=send(b'AT+PN')
106 # Change BT 115200:
107 resp=send(b'AT+BAUD%s' % baud2id[115200])
108 ser.close()
110 time.sleep(1)
111 print("Trying to connect add-on with the new settings.")
112 ser = serial.Serial(
113 port='/dev/ttyUSB0',
114 baudrate=115200,
115 parity=serial.PARITY_NONE,
116 stopbits=serial.STOPBITS_ONE,
117 bytesize=serial.EIGHTBITS
119 ser.isOpen()
120 if (send(b'AT', timeout=2) == b'OK'):
121 print("HC-06 dongle successfully reset")
122 else:
123 print("Lost contact with add-on, please try again")
124 ser.close()