From 5d6e792a858252b7aff8f3371bf1bc0ee6f728aa Mon Sep 17 00:00:00 2001 From: saji Date: Mon, 8 May 2023 01:28:36 -0500 Subject: [PATCH] wip: restructure code need to decouple socketcan from gotelem --- db/can_adapter.go => can_adapter.go | 7 +++---- can/frame.go => frame.go | 2 +- can/frame_kind.go => frame_kind.go | 2 +- main.go | 9 --------- socketcan/socketcan.go | 24 ++++++++++++------------ socketcan/socketcan_test.go | 10 +++++----- 6 files changed, 22 insertions(+), 32 deletions(-) rename db/can_adapter.go => can_adapter.go (66%) rename can/frame.go => frame.go (98%) rename can/frame_kind.go => frame_kind.go (97%) delete mode 100644 main.go diff --git a/db/can_adapter.go b/can_adapter.go similarity index 66% rename from db/can_adapter.go rename to can_adapter.go index c69da99..0a5c4ef 100644 --- a/db/can_adapter.go +++ b/can_adapter.go @@ -1,8 +1,7 @@ -package db +package gotelem import ( "github.com/jmoiron/sqlx" - "github.com/kschamplin/gotelem/can" _ "github.com/mattn/go-sqlite3" ) @@ -12,11 +11,11 @@ type CanDB struct { Db *sqlx.DB } -func (cdb *CanDB) Send(_ *can.Frame) error { +func (cdb *CanDB) Send(_ *Frame) error { panic("not implemented") // TODO: Implement } -func (cdb *CanDB) Recv() (*can.Frame, error) { +func (cdb *CanDB) Recv() (*Frame, error) { panic("not implemented") // TODO: Implement } diff --git a/can/frame.go b/frame.go similarity index 98% rename from can/frame.go rename to frame.go index 87803b5..da837b3 100644 --- a/can/frame.go +++ b/frame.go @@ -4,7 +4,7 @@ // we also define standard interfaces for objects that can accept // can frames. We can use this pattern to easily extend the capabiltiies of the program // by writing "adapters" to various devices/formats (xbee, sqlite, network socket, socketcan) -package can +package gotelem // Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended, // but if it is extended, the Kind should be EFF. diff --git a/can/frame_kind.go b/frame_kind.go similarity index 97% rename from can/frame_kind.go rename to frame_kind.go index 948ab75..5a29470 100644 --- a/can/frame_kind.go +++ b/frame_kind.go @@ -1,6 +1,6 @@ // Code generated by "stringer -output=frame_kind.go -type Kind"; DO NOT EDIT. -package can +package gotelem import "strconv" diff --git a/main.go b/main.go deleted file mode 100644 index 7dd5a2c..0000000 --- a/main.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import ( - "github.com/kschamplin/gotelem/cmd" -) - -func main() { - cmd.Execute() -} diff --git a/socketcan/socketcan.go b/socketcan/socketcan.go index 82c1a53..e6f4977 100644 --- a/socketcan/socketcan.go +++ b/socketcan/socketcan.go @@ -9,7 +9,7 @@ import ( "fmt" "net" - "github.com/kschamplin/gotelem/can" + "github.com/kschamplin/gotelem" "golang.org/x/sys/unix" ) @@ -96,12 +96,12 @@ func (sck *CanSocket) SetFDMode(enable bool) error { } // SetFilters will set the socketCAN filters based on a standard CAN filter list. -func (sck *CanSocket) SetFilters(filters []can.CanFilter) error { +func (sck *CanSocket) SetFilters(filters []gotelem.CanFilter) error { // helper function to make a filter. // id and mask are straightforward, if inverted is true, the filter // will reject anything that matches. - makeFilter := func(filter can.CanFilter) unix.CanFilter { + makeFilter := func(filter gotelem.CanFilter) unix.CanFilter { f := unix.CanFilter{Id: filter.Id, Mask: filter.Mask} if filter.Inverted { @@ -119,19 +119,19 @@ func (sck *CanSocket) SetFilters(filters []can.CanFilter) error { } // Send sends a CAN frame -func (sck *CanSocket) Send(msg *can.Frame) error { +func (sck *CanSocket) Send(msg *gotelem.Frame) error { buf := make([]byte, fdFrameSize) idToWrite := msg.Id switch msg.Kind { - case can.SFF: + case gotelem.SFF: idToWrite &= unix.CAN_SFF_MASK - case can.EFF: + case gotelem.EFF: idToWrite &= unix.CAN_EFF_MASK idToWrite |= unix.CAN_EFF_FLAG - case can.RTR: + case gotelem.RTR: idToWrite |= unix.CAN_RTR_FLAG default: return errors.New("you can't send error frames") @@ -164,7 +164,7 @@ func (sck *CanSocket) Send(msg *can.Frame) error { return nil } -func (sck *CanSocket) Recv() (*can.Frame, error) { +func (sck *CanSocket) Recv() (*gotelem.Frame, error) { // todo: support extended frames. buf := make([]byte, fdFrameSize) @@ -175,18 +175,18 @@ func (sck *CanSocket) Recv() (*can.Frame, error) { id := binary.LittleEndian.Uint32(buf[0:4]) - var k can.Kind + var k gotelem.Kind if id&unix.CAN_EFF_FLAG != 0 { // extended id frame - k = can.EFF + k = gotelem.EFF } else { // it's a normal can frame - k = can.SFF + k = gotelem.SFF } dataLength := uint8(buf[4]) - result := &can.Frame{ + result := &gotelem.Frame{ Id: id & unix.CAN_EFF_MASK, Kind: k, Data: buf[8 : dataLength+8], diff --git a/socketcan/socketcan_test.go b/socketcan/socketcan_test.go index 07e4881..be9c915 100644 --- a/socketcan/socketcan_test.go +++ b/socketcan/socketcan_test.go @@ -5,7 +5,7 @@ import ( "net" "testing" - "github.com/kschamplin/gotelem/can" + "github.com/kschamplin/gotelem" ) func TestCanSocket(t *testing.T) { @@ -41,9 +41,9 @@ func TestCanSocket(t *testing.T) { defer sock.Close() // make a packet. - testFrame := &can.Frame{ + testFrame := &gotelem.Frame{ Id: 0x123, - Kind: can.SFF, + Kind: gotelem.SFF, Data: []byte{0, 1, 2, 3, 4, 5, 6, 7}, } err := sock.Send(testFrame) @@ -59,9 +59,9 @@ func TestCanSocket(t *testing.T) { defer sock.Close() defer rsock.Close() - testFrame := &can.Frame{ + testFrame := &gotelem.Frame{ Id: 0x234, - Kind: can.SFF, + Kind: gotelem.SFF, Data: []byte{0, 1, 2, 3, 4, 5, 6, 7}, } _ = sock.Send(testFrame)