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) {
s = make([]byte, timeSize)
s = make([]byte, timeDataSize)
binary.BigEndian.PutUint64(s[:], time)
return s
}
@ -45,6 +45,13 @@ func chkTime(d []byte) error {
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
func UpdateTime(d []byte, t int) error {
err := chkTime(d)
@ -52,8 +59,8 @@ func UpdateTime(d []byte, t int) error {
return err
}
ts := TimeBytes(uint64(t))
for i := range d[timeIndx : timeIndx+8] {
d[i+timeIndx] = ts[i]
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
d[i+timeDataIndx] = ts[i]
}
return nil
}
@ -63,14 +70,27 @@ func TimeOf(d []byte) (t uint64, err error) {
if err != nil {
return 0, err
}
for i := range d[timeIndx : timeIndx+8] {
t |= uint64(d[i+timeIndx]) << uint(56-i*8)
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
t |= uint64(d[i+timeDataIndx]) << uint(56-i*timeDataSize)
}
return t, nil
}
func GpsStrBytes(l string) (out []byte) {
out = make([]byte, gpsSize)
out = make([]byte, gpsDataSize)
copy(out, []byte(l))
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 (
timeDescTag = 234
timeTagIndx = 13
timeIndx = 15
timeSize = 8
timeDataIndx = 15
timeDataSize = 8
)
// Consts relating to gps description
const (
gpsDescTag = 235
gpsTagIndx = 23
gpsIndx = 25
gpsSize = 32 // bytes
gpsDataIndx = 25
gpsDataSize = 32 // bytes
)
// Program specific information

View File

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