.. _cspifc:

==============
CSP Interfaces
==============

The Polaris receiver is operated remotely via `CubeSat Space Protocol (CSP) <http://www.libcsp.org/>`_ messages. The :ref:`polaris-client`, :ref:`prop-client`, and :ref:`btp` all contain wrapper function around CSP protocols. In addition, Polaris also implements the standard CSP service ports such as ``CSP_PING`` and ``CSP_REBOOT``.

Configuration CSP parameters
----------------------------

By default, the receiver uses address ``20`` but this can be changed by adjusting the address property ``sys.csp.address`` and saving the ``sys`` group. The system must be rebooted for the address change to take effect.

The receiver supports CSP via CAN-bus (``CAN`` interface), RS-422 (``KISS`` interface) and Ethernet (``UDP`` interface) in the P1 connector. The CAN and KISS interfaces are always enabled for incoming messages, but the default routing table sends egress messages on the CAN interface for all traffic except loopback.

The Ethernet/UDP interface must be enabled by setting ``sys.net.enable`` to ``true`` which powers on the Ethernet PHY and enables the IP stack. The system uses a MAC address starting with ``02:aa`` and ending with the serial number of the system (printed at boot time and available in the ``sys.serial`` property). The IP address of the system can be set using the ``sys.net.ip`` property. The UDP interface wraps CSP frames in UDP packets and accepts incoming CSP messages on port 53001. Outgoing frames are sent to port 53001 on a destination IP address constructed from the configured IP address with the least significant octet set to the destination CSP MAC address (typically equal to CSP address). With the default IP address of ``192.168.100.20``, outgoing frames for address 1 will e.g. be sent to ``192.168.100.1:53001``.

The current routing table can be listed using the ``csp route show`` command in the debug shell:

.. code-block:: none

    [polaris] csp route show 
    20/5 LOOP
    0/0 CAN

The results are listed using `CIDR notation <https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing>`_. CSP addresses are 5 bits long so, ``20/5`` is an exact match to address ``20``, i.e. the loopback address. ``0/0`` marks the default route. To adjust CSP routing, the ``sys.csp.routes`` property should be used. As an example, to send all traffic via the RS-422/KISS interface set the property to ``0/0 KISS`` and save the ``sys`` group. Additional routes can be added as comma-separated values. It is not necessary to add an explicit route for the loopback interface.

The CSP connection can be tested using the ``csp ping`` debug shell command. This can be combined with the ``watch`` command to e.g. ping a system 10 times and report the number of successful replies. This example pings address 1 with 100 byte frames:

.. code-block:: none

    [polaris] watch -c10 -s csp ping -s100 1
    Running command 'csp ping -s100 1' every 1000 ms
    Reply in 4 ms
    Reply in 4 ms
    Reply in 4 ms
    Reply in 4 ms
    Reply in 5 ms
    Reply in 5 ms
    Reply in 4 ms
    Reply in 4 ms
    Reply in 4 ms
    Reply in 6 ms
    10 executions, 10 success 0 failures
