/
header.go
76 lines (55 loc) · 1.7 KB
/
header.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
package demodulation
import (
"fmt"
"os"
"strconv"
"github.com/Hunter-Dolan/midrange/fec"
"github.com/Hunter-Dolan/midrange/options"
"github.com/Hunter-Dolan/midrange/utils"
)
type header struct {
packet
demodulator *Demodulator
transmissionHash *fec.DataConfidenceCollection
dataLength int
}
func (h *header) correct() bool {
h.data.HashVerifier = func(data []bool, hash []bool) bool {
valid := fec.SHA1DirectBinVerifier(data, hash)
if valid {
intLength := options.HeaderDataLengthIntegerBitLength
dataLengthBool := data[:intLength]
dataLengthBin := utils.BoolToBin(dataLengthBool)
dataLengthDec, _ := strconv.ParseInt(dataLengthBin, 2, 64)
dataLength := int(dataLengthDec)
approxBitsMax := ((h.demodulator.dataLength - options.HeaderLengthBits) / options.PacketTotalLength) * options.PacketDataLength
approxBitsMin := approxBitsMax - h.demodulator.carrierCount
if dataLength <= approxBitsMax && dataLength > approxBitsMin {
return true
}
}
return false
}
correct := h.data.CorrectData(h.hash, true)
if !correct {
correct = h.data.CorrectData(h.hash, false)
if !correct {
fmt.Println("Unable to decode header")
os.Exit(0)
}
}
return correct
}
func (h *header) parse() {
boolData := h.data.Data()
intLength := options.HeaderDataLengthIntegerBitLength
checkLength := options.HeaderTransmissionChecksumLength
dataLengthBool := boolData[:intLength]
dataLengthBin := utils.BoolToBin(dataLengthBool)
dataLengthDec, _ := strconv.ParseInt(dataLengthBin, 2, 64)
h.dataLength = int(dataLengthDec)
h.transmissionHash = h.data.Slice(intLength, checkLength)
}
func (p *header) Length() int {
return options.HeaderLengthBits + options.PacketChecksumLength
}