psi: improved commenting in op.go

This commit is contained in:
saxon 2018-12-12 23:17:28 +10:30
parent fe2c5d1033
commit 2ca393c276
1 changed files with 31 additions and 14 deletions

View File

@ -32,29 +32,37 @@ import (
"errors" "errors"
) )
func TimeBytes(time uint64) (s []byte) { // timeBytes takes a timestamp as an uint64 and converts to an 8 byte slice -
// allows for updating of timestamp in pmt time descriptor.
func TimeBytes(t uint64) (s []byte) {
s = make([]byte, timeDataSize) s = make([]byte, timeDataSize)
binary.BigEndian.PutUint64(s[:], time) binary.BigEndian.PutUint64(s[:], t)
return s return s
} }
func chkTime(d []byte) error { // ChkTime takes a psi as a byte slice and checks to see if it has a time descriptor
if d[timeTagIndx] != timeDescTag { // - if so return nil, otherwise return error
func ChkTime(p []byte) error {
if p[timeTagIndx] != timeDescTag {
return errors.New("PSI does not contain a time descriptor, cannot update") return errors.New("PSI does not contain a time descriptor, cannot update")
} }
return nil return nil
} }
func chkGps(d []byte) error { // ChkGps takes a psi as a byte slice and checks to see if it has a gps descriptor
if d[gpsTagIndx] != gpsDescTag { // - if so return nil, otherwise return error
func ChkGps(p []byte) error {
if p[gpsTagIndx] != gpsDescTag {
return errors.New("PSI does not contain a gps descriptor, cannot update") return errors.New("PSI does not contain a gps descriptor, cannot update")
} }
return nil return nil
} }
// Updatetime // UpdateTime takes the byte slice representation of a psi-pmt as well as a time
// as an integer and attempts to update the time descriptor in the pmt with the
// given time if the time descriptor exists, otherwise an error is returned
func UpdateTime(d []byte, t int) error { func UpdateTime(d []byte, t int) error {
err := chkTime(d) err := ChkTime(d)
if err != nil { if err != nil {
return err return err
} }
@ -65,26 +73,35 @@ func UpdateTime(d []byte, t int) error {
return nil return nil
} }
func TimeOf(d []byte) (t uint64, err error) { // TimeOf takes a byte slice representation of a psi-pmt and extracts it's
err = chkTime(d) // timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil
// if it does not exist
func TimeOf(p []byte) (t uint64, err error) {
err = ChkTime(p)
if err != nil { if err != nil {
return 0, err return 0, err
} }
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] { // TODO: could probably write a byte slice to int func, so that this would
t |= uint64(d[i+timeDataIndx]) << uint(56-i*timeDataSize) // be replaced by extraction of time data slice and then call to conversion func
for i := range p[timeDataIndx : timeDataIndx+timeDataSize] {
t |= uint64(p[i+timeDataIndx]) << uint(56-i*timeDataSize)
} }
return t, nil return t, nil
} }
// GpsStrBytes take a string of gps data and converts to a 32 byte slice -
// easy update of slice representation of a pmt with gps descriptor
func GpsStrBytes(l string) (out []byte) { func GpsStrBytes(l string) (out []byte) {
out = make([]byte, gpsDataSize) out = make([]byte, gpsDataSize)
copy(out, []byte(l)) copy(out, []byte(l))
return return
} }
// UpdateGps takes a byte slice representation of a psi-pmt containing a gps
// descriptor and attempts to update the gps data value with the passed string.
// If the psi does not contain a gps descriptor, and error is returned.
func UpdateGps(d []byte, s string) error { func UpdateGps(d []byte, s string) error {
// First check if there is a GPS descriptor err := ChkGps(d)
err := chkGps(d)
if err != nil { if err != nil {
return err return err
} }