diff --git a/bsp_linux/comIF/ArduinoComIF.cpp b/bsp_linux/comIF/ArduinoComIF.cpp deleted file mode 100644 index ffc59b47..00000000 --- a/bsp_linux/comIF/ArduinoComIF.cpp +++ /dev/null @@ -1,324 +0,0 @@ -#include "ArduinoCookie.h" -#include "ArduinoComIF.h" - -#include -#include -#include -#include -#include -#include -#include - -#include - -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(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(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; - } -} diff --git a/bsp_linux/comIF/ArduinoComIF.h b/bsp_linux/comIF/ArduinoComIF.h deleted file mode 100644 index 9ab166de..00000000 --- a/bsp_linux/comIF/ArduinoComIF.h +++ /dev/null @@ -1,73 +0,0 @@ -//#ifndef MISSION_ARDUINOCOMMINTERFACE_H_ -//#define MISSION_ARDUINOCOMMINTERFACE_H_ -// -//#include -//#include -//#include -//#include -//#include -//#include -// -//#include "../../framework/objectmanager/SystemObject.h" -//#include "ArduinoCookie.h" -// -////Forward declaration, so users don't peek -//class ArduinoCookie; -// -//class ArduinoComIF: public SystemObject, -// public DeviceCommunicationIF { -//public: -// static const uint8_t MAX_NUMBER_OF_SPI_DEVICES = 8; -// static const uint8_t MAX_PACKET_SIZE = 64; -// -// static const uint8_t COMMAND_INVALID = -1; -// static const uint8_t COMMAND_SPI = 1; -// -// ArduinoComIF(object_id_t setObjectId, const char *serialDevice); -// virtual ~ArduinoComIF(); -// -// virtual ReturnValue_t open(Cookie **cookie, uint32_t address, -// uint32_t maxReplyLen); -// -// virtual ReturnValue_t reOpen(Cookie *cookie, uint32_t address, -// uint32_t maxReplyLen); -// -// virtual void close(Cookie *cookie); -// -// //SHOULDDO can data be const? -// virtual ReturnValue_t sendMessage(Cookie *cookie, uint8_t *data, -// uint32_t len); -// -// virtual ReturnValue_t getSendSuccess(Cookie *cookie); -// -// virtual ReturnValue_t requestReceiveMessage(Cookie *cookie); -// -// virtual ReturnValue_t readReceivedMessage(Cookie *cookie, uint8_t **buffer, -// uint32_t *size); -// -// virtual ReturnValue_t setAddress(Cookie *cookie, uint32_t address); -// -// virtual uint32_t getAddress(Cookie *cookie); -// -// virtual ReturnValue_t setParameter(Cookie *cookie, uint32_t parameter); -// -// virtual uint32_t getParameter(Cookie *cookie); -//private: -// //remembering if the initialization in the ctor worked -// //if not, all calls are disabled -// bool initialized; -// int serialPort; -// //used to know where to put the data if a reply is received -// FixedMap spiMap; -// -// SimpleRingBuffer rxBuffer; -// -// ReturnValue_t sendMessage(uint8_t command, uint8_t address, -// const uint8_t *data, size_t dataLen); -// -// void handleSerialPortRx(); -// -// void handlePacket(uint8_t *packet, size_t packetLen); -//}; -// -//#endif /* MISSION_ARDUINOCOMMINTERFACE_H_ */ diff --git a/bsp_linux/comIF/ArduinoCookie.cpp b/bsp_linux/comIF/ArduinoCookie.cpp deleted file mode 100644 index d7e81192..00000000 --- a/bsp_linux/comIF/ArduinoCookie.cpp +++ /dev/null @@ -1,12 +0,0 @@ -//#include -// -//ArduinoCookie::ArduinoCookie(Protocol_t protocol, uint8_t address, -// size_t maxReplySize) : -// command(protocol), address(address), receivedDataLen(0), maxReplySize( -// maxReplySize) { -// replyBuffer = new uint8_t[maxReplySize]; -//} -// -//ArduinoCookie::~ArduinoCookie() { -// delete[] replyBuffer; -//} diff --git a/bsp_linux/comIF/ArduinoCookie.h b/bsp_linux/comIF/ArduinoCookie.h deleted file mode 100644 index 64eed4ad..00000000 --- a/bsp_linux/comIF/ArduinoCookie.h +++ /dev/null @@ -1,25 +0,0 @@ -//#ifndef MISSION_ARDUINO_ARDUINOCOOKIE_H_ -//#define MISSION_ARDUINO_ARDUINOCOOKIE_H_ -// -//#include -// -//#include -//#include -// -//class ArduinoCookie: public Cookie { -//public: -// enum Protocol_t { -// INVALID = 0, SPI = 1 -// }; -// ArduinoCookie(Protocol_t protocol, uint8_t address, size_t maxReplySize); -// virtual ~ArduinoCookie(); -// -// uint8_t command; -// uint8_t address; -// uint8_t *replyBuffer; -// size_t receivedDataLen; -// size_t maxReplySize; -// -//}; -// -//#endif /* MISSION_ARDUINO_ARDUINOCOOKIE_H_ */