psi: wrote test for gpsUpdate - appears to be working

This commit is contained in:
saxon 2018-12-12 17:02:03 +10:30
parent 3cf6c00991
commit f320746b5d
3 changed files with 43 additions and 23 deletions

View File

@ -33,7 +33,7 @@ import (
) )
func TimeBytes(time uint64) (s []byte) { func TimeBytes(time uint64) (s []byte) {
s = make([]byte, timeSize) s = make([]byte, timeDataSize)
binary.BigEndian.PutUint64(s[:], time) binary.BigEndian.PutUint64(s[:], time)
return s return s
} }
@ -45,6 +45,13 @@ func chkTime(d []byte) error {
return nil return nil
} }
func chkGps(d []byte) error {
if d[gpsTagIndx] != gpsDescTag {
return errors.New("PSI does not contain a gps descriptor, cannot update")
}
return nil
}
// Updatetime // Updatetime
func UpdateTime(d []byte, t int) error { func UpdateTime(d []byte, t int) error {
err := chkTime(d) err := chkTime(d)
@ -52,8 +59,8 @@ func UpdateTime(d []byte, t int) error {
return err return err
} }
ts := TimeBytes(uint64(t)) ts := TimeBytes(uint64(t))
for i := range d[timeIndx : timeIndx+8] { for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
d[i+timeIndx] = ts[i] d[i+timeDataIndx] = ts[i]
} }
return nil return nil
} }
@ -63,14 +70,27 @@ func TimeOf(d []byte) (t uint64, err error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
for i := range d[timeIndx : timeIndx+8] { for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
t |= uint64(d[i+timeIndx]) << uint(56-i*8) t |= uint64(d[i+timeDataIndx]) << uint(56-i*timeDataSize)
} }
return t, nil return t, nil
} }
func GpsStrBytes(l string) (out []byte) { func GpsStrBytes(l string) (out []byte) {
out = make([]byte, gpsSize) out = make([]byte, gpsDataSize)
copy(out, []byte(l)) copy(out, []byte(l))
return return
} }
func UpdateGps(d []byte, s string) error {
// First check if there is a GPS descriptor
err := chkGps(d)
if err != nil {
return err
}
gb := GpsStrBytes(s)
for i := range d[gpsDataIndx : gpsDataIndx+gpsDataSize] {
d[i+gpsDataIndx] = gb[i]
}
return nil
}

View File

@ -51,16 +51,16 @@ const (
const ( const (
timeDescTag = 234 timeDescTag = 234
timeTagIndx = 13 timeTagIndx = 13
timeIndx = 15 timeDataIndx = 15
timeSize = 8 timeDataSize = 8
) )
// Consts relating to gps description // Consts relating to gps description
const ( const (
gpsDescTag = 235 gpsDescTag = 235
gpsTagIndx = 23 gpsTagIndx = 23
gpsIndx = 25 gpsDataIndx = 25
gpsSize = 32 // bytes gpsDataSize = 32 // bytes
) )
// Program specific information // Program specific information

View File

@ -60,10 +60,10 @@ var (
pmtTimeGpsBytesPart1 = []byte{ pmtTimeGpsBytesPart1 = []byte{
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a, 0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
byte(timeDescTag), // Descriptor tag for timestamp byte(timeDescTag), // Descriptor tag for timestamp
byte(timeSize), // Length of bytes to follow byte(timeDataSize), // Length of bytes to follow
0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // Timestamp data 0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // Timestamp data
byte(gpsDescTag), // Descriptor tag for gps byte(gpsDescTag), // Descriptor tag for gps
byte(gpsSize), // Length of bytes to follow byte(gpsDataSize), // Length of bytes to follow
} }
pmtTimeGpsBytesPart2 = []byte{ pmtTimeGpsBytesPart2 = []byte{
0x1b, 0xe1, 0x00, 0xf0, 0x00, 0x1b, 0xe1, 0x00, 0xf0, 0x00,
@ -75,7 +75,7 @@ var (
pmtTimeBytes1 = []byte{ pmtTimeBytes1 = []byte{
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a, 0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
byte(timeDescTag), // Descriptor tag byte(timeDescTag), // Descriptor tag
byte(timeSize), // Length of bytes to follow byte(timeDataSize), // Length of bytes to follow
0x00, 0x00, 0x00, 0x00, 0x49, 0xA2, 0x36, 0x0B, // timestamp 0x00, 0x00, 0x00, 0x00, 0x49, 0xA2, 0x36, 0x0B, // timestamp
0x1b, 0xe1, 0x00, 0xf0, 0x00, 0x1b, 0xe1, 0x00, 0xf0, 0x00,
} }
@ -84,7 +84,7 @@ var (
pmtTimeBytes2 = []byte{ pmtTimeBytes2 = []byte{
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a, 0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
byte(timeDescTag), // Descriptor tag byte(timeDescTag), // Descriptor tag
byte(timeSize), // Length of bytes to follow byte(timeDataSize), // Length of bytes to follow
0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // timestamp 0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // timestamp
0x1b, 0xe1, 0x00, 0xf0, 0x00, 0x1b, 0xe1, 0x00, 0xf0, 0x00,
} }
@ -136,7 +136,7 @@ var bytesTests = []struct {
Pd: []Desc{ Pd: []Desc{
Desc{ Desc{
Dt: byte(timeDescTag), Dt: byte(timeDescTag),
Dl: byte(timeSize), Dl: byte(timeDataSize),
Dd: TimeBytes(tstTime1), Dd: TimeBytes(tstTime1),
}, },
}, },
@ -171,12 +171,12 @@ var bytesTests = []struct {
Pd: []Desc{ Pd: []Desc{
Desc{ Desc{
Dt: byte(timeDescTag), Dt: byte(timeDescTag),
Dl: byte(timeSize), Dl: byte(timeDataSize),
Dd: TimeBytes(tstTime2), Dd: TimeBytes(tstTime2),
}, },
Desc{ Desc{
Dt: byte(gpsDescTag), Dt: byte(gpsDescTag),
Dl: byte(gpsSize), Dl: byte(gpsDataSize),
Dd: GpsStrBytes(gpsTstStr1), Dd: GpsStrBytes(gpsTstStr1),
}, },
}, },
@ -239,7 +239,7 @@ func TestTimeGet(t *testing.T) {
func TestGpsUpdate(t *testing.T) { func TestGpsUpdate(t *testing.T) {
cpy := make([]byte, len(pmtTimeGpsBytes1)) cpy := make([]byte, len(pmtTimeGpsBytes1))
copy(cpy, pmtTimeGpsBytes1) copy(cpy, pmtTimeGpsBytes1)
err := UpdateGps(cpy, tstTime2) err := UpdateGps(cpy, gpsTstStr2)
if err != nil { if err != nil {
t.Errorf("Update time returned err: %v", err) t.Errorf("Update time returned err: %v", err)
} }