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

View File

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

View File

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