mts: got rid of LocationStrBytes func

This commit is contained in:
saxon 2019-01-18 13:21:35 +10:30
parent a58a784a16
commit 98d89a4e4e
2 changed files with 32 additions and 22 deletions

View File

@ -99,14 +99,6 @@ func LocationFrom(p []byte) (g string, err error) {
return g, nil
}
// LocationStrBytes take a string of location data and converts to a 32 byte slice -
// easy update of slice representation of a pmt with location descriptor
func LocationStrBytes(s string) []byte {
var b [LocationDataSize]byte
copy(b[:], s)
return b[:]
}
// UpdateLocation takes a byte slice representation of a psi-pmt containing a location
// descriptor and attempts to update the location data value with the passed string.
// If the psi does not contain a location descriptor, and error is returned.
@ -114,8 +106,15 @@ func UpdateLocation(d []byte, s string) error {
if !HasLocation(d) {
return errors.New("pmt does not location descriptor, cannot update")
}
gb := LocationStrBytes(s)
copy(d[LocationDataIndx:LocationDataIndx+LocationDataSize], gb)
sb := []byte(s)
locBytes := d[LocationDataIndx : LocationDataIndx+LocationDataSize]
for i, _ := range locBytes {
if i < len(sb) {
locBytes[i] = sb[i]
} else {
locBytes[i] = 0x00
}
}
updateCrc(d)
return nil
}

View File

@ -123,9 +123,20 @@ const (
// GPS string for testing
// TODO: make these realistic
const (
locationTstStr1 = "$GPGGA,123519,4807.038,N,01131.0"
locationTstStr2 = "$GPGGA,183710,4902.048,N,02171.0"
var (
locationTstStr1 = [LocationDataSize]byte{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
}
locationTstStr2 = [LocationDataSize]byte{
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
}
)
// err message
@ -175,10 +186,10 @@ var (
}
// Bytes representing pmt with time1 and location1
pmtTimeLocationBytes1 = buildPmtTimeLocationBytes(locationTstStr1)
pmtTimeLocationBytes1 = buildPmtTimeLocationBytes(locationTstStr1[:])
// bytes representing pmt with with time1 and location 2
pmtTimeLocationBytes2 = buildPmtTimeLocationBytes(locationTstStr2)
pmtTimeLocationBytes2 = buildPmtTimeLocationBytes(locationTstStr2[:])
)
// bytesTests contains data for testing the Bytes() funcs for the PSI data struct
@ -262,7 +273,7 @@ var bytesTests = []struct {
{
Dt: LocationDescTag,
Dl: LocationDataSize,
Dd: LocationStrBytes(locationTstStr1),
Dd: locationTstStr1[:],
},
},
Essd: &ESSD{
@ -273,7 +284,7 @@ var bytesTests = []struct {
},
},
},
want: buildPmtTimeLocationBytes(locationTstStr1),
want: buildPmtTimeLocationBytes(locationTstStr1[:]),
},
}
@ -326,7 +337,7 @@ func TestTimeGet(t *testing.T) {
// TestLocationGet checks that we can correctly get location data from a pmt table
func TestLocationGet(t *testing.T) {
pb := standardPmtTimeLocation.Bytes()
err := UpdateLocation(pb, locationTstStr1)
err := UpdateLocation(pb, string(locationTstStr1[:]))
if err != nil {
t.Errorf("Error for TestLocationGet UpdateLocation(pb, locationTstStr1): %v", err)
}
@ -334,7 +345,7 @@ func TestLocationGet(t *testing.T) {
if err != nil {
t.Errorf("Error for TestLocationGet LocationOf(pb): %v", err)
}
if g != locationTstStr1 {
if g != string(locationTstStr1[:]) {
t.Errorf(errCmp, "TestLocationGet", locationTstStr1, g)
}
}
@ -344,7 +355,7 @@ func TestLocationUpdate(t *testing.T) {
cpy := make([]byte, len(pmtTimeLocationBytes1))
copy(cpy, pmtTimeLocationBytes1)
cpy = addCrc(cpy)
err := UpdateLocation(cpy, locationTstStr2)
err := UpdateLocation(cpy, string(locationTstStr2[:]))
cpy = cpy[:len(cpy)-4]
if err != nil {
t.Errorf("Update time returned err: %v", err)
@ -366,7 +377,7 @@ func TestTrim(t *testing.T) {
// buildPmtTimeLocationBytes is a helper function to help construct the byte slices
// for pmts with time and location, as the location data field is 32 bytes, i.e. quite large
// to type out
func buildPmtTimeLocationBytes(tstStr string) []byte {
func buildPmtTimeLocationBytes(tstStr []byte) []byte {
return append(append(append(make([]byte, 0), pmtTimeLocationBytesPart1...),
LocationStrBytes(tstStr)...), pmtTimeLocationBytesPart2...)
tstStr...), pmtTimeLocationBytesPart2...)
}