eive-obsw/linux/csp/CspComIF.cpp

399 lines
14 KiB
C++
Raw Normal View History

2020-12-14 08:42:48 +01:00
#include "CspComIF.h"
2020-12-29 13:59:31 +01:00
2020-12-14 08:42:48 +01:00
#include <csp/drivers/can_socketcan.h>
#include <fsfw/serialize/SerializeAdapter.h>
2022-01-17 15:58:27 +01:00
#include <fsfw/serviceinterface/ServiceInterfaceStream.h>
2022-08-26 03:20:44 +02:00
#include <mission/devices/devicedefinitions/GomspaceDefinitions.h>
#include <p60acu.h>
#include <p60dock.h>
2022-08-26 14:28:06 +02:00
#include <p60pdu.h>
#include <param/param_string.h>
#include <param/rparam_client.h>
2020-12-14 08:42:48 +01:00
#include "mission/csp/CspCookie.h"
using namespace GOMSPACE;
2020-12-14 08:42:48 +01:00
2023-03-24 11:33:43 +01:00
CspComIF::CspComIF(object_id_t objectId, const char* routeTaskName, uint32_t routerRealTimePriority)
: SystemObject(objectId),
routerRealTimePriority(routerRealTimePriority),
routerTaskName(routeTaskName) {}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
CspComIF::~CspComIF() {}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
ReturnValue_t CspComIF::initializeInterface(CookieIF* cookie) {
if (cookie == nullptr) {
return NULLPOINTER;
}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
CspCookie* cspCookie = dynamic_cast<CspCookie*>(cookie);
if (cspCookie == nullptr) {
return NULLPOINTER;
}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
/* Perform CAN and CSP initialization only once */
if (cspDeviceMap.empty()) {
sif::info << "Performing " << canInterface << " initialization.." << std::endl;
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
/* Define the memory to allocate for the CSP stack */
int buf_count = 10;
int buf_size = 300;
/* Init CSP and CSP buffer system */
if (csp_init(cspOwnAddress) != CSP_ERR_NONE ||
csp_buffer_init(buf_count, buf_size) != CSP_ERR_NONE) {
sif::error << "Failed to init CSP\r\n" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
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;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
/* Start the route task */
2023-03-24 11:33:43 +01:00
result = startRouterTask();
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::error << "Failed to start csp route task" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
sif::info << canInterface << " initialized successfully" << std::endl;
}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
uint8_t cspAddress = cspCookie->getCspAddress();
uint16_t maxReplyLength = cspCookie->getMaxReplyLength();
if (cspDeviceMap.find(cspAddress) == cspDeviceMap.end()) {
/* Insert device information in CSP map */
2022-08-27 01:02:08 +02:00
cspDeviceMap.emplace(cspAddress, ReplyInfo(maxReplyLength));
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2020-12-14 08:42:48 +01:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t CspComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
int result;
2022-08-26 03:20:44 +02:00
if (cookie == nullptr) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
CspCookie* cspCookie = dynamic_cast<CspCookie*>(cookie);
2022-08-26 03:20:44 +02:00
if (cspCookie == nullptr) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
uint8_t cspPort;
uint16_t querySize = 0;
2022-08-26 14:28:06 +02:00
if (cspCookie->getRequest() == GOMSPACE::SpecialRequestTypes::DEFAULT_COM_IF) {
2022-08-26 03:20:44 +02:00
/* Extract csp port and bytes to query from command buffer */
result = getPortAndQuerySize(&sendData, &sendLen, &cspPort, &querySize);
if (result != returnvalue::OK) {
2022-08-26 03:20:44 +02:00
return result;
}
} else {
cspPort = cspCookie->getCspPort();
querySize = cspCookie->getReplyLen();
2022-01-17 15:58:27 +01:00
}
2022-08-26 14:28:06 +02:00
if (querySize > cspCookie->getMaxReplyLength()) {
sif::error << "Query size " << querySize << " is larger than maximum allowed "
<< cspCookie->getMaxReplyLength() << std::endl;
return returnvalue::FAILED;
}
2022-01-17 15:58:27 +01:00
uint8_t cspAddress = cspCookie->getCspAddress();
2022-08-27 01:02:08 +02:00
auto iter = cspDeviceMap.find(cspAddress);
if (iter == cspDeviceMap.end()) {
return returnvalue::FAILED;
}
2022-01-17 15:58:27 +01:00
switch (cspPort) {
case (CspPorts::CSP_PING): {
2022-01-17 15:58:27 +01:00
initiatePingRequest(cspAddress, querySize);
break;
}
case (CspPorts::CSP_REBOOT): {
2022-01-17 15:58:27 +01:00
csp_reboot(cspAddress);
break;
}
case (CspPorts::P60_PORT_GNDWDT_RESET_ENUM):
case (CspPorts::P60_PORT_RPARAM_ENUM): {
2022-08-26 14:28:06 +02:00
if (cspCookie->getRequest() != SpecialRequestTypes::DEFAULT_COM_IF) {
2022-08-26 03:20:44 +02:00
param_index_t requestStruct{};
2022-08-27 01:02:08 +02:00
requestStruct.physaddr = iter->second.replyBuf.data();
2022-08-26 14:28:06 +02:00
auto req = cspCookie->getRequest();
if (req == GOMSPACE::SpecialRequestTypes::GET_PDU_HK) {
if (!p60pdu_get_hk(&requestStruct, cspAddress, cspCookie->getTimeout())) {
return returnvalue::FAILED;
}
2022-08-26 14:28:06 +02:00
} else if (req == GOMSPACE::SpecialRequestTypes::GET_ACU_HK) {
if (!p60acu_get_hk(&requestStruct, cspAddress, cspCookie->getTimeout())) {
return returnvalue::FAILED;
}
2022-08-26 14:28:06 +02:00
} else if (req == GOMSPACE::SpecialRequestTypes::GET_P60DOCK_HK) {
if (!p60dock_get_hk(&requestStruct, cspAddress, cspCookie->getTimeout())) {
return returnvalue::FAILED;
}
2022-08-26 14:28:06 +02:00
} else if (req == GOMSPACE::SpecialRequestTypes::GET_PDU_CONFIG) {
requestStruct.table = p60pdu_config;
requestStruct.mem_id = P60PDU_PARAM;
requestStruct.count = p60pdu_config_count;
requestStruct.size = P60PDU_PARAM_SIZE;
2023-02-17 12:19:53 +01:00
result = rparam_get_full_table(&requestStruct, cspAddress, P60_PORT_RPARAM,
requestStruct.mem_id, cspCookie->getTimeout());
2022-08-27 01:02:08 +02:00
if (result != 0) {
return returnvalue::FAILED;
}
} else if (req == GOMSPACE::SpecialRequestTypes::GET_ACU_CONFIG) {
requestStruct.table = p60acu_config;
requestStruct.mem_id = P60ACU_PARAM;
requestStruct.count = p60acu_config_count;
requestStruct.size = P60ACU_PARAM_SIZE;
2023-02-17 12:19:53 +01:00
result = rparam_get_full_table(&requestStruct, cspAddress, P60_PORT_RPARAM,
requestStruct.mem_id, cspCookie->getTimeout());
2022-08-27 01:02:08 +02:00
if (result != 0) {
return returnvalue::FAILED;
}
} else if (req == GOMSPACE::SpecialRequestTypes::GET_P60DOCK_CONFIG) {
requestStruct.table = p60dock_config;
requestStruct.mem_id = P60DOCK_PARAM;
requestStruct.count = p60dock_config_count;
requestStruct.size = P60DOCK_PARAM_SIZE;
2023-02-17 12:19:53 +01:00
result = rparam_get_full_table(&requestStruct, cspAddress, P60_PORT_RPARAM,
requestStruct.mem_id, cspCookie->getTimeout());
2022-08-27 01:02:08 +02:00
if (result != 0) {
return returnvalue::FAILED;
}
2022-09-14 16:47:58 +02:00
} else if (req == GOMSPACE::SpecialRequestTypes::SAVE_TABLE) {
if (sendLen < 2) {
2022-08-31 22:52:32 +02:00
return returnvalue::FAILED;
}
const TableInfo* tableInfo = reinterpret_cast<const TableInfo*>(sendData);
2023-02-17 12:19:53 +01:00
result = gs_rparam_save(cspAddress, cspCookie->getTimeout(), tableInfo->sourceTable,
tableInfo->targetTable);
2022-08-31 22:52:32 +02:00
if (result != 0) {
return returnvalue::FAILED;
}
2022-09-14 16:47:58 +02:00
} else if (req == GOMSPACE::SpecialRequestTypes::LOAD_TABLE) {
if (sendLen < 2) {
2022-09-01 17:31:43 +02:00
return returnvalue::FAILED;
}
const TableInfo* tableInfo = reinterpret_cast<const TableInfo*>(sendData);
2023-02-17 12:19:53 +01:00
result = gs_rparam_load(cspAddress, cspCookie->getTimeout(), tableInfo->sourceTable,
tableInfo->targetTable);
2022-09-01 17:31:43 +02:00
if (result != 0) {
return returnvalue::FAILED;
}
2022-08-26 03:20:44 +02:00
}
} else {
/* No CSP fixed port was selected. Send data to the specified port and
* wait for querySize number of bytes */
result = cspTransfer(cspAddress, cspPort, sendData, sendLen, querySize);
if (result != returnvalue::OK) {
return returnvalue::FAILED;
2022-08-26 03:20:44 +02:00
}
2022-01-17 15:58:27 +01:00
}
2022-08-27 01:02:08 +02:00
iter->second.replyLen = querySize;
2022-01-17 15:58:27 +01:00
break;
}
default:
sif::error << "CspComIF: Invalid port specified" << std::endl;
break;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
ReturnValue_t CspComIF::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; }
2022-01-17 15:58:27 +01:00
ReturnValue_t CspComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t CspComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
if (cookie == NULL) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
CspCookie* cspCookie = dynamic_cast<CspCookie*>(cookie);
if (cspCookie == NULL) {
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-01-17 15:58:27 +01:00
uint8_t cspAddress = cspCookie->getCspAddress();
2022-08-27 01:02:08 +02:00
auto iter = cspDeviceMap.find(cspAddress);
if (iter == cspDeviceMap.end()) {
return returnvalue::FAILED;
}
*buffer = iter->second.replyBuf.data();
*size = iter->second.replyLen;
2022-01-17 15:58:27 +01:00
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
ReturnValue_t CspComIF::cspTransfer(uint8_t cspAddress, uint8_t cspPort, const uint8_t* cmdBuffer,
int cmdLen, uint16_t querySize) {
uint32_t timeout_ms = 1000;
uint16_t bytesRead = 0;
2022-04-11 17:16:25 +02:00
int32_t expectedSize = static_cast<int32_t>(querySize);
2022-08-27 01:02:08 +02:00
auto iter = cspDeviceMap.find(cspAddress);
2022-01-17 15:58:27 +01:00
if (iter == cspDeviceMap.end()) {
sif::error << "CSP device with address " << cspAddress << " no found in"
<< " device map" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-27 01:02:08 +02:00
uint8_t* replyBuffer = iter->second.replyBuf.data();
2022-01-17 15:58:27 +01:00
csp_conn_t* conn = csp_connect(CSP_PRIO_HIGH, cspAddress, cspPort, 0, CSP_O_NONE);
csp_packet_t* commandPacket = (csp_packet_t*)csp_buffer_get(cmdLen);
if (commandPacket == NULL) {
sif::error << "CspComIF::cspTransfer: Failed to get memory for a csp packet from the csp "
<< "stack" << std::endl;
csp_close(conn);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
memcpy(commandPacket->data, cmdBuffer, cmdLen);
commandPacket->length = cmdLen;
if (!csp_send(conn, commandPacket, timeout_ms)) {
csp_buffer_free(commandPacket);
sif::error << "CspComIF::cspTransfer: Failed to send csp packet" << std::endl;
csp_close(conn);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
/* Return when no reply is expected */
if (expectedSize == 0) {
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
csp_packet_t* reply;
reply = csp_read(conn, timeout_ms);
if (reply == NULL) {
sif::error << "CspComIF::cspTransfer: Failed to read csp packet" << std::endl;
csp_close(conn);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
memcpy(replyBuffer, reply->data, reply->length);
expectedSize = expectedSize - reply->length;
bytesRead += reply->length;
csp_buffer_free(reply);
while (expectedSize > 0) {
reply = csp_read(conn, timeout_ms);
if (reply == NULL) {
2022-01-17 15:58:27 +01:00
sif::error << "CspComIF::cspTransfer: Failed to read csp packet" << std::endl;
csp_close(conn);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-27 01:02:08 +02:00
if ((reply->length + bytesRead) > iter->second.replyBuf.size()) {
2022-01-17 15:58:27 +01:00
sif::error << "CspComIF::cspTransfer: Reply buffer to short" << std::endl;
csp_buffer_free(reply);
csp_close(conn);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
}
2022-01-17 15:58:27 +01:00
memcpy(replyBuffer + bytesRead, reply->data, reply->length);
expectedSize = expectedSize - reply->length;
bytesRead += reply->length;
csp_buffer_free(reply);
2022-01-17 15:58:27 +01:00
}
2022-01-17 15:58:27 +01:00
if (expectedSize != 0) {
sif::error << "CspComIF::cspTransfer: Received more bytes than requested" << std::endl;
sif::debug << "CspComIF::cspTransfer: Received bytes: " << bytesRead << std::endl;
csp_close(conn);
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2020-12-14 08:42:48 +01:00
2022-01-17 15:58:27 +01:00
csp_close(conn);
2020-12-14 08:42:48 +01:00
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2020-12-14 08:42:48 +01:00
}
2020-12-20 13:31:44 +01:00
2022-01-17 15:58:27 +01:00
ReturnValue_t CspComIF::getPortAndQuerySize(const uint8_t** sendData, size_t* sendLen,
uint8_t* cspPort, uint16_t* querySize) {
ReturnValue_t result =
SerializeAdapter::deSerialize(cspPort, sendData, sendLen, SerializeIF::Endianness::BIG);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::error << "CspComIF: Failed to deserialize CSP port from command "
<< "buffer" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
SerializeAdapter::deSerialize(querySize, sendData, sendLen, SerializeIF::Endianness::BIG);
2022-08-24 17:27:47 +02:00
if (result != returnvalue::OK) {
2022-01-17 15:58:27 +01:00
sif::error << "CspComIF: Failed to deserialize querySize from command "
<< "buffer" << std::endl;
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2022-01-17 15:58:27 +01:00
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2020-12-20 13:31:44 +01:00
}
2022-01-17 15:58:27 +01:00
void CspComIF::initiatePingRequest(uint8_t cspAddress, uint16_t querySize) {
uint32_t timeout_ms = 500;
uint32_t replyTime = csp_ping(cspAddress, timeout_ms, querySize, CSP_O_NONE);
sif::info << "Ping address: " << cspAddress << ", reply after " << replyTime << " ms"
<< std::endl;
2022-08-27 01:02:08 +02:00
auto iter = cspDeviceMap.find(cspAddress);
if (iter == cspDeviceMap.end()) {
return;
}
2022-01-17 15:58:27 +01:00
/* Store reply time in reply buffer * */
2022-08-27 01:02:08 +02:00
uint8_t* replyBuffer = iter->second.replyBuf.data();
2022-01-17 15:58:27 +01:00
memcpy(replyBuffer, &replyTime, sizeof(replyTime));
2022-08-27 01:02:08 +02:00
iter->second.replyLen = sizeof(replyTime);
2020-12-20 13:31:44 +01:00
}
2023-03-24 11:33:43 +01:00
ReturnValue_t CspComIF::startRouterTask() {
pthread_attr_t attr;
int res = pthread_attr_init(&attr);
if (res) {
return returnvalue::FAILED;
}
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2023-03-24 14:53:07 +01:00
res = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (res != 0) {
return returnvalue::FAILED;
}
2023-03-24 11:33:43 +01:00
// Set scheduling policy to SCHED_RR
res = pthread_attr_setschedpolicy(&attr, SCHED_RR);
if (res) {
pthread_attr_destroy(&attr);
return returnvalue::FAILED;
}
struct sched_param sched_param;
sched_param.sched_priority = routerRealTimePriority;
res = pthread_attr_setschedparam(&attr, &sched_param);
if (res) {
pthread_attr_destroy(&attr);
return returnvalue::FAILED;
}
2023-03-24 14:53:07 +01:00
res = pthread_create(&routerTaskHandle, &attr, routerWorkWrapper, NULL);
2023-03-24 11:33:43 +01:00
if (res) {
pthread_attr_destroy(&attr);
return returnvalue::FAILED;
}
2023-03-24 14:53:07 +01:00
res = pthread_setname_np(routerTaskHandle, routerTaskName);
2023-03-24 11:33:43 +01:00
if (res) {
2023-03-24 14:53:07 +01:00
pthread_attr_destroy(&attr);
2023-03-24 11:33:43 +01:00
return returnvalue::FAILED;
}
2023-03-24 14:53:07 +01:00
pthread_attr_destroy(&attr);
2023-03-24 11:33:43 +01:00
return returnvalue::OK;
}
void* CspComIF::routerWorkWrapper(void* args) {
/* Here there be routing */
while (1) {
csp_route_work(FIFO_TIMEOUT);
}
return nullptr;
}