2019-01-07 06:19:47 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
helpers.go
|
|
|
|
DESCRIPTION
|
|
|
|
helpers.go provides functionality for editing and reading bytes slices
|
|
|
|
directly in order to insert/read timestamp and location data in psi.
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
helpers.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package psi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) []byte {
|
2019-01-07 09:43:50 +03:00
|
|
|
var s [TimeDataSize]byte
|
2019-01-07 06:19:47 +03:00
|
|
|
binary.BigEndian.PutUint64(s[:], t)
|
|
|
|
return s[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func HasTime(p []byte) bool {
|
2019-01-07 09:43:50 +03:00
|
|
|
if p[TimeTagIndx] == TimeDescTag {
|
2019-01-07 06:19:47 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func HasLocation(p []byte) bool {
|
2019-01-07 09:43:50 +03:00
|
|
|
if p[LocationTagIndx] == LocationDescTag {
|
2019-01-07 06:19:47 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(dst []byte, t uint64) error {
|
|
|
|
if !HasTime(dst) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return errors.New("pmt does not have time descriptor, cannot update")
|
2019-01-07 06:19:47 +03:00
|
|
|
}
|
|
|
|
ts := TimeBytes(uint64(t))
|
2019-01-07 09:43:50 +03:00
|
|
|
for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
|
|
|
|
dst[i+TimeDataIndx] = ts[i]
|
2019-01-07 06:19:47 +03:00
|
|
|
}
|
2019-01-07 08:55:08 +03:00
|
|
|
updateCrc(dst)
|
2019-01-07 06:19:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimeFrom 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 TimeFrom(p []byte) (t uint64, err error) {
|
|
|
|
if !HasTime(p) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return 0, errors.New("pmt does not have a time descriptor")
|
2019-01-07 06:19:47 +03:00
|
|
|
}
|
2019-01-07 09:43:50 +03:00
|
|
|
t = binary.BigEndian.Uint64(p[TimeDataIndx : TimeDataIndx+TimeDataSize])
|
2019-01-07 06:19:47 +03:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocationFrom 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 LocationFrom(p []byte) (g string, err error) {
|
|
|
|
if !HasLocation(p) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return "", errors.New("pmt does not have location descriptor")
|
2019-01-07 06:19:47 +03:00
|
|
|
}
|
2019-01-07 09:43:50 +03:00
|
|
|
gBytes := p[LocationDataIndx : LocationDataIndx+LocationDataSize]
|
2019-01-07 06:19:47 +03:00
|
|
|
gBytes = bytes.Trim(gBytes, "\x00")
|
|
|
|
g = string(gBytes)
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func LocationStrBytes(s string) []byte {
|
2019-01-07 09:43:50 +03:00
|
|
|
var b [LocationDataSize]byte
|
2019-01-07 06:19:47 +03:00
|
|
|
copy(b[:], s)
|
|
|
|
return b[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
if !HasLocation(d) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return errors.New("pmt does not location descriptor, cannot update")
|
2019-01-07 06:19:47 +03:00
|
|
|
}
|
|
|
|
gb := LocationStrBytes(s)
|
2019-01-07 09:43:50 +03:00
|
|
|
copy(d[LocationDataIndx:LocationDataIndx+LocationDataSize], gb)
|
2019-01-07 08:55:08 +03:00
|
|
|
updateCrc(d)
|
2019-01-07 06:19:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-07 06:54:54 +03:00
|
|
|
|
|
|
|
func trim(d []byte, t byte) []byte {
|
|
|
|
for i, b := range d {
|
|
|
|
if b == t {
|
|
|
|
return d[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|