2018-10-08 17:01:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-09 16:28:59 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2018-10-08 17:01:01 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-10-09 16:28:59 +00:00
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
2018-10-08 17:01:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HeaderStd struct {
|
2018-10-09 16:28:59 +00:00
|
|
|
SampleRate uint32
|
|
|
|
CenterFrequency uint64
|
|
|
|
StartTimestamp int64
|
|
|
|
SampleSize uint32
|
|
|
|
Filler uint32
|
|
|
|
CRC32 uint32
|
2018-10-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
func check(e error) {
|
2018-10-09 16:28:59 +00:00
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
2018-10-08 23:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func analyze(r *bufio.Reader) HeaderStd {
|
2018-10-09 16:28:59 +00:00
|
|
|
headerbuf := make([]byte, 32) // This is a full header with CRC
|
2018-10-08 17:01:01 +00:00
|
|
|
n, err := r.Read(headerbuf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-10-09 16:28:59 +00:00
|
|
|
if n != 32 {
|
2018-10-08 17:01:01 +00:00
|
|
|
panic("Header too small")
|
|
|
|
}
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 17:01:01 +00:00
|
|
|
var header HeaderStd
|
|
|
|
headerr := bytes.NewReader(headerbuf)
|
|
|
|
err = binary.Read(headerr, binary.LittleEndian, &header)
|
2018-10-08 23:06:39 +00:00
|
|
|
check(err)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-09 16:28:59 +00:00
|
|
|
return header
|
2018-10-08 23:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeHeader(writer *bufio.Writer, header *HeaderStd) {
|
|
|
|
var bin_buf bytes.Buffer
|
|
|
|
binary.Write(&bin_buf, binary.LittleEndian, header)
|
|
|
|
noh, err := writer.Write(bin_buf.Bytes())
|
|
|
|
check(err)
|
|
|
|
fmt.Printf("Wrote %d bytes header\n", noh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCRC(header *HeaderStd) {
|
|
|
|
var bin_buf bytes.Buffer
|
|
|
|
header.Filler = 0
|
|
|
|
binary.Write(&bin_buf, binary.LittleEndian, header)
|
|
|
|
header.CRC32 = crc32.ChecksumIEEE(bin_buf.Bytes()[0:28])
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:28:59 +00:00
|
|
|
func GetCRC(header *HeaderStd) uint32 {
|
2018-10-09 14:40:57 +00:00
|
|
|
var bin_buf bytes.Buffer
|
|
|
|
header.Filler = 0
|
|
|
|
binary.Write(&bin_buf, binary.LittleEndian, header)
|
|
|
|
return crc32.ChecksumIEEE(bin_buf.Bytes()[0:28])
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHeader(header *HeaderStd) {
|
2018-10-09 16:28:59 +00:00
|
|
|
fmt.Println("Sample rate:", header.SampleRate)
|
|
|
|
fmt.Println("Frequency :", header.CenterFrequency)
|
|
|
|
fmt.Println("Sample Size:", header.SampleSize)
|
|
|
|
tm := time.Unix(header.StartTimestamp, 0)
|
|
|
|
fmt.Println("Start :", tm)
|
2018-10-09 14:40:57 +00:00
|
|
|
fmt.Println("CRC32 :", header.CRC32)
|
2018-10-09 16:28:59 +00:00
|
|
|
fmt.Println("CRC32 OK :", GetCRC(header))
|
2018-10-09 14:40:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
func copyContent(reader *bufio.Reader, writer *bufio.Writer, blockSize uint) {
|
|
|
|
p := make([]byte, blockSize*4096) // buffer in 4k multiples
|
|
|
|
var sz int64 = 0
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
for {
|
|
|
|
n, err := reader.Read(p)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
writer.Write(p[0:n])
|
|
|
|
sz += int64(n)
|
|
|
|
break
|
|
|
|
} else {
|
2019-07-22 21:14:23 +00:00
|
|
|
fmt.Println("An error occurred during content copy. Aborting")
|
2018-10-08 23:06:39 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writer.Write(p)
|
2018-10-09 16:28:59 +00:00
|
|
|
sz += int64(blockSize) * 4096
|
2018-10-08 23:06:39 +00:00
|
|
|
}
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
fmt.Printf("Wrote %d bytes\r", sz)
|
|
|
|
}
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
fmt.Printf("Wrote %d bytes\r", sz)
|
2018-10-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-10-09 16:28:59 +00:00
|
|
|
inFileStr := flag.String("in", "foo", "input file")
|
2018-10-08 23:06:39 +00:00
|
|
|
outFileStr := flag.String("out", "foo", "output file")
|
|
|
|
sampleRate := flag.Uint("sr", 0, "Sample rate (S/s)")
|
|
|
|
centerFreq := flag.Uint64("cf", 0, "Center frequency (Hz)")
|
|
|
|
sampleSize := flag.Uint("sz", 16, "Sample size (16 or 24)")
|
2018-10-09 16:28:59 +00:00
|
|
|
timeStr := flag.String("ts", "", "start time RFC3339 (ex: 2006-01-02T15:04:05Z)")
|
|
|
|
timeNow := flag.Bool("now", false, "use now for start time")
|
|
|
|
blockSize := flag.Uint("bz", 1, "Copy block size in multiple of 4k")
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 17:01:01 +00:00
|
|
|
flag.Parse()
|
|
|
|
flagSeen := make(map[string]bool)
|
|
|
|
flag.Visit(func(f *flag.Flag) { flagSeen[f.Name] = true })
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 17:01:01 +00:00
|
|
|
if flagSeen["in"] {
|
2018-10-08 23:06:39 +00:00
|
|
|
fmt.Println("input file :", *inFileStr)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-09 16:28:59 +00:00
|
|
|
// open input file
|
|
|
|
fi, err := os.Open(*inFileStr)
|
|
|
|
check(err)
|
|
|
|
// close fi on exit and check for its returned error
|
|
|
|
defer func() {
|
|
|
|
err := fi.Close()
|
|
|
|
check(err)
|
|
|
|
}()
|
|
|
|
// make a read buffer
|
|
|
|
reader := bufio.NewReader(fi)
|
2018-10-08 23:06:39 +00:00
|
|
|
var headerOrigin HeaderStd = analyze(reader)
|
|
|
|
printHeader(&headerOrigin)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
if flagSeen["out"] {
|
|
|
|
if flagSeen["sr"] {
|
|
|
|
headerOrigin.SampleRate = uint32(*sampleRate)
|
2019-03-17 00:35:52 +00:00
|
|
|
}
|
|
|
|
if flagSeen["cf"] {
|
2018-10-08 23:06:39 +00:00
|
|
|
headerOrigin.CenterFrequency = *centerFreq
|
2019-03-17 00:35:52 +00:00
|
|
|
}
|
|
|
|
if flagSeen["sz"] {
|
2018-10-08 23:06:39 +00:00
|
|
|
if (*sampleSize == 16) || (*sampleSize == 24) {
|
2018-10-09 14:40:57 +00:00
|
|
|
headerOrigin.SampleSize = uint32(*sampleSize)
|
2018-10-08 23:06:39 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("Incorrect sample size specified. Defaulting to 16")
|
|
|
|
headerOrigin.SampleSize = 16
|
|
|
|
}
|
2019-03-17 00:35:52 +00:00
|
|
|
}
|
|
|
|
if flagSeen["ts"] {
|
2018-10-08 23:06:39 +00:00
|
|
|
t, err := time.Parse(time.RFC3339, *timeStr)
|
2018-10-09 16:28:59 +00:00
|
|
|
if err == nil {
|
2018-10-08 23:06:39 +00:00
|
|
|
headerOrigin.StartTimestamp = t.Unix()
|
|
|
|
} else {
|
|
|
|
fmt.Println("Incorrect time specified. Defaulting to now")
|
|
|
|
headerOrigin.StartTimestamp = int64(time.Now().Unix())
|
|
|
|
}
|
|
|
|
} else if *timeNow {
|
|
|
|
headerOrigin.StartTimestamp = int64(time.Now().Unix())
|
|
|
|
}
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
fmt.Println("\nHeader is now")
|
|
|
|
printHeader(&headerOrigin)
|
|
|
|
setCRC(&headerOrigin)
|
|
|
|
fmt.Println("CRC32 :", headerOrigin.CRC32)
|
|
|
|
fmt.Println("Output file:", *outFileStr)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
fo, err := os.Create(*outFileStr)
|
|
|
|
check(err)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-09 16:28:59 +00:00
|
|
|
defer func() {
|
|
|
|
err := fo.Close()
|
|
|
|
check(err)
|
|
|
|
}()
|
2018-10-08 23:06:39 +00:00
|
|
|
|
|
|
|
writer := bufio.NewWriter(fo)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
writeHeader(writer, &headerOrigin)
|
|
|
|
copyContent(reader, writer, *blockSize)
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 23:06:39 +00:00
|
|
|
fmt.Println("\nCopy done")
|
|
|
|
writer.Flush()
|
|
|
|
}
|
2018-10-09 14:40:57 +00:00
|
|
|
|
2018-10-08 17:01:01 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("No input file given")
|
|
|
|
}
|
|
|
|
}
|