parts of p60dock handler
This commit is contained in:
@ -1,324 +1,324 @@
|
||||
#include "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;
|
||||
}
|
||||
}
|
||||
//#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;
|
||||
// }
|
||||
//}
|
||||
|
120
bsp_linux/comIF/P60DockComIF.cpp
Normal file
120
bsp_linux/comIF/P60DockComIF.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
#include "P60DockComIF.h"
|
||||
|
||||
#include <csp/csp.h>
|
||||
#include <csp/drivers/can_socketcan.h>
|
||||
#include <bsp_linux/comIF/cookies/P60DockCookie.h>
|
||||
#include <fsfw/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include <gomspace/libparam_client/include/gs/param/rparam.h>
|
||||
|
||||
P60DockComIF::P60DockComIF(object_id_t objectId) :
|
||||
SystemObject(objectId) {
|
||||
|
||||
}
|
||||
|
||||
P60DockComIF::~P60DockComIF() {
|
||||
}
|
||||
|
||||
ReturnValue_t P60DockComIF::initializeInterface(CookieIF *cookie) {
|
||||
if(cookie == nullptr) {
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
P60DockCookie* p60DockCookie = dynamic_cast<P60DockCookie*>(cookie);
|
||||
uint8_t cspAddress = p60DockCookie->getCspAddress();
|
||||
char* canInterface = p60DockCookie->getCanIf();
|
||||
int bitrate = p60DockCookie->getBitrate();
|
||||
|
||||
int buf_count = 10;
|
||||
int buf_size = 300;
|
||||
/* Init CSP and CSP buffer system */
|
||||
if (csp_init(cspAddress) != CSP_ERR_NONE
|
||||
|| csp_buffer_init(buf_count, buf_size) != CSP_ERR_NONE) {
|
||||
sif::error << "Failed to init CSP\r\n" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
int promisc = 0; // Set filter mode on
|
||||
csp_iface_t *csp_if_ptr = &csp_if;
|
||||
csp_if_ptr = csp_can_socketcan_init(canInterface, bitrate, promisc);
|
||||
|
||||
/* Set default route and start router */
|
||||
uint8_t address = CSP_DEFAULT_ROUTE;
|
||||
uint8_t netmask = 0;
|
||||
uint8_t mac = CSP_NODE_MAC;
|
||||
int result = csp_rtable_set(address, netmask, csp_if_ptr, mac);
|
||||
if(result != CSP_ERR_NONE){
|
||||
sif::error << "Failed to add can interface to router table"
|
||||
<< std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
/* Start the route task */
|
||||
unsigned int task_stack_size = 512;
|
||||
unsigned int priority = 0;
|
||||
result = csp_route_start_task(task_stack_size, priority);
|
||||
if(result != CSP_ERR_NONE){
|
||||
sif::error << "Failed to start csp route task" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t P60DockComIF::sendMessage(CookieIF *cookie,
|
||||
const uint8_t * sendData, size_t sendLen) {
|
||||
P60DockCookie* p60DockCookie = dynamic_cast<P60DockCookie*> (cookie);
|
||||
MessageType_t messageType = p60DockCookie->getMessageType();
|
||||
|
||||
switch(messageType){
|
||||
case(P60DockCookie::REBOOT):{
|
||||
csp_reboot(p60DockCookie->getCspAddress());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t P60DockComIF::getSendSuccess(CookieIF *cookie) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t P60DockComIF::requestReceiveMessage(CookieIF *cookie,
|
||||
size_t requestLen) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t P60DockComIF::readReceivedMessage(CookieIF *cookie,
|
||||
uint8_t** buffer, size_t* size) {
|
||||
P60DockCookie* p60DockCookie = dynamic_cast<P60DockCookie*> (cookie);
|
||||
MessageType_t messageType = p60DockCookie->getMessageType();
|
||||
|
||||
switch(messageType){
|
||||
case(P60DockCookie::READ_MODULE_CONFIG):{
|
||||
uint32_t timeout = 1000;
|
||||
uint8_t p60dockAddress = p60DockCookie->getCspAddress();
|
||||
gs_param_table_instance_t moduleConfig;
|
||||
moduleConfig.rows = moduleConfigTable;
|
||||
moduleConfig.id = p60dockAddress;
|
||||
moduleConfig.row_count = p60dock_hk_count;
|
||||
moduleConfig.memory_size = P60DOCK_HK_SIZE;
|
||||
moduleConfig.memory = *buffer;
|
||||
/* Read complete module configuration table from P60 Dock and store data
|
||||
* in buffer */
|
||||
int result = gs_rparam_get_full_table(&moduleConfig, p60dockAddress,
|
||||
node_hk.id, GS_RPARAM_MAGIC_CHECKSUM, timeout);
|
||||
*size = P60DOCK_HK_SIZE;
|
||||
if (result != GS_OK) {
|
||||
sif::info
|
||||
<< "Failed retrieving module configuration from P60 dock with error code "
|
||||
<< result << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
52
bsp_linux/comIF/P60DockComIF.h
Normal file
52
bsp_linux/comIF/P60DockComIF.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* P60DockComIF.h
|
||||
*
|
||||
* Created on: 01.12.2020
|
||||
* Author: jakob
|
||||
*/
|
||||
|
||||
#ifndef BSP_LINUX_COMIF_P60DOCKCOMIF_H_
|
||||
#define BSP_LINUX_COMIF_P60DOCKCOMIF_H_
|
||||
|
||||
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
|
||||
#include <fsfw/objectmanager/SystemObject.h>
|
||||
#include <fsfw/returnvalues/HasReturnvaluesIF.h>
|
||||
|
||||
#include <gomspace/libcsp/include/csp/csp_types.h>
|
||||
#include <gomspace/libcsp/include/csp/csp_error.h>
|
||||
#include <gomspace/libparam_client/include/gs/param/types.h>
|
||||
#include <gs/param/internal/types.h>
|
||||
|
||||
/**
|
||||
* @brief This is the communication interface to the cubesat space protocol
|
||||
* stack. The physical layer used for this implementation is CAN.
|
||||
* @author Jakob Meier
|
||||
*/
|
||||
class P60DockComIF: public DeviceCommunicationIF, public SystemObject {
|
||||
public:
|
||||
P60DockComIF(object_id_t objectId);
|
||||
virtual ~P60DockComIF();
|
||||
|
||||
ReturnValue_t initializeInterface(CookieIF * cookie) override;
|
||||
ReturnValue_t sendMessage(CookieIF *cookie, const uint8_t * sendData,
|
||||
size_t sendLen) override;
|
||||
ReturnValue_t getSendSuccess(CookieIF *cookie) override;
|
||||
ReturnValue_t requestReceiveMessage(CookieIF *cookie,
|
||||
size_t requestLen) override;
|
||||
ReturnValue_t readReceivedMessage(CookieIF *cookie,
|
||||
uint8_t **readData, size_t *readLen) override;
|
||||
|
||||
private:
|
||||
/* Interface struct for csp protocol stack */
|
||||
csp_iface_t csp_if;
|
||||
/* Table definitions. According to gomspace software documentation there
|
||||
* exist four tables each identified by a number*/
|
||||
uint8_t boardConfigTable = 0;
|
||||
uint8_t modulConfigTable = 1;
|
||||
uint8_t calibrationParamTable = 2;
|
||||
uint8_t tmDataTable = 4;
|
||||
unsigned int moduleConfigTableRows = 32;
|
||||
|
||||
};
|
||||
|
||||
#endif /* BSP_LINUX_COMIF_P60DOCKCOMIF_H_ */
|
38
bsp_linux/comIF/cookies/P60DockCookie.cpp
Normal file
38
bsp_linux/comIF/cookies/P60DockCookie.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "bsp_linux/comIF/cookies/P60DockCookie.h"
|
||||
|
||||
|
||||
P60DockCookie::P60DockCookie(char* canInterface_, uint8_t cspAddress_) :
|
||||
canInterface(canInterface_), cspAddress(cspAddress_) {
|
||||
|
||||
}
|
||||
|
||||
P60DockCookie::~P60DockCookie() {
|
||||
}
|
||||
|
||||
uint8_t P60DockCookie::getCspAddress(){
|
||||
return cspAddress;
|
||||
}
|
||||
|
||||
char* P60DockCookie::getCanIf(){
|
||||
return canInterface;
|
||||
}
|
||||
|
||||
int P60DockCookie::getBitrate(){
|
||||
return bitrate;
|
||||
}
|
||||
|
||||
void P60DockCookie::setPingMessage(){
|
||||
nextMessage = PING;
|
||||
}
|
||||
|
||||
void P60DockCookie::setRebootMessage(){
|
||||
nextMessage = REBOOT;
|
||||
}
|
||||
|
||||
void P60DockCookie::setReadModuleCfgMessage(){
|
||||
nextMessage = READ_MODULE_CONFIG;
|
||||
}
|
||||
|
||||
MessageType_t P60DockCookie::getMessageType(){
|
||||
return nextMessage;
|
||||
}
|
49
bsp_linux/comIF/cookies/P60DockCookie.h
Normal file
49
bsp_linux/comIF/cookies/P60DockCookie.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef BSP_LINUX_COMIF_COOKIES_P60DockCookie_H_
|
||||
#define BSP_LINUX_COMIF_COOKIES_P60DockCookie_H_
|
||||
|
||||
#include <fsfw/devicehandlers/CookieIF.h>
|
||||
|
||||
typedef uint32_t MessageType_t;
|
||||
|
||||
/**
|
||||
* @brief This is the cookie for the communication interface to the cubesat
|
||||
* space protocol (CSP) implementation of gomspace. The communication
|
||||
* interface uses CAN as the physical layer. Therefore the cookie also
|
||||
* holds the CAN instance to use.
|
||||
* @author Jakob Meier
|
||||
*/
|
||||
class P60DockCookie: public CookieIF {
|
||||
public:
|
||||
/**
|
||||
* Constructor for the CSP cookie
|
||||
* @param canInterface_ The CAN interface to use. E.g. "can0" or "can1".
|
||||
* @param cspAddress_ The CSP address of the target device.
|
||||
*/
|
||||
P60DockCookie(char* canInterface_, uint8_t cspAddress_);
|
||||
virtual ~P60DockCookie();
|
||||
|
||||
uint8_t getCspAddress();
|
||||
char* getCanIf();
|
||||
int getBitrate();
|
||||
void setPingMessage();
|
||||
void setRebootMessage();
|
||||
void setReadModuleCfgMessage();
|
||||
MessageType_t getMessageType();
|
||||
|
||||
/* Message type defines the type of the next data transfer between the
|
||||
* CSP device and the OBC. */
|
||||
static const MessageType_t MESSAGE_NONE = 0x0;
|
||||
static const MessageType_t PING = 0x1;
|
||||
static const MessageType_t REBOOT = 0x4;
|
||||
static const MessageType_t READ_MODULE_CONFIG = 0x71;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
char* canInterface;
|
||||
uint8_t cspAddress;
|
||||
int bitrate = 1000;
|
||||
MessageType_t nextMessage = MESSAGE_NONE;
|
||||
};
|
||||
|
||||
#endif /* BSP_LINUX_COMIF_COOKIES_P60DockCookie_H_ */
|
Reference in New Issue
Block a user