2023-04-13 17:55:56 +02:00
|
|
|
#include "ArcsecDatalinkLayer.h"
|
|
|
|
|
|
|
|
extern "C" {
|
2023-04-15 15:15:06 +02:00
|
|
|
#include <wire/common/SLIP.h>
|
2023-04-13 18:09:02 +02:00
|
|
|
#include <wire/common/misc.h>
|
2023-04-13 17:55:56 +02:00
|
|
|
}
|
2021-12-02 08:05:33 +01:00
|
|
|
|
2023-03-22 10:38:41 +01:00
|
|
|
ArcsecDatalinkLayer::ArcsecDatalinkLayer() : decodeRingBuf(BUFFER_LENGTHS, true) { slipInit(); }
|
2021-12-02 08:05:33 +01:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
ArcsecDatalinkLayer::~ArcsecDatalinkLayer() {}
|
2021-12-02 08:05:33 +01:00
|
|
|
|
2023-03-21 18:47:42 +01:00
|
|
|
ReturnValue_t ArcsecDatalinkLayer::checkRingBufForFrame(const uint8_t** decodedFrame,
|
|
|
|
size_t& frameLen) {
|
|
|
|
size_t currentLen = decodeRingBuf.getAvailableReadData();
|
2023-03-24 00:59:41 +01:00
|
|
|
if (currentLen == 0) {
|
2023-03-24 02:34:38 +01:00
|
|
|
return DEC_IN_PROGRESS;
|
2023-03-24 00:59:41 +01:00
|
|
|
}
|
2023-03-22 10:38:41 +01:00
|
|
|
decodeRingBuf.readData(rxAnalysisBuffer, currentLen);
|
2023-04-15 15:15:06 +02:00
|
|
|
|
|
|
|
bool startFound = false;
|
|
|
|
size_t startIdx = 0;
|
2023-03-21 18:47:42 +01:00
|
|
|
for (size_t idx = 0; idx < currentLen; idx++) {
|
2023-04-15 15:15:06 +02:00
|
|
|
if (rxAnalysisBuffer[idx] != SLIP_START_AND_END) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (not startFound) {
|
|
|
|
startFound = true;
|
|
|
|
startIdx = idx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Now we can try decoding the whole frame.
|
|
|
|
size_t encodedDataSize = 0;
|
|
|
|
// TODO: Find SLIP ID
|
|
|
|
slip_error_t slipError = slip_decode_frame(
|
|
|
|
decodedRxFrame, &rxFrameSize, rxAnalysisBuffer + startIdx,
|
|
|
|
sizeof(rxAnalysisBuffer) - startIdx, &encodedDataSize, ARC_DEF_SAGITTA_SLIP_ID);
|
|
|
|
decodeRingBuf.deleteData(idx + 1);
|
|
|
|
switch (slipError) {
|
|
|
|
case (SLIP_OK): {
|
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
case (SLIP_BAD_CRC): {
|
|
|
|
return CRC_FAILURE;
|
2023-03-21 18:47:42 +01:00
|
|
|
}
|
2023-04-15 15:15:06 +02:00
|
|
|
case (SLIP_OVERFLOW): {
|
|
|
|
return SLIP_OVERFLOW_RETVAL;
|
|
|
|
}
|
|
|
|
// Should not happen, we searched for start and end marker..
|
|
|
|
case (SLIP_NO_END): {
|
2023-04-13 17:55:56 +02:00
|
|
|
return returnvalue::FAILED;
|
2023-03-21 18:47:42 +01:00
|
|
|
}
|
2023-04-15 15:15:06 +02:00
|
|
|
case (SLIP_ID_MISMATCH): {
|
|
|
|
return SLIP_ID_MISSMATCH_RETVAL;
|
2023-03-21 18:47:42 +01:00
|
|
|
}
|
2023-04-15 15:15:06 +02:00
|
|
|
default: {
|
2023-03-21 18:47:42 +01:00
|
|
|
return returnvalue::FAILED;
|
2023-04-15 15:15:06 +02:00
|
|
|
}
|
2023-03-21 18:47:42 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 15:15:06 +02:00
|
|
|
// switch (decResult) {
|
|
|
|
// case ARC_DEC_INPROGRESS: {
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// case ARC_DEC_ERROR: {
|
|
|
|
// decodeRingBuf.deleteData(idx);
|
|
|
|
// return returnvalue::FAILED;
|
|
|
|
// }
|
|
|
|
// case ARC_DEC_ASYNC:
|
|
|
|
// case ARC_DEC_SYNC: {
|
|
|
|
// // Reset length of SLIP struct for next frame
|
|
|
|
// slipInfo.length = 0;
|
|
|
|
// if (decodedFrame != nullptr) {
|
|
|
|
// *decodedFrame = decodedRxFrame;
|
|
|
|
// }
|
|
|
|
// frameLen = rxFrameSize;
|
|
|
|
// decodeRingBuf.deleteData(idx);
|
|
|
|
// return returnvalue::OK;
|
|
|
|
// }
|
|
|
|
// default:
|
|
|
|
// sif::debug << "ArcsecDatalinkLayer::decodeFrame: Unknown result code" << std::endl;
|
|
|
|
// break;
|
|
|
|
// return returnvalue::FAILED;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// decodeRingBuf.deleteData(currentLen);
|
2023-03-21 18:47:42 +01:00
|
|
|
return DEC_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2023-03-21 20:35:28 +01:00
|
|
|
ReturnValue_t ArcsecDatalinkLayer::feedData(const uint8_t* rawData, size_t rawDataLen) {
|
|
|
|
if (rawDataLen > 4096) {
|
|
|
|
sif::error << "ArcsecDatalinklayer: Can not write more than 4096 bytes to ring buffer"
|
|
|
|
<< std::endl;
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
return decodeRingBuf.writeData(rawData, rawDataLen);
|
|
|
|
}
|
|
|
|
|
2023-03-22 10:38:41 +01:00
|
|
|
void ArcsecDatalinkLayer::reset() {
|
2023-04-15 15:15:06 +02:00
|
|
|
// slipInit();
|
2023-03-22 10:38:41 +01:00
|
|
|
decodeRingBuf.clear();
|
2021-12-02 08:05:33 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 15:15:06 +02:00
|
|
|
// void ArcsecDatalinkLayer::slipInit() {
|
|
|
|
// slip_decode_init(rxBufferArc, sizeof(rxBufferArc), &slipInfo);
|
|
|
|
// }
|
2021-12-02 08:05:33 +01:00
|
|
|
|
2023-03-21 20:35:28 +01:00
|
|
|
void ArcsecDatalinkLayer::encodeFrame(const uint8_t* data, size_t length, const uint8_t** txFrame,
|
2023-03-21 18:47:42 +01:00
|
|
|
size_t& size) {
|
2023-04-15 15:15:06 +02:00
|
|
|
slip_encode_frame(data, length, txEncoded, &size, ARC_DEF_SAGITTA_SLIP_ID);
|
2023-03-21 18:47:42 +01:00
|
|
|
if (txFrame != nullptr) {
|
|
|
|
*txFrame = txEncoded;
|
|
|
|
}
|
2021-12-02 08:05:33 +01:00
|
|
|
}
|