retry on failed connection

This commit is contained in:
Michele Balistreri 2022-08-12 08:25:46 +02:00
parent a38640c721
commit cd9383e103
3 changed files with 24 additions and 12 deletions

19
flow.go
View File

@ -179,11 +179,11 @@ func (f *KeycardFlow) closeKeycard(kc *keycardContext) {
}
}
func (f *KeycardFlow) connect() *keycardContext {
func (f *KeycardFlow) connect() (*keycardContext, error) {
kc, err := startKeycardContext()
if err != nil {
return nil
return nil, restartErr()
}
f.pause(InsertCard, ErrorConnection, FlowParams{})
@ -192,23 +192,24 @@ func (f *KeycardFlow) connect() *keycardContext {
if f.state != Cancelling {
panic("Resuming is not expected during connection")
}
return nil
return nil, giveupErr()
case <-kc.connected:
if kc.runErr != nil {
return nil
return nil, restartErr()
}
f.state = Running
signal.Send(CardInserted, FlowStatus{})
return kc
return kc, nil
}
}
func (f *KeycardFlow) connectedFlow() (FlowStatus, error) {
kc := f.connect()
kc, err := f.connect()
defer f.closeKeycard(kc)
if kc == nil {
return nil, f.pauseAndRestart(InsertCard, ErrorConnection)
if err != nil {
return nil, err
}
if factoryReset, ok := f.params[FactoryReset]; ok && factoryReset.(bool) {
@ -219,7 +220,7 @@ func (f *KeycardFlow) connectedFlow() (FlowStatus, error) {
}
}
err := f.selectKeycard(kc)
err = f.selectKeycard(kc)
if err != nil {
return nil, err

View File

@ -11,7 +11,7 @@ import (
)
func (f *KeycardFlow) factoryReset(kc *keycardContext) error {
err := kc.factoryReset()
err := kc.factoryReset(true)
if err == nil {
delete(f.params, FactoryReset)

View File

@ -57,6 +57,7 @@ func (kc *keycardContext) start() error {
err = newKeycardError("cannot get readers")
l(err.Error())
close(kc.connected)
_ = kc.cardCtx.Release()
return err
}
@ -67,6 +68,7 @@ func (kc *keycardContext) start() error {
err = newKeycardError("no smartcard reader found")
l(err.Error())
close(kc.connected)
_ = kc.cardCtx.Release()
return err
}
@ -95,6 +97,7 @@ func (kc *keycardContext) run() {
l(err.Error())
kc.runErr = err
close(kc.connected)
_ = kc.cardCtx.Release()
return
}
@ -106,8 +109,10 @@ func (kc *keycardContext) run() {
card, err := kc.cardCtx.Connect(reader, scard.ShareShared, scard.ProtocolAny)
if err != nil {
// error connecting to card
l(err.Error())
kc.runErr = err
close(kc.connected)
_ = kc.cardCtx.Release()
return
}
@ -115,6 +120,7 @@ func (kc *keycardContext) run() {
if err != nil {
kc.runErr = err
close(kc.connected)
_ = kc.cardCtx.Release()
return
}
@ -465,7 +471,7 @@ func (kc *keycardContext) changePairingPassword(pairingPassword string) error {
return nil
}
func (kc *keycardContext) factoryReset() error {
func (kc *keycardContext) factoryReset(retry bool) error {
<-kc.connected
if kc.runErr != nil {
return kc.runErr
@ -491,7 +497,12 @@ func (kc *keycardContext) factoryReset() error {
if err := cmdSet.DeleteObject(aid); err != nil {
l("error deleting keycard aid %+v", err)
return err
if retry {
return kc.factoryReset(false)
} else {
return err
}
}
if err := cmdSet.InstallKeycardApplet(); err != nil {