eive-obsw/bsp_linux/comIF/ArduinoComIF.cpp

325 lines
9.5 KiB
C++
Raw Normal View History

2020-12-04 14:14:08 +01:00
//#include "cookies/ArduinoCookie.h"
//#include "ArduinoComIF.h"
//
//#include <fsfw/globalfunctions/DleEncoder.h>
//#include <stdio.h>
//#include <string.h>
//#include <unistd.h>
//#include <fcntl.h>
//#include <errno.h>
//#include <fsfw/globalfunctions/CRC.h>
//
//#include <termios.h>
//
//ArduinoCommInterface::ArduinoCommInterface(object_id_t setObjectId,
// const char *serialDevice) :
// spiMap(MAX_NUMBER_OF_SPI_DEVICES), rxBuffer(
// MAX_PACKET_SIZE * MAX_NUMBER_OF_SPI_DEVICES*10, true), SystemObject(setObjectId) {
// initialized = false;
// serialPort = ::open("/dev/ttyUSB0", O_RDWR);
//
// if (serialPort < 0) {
// //configuration error
// printf("Error %i from open: %s\n", errno, strerror(errno));
// return;
// }
//
// struct termios tty;
// memset(&tty, 0, sizeof tty);
//
// // Read in existing settings, and handle any error
// if (tcgetattr(serialPort, &tty) != 0) {
// printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
// return;
// }
//
// tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity
// tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
// tty.c_cflag |= CS8; // 8 bits per byte
// tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
// tty.c_lflag &= ~ICANON; //Disable Canonical Mode
// tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
// tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// tty.c_cc[VTIME] = 0; // Non Blocking
// tty.c_cc[VMIN] = 0;
//
// cfsetispeed(&tty, B9600); //Baudrate
//
// if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
// //printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
// return;
// }
//
// initialized = true;
//
//}
//
//ArduinoCommInterface::~ArduinoCommInterface() {
// ::close(serialPort);
//}
//
//ReturnValue_t ArduinoCommInterface::open(Cookie **cookie, uint32_t address,
// uint32_t maxReplyLen) {
// //This is a hack, will be gone with https://egit.irs.uni-stuttgart.de/fsfw/fsfw/issues/19
// switch ((address >> 8) & 0xff) {
// case 0:
// *cookie = new ArduinoCookie(ArduinoCookie::SPI, address, maxReplyLen);
// spiMap.insert(address, (ArduinoCookie*) *cookie); //Yes, I *do* know that it is an ArduinoSpiCookie, I just new'd it
// break;
// default:
// return HasReturnvaluesIF::RETURN_FAILED;
// }
// return HasReturnvaluesIF::RETURN_OK;
//}
//
//ReturnValue_t ArduinoCommInterface::reOpen(Cookie *cookie, uint32_t address,
// uint32_t maxReplyLen) {
// //too lazy right now will be irrelevant with https://egit.irs.uni-stuttgart.de/fsfw/fsfw/issues/19
// return HasReturnvaluesIF::RETURN_FAILED;
//}
//
//void ArduinoCommInterface::close(Cookie *cookie) {
// //too lazy as well, find the correct Map, delete it there, then the cookie...
//}
//
//ReturnValue_t ArduinoCommInterface::sendMessage(Cookie *cookie, uint8_t *data,
// uint32_t len) {
// ArduinoCookie *arduinoCookie = dynamic_cast<ArduinoCookie*>(cookie);
// if (arduinoCookie == NULL) {
// return INVALID_COOKIE_TYPE;
// }
//
// return sendMessage(arduinoCookie->command, arduinoCookie->address, data,
// len);
//}
//
//ReturnValue_t ArduinoCommInterface::getSendSuccess(Cookie *cookie) {
// return RETURN_OK;
//}
//
//ReturnValue_t ArduinoCommInterface::requestReceiveMessage(Cookie *cookie) {
// return RETURN_OK;
//}
//
//ReturnValue_t ArduinoCommInterface::readReceivedMessage(Cookie *cookie,
// uint8_t **buffer, uint32_t *size) {
//
// handleSerialPortRx();
//
// ArduinoCookie *arduinoCookie = dynamic_cast<ArduinoCookie*>(cookie);
// if (arduinoCookie == NULL) {
// return INVALID_COOKIE_TYPE;
// }
//
// *buffer = arduinoCookie->replyBuffer;
// *size = arduinoCookie->receivedDataLen;
// return HasReturnvaluesIF::RETURN_OK;
//}
//
//ReturnValue_t ArduinoCommInterface::setAddress(Cookie *cookie,
// uint32_t address) {
// //not implemented
// return RETURN_FAILED;
//}
//
//uint32_t ArduinoCommInterface::getAddress(Cookie *cookie) {
// //not implemented
// return 0;
//}
//
//ReturnValue_t ArduinoCommInterface::setParameter(Cookie *cookie,
// uint32_t parameter) {
// //not implemented
// return RETURN_FAILED;
//}
//
//uint32_t ArduinoCommInterface::getParameter(Cookie *cookie) {
// //not implemented
// return 0;
//}
//
//ReturnValue_t ArduinoCommInterface::sendMessage(uint8_t command,
// uint8_t address, const uint8_t *data, size_t dataLen) {
// if (dataLen > UINT16_MAX) {
// return TOO_MUCH_DATA;
// }
//
// //being conservative here
// uint8_t sendBuffer[(dataLen + 6) * 2 + 2];
//
// sendBuffer[0] = DleEncoder::STX;
//
// uint8_t *currentPosition = sendBuffer + 1;
// size_t remainingLen = sizeof(sendBuffer) - 1;
// uint32_t encodedLen;
//
// ReturnValue_t result = DleEncoder::encode(&command, 1, currentPosition,
// remainingLen, &encodedLen, false);
// if (result != RETURN_OK) {
// return result;
// }
// currentPosition += encodedLen;
// remainingLen -= encodedLen; //DleEncoder will never return encodedLen > remainingLen
//
// result = DleEncoder::encode(&address, 1, currentPosition, remainingLen,
// &encodedLen, false);
// if (result != RETURN_OK) {
// return result;
// }
// currentPosition += encodedLen;
// remainingLen -= encodedLen; //DleEncoder will never return encodedLen > remainingLen
//
// uint8_t temporaryBuffer[2];
//
// //note to Lukas: yes we _could_ use Serialize here, but for 16 bit it is a bit too much...
// temporaryBuffer[0] = dataLen >> 8; //we checked dataLen above
// temporaryBuffer[1] = dataLen;
//
// result = DleEncoder::encode(temporaryBuffer, 2, currentPosition,
// remainingLen, &encodedLen, false);
// if (result != RETURN_OK) {
// return result;
// }
// currentPosition += encodedLen;
// remainingLen -= encodedLen; //DleEncoder will never return encodedLen > remainingLen
//
// //encoding the actual data
// result = DleEncoder::encode(data, dataLen, currentPosition, remainingLen,
// &encodedLen, false);
// if (result != RETURN_OK) {
// return result;
// }
// currentPosition += encodedLen;
// remainingLen -= encodedLen; //DleEncoder will never return encodedLen > remainingLen
//
// uint16_t crc = CRC::crc16ccitt(&command, 1);
// crc = CRC::crc16ccitt(&address, 1, crc);
// //fortunately the length is still there
// crc = CRC::crc16ccitt(temporaryBuffer, 2, crc);
// crc = CRC::crc16ccitt(data, dataLen, crc);
//
// temporaryBuffer[0] = crc >> 8;
// temporaryBuffer[1] = crc;
//
// result = DleEncoder::encode(temporaryBuffer, 2, currentPosition,
// remainingLen, &encodedLen, false);
// if (result != RETURN_OK) {
// return result;
// }
// currentPosition += encodedLen;
// remainingLen -= encodedLen; //DleEncoder will never return encodedLen > remainingLen
//
// if (remainingLen > 0) {
// *currentPosition = DleEncoder::ETX;
// }
// remainingLen -= 1;
//
// encodedLen = sizeof(sendBuffer) - remainingLen;
//
// ssize_t writtenlen = write(serialPort, sendBuffer, encodedLen);
// if (writtenlen < 0) {
// //we could try to find out what happened...
// return RETURN_FAILED;
// }
// if (writtenlen != encodedLen) {
// //the OS failed us, we do not try to block until everything is written, as
// //we can not block the whole system here
// return RETURN_FAILED;
// }
// return RETURN_OK;
//}
//
//void ArduinoCommInterface::handleSerialPortRx() {
// uint32_t availableSpace = rxBuffer.availableWriteSpace();
//
// uint8_t dataFromSerial[availableSpace];
//
// ssize_t bytesRead = read(serialPort, dataFromSerial,
// sizeof(dataFromSerial));
//
// if (bytesRead < 0) {
// return;
// }
//
// rxBuffer.writeData(dataFromSerial, bytesRead);
//
// uint8_t dataReceivedSoFar[rxBuffer.maxSize()];
//
// uint32_t dataLenReceivedSoFar = 0;
//
// rxBuffer.readData(dataReceivedSoFar, sizeof(dataReceivedSoFar), true,
// &dataLenReceivedSoFar);
//
// //look for STX
// size_t firstSTXinRawData = 0;
// while ((firstSTXinRawData < dataLenReceivedSoFar)
// && (dataReceivedSoFar[firstSTXinRawData] != DleEncoder::STX)) {
// firstSTXinRawData++;
// }
//
// if (dataReceivedSoFar[firstSTXinRawData] != DleEncoder::STX) {
// //there is no STX in our data, throw it away...
// rxBuffer.deleteData(dataLenReceivedSoFar);
// return;
// }
//
// uint8_t packet[MAX_PACKET_SIZE];
// uint32_t packetLen;
//
// uint32_t readSize;
//
// ReturnValue_t result = DleEncoder::decode(
// dataReceivedSoFar + firstSTXinRawData,
// dataLenReceivedSoFar - firstSTXinRawData, &readSize, packet,
// sizeof(packet), &packetLen);
//
// size_t toDelete = firstSTXinRawData;
// if (result == HasReturnvaluesIF::RETURN_OK) {
// handlePacket(packet, packetLen);
//
// //after handling the packet, we can delete it from the raw stream, it has been copied to packet
// toDelete += readSize;
// }
//
// //remove Data which was processed
// rxBuffer.deleteData(toDelete);
//}
//
//void ArduinoCommInterface::handlePacket(uint8_t *packet, size_t packetLen) {
// uint16_t crc = CRC::crc16ccitt(packet, packetLen);
// if (crc != 0) {
// //CRC error
// return;
// }
//
// uint8_t command = packet[0];
// uint8_t address = packet[1];
//
// uint16_t size = (packet[2] << 8) + packet[3];
//
// if (size != packetLen - 6) {
// //Invalid Length
// return;
// }
//
// switch (command) {
// case ArduinoCookie::SPI: {
// ArduinoCookie **itsComplicated;
// ReturnValue_t result = spiMap.find(address, &itsComplicated);
// if (result != RETURN_OK) {
// //we do no know this address
// return;
// }
// ArduinoCookie *theActualCookie = *itsComplicated;
// if (packetLen > theActualCookie->maxReplySize + 6) {
// packetLen = theActualCookie->maxReplySize + 6;
// }
// memcpy(theActualCookie->replyBuffer, packet + 4, packetLen - 6);
// theActualCookie->receivedDataLen = packetLen - 6;
// }
// break;
// default:
// return;
// }
//}