Solaris Software
Toggle main menu visibility
Loading...
Searching...
No Matches
datalogger.c
Go to the documentation of this file.
1
/**
2
* @file datalogger.c
3
* @brief SD card packet logger — SPP consumer service implementation.
4
*/
5
6
#include "
spp/services/datalogger/datalogger.h
"
7
8
#include "
spp/hal/storage/storage.h
"
9
#include "
spp/hal/spi/spi.h
"
10
#include "
spp/core/packet.h
"
11
#include "
spp/services/log/log.h
"
12
#include "
spp/core/types.h
"
13
#include "
spp/services/fsm/fsm.h
"
14
15
#include <string.h>
16
17
/* ----------------------------------------------------------------
18
* CONSTANTS
19
* ---------------------------------------------------------------- */
20
static
SPP_Packet_t
s_packetBuffer
[
K_DATALOGGER_BUFFER_PACKETS
];
21
static
spp_uint8_t
s_packetIndex
= 0;
/**< Index of the next packet to be written to the buffer. */
22
static
spp_uint32_t
s_currentDataSector
= 0;
/**< Current sector of the data log. */
23
static
spp_uint16_t
s_blocksWritten
=
24
K_DATALOGGER_BUFFER_SIZE
/
K_DATALOGGER_SECTOR_SIZE
;
/**< Number of blocks written to the current sector. */
25
static
SPP_RetVal_t
s_initStatus
=
K_SPP_ERROR_NOT_INITIALIZED
;
26
27
28
/* ----------------------------------------------------------------
29
* STATIC FUNCTIONS DECLARATIONS
30
* ---------------------------------------------------------------- */
31
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_init
(
void
);
32
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_deliverToMailbox
(
const
SPP_Packet_t
packet);
33
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_consumeData
(
void
*p_data);
34
35
/* ----------------------------------------------------------------
36
* VARIABLES
37
* ---------------------------------------------------------------- */
38
static
const
SPP_SERVICE_ConsumerContract_t
g_dataloggerConsumerContract
= {
39
.priority =
K_DATALOGGER_CONSUMER_PRIO
,
40
.p_nameConsumer =
"datalogger"
,
41
.tiemoutMs =
K_DATALOGGER_TIMEOUT_MS
,
42
.init =
SPP_SERVICES_DATALOGGER_init
,
43
.deliverToMailbox =
SPP_SERVICES_DATALOGGER_deliverToMailbox
,
44
.consumeData =
SPP_SERVICES_DATALOGGER_consumeData
,
45
};
46
47
/* ----------------------------------------------------------------
48
* PUBLIC FUNCTIONS
49
* ---------------------------------------------------------------- */
50
const
SPP_SERVICE_ConsumerContract_t
*
SPP_SERVICES_DATALOGGER_getConsumerContract
(
void
)
51
{
52
return
&
g_dataloggerConsumerContract
;
53
}
54
55
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_getInitStatus
(
void
)
56
{
57
return
s_initStatus
;
58
}
59
60
/* ----------------------------------------------------------------
61
* STATIC FUNCTIONS
62
* ---------------------------------------------------------------- */
63
64
/**
65
* @brief Initializes the SD card storage and resets the logging indices.
66
* @return Result returned by the storage HAL initialization.
67
*/
68
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_init
(
void
)
69
{
70
/* Init the variables */
71
s_currentDataSector
=
K_DATALOGGER_FIRST_SECTOR
;
72
s_packetIndex
= 0;
73
74
/* Initialize SD card storage. */
75
s_initStatus
=
SPP_HAL_STORAGE_init
();
76
if
(
s_initStatus
!=
K_SPP_OK
)
77
{
78
return
s_initStatus
;
79
}
80
return
K_SPP_OK
;
81
}
82
83
/**
84
* @brief Copies a received packet into the internal logging buffer.
85
* @param packet Packet dispatched by PUBSUB.
86
* @return K_SPP_OK.
87
*/
88
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_deliverToMailbox
(
const
SPP_Packet_t
packet)
89
{
90
/* Copy the packets into the buffer*/
91
s_packetBuffer
[
s_packetIndex
] = packet;
92
s_packetIndex
++;
93
return
K_SPP_OK
;
94
}
95
96
/**
97
* @brief Writes the buffered packets to consecutive SD card sectors when full.
98
*
99
* On successful writing, advances the sector index and resets the buffer.
100
* On failure, retains the buffered packets for a later attempt.
101
*
102
* @param p_data Unused.
103
* @return K_SPP_OK, regardless of the storage write result.
104
*/
105
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_consumeData
(
void
*p_data)
106
{
107
if
((
s_packetIndex
*
K_SPP_TOTAL_PACKET_SIZE
) >=
K_DATALOGGER_BUFFER_SIZE
)
108
{
109
SPP_RetVal_t
ret =
SPP_HAL_STORAGE_write
(
s_packetBuffer
,
s_currentDataSector
,
s_blocksWritten
);
110
if
(ret ==
K_SPP_OK
)
111
{
112
s_currentDataSector
+=
s_blocksWritten
;
113
s_packetIndex
= 0;
114
}
115
}
116
117
return
K_SPP_OK
;
118
}
SPP_SERVICES_DATALOGGER_getConsumerContract
const SPP_SERVICE_ConsumerContract_t * SPP_SERVICES_DATALOGGER_getConsumerContract(void)
Returns the datalogger consumer contract.
Definition
datalogger.c:50
SPP_SERVICES_DATALOGGER_getInitStatus
SPP_RetVal_t SPP_SERVICES_DATALOGGER_getInitStatus(void)
Returns the stored SD card initialization status.
Definition
datalogger.c:55
s_currentDataSector
static spp_uint32_t s_currentDataSector
Definition
datalogger.c:22
s_blocksWritten
static spp_uint16_t s_blocksWritten
Definition
datalogger.c:23
SPP_SERVICES_DATALOGGER_consumeData
static SPP_RetVal_t SPP_SERVICES_DATALOGGER_consumeData(void *p_data)
Writes the buffered packets to consecutive SD card sectors when full.
Definition
datalogger.c:105
s_packetIndex
static spp_uint8_t s_packetIndex
Definition
datalogger.c:21
g_dataloggerConsumerContract
static const SPP_SERVICE_ConsumerContract_t g_dataloggerConsumerContract
Definition
datalogger.c:38
SPP_SERVICES_DATALOGGER_init
static SPP_RetVal_t SPP_SERVICES_DATALOGGER_init(void)
Initializes the SD card storage and resets the logging indices.
Definition
datalogger.c:68
SPP_SERVICES_DATALOGGER_deliverToMailbox
static SPP_RetVal_t SPP_SERVICES_DATALOGGER_deliverToMailbox(const SPP_Packet_t packet)
Copies a received packet into the internal logging buffer.
Definition
datalogger.c:88
datalogger.h
SD card packet logger service.
K_DATALOGGER_CONSUMER_PRIO
#define K_DATALOGGER_CONSUMER_PRIO
Definition
datalogger.h:17
K_DATALOGGER_FIRST_SECTOR
#define K_DATALOGGER_FIRST_SECTOR
Definition
datalogger.h:20
K_DATALOGGER_BUFFER_PACKETS
#define K_DATALOGGER_BUFFER_PACKETS
Definition
datalogger.h:22
K_DATALOGGER_SECTOR_SIZE
#define K_DATALOGGER_SECTOR_SIZE
Definition
datalogger.h:19
K_DATALOGGER_TIMEOUT_MS
#define K_DATALOGGER_TIMEOUT_MS
Definition
datalogger.h:18
K_DATALOGGER_BUFFER_SIZE
#define K_DATALOGGER_BUFFER_SIZE
Definition
datalogger.h:21
fsm.h
Finite State Machine (FSM) module for the SPP core.
log.h
SPP logging service.
packet.h
Solaris Packet Protocol (SPP) packet structure definitions.
SPP_Packet_t
SPP_Packet_t
Definition
packet.h:79
K_SPP_TOTAL_PACKET_SIZE
#define K_SPP_TOTAL_PACKET_SIZE
Definition
packet.h:29
s_packetBuffer
static SPP_Packet_t s_packetBuffer[K_SPP_SERVICES_PUBSUB_BUFFER_SIZE]
Definition
pubsub.c:37
SPP_RetVal_t
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition
returnTypes.h:24
K_SPP_ERROR_NOT_INITIALIZED
@ K_SPP_ERROR_NOT_INITIALIZED
Definition
returnTypes.h:31
K_SPP_OK
@ K_SPP_OK
Definition
returnTypes.h:25
s_initStatus
static SPP_RetVal_t s_initStatus
Definition
uart.c:32
spi.h
SPP SPI HAL API — dispatches through the registered HAL port.
SPP_HAL_STORAGE_init
SPP_RetVal_t SPP_HAL_STORAGE_init(void)
Initializes the storage device through the registered HAL port.
Definition
storage.c:18
SPP_HAL_STORAGE_write
SPP_RetVal_t SPP_HAL_STORAGE_write(const void *p_buffer, spp_uint32_t first_block, spp_uint16_t count)
Writes consecutive storage blocks through the HAL.
Definition
storage.c:38
storage.h
SPP storage HAL API — dispatches through the registered HAL port.
SPP_SERVICE_ConsumerContract_t
Describes the contract of a consumer module. This module needs to have:
Definition
service.h:40
types.h
Portable integer types and SPI/storage configuration types for SPP.
spp_uint16_t
uint16_t spp_uint16_t
Definition
types.h:29
spp_uint32_t
uint32_t spp_uint32_t
Definition
types.h:31
spp_uint8_t
uint8_t spp_uint8_t
Definition
types.h:27
solaris-v2
spp
services
datalogger
datalogger.c
Generated by
1.18.0