Midnight Sun Firmware
Loading...
Searching...
No Matches
FOTA

Firmware Over the Air (FOTA) Update system. More...

Collaboration diagram for FOTA:

Classes

struct  FotaDatagramHeaderPacketPayload
 Structure representing the datagram header (In the payload) More...
 
struct  FotaDatagram
 Structure representing datagram. More...
 
struct  FotaPacket
 FOTA Packet structure. More...
 
struct  GpioAddress
 Port and pin data. More...
 
struct  UartSettings
 UART Settings. More...
 
struct  NetworkBuffer
 struct containing details of circular buffer More...
 
struct  PacketManager
 Packet manager context structure. More...
 

Macros

#define FOTA_MAX_DATAGRAM_SIZE   (8U * 256U)
 Maximum supported datagram size (2KB)
 
#define FOTA_MAX_PACKETS_PER_DATAGRAM   ((FOTA_MAX_DATAGRAM_SIZE / FOTA_PACKET_PAYLOAD_SIZE) + 1U)
 Maximum number of packets that can make up a datagram (8 data + 1 Header)
 
#define NUM_FLASH_PAGES   128U
 256KB of memory / 2KB per page
 
#define FOTA_PAGE_BYTES   ((uint32_t)0x800U)
 2KB per flash page as per datasheet
 
#define FOTA_PING_BYTES   ((uint32_t)0x1000U)
 4KB max per ping
 
#define FOTA_FLASH_WORD_SIZE   4U
 ARM32 Word size is 4 bytes.
 
#define FOTA_ADDR_TO_PAGE(addr)   (((uintptr_t)(addr) - (uintptr_t)APPLICATION_START_ADDRESS) / FOTA_PAGE_BYTES)
 Convert memory address to page number. More...
 
#define FOTA_PAGE_TO_ADDR(page)   ((uintptr_t)(page) * (uintptr_t)FOTA_PAGE_BYTES + (uintptr_t)APPLICATION_START_ADDRESS)
 Convert page number to memory address. More...
 
#define LOG_FOTA_DEBUG(msg)   printf("[FOTA DEBUG]: %s\n");
 Debug log.
 
#define LOG_FOTA_WARNING(msg)   printf("[FOTA WARNING]: %s\n");
 Warning log.
 
#define LOG_FOTA_ERROR(msg)   printf("[FOTA ERROR]: %s\n");
 Error log.
 
#define FLASH_START_ADDRESS_LINKERSCRIPT   ((uint32_t) & _flash_start)
 Flash start address as defined in the linkerscripts.
 
#define FLASH_SIZE_LINKERSCRIPT   ((size_t) & _flash_size)
 Flash size as defined in the linkerscripts.
 
#define FLASH_PAGE_SIZE_LINKERSCRIPT   ((size_t) & _flash_page_size)
 Flash page size as defined in the linkerscripts.
 
#define APPLICATION_START_ADDRESS   ((uint32_t) & _application_start)
 Application start address as defined in the linkerscripts.
 
#define APPLICATION_SIZE   ((size_t) & _application_size)
 Application size as defined in the linkerscripts.
 
#define BOOTLOADER_START_ADDRESS   ((uint32_t) & _bootloader_start)
 Bootloader start address as defined in the linkerscripts.
 
#define BOOTLOADER_SIZE   ((size_t) & _bootloader_size)
 Bootloader size as defined in the linkerscripts.
 
#define SRAM_START_ADDRESS   ((uint32_t) & _sram_start)
 SRAM start address as defined in the linkerscripts.
 
#define SRAM_SIZE   ((size_t) & _sram_size)
 SRAM size as defined in the linkerscripts.
 
#define FOTA_PACKET_PAYLOAD_SIZE   256U
 FOTA packet payload size. More...
 
#define FOTA_PACKET_SOF   0xAAU
 FOTA packet start of frame.
 
#define FOTA_PACKET_EOF   0xBBU
 FOTA packet end of frame.
 
#define FOTA_PACKET_MINIMUM_SIZE   14U
 FOTA packet minimum size.
 
#define FOTA_TIMEOUT_MS   15000U
 
