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_SIZE
];
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
= 0;
/**< Number of blocks written to the current sector. */
24
static
SPP_RetVal_t
s_initStatus
=
K_SPP_ERROR_NOT_INITIALIZED
;
25
26
27
/* ----------------------------------------------------------------
28
* STATIC FUNCTIONS DECLARATIONS
29
* ---------------------------------------------------------------- */
30
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_init
(
void
);
31
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_deliverToMailbox
(
const
SPP_Packet_t
packet);
32
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_consumeData
(
void
*p_data);
33
34
/* ----------------------------------------------------------------
35
* VARIABLES
36
* ---------------------------------------------------------------- */
37
static
const
SPP_SERVICE_ConsumerContract_t
g_dataloggerConsumerContract
= {
38
.priority =
K_DATALOGGER_CONSUMER_PRIO
,
39
.p_nameConsumer =
"datalogger"
,
40
.tiemoutMs =
K_DATALOGGER_TIMEOUT_MS
,
41
.suscribeToApid = 0U,
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
* @brief Initialise the SD card and open the log file for writing.
65
* @return K_SPP_OK on success, K_SPP_ERROR on mount or file-open failure.
66
*/
67
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_init
(
void
)
68
{
69
FsmErrors_t
*p_fsmErrors =
SPP_CORE_FSM_getErrorsBit
();
70
if
(p_fsmErrors == NULL)
71
{
72
return
K_SPP_ERROR_NULL_POINTER
;
73
}
74
/* Init the variables */
75
s_currentDataSector
=
K_DATALOGGER_FIRST_SECTOR
;
76
s_packetIndex
= 0;
77
s_blocksWritten
= 1;
78
79
/* Init SPI for SD card */
80
s_initStatus
=
SPP_HAL_STORAGE_init
();
81
if
(
s_initStatus
!=
K_SPP_OK
)
82
{
83
p_fsmErrors->dataloggerInitError = 1;
84
return
s_initStatus
;
85
}
86
return
K_SPP_OK
;
87
}
88
89
/**
90
* @brief Receive a packet from the pub/sub router into the consumer mailbox.
91
* @param p_pkt Packet dispatched by the router.
92
* @return K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER if p_pkt is NULL.
93
*/
94
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_deliverToMailbox
(
const
SPP_Packet_t
packet)
95
{
96
/* Copy the packets into the buffer*/
97
s_packetBuffer
[
s_packetIndex
] = packet;
98
s_packetIndex
++;
99
return
K_SPP_OK
;
100
}
101
102
/**
103
* @brief Consume a packet from the mailbox and write it to the SD card log file.
104
* @param p_data Unused (context pointer reserved for future use).
105
* @return K_SPP_OK on success, K_SPP_ERROR if the file is not open or write fails.
106
*/
107
static
SPP_RetVal_t
SPP_SERVICES_DATALOGGER_consumeData
(
void
*p_data)
108
{
109
if
((
s_packetIndex
*
K_SPP_TOTAL_PACKET_SIZE
) >=
K_DATALOGGER_BUFFER_SIZE
)
110
{
111
SPP_RetVal_t
ret =
SPP_HAL_STORAGE_write
(
s_packetBuffer
,
s_currentDataSector
,
s_blocksWritten
);
112
if
(ret ==
K_SPP_OK
)
113
{
114
s_currentDataSector
++;
115
s_blocksWritten
++;
116
s_packetIndex
= 0;
117
}
118
}
119
120
return
K_SPP_OK
;
121
}
SPP_SERVICES_DATALOGGER_getConsumerContract
const SPP_SERVICE_ConsumerContract_t * SPP_SERVICES_DATALOGGER_getConsumerContract(void)
Definition
datalogger.c:50
SPP_SERVICES_DATALOGGER_getInitStatus
SPP_RetVal_t SPP_SERVICES_DATALOGGER_getInitStatus(void)
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)
Consume a packet from the mailbox and write it to the SD card log file.
Definition
datalogger.c:107
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:37
SPP_SERVICES_DATALOGGER_init
static SPP_RetVal_t SPP_SERVICES_DATALOGGER_init(void)
Initialise the SD card and open the log file for writing.
Definition
datalogger.c:67
s_initStatus
static SPP_RetVal_t s_initStatus
Definition
datalogger.c:24
SPP_SERVICES_DATALOGGER_deliverToMailbox
static SPP_RetVal_t SPP_SERVICES_DATALOGGER_deliverToMailbox(const SPP_Packet_t packet)
Receive a packet from the pub/sub router into the consumer mailbox.
Definition
datalogger.c:94
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:21
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:22
SPP_CORE_FSM_getErrorsBit
spp_uint16_t * SPP_CORE_FSM_getErrorsBit(void)
Get the pointer to the FSM errors bit.
Definition
fsm.c:33
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:41
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
K_SPP_ERROR_NULL_POINTER
@ K_SPP_ERROR_NULL_POINTER
Definition
returnTypes.h:30
spi.h
SPP SPI HAL API — dispatches through the registered HAL port.
SPP_HAL_STORAGE_init
SPP_RetVal_t SPP_HAL_STORAGE_init(void)
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)
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:43
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
FsmErrors_t
Definition
custom.h:23
solaris-v2
spp
services
datalogger
datalogger.c
Generated by
1.18.0