Add progress to DFU

This commit is contained in:
Elara 2021-08-21 00:04:29 -07:00
parent fd9d97629a
commit 213fe39b21

29
dfu.go
View File

@ -55,6 +55,12 @@ var (
var btOptsCmd = map[string]interface{}{"type": "command"} var btOptsCmd = map[string]interface{}{"type": "command"}
type DFUProgress struct {
Sent int `json:"sent"`
Received int `json:"recvd"`
Total int64 `json:"total"`
}
// DFU stores everything required for doing firmware upgrades // DFU stores everything required for doing firmware upgrades
type DFU struct { type DFU struct {
initPacket fs.File initPacket fs.File
@ -64,6 +70,7 @@ type DFU struct {
bytesSent int bytesSent int
bytesRecvd int bytesRecvd int
fwSendDone bool fwSendDone bool
progress chan DFUProgress
ctrlPointChar *gatt.GattCharacteristic1 ctrlPointChar *gatt.GattCharacteristic1
packetChar *gatt.GattCharacteristic1 packetChar *gatt.GattCharacteristic1
} }
@ -169,8 +176,28 @@ func getFlSize(fl fs.File) (int64, error) {
return flInfo.Size(), nil return flInfo.Size(), nil
} }
func (dfu *DFU) Progress() <-chan DFUProgress {
if dfu.progress == nil {
dfu.progress = make(chan DFUProgress, 5)
}
return dfu.progress
}
func (dfu *DFU) sendProgress() {
dfu.progress <- DFUProgress{
Sent: dfu.bytesSent,
Received: dfu.bytesRecvd,
Total: dfu.fwSize,
}
}
// Start DFU process // Start DFU process
func (dfu *DFU) Start() error { func (dfu *DFU) Start() error {
if dfu.progress == nil {
dfu.progress = make(chan DFUProgress, 5)
}
defer close(dfu.progress)
if dfu.fwImage == nil || dfu.initPacket == nil { if dfu.fwImage == nil || dfu.initPacket == nil {
return ErrDFUNoFilesLoaded return ErrDFUNoFilesLoaded
} }
@ -325,6 +352,7 @@ func (dfu *DFU) stepSeven() error {
n, err := dfu.fwImage.Read(segment) n, err := dfu.fwImage.Read(segment)
// If EOF, send is done // If EOF, send is done
if err == io.EOF { if err == io.EOF {
dfu.sendProgress()
dfu.fwSendDone = true dfu.fwSendDone = true
return nil return nil
} else if err != nil { } else if err != nil {
@ -345,6 +373,7 @@ func (dfu *DFU) stepSeven() error {
if dfu.bytesRecvd != dfu.bytesSent { if dfu.bytesRecvd != dfu.bytesSent {
return ErrDFUSizeMismatch return ErrDFUSizeMismatch
} }
dfu.sendProgress()
return nil return nil
}) })
if err != nil { if err != nil {