forked from evilpete/insteonrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrf_send.py
executable file
·198 lines (146 loc) · 4.46 KB
/
rf_send.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
#!/usr/bin/python
import sys, traceback
import readline
#import rlcompleter
import time
import argparse
from rflib import *
no_xmit=True
def_fr=914950000
# fr=914944000
def_br=9124
invert_data=False
opt_debug = False
opt_verbose=True
def b_to_binstr(s, invert=False) :
"""
Convert string of "101010" to binary string
"""
ret = list()
if opt_debug :
s = s.translate(None, '-')
# a8 = [ s[i:i+8] for i in xrange(0, len(s), 8) ]
print >> sys.stderr, "s=", len(s), [s[x:x+8] for x in range(0, len(s), 8)]
sys.stderr.flush()
try :
if invert :
r = bytearray(int(s[x:x+8], 2) ^ 0xFF for x in range(0, len(s), 8))
else :
r = bytearray(int(s[x:x+8], 2) for x in range(0, len(s), 8))
except Exception, err :
print err
print "x =", x
traceback.print_exc(file=sys.stdout)
finally :
return r
def parse_args():
parser = argparse.ArgumentParser(
description='Send insteon commands from rfcat')
parser.add_argument('-v', '--verbose',
help='Increase debug verbosity', action='count')
parser.add_argument('-i', '--invert',
help='Invert data',
action='store_true', default=False)
parser.add_argument('-d', '--debug',
help='Log debugging messages',
action='store_true', default=False)
args = parser.parse_args()
return args
def init_rf( **kwargs ) :
global opt_debug
d = RfCat(debug=False)
# print "rf_configure"
# print "getMARCSTATE : ", d.getMARCSTATE()
# # d.strobeModeIDLE()
# print "getMARCSTATE : ", d.getMARCSTATE()
if "debug" in kwargs :
debug = 1
fr = kwargs.get("freq", def_fr)
d.setFreq( fr)
if opt_debug or opt_verbose:
print "setFreq"
print "Freq = ", d.getFreq()
d.setMdmChanBW(200000)
# d.setMdmChanBW(220000)
if opt_debug :
print "bandwidth ", d.getMdmChanBW()
br = kwargs.get("baud", def_br)
d.setMdmDRate(br)
if opt_debug or opt_verbose:
print "Baud \t", d.getMdmDRate()
d.setBSLimit(BSCFG_BS_LIMIT_12)
if opt_debug :
print "setBSLimit"
d.makePktFLEN(58)
#print "makePktFLEN"
# d.setMdmModulation(MOD_GFSK)
d.setMdmModulation(MOD_2FSK)
d.setMdmDeviatn(75000)
# d.setMdmSyncWord(0x6666)
# d.setPktPQT(1)
# d.setMdmSyncMode(4) # SYNCM_CARRIER)
# d.rf_configure(**ifo.__dict__)
d.setMaxPower()
if opt_debug :
print "RadioConfig : "
d.printRadioConfig()
return d
space_i="\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66"
space="\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99"
def send_pkt(d, s, n=1, r=1) :
"""
send_pkt(rfcat_obj, sring, xmit_count, repeat_count) :
"""
if opt_debug or opt_verbose :
print "send_pkt=", len(s), s
bs = b_to_binstr(s, invert_data)
# print "bs=", len(bs), bs
if opt_debug :
sbs = ''.join("{0:08b}".format(x) for x in bytearray(bs))
print "sbs =", len(sbs), sbs
# for i in xrange(n -1) :
# bs = bs + space_i + bs
# d.setModeRX()
for i in xrange(r) :
d.RFxmit( bs )
time.sleep(60/1000)
d.strobeModeIDLE()
def main() :
d = init_rf()
while True :
try :
sys.stdout.flush()
line = sys.stdin.readline()
# print len(line)
if line.startswith("#") :
continue
line = line.strip()
if opt_debug :
print "line : {:d} \"{:s}\"".format(len(line),line)
if len(line) == 1 :
continue
if not line :
if opt_verbose :
print "Not Line"
exit(0)
# dump_pkt(line)
send_pkt(d, line)
sys.stdout.flush()
except KeyboardInterrupt:
break
except Exception, err :
print "Send Failed :\n\t", err
if opt_debug :
print '-'*60
traceback.print_exc(file=sys.stdout)
print '-'*60
# raise
if __name__ == "__main__":
args = parse_args()
opt_debug = args.debug
opt_verbose = args.verbose
invert_data = args.invert
if opt_debug or opt_verbose :
print "opt_debug=", opt_debug, "opt_verbose", opt_verbose, "invert_data", invert_data
main()
exit(0)