F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileManager.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FileManager.hpp
3 // \author bocchino
4 // \brief hpp file for FileManager component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 
17 #include "Fw/Types/Assert.hpp"
18 #include "Fw/Types/BasicTypes.hpp"
19 
20 namespace Svc {
21 
22  // ----------------------------------------------------------------------
23  // Construction, initialization, and destruction
24  // ----------------------------------------------------------------------
25 
28  const char *const compName
29  ) :
30  FileManagerComponentBase(compName),
31  commandCount(0),
32  errorCount(0)
33  {
34 
35  }
36 
37  void FileManager ::
39  const NATIVE_INT_TYPE queueDepth,
40  const NATIVE_INT_TYPE instance
41  )
42  {
43  FileManagerComponentBase::init(queueDepth, instance);
44  }
45 
48  {
49 
50  }
51 
52  // ----------------------------------------------------------------------
53  // Command handler implementations
54  // ----------------------------------------------------------------------
55 
56  void FileManager ::
57  CreateDirectory_cmdHandler(
58  const FwOpcodeType opCode,
59  const U32 cmdSeq,
60  const Fw::CmdStringArg& dirName
61  )
62  {
63  Fw::LogStringArg logStringDirName(dirName.toChar());
64  this->log_ACTIVITY_HI_CreateDirectoryStarted(logStringDirName);
65  const Os::FileSystem::Status status =
67  if (status != Os::FileSystem::OP_OK) {
68  this->log_WARNING_HI_DirectoryCreateError(
69  logStringDirName,
70  status
71  );
72  } else {
73  this->log_ACTIVITY_HI_CreateDirectorySucceeded(logStringDirName);
74  }
75  this->emitTelemetry(status);
76  this->sendCommandResponse(opCode, cmdSeq, status);
77  }
78 
79  void FileManager ::
80  RemoveFile_cmdHandler(
81  const FwOpcodeType opCode,
82  const U32 cmdSeq,
83  const Fw::CmdStringArg& fileName
84  )
85  {
86  Fw::LogStringArg logStringFileName(fileName.toChar());
87  this->log_ACTIVITY_HI_RemoveFileStarted(logStringFileName);
88  const Os::FileSystem::Status status =
90  if (status != Os::FileSystem::OP_OK) {
91  this->log_WARNING_HI_FileRemoveError(
92  logStringFileName,
93  status
94  );
95  } else {
96  this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
97  }
98  this->emitTelemetry(status);
99  this->sendCommandResponse(opCode, cmdSeq, status);
100  }
101 
102  void FileManager ::
103  MoveFile_cmdHandler(
104  const FwOpcodeType opCode,
105  const U32 cmdSeq,
106  const Fw::CmdStringArg& sourceFileName,
107  const Fw::CmdStringArg& destFileName
108  )
109  {
110  Fw::LogStringArg logStringSource(sourceFileName.toChar());
111  Fw::LogStringArg logStringDest(destFileName.toChar());
112  this->log_ACTIVITY_HI_MoveFileStarted(logStringSource, logStringDest);
113  const Os::FileSystem::Status status =
115  sourceFileName.toChar(),
116  destFileName.toChar()
117  );
118  if (status != Os::FileSystem::OP_OK) {
119  this->log_WARNING_HI_FileMoveError(
120  logStringSource, logStringDest, status
121  );
122  } else {
123  this->log_ACTIVITY_HI_MoveFileSucceeded(logStringSource, logStringDest);
124  }
125  this->emitTelemetry(status);
126  this->sendCommandResponse(opCode, cmdSeq, status);
127  }
128 
129  void FileManager ::
130  RemoveDirectory_cmdHandler(
131  const FwOpcodeType opCode,
132  const U32 cmdSeq,
133  const Fw::CmdStringArg& dirName
134  )
135  {
136  Fw::LogStringArg logStringDirName(dirName.toChar());
137  this->log_ACTIVITY_HI_RemoveDirectoryStarted(logStringDirName);
138  const Os::FileSystem::Status status =
140  if (status != Os::FileSystem::OP_OK) {
141  this->log_WARNING_HI_DirectoryRemoveError(
142  logStringDirName,
143  status
144  );
145  } else {
146  this->log_ACTIVITY_HI_RemoveDirectorySucceeded(logStringDirName);
147  }
148  this->emitTelemetry(status);
149  this->sendCommandResponse(opCode, cmdSeq, status);
150  }
151 
152  void FileManager ::
153  ShellCommand_cmdHandler(
154  const FwOpcodeType opCode,
155  const U32 cmdSeq,
156  const Fw::CmdStringArg& command,
157  const Fw::CmdStringArg& logFileName
158  )
159  {
160  Fw::LogStringArg logStringCommand(command.toChar());
161  this->log_ACTIVITY_HI_ShellCommandStarted(
162  logStringCommand
163  );
164  NATIVE_INT_TYPE status =
165  this->systemCall(command, logFileName);
166  if (status == 0) {
167  this->log_ACTIVITY_HI_ShellCommandSucceeded(
168  logStringCommand
169  );
170  } else {
171  this->log_WARNING_HI_ShellCommandFailed(
172  logStringCommand, status
173  );
174  }
175  this->emitTelemetry(
177  );
178  this->sendCommandResponse(
179  opCode,
180  cmdSeq,
182  );
183  }
184 
185  void FileManager ::
186  AppendFile_cmdHandler(
187  const FwOpcodeType opCode,
188  const U32 cmdSeq,
189  const Fw::CmdStringArg& source,
190  const Fw::CmdStringArg& target
191  )
192  {
193  Fw::LogStringArg logStringSource(source.toChar());
194  Fw::LogStringArg logStringTarget(target.toChar());
195  this->log_ACTIVITY_HI_AppendFileStarted(logStringSource, logStringTarget);
196 
197  Os::FileSystem::Status status;
198  status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
199  if (status != Os::FileSystem::OP_OK) {
200  this->log_WARNING_HI_AppendFileFailed(
201  logStringSource,
202  logStringTarget,
203  status
204  );
205  } else {
206  this->log_ACTIVITY_HI_AppendFileSucceeded(
207  logStringSource,
208  logStringTarget
209  );
210  }
211 
212  this->emitTelemetry(status);
213  this->sendCommandResponse(opCode, cmdSeq, status);
214  }
215 
216  void FileManager ::
217  pingIn_handler(
218  const NATIVE_INT_TYPE portNum,
219  U32 key
220  )
221  {
222  // return key
223  this->pingOut_out(0,key);
224  }
225  // ----------------------------------------------------------------------
226  // Helper methods
227  // ----------------------------------------------------------------------
228 
229  NATIVE_INT_TYPE FileManager ::
230  systemCall(
231  const Fw::CmdStringArg& command,
232  const Fw::CmdStringArg& logFileName
233  ) const
234  {
235  const char evalStr[] = "eval '%s' 1>>%s 2>&1\n";
236  const U32 bufferSize = sizeof(evalStr) - 4 + 2 * FW_CMD_STRING_MAX_SIZE;
237  char buffer[bufferSize];
238 
239  NATIVE_INT_TYPE bytesCopied = snprintf(
240  buffer, sizeof(buffer), evalStr,
241  command.toChar(),
242  logFileName.toChar()
243  );
244  FW_ASSERT(static_cast<NATIVE_UINT_TYPE>(bytesCopied) < sizeof(buffer));
245 
246  const int status = system(buffer);
247  return status;
248  }
249 
250  void FileManager ::
251  emitTelemetry(const Os::FileSystem::Status status)
252  {
253  if (status == Os::FileSystem::OP_OK) {
254  ++this->commandCount;
255  this->tlmWrite_CommandsExecuted(this->commandCount);
256  }
257  else {
258  ++this->errorCount;
259  this->tlmWrite_Errors(this->errorCount);
260  }
261  }
262 
263  void FileManager ::
264  sendCommandResponse(
265  const FwOpcodeType opCode,
266  const U32 cmdSeq,
267  const Os::FileSystem::Status status
268  )
269  {
270  this->cmdResponse_out(
271  opCode,
272  cmdSeq,
273  (status == Os::FileSystem::OP_OK) ?
274  Fw::COMMAND_OK : Fw::COMMAND_EXECUTION_ERROR
275  );
276  }
277 
278 }
Os::FileSystem::Status
Status
Definition: FileSystem.hpp:15
Fw::CmdStringArg::toChar
const char * toChar(void) const
Definition: CmdString.cpp:34
FW_CMD_STRING_MAX_SIZE
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition: FpConfig.hpp:231
Svc::FileManager::init
void init(const NATIVE_INT_TYPE queueDepth, const NATIVE_INT_TYPE instance)
Definition: FileManager.cpp:38
Fw::LogStringArg
Definition: LogString.hpp:11
Os::FileSystem::removeDirectory
Status removeDirectory(const char *path)
remove a directory at location path
Definition: FileSystem.cpp:13
Fw::CmdStringArg
Definition: CmdString.hpp:11
Assert.hpp
Os::FileSystem::OTHER_ERROR
@ OTHER_ERROR
other OS-specific error
Definition: FileSystem.hpp:26
Os::FileSystem::moveFile
Status moveFile(const char *originPath, const char *destPath)
Definition: FileSystem.cpp:27
Os::FileSystem::OP_OK
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:16
Svc::FileManager::~FileManager
~FileManager(void)
Definition: FileManager.cpp:47
FwOpcodeType
#define FwOpcodeType
Type representation for a command opcode.
Definition: FpConfig.hpp:62
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Os::FileSystem::appendFile
Status appendFile(const char *originPath, const char *destPath, bool createMissingDest=false)
copies a file from origin to destination
Definition: FileSystem.cpp:404
Svc
Definition: ActiveRateGroupImplCfg.hpp:18
FileManager.hpp
Os::FileSystem::createDirectory
Status createDirectory(const char *path)
create a new directory at location path
Definition: FileSystem.cpp:9
BasicTypes.hpp
Declares ISF basic types.
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Os::FileSystem::removeFile
Status removeFile(const char *path)
removes a file at location path
Definition: FileSystem.cpp:22
Svc::FileManager::FileManager
FileManager(const char *const compName)
Definition: FileManager.cpp:27