diff --git a/go.mod b/go.mod index f05e608..10d8477 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,8 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/jmoiron/sqlx v1.3.5 // indirect + github.com/mattn/go-sqlite3 v1.14.16 // indirect github.com/philhofer/fwd v1.1.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/tinylib/msgp v1.1.8 // indirect diff --git a/go.sum b/go.sum index b2c58ae..c24c542 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,12 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= diff --git a/internal/can/frame.go b/internal/can/frame.go index 1e1d0d9..7f26ca3 100644 --- a/internal/can/frame.go +++ b/internal/can/frame.go @@ -1,5 +1,13 @@ +// Package can provides generic CAN interfaces and types. +// +// It has a generic can Frame (packet), as well as a filter type. +// 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 +// Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended, +// but if it is extended, the Kind should be EFF. type Frame struct { Id uint32 Data []byte @@ -7,6 +15,8 @@ type Frame struct { } //go:generate stringer -output=frame_kind.go -type Kind + +// Kind is the type of the can Frame type Kind uint8 const ( @@ -16,20 +26,26 @@ const ( ERR // Error Frame ) +// CanFilter is a basic filter for masking out data. It has an Inverted flag +// which indicates opposite behavior (reject all packets that match Id and Mask). +// The filter matches when (packet.Id & filter.Mask) == filter.Id type CanFilter struct { Id uint32 Mask uint32 Inverted bool } +// CanSink is an object that can accept Frames to transmit. type CanSink interface { Send(*Frame) error } +// CanSource is an object that can receive Frames. type CanSource interface { Recv() (*Frame, error) } +// CanTransciever is an object that can both send and receive Frames. type CanTransciever interface { CanSink CanSource diff --git a/internal/db/can_adapter.go b/internal/db/can_adapter.go index 07ae9ca..7802473 100644 --- a/internal/db/can_adapter.go +++ b/internal/db/can_adapter.go @@ -3,13 +3,15 @@ package db import ( "database/sql" + "github.com/jmoiron/sqlx" + _ "github.com/mattn/go-sqlite3" "github.com/kschamplin/gotelem/internal/can" ) // this file implements a CAN adapter for the sqlite db. type CanDB struct { - Db *sql.DB + Db *sqlx.DB } func (cdb *CanDB) Send(_ *can.Frame) error { diff --git a/internal/xbee/api_frame.go b/internal/xbee/api_frame.go index eca34c4..cd55c3c 100644 --- a/internal/xbee/api_frame.go +++ b/internal/xbee/api_frame.go @@ -1,3 +1,8 @@ +// Package xbee implements xbee API encoding and decoding. + +// It encodes and decodes +// API frames from io.Writer and io.Reader by providing a WriteFrame function and +// a scanner.split function. It also includes package xbee import ( @@ -48,27 +53,6 @@ func WriteFrame(w io.Writer, cmd Frameable) (n int, err error) { return w.Write(frame) } -func makeXbeeApiFrame(cmd Frameable) ([]byte, error) { - dataBuf, _ := cmd.Bytes() - frameBuf := make([]byte, len(dataBuf)+4) - - // move data and construct the frame - - frameBuf[0] = 0x7E // start delimiter - - // length - // todo: check endiannes (0x7e, msb lsb) - binary.BigEndian.PutUint16(frameBuf[1:3], uint16(len(dataBuf))) - - copy(frameBuf[3:], dataBuf) - - chksum := calculateChecksum(dataBuf) - - frameBuf[len(frameBuf)-1] = chksum - - return frameBuf, nil -} - // now we can describe frames in other files that implement Frameable. // the remaining challenge is reception and actual API frames. // xbee uses the first byte of the "frame data" as the API identifier or command.