Skip to content

Commit

Permalink
Add SDO server functionality, add conversion from string to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Petersen committed Jul 3, 2024
1 parent 75d264d commit 15761fe
Show file tree
Hide file tree
Showing 19 changed files with 1,018 additions and 236 deletions.
69 changes: 36 additions & 33 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,44 @@ import (

var Lock = maplock.New()

type SDOAbortCode int

const (
SDO_ERR_TOGGLE_BIT = 0x05030000
SDO_ERR_TIMEOUT = 0x05040000
SDO_ERR_COMMAND = 0x05040001
SDO_ERR_BLOCK_SIZE = 0x05040002
SDO_ERR_BLOCK_SEQUENCE = 0x05040003
SDO_ERR_BLOCK_CRC = 0x05040004
SDO_ERR_MEMORY = 0x05040005
SDO_ERR_ACCESS_UNSUPPORTED = 0x06010000
SDO_ERR_ACCESS_WO = 0x06010001
SDO_ERR_ACCESS_RO = 0x06010002
SDO_ERR_NO_OBJECT = 0x06020000
SDO_ERR_MAPPING_OBJECT = 0x06040041
SDO_ERR_MAPPING_LENGTH = 0x06040042
SDO_ERR_GENERAL_PARAMETER = 0x06040043
SDO_ERR_GENERAL_DEVICE = 0x06040047
SDO_ERR_HARDWARE = 0x06060000
SDO_ERR_DATATYPE = 0x06070010
SDO_ERR_DATATYPE_HIGH = 0x06070012
SDO_ERR_DATATYPE_LOW = 0x06070013
SDO_ERR_NO_SUB_INDEX = 0x06090011
SDO_ERR_VALUE_RANGE = 0x06090030
SDO_ERR_VALUE_HIGH = 0x06090031
SDO_ERR_VALUE_LOW = 0x06090032
SDO_ERR_VALUE_MIN_MAX = 0x06090036
SDO_ERR_SDO_CONNECTION = 0x060A0023
SDO_ERR_GENERAL = 0x08000000
SDO_ERR_DATA_STORE = 0x08000020
SDO_ERR_DATA_STORE_LOCAL = 0x08000021
SDO_ERR_DATA_STORE_STATE = 0x08000022
SDO_ERR_OBJECT_DICTIONARY = 0x08000023
SDO_ERR_NO_DATA = 0x08000024
SDO_ERR_TOGGLE_BIT SDOAbortCode = 0x05030000
SDO_ERR_TIMEOUT SDOAbortCode = 0x05040000
SDO_ERR_COMMAND SDOAbortCode = 0x05040001
SDO_ERR_BLOCK_SIZE SDOAbortCode = 0x05040002
SDO_ERR_BLOCK_SEQUENCE SDOAbortCode = 0x05040003
SDO_ERR_BLOCK_CRC SDOAbortCode = 0x05040004
SDO_ERR_MEMORY SDOAbortCode = 0x05040005
SDO_ERR_ACCESS_UNSUPPORTED SDOAbortCode = 0x06010000
SDO_ERR_ACCESS_WO SDOAbortCode = 0x06010001
SDO_ERR_ACCESS_RO SDOAbortCode = 0x06010002
SDO_ERR_NO_OBJECT SDOAbortCode = 0x06020000
SDO_ERR_MAPPING_OBJECT SDOAbortCode = 0x06040041
SDO_ERR_MAPPING_LENGTH SDOAbortCode = 0x06040042
SDO_ERR_GENERAL_PARAMETER SDOAbortCode = 0x06040043
SDO_ERR_GENERAL_DEVICE SDOAbortCode = 0x06040047
SDO_ERR_HARDWARE SDOAbortCode = 0x06060000
SDO_ERR_DATATYPE SDOAbortCode = 0x06070010
SDO_ERR_DATATYPE_HIGH SDOAbortCode = 0x06070012
SDO_ERR_DATATYPE_LOW SDOAbortCode = 0x06070013
SDO_ERR_NO_SUB_INDEX SDOAbortCode = 0x06090011
SDO_ERR_VALUE_RANGE SDOAbortCode = 0x06090030
SDO_ERR_VALUE_HIGH SDOAbortCode = 0x06090031
SDO_ERR_VALUE_LOW SDOAbortCode = 0x06090032
SDO_ERR_VALUE_MIN_MAX SDOAbortCode = 0x06090036
SDO_ERR_SDO_CONNECTION SDOAbortCode = 0x060A0023
SDO_ERR_GENERAL SDOAbortCode = 0x08000000
SDO_ERR_DATA_STORE SDOAbortCode = 0x08000020
SDO_ERR_DATA_STORE_LOCAL SDOAbortCode = 0x08000021
SDO_ERR_DATA_STORE_STATE SDOAbortCode = 0x08000022
SDO_ERR_OBJECT_DICTIONARY SDOAbortCode = 0x08000023
SDO_ERR_NO_DATA SDOAbortCode = 0x08000024
NO_ERROR SDOAbortCode = 0x0
)

func GetAbortCodeText(code uint32) string {
func GetAbortCodeText(code SDOAbortCode) string {
switch code {
case SDO_ERR_TOGGLE_BIT:
return "SDO toggle bit error (protocol violation)"
Expand Down Expand Up @@ -119,7 +122,7 @@ type TransferAbort struct {
func (e TransferAbort) Error() string {
if len(e.AbortCode) == 4 {
code := binary.LittleEndian.Uint32(e.AbortCode)
return GetAbortCodeText(code)
return GetAbortCodeText(SDOAbortCode(code))
}

return fmt.Sprintf("Server aborted upload")
Expand Down
2 changes: 1 addition & 1 deletion cmd/canopendump.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
bus.ConnectAndPublish()
}

// logCANFrame logs a frame with the same format as candump from can-utils.
// logCANFrame logs a frame with the same format as candump from can-canopen.
func logCANFrame(frm can.Frame) {
canopenFrm := canopen.CANopenFrame(frm)
var msgType string
Expand Down
26 changes: 10 additions & 16 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,16 @@ func (frm Frame) CANFrame() can.Frame {
}
}

// Marshal returns the byte encoding of frm.
func Marshal(frm Frame) (b []byte, err error) {
canFrm := frm.CANFrame()

return can.Marshal(canFrm)
}

// Unmarshal parses the bytes b and stores the result in the value
// pointed to by frm.
func Unmarshal(b []byte, frm *Frame) error {
canFrm := can.Frame{}
if err := can.Unmarshal(b, &canFrm); err != nil {
return err
func (frm *Frame) ObjectIndex() ObjectIndex {
if len(frm.Data) < 4 {
return ObjectIndex{}
}

*frm = CANopenFrame(canFrm)

return nil
return ObjectIndex{
Index: Index{
B0: frm.Data[1],
B1: frm.Data[2],
},
SubIndex: frm.Data[3],
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/FabianPetersen/can v0.2.7
github.com/avast/retry-go v3.0.0+incompatible
github.com/jpillora/maplock v0.0.0-20160420012925-5c725ac6e22a
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/FabianPetersen/can v0.2.7 h1:4uSsjUf9FMSQK7RgMu2stnuKMmBAyP6qAji7t4rbPgQ=
github.com/FabianPetersen/can v0.2.7/go.mod h1:Peraw3e+jsctajc8Tl5faMk95eBq9s7P/kMtlxUFRk0=
github.com/FabianPetersen/can v0.4.5 h1:i2QwyKcopZjjklIb4X1f7TbmfKKel5bZoTNdhEamhb4=
github.com/FabianPetersen/can v0.4.5/go.mod h1:kamC/zNbNQByXsg3hsSMPV9J7xRQtu/BYTQepFFXhgQ=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -16,6 +18,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
9 changes: 0 additions & 9 deletions heartbeat.go

This file was deleted.

16 changes: 16 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ func NewObjectIndex(index uint16, subIndex uint8) ObjectIndex {
SubIndex: subIndex,
}
}

func (objectIndex *ObjectIndex) Compare(other ObjectIndex) bool {
return objectIndex.Index.B0 == other.Index.B0 && objectIndex.Index.B1 == other.Index.B1 && objectIndex.SubIndex == other.SubIndex
}

func (objectIndex *ObjectIndex) Bytes() []byte {
return []byte{
objectIndex.Index.B0,
objectIndex.Index.B1,
objectIndex.SubIndex,
}
}

func (index *Index) Index() uint16 {
return uint16(index.B0) + (uint16(index.B1) << 8)
}
34 changes: 0 additions & 34 deletions producer.go

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 15761fe

Please sign in to comment.