2021-10-23 03:47:57 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"go.arsenm.dev/itd/internal/types"
|
|
|
|
)
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// DFUProgress stores the progress of a DFU upfate
|
2021-10-23 03:47:57 +00:00
|
|
|
type DFUProgress types.DFUProgress
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// UpgradeType indicates the type of upgrade to be performed
|
2021-10-23 03:47:57 +00:00
|
|
|
type UpgradeType uint8
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// Type of DFU upgrade
|
2021-10-23 03:47:57 +00:00
|
|
|
const (
|
|
|
|
UpgradeTypeArchive UpgradeType = iota
|
|
|
|
UpgradeTypeFiles
|
|
|
|
)
|
|
|
|
|
2021-10-23 04:01:18 +00:00
|
|
|
// FirmwareUpgrade initiates a DFU update and returns the progress channel
|
2021-10-23 03:47:57 +00:00
|
|
|
func (c *Client) FirmwareUpgrade(upgType UpgradeType, files ...string) (<-chan DFUProgress, error) {
|
|
|
|
err := json.NewEncoder(c.conn).Encode(types.Request{
|
|
|
|
Type: types.ReqTypeFwUpgrade,
|
|
|
|
Data: types.ReqDataFwUpgrade{
|
|
|
|
Type: int(upgType),
|
|
|
|
Files: files,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-24 01:03:17 +00:00
|
|
|
c.dfuProgressCh = make(chan types.Response, 5)
|
|
|
|
|
|
|
|
out := make(chan DFUProgress, 5)
|
|
|
|
go func() {
|
|
|
|
for res := range c.dfuProgressCh {
|
|
|
|
progress, err := decodeDFUProgress(res.Value)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out <- progress
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
2021-10-23 03:47:57 +00:00
|
|
|
}
|