2022-04-09 14:43:06 +02:00
|
|
|
#include "ScexDefinitions.h"
|
|
|
|
|
2022-04-29 15:46:16 +02:00
|
|
|
#include <fsfw/globalfunctions/CRC.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
uint8_t scex::createCmdByte(Cmds cmd, bool tempCheck) {
|
2022-04-09 14:43:06 +02:00
|
|
|
return (IDLE_BIT_0_DEF_STATE << 7) | (IDLE_BIT_1_DEF_STATE << 6) | (cmd << 1) | tempCheck;
|
|
|
|
}
|
2022-04-29 15:46:16 +02:00
|
|
|
|
2022-06-21 16:44:14 +02:00
|
|
|
ReturnValue_t scex::prepareScexCmd(Cmds cmd, std::pair<uint8_t*, size_t> cmdBufPair, size_t& cmdLen,
|
|
|
|
std::pair<const uint8_t*, size_t> usrDataPair, bool tempCheck) {
|
2022-04-29 15:46:16 +02:00
|
|
|
using namespace scex;
|
|
|
|
uint8_t* cmdBuf = cmdBufPair.first;
|
|
|
|
const uint8_t* userData = usrDataPair.first;
|
|
|
|
// Send command
|
|
|
|
if (cmdBuf == nullptr or (cmdBufPair.second < usrDataPair.second + HEADER_LEN + CRC_LEN) or
|
|
|
|
(usrDataPair.second > 0 and userData == nullptr)) {
|
|
|
|
cmdLen = 0;
|
2022-08-29 16:05:05 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-04-29 15:46:16 +02:00
|
|
|
}
|
2022-05-29 17:52:13 +02:00
|
|
|
cmdBuf[0] = createCmdByte(cmd, tempCheck);
|
2022-04-29 15:46:16 +02:00
|
|
|
// These two fields are the packet counter and the total packet count. Those are 1 and 1 for each
|
|
|
|
// telecommand so far
|
|
|
|
cmdBuf[1] = 1;
|
|
|
|
cmdBuf[2] = 1;
|
|
|
|
cmdBuf[3] = (usrDataPair.second >> 8) & 0xff;
|
|
|
|
cmdBuf[4] = usrDataPair.second & 0xff;
|
|
|
|
std::memcpy(cmdBuf + HEADER_LEN, userData, usrDataPair.second);
|
|
|
|
uint16_t crc = CRC::crc16ccitt(cmdBuf, usrDataPair.second + HEADER_LEN);
|
|
|
|
cmdBuf[usrDataPair.second + HEADER_LEN] = (crc >> 8) & 0xff;
|
|
|
|
cmdBuf[usrDataPair.second + HEADER_LEN + 1] = crc & 0xff;
|
|
|
|
cmdLen = usrDataPair.second + HEADER_LEN + CRC_LEN;
|
2022-08-29 16:05:05 +02:00
|
|
|
return returnvalue::OK;
|
2022-04-29 15:46:16 +02:00
|
|
|
}
|