av/stream/mts/psi/op.go

77 lines
1.7 KiB
Go
Raw Normal View History

2018-12-12 02:48:05 +03:00
/*
NAME
op.go
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"
)
func TimeBytes(time uint64) (s []byte) {
s = make([]byte, timeSize)
binary.BigEndian.PutUint64(s[:], time)
2018-12-11 09:46:26 +03:00
return s
}
func chkTime(d []byte) error {
if d[timeTagIndx] != timeDescTag {
2018-12-11 09:46:26 +03:00
return errors.New("PSI does not contain a time descriptor, cannot update")
}
return nil
}
// Updatetime
func UpdateTime(d []byte, t int) error {
err := chkTime(d)
if err != nil {
return err
}
ts := TimeBytes(uint64(t))
2018-12-11 09:46:26 +03:00
for i := range d[timeIndx : timeIndx+8] {
d[i+timeIndx] = ts[i]
}
return nil
}
func TimeOf(d []byte) (t uint64, err error) {
err = chkTime(d)
if err != nil {
return 0, err
}
for i := range d[timeIndx : timeIndx+8] {
t |= uint64(d[i+timeIndx]) << uint(56-i*8)
}
return t, nil
}
func GpsStrBytes(l string) (out []byte) {
out = make([]byte, gpsSize)
copy(out, []byte(l))
return
}