Skip to content

Commit e65b2ed

Browse files
committed
feat: use external CRC mod
Signed-off-by: Artur Troian <[email protected]>
1 parent 2f2855f commit e65b2ed

File tree

7 files changed

+36
-156
lines changed

7 files changed

+36
-156
lines changed

main.go renamed to cmd/ymodem/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/NotifAi/serial"
1414
"github.com/spf13/cobra"
1515

16+
ytypes "github.com/NotifAi/ymodem/types"
1617
"github.com/NotifAi/ymodem/ymodem"
1718
)
1819

@@ -31,7 +32,7 @@ func main() {
3132
Long: ``,
3233
Run: func(cmd *cobra.Command, args []string) {
3334
// Open connection
34-
connection, err := serial.OpenPort(&serial.Config{Name: port, Baud: 115200})
35+
connection, err := serial.OpenPort(serial.Config{Name: port, Baud: 115200})
3536
if err != nil {
3637
log.Fatalln(err)
3738
}
@@ -85,7 +86,7 @@ func main() {
8586
files = append(files, ymodem.File{Data: data, Name: filepath.Base(f)})
8687
}
8788

88-
bar := newProgress()
89+
bar := ytypes.NewProgress()
8990
// Send files
9091
err = ymodem.ModemSend(connection, bar, bSize, files)
9192

@@ -109,7 +110,7 @@ func main() {
109110
Long: ``,
110111
Run: func(cmd *cobra.Command, args []string) {
111112
// Open connection
112-
connection, err := serial.OpenPort(&serial.Config{Name: port, Baud: 115200})
113+
connection, err := serial.OpenPort(serial.Config{Name: port, Baud: 115200})
113114
if err != nil {
114115
log.Fatalln(err)
115116
}

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ go 1.13
44

55
require (
66
github.com/NotifAi/serial v0.2.3
7-
github.com/onsi/ginkgo v1.10.1
8-
github.com/onsi/gomega v1.7.0
7+
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6
98
github.com/spf13/cobra v0.0.5
109
github.com/vbauerster/mpb/v4 v4.9.3
1110
)

go.sum

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2-
github.com/NotifAi/serial v0.1.0 h1:nYrfuZB/Fn55DIurJ9F8rIE3Eq5kriPUC4V1BCYY95U=
3-
github.com/NotifAi/serial v0.1.0/go.mod h1:xT0Wsw9XDw01/Zx7JM7JVchvDxzetEvQ6PKyqyIp/xg=
4-
github.com/NotifAi/serial v0.2.0/go.mod h1:xT0Wsw9XDw01/Zx7JM7JVchvDxzetEvQ6PKyqyIp/xg=
5-
github.com/NotifAi/serial v0.2.2/go.mod h1:xT0Wsw9XDw01/Zx7JM7JVchvDxzetEvQ6PKyqyIp/xg=
2+
github.com/NotifAi/serial v0.2.3 h1:kfD6mn4e04/m/braL9S5LKtDJMRmuqvFsfyjQUtnrP0=
3+
github.com/NotifAi/serial v0.2.3/go.mod h1:xT0Wsw9XDw01/Zx7JM7JVchvDxzetEvQ6PKyqyIp/xg=
64
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
75
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
86
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
@@ -13,21 +11,14 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
1311
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1412
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
1513
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
16-
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
17-
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1814
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
19-
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
20-
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
15+
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6 h1:IIVxLyDUYErC950b8kecjoqDet8P5S4lcVRUOM6rdkU=
16+
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6/go.mod h1:JslaLRrzGsOKJgFEPBP65Whn+rdwDQSk0I0MCRFe2Zw=
2117
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
2218
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
2319
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
2420
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
2521
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
26-
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
27-
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
28-
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
29-
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
30-
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
3122
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
3223
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3324
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
@@ -48,12 +39,8 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf
4839
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4940
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 h1:Gv7RPwsi3eZ2Fgewe3CBsuOebPwO27PoXzRpJPsvSSM=
5041
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
51-
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
5242
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
5343
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
54-
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
55-
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
56-
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5744
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5845
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5946
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -65,10 +52,5 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
6552
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6653
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6754
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
68-
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
69-
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
70-
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
71-
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
72-
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7355
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
7456
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

bar.go renamed to types/bar.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
package main
1+
package ytypes
22

33
import (
44
"time"
55

66
"github.com/vbauerster/mpb/v4"
77
"github.com/vbauerster/mpb/v4/decor"
8-
9-
ytypes "github.com/NotifAi/ymodem/types"
108
)
119

1210
type bars struct {
@@ -19,18 +17,18 @@ type bar struct {
1917
startTs time.Time
2018
}
2119

22-
var _ ytypes.Progress = (*bars)(nil)
23-
var _ ytypes.Bar = (*bar)(nil)
20+
var _ Progress = (*bars)(nil)
21+
var _ Bar = (*bar)(nil)
2422

25-
func newProgress() ytypes.Progress {
23+
func NewProgress() Progress {
2624
b := &bars{
2725
b: mpb.New(mpb.WithWidth(64)),
2826
}
2927

3028
return b
3129
}
3230

33-
func (b *bars) Create(name string, ln int) ytypes.Bar {
31+
func (b *bars) Create(name string, ln int) Bar {
3432
br := &bar{
3533
b: b.b.AddBar(int64(ln),
3634
mpb.BarClearOnComplete(),

xmodem/xmodem_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

ymodem/ymodem.go

Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"io"
88

9+
"github.com/howeyc/crc16"
10+
911
ytypes "github.com/NotifAi/ymodem/types"
1012
)
1113

@@ -26,69 +28,17 @@ type File struct {
2628
bytesBar ytypes.Bar
2729
}
2830

29-
func CRC16(data []byte) uint16 {
30-
var u16CRC uint16 = 0
31-
32-
for _, character := range data {
33-
part := uint16(character)
34-
35-
u16CRC = u16CRC ^ (part << 8)
36-
for i := 0; i < 8; i++ {
37-
if u16CRC&0x8000 > 0 {
38-
u16CRC = u16CRC<<1 ^ 0x1021
39-
} else {
40-
u16CRC = u16CRC << 1
41-
}
42-
}
43-
}
44-
45-
return u16CRC
46-
}
47-
48-
func CRC16Constant(data []byte, length int) uint16 {
49-
var u16CRC uint16 = 0
50-
51-
for _, character := range data {
52-
part := uint16(character)
53-
54-
u16CRC = u16CRC ^ (part << 8)
55-
for i := 0; i < 8; i++ {
56-
if u16CRC&0x8000 > 0 {
57-
u16CRC = u16CRC<<1 ^ 0x1021
58-
} else {
59-
u16CRC = u16CRC << 1
60-
}
61-
}
62-
}
63-
64-
for c := 0; c < length-len(data); c++ {
65-
u16CRC = u16CRC ^ (0x04 << 8)
66-
for i := 0; i < 8; i++ {
67-
if u16CRC&0x8000 > 0 {
68-
u16CRC = u16CRC<<1 ^ 0x1021
69-
} else {
70-
u16CRC = u16CRC << 1
71-
}
72-
}
73-
}
74-
75-
return u16CRC
76-
}
77-
7831
func sendBlock(c io.ReadWriter, bs int, block uint8, data []byte) error {
79-
// send STX
80-
if _, err := c.Write([]byte{SOH}); err != nil {
81-
return err
82-
}
83-
if _, err := c.Write([]byte{block}); err != nil {
84-
return err
85-
}
86-
if _, err := c.Write([]byte{255 - block}); err != nil {
87-
return err
32+
var toSend bytes.Buffer
33+
34+
if bs == 128 {
35+
toSend.WriteByte(SOH)
36+
} else {
37+
toSend.WriteByte(STX)
8838
}
8939

90-
// send data
91-
var toSend bytes.Buffer
40+
toSend.WriteByte(block) // block id
41+
toSend.WriteByte(255 - block) // 2nd complement to block id
9242
toSend.Write(data)
9343

9444
padding := bs - len(data)
@@ -97,10 +47,9 @@ func sendBlock(c io.ReadWriter, bs int, block uint8, data []byte) error {
9747
toSend.Write(buf)
9848
}
9949

100-
// calc CRC
101-
u16CRC := CRC16Constant(data, bs)
102-
toSend.Write([]byte{uint8(u16CRC >> 8)})
103-
toSend.Write([]byte{uint8(u16CRC & 0x0FF)})
50+
crc := crc16.ChecksumCCITTFalse(toSend.Bytes()[3:])
51+
toSend.Write([]byte{uint8(crc >> 8)})
52+
toSend.Write([]byte{uint8(crc & 0x0FF)})
10453

10554
sent := 0
10655
for sent < toSend.Len() {
@@ -125,6 +74,12 @@ func ModemSend(c io.ReadWriter, progress ytypes.Progress, bs int, files []File)
12574
_, _ = c.Write([]byte{CAN, CAN})
12675
}
12776

77+
padding := make([]byte, bs)
78+
79+
for i := range padding {
80+
padding[i] = 0
81+
}
82+
12883
var err error
12984

13085
defer func() {
@@ -167,8 +122,8 @@ func ModemSend(c io.ReadWriter, progress ytypes.Progress, bs int, files []File)
167122
send.WriteString(files[fi].Name)
168123
send.WriteByte(0x0)
169124
send.WriteString(fmt.Sprintf("%d ", len(files[fi].Data)))
170-
for send.Len() < bs {
171-
send.Write([]byte{0x0})
125+
if send.Len() < bs {
126+
send.Write(padding[:bs-send.Len()])
172127
}
173128

174129
if err = sendBlock(c, bs, 0, send.Bytes()); err != nil {
@@ -340,8 +295,7 @@ func receivePacket(c io.ReadWriter, bs int) ([]byte, error) {
340295
crc |= uint16(oBuffer[0])
341296

342297
// Calculate CRC
343-
crcCalc := CRC16(pData.Bytes())
344-
if crcCalc != crc {
298+
if crc16.ChecksumCCITTFalse(pData.Bytes()) != crc {
345299
if _, err := c.Write([]byte{NAK}); err != nil {
346300
return nil, err
347301
}

ymodem/ymodem_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)