Skip to content

Commit

Permalink
nmxact: enable upload cancel by target device
Browse files Browse the repository at this point in the history
Signed-off-by: Mateo Ligne <[email protected]>
  • Loading branch information
mateo-ligne-netatmo committed Dec 16, 2021
1 parent f26e34e commit a921251
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions nmxact/xact/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const IMAGE_UPLOAD_START_WS = 1
const IMAGE_UPLOAD_DEF_MAX_WS = 5
const IMAGE_UPLOAD_STATUS_EXPECTED = 0
const IMAGE_UPLOAD_STATUS_RQ = 1
const IMAGE_UPLOAD_REFUSED = 10

type ImageUploadProgressFn func(c *ImageUploadCmd, r *nmp.ImageUploadRsp)
type ImageUploadCmd struct {
Expand Down Expand Up @@ -258,13 +259,15 @@ func (t *ImageUploadIntTracker) ProcessMissedChunks() {
}
}

func (t *ImageUploadIntTracker) HandleResponse(c *ImageUploadCmd, rsp nmp.NmpRsp, res *ImageUploadResult) bool {
func (t *ImageUploadIntTracker) HandleResponse(c *ImageUploadCmd, rsp nmp.NmpRsp, res *ImageUploadResult, updatable *bool) bool {
t.Mutex.Lock()
defer t.Mutex.Unlock()
wFull := false

if rsp != nil {
irsp := rsp.(*nmp.ImageUploadRsp)
if irsp.Rc == IMAGE_UPLOAD_REFUSED {
*updatable = false
}
res.Rsps = append(res.Rsps, irsp)
t.UpdateTracker(int(irsp.Off), IMAGE_UPLOAD_STATUS_RQ)

Expand Down Expand Up @@ -327,6 +330,7 @@ func (c *ImageUploadCmd) Run(s sesn.Sesn) (Result, error) {
ch := make(chan int)
rspc := make(chan nmp.NmpRsp, c.MaxWinSz)
errc := make(chan error, c.MaxWinSz)
var upd bool = true

t := ImageUploadIntTracker{
TuneWS: true,
Expand All @@ -349,6 +353,10 @@ func (c *ImageUploadCmd) Run(s sesn.Sesn) (Result, error) {
continue
}

if !upd {
goto out
}

t.Mutex.Lock()
r, err := nextImageUploadReq(s, c.Upgrade, c.Data, t.Off, c.ImageNum)
if err != nil {
Expand Down Expand Up @@ -379,7 +387,7 @@ func (c *ImageUploadCmd) Run(s sesn.Sesn) (Result, error) {
}
return
case rsp := <-rspc:
sig := t.HandleResponse(c, rsp, res)
sig := t.HandleResponse(c, rsp, res, &upd)
if sig {
<-ch
}
Expand All @@ -388,8 +396,12 @@ func (c *ImageUploadCmd) Run(s sesn.Sesn) (Result, error) {
}(int(r.Off))
}

out:
if int(t.MaxRxOff) == len(c.Data) {
return res, nil
} else if !upd{
return nil, fmt.Errorf("ImageUpload aborted by receiver after %d/%d bytes",
t.MaxRxOff, len(c.Data))
} else {
return nil, fmt.Errorf("ImageUpload unexpected error after %d/%d bytes",
t.MaxRxOff, len(c.Data))
Expand Down

0 comments on commit a921251

Please sign in to comment.