forked from RfidResearchGroup/proxmark3
-
Notifications
You must be signed in to change notification settings - Fork 3
/
testMQTT.py
78 lines (62 loc) · 2.04 KB
/
testMQTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# python3.6
import random
from paho.mqtt import client as mqtt_client
broker = '0.0.0.0'
port = 1883
topic1 = "apdu1" #Sending responses
topic2 = "apdu2" #Waiting for commands
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = ''
password = ''
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def publish(client, cmd):
msg_count = 0
data = None
if (cmd == 1):
data = [0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00]
if (cmd == 3):
data = [0x01]
msg = f"messages: {msg_count}"
result = client.publish(topic1, bytearray(data)) #APDU2
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send {msg} to topic {topic2}")
else:
print(f"Failed to send message to topic {topic2}")
msg_count += 1
sendcmd = 0
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
global sendcmd
apdu_msg = msg.payload
print(apdu_msg)
print(len(apdu_msg))
if len(apdu_msg) == 10:
print("PING mensaje!")
sent_cmd = True
publish(client, 1)
sendcmd = 1
if (sendcmd == 0):
publish(client, 1)
sendcmd = 3
#print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
client.subscribe(topic2)
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
if __name__ == '__main__':
run()