av/stream/mts/psi/op.go

114 lines
3.5 KiB
Go
Raw Normal View History

2018-12-12 02:48:05 +03:00
/*
NAME
op.go
2018-12-12 02:48:05 +03:00
DESCRIPTION
op.go provides functionality for editing and reading bytes slices
directly in order to insert/read timestamp and gps data in psi.
AUTHOR
Saxon Milton <saxon@ausocean.org>
LICENSE
op.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
2018-12-11 09:46:26 +03:00
package psi
import (
"encoding/binary"
"errors"
)
2018-12-12 15:47:28 +03:00
// 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)
2018-12-12 15:47:28 +03:00
binary.BigEndian.PutUint64(s[:], t)
2018-12-11 09:46:26 +03:00
return s
}
2018-12-12 15:47:28 +03:00
// ChkTime takes a psi as a byte slice and checks to see if it has a time descriptor
// - if so return nil, otherwise return error
func ChkTime(p []byte) error {
if p[timeTagIndx] != timeDescTag {
2018-12-11 09:46:26 +03:00
return errors.New("PSI does not contain a time descriptor, cannot update")
}
return nil
}
2018-12-12 15:47:28 +03:00
// ChkGps takes a psi as a byte slice and checks to see if it has a gps descriptor
// - 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 nil
}
2018-12-12 15:47:28 +03:00
// 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 uint64) error {
2018-12-12 15:47:28 +03:00
err := ChkTime(d)
2018-12-11 09:46:26 +03:00
if err != nil {
return err
}
ts := TimeBytes(uint64(t))
for i := range d[timeDataIndx : timeDataIndx+timeDataSize] {
d[i+timeDataIndx] = ts[i]
2018-12-11 09:46:26 +03:00
}
return nil
}
2018-12-12 15:47:28 +03:00
// TimeOf takes a byte slice representation of a psi-pmt and extracts it's
// 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)
2018-12-11 09:46:26 +03:00
if err != nil {
return 0, err
}
2018-12-12 15:47:28 +03:00
// TODO: could probably write a byte slice to int func, so that this would
// 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)
2018-12-11 09:46:26 +03:00
}
return t, nil
}
2018-12-12 15:47:28 +03:00
// 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) {
out = make([]byte, gpsDataSize)
copy(out, []byte(l))
return
}
2018-12-12 15:47:28 +03:00
// 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 {
2018-12-12 15:47:28 +03:00
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
}