Python engine

*** PRELIMINARY ***

Description

Some of the SpringCore products configured in Smart Reader Mode feature a lightweight Python runtime, nickame ‘PySCore’.

This scripting engine is suitable to implement a NFC transaction that is too complex for the regular NFC Templates engine, and/or to format the data according to a format that is not natively supported by the product.

The product supports three scripts, that are launched in response to specific events.

  • startup runs every time the product starts,
  • processNfc runs every time a NFC card has been found and is ready for processing,
  • output runs every time NFC (or BLE data) is available, and ready to be sent to the host.

Some support modules are available:

  • springcore: this module gives access to most features of the SpringCore product itself.
  • springcore.scard: this module gives access the smart card interfaces of the product, including (of course) the NFC interface for contactless cards and RFID tags, but also the SAM slot(s), if the product has some. For convenience, this module has been inspired by pyscard, a Python framework for building smart card aware applications over PC/SC.
  • logging: a logging facility, to send debug messages to the host.
  • utils: a collection of utilities.
  • mbedtls: a port of python-mbedtls that opens access to the cryptographic functions of the device.

Specificities

SpringCore is a size-constrained platform that uses an embedded kernel (and not a complete OS with a file system and a virtually unlimited memory); as a result, the Python engine is considerably limited in terms of capabilities and flexibility (which does not, fortunately, prevent it from covering many use cases). The main restrictions are:

  • Single file scripts only: every script is a standalone file. Except for the built-in support modules that are provided by the firmware itself, the import clause is not supported.
  • Size limit: every script must fit in a 8kB text file. Comments are welcome, but keep in mind that comments are counted in the actual size!
  • Memory limit: every script must run within 8kB RAM. A script that requires too much memory would be killed by the runtime.
  • Time limit: every script must run within 1 second. A script that last more than 1000ms would be killed by the runtime.

Console, logging and exceptions

Python standard print function is a dummy function under PyScore. Calling print will not raise an error, but will not produce anything. Generally speaking, there is no stdout stream in the SpringCore device.

To follow the execution stream, use the logging module. This module sends its messages to the USB host using the SpringCore Direct protocol, where they can be shown by the SpringCoreTrace application. If there is no active USB connection, logging does not produce anything.

Python exceptions (and everything that would normally be echoed to stderr) are redirected to logging.critical.