Skip to content

Commit

Permalink
refactor: move the Chunk into the pcic pakage
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed Dec 24, 2023
1 parent 02a018c commit 1ea421d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 45 deletions.
59 changes: 30 additions & 29 deletions pkg/chunk/chunk.go → pkg/pcic/chunk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package chunk
package pcic

import (
"encoding/binary"
Expand All @@ -7,8 +7,11 @@ import (
"time"
)

type ChunkType uint32
type DataFormat uint32
type (
ChunkType uint32
DataFormat uint32
ChunkOption func(c *Chunk)
)

// The known Chunk Types
const (
Expand All @@ -33,19 +36,8 @@ var (
byteSizeLUT [FORMAT_MAX]uint32 = [FORMAT_MAX]uint32{1, 1, 2, 2, 4, 4, 4, 8, 8}
)

type Chunk interface {
UnmarshalBinary(data []byte) error
MarshalBinary() (data []byte, err error)
Type() ChunkType
Size() uint32
FrameCount() uint32
Status() uint32
TimeStamp() time.Time
Bytes() []byte
}

// A ChunkData object contains all the information based on a PCIC chunk
type ChunkData struct {
// A Chunk object contains all the information based on a PCIC chunk
type Chunk struct {
chunkType ChunkType // The type of the Chunk, each chunk type requires a unique ID
chunkSize uint32 // The size of the complete chunk, including the header and the data
headerSize uint32 // The Size of the chunk header after this amount of bytes the data section starts
Expand Down Expand Up @@ -82,51 +74,60 @@ const (
MaxSupportedChunkHeaderVersion = 3
)

func New(cType ChunkType) *ChunkData {
chunk := &ChunkData{
chunkType: cType,
metadata: "{}",
data: []byte{},
func NewChunk(options ...ChunkOption) *Chunk {
chunk := &Chunk{
metadata: "{}",
data: []byte{},
}
// Apply options
for _, opt := range options {
opt(chunk)
}
return chunk
}

func (c *ChunkData) Type() ChunkType {
func WithChunkType(cType ChunkType) ChunkOption {
return func(c *Chunk) {
c.chunkType = cType
}
}

func (c *Chunk) Type() ChunkType {
return c.chunkType
}

func (c *ChunkData) Size() int {
func (c *Chunk) Size() int {
return int(c.chunkSize)
}

func (c *ChunkData) FrameCount() uint32 {
func (c *Chunk) FrameCount() uint32 {
return c.frameCount
}

func (c *ChunkData) Status() uint32 {
func (c *Chunk) Status() uint32 {
return c.statusCode
}

func (c *ChunkData) TimeStamp() time.Time {
func (c *Chunk) TimeStamp() time.Time {
return time.Unix(int64(c.timestampSec), int64(c.timestampNSec))
}

func (c *ChunkData) Bytes() []byte {
func (c *Chunk) Bytes() []byte {
return c.data
}

// MarshalBinary creates a binary representation of the Chunk
//
// The binary representation is encoded in the byte slice
func (c *ChunkData) MarshalBinary() (data []byte, err error) {
func (c *Chunk) MarshalBinary() (data []byte, err error) {
return []byte{}, nil
}

// UnmarshalBinary creates a ChunkData from a byte slice
//
// It copies the data from the input slice to comply with the BinaryUnmarshaler
// interface.
func (c *ChunkData) UnmarshalBinary(data []byte) error {
func (c *Chunk) UnmarshalBinary(data []byte) error {
dataLen := uint32(len(data))
if dataLen < offsetOfData {
return errors.New("unable to parse an empty input")
Expand Down
18 changes: 11 additions & 7 deletions pkg/chunk/chunk_test.go → pkg/pcic/chunk_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package chunk_test
package pcic_test

import (
"testing"
"time"

"github.com/graugans/go-ovp8xx/pkg/chunk"
"github.com/graugans/go-ovp8xx/pkg/pcic"
"github.com/stretchr/testify/assert"
)

func TestChunkType(t *testing.T) {
c := chunk.New(chunk.RADIAL_DISTANCE_NOISE)
c := pcic.NewChunk(pcic.WithChunkType(pcic.RADIAL_DISTANCE_NOISE))
assert.Equal(t,
chunk.RADIAL_DISTANCE_NOISE,
pcic.RADIAL_DISTANCE_NOISE,
c.Type(),
"There is a chunk type mismatch detected",
)
}

func TestChunkParse(t *testing.T) {
c := chunk.ChunkData{}
func TestUnmarshalBinary(t *testing.T) {
c := pcic.Chunk{}
assert.Error(t,
c.UnmarshalBinary([]byte{}),
"An error is expected when sending an empty byte slice",
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestChunkParse(t *testing.T) {
"A successful parse expected",
)
assert.Equal(t,
chunk.RADIAL_DISTANCE_NOISE,
pcic.RADIAL_DISTANCE_NOISE,
c.Type(),
"Type mismatch detected",
)
Expand Down Expand Up @@ -182,3 +182,7 @@ func TestChunkParse(t *testing.T) {
"A data size mismatch occurred",
)
}

func TestRoundtrip(t *testing.T) {

}
9 changes: 6 additions & 3 deletions pkg/pcic/frame.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package pcic

import "github.com/graugans/go-ovp8xx/pkg/chunk"

type Frame struct {
Chunks []chunk.ChunkData
Chunks []Chunk
size int
}

func (f *Frame) Size() int {
return f.size
}
4 changes: 1 addition & 3 deletions pkg/pcic/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"fmt"
"io"

"github.com/graugans/go-ovp8xx/pkg/chunk"
)

type PCIC struct {
Expand Down Expand Up @@ -152,7 +150,7 @@ func asyncResultParser(data []byte) (Frame, error) {
remainingBytes := len(content)
offset := 0
for remainingBytes > 0 {
c := chunk.ChunkData{}
c := Chunk{}
if err := c.UnmarshalBinary(content[offset:]); err != nil {
return frame, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/pcic/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"testing"

"github.com/graugans/go-ovp8xx/pkg/chunk"
"github.com/graugans/go-ovp8xx/pkg/pcic"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestMinimalReceive(t *testing.T) {
}

func TestReceiveWithChunk(t *testing.T) {
c := chunk.ChunkData{}
c := pcic.Chunk{}
chunkData := []byte{
0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */
0x34, 0x00, 0x00, 0x00, /* CHUNK_SIZE */
Expand Down Expand Up @@ -85,7 +84,7 @@ func TestReceiveWithChunk(t *testing.T) {
assert.NoError(t, err, "We expect no error while receiving data")

assert.Equal(t,
chunk.RADIAL_DISTANCE_NOISE,
pcic.RADIAL_DISTANCE_NOISE,
testHandler.frame.Chunks[0].Type(),
)

Expand Down

0 comments on commit 1ea421d

Please sign in to comment.