feat: Draft architecture for incp hub
This commit is contained in:
1
app/src/ntl/CMakeLists.txt
Normal file
1
app/src/ntl/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_include_directories(${EXECUTABLE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
7
app/src/ntl/README.md
Normal file
7
app/src/ntl/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# NTL
|
||||
|
||||
Nanosatlab Transport Layer (from IP model)
|
||||
|
||||
## Byte ordering
|
||||
|
||||
Multi-byte fields are sent in little-endian order. (Natural order for x86 and arm)
|
||||
96
app/src/ntl/include/ntl/ntl.h
Normal file
96
app/src/ntl/include/ntl/ntl.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#ifndef NTL_H_
|
||||
#define NTL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
// === Type Aliases ===
|
||||
|
||||
/// @brief An NTL port number
|
||||
typedef uint8_t ntl_port_t;
|
||||
/// @brief An NTL check value
|
||||
typedef uint32_t ntl_chk_t;
|
||||
|
||||
|
||||
|
||||
// === Special Port Values ===
|
||||
|
||||
/// @brief Denotes an invalid port number
|
||||
#define NTL_PORT_EMPTY ((ntl_port_t)0)
|
||||
/// @brief The highest valid port number
|
||||
#define NTL_MAX_PORT (~(ntl_port_t)0)
|
||||
|
||||
|
||||
|
||||
// === Special Check Values ===
|
||||
|
||||
/// @brief Denotes no check value to validate
|
||||
#define NTL_CHK_EMPTY ((ntl_chk_t)0)
|
||||
|
||||
|
||||
|
||||
// === Struct Definitions ===
|
||||
|
||||
/// @brief Packed header format
|
||||
typedef struct __attribute__((packed, aligned(4))) {
|
||||
ntl_port_t dst_port;
|
||||
ntl_port_t src_port;
|
||||
uint16_t len;
|
||||
ntl_chk_t chk;
|
||||
} ntl_header_t;
|
||||
|
||||
/// @brief An NTL packet (packed) with header and payload
|
||||
typedef struct __attribute__((packed, aligned(4))) {
|
||||
ntl_header_t header;
|
||||
uint8_t payload[];
|
||||
} ntl_packet_t;
|
||||
|
||||
|
||||
|
||||
// === Convenience Functions ===
|
||||
|
||||
/// @brief Convert uint16 value from host to network byte order
|
||||
/// @param value The value in host order
|
||||
/// @return The value in network order
|
||||
static inline uint16_t ntl_htons(uint16_t value)
|
||||
{
|
||||
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
|
||||
defined(__BIG_ENDIAN__) || \
|
||||
defined(__ARMEB__) || \
|
||||
defined(__THUMBEB__) || \
|
||||
defined(__AARCH64EB__) || \
|
||||
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
|
||||
return ((value << 8) & 0xFF) | ((value & 0xFF) << 8);
|
||||
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
|
||||
defined(__LITTLE_ENDIAN__) || \
|
||||
defined(__ARMEL__) || \
|
||||
defined(__THUMBEL__) || \
|
||||
defined(__AARCH64EL__) || \
|
||||
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
|
||||
return value;
|
||||
#else
|
||||
#error "Unable to detect byte endianness of platform."
|
||||
#endif
|
||||
}
|
||||
|
||||
/// @brief Populates an NTL header from the provided fields
|
||||
/// @param hdr The header to pupulate
|
||||
/// @param dst The destination port
|
||||
/// @param src The source port
|
||||
/// @param len The length of the NTL payload in bytes
|
||||
static inline void ntl_build_header(volatile ntl_header_t *hdr, ntl_port_t dst, ntl_port_t src, uint16_t len)
|
||||
{
|
||||
hdr->dst_port = dst;
|
||||
hdr->src_port = src;
|
||||
hdr->len = ntl_htons(len);
|
||||
//REVIEW: Disabled for now, add a flag to set this to an actual check?
|
||||
hdr->chk = NTL_CHK_EMPTY;
|
||||
}
|
||||
|
||||
static inline ntl_port_t ntl_get_dst(const ntl_header_t *hdr) { return hdr->dst_port; }
|
||||
static inline ntl_port_t ntl_get_src(const ntl_header_t *hdr) { return hdr->src_port; }
|
||||
static inline ntl_port_t ntl_get_len(const ntl_header_t *hdr) { return ntl_htons(hdr->len); }
|
||||
static inline ntl_port_t ntl_get_chk(const ntl_header_t *hdr) { return hdr->chk; }
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user