#define UART_MAX_BUFFER_LEN   256U
 Maximum size permitted for a UART transaction.
 
#define UART_TIMEOUT_MS   1U
 Maximum time permitted for a UART transaction.
 
#define FOTA_UART_TIMEOUT_MS   10U
 Maximum time permitted for a FOTA UART transaction.
 
#define NETWORK_BUFFER_SIZE   1024U
 
#define FOTA_MAX_ACTIVE_DATAGRAMS   2U
 

Enumerations

enum  FotaDatagramType {
  FOTA_DATAGRAM_TYPE_FIRMWARE_METADATA , FOTA_DATAGRAM_TYPE_FIRMWARE_CHUNK , FOTA_DATAGRAM_TYPE_UPDATE_REQUEST , FOTA_DATAGRAM_TYPE_UPDATE_RESPONSE ,
  FOTA_DATAGRAM_TYPE_VERIFICATION
}
 Datagram types corresponding to different FOTA operations. More...
 
enum  FotaError {
  FOTA_ERROR_SUCCESS , FOTA_ERROR_RESOURCE_EXHAUSTED , FOTA_ERROR_INVALID_ARGS , FOTA_ERROR_INTERNAL_ERROR ,
  FOTA_ERROR_CRC32_MISMATCH , FOTA_ERROR_CRC32_DATA_NOT_ALIGNED , FOTA_ERROR_FLASH_WRITE_FAILED , FOTA_ERROR_FLASH_WRITE_OUT_OF_BOUNDS ,
  FOTA_ERROR_FLASH_WRITE_NOT_ALIGNED , FOTA_ERROR_FLASH_READ_FAILED , FOTA_ERROR_FLASH_ERASE , FOTA_ERROR_FLASH_VERIFICATION_FAILED ,
  FOTA_ERROR_INVALID_PACKET , FOTA_ERROR_NO_MEMORY , FOTA_ERROR_NO_DATAGRAM_FOUND , FOTA_ERROR_JUMP_FAILED
}
 FOTA Error code definitions. More...
 
enum  FotaJumpRequest { FOTA_JUMP_APPLICATION , FOTA_JUMP_BOOTLOADER , FOTA_JUMP_MIDSUN_BIOS }
 
enum  FotaPacketType {
  FOTA_PACKET_TYPE_DATA , FOTA_PACKET_TYPE_HEADER , FOTA_PACKET_TYPE_ACK , FOTA_PACKET_TYPE_NACK ,
  FOTA_PACKET_TYPE_ERROR
}
 FOTA Packet Type. More...
 
enum  UartPort {
  UART_PORT_1 = 0 , UART_PORT_2 , NUM_UART_PORTS , UART_PORT_1 = 0 ,
  UART_PORT_2 , NUM_UART_PORTS
}
 UART Port selection.
 
enum  UartFlowControl {
  UART_FLOW_CONTROL_NONE , UART_FLOW_CONTROL_RTS , UART_FLOW_CONTROL_CTS , UART_FLOW_CONTROL_RTS_CTS ,
  UART_FLOW_CONTROL_NONE , UART_FLOW_CONTROL_RTS , UART_FLOW_CONTROL_CTS , UART_FLOW_CONTROL_RTS_CTS
}
 UART Flow control selection. More...
 
enum  GpioPort {
  GPIO_PORT_A = 0 , GPIO_PORT_B , NUM_GPIO_PORTS , GPIO_PORT_A = 0 ,
  GPIO_PORT_B , GPIO_PORT_C , GPIO_PORT_D , GPIO_PORT_E ,
  GPIO_PORT_H , NUM_GPIO_PORTS
}
 UART Settings. More...
 
enum  PacketReceiverState {
  PKT_STATE_WAITING_SOF , PKT_STATE_READING_HEADER , PKT_STATE_READING_PAYLOAD , PKT_STATE_READING_CRC ,
  PKT_STATE_READING_EOF
}
 State machine states for packet receiving. More...
 

Functions

FotaError fota_init ()
 
void fota_process (void)
 
FotaError fota_datagram_init (FotaDatagram *datagram, FotaDatagramType type, uint32_t datagram_id, uint8_t *data, uint32_t length)
 Initialize a datagram structure. More...
 
FotaError fota_datagram_to_packets (FotaDatagram *datagram, FotaPacket *packets, uint16_t *num_packets, uint16_t max_packets)
 Fragment a datagram into packets for transmission. More...
 
FotaError fota_datagram_process_data_packet (FotaDatagram *datagram, FotaPacket *packet)
 Process a received packet and incorporate it into a datagram. More...
 
FotaError fota_datagram_process_header_packet (FotaDatagram *datagram, FotaPacket *packet)
 Process a received header packet and incorporate it into a datagram. More...
 
bool fota_datagram_is_complete (FotaDatagram *datagram)
 Check if a datagram has been completely received. More...
 
FotaError fota_datagram_verify (FotaDatagram *datagram)
 Verify the integrity of a complete datagram. More...
 
FotaError fota_encryption_init ()
 Initialize the encryption unit. More...
 
uint32_t fota_calculate_crc32 (uint8_t *data, uint32_t length_words)
 Calculate the CRC32 for a given buffer of 4-byte aligned data. More...
 
FotaError fota_verify_packet_encryption (FotaPacket *packet)
 Verify a FOTA packets encryption. More...
 
FotaError fota_verify_datagram_encryption (FotaDatagram *datagram)
 Verify a FOTA datagrams encryption. More...
 
FotaError fota_flash_write (uintptr_t address, uint8_t *buffer, uint32_t buffer_len)
 Write to flash memory. More...
 
FotaError fota_flash_erase (uint8_t start_page, uint8_t num_pages)
 Erase some number of flash pages. More...
 
FotaError fota_flash_read (uintptr_t address, uint8_t *buffer, uint32_t buffer_len)
 Read from flash memory. More...
 
FotaError fota_verify_flash_memory (void)
 Verify that the flash memory is not erased. More...
 
FotaError fota_jump (FotaJumpRequest jump_request)
 
FotaError fota_packet_init (FotaPacket *packet, FotaPacketType type, uint8_t sequence, uint16_t length)
 Initialize a packet structure. More...
 
FotaError fota_packet_set_crc (FotaPacket *packet)
 Calculate and set the CRC for a packet. More...
 
FotaError fota_packet_serialize (FotaPacket *packet, uint8_t *buffer, uint32_t buf_size, uint32_t *bytes_written)
 Serialize packet into a buffer for transmission. More...
 
FotaError fota_packet_deserialize (FotaPacket *packet, uint8_t *buffer, uint32_t buf_size)
 Deserialize packet from received data. More...
 
bool fota_is_timed_out (void)
 Checks if FOTA has timed out, meaning no requests have been received. More...
 
FotaError network_init (UartPort uart, UartSettings *settings, NetworkBuffer *network_buffer)
 Initialize UART instance, assuming standard 8 bits 1 stop bit. More...
 
FotaError network_read (UartPort uart, uint8_t *data, size_t len)
 Read data from the network buffer. More...
 
FotaError network_tx (UartPort uart, uint8_t *data, size_t len)
 Transmit data from the UART port. More...
 
bool isTimeout (bool is_tx)
 Checks if tx or rx command has timed out. More...
 
FotaError network_buffer_init (NetworkBuffer *network_buffer)
 Initalizes circular buffer for use, by initalizing assocated structs. More...
 
uint16_t network_buffer_num_items (NetworkBuffer *network_buffer)
 Fetches the number of items in the network buffer. More...
 
uint16_t network_buffer_num_empty_slots (NetworkBuffer *network_buffer)
 Fetches the number of empty slots in the network buffer. More...
 
bool network_buffer_empty (NetworkBuffer *network_buffer)
 Checks if the network buffer is empty. More...
 
bool network_buffer_full (NetworkBuffer *network_buffer)
 Checks if the network buffer is full. More...
 
FotaError network_buffer_write (NetworkBuffer *network_buffer, uint8_t *data)
 Write data bytes. More...
 
FotaError network_buffer_read (NetworkBuffer *network_buffer, uint8_t *data)
 Read data bytes. More...
 
FotaError packet_manager_init (PacketManager *manager, UartSettings *uart2_settings, void(*callback)(FotaDatagram *datagram))
 Initialize the packet manager. More...
 
FotaError packet_manager_process (PacketManager *manager)
 Process received data from the network buffer. More...
 
FotaError packet_manager_send_datagram (PacketManager *manager, FotaDatagram *datagram, FotaError(*send_func)(int8_t *data, uint32_t length))
 Send a datagram by fragmenting it into packets. More...
 
FotaError packet_manager_create_datagram (PacketManager *manager, FotaDatagramType type, uint8_t *data, uint32_t length, FotaDatagram *datagram)
 Create a new datagram for sending. More...
 
FotaError packet_manager_get_datagram (PacketManager *manager, uint32_t datagram_id, FotaDatagram **datagram)
 Get a pointer to a completed datagram. More...
 
FotaError packet_manager_free_datagram (PacketManager *manager, uint32_t datagram_id)
 Free a datagram slot. More...
 

Variables

uint32_t _flash_start
 
uint32_t _flash_size
 
uint32_t _flash_page_size
 
uint32_t _accumulator_memory
 
uint32_t _accumulator_memory_size
 
uint32_t _application_start
 
uint32_t _application_size
 
uint32_t _bootloader_start
 
uint32_t _bootloader_size
 
uint32_t _bios_start
 
uint32_t _bios_size
 
uint32_t _sram_start
 
uint32_t _sram_size
 

Detailed Description

Firmware Over the Air (FOTA) Update system.

Macro Definition Documentation

◆ FOTA_ADDR_TO_PAGE

#define FOTA_ADDR_TO_PAGE (   addr)    (((uintptr_t)(addr) - (uintptr_t)APPLICATION_START_ADDRESS) / FOTA_PAGE_BYTES)

Convert memory address to page number.

Parameters
addrAddress to be converted to page number
Returns
Page number

◆ FOTA_PACKET_PAYLOAD_SIZE

#define FOTA_PACKET_PAYLOAD_SIZE   256U

FOTA packet payload size.

FotaPacket Serialized Format:

+---------—+-------------—+---------------—+-------------—+----------------—+ | Field | Size (bytes) | Description | Example Value | Notes | +---------—+-------------—+---------------—+-------------—+----------------—+ | sof | 1 | Start of Frame | 0xAA | Always 0xAA | | packet_type| 1 | Packet Type Enum | 0x01 | Type identifier | | datagram_id| 4 | Datagram ID | 0x00000005 | Big/little endian | | sequence_num| 1 | Packet Index | 0x02 | 0–7 in a datagram | | payload_len| 2 | Payload Length | 0x0040 | In bytes | | payload | N (variable) | Payload Data | [...] | Up to defined max | | crc32 | 4 | CRC32 Checksum | 0xDEADBEEF | On payload | | eof | 1 | End of Frame | 0xBB | Always 0xBB | +---------—+-------------—+---------------—+-------------—+----------------—+

Total Size: 14 bytes of header/footer + N bytes payload

◆ FOTA_PAGE_TO_ADDR

#define FOTA_PAGE_TO_ADDR (   page)    ((uintptr_t)(page) * (uintptr_t)FOTA_PAGE_BYTES + (uintptr_t)APPLICATION_START_ADDRESS)

Convert page number to memory address.

Parameters
pagePage number
Returns
32-bit address

Enumeration Type Documentation

◆ FotaDatagramType

Datagram types corresponding to different FOTA operations.

Enumerator
FOTA_DATAGRAM_TYPE_FIRMWARE_METADATA 

Firmware metadata information

FOTA_DATAGRAM_TYPE_FIRMWARE_CHUNK 

Chunk of firmware data

FOTA_DATAGRAM_TYPE_UPDATE_REQUEST 

Request to perform update

FOTA_DATAGRAM_TYPE_UPDATE_RESPONSE 

Response to update request

FOTA_DATAGRAM_TYPE_VERIFICATION 

Verification information

◆ FotaError

enum FotaError

FOTA Error code definitions.

Enumerator
FOTA_ERROR_SUCCESS 

Success error code

FOTA_ERROR_RESOURCE_EXHAUSTED 

Resource exhausted error code

FOTA_ERROR_INVALID_ARGS 

Invalid arguments error code

FOTA_ERROR_INTERNAL_ERROR 

Internal issue error code

FOTA_ERROR_CRC32_MISMATCH 

CRC mismatch error code

FOTA_ERROR_CRC32_DATA_NOT_ALIGNED 

Data not 4-byte aligned in CRC32 calculation

◆ FotaJumpRequest

Enumerator
FOTA_JUMP_APPLICATION 

Jump to application memory

FOTA_JUMP_BOOTLOADER 

Jump to bootloader memory

FOTA_JUMP_MIDSUN_BIOS 

Jump to Midnight Sun BIOS

◆ FotaPacketType

FOTA Packet Type.

Enumerator
FOTA_PACKET_TYPE_DATA 

Data packet

FOTA_PACKET_TYPE_HEADER 

Header packet

FOTA_PACKET_TYPE_ACK 

Acknowledgment packet

FOTA_PACKET_TYPE_NACK 

Not Acknowledged packet

FOTA_PACKET_TYPE_ERROR 

Error packet

◆ GpioPort

enum GpioPort

UART Settings.

TX Pin will transmit data. RX pin will receive data. The baudrate is the number of symbols sent per second. Both devices must agree on the baudrate with a tolerance of +-3%. Flow control is of type UartFlowControl.

Enumerator
GPIO_PORT_A 

Gpio port A

GPIO_PORT_B 

Gpio port B

NUM_GPIO_PORTS 

Number of Gpio Ports

GPIO_PORT_A 

GPIO Port A

GPIO_PORT_B 

GPIO Port B

GPIO_PORT_C 

GPIO Port C

GPIO_PORT_D 

GPIO Port D

GPIO_PORT_E 

GPIO Port E

GPIO_PORT_H 

GPIO Port H

NUM_GPIO_PORTS 

Number of GPIO Ports

◆ PacketReceiverState

State machine states for packet receiving.

Enumerator
PKT_STATE_WAITING_SOF 

Waiting for start of frame

PKT_STATE_READING_HEADER 

Reading packet header

PKT_STATE_READING_PAYLOAD 

Reading packet payload

PKT_STATE_READING_CRC 

Reading CRC

PKT_STATE_READING_EOF 

Reading end of frame

◆ UartFlowControl

UART Flow control selection.

Flow control is a method of synchronizing UART communication between 2 devices. For most applications, this can be set to UART_FLOW_CONTROL_NONE.

Enumerator
UART_FLOW_CONTROL_NONE 

Disable flow control

UART_FLOW_CONTROL_RTS 

Enable request-to-send flow control

UART_FLOW_CONTROL_CTS 

Enable clear-to-send flow control

UART_FLOW_CONTROL_RTS_CTS 

Enable both request-to-send and clear-to-send flow control

UART_FLOW_CONTROL_NONE 

Disable flow control

UART_FLOW_CONTROL_RTS 

Enable request-to-send flow control

UART_FLOW_CONTROL_CTS 

Enable clear-to-send flow control

UART_FLOW_CONTROL_RTS_CTS 

Enable both request-to-send and clear-to-send flow control

Function Documentation

◆ fota_calculate_crc32()

uint32_t fota_calculate_crc32 ( uint8_t *  data,
uint32_t  length_words 
)

Calculate the CRC32 for a given buffer of 4-byte aligned data.

Parameters
data_startPointer to the start of the data
length_wordsSize of the data in words

◆ fota_datagram_init()

FotaError fota_datagram_init ( FotaDatagram datagram,
FotaDatagramType  type,
uint32_t  datagram_id,
uint8_t *  data,
uint32_t  length 
)

Initialize a datagram structure.

Parameters
datagramPointer to datagram to initialize
typeType of datagram
datagram_idUnique ID for this datagram
dataData to include in datagram (can be NULL if building incrementally)
lengthLength of data
Returns
FotaError Error code

◆ fota_datagram_is_complete()

bool fota_datagram_is_complete ( FotaDatagram datagram)

Check if a datagram has been completely received.

Parameters
datagramDatagram to check
Returns
TRUE if datagram is complete FALSE otherwise

◆ fota_datagram_process_data_packet()

FotaError fota_datagram_process_data_packet ( FotaDatagram datagram,
FotaPacket packet 
)

Process a received packet and incorporate it into a datagram.

Parameters
datagramDatagram being reconstructed
packetReceived packet
Returns
FotaError Error code

◆ fota_datagram_process_header_packet()

FotaError fota_datagram_process_header_packet ( FotaDatagram datagram,
FotaPacket packet 
)

Process a received header packet and incorporate it into a datagram.

Parameters
datagramDatagram being reconstructed
packetReceived header packet
Returns
FotaError Error code

◆ fota_datagram_to_packets()

FotaError fota_datagram_to_packets ( FotaDatagram datagram,
FotaPacket packets,
uint16_t *  num_packets,
uint16_t  max_packets 
)

Fragment a datagram into packets for transmission.

Parameters
datagramDatagram to fragment
packetsArray to store generated packets
num_packetsPointer to store number of packets generated
max_packetsMaximum number of packets that can be stored in the array
Returns
FotaError Error code

◆ fota_datagram_verify()

FotaError fota_datagram_verify ( FotaDatagram datagram)

Verify the integrity of a complete datagram.

Parameters
datagramDatagram to verify
Returns
FotaError Error code

◆ fota_encryption_init()

FotaError fota_encryption_init ( )

Initialize the encryption unit.

Returns
FOTA_SUCCESS if initialized succesfully FOTA_ERROR_INTERNAL_ERROR if initialization fails

◆ fota_flash_erase()

FotaError fota_flash_erase ( uint8_t  start_page,
uint8_t  num_pages 
)

Erase some number of flash pages.

Parameters
start_pageInitial page number to erase
num_pagesNumber of pages to erase
Returns
FOTA_ERROR_SUCCESS if data is erased succesfully FOTA_ERROR_FLASH_ERASE if page is out of bounds OR erasing failed

◆ fota_flash_read()

FotaError fota_flash_read ( uintptr_t  address,
uint8_t *  buffer,
uint32_t  buffer_len 
)

Read from flash memory.

Parameters
addressBase memory address to read from
bufferPointer to the data buffer
buffer_lenLength of the data buffer
Returns
FOTA_ERROR_SUCCESS if data is read succesfully FOTA_ERROR_FLASH_READ_FAILED if data read failed

◆ fota_flash_write()

FotaError fota_flash_write ( uintptr_t  address,
uint8_t *  buffer,
uint32_t  buffer_len 
)

Write to flash memory.

Parameters
addressBase memory address to write to
bufferPointer to the data buffer
buffer_lenLength of the data buffer
Returns
FOTA_ERROR_SUCCESS if data is written succesfully FOTA_ERROR_FLASH_WRITE_FAILED if HAL flash fails FOTA_ERROR_FLASH_WRITE_OUT_OF_BOUNDS if address is out of bounds FOTA_ERROR_FLASH_WRITE_NOT_ALIGNED if data is not aligned

◆ fota_is_timed_out()

bool fota_is_timed_out ( void  )

Checks if FOTA has timed out, meaning no requests have been received.

Returns
TRUE if a timeout event has occurred FALSE if no timeout has occurred

◆ fota_packet_deserialize()

FotaError fota_packet_deserialize ( FotaPacket packet,
uint8_t *  buffer,
uint32_t  buf_size 
)

Deserialize packet from received data.

Parameters
packetOutput packet structure
bufferInput buffer containing serialized packet
buf_sizeSize of input buffer
Returns
FotaError Error code

◆ fota_packet_init()

FotaError fota_packet_init ( FotaPacket packet,
FotaPacketType  type,
uint8_t  sequence,
uint16_t  length 
)

Initialize a packet structure.

Parameters
packetPointer to the packet to initialize
typeType of packet
sequenceSequence number
lengthPayload length
Returns
FotaError Error code

◆ fota_packet_serialize()

FotaError fota_packet_serialize ( FotaPacket packet,
uint8_t *  buffer,
uint32_t  buf_size,
uint32_t *  bytes_written 
)

Serialize packet into a buffer for transmission.

Parameters
packetPacket to serialize
bufferOutput buffer
buf_sizeSize of output buffer
bytes_writtenNumber of bytes written
Returns
FotaError Error code

◆ fota_packet_set_crc()

FotaError fota_packet_set_crc ( FotaPacket packet)

Calculate and set the CRC for a packet.

Parameters
packetPointer to packet
Returns
FotaError Error code

◆ fota_verify_datagram_encryption()

FotaError fota_verify_datagram_encryption ( FotaDatagram datagram)

Verify a FOTA datagrams encryption.

Parameters
datagramPointer to the encrypted datagram
Returns
FOTA_SUCCESS if encryption is decoded succesfully and the datagram is valid

◆ fota_verify_flash_memory()

FotaError fota_verify_flash_memory ( void  )

Verify that the flash memory is not erased.

This checks the entire application flash memory to ensure it is not entirely erased

Returns
FOTA_ERROR_SUCCESS if the memomry is valid FOTA_ERROR_FLASH_ERR if the memory is corrupt

◆ fota_verify_packet_encryption()

FotaError fota_verify_packet_encryption ( FotaPacket packet)

Verify a FOTA packets encryption.

Parameters
packetPointer to the encrypted packet
Returns
FOTA_SUCCESS if encryption is decoded succesfully and the datagram is valid

◆ isTimeout()

bool isTimeout ( bool  is_tx)

Checks if tx or rx command has timed out.

Subtracts the difference of current clock time and clock time when command started

Parameters
is_txif it is checking timeout for a tx or an rx command
Returns
true if time elapsed is greater than FOTA_UART_TIMEOUT_MS false if time elapsed is less than FOTA_UART_TIMEOUT_MS

◆ network_buffer_empty()

bool network_buffer_empty ( NetworkBuffer network_buffer)

Checks if the network buffer is empty.

Parameters
network_bufferPointer to the network buffer
Returns
TRUE if network buffer is empty FALSE if network buffer has data

◆ network_buffer_full()

bool network_buffer_full ( NetworkBuffer network_buffer)

Checks if the network buffer is full.

Parameters
network_bufferPointer to the network buffer
Returns
TRUE if network buffer is full FALSE if network buffer is not full

◆ network_buffer_init()

FotaError network_buffer_init ( NetworkBuffer network_buffer)

Initalizes circular buffer for use, by initalizing assocated structs.

Parameters
network_bufferPointer to the network buffer
Returns
FOTA_ERROR_SUCCESS if network buffer is initialized successfully FOTA_ERROR_INVALID_ARGS if one of the parameters are incorrect

◆ network_buffer_num_empty_slots()

uint16_t network_buffer_num_empty_slots ( NetworkBuffer network_buffer)

Fetches the number of empty slots in the network buffer.

Parameters
network_bufferPointer to the network buffer
Returns
Number of empty slots in the network buffer

◆ network_buffer_num_items()

uint16_t network_buffer_num_items ( NetworkBuffer network_buffer)

Fetches the number of items in the network buffer.

Parameters
network_bufferPointer to the network buffer
Returns
Number of items in the network buffer

◆ network_buffer_read()

FotaError network_buffer_read ( NetworkBuffer network_buffer,
uint8_t *  data 
)

Read data bytes.

Parameters
network_bufferPointer to the network buffer
dataReads packet from circular buffer into a provided location
Returns
FOTA_ERROR_SUCCESS if network buffer is initialized successfully FOTA_ERROR_INVALID_ARGS if one of the parameters are incorrect

◆ network_buffer_write()

FotaError network_buffer_write ( NetworkBuffer network_buffer,
uint8_t *  data 
)

Write data bytes.

Parameters
network_bufferPointer to the network buffer
dataPointer to data byte to write
Returns
FOTA_ERROR_SUCCESS if network buffer is initialized successfully FOTA_ERROR_INVALID_ARGS if one of the parameters are incorrect

◆ network_init()

FotaError network_init ( UartPort  uart,
UartSettings settings,
NetworkBuffer network_buffer 
)

Initialize UART instance, assuming standard 8 bits 1 stop bit.

Parameters
uartSpecifies which UART port to initialize
settingsPointer to UART initialization settings
network_bufferPointer to the network buffer for storing recieved data
Returns
FOTA_ERROR_SUCCESS if initialization succeeded FOTA_ERROR_INVALID_ARGS if one of the parameters are incorrect FOTA_ERROR_INTERNAL_ERROR if HAL initialization fails FOTA_RESOURCE_EXHAUSTED if already initialized

◆ network_read()

FotaError network_read ( UartPort  uart,
uint8_t *  data,
size_t  len 
)

Read data from the network buffer.

Receives len bytes of data from the network buffer. Receives up to NETWORK_BUFFER_SIZE.

Parameters
uartSpecifies which UART port the data is coming from
dataPointer to the read data buffer
lenLength of data to receive
Returns
FOTA_ERROR_SUCCESS if receiving succeeded FOTA_ERROR_INVALID_ARGS if one of the parameters are incorrect FOTA_RESOURCE_EXHAUSTED if uart port is not initialized FOTA_ERROR_INTERNAL_ERROR if reading from network buffer fails or takes too long

◆ network_tx()

FotaError network_tx ( UartPort  uart,
uint8_t *  data,
size_t  len 
)

Transmit data from the UART port.

Sends length bytes of data from UART TX queue to specified UART port. Sends up to UART_MAX_BUFFER_LEN. Updates start time of tx command and sets transmitting status to true, check timeout status in main loop.

Parameters
uartSpecifies which UART port to send from
dataPointer to the tx data buffer
lenLength of data to send
Returns
FOTA_ERROR_SUCCESS if transmission succeeded FOTA_ERROR_INVALID_ARGS if one of the parameters are incorrect FOTA_RESOURCE_EXHAUSTED if uart port is not initialized FOTA_ERROR_INTERNAL_ERROR if HAL transmission fails

◆ packet_manager_create_datagram()

FotaError packet_manager_create_datagram ( PacketManager manager,
FotaDatagramType  type,
uint8_t *  data,
uint32_t  length,
FotaDatagram datagram 
)

Create a new datagram for sending.

Parameters
managerPointer to packet manager structure
typeType of datagram to create
dataData to include in datagram
lengthLength of data
datagramPointer to store the created datagram
Returns
Error code

◆ packet_manager_free_datagram()

FotaError packet_manager_free_datagram ( PacketManager manager,
uint32_t  datagram_id 
)

Free a datagram slot.

Parameters
managerPointer to packet manager structure
datagram_idID of the datagram to free
Returns
Error code

◆ packet_manager_get_datagram()

FotaError packet_manager_get_datagram ( PacketManager manager,
uint32_t  datagram_id,
FotaDatagram **  datagram 
)

Get a pointer to a completed datagram.

Parameters
managerPointer to packet manager structure
datagram_idID of the datagram to retrieve
datagramPointer to store pointer to datagram
Returns
Error code

◆ packet_manager_init()

FotaError packet_manager_init ( PacketManager manager,
UartSettings uart2_settings,
void(*)(FotaDatagram *datagram)  callback 
)

Initialize the packet manager.

Parameters
managerPointer to packet manager structure
uart2_settingsPointer to the UART settings needed for the network buffer
callbackCallback function for completed datagrams (can be NULL)
Returns
Error code

◆ packet_manager_process()

FotaError packet_manager_process ( PacketManager manager)

Process received data from the network buffer.

This function shall be called periodically to process any data in the network buffer It will extract bytes from the buffer, assemble them into packets, and then assemble packets into datagrams.

Parameters
managerPointer to packet manager structure
Returns
Error code

◆ packet_manager_send_datagram()

FotaError packet_manager_send_datagram ( PacketManager manager,
FotaDatagram datagram,
FotaError(*)(int8_t *data, uint32_t length)  send_func 
)

Send a datagram by fragmenting it into packets.

Parameters
managerPointer to packet manager structure
datagramDatagram to send
send_funcFunction to call to send each packet
Returns
Error code