forked from nbeguier/rfxcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfxproto.py
executable file
·583 lines (491 loc) · 18.5 KB
/
rfxproto.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env python3
# coding=UTF-8
"""
RFXPROTO.PY
Based on Sebastian Sjoholm work https://github.com/ssjoholm/rfxcmd_gc
Copyright 2012-2014 Sebastian Sjoholm, sebastian.sjoholm@gmail.com
Licensed under the GNU General Public License, Version 3.0
Copyright 2018-2021 by Nicolas BEGUIER, nicolas_beguier@hotmail.com
#
# NOTES
#
# RFXCOM is a Trademark of RFSmartLink.
#
# ------------------------------------------------------------------------------
#
# Protocol License Agreement
#
# The RFXtrx protocols are owned by RFXCOM, and are protected under applicable
# copyright laws.
#
# ==============================================================================
# It is only allowed to use this protocol or any part of it for RFXCOM products
# ==============================================================================
#
# The above Protocol License Agreement and the permission notice shall be
# included in all software using the RFXtrx protocols.
#
# Any use in violation of the foregoing restrictions may subject the user to
# criminal sanctions under applicable laws, as well as to civil liability for
# the breach of the terms and conditions of this license.
#
# ------------------------------------------------------------------------------
"""
__author__ = "Sebastian Sjoholm"
__copyright__ = "Copyright 2012-2014, Sebastian Sjoholm"
__license__ = "GPL"
__version__ = "2.0.1"
__maintainer__ = "Nicolas Béguier"
__date__ = "$Date: 2019-06-12 08:05:33 +0100 (Thu, 12 Jun 2019) $"
# Default modules
from codecs import decode as codecs_decode
import logging
import sys
import optparse
import time
import binascii
import traceback
# Serial
try:
import serial
except ImportError as err:
print(err)
sys.exit(1)
# Debug
# from pdb import set_trace as st
# RFXCMD library
from lib.rfx_utils import ByteToHex
# ------------------------------------------------------------------------------
class rfxcmd_data:
def __init__(
self,
reset="0d00000000000000000000000000",
status="0d00000002000000000000000000",
save="0d00000006000000000000000000"
):
self.reset = reset
self.status = status
self.save = save
class serial_data:
def __init__(
self,
device=None,
port=None,
rate=38400,
timeout=9
):
self.device = device
self.port = port
self.rate = rate
self.timeout = timeout
# ------------------------------------------------------------------------------
"""
Bit manipulation
Source: http://wiki.python.org/moin/BitManipulation
"""
def testBit(int_type, offset):
mask = 1 << offset
return int_type & mask
def setBit(int_type, offset):
mask = 1 << offset
return int_type | mask
def clearBit(int_type, offset):
mask = ~(1 << offset)
return int_type & mask
def toggleBit(int_type, offset):
mask = 1 << offset
return int_type ^ mask
# ------------------------------------------------------------------------------
def print_version():
"""
Print version information
"""
print("RFXPROTO Version: {}".format(__version__))
print(__date__.replace("$", ""))
return
# ------------------------------------------------------------------------------
def readbytes(number):
"""
Read amount of bytes from serial device
"""
buf = bytes()
for _ in range(number):
try:
byte = SERIAL.device.read()
except IOError as err:
LOGGER.error(err)
return False
except OSError as err:
LOGGER.error(err)
return False
buf += byte
return buf
# ------------------------------------------------------------------------------
def rfx_setmode(protocol, state):
"""
Create setmode message
"""
LOGGER.debug("Send status message")
res = None
try:
res = rfx_sendmsg(RFXCMD.status)
LOGGER.debug("Result: %s", str(res))
except Exception as err:
LOGGER.error(err)
raise
LOGGER.debug("Result: %s", str(ByteToHex(res)))
if res:
LOGGER.debug("Decode result")
try:
bstr = rfx_decode(res)
LOGGER.debug("Result: %s", str(bstr))
except Exception as err:
LOGGER.error(err)
raise
LOGGER.debug("Binary: %s" % str(bstr))
# Change
bstr[protocol] = str(state)
LOGGER.debug("Binary: " % str(bstr))
# Complete message
msg3 = bstr[0] + bstr[1] + bstr[2] + bstr[3] + bstr[4] + bstr[5] + bstr[6] + bstr[7]
msg3_int = int(msg3, 2)
msg3_hex = hex(msg3_int)[2:].zfill(2)
msg4 = bstr[8] + bstr[9] + bstr[10] + bstr[11] + bstr[12] + bstr[13] + bstr[14] + bstr[15]
msg4_int = int(msg4, 2)
msg4_hex = hex(msg4_int)[2:].zfill(2)
msg5 = bstr[16] + bstr[17] + bstr[18] + bstr[19] + bstr[20] + bstr[21] + bstr[22] + bstr[23]
msg5_int = int(msg5, 2)
msg5_hex = hex(msg5_int)[2:].zfill(2)
LOGGER.debug("msg3: %s / %s" % (str(msg3), msg3_hex))
LOGGER.debug("msg4: %s / %s" % (str(msg4), msg4_hex))
LOGGER.debug("msg5: %s / %s" % (str(msg5), msg5_hex))
# Send command
command = "0D000000035300%s%s%s00000000" % (msg3_hex, msg4_hex, msg5_hex)
LOGGER.debug("Command: %s" % command.upper())
try:
rfx_decodestatus(rfx_sendmsg(command.upper()))
except Exception as err:
LOGGER.error(err)
raise
return True
LOGGER.error("Invalid result received")
return False
# ------------------------------------------------------------------------------
def rfx_decode(message):
"""
Decode RFX message and output the protocol part in a binary string list
"""
LOGGER.debug("RFX_Encode -> BinStr")
data = {\
'packetlen' : ByteToHex(message[0]),\
'packettype' : ByteToHex(message[1]),\
'subtype' : ByteToHex(message[2]),\
'seqnbr' : ByteToHex(message[3]),\
'cmnd' : ByteToHex(message[4]),\
'msg1' : ByteToHex(message[5]),\
'msg2' : ByteToHex(message[6]),\
'msg3' : ByteToHex(message[7]),\
'msg4' : ByteToHex(message[8]),\
'msg5' : ByteToHex(message[9]),\
'msg6' : ByteToHex(message[10]),\
'msg7' : ByteToHex(message[11]),\
'msg8' : ByteToHex(message[12]),\
'msg9' : ByteToHex(message[13])}
LOGGER.debug("Extract the protocol part")
try:
msg3_bin = binascii.a2b_hex(data['msg3'])
msg3_binstr = "".join("{:08b}".format(ord(i)) for i in msg3_bin)
except Exception as err:
LOGGER.error(err)
raise
LOGGER.debug("msg3_bin: %s", str(msg3_bin))
LOGGER.debug("msg3_binstr: %s", str(msg3_binstr))
msg4_bin = binascii.a2b_hex(data['msg4'])
msg4_binstr = "".join("{:08b}".format(ord(i)) for i in msg4_bin)
msg5_bin = binascii.a2b_hex(data['msg5'])
msg5_binstr = "".join("{:08b}".format(ord(i)) for i in msg5_bin)
return list(msg3_binstr + msg4_binstr + msg5_binstr)
# ------------------------------------------------------------------------------
def rfx_decodestatus(message):
LOGGER.debug("Decode status message")
data = {
'packetlen' : ByteToHex(message[0]),
'packettype' : ByteToHex(message[1]),
'subtype' : ByteToHex(message[2]),
'seqnbr' : ByteToHex(message[3]),
'cmnd' : ByteToHex(message[4]),
'msg1' : ByteToHex(message[5]),
'msg2' : ByteToHex(message[6]),
'msg3' : ByteToHex(message[7]),
'msg4' : ByteToHex(message[8]),
'msg5' : ByteToHex(message[9]),
'msg6' : ByteToHex(message[10]),
'msg7' : ByteToHex(message[11]),
'msg8' : ByteToHex(message[12]),
'msg9' : ByteToHex(message[13])
}
LOGGER.info("RFX protocols")
LOGGER.info("-------------------------------------------------------------------")
LOGGER.info("#\tProtocol\t\t\tState")
LOGGER.info("-------------------------------------------------------------------")
# MSG 3
if testBit(int(data['msg3'], 16), 7) == 128:
LOGGER.info("0\tUndecoded\t\t\tEnabled")
else:
LOGGER.info("0\tUndecoded\t\t\tDisabled")
if testBit(int(data['msg3'], 16), 6) == 64:
LOGGER.info("1\tRFU\t\t\t\tEnabled")
else:
LOGGER.info("1\tRFU\t\t\t\tDisabled")
if testBit(int(data['msg3'], 16), 5) == 32:
LOGGER.info("2\tByrox SX\t\t\tEnabled")
else:
LOGGER.info("2\tByrox SX\t\t\tDisabled")
if testBit(int(data['msg3'], 16), 4) == 16:
LOGGER.info("3\tRSL\t\t\t\tEnabled")
else:
LOGGER.info("3\tRSL\t\t\t\tDisabled")
if testBit(int(data['msg3'], 16), 3) == 8:
LOGGER.info("4\tLightning4\t\t\tEnabled")
else:
LOGGER.info("4\tLightning4\t\t\tDisabled")
if testBit(int(data['msg3'], 16), 2) == 4:
LOGGER.info("5\tFineOffset/Viking\t\tEnabled")
else:
LOGGER.info("5\tFineOffset/Viking\t\tDisabled")
if testBit(int(data['msg3'], 16), 1) == 2:
LOGGER.info("6\tRubicson\t\t\tEnabled")
else:
LOGGER.info("6\tRubicson\t\t\tDisabled")
if testBit(int(data['msg3'], 16), 0) == 1:
LOGGER.info("7\tAE Blyss\t\t\tEnabled")
else:
LOGGER.info("7\tAE Blyss\t\t\tDisabled")
# MSG 4
if testBit(int(data['msg4'], 16), 6) == 128:
LOGGER.info("8\tBlindsT1/T2/T3/T4\t\tEnabled")
else:
LOGGER.info("8\tBlindsT1/T2/T3/T4\t\tDisabled")
if testBit(int(data['msg4'], 16), 6) == 64:
LOGGER.info("9\tBlindsT0\t\t\tEnabled")
else:
LOGGER.info("9\tBlindsT0\t\t\tDisabled")
if testBit(int(data['msg4'], 16), 5) == 32:
LOGGER.info("10\tProGuard\t\t\tEnabled")
else:
LOGGER.info("10\tProGuard\t\t\tDisabled")
if testBit(int(data['msg4'], 16), 4) == 16:
LOGGER.info("11\tFS20\t\t\t\tEnabled")
else:
LOGGER.info("11\tFS20\t\t\t\tDisabled")
if testBit(int(data['msg4'], 16), 3) == 8:
LOGGER.info("12\tLa Crosse\t\t\tEnabled")
else:
LOGGER.info("12\tLa Crosse\t\t\tDisabled")
if testBit(int(data['msg4'], 16), 2) == 4:
LOGGER.info("13\tHideki/UPM\t\t\tEnabled")
else:
LOGGER.info("13\tHideki/UPM\t\t\tDisabled")
if testBit(int(data['msg4'], 16), 1) == 2:
LOGGER.info("14\tAD LightwaveRF\t\t\tEnabled")
else:
LOGGER.info("14\tAD LightwaveRF\t\t\tDisabled")
if testBit(int(data['msg4'], 16), 0) == 1:
LOGGER.info("15\tMertik\t\t\t\tEnabled")
else:
LOGGER.info("15\tMertik\t\t\t\tDisabled")
# MSG 5
if testBit(int(data['msg5'], 16), 6) == 128:
LOGGER.info("16\tBlindsT1/T2/T3/T4\t\tEnabled")
else:
LOGGER.info("16\tBlindsT1/T2/T3/T4\t\tDisabled")
if testBit(int(data['msg5'], 16), 6) == 64:
LOGGER.info("17\tBlindsT0\t\t\tEnabled")
else:
LOGGER.info("17\tBlindsT0\t\t\tDisabled")
if testBit(int(data['msg5'], 16), 5) == 32:
LOGGER.info("18\tOregon Scientific\t\tEnabled")
else:
LOGGER.info("18\tOregon Scientific\t\tDisabled")
if testBit(int(data['msg5'], 16), 4) == 16:
LOGGER.info("19\tMeiantech\t\t\tEnabled")
else:
LOGGER.info("19\tMeiantech\t\t\tDisabled")
if testBit(int(data['msg5'], 16), 3) == 8:
LOGGER.info("20\tHomeEasy EU\t\t\tEnabled")
else:
LOGGER.info("20\tHomeEasy EU\t\t\tDisabled")
if testBit(int(data['msg5'], 16), 2) == 4:
LOGGER.info("21\tAC\t\t\t\tEnabled")
else:
LOGGER.info("21\tAC\t\t\t\tDisabled")
if testBit(int(data['msg5'], 16), 1) == 2:
LOGGER.info("22\tARC\t\t\t\tEnabled")
else:
LOGGER.info("22\tARC\t\t\t\tDisabled")
if testBit(int(data['msg5'], 16), 0) == 1:
LOGGER.info("23\tX10\t\t\t\tEnabled")
else:
LOGGER.info("23\tX10\t\t\t\tDisabled")
return
# ------------------------------------------------------------------------------
def rfx_sendmsg(message=None):
if message is None:
LOGGER.debug("No message was specified")
return False
# Check that serial module is loaded
try:
LOGGER.debug("Serial extension version: " + serial.VERSION)
except:
LOGGER.error("Error: Serial extension for Python could not be loaded")
return False
# Check for serial device
if SERIAL.port:
LOGGER.debug("Device: " + SERIAL.port)
else:
LOGGER.error("Device name missing")
return False
# Open serial port
LOGGER.debug("Open Serialport")
try:
SERIAL.device = serial.Serial(SERIAL.port, SERIAL.rate, timeout=SERIAL.timeout)
except serial.SerialException as err:
LOGGER.error("Error: Failed to connect on device, %s" % err)
return False
# Flush buffer
LOGGER.debug("Serialport flush output")
SERIAL.device.flushOutput()
LOGGER.debug("Serialport flush input")
SERIAL.device.flushInput()
# Send RESET
LOGGER.debug("Send RFX reset")
SERIAL.device.write(codecs_decode(RFXCMD.reset, "hex"))
time.sleep(1)
# Flush buffer
LOGGER.debug("Serialport flush output")
SERIAL.device.flushOutput()
LOGGER.debug("Serialport flush input")
SERIAL.device.flushInput()
LOGGER.debug("Send message")
SERIAL.device.write(codecs_decode(message, "hex"))
time.sleep(1)
# Waiting reply
result = None
byte = None
LOGGER.debug("Wait for the reply")
try:
while 1:
time.sleep(0.01)
try:
try:
if SERIAL.device.inWaiting() != 0:
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
LOGGER.debug("Timestamp: %s", timestamp)
LOGGER.debug("SerWaiting: %d", SERIAL.device.inWaiting())
byte = SERIAL.device.read()
LOGGER.debug("Byte: %s", ByteToHex(byte))
except IOError as err:
LOGGER.error("Serial read error: %s", err)
break
try:
if byte is not None:
if ord(byte) == 20 or ord(byte) == 13:
result = byte + readbytes(ord(byte))
LOGGER.debug("Message: %s", ByteToHex(result))
break
else:
result = byte + readbytes(ord(byte))
LOGGER.debug("Message: %s", ByteToHex(result))
LOGGER.error("Wrong or no response received")
sys.exit(1)
except Exception as err:
LOGGER.error(err)
sys.exit(1)
except OSError as err:
LOGGER.error("Error in message: %s", ByteToHex(message))
LOGGER.error(err)
LOGGER.error("Traceback: %s", traceback.format_exc())
LOGGER.error("Serial error (%s) ", ByteToHex(message))
break
except KeyboardInterrupt:
LOGGER.debug("Received keyboard interrupt")
pass
LOGGER.debug("Close serial port")
try:
SERIAL.device.close()
LOGGER.debug("Serial port closed")
except:
LOGGER.error("Error: Failed to close the serial port (%s)" % SERIAL.port)
return False
return result
# ------------------------------------------------------------------------------
if __name__ == '__main__':
RFXCMD = rfxcmd_data()
SERIAL = serial_data()
PARSER = optparse.OptionParser()
PARSER.add_option("-d", "--device", action="store", type="string", dest="device", help="Serial device of the RFXtrx433")
PARSER.add_option("-l", "--list", action="store_true", dest="list", help="List all protocols")
PARSER.add_option("-p", "--protocol", action="store", type="string", dest="protocol", help="Protocol number")
PARSER.add_option("-s", "--setstate", action="store", type="string", dest="state", help="Set protocol state (on or off)")
PARSER.add_option("-v", "--save", action="store_true", dest="save", help="Save current settings in device (Note device have limited write cycles)")
PARSER.add_option("-V", "--version", action="store_true", dest="version", help="Print rfxcmd version information")
PARSER.add_option("-D", "--debug", action="store_true", dest="debug", help="Debug logging on screen")
(OPTIONS, ARGS) = PARSER.parse_args()
if OPTIONS.debug:
LOGLEVEL = logging.DEBUG
else:
LOGLEVEL = logging.ERROR
# Logging
FORMATTER = logging.Formatter('%(asctime)s - %(threadName)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s')
HANDLER = logging.StreamHandler()
HANDLER.setFormatter(FORMATTER)
LOGGER = logging.getLogger("RFXPROTO")
LOGGER.setLevel(LOGLEVEL)
LOGGER.addHandler(HANDLER)
LOGGER.debug("Logger started")
LOGGER.debug("Version: %s", __version__)
LOGGER.debug("Date: %s", __date__.replace('$', ''))
if OPTIONS.version:
LOGGER.debug("Print version")
print_version()
if OPTIONS.device:
SERIAL.port = OPTIONS.device
LOGGER.debug("Serial device: %s", SERIAL.port)
else:
SERIAL.port = None
LOGGER.debug("Serial device: %s", SERIAL.port)
# Set protocol state
if OPTIONS.protocol:
pnum = int(OPTIONS.protocol)
if OPTIONS.state == "on" or OPTIONS.state == "off":
if not SERIAL.port == None:
LOGGER.debug("Protocol num: %s" % str(pnum))
LOGGER.debug("Protocol state: %s" % OPTIONS.state)
if OPTIONS.state == "on":
rfx_setmode(pnum, 1)
else:
rfx_setmode(pnum, 0)
else:
LOGGER.error("Serial device is missing")
else:
LOGGER.error("Unknown state parameter for protocol")
# Get current status
if OPTIONS.list:
if not SERIAL.port == None:
LOGGER.debug("Get current state")
try:
RESULT = rfx_sendmsg(RFXCMD.status)
LOGGER.debug("Result: %s" % ByteToHex(RESULT))
if RESULT:
rfx_decodestatus(RESULT)
else:
LOGGER.error("Invalid result received")
except Exception as err:
LOGGER.error("Send failed, error: {}".format(err))
else:
LOGGER.error("Serial device is missing")
LOGGER.debug("Exit")
sys.exit(0)
# ------------------------------------------------------------------------------
# END
# ------------------------------------------------------------------------------