psi: removed declaration and initialisation of standard psi structures in std.go as this is dangerous

This commit is contained in:
saxon 2019-01-07 17:13:50 +10:30
parent 561e603d96
commit ffc1af2cd4
5 changed files with 200 additions and 117 deletions

View File

@ -36,6 +36,89 @@ import (
"bitbucket.org/ausocean/av/stream/mts/psi" "bitbucket.org/ausocean/av/stream/mts/psi"
) )
// Some common manifestations of PSI
var (
// PSI struct to represent basic pat
StandardPat = psi.PSI{
Pf: 0x00,
Tid: 0x00,
Ssi: true,
Pb: false,
Sl: 0x0d,
Tss: &psi.TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &psi.PAT{
Pn: 0x01,
Pmpid: 0x1000,
},
},
}
// PSI struct to represent basic pmt without descriptors for time and location
StandardPmt = psi.PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x12,
Tss: &psi.TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &psi.PMT{
Pcrpid: 0x0100, // wrong
Pil: 0,
Essd: &psi.ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
// Std pmt with time and location descriptors, time and location fields are zeroed out
StandardPmtTimeLocation = psi.PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x3e,
Tss: &psi.TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &psi.PMT{
Pcrpid: 0x0100,
Pil: psi.PmtTimeLocationPil,
Pd: []psi.Desc{
{
Dt: psi.TimeDescTag,
Dl: psi.TimeDataSize,
Dd: make([]byte, psi.TimeDataSize),
},
{
Dt: psi.LocationDescTag,
Dl: psi.LocationDataSize,
Dd: make([]byte, psi.LocationDataSize),
},
},
Essd: &psi.ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
)
const ( const (
psiPacketSize = 184 psiPacketSize = 184
psiSendCount = 7 psiSendCount = 7
@ -57,8 +140,8 @@ func SetLocation(g string) {
} }
var ( var (
patTable = psi.StdPat.Bytes() patTable = StandardPat.Bytes()
pmtTable = psi.StdPmtTimeLocation.Bytes() pmtTable = StandardPmtTimeLocation.Bytes()
) )
const ( const (

View File

@ -36,7 +36,7 @@ import (
// TimeBytes takes a timestamp as an uint64 and converts to an 8 byte slice - // TimeBytes takes a timestamp as an uint64 and converts to an 8 byte slice -
// allows for updating of timestamp in pmt time descriptor. // allows for updating of timestamp in pmt time descriptor.
func TimeBytes(t uint64) []byte { func TimeBytes(t uint64) []byte {
var s [timeDataSize]byte var s [TimeDataSize]byte
binary.BigEndian.PutUint64(s[:], t) binary.BigEndian.PutUint64(s[:], t)
return s[:] return s[:]
} }
@ -44,7 +44,7 @@ func TimeBytes(t uint64) []byte {
// HasTime takes a psi as a byte slice and checks to see if it has a time descriptor // HasTime takes a psi as a byte slice and checks to see if it has a time descriptor
// - if so return nil, otherwise return error // - if so return nil, otherwise return error
func HasTime(p []byte) bool { func HasTime(p []byte) bool {
if p[timeTagIndx] == timeDescTag { if p[TimeTagIndx] == TimeDescTag {
return true return true
} }
return false return false
@ -53,7 +53,7 @@ func HasTime(p []byte) bool {
// HasLocation takes a psi as a byte slice and checks to see if it has a location descriptor // HasLocation takes a psi as a byte slice and checks to see if it has a location descriptor
// - if so return nil, otherwise return error // - if so return nil, otherwise return error
func HasLocation(p []byte) bool { func HasLocation(p []byte) bool {
if p[locationTagIndx] == locationDescTag { if p[LocationTagIndx] == LocationDescTag {
return true return true
} }
return false return false
@ -67,8 +67,8 @@ func UpdateTime(dst []byte, t uint64) error {
return errors.New("pmt does not have time descriptor, cannot update") return errors.New("pmt does not have time descriptor, cannot update")
} }
ts := TimeBytes(uint64(t)) ts := TimeBytes(uint64(t))
for i := range dst[timeDataIndx : timeDataIndx+timeDataSize] { for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
dst[i+timeDataIndx] = ts[i] dst[i+TimeDataIndx] = ts[i]
} }
updateCrc(dst) updateCrc(dst)
return nil return nil
@ -81,7 +81,7 @@ func TimeFrom(p []byte) (t uint64, err error) {
if !HasTime(p) { if !HasTime(p) {
return 0, errors.New("pmt does not have a time descriptor") return 0, errors.New("pmt does not have a time descriptor")
} }
t = binary.BigEndian.Uint64(p[timeDataIndx : timeDataIndx+timeDataSize]) t = binary.BigEndian.Uint64(p[TimeDataIndx : TimeDataIndx+TimeDataSize])
return t, nil return t, nil
} }
@ -92,7 +92,7 @@ func LocationFrom(p []byte) (g string, err error) {
if !HasLocation(p) { if !HasLocation(p) {
return "", errors.New("pmt does not have location descriptor") return "", errors.New("pmt does not have location descriptor")
} }
gBytes := p[locationDataIndx : locationDataIndx+locationDataSize] gBytes := p[LocationDataIndx : LocationDataIndx+LocationDataSize]
gBytes = bytes.Trim(gBytes, "\x00") gBytes = bytes.Trim(gBytes, "\x00")
g = string(gBytes) g = string(gBytes)
return g, nil return g, nil
@ -101,7 +101,7 @@ func LocationFrom(p []byte) (g string, err error) {
// LocationStrBytes take a string of location data and converts to a 32 byte slice - // 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 // easy update of slice representation of a pmt with location descriptor
func LocationStrBytes(s string) []byte { func LocationStrBytes(s string) []byte {
var b [locationDataSize]byte var b [LocationDataSize]byte
copy(b[:], s) copy(b[:], s)
return b[:] return b[:]
} }
@ -114,7 +114,7 @@ func UpdateLocation(d []byte, s string) error {
return errors.New("pmt does not location descriptor, cannot update") return errors.New("pmt does not location descriptor, cannot update")
} }
gb := LocationStrBytes(s) gb := LocationStrBytes(s)
copy(d[locationDataIndx:locationDataIndx+locationDataSize], gb) copy(d[LocationDataIndx:LocationDataIndx+LocationDataSize], gb)
updateCrc(d) updateCrc(d)
return nil return nil
} }

View File

@ -44,18 +44,18 @@ const (
// Consts relating to time description // Consts relating to time description
const ( const (
timeDescTag = 234 TimeDescTag = 234
timeTagIndx = 13 TimeTagIndx = 13
timeDataIndx = 15 TimeDataIndx = 15
timeDataSize = 8 // bytes, because time is stored in uint64 TimeDataSize = 8 // bytes, because time is stored in uint64
) )
// Consts relating to location description // Consts relating to location description
const ( const (
locationDescTag = 235 LocationDescTag = 235
locationTagIndx = 23 LocationTagIndx = 23
locationDataIndx = 25 LocationDataIndx = 25
locationDataSize = 32 // bytes LocationDataSize = 32 // bytes
) )
// Program specific information // Program specific information

View File

@ -31,6 +31,89 @@ import (
"testing" "testing"
) )
// Some common manifestations of PSI
var (
// PSI struct to represent basic pat
StandardPat = PSI{
Pf: 0x00,
Tid: 0x00,
Ssi: true,
Pb: false,
Sl: 0x0d,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PAT{
Pn: 0x01,
Pmpid: 0x1000,
},
},
}
// PSI struct to represent basic pmt without descriptors for time and location
StandardPmt = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x12,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100, // wrong
Pil: 0,
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
// Std pmt with time and location descriptors, time and location fields are zeroed out
StandardPmtTimeLocation = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x3e,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100,
Pil: PmtTimeLocationPil,
Pd: []Desc{
{
Dt: TimeDescTag,
Dl: TimeDataSize,
Dd: make([]byte, TimeDataSize),
},
{
Dt: LocationDescTag,
Dl: LocationDataSize,
Dd: make([]byte, LocationDataSize),
},
},
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
)
// Times as ints for testing // Times as ints for testing
const ( const (
tstTime1 = 1235367435 // 0x49a2360b tstTime1 = 1235367435 // 0x49a2360b
@ -60,11 +143,11 @@ var (
var ( var (
pmtTimeLocationBytesPart1 = []byte{ pmtTimeLocationBytesPart1 = []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,
timeDescTag, // Descriptor tag for timestamp TimeDescTag, // Descriptor tag for timestamp
timeDataSize, // Length of bytes to follow 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
locationDescTag, // Descriptor tag for location LocationDescTag, // Descriptor tag for location
locationDataSize, // Length of bytes to follow LocationDataSize, // Length of bytes to follow
} }
pmtTimeLocationBytesPart2 = []byte{ pmtTimeLocationBytesPart2 = []byte{
0x1b, 0xe1, 0x00, 0xf0, 0x00, 0x1b, 0xe1, 0x00, 0xf0, 0x00,
@ -75,8 +158,8 @@ var (
// Bytes representing pmt with tstTime1 // Bytes representing pmt with tstTime1
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,
timeDescTag, // Descriptor tag TimeDescTag, // Descriptor tag
timeDataSize, // Length of bytes to follow 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,8 +167,8 @@ var (
// Bytes representing pmt with tstTime 2 // Bytes representing pmt with tstTime 2
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,
timeDescTag, // Descriptor tag TimeDescTag, // Descriptor tag
timeDataSize, // Length of bytes to follow 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,8 +219,8 @@ var bytesTests = []struct {
Pil: 10, Pil: 10,
Pd: []Desc{ Pd: []Desc{
{ {
Dt: timeDescTag, Dt: TimeDescTag,
Dl: timeDataSize, Dl: TimeDataSize,
Dd: TimeBytes(tstTime1), Dd: TimeBytes(tstTime1),
}, },
}, },
@ -171,13 +254,13 @@ var bytesTests = []struct {
Pil: 10, Pil: 10,
Pd: []Desc{ Pd: []Desc{
{ {
Dt: timeDescTag, Dt: TimeDescTag,
Dl: timeDataSize, Dl: TimeDataSize,
Dd: TimeBytes(tstTime2), Dd: TimeBytes(tstTime2),
}, },
{ {
Dt: locationDescTag, Dt: LocationDescTag,
Dl: locationDataSize, Dl: LocationDataSize,
Dd: LocationStrBytes(locationTstStr1), Dd: LocationStrBytes(locationTstStr1),
}, },
}, },

View File

@ -27,90 +27,7 @@ LICENSE
package psi package psi
const ( const (
pmtTimeLocationPil = 44 PmtTimeLocationPil = 44
)
// Some common manifestations of PSI
var (
// PSI struct to represent basic pat
StandardPat = PSI{
Pf: 0x00,
Tid: 0x00,
Ssi: true,
Pb: false,
Sl: 0x0d,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PAT{
Pn: 0x01,
Pmpid: 0x1000,
},
},
}
// PSI struct to represent basic pmt without descriptors for time and location
StandardPmt = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x12,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100, // wrong
Pil: 0,
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
// Std pmt with time and location descriptors, time and location fields are zeroed out
StandardPmtTimeLocation = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x3e,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100,
Pil: pmtTimeLocationPil,
Pd: []Desc{
{
Dt: timeDescTag,
Dl: timeDataSize,
Dd: make([]byte, timeDataSize),
},
{
Dt: locationDescTag,
Dl: locationDataSize,
Dd: make([]byte, locationDataSize),
},
},
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
},
},
}
) )
// Std PSI in bytes form // Std PSI in bytes form