Software APIs
Macros | Typedefs | Enumerations | Functions
simple_serial.h File Reference

(30d7e787c7)

Simple serial protocol for side-channel analysis. More...

#include <stddef.h>
#include <stdint.h>
#include "sw/device/lib/base/status.h"
#include "sw/device/lib/dif/dif_base.h"
#include "sw/device/lib/dif/dif_uart.h"

Go to the source code of this file.

Macros

#define SS_CHECK(condition)
 Sends an error message over UART if condition evaluates to false. More...
 
#define SS_CHECK_STATUS_OK(expr)
 Sends an error message over UART if the status represents an error. More...
 
#define SS_CHECK_DIF_OK(dif_call)
 Sends an error message over UART if DIF does not return kDifOk. More...
 

Typedefs

typedef enum simple_serial_result simple_serial_result_t
 Simple serial status codes.
 
typedef void(* simple_serial_command_handler) (const uint8_t *, size_t)
 Command handlers must conform to this prototype.
 

Enumerations

enum  simple_serial_result {
  kSimpleSerialOk = 0 ,
  kSimpleSerialError = 1
}
 Simple serial status codes.
 

Functions

void simple_serial_init (const dif_uart_t *uart)
 Initializes the data structures used by simple serial. More...
 
simple_serial_result_t simple_serial_register_handler (uint8_t cmd, simple_serial_command_handler handler)
 Registers a handler for a simple serial command. More...
 
void simple_serial_process_packet (void)
 Waits for a simple serial packet and dispatches it to the appropriate handler.
 
void simple_serial_send_packet (const uint8_t cmd, const uint8_t *data, size_t data_len)
 Sends a simple serial packet over UART. More...
 
void simple_serial_send_status (uint8_t res)
 Sends a simple serial status packer over UART. More...
 
void simple_serial_print_hex (const uint8_t *data, size_t data_len)
 Sends a buffer over UART as a hex encoded string. More...
 

Detailed Description

Simple serial protocol for side-channel analysis.

This library implements simple serial protocol version 1.1 and provides built-in handlers for 'v' (version) and 's' (seed PRNG) commands. Clients can implement additional command by registering their handlers using simple_serial_register_handler(). See https://wiki.newae.com/SimpleSerial for details on the protocol.

Definition in file simple_serial.h.

Macro Definition Documentation

◆ SS_CHECK

#define SS_CHECK (   condition)
Value:
do { \
if (!(condition)) { \
simple_serial_send_status(kSimpleSerialError); \
return; \
} \
} while (false)

Sends an error message over UART if condition evaluates to false.

Definition at line 29 of file simple_serial.h.

◆ SS_CHECK_DIF_OK

#define SS_CHECK_DIF_OK (   dif_call)
Value:
do { \
if (dif_call != kDifOk) { \
simple_serial_send_status(kSimpleSerialError); \
return; \
} \
} while (false)

Sends an error message over UART if DIF does not return kDifOk.

Definition at line 53 of file simple_serial.h.

◆ SS_CHECK_STATUS_OK

#define SS_CHECK_STATUS_OK (   expr)
Value:
do { \
status_t status_ = expr; \
if (!(status_ok(status_))) { \
unsigned char *buf = (unsigned char *)&status_.value; \
simple_serial_send_packet('z', buf, sizeof(status_.value)); \
return; \
} \
} while (false)

Sends an error message over UART if the status represents an error.

Definition at line 40 of file simple_serial.h.

Function Documentation

◆ simple_serial_init()

void simple_serial_init ( const dif_uart_t *  uart)

Initializes the data structures used by simple serial.

This function also registers handlers for 'v' (version) and 's' (seed PRNG) commands.

Parameters
uartHandle to an initialized UART device.

Definition at line 192 of file simple_serial.c.

◆ simple_serial_print_hex()

void simple_serial_print_hex ( const uint8_t *  data,
size_t  data_len 
)

Sends a buffer over UART as a hex encoded string.

Parameters
dataA buffer
data_lenSize of the buffer.

Definition at line 239 of file simple_serial.c.

◆ simple_serial_register_handler()

simple_serial_result_t simple_serial_register_handler ( uint8_t  cmd,
simple_serial_command_handler  handler 
)

Registers a handler for a simple serial command.

Clients cannot register handlers for 'v' (version) and 's' (seed PRNG) commands since these are handled by this library.

Parameters
cmdSimple serial command.
handlerCommand handler.

Definition at line 204 of file simple_serial.c.

◆ simple_serial_send_packet()

void simple_serial_send_packet ( const uint8_t  cmd,
const uint8_t *  data,
size_t  data_len 
)

Sends a simple serial packet over UART.

Parameters
cmdSimple serial command.
dataPacket payload.
data_lenPayload length.

Definition at line 225 of file simple_serial.c.

◆ simple_serial_send_status()

void simple_serial_send_status ( uint8_t  res)

Sends a simple serial status packer over UART.

Parameters
resStatus code.

Definition at line 235 of file simple_serial.c.