Solaris Software
Loading...
Searching...
No Matches
databank.c
Go to the documentation of this file.
1/**
2 * @file databank.c
3 * @brief Static packet pool implementation.
4 */
5
9#include "spp/core/packet.h"
10#include "spp/hal/time/time.h"
11#include "spp/util/crc.h"
12
13#include <string.h>
14#include <stddef.h>
15
16/* ----------------------------------------------------------------
17 * Private state
18 * ---------------------------------------------------------------- */
19
20/** @brief Static storage for all packets. */
22
23/** @brief Control structure (free-list stack). */
25
26/** @brief Tracks whether the pool has been initialised. */
28
29/* ----------------------------------------------------------------
30 * Public API
31 * ---------------------------------------------------------------- */
32
34{
35 if (s_initialized)
36 {
38 }
39
40 memset(s_packets, 0, sizeof(s_packets));
42
43 for (spp_uint32_t i = 0U; i < K_SPP_DATABANK_SIZE; i++)
44 {
47 }
48
49 s_initialized = true;
50 return K_SPP_OK;
51}
52
54{
55 if (!s_initialized || (s_databank.freeCount == 0U))
56 {
57 return NULL;
58 }
59
62}
63
65{
66 if (p_packet == NULL)
67 {
69 }
70
71 /* Validate that the pointer belongs to the static pool. */
72 if ((p_packet < &s_packets[0]) || (p_packet > &s_packets[K_SPP_DATABANK_SIZE - 1U]))
73 {
74 return K_SPP_ERROR;
75 }
76
77 /* Guard against double-return. */
78 for (spp_uint32_t i = 0U; i < s_databank.freeCount; i++)
79 {
80 if (s_databank.p_freePackets[i] == p_packet)
81 {
82 return K_SPP_ERROR_ALREADY_INITIALIZED; /* Already in free list. */
83 }
84 }
85
87 {
88 return K_SPP_ERROR; /* Pool is already full — should not happen. */
89 }
90
93 return K_SPP_OK;
94}
95
100
101/* ----------------------------------------------------------------
102 * Private helpers
103 * ---------------------------------------------------------------- */
104
105static spp_uint16_t packetCrc(const SPP_Packet_t *p_packet)
106{
107 /* CRC covers all bytes up to (but not including) the crc field.
108 * The packet is zeroed before filling, so padding bytes are 0
109 * and contribute deterministically to the checksum. */
110 return SPP_UTIL_crc16((const spp_uint8_t *)p_packet, (spp_uint32_t)offsetof(SPP_Packet_t, crc));
111}
112
113/* ----------------------------------------------------------------
114 * Packet fill helper
115 * ---------------------------------------------------------------- */
116
118 const void *p_data, spp_uint16_t dataLen)
119{
120 if ((p_packet == NULL) || (p_data == NULL))
121 {
123 }
124 if (dataLen > K_SPP_PKT_PAYLOAD_MAX)
125 {
127 }
128
129 /* Zero the whole struct so padding bytes are deterministic. */
130 memset(p_packet, 0xFF, sizeof(SPP_Packet_t));
131
132 p_packet->primaryHeader.version = K_SPP_PKT_VERSION;
133 p_packet->primaryHeader.apid = apid;
134 p_packet->primaryHeader.seq = seq;
135 p_packet->primaryHeader.payloadLen = dataLen;
136
137 p_packet->secondaryHeader.timestampMs = SPP_HAL_TIME_getTimeMs();
138 p_packet->secondaryHeader.dropCounter = 0U;
139
140 memcpy(p_packet->payload, p_data, dataLen);
141
142 p_packet->crc = packetCrc(p_packet);
143
144 return K_SPP_OK;
145}
spp_uint16_t SPP_UTIL_crc16(const spp_uint8_t *p_data, spp_uint32_t length)
Compute a CRC-16/CCITT checksum over a byte buffer.
Definition crc.c:8
CRC-16/CCITT checksum utility.
SPP_Packet_t * SPP_SERVICES_DATABANK_getPacket(void)
Acquire a free packet from the pool.
Definition databank.c:53
SPP_RetVal_t SPP_SERVICES_DATABANK_returnPacket(SPP_Packet_t *p_packet)
Return a packet to the pool after processing.
Definition databank.c:64
static spp_bool_t s_initialized
Tracks whether the pool has been initialised.
Definition databank.c:27
static spp_uint16_t packetCrc(const SPP_Packet_t *p_packet)
Definition databank.c:105
SPP_RetVal_t SPP_SERVICES_DATABANK_init(void)
Initialise the packet pool.
Definition databank.c:33
spp_uint32_t SPP_SERVICES_DATABANK_freeCount(void)
Return the number of free packets currently available.
Definition databank.c:96
static SPP_Packet_t s_packets[K_SPP_DATABANK_SIZE]
Static storage for all packets.
Definition databank.c:21
SPP_RetVal_t SPP_SERVICES_DATABANK_packetData(SPP_Packet_t *p_packet, spp_uint16_t apid, spp_uint16_t seq, const void *p_data, spp_uint16_t dataLen)
Fill a packet with data and compute its CRC.
Definition databank.c:117
static SPP_Databank_t s_databank
Control structure (free-list stack).
Definition databank.c:24
Static packet pool manager.
#define K_SPP_DATABANK_SIZE
Number of packets in the static databank pool.
Definition macros.h:47
Solaris Packet Protocol (SPP) packet structure definitions.
SPP_Packet_t
Definition packet.h:79
#define K_SPP_PKT_VERSION
Current SPP protocol version embedded in each packet.
Definition packet.h:24
#define K_SPP_PKT_PAYLOAD_MAX
Maximum payload size in bytes per packet.
Definition packet.h:27
Standard return-value type used across all SPP modules.
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition returnTypes.h:24
@ K_SPP_OK
Definition returnTypes.h:25
@ K_SPP_ERROR
Definition returnTypes.h:26
@ K_SPP_ERROR_ALREADY_INITIALIZED
Definition returnTypes.h:29
@ K_SPP_ERROR_NULL_POINTER
Definition returnTypes.h:30
@ K_SPP_ERROR_INVALID_PARAMETER
Definition returnTypes.h:32
Internal databank control structure.
Definition databank.h:38
SPP_Packet_t * p_freePackets[K_SPP_DATABANK_SIZE]
Definition databank.h:39
spp_uint32_t freeCount
Definition databank.h:40
spp_uint32_t SPP_HAL_TIME_getTimeMs(void)
Return the current hardware time in milliseconds.
Definition time.c:17
SPP HAL time API — dispatches through the registered HAL port.
uint16_t spp_uint16_t
Definition types.h:29
uint32_t spp_uint32_t
Definition types.h:31
uint8_t spp_uint8_t
Definition types.h:27
bool spp_bool_t
Definition types.h:36