Skip to content

Commit 307f56e

Browse files
Basic Reading Implementation (untested)
1 parent 10089dd commit 307f56e

File tree

12 files changed

+242
-104
lines changed

12 files changed

+242
-104
lines changed

FprimeZephyrReference/Components/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/Burnwire/")
1010
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/BootloaderTrigger/")
1111
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/AntennaDeployer/")
1212
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/FsSpace/")
13-
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/CameraManager/")
13+
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/PayloadHandler/")

FprimeZephyrReference/Components/CameraManager/CameraManager.cpp

Lines changed: 0 additions & 46 deletions
This file was deleted.

FprimeZephyrReference/Components/CameraManager/CameraManager.hpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

FprimeZephyrReference/Components/CameraManager/CMakeLists.txt renamed to FprimeZephyrReference/Components/PayloadHandler/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
register_fprime_library(
1818
AUTOCODER_INPUTS
19-
"${CMAKE_CURRENT_LIST_DIR}/CameraManager.fpp"
19+
"${CMAKE_CURRENT_LIST_DIR}/PayloadHandler.fpp"
2020
SOURCES
21-
"${CMAKE_CURRENT_LIST_DIR}/CameraManager.cpp"
21+
"${CMAKE_CURRENT_LIST_DIR}/PayloadHandler.cpp"
2222
# DEPENDS
2323
# MyPackage_MyOtherModule
2424
)
2525

2626
### Unit Tests ###
2727
# register_fprime_ut(
2828
# AUTOCODER_INPUTS
29-
# "${CMAKE_CURRENT_LIST_DIR}/CameraManager.fpp"
29+
# "${CMAKE_CURRENT_LIST_DIR}/PayloadHandler.fpp"
3030
# SOURCES
31-
# "${CMAKE_CURRENT_LIST_DIR}/test/ut/CameraManagerTestMain.cpp"
32-
# "${CMAKE_CURRENT_LIST_DIR}/test/ut/CameraManagerTester.cpp"
31+
# "${CMAKE_CURRENT_LIST_DIR}/test/ut/PayloadHandlerTestMain.cpp"
32+
# "${CMAKE_CURRENT_LIST_DIR}/test/ut/PayloadHandlerTester.cpp"
3333
# DEPENDS
3434
# STest # For rules-based testing
3535
# UT_AUTO_HELPERS
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// ======================================================================
2+
// \title PayloadHandler.cpp
3+
// \author robertpendergrast
4+
// \brief cpp file for PayloadHandler component implementation class
5+
// ======================================================================
6+
#include "Os/File.hpp"
7+
#include "FprimeZephyrReference/Components/PayloadHandler/PayloadHandler.hpp"
8+
9+
namespace Components {
10+
11+
// ----------------------------------------------------------------------
12+
// Component construction and destruction
13+
// ----------------------------------------------------------------------
14+
15+
PayloadHandler ::PayloadHandler(const char* const compName) : PayloadHandlerComponentBase(compName) {}
16+
PayloadHandler ::~PayloadHandler() {}
17+
18+
19+
// ----------------------------------------------------------------------
20+
// Handler implementations for typed input ports
21+
// ----------------------------------------------------------------------
22+
23+
24+
void PayloadHandler ::in_port_handler(FwIndexType portNum, Fw::Buffer& buffer, const Drv::ByteStreamStatus& status) {
25+
26+
const U8* data = buffer.getData();
27+
FwSizeType size = buffer.getSize();
28+
29+
for (FwSizeType i = 0; i < size; i++) {
30+
// Process each byte of data as needed
31+
U8 byte = data[i];
32+
33+
34+
if (!m_receiving){
35+
// We are not currently receiving a file
36+
37+
// Append byte to line buffer: This is how we check the header to determine data type
38+
if (m_lineIndex < sizeof(m_lineBuffer) - 1) {
39+
m_lineBuffer[m_lineIndex++] = byte;
40+
}
41+
42+
// Have we reached the end of the line? If so that means we have a header
43+
// Check to see what the header is.
44+
if (byte == '\n' || byte == '\r') {
45+
m_lineBuffer[m_lineIndex] = 0; // Null-terminate
46+
m_lineIndex = 0;
47+
48+
// Check the header.
49+
// Right now I'm just checking for an image start tag, but we can expand this to other types later
50+
if (strstr((const char*)m_lineBuffer, "<IMG_START>")) {
51+
m_receiving = true;
52+
m_bytes_received = 0;
53+
m_expected_size = 0;
54+
continue;
55+
}
56+
57+
// If in receiving mode and expected size not set, this line is the size
58+
if (m_receiving && m_expected_size == 0) {
59+
60+
// First we set the expected size
61+
m_expected_size = atoi((const char*)m_lineBuffer);
62+
63+
64+
// Then we open the file to write to, which we will be writing to over a lot of iterations
65+
if (m_data_file_count >= 9) {
66+
m_data_file_count = 0;
67+
}
68+
69+
char filenameBuffer[20];
70+
snprintf(filenameBuffer, sizeof(filenameBuffer), "payload_%d.jpg", m_data_file_count);
71+
m_currentFilename = filenameBuffer;
72+
73+
// Open the file and prepare to write in the next iteration
74+
Os::File::Status fileStatus = m_file.open(m_currentFilename.c_str(), Os::File::OPEN_CREATE, Os::File::OVERWRITE);
75+
if (fileStatus != Os::File::OP_OK) {
76+
m_receiving = false;
77+
continue;
78+
}
79+
continue;
80+
}
81+
}
82+
83+
84+
} else if (m_bytes_received < m_expected_size){
85+
// We are currently receiving a file
86+
87+
// Cast byte to a buffer
88+
// Write a byte to the file
89+
FwSizeType oneByte = 1;
90+
m_file.write(&byte, oneByte);
91+
m_bytes_received++;
92+
93+
// Check to see if we are done receiving
94+
if (m_bytes_received >= m_expected_size){
95+
m_file.flush();
96+
m_file.close();
97+
m_receiving = false;
98+
m_data_file_count++;
99+
100+
// Log data received event
101+
Fw::LogStringArg logPath(m_currentFilename.c_str());
102+
this->log_ACTIVITY_HI_DataReceived(m_bytes_received, logPath);
103+
}
104+
}
105+
}
106+
}
107+
108+
// ----------------------------------------------------------------------
109+
// Handler implementations for commands
110+
// ----------------------------------------------------------------------
111+
112+
void PayloadHandler ::SEND_COMMAND_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdStringArg& cmd) {
113+
114+
// Append newline to command to send over UART
115+
Fw::CmdStringArg tempCmd = cmd;
116+
tempCmd += "\n";
117+
Fw::Buffer commandBuffer(
118+
reinterpret_cast<U8*>(const_cast<char*>(tempCmd.toChar())),
119+
tempCmd.length()
120+
);
121+
122+
// Send command over output port
123+
Drv::ByteStreamStatus sendStatus = this->out_port_out(0, commandBuffer);
124+
125+
Fw::LogStringArg logCmd(cmd);
126+
127+
// Log success or failure
128+
if (sendStatus != Drv::ByteStreamStatus::OP_OK) {
129+
this->log_WARNING_HI_CommandError(logCmd);
130+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
131+
return;
132+
}
133+
else {
134+
this->log_ACTIVITY_HI_CommandSuccess(logCmd);
135+
}
136+
137+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
138+
}
139+
140+
} // namespace Components

FprimeZephyrReference/Components/CameraManager/CameraManager.fpp renamed to FprimeZephyrReference/Components/PayloadHandler/PayloadHandler.fpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
module Components {
22
@ Manager for Nicla Vision
3-
passive component CameraManager {
3+
passive component PayloadHandler {
44

55
# One async command/port is required for active components
66
# This should be overridden by the developers with a useful command/port
77
@ TODO
8-
sync command TAKE_IMAGE
8+
sync command SEND_COMMAND(cmd: string) # Command to send data over UART
99

10-
event TakeImageError() severity warning high format "Failed to take picture"
10+
event CommandError(cmd: string) severity warning high format "Failed to send {} command over UART"
1111

12-
event PictureTaken() severity activity high format "Picture Taken"
12+
event CommandSuccess(cmd: string) severity activity high format "Command {} sent successfully"
13+
14+
event DataReceived( data: U8, path: string) severity activity high format "Stored {} bytes of payload data to {}"
1315

1416
output port out_port: Drv.ByteStreamSend
1517

18+
sync input port in_port: Drv.ByteStreamData
19+
1620
##############################################################################
1721
#### Uncomment the following examples to start customizing your component ####
1822
##############################################################################
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ======================================================================
2+
// \title PayloadHandler.hpp
3+
// \author robertpendergrast
4+
// \brief hpp file for PayloadHandler component implementation class
5+
// ======================================================================
6+
7+
#ifndef FprimeZephyrReference_PayloadHandler_HPP
8+
#define FprimeZephyrReference_PayloadHandler_HPP
9+
10+
#include <string>
11+
#include <cstddef>
12+
#include "Os/File.hpp"
13+
#include "FprimeZephyrReference/Components/PayloadHandler/PayloadHandlerComponentAc.hpp"
14+
15+
namespace Components {
16+
17+
class PayloadHandler final : public PayloadHandlerComponentBase {
18+
public:
19+
// ----------------------------------------------------------------------
20+
// Component construction and destruction
21+
// ----------------------------------------------------------------------
22+
23+
//! Construct PayloadHandler object
24+
PayloadHandler(const char* const compName //!< The component name
25+
);
26+
27+
//! Destroy PayloadHandler object
28+
~PayloadHandler();
29+
30+
U8 m_data_file_count = 0;
31+
bool m_receiving = false;
32+
U32 m_expected_size = 0;
33+
U32 m_bytes_received = 0;
34+
35+
U8 m_lineBuffer[128];
36+
size_t m_lineIndex = 0;
37+
Os::File m_file;
38+
std::string m_currentFilename;
39+
40+
private:
41+
// ----------------------------------------------------------------------
42+
// Handler implementations for typed input ports
43+
// ----------------------------------------------------------------------
44+
45+
//! Handler implementation for in_port
46+
//! Handler implementation for in_port
47+
void in_port_handler(FwIndexType portNum, //!< The port number
48+
Fw::Buffer& buffer,
49+
const Drv::ByteStreamStatus& status);
50+
51+
private:
52+
// ----------------------------------------------------------------------
53+
// Handler implementations for commands
54+
// ----------------------------------------------------------------------
55+
56+
//! Handler implementation for command SEND_COMMAND
57+
//!
58+
//! TODO
59+
void SEND_COMMAND_cmdHandler(FwOpcodeType opCode, //!< The opcode
60+
U32 cmdSeq, //!< The command sequence number
61+
const Fw::CmdStringArg& cmd);
62+
};
63+
64+
} // namespace Components
65+
66+
#endif
File renamed without changes.

FprimeZephyrReference/ReferenceDeployment/Top/instances.fpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module ReferenceDeployment {
9090

9191
instance fsSpace: Components.FsSpace base id 0x10030000
9292

93-
instance camera: Components.CameraManager base id 0x10031000
93+
instance payload: Components.PayloadHandler base id 0x10031000
9494

9595
instance peripheralUartDriver: Zephyr.ZephyrUartDriver base id 0x10032000
9696

FprimeZephyrReference/ReferenceDeployment/Top/topology.fpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module ReferenceDeployment {
4444
# For UART sideband communication
4545
instance comDriver
4646
instance fsSpace
47-
instance camera
47+
instance payload
4848
instance peripheralUartDriver
4949

5050

@@ -164,8 +164,9 @@ module ReferenceDeployment {
164164
imuManager.temperatureGet -> lsm6dsoManager.temperatureGet
165165
}
166166

167-
connections Camera {
168-
camera.out_port -> peripheralUartDriver.$send
167+
connections PayloadHandler {
168+
payload.out_port -> peripheralUartDriver.$send
169+
peripheralUartDriver.$recv -> payload.in_port
169170
}
170171
}
171172
}

0 commit comments

Comments
 (0)