Files

92 lines
3.6 KiB
Go

package goirsdk
import (
"bytes"
"encoding/binary"
"fmt"
)
const (
FileHeaderSize = 112 // FileHeaderSize is the size of the headers
HeaderSize = 4 // HeaderSize is the size of a single header
)
// TelemetryHeaders struct to hold an IBT file's headers
type TelemetryHeaders struct {
Version int32
Status int32 // Status of 1 indicates a completed session and status of 0 a live session
TickRate int32 // TickRate indicates the frequency of writes (usually 60)
SessionInfoUpdate int32 // SessionInfoUpdate indicates the number of times the SessionInfo was
// updated. 0 for finished sessions and >1 for active sessions
SessionInfoLength int32 // SessionInfoLength is the length of the session info buffer
SessionInfoOffset int32 // SessionInfoOffset is the offset of the session info in the buffer
NumVars int32 // NumVars is the number of variables in each input
VarHeaderOffset int32 // VarHeaderOffset is the offset of the VarHeader
NumBuf int32 // NumBuf will be 1 for static files and 3 for live telemetry files
BufLen int32 // BufLen is the length for parsing VarHeader values
Padding [12]byte // Padding
BufOffset int32 // I still don't know what this is:
}
// readHeader will read the header out of the telemetry data
func (i *IBT) readHeader() error {
var headerRaw [FileHeaderSize]byte
_, err := i.File.ReadAt(headerRaw[:], 0)
if err != nil {
return fmt.Errorf("failed to read headers from file: %v", err)
}
i.Headers, err = parseTelemetryHeader(headerRaw)
if err != nil {
return fmt.Errorf("unable to read headers from file: %v", err)
}
// Write to the output file - TODO: this should only write if necessary
if i.Opts.IBTExport {
err = i.exportIBT(headerRaw[:], 0)
if err != nil {
i.Opts.Logger.Debug("Failed to export headers", "err", err)
}
}
return nil
}
// parseTelemetryHeader will read the IBT file headers from a correctly sized
// buffer.
// You need to pass a the first FILE_HEADER_SIZE bytes of the buffer
func parseTelemetryHeader(buf [FileHeaderSize]byte) (*TelemetryHeaders, error) {
// utils.HexDump(buf[:])
// fmt.Printf("Len: %d\n", len(buf))
dst := TelemetryHeaders{}
err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst)
if err != nil {
return nil, fmt.Errorf("unable to unpack data: %v", err)
}
return &dst, nil
}
// ToString renders a string showing the values of the struct
func (th *TelemetryHeaders) ToString() string {
return fmt.Sprintf(
"Version: %5d (0x%04x)\n"+
"Status: %5d (0x%04x)\n"+
"TickRate: %5d (0x%04x)\n"+
"SIUpdate: %5d (0x%04x)\n"+
"SILength: %5d (0x%04x)\n"+
"SIOffset: %5d (0x%04x)\n"+
"NumVars: %5d (0x%04x)\n"+
"VarHeaderOffset: %5d (0x%04x)\n"+
"NumBuf: %5d (0x%04x)\n"+
"BufLen: %5d (0x%04x)\n"+
"BufOffset: %5d (0x%04x)\n",
th.Version, th.Version, th.Status, th.Status, th.TickRate, th.TickRate,
th.SessionInfoUpdate, th.SessionInfoUpdate,
th.SessionInfoLength, th.SessionInfoLength,
th.SessionInfoOffset, th.SessionInfoOffset,
th.NumVars, th.NumVars, th.VarHeaderOffset, th.VarHeaderOffset,
th.NumBuf, th.NumBuf, th.BufLen, th.BufLen, th.BufOffset, th.BufOffset,
)
}