This commit is contained in:
saji 2023-04-22 02:48:04 -05:00
parent c7cda9db7a
commit 7c5bfb8e7b
5 changed files with 33 additions and 22 deletions

2
go.mod
View file

@ -9,6 +9,8 @@ require (
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect 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/philhofer/fwd v1.1.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect github.com/tinylib/msgp v1.1.8 // indirect

7
go.sum
View file

@ -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 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 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 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=

View file

@ -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 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 { type Frame struct {
Id uint32 Id uint32
Data []byte Data []byte
@ -7,6 +15,8 @@ type Frame struct {
} }
//go:generate stringer -output=frame_kind.go -type Kind //go:generate stringer -output=frame_kind.go -type Kind
// Kind is the type of the can Frame
type Kind uint8 type Kind uint8
const ( const (
@ -16,20 +26,26 @@ const (
ERR // Error Frame 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 { type CanFilter struct {
Id uint32 Id uint32
Mask uint32 Mask uint32
Inverted bool Inverted bool
} }
// CanSink is an object that can accept Frames to transmit.
type CanSink interface { type CanSink interface {
Send(*Frame) error Send(*Frame) error
} }
// CanSource is an object that can receive Frames.
type CanSource interface { type CanSource interface {
Recv() (*Frame, error) Recv() (*Frame, error)
} }
// CanTransciever is an object that can both send and receive Frames.
type CanTransciever interface { type CanTransciever interface {
CanSink CanSink
CanSource CanSource

View file

@ -3,13 +3,15 @@ package db
import ( import (
"database/sql" "database/sql"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/kschamplin/gotelem/internal/can" "github.com/kschamplin/gotelem/internal/can"
) )
// this file implements a CAN adapter for the sqlite db. // this file implements a CAN adapter for the sqlite db.
type CanDB struct { type CanDB struct {
Db *sql.DB Db *sqlx.DB
} }
func (cdb *CanDB) Send(_ *can.Frame) error { func (cdb *CanDB) Send(_ *can.Frame) error {

View file

@ -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 package xbee
import ( import (
@ -48,27 +53,6 @@ func WriteFrame(w io.Writer, cmd Frameable) (n int, err error) {
return w.Write(frame) 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. // now we can describe frames in other files that implement Frameable.
// the remaining challenge is reception and actual API frames. // the remaining challenge is reception and actual API frames.
// xbee uses the first byte of the "frame data" as the API identifier or command. // xbee uses the first byte of the "frame data" as the API identifier or command.