-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_evs_log.c
270 lines (231 loc) · 9.72 KB
/
cfe_evs_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
** File: cfe_evs_log.c
**
** Title: Event Services API - Log Control Interfaces
**
** Purpose: This module defines the top level functions of the
** Event Services Log control interfaces
**
*/
/* Include Files */
#include "cfe_evs_module_all.h" /* All EVS internal definitions and API */
#include <string.h>
/*----------------------------------------------------------------
*
* Function: EVS_AddLog
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void EVS_AddLog(CFE_EVS_LongEventTlm_t *EVS_PktPtr)
{
/* Serialize access to event log control variables */
OS_MutSemTake(CFE_EVS_Global.EVS_SharedDataMutexID);
if ((CFE_EVS_Global.EVS_LogPtr->LogFullFlag == true) &&
(CFE_EVS_Global.EVS_LogPtr->LogMode == CFE_EVS_LogMode_DISCARD))
{
/* If log is full and in discard mode, just count the event */
CFE_EVS_Global.EVS_LogPtr->LogOverflowCounter++;
}
else
{
if (CFE_EVS_Global.EVS_LogPtr->LogFullFlag == true)
{
/* If log is full and in wrap mode, count it and store it */
CFE_EVS_Global.EVS_LogPtr->LogOverflowCounter++;
}
/* Copy the event data to the next available entry in the log */
memcpy(&CFE_EVS_Global.EVS_LogPtr->LogEntry[CFE_EVS_Global.EVS_LogPtr->Next], EVS_PktPtr, sizeof(*EVS_PktPtr));
CFE_EVS_Global.EVS_LogPtr->Next++;
if (CFE_EVS_Global.EVS_LogPtr->Next >= CFE_PLATFORM_EVS_LOG_MAX)
{
/* This is important, even if we are in discard mode */
CFE_EVS_Global.EVS_LogPtr->Next = 0;
}
/* Log count cannot exceed the number of entries in the log */
if (CFE_EVS_Global.EVS_LogPtr->LogCount < CFE_PLATFORM_EVS_LOG_MAX)
{
CFE_EVS_Global.EVS_LogPtr->LogCount++;
if (CFE_EVS_Global.EVS_LogPtr->LogCount == CFE_PLATFORM_EVS_LOG_MAX)
{
/* The full flag and log count are somewhat redundant */
CFE_EVS_Global.EVS_LogPtr->LogFullFlag = true;
}
}
}
OS_MutSemGive(CFE_EVS_Global.EVS_SharedDataMutexID);
return;
}
/*----------------------------------------------------------------
*
* Function: EVS_ClearLog
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void EVS_ClearLog(void)
{
/* Serialize access to event log control variables */
OS_MutSemTake(CFE_EVS_Global.EVS_SharedDataMutexID);
/* Clears everything but LogMode (overwrite vs discard) */
CFE_EVS_Global.EVS_LogPtr->Next = 0;
CFE_EVS_Global.EVS_LogPtr->LogCount = 0;
CFE_EVS_Global.EVS_LogPtr->LogFullFlag = false;
CFE_EVS_Global.EVS_LogPtr->LogOverflowCounter = 0;
memset(CFE_EVS_Global.EVS_LogPtr->LogEntry, 0, sizeof(CFE_EVS_Global.EVS_LogPtr->LogEntry));
OS_MutSemGive(CFE_EVS_Global.EVS_SharedDataMutexID);
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_EVS_WriteLogDataFileCmd
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_EVS_WriteLogDataFileCmd(const CFE_EVS_WriteLogDataFileCmd_t *data)
{
const CFE_EVS_LogFileCmd_Payload_t *CmdPtr = &data->Payload;
int32 Result;
int32 LogIndex;
int32 BytesWritten;
osal_id_t LogFileHandle = OS_OBJECT_ID_UNDEFINED;
uint32 i;
CFE_FS_Header_t LogFileHdr;
char LogFilename[OS_MAX_PATH_LEN];
/*
** Copy the filename into local buffer with default name/path/extension if not specified
*/
Result = CFE_FS_ParseInputFileNameEx(LogFilename, CmdPtr->LogFilename, sizeof(LogFilename),
sizeof(CmdPtr->LogFilename), CFE_PLATFORM_EVS_DEFAULT_LOG_FILE,
CFE_FS_GetDefaultMountPoint(CFE_FS_FileCategory_BINARY_DATA_DUMP),
CFE_FS_GetDefaultExtension(CFE_FS_FileCategory_BINARY_DATA_DUMP));
if (Result != OS_SUCCESS)
{
EVS_SendEvent(CFE_EVS_ERR_CRLOGFILE_EID, CFE_EVS_EventType_ERROR,
"Write Log File Command Error: CFE_FS_ParseInputFileNameEx() = 0x%08X", (unsigned int)Result);
}
else
{
/* Create the log file */
Result = OS_OpenCreate(&LogFileHandle, LogFilename, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY);
if (Result != OS_SUCCESS)
{
EVS_SendEvent(CFE_EVS_ERR_CRLOGFILE_EID, CFE_EVS_EventType_ERROR,
"Write Log File Command Error: OS_OpenCreate = 0x%08X, filename = %s", (unsigned int)Result,
LogFilename);
}
}
if (Result == OS_SUCCESS)
{
/* Result will be overridden if everything works */
Result = CFE_EVS_FILE_WRITE_ERROR;
/* Initialize cFE file header for an event log file */
CFE_FS_InitHeader(&LogFileHdr, "cFE EVS Log File", CFE_FS_SubType_EVS_EVENTLOG);
/* Write the file header to the log file */
BytesWritten = CFE_FS_WriteHeader(LogFileHandle, &LogFileHdr);
if (BytesWritten == sizeof(LogFileHdr))
{
/* Serialize access to event log control variables */
OS_MutSemTake(CFE_EVS_Global.EVS_SharedDataMutexID);
/* Is the log full? -- Doesn't matter if wrap mode is enabled */
if (CFE_EVS_Global.EVS_LogPtr->LogCount == CFE_PLATFORM_EVS_LOG_MAX)
{
/* Start with log entry that will be overwritten next (oldest) */
LogIndex = CFE_EVS_Global.EVS_LogPtr->Next;
}
else
{
/* Start with the first entry in the log (oldest) */
LogIndex = 0;
}
/* Write all the "in-use" event log entries to the file */
for (i = 0; i < CFE_EVS_Global.EVS_LogPtr->LogCount; i++)
{
BytesWritten = OS_write(LogFileHandle, &CFE_EVS_Global.EVS_LogPtr->LogEntry[LogIndex],
sizeof(CFE_EVS_Global.EVS_LogPtr->LogEntry[LogIndex]));
if (BytesWritten == sizeof(CFE_EVS_Global.EVS_LogPtr->LogEntry[LogIndex]))
{
LogIndex++;
if (LogIndex >= CFE_PLATFORM_EVS_LOG_MAX)
{
LogIndex = 0;
}
}
else
{
break;
}
}
OS_MutSemGive(CFE_EVS_Global.EVS_SharedDataMutexID);
/* Process command handler success result */
if (i == CFE_EVS_Global.EVS_LogPtr->LogCount)
{
EVS_SendEvent(CFE_EVS_WRLOG_EID, CFE_EVS_EventType_DEBUG,
"Write Log File Command: %d event log entries written to %s",
(int)CFE_EVS_Global.EVS_LogPtr->LogCount, LogFilename);
Result = CFE_SUCCESS;
}
else
{
EVS_SendEvent(CFE_EVS_ERR_WRLOGFILE_EID, CFE_EVS_EventType_ERROR,
"Write Log File Command Error: OS_write = 0x%08X, filename = %s",
(unsigned int)BytesWritten, LogFilename);
}
}
OS_close(LogFileHandle);
}
return (Result);
}
/*----------------------------------------------------------------
*
* Function: CFE_EVS_SetLogModeCmd
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_EVS_SetLogModeCmd(const CFE_EVS_SetLogModeCmd_t *data)
{
const CFE_EVS_SetLogMode_Payload_t *CmdPtr = &data->Payload;
int32 Status;
if ((CmdPtr->LogMode == CFE_EVS_LogMode_OVERWRITE) || (CmdPtr->LogMode == CFE_EVS_LogMode_DISCARD))
{
/* Serialize access to event log control variables */
OS_MutSemTake(CFE_EVS_Global.EVS_SharedDataMutexID);
CFE_EVS_Global.EVS_LogPtr->LogMode = CmdPtr->LogMode;
OS_MutSemGive(CFE_EVS_Global.EVS_SharedDataMutexID);
EVS_SendEvent(CFE_EVS_LOGMODE_EID, CFE_EVS_EventType_DEBUG, "Set Log Mode Command: Log Mode = %d",
(int)CmdPtr->LogMode);
Status = CFE_SUCCESS;
}
else
{
Status = CFE_EVS_INVALID_PARAMETER;
EVS_SendEvent(CFE_EVS_ERR_LOGMODE_EID, CFE_EVS_EventType_ERROR, "Set Log Mode Command Error: Log Mode = %d",
(int)CmdPtr->LogMode);
}
return Status;
}