-
Notifications
You must be signed in to change notification settings - Fork 1
/
rmnd.py
executable file
·178 lines (149 loc) · 3.52 KB
/
rmnd.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
import base64
import enum
import fcntl
import json
import os
import select
import signal
import socket
import struct
import sys
import time
room: str
nodeIds = []
rms: str
rmsIP: str
rmsPort: int
mts = None
mtsBuffer = None
mtsExpectedLen = 0
MtsOPL = 1
MtsLogin = 2
MtsLoginResponse = 3
MtsCommunicationKeyReq = 4
MtsCommunicationKeys = 5
MtsRoomsMap = 7
MtsOplCommands = 8
MtsFirmware = 9
def MTSMessage(route, attributeRoute, jwt, data, reply):
mtsMessage = { "route": route,
"attributeRoute": attributeRoute,
"jwt": jwt,
"data": data.decode("utf-8"),
"reply": reply }
j = json.dumps(mtsMessage)
return j
def MTSRoomsMap(roomNodeIds):
global nodeIds
print(roomNodeIds)
nodeIds = roomNodeIds['NodeIds']
for nd in nodeIds:
print(nd)
def MTSOpl(opl):
print('opl')
# this is a raw opl message
# if came from MTS, so it just needs to be forwarded to the
# correct BT address...
def MTSSend(mtsMessage):
global mts
#print(mtsMessage)
b = bytes(mtsMessage, 'utf-8')
#print(b)
l = len(b)
h = l.to_bytes(4, byteorder='little')
#print(h)
d = h + b
#print(d)
mts.send(d)
def MTSReceive(data):
#print('MTSReceive:', data)
j = data.decode('utf-8')
#print(j)
mtsMessage = json.loads(j)
#print(mtsMessage)
reply = mtsMessage['data']
#print(reply)
repData = base64.b64decode(reply)
#print(repData)
if mtsMessage['route'] == MtsOPL: # OPL packets are tunneled through MTS
MTSOPL(obj)
return
j = repData.decode('utf-8')
#print(j)
obj = json.loads(j)
if mtsMessage['route'] == MtsRoomsMap:
MTSRoomsMap(obj[0])
else:
print('MTSRoute %d not implemented' % (mtsMessage['route']))
def MTSBufferInit():
global mtsBuffer, mtsBufferLen, mtsExpectedLen
mtsBuffer = bytes(0)
mtsBufferLen = 0
mtsExpectedLen = 0
# Main
if len(sys.argv) != 3:
print("usage: rmnd <room number> <rmsserverIP:rmsserverPort")
sys.exit(1)
# Parse command line arguments
room = sys.argv[1]
rms = sys.argv[2]
x = rms.split(':')
rmsIP = x[0]
rmsPort = int(x[1])
print("RmNd Version 0.1 (Room: %s)" % room)
# connect to MTS server
while 1:
mts = socket.socket()
try:
#print('connecting...')
mts.connect((rmsIP, rmsPort))
print('connected')
break
except Exception as e:
print(e[1])
time.sleep(2)
mts.setblocking(0)
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NDELAY)
# get our nodeIds
MTSBufferInit()
req = {"RoomName": room}
reqData = base64.b64encode(bytes(json.dumps(req), 'utf-8'))
mtsMessage = MTSMessage(MtsRoomsMap, None, "jwt", reqData, False)
#print('send %s' % (mtsMessage))
MTSSend(mtsMessage)
# set up bluetooth advertizing
# note any devices that register...
# Wait for an incoming message...
eof = False
while 1:
if eof:
break
r = [mts]
try:
r,_w,_e = select.select(r, [], [], 1)
except Exception as e:
print(e)
break
for f in r:
if f == mts:
try:
data = mts.recv(1024)
#print('received:', data)
if mtsExpectedLen == 0:
l = data[0:4]
#print(l)
mtsExpectedLen = int.from_bytes(l, "little")
#print(mtsExpectedLen)
data = data[4:]
#print(data)
mtsBuffer += data
if (len(data) == mtsExpectedLen):
MTSReceive(data)
MTSBufferInit()
except Exception as e:
print(e)
break
if data == '':
print('EOF from RMSServer')
eof = True