Skip to content

Commit

Permalink
interface: add RAISE_CUSTOM policy
Browse files Browse the repository at this point in the history
  • Loading branch information
sgliner-ledger committed Nov 8, 2023
1 parent 2ad130b commit f42a2bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.13.0] - 2023-11-8

### Added
- interface: Adding RAISE_CUSTOM raise policy to allow custom definition of white-listed status.

## [1.12.0] - 2023-10-20

### Added:
Expand Down
5 changes: 4 additions & 1 deletion doc/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ By default, this policy is ``RaisePolicy.RAISE_ALL_BUT_0x9000``, which means the
backend will raise an :py:class:`ExceptionRAPDU <ragger.error.ExceptionRAPDU>`
if the :term:`APDU` returned by the :term:`application` does not end with
``b'0x9000'``, else returns a :py:class:`RAPDU <ragger.utils.structs.RAPDU>`
instance. This behavior can be change with the two other options:
instance. This behavior can be change with the three other options:

- ``RaisePolicy.RAISE_NOTHING``, where the backend will never raise, and always
returns a proper :py:class:`RAPDU <ragger.utils.structs.RAPDU>`.
- ``RaisePolicy.RAISE_ALL``, where the backend will always raise a
:py:class:`ExceptionAPDU <ragger.error.ExceptionRAPDU>`, whatever the status.
- ``RaisePolicy.RAISE_CUSTOM``, where the backend will raise a
:py:class:`ExceptionAPDU <ragger.error.ExceptionRAPDU>`, for :term:`APDU` ending with
status defined in ``whitelisted_status``.

From that, every higher-level error management can be performed on top of
``Ragger``.
14 changes: 11 additions & 3 deletions src/ragger/backend/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pathlib import Path
from time import time
from types import TracebackType
from typing import Optional, Type, Generator, Any
from typing import Optional, Type, Generator, Any, Iterable

from ragger.firmware import Firmware
from ragger.utils import pack_APDU, RAPDU, Crop
Expand All @@ -30,11 +30,15 @@ class RaisePolicy(Enum):
RAISE_NOTHING = auto()
RAISE_ALL_BUT_0x9000 = auto()
RAISE_ALL = auto()
RAISE_CUSTOM = auto()


class BackendInterface(ABC):

def __init__(self, firmware: Firmware, log_apdu_file: Optional[Path] = None):
def __init__(self,
firmware: Firmware,
log_apdu_file: Optional[Path] = None,
whitelisted_status: Iterable = ()):
"""Initializes the Backend
:param firmware: Which Firmware will be managed
Expand All @@ -50,6 +54,8 @@ def __init__(self, firmware: Firmware, log_apdu_file: Optional[Path] = None):
self.logger = get_default_logger()
self.apdu_logger = get_apdu_logger()

self.whitelisted_status = whitelisted_status

@property
def firmware(self) -> Firmware:
"""
Expand Down Expand Up @@ -94,7 +100,9 @@ def is_raise_required(self, rapdu: RAPDU) -> bool:
"""
return ((self.raise_policy == RaisePolicy.RAISE_ALL)
or ((self.raise_policy == RaisePolicy.RAISE_ALL_BUT_0x9000) and
(rapdu.status != 0x9000)))
(rapdu.status != 0x9000))
or ((self.raise_policy == RaisePolicy.RAISE_CUSTOM) and
(rapdu.status not in self.whitelisted_status)))

def send(self, cla: int, ins: int, p1: int = 0, p2: int = 0, data: bytes = b"") -> None:
"""
Expand Down

0 comments on commit f42a2bb

Please sign in to comment.