2018-12-12 02:48:05 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-12-13 04:52:06 +03:00
|
|
|
op.go
|
2018-12-12 02:48:05 +03:00
|
|
|
DESCRIPTION
|
|
|
|
op.go provides functionality for editing and reading bytes slices
|
2018-12-14 08:32:47 +03:00
|
|
|
directly in order to insert/read timestamp and location data in psi.
|
2018-12-12 02:48:05 +03:00
|
|
|
|
|
|
|
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 (
|
2018-12-14 06:16:10 +03:00
|
|
|
"bytes"
|
2018-12-11 09:46:26 +03:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2018-12-13 07:39:23 +03:00
|
|
|
"hash/crc32"
|
|
|
|
"math/bits"
|
2018-12-11 09:46:26 +03:00
|
|
|
)
|
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// TimeBytes takes a timestamp as an uint64 and converts to an 8 byte slice -
|
2018-12-12 15:47:28 +03:00
|
|
|
// allows for updating of timestamp in pmt time descriptor.
|
2018-12-14 08:52:48 +03:00
|
|
|
func TimeBytes(t uint64) []byte {
|
|
|
|
var s [timeDataSize]byte
|
2018-12-12 15:47:28 +03:00
|
|
|
binary.BigEndian.PutUint64(s[:], t)
|
2018-12-14 08:52:48 +03:00
|
|
|
return s[:]
|
2018-12-11 09:46:26 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// HasTime takes a psi as a byte slice and checks to see if it has a time descriptor
|
2018-12-12 15:47:28 +03:00
|
|
|
// - if so return nil, otherwise return error
|
2018-12-14 08:41:45 +03:00
|
|
|
func HasTime(p []byte) error {
|
2018-12-12 15:47:28 +03:00
|
|
|
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-14 09:16:36 +03:00
|
|
|
// HasLocation takes a psi as a byte slice and checks to see if it has a location descriptor
|
2018-12-12 15:47:28 +03:00
|
|
|
// - if so return nil, otherwise return error
|
2018-12-14 08:36:01 +03:00
|
|
|
func HasLocation(p []byte) error {
|
2018-12-14 08:32:47 +03:00
|
|
|
if p[locationTagIndx] != locationDescTag {
|
|
|
|
return errors.New("PSI does not contain a location descriptor, cannot update")
|
2018-12-12 09:32:03 +03:00
|
|
|
}
|
|
|
|
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
|
2018-12-14 08:51:56 +03:00
|
|
|
func UpdateTime(dst []byte, t uint64) error {
|
|
|
|
err := HasTime(dst)
|
2018-12-11 09:46:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
ts := TimeBytes(uint64(t))
|
2018-12-14 08:51:56 +03:00
|
|
|
for i := range dst[timeDataIndx : timeDataIndx+timeDataSize] {
|
|
|
|
dst[i+timeDataIndx] = ts[i]
|
2018-12-11 09:46:26 +03:00
|
|
|
}
|
2018-12-14 08:51:56 +03:00
|
|
|
dst = updateCrc(dst)
|
2018-12-11 09:46:26 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// TimeFrom takes a byte slice representation of a psi-pmt and extracts it's
|
2018-12-12 15:47:28 +03:00
|
|
|
// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil
|
|
|
|
// if it does not exist
|
2018-12-14 08:36:01 +03:00
|
|
|
func TimeFrom(p []byte) (t uint64, err error) {
|
2018-12-14 08:41:45 +03:00
|
|
|
err = HasTime(p)
|
2018-12-11 09:46:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-12-14 09:01:13 +03:00
|
|
|
t = binary.BigEndian.Uint64(p[timeDataIndx : timeDataIndx+timeDataSize])
|
2018-12-11 09:46:26 +03:00
|
|
|
return t, nil
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// LocationFrom takes a byte slice representation of a psi-pmt and extracts it's
|
2018-12-14 03:45:49 +03:00
|
|
|
// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil
|
|
|
|
// if it does not exist
|
2018-12-14 08:36:01 +03:00
|
|
|
func LocationFrom(p []byte) (g string, err error) {
|
|
|
|
err = HasLocation(p)
|
2018-12-14 03:45:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-12-14 08:32:47 +03:00
|
|
|
gBytes := p[locationDataIndx : locationDataIndx+locationDataSize]
|
2018-12-14 06:16:10 +03:00
|
|
|
gBytes = bytes.Trim(gBytes, "\x00")
|
2018-12-14 03:45:49 +03:00
|
|
|
g = string(gBytes)
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// 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
|
2018-12-14 09:07:13 +03:00
|
|
|
func LocationStrBytes(s string) []byte {
|
|
|
|
if len(s) != locationDataSize {
|
|
|
|
panic("Location string not the right size")
|
|
|
|
}
|
|
|
|
var b [locationDataSize]byte
|
|
|
|
copy(b[:], s)
|
|
|
|
return b[:]
|
2018-12-12 08:56:59 +03:00
|
|
|
}
|
2018-12-12 09:32:03 +03:00
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// 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.
|
|
|
|
func UpdateLocation(d []byte, s string) error {
|
2018-12-14 08:36:01 +03:00
|
|
|
err := HasLocation(d)
|
2018-12-12 09:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-14 08:32:47 +03:00
|
|
|
gb := LocationStrBytes(s)
|
|
|
|
for i := range d[locationDataIndx : locationDataIndx+locationDataSize] {
|
|
|
|
d[i+locationDataIndx] = gb[i]
|
2018-12-12 09:32:03 +03:00
|
|
|
}
|
2018-12-13 07:39:23 +03:00
|
|
|
d = updateCrc(d)
|
2018-12-12 09:32:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-13 07:39:23 +03:00
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// addCrc appends a crc table to a given psi table in bytes
|
2018-12-13 07:39:23 +03:00
|
|
|
func addCrc(out []byte) []byte {
|
|
|
|
out = append(out, make([]byte, 4)...)
|
|
|
|
out = updateCrc(out)
|
|
|
|
return out
|
|
|
|
}
|
2018-12-13 08:21:51 +03:00
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// updateCrc updates the crc of psi bytes slice that may have been modified
|
2018-12-13 07:39:23 +03:00
|
|
|
func updateCrc(out []byte) []byte {
|
|
|
|
crc32 := crc32_Update(0xffffffff, crc32_MakeTable(bits.Reverse32(crc32.IEEE)), out[1:len(out)-4])
|
|
|
|
out[len(out)-4] = byte(crc32 >> 24)
|
|
|
|
out[len(out)-3] = byte(crc32 >> 16)
|
|
|
|
out[len(out)-2] = byte(crc32 >> 8)
|
|
|
|
out[len(out)-1] = byte(crc32)
|
|
|
|
return out
|
|
|
|
}
|