Skip to content

Commit

Permalink
Merge pull request #2 from samsamfire/dev
Browse files Browse the repository at this point in the history
PR for v1.1
  • Loading branch information
samsamfire authored Dec 30, 2023
2 parents 2fcfedc + 7b98258 commit 208a51c
Show file tree
Hide file tree
Showing 48 changed files with 3,108 additions and 1,274 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Binary file modified README.md
Binary file not shown.
9 changes: 1 addition & 8 deletions cmd/canopen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ func main() {
if err != nil {
panic(err)
}
node.OD.AddFile(0x3003, "File", "example2.bin", os.O_RDONLY|os.O_CREATE, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
err = node.GetOD().AddFile(0x3003, "File", "example2.bin", os.O_RDONLY|os.O_CREATE, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
e := network.Process()
if e != nil {
panic(e)
}
}
1 change: 0 additions & 1 deletion cmd/canopen_http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func main() {
if e != nil {
panic(e)
}
go func() { network.Process() }()
gateway := canopen.NewGateway(1, 1, 100, &network)
gateway.ListenAndServe(fmt.Sprintf(":%d", DEFAULT_HTTP_PORT))

Expand Down
10 changes: 2 additions & 8 deletions cmd/canopen_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ func main() {
panic(err)
}
// Add file extension
node.OD.AddFile(0x200F, "File", "example.bin", os.O_RDONLY|os.O_CREATE, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
err = node.GetOD().AddFile(0x200F, "File", "example.bin", os.O_RDONLY|os.O_CREATE, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
e := network.Process()
if e != nil {
panic(e)
}
select {}
}
13 changes: 6 additions & 7 deletions cmd/sdo_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ func main() {
flag.Parse()

network := canopen.NewNetwork(nil)
e := network.Connect("", *channel, 500000)
if e != nil {
panic(e)
err := network.Connect("", *channel, 500000)
if err != nil {
panic(err)
}
go func() { network.Process() }()

// Load corresponding OD to be able to read values from strings
e = network.AddNode(0x10, "../../testdata/base.eds")
if e != nil {
panic(e)
_, err = network.AddNode(0x10, "../../testdata/base.eds", false)
if err != nil {
panic(err)
}
network.Read(0x10, "INTEGER16 value", "")
network.Read(0x10, "INTEGER8 value", "")
Expand Down
11 changes: 6 additions & 5 deletions crc_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package canopen

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCcittSingle(t *testing.T) {
crc := CRC16(0)
crc.ccittSingle(10)
if crc != 0xA14A {
t.Errorf("Was expecting 0xA14A, got %x", crc)
}

assert.EqualValues(t, 0xA14A, crc)
}
6 changes: 2 additions & 4 deletions emergency.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,7 @@ func NewEM(
emergency.Fifo = make([]EMFifo, fifoSize)

// Get cob id initial & verify
cobIdEmergency := uint32(0)
ret := entry1014.Uint32(0, &cobIdEmergency)
cobIdEmergency, ret := entry1014.Uint32(0)
if ret != nil || (cobIdEmergency&0x7FFFF800) != 0 {
// Don't break if only value is wrong
if ret != nil {
Expand All @@ -575,8 +574,7 @@ func NewEM(
emergency.txBuffer = NewFrame(producerCanId, 0, 8)
emergency.InhibitEmTimeUs = 0
emergency.InhibitEmTimer = 0
inhibitTime100us := uint16(0)
ret = entry1015.Uint16(0, &inhibitTime100us)
inhibitTime100us, ret := entry1015.Uint16(0)
if ret == nil {
emergency.InhibitEmTimeUs = uint32(inhibitTime100us) * 100
entry1015.AddExtension(emergency, ReadEntryDefault, WriteEntry1015)
Expand Down
70 changes: 26 additions & 44 deletions fifo_test.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,60 @@
package canopen

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFifoWrite(t *testing.T) {
fifo := NewFifo(100)
res := fifo.Write([]byte{1, 2, 3, 4, 5}, nil)
if res != 5 {
t.Errorf("Written only %v", res)
}
if fifo.writePos != 5 {
t.Errorf("Write position is %v", fifo.writePos)
}
if fifo.readPos != 0 {
t.Error()
}
assert.Equal(t, 5, res)
assert.Equal(t, 5, fifo.writePos)
assert.Equal(t, 0, fifo.readPos)

res = fifo.Write(make([]byte, 500), nil)
if res != 94 {
t.Errorf("Wrote %v", res)
}
assert.Equal(t, 94, res)
res = fifo.Write([]byte{1}, nil)
if res != 0 {
t.Error()
}
assert.Equal(t, 0, res)

// Free up some space by reading then re writing
var eof bool = false
fifo.Read(make([]byte, 10), &eof)
res = fifo.Write(make([]byte, 10), nil)
if res != 10 {
t.Error()
}

assert.Equal(t, 10, res)
}

func TestFifoRead(t *testing.T) {
fifo := NewFifo(100)
receive_buffer := make([]byte, 10)
var eof bool = false
res := fifo.Read(receive_buffer, &eof)
if res != 0 {
t.Error()
}
assert.Equal(t, 0, res)

// Write to fifo
res = fifo.Write([]byte{1, 2, 3, 4}, nil)
if res != 4 && fifo.writePos != 4 {
t.Error()
}
assert.Equal(t, 4, res)
assert.Equal(t, 4, fifo.writePos)
res = fifo.Read(receive_buffer, &eof)
if res != 4 {
t.Errorf("Res is %v", res)
}
assert.Equal(t, 4, res)
}

func TestFifoAltRead(t *testing.T) {
fifo := NewFifo(101)
if fifo.AltGetOccupied() != 0 {
t.Fatal("fifo should be empty")
}
assert.Equal(t, 0, fifo.AltGetOccupied())

rxBuffer := make([]byte, 7)
res := fifo.AltRead(rxBuffer)
if res != 0 {
t.Error()
}
assert.Equal(t, 0, res)

// Write to fifo
for i := 0; i < 10; i++ {
res = fifo.Write([]byte("1234567891"), nil)
if res != 10 {
t.Fatalf("should be exactly 10, got %v", res)
}
assert.Equal(t, 10, res)
}
res = fifo.AltRead(rxBuffer)
if res != 7 || string(rxBuffer) != "1234567" {
t.Fatal("alt read problem")
}
if fifo.AltGetOccupied() != 93 {
t.Fatalf("should be 93 left, instead %v", fifo.AltGetOccupied())
}
assert.Equal(t, 7, res)
assert.Equal(t, "1234567", string(rxBuffer))
assert.Equal(t, 93, fifo.AltGetOccupied())
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ require (
gopkg.in/ini.v1 v1.67.0
)

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -18,3 +25,5 @@ gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 1 addition & 2 deletions heartbeat_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ func NewHBConsumer(busManager *BusManager, em *EM, entry1016 *Entry) (*HBConsume
log.Debugf("[HB CONSUMER] %v possible entries for nodes to monitor", consumer.nbMonitoredNodes)
consumer.monitoredNodes = make([]HBConsumerNode, consumer.nbMonitoredNodes)
for index := 0; index < int(consumer.nbMonitoredNodes); index++ {
var hbConsValue uint32
err := entry1016.Uint32(uint8(index)+1, &hbConsValue)
hbConsValue, err := entry1016.Uint32(uint8(index) + 1)
if err != nil {
log.Errorf("[HB CONSUMER][%x|%x] reading %v failed : %v", entry1016.Index, index+1, entry1016.Name, err)
return nil, ErrOdParameters
Expand Down
5 changes: 1 addition & 4 deletions http_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ func createGateway() *HTTPGatewayServer {
if e != nil {
panic(e)
}
e = network.AddNode(NODE_ID_TEST, "testdata/base.eds")
_, e = network.AddNode(NODE_ID_TEST, "testdata/base.eds", true)
if e != nil {
panic(e)
}
gateway := NewGateway(1, 1, 100, &network)
go func() {
network.Process()
}()
return gateway
}

Expand Down
Loading

0 comments on commit 208a51c

Please sign in to comment.