/
r900.go
291 lines (236 loc) · 7.48 KB
/
r900.go
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
// RTLAMR - An rtl-sdr receiver for smart meters operating in the 900MHz ISM band.
// Copyright (C) 2014 Douglas Hall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package r900
import (
"bytes"
"fmt"
"math"
"strconv"
"github.com/bemasher/rtlamr/decode"
"github.com/bemasher/rtlamr/parse"
"github.com/bemasher/rtlamr/r900/gf"
)
const (
PayloadSymbols = 42
)
func init() {
parse.Register("r900", NewParser)
}
func NewPacketConfig(symbolLength int) (cfg decode.PacketConfig) {
cfg.CenterFreq = 912380000
cfg.DataRate = 32768
cfg.SymbolLength = symbolLength
cfg.PreambleSymbols = 32
cfg.PacketSymbols = 116
cfg.Preamble = "00000000000000001110010101100100"
return
}
type Parser struct {
decode.Decoder
field *gf.Field
rsBuf [31]byte
csum []float64
filtered [][3]float64
quantized []byte
}
func NewParser(symbolLength, decimation int) parse.Parser {
p := new(Parser)
p.Decoder = decode.NewDecoder(NewPacketConfig(symbolLength), decimation)
// GF of order 32, polynomial 37, generator 2.
p.field = gf.NewField(32, 37, 2)
p.csum = make([]float64, p.Decoder.DecCfg.BufferLength+1)
p.filtered = make([][3]float64, p.Decoder.DecCfg.BufferLength)
p.quantized = make([]byte, p.Decoder.DecCfg.BufferLength)
return p
}
func (p Parser) Dec() decode.Decoder {
return p.Decoder
}
func (p *Parser) Cfg() *decode.PacketConfig {
return &p.Decoder.Cfg
}
// Perform matched filtering.
func (p Parser) Filter() {
// This function computes the convolution of each symbol kernel with the
// signal. The naive approach requires for each symbol to calculate the
// summation of samples between a pair of indices.
// 0 |--------|
// 1 |--------|
// 2 |--------|
// 3 |--------|
// To avoid redundant calculations we compute the cumulative sum of the
// signal. This reduces each summation to the difference between the two
// indices of the cumulative sum.
var sum float64
for idx, v := range p.Decoder.Signal {
sum += v
p.csum[idx+1] = sum
}
// There are six symbols, composed of three base symbols and their bitwise
// inversions. Compute the convolution of each base symbol with the
// signal.
// 1100 -> 0011
// 1010 -> 0101
// 1001 -> 0110
// This is basically unreadable because of a lot of algebraic
// simplification but is necessary for efficiency.
for idx := 0; idx < p.Decoder.DecCfg.BufferLength-p.Decoder.DecCfg.SymbolLength*4; idx++ {
c0 := p.csum[idx]
c1 := p.csum[idx+p.Decoder.DecCfg.SymbolLength] * 2
c2 := p.csum[idx+p.Decoder.DecCfg.SymbolLength*2] * 2
c3 := p.csum[idx+p.Decoder.DecCfg.SymbolLength*3] * 2
c4 := p.csum[idx+p.Decoder.DecCfg.SymbolLength*4]
p.filtered[idx][0] = c2 - c4 - c0 // 1100
p.filtered[idx][1] = c1 - c2 + c3 - c4 - c0 // 1010
p.filtered[idx][2] = c1 - c3 + c4 - c0 // 1001
}
}
// Determine the symbol that exists at each sample of the signal.
func (p Parser) Quantize() {
// 0 0011, 3 1100
// 1 0101, 4 1010
// 2 0110, 5 1001
for idx, vec := range p.filtered {
argmax := byte(0)
max := math.Abs(vec[0])
// If v1 is larger than v0, update max and argmax.
if v1 := math.Abs(vec[1]); v1 > max {
max = v1
argmax = 1
}
// If v2 is larger than the greater of v1 or v0, update max and argmax.
if v2 := math.Abs(vec[2]); v2 > max {
max = v2
argmax = 2
}
// Set the output symbol index.
p.quantized[idx] = argmax
// If the sign is negative, jump to the index of the inverted symbol.
if vec[argmax] > 0 {
p.quantized[idx] += 3
}
}
}
// Given a list of indices the preamble exists at, decode and parse a message.
func (p Parser) Parse(indices []int) (msgs []parse.Message) {
p.Filter()
p.Quantize()
preambleLength := p.Decoder.DecCfg.PreambleLength
symbolLength := p.Decoder.DecCfg.SymbolLength
symbols := make([]byte, 21)
zeros := make([]byte, 5)
seen := make(map[string]bool)
for _, preambleIdx := range indices {
if preambleIdx > p.Decoder.DecCfg.BlockSize {
break
}
payloadIdx := preambleIdx + preambleLength
var digits string
for idx := 0; idx < PayloadSymbols*4*p.Decoder.DecCfg.SymbolLength; idx += symbolLength * 4 {
qIdx := payloadIdx + idx
digits += strconv.Itoa(int(p.quantized[qIdx]))
}
var (
bits string
badSymbol bool
)
for idx := 0; idx < len(digits); idx += 2 {
symbol, _ := strconv.ParseInt(digits[idx:idx+2], 6, 32)
if symbol > 31 {
badSymbol = true
break
}
symbols[idx>>1] = byte(symbol)
bits += fmt.Sprintf("%05b", symbol)
}
if badSymbol || seen[bits] {
continue
}
seen[bits] = true
copy(p.rsBuf[:], symbols[:16])
copy(p.rsBuf[26:], symbols[16:])
syndromes := p.field.Syndrome(p.rsBuf[:], 5, 29)
if !bytes.Equal(zeros, syndromes) {
continue
}
id, _ := strconv.ParseUint(bits[:32], 2, 32)
unkn1, _ := strconv.ParseUint(bits[32:40], 2, 8)
nouse, _ := strconv.ParseUint(bits[40:46], 2, 6)
backflow, _ := strconv.ParseUint(bits[46:48], 2, 2)
consumption, _ := strconv.ParseUint(bits[48:72], 2, 24)
unkn3, _ := strconv.ParseUint(bits[72:74], 2, 2)
leak, _ := strconv.ParseUint(bits[74:78], 2, 4)
leaknow, _ := strconv.ParseUint(bits[78:80], 2, 2)
var r900 R900
r900.ID = uint32(id)
r900.Unkn1 = uint8(unkn1)
r900.NoUse = uint8(nouse)
r900.BackFlow = uint8(backflow)
r900.Consumption = uint32(consumption)
r900.Unkn3 = uint8(unkn3)
r900.Leak = uint8(leak)
r900.LeakNow = uint8(leaknow)
copy(r900.checksum[:], symbols[16:])
msgs = append(msgs, r900)
}
return
}
type R900 struct {
ID uint32 `xml:",attr"` // 32 bits
Unkn1 uint8 `xml:",attr"` // 8 bits
NoUse uint8 `xml:",attr"` // 6 bits, day bins of no use
BackFlow uint8 `xml:",attr"` // 2 bits, backflow past 35d hi/lo
Consumption uint32 `xml:",attr"` // 24 bits
Unkn3 uint8 `xml:",attr"` // 2 bits
Leak uint8 `xml:",attr"` // 4 bits, day bins of leak
LeakNow uint8 `xml:",attr"` // 2 bits, leak past 24h hi/lo
checksum [5]byte
}
func (r900 R900) MsgType() string {
return "R900"
}
func (r900 R900) MeterID() uint32 {
return r900.ID
}
func (r900 R900) MeterType() uint8 {
return r900.Unkn1
}
func (r900 R900) Checksum() []byte {
return r900.checksum[:]
}
func (r900 R900) String() string {
return fmt.Sprintf("{ID:%10d Unkn1:0x%02X NoUse:%2d BackFlow:%1d Consumption:%8d Unkn3:0x%02X Leak:%2d LeakNow:%1d}",
r900.ID,
r900.Unkn1,
r900.NoUse,
r900.BackFlow,
r900.Consumption,
r900.Unkn3,
r900.Leak,
r900.LeakNow,
)
}
func (r900 R900) Record() (r []string) {
r = append(r, strconv.FormatUint(uint64(r900.ID), 10))
r = append(r, strconv.FormatUint(uint64(r900.Unkn1), 10))
r = append(r, strconv.FormatUint(uint64(r900.NoUse), 10))
r = append(r, strconv.FormatUint(uint64(r900.BackFlow), 10))
r = append(r, strconv.FormatUint(uint64(r900.Consumption), 10))
r = append(r, strconv.FormatUint(uint64(r900.Unkn3), 10))
r = append(r, strconv.FormatUint(uint64(r900.Leak), 10))
r = append(r, strconv.FormatUint(uint64(r900.LeakNow), 10))
return
}