@startuml
title Backend state machine

' This file documents the state machine as implemented in state.c. It
' can be converted to a diagram by way of PlantUML, see
' http://plantuml.com/
'
' This version of the state machine is for the benefit of people who
' want to use the eID Viewer backend library, and don't care about how
' everything is implemented. It therefore contains documents which
' external function call has which state machine transition as an
' effect, rather than which event (as in the other state machine
' diagram)

[*] --> LIBOPEN

LIBOPEN --> CALLBACKS : eid_vwr_createcallbacks()

state CALLBACKS {
  state NO_TOKEN
  state TOKEN
  state FILE
  state CARD_INVALID

  [*] --> NO_TOKEN

  NO_TOKEN --> FILE : eid_vwr_be_deserialize()
  FILE --> NO_TOKEN : eid_vwr_close_file()
  FILE --> TOKEN
  FILE --> NO_TOKEN

  TOKEN --> NO_TOKEN
  TOKEN --> CARD_INVALID : eid_vwr_be_set_invalid()

  CARD_INVALID --> NO_TOKEN

  state NO_TOKEN {
    state NO_READER
    state READY

    [*] --> NO_READER
    NO_READER --> READY

    READY --> TOKEN
  }

  state TOKEN {
    state TOKEN_ID
    state TOKEN_CERTS
    state TOKEN_ERROR
    state TOKEN_WAIT


    [*] --> TOKEN_ID

    TOKEN_ID --> TOKEN_CERTS
    TOKEN_ID --> TOKEN_ERROR

    TOKEN_CERTS --> TOKEN_WAIT
    TOKEN_CERTS --> TOKEN_ERROR
 
    state TOKEN_WAIT {
      state TOKEN_IDLE
      state TOKEN_PINOP
      state TOKEN_SERIALIZE
	  state TOKEN_CHALLENGE

      [*] --> TOKEN_IDLE

      TOKEN_IDLE --> TOKEN_PINOP : eid_vwr_pinop()
      TOKEN_IDLE --> TOKEN_SERIALIZE : eid_vwr_be_serialize()
	  TOKEN_IDLE --> TOKEN_CHALLENGE : eid_vwr_challenge()

      TOKEN_PINOP --> TOKEN_IDLE
      TOKEN_PINOP --> TOKEN_IDLE

      TOKEN_SERIALIZE --> TOKEN_IDLE
      TOKEN_SERIALIZE --> TOKEN_ERROR

      TOKEN_CHALLENGE --> TOKEN_IDLE
      TOKEN_CHALLENGE --> TOKEN_IDLE
    }

  }
}

@enduml
