wip: restructure code

need to decouple socketcan from gotelem
This commit is contained in:
saji 2023-05-08 01:28:36 -05:00
parent 709b1f0bac
commit 5d6e792a85
6 changed files with 22 additions and 32 deletions

View file

@ -1,8 +1,7 @@
package db package gotelem
import ( import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/kschamplin/gotelem/can"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -12,11 +11,11 @@ type CanDB struct {
Db *sqlx.DB Db *sqlx.DB
} }
func (cdb *CanDB) Send(_ *can.Frame) error { func (cdb *CanDB) Send(_ *Frame) error {
panic("not implemented") // TODO: Implement panic("not implemented") // TODO: Implement
} }
func (cdb *CanDB) Recv() (*can.Frame, error) { func (cdb *CanDB) Recv() (*Frame, error) {
panic("not implemented") // TODO: Implement panic("not implemented") // TODO: Implement
} }

View file

@ -4,7 +4,7 @@
// we also define standard interfaces for objects that can accept // 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 // 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) // 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, // Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended,
// but if it is extended, the Kind should be EFF. // but if it is extended, the Kind should be EFF.

View file

@ -1,6 +1,6 @@
// Code generated by "stringer -output=frame_kind.go -type Kind"; DO NOT EDIT. // Code generated by "stringer -output=frame_kind.go -type Kind"; DO NOT EDIT.
package can package gotelem
import "strconv" import "strconv"

View file

@ -1,9 +0,0 @@
package main
import (
"github.com/kschamplin/gotelem/cmd"
)
func main() {
cmd.Execute()
}

View file

@ -9,7 +9,7 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/kschamplin/gotelem/can" "github.com/kschamplin/gotelem"
"golang.org/x/sys/unix" "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. // 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. // helper function to make a filter.
// id and mask are straightforward, if inverted is true, the filter // id and mask are straightforward, if inverted is true, the filter
// will reject anything that matches. // 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} f := unix.CanFilter{Id: filter.Id, Mask: filter.Mask}
if filter.Inverted { if filter.Inverted {
@ -119,19 +119,19 @@ func (sck *CanSocket) SetFilters(filters []can.CanFilter) error {
} }
// Send sends a CAN frame // 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) buf := make([]byte, fdFrameSize)
idToWrite := msg.Id idToWrite := msg.Id
switch msg.Kind { switch msg.Kind {
case can.SFF: case gotelem.SFF:
idToWrite &= unix.CAN_SFF_MASK idToWrite &= unix.CAN_SFF_MASK
case can.EFF: case gotelem.EFF:
idToWrite &= unix.CAN_EFF_MASK idToWrite &= unix.CAN_EFF_MASK
idToWrite |= unix.CAN_EFF_FLAG idToWrite |= unix.CAN_EFF_FLAG
case can.RTR: case gotelem.RTR:
idToWrite |= unix.CAN_RTR_FLAG idToWrite |= unix.CAN_RTR_FLAG
default: default:
return errors.New("you can't send error frames") return errors.New("you can't send error frames")
@ -164,7 +164,7 @@ func (sck *CanSocket) Send(msg *can.Frame) error {
return nil return nil
} }
func (sck *CanSocket) Recv() (*can.Frame, error) { func (sck *CanSocket) Recv() (*gotelem.Frame, error) {
// todo: support extended frames. // todo: support extended frames.
buf := make([]byte, fdFrameSize) buf := make([]byte, fdFrameSize)
@ -175,18 +175,18 @@ func (sck *CanSocket) Recv() (*can.Frame, error) {
id := binary.LittleEndian.Uint32(buf[0:4]) id := binary.LittleEndian.Uint32(buf[0:4])
var k can.Kind var k gotelem.Kind
if id&unix.CAN_EFF_FLAG != 0 { if id&unix.CAN_EFF_FLAG != 0 {
// extended id frame // extended id frame
k = can.EFF k = gotelem.EFF
} else { } else {
// it's a normal can frame // it's a normal can frame
k = can.SFF k = gotelem.SFF
} }
dataLength := uint8(buf[4]) dataLength := uint8(buf[4])
result := &can.Frame{ result := &gotelem.Frame{
Id: id & unix.CAN_EFF_MASK, Id: id & unix.CAN_EFF_MASK,
Kind: k, Kind: k,
Data: buf[8 : dataLength+8], Data: buf[8 : dataLength+8],

View file

@ -5,7 +5,7 @@ import (
"net" "net"
"testing" "testing"
"github.com/kschamplin/gotelem/can" "github.com/kschamplin/gotelem"
) )
func TestCanSocket(t *testing.T) { func TestCanSocket(t *testing.T) {
@ -41,9 +41,9 @@ func TestCanSocket(t *testing.T) {
defer sock.Close() defer sock.Close()
// make a packet. // make a packet.
testFrame := &can.Frame{ testFrame := &gotelem.Frame{
Id: 0x123, Id: 0x123,
Kind: can.SFF, Kind: gotelem.SFF,
Data: []byte{0, 1, 2, 3, 4, 5, 6, 7}, Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
} }
err := sock.Send(testFrame) err := sock.Send(testFrame)
@ -59,9 +59,9 @@ func TestCanSocket(t *testing.T) {
defer sock.Close() defer sock.Close()
defer rsock.Close() defer rsock.Close()
testFrame := &can.Frame{ testFrame := &gotelem.Frame{
Id: 0x234, Id: 0x234,
Kind: can.SFF, Kind: gotelem.SFF,
Data: []byte{0, 1, 2, 3, 4, 5, 6, 7}, Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
} }
_ = sock.Send(testFrame) _ = sock.Send(testFrame)