fsfw-example-common/stm32h7/networking/TmTcLwIpUdpBridge.cpp

190 lines
6.4 KiB
C++
Raw Normal View History

2021-07-12 21:21:03 +02:00
#include "TmTcLwIpUdpBridge.h"
#include <fsfw/ipc/MutexGuard.h>
#include <fsfw/serialize/EndianConverter.h>
2022-05-05 20:55:28 +02:00
#include <fsfw/serviceinterface/ServiceInterface.h>
2021-07-12 21:21:03 +02:00
2022-05-05 20:55:28 +02:00
#include "app_ethernet.h"
#include "udp_config.h"
2021-07-12 21:21:03 +02:00
2022-08-08 12:32:06 +02:00
TmTcLwIpUdpBridge::TmTcLwIpUdpBridge(object_id_t objectId, object_id_t ccsdsPacketDistributor,
object_id_t tmStoreId, object_id_t tcStoreId)
2022-05-05 20:55:28 +02:00
: TmTcBridge(objectId, ccsdsPacketDistributor, tmStoreId, tcStoreId) {
TmTcLwIpUdpBridge::lastAdd.addr = IPADDR_TYPE_ANY;
2021-07-12 21:21:03 +02:00
}
TmTcLwIpUdpBridge::~TmTcLwIpUdpBridge() = default;
2021-07-12 21:21:03 +02:00
ReturnValue_t TmTcLwIpUdpBridge::initialize() {
2022-05-05 20:55:28 +02:00
TmTcBridge::initialize();
bridgeLock = MutexFactory::instance()->createMutex();
if (bridgeLock == nullptr) {
return ObjectManagerIF::CHILD_INIT_FAILED;
}
ReturnValue_t result = udp_server_init();
return result;
2021-07-12 21:21:03 +02:00
}
ReturnValue_t TmTcLwIpUdpBridge::udp_server_init() {
2022-05-05 20:55:28 +02:00
err_t err;
/* Create a new UDP control block */
TmTcLwIpUdpBridge::upcb = udp_new();
if (TmTcLwIpUdpBridge::upcb) {
sif::printInfo("Opening UDP server on port %d\n", UDP_SERVER_PORT);
2022-05-05 20:55:28 +02:00
/* Bind the upcb to the UDP_PORT port */
/* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
err = udp_bind(TmTcLwIpUdpBridge::upcb, IP_ADDR_ANY, UDP_SERVER_PORT);
if (err == ERR_OK) {
/* Set a receive callback for the upcb */
2022-08-08 12:32:06 +02:00
udp_recv(TmTcLwIpUdpBridge::upcb, &udp_server_receive_callback, (void *)this);
2022-05-05 20:55:28 +02:00
return RETURN_OK;
2021-07-12 21:21:03 +02:00
} else {
2022-05-05 20:55:28 +02:00
udp_remove(TmTcLwIpUdpBridge::upcb);
return RETURN_FAILED;
2021-07-12 21:21:03 +02:00
}
2022-05-05 20:55:28 +02:00
} else {
return RETURN_FAILED;
}
2021-07-12 21:21:03 +02:00
}
ReturnValue_t TmTcLwIpUdpBridge::performOperation(uint8_t operationCode) {
2022-05-05 20:55:28 +02:00
TmTcBridge::performOperation();
2021-07-12 21:21:03 +02:00
#if OBSW_TCPIP_UDP_WIRETAPPING == 1
2022-05-05 20:55:28 +02:00
if (connectFlag) {
2022-05-22 15:30:08 +02:00
uint32_t ipAddress = ((ip4_addr *)&lastAdd)->addr;
2022-05-05 20:55:28 +02:00
int ipAddress1 = (ipAddress & 0xFF000000) >> 24;
int ipAddress2 = (ipAddress & 0xFF0000) >> 16;
int ipAddress3 = (ipAddress & 0xFF00) >> 8;
int ipAddress4 = ipAddress & 0xFF;
2021-07-12 21:21:03 +02:00
#if OBSW_VERBOSE_LEVEL == 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-08-08 12:32:06 +02:00
sif::info << "TmTcLwIpUdpBridge: Client IP Address " << std::dec << ipAddress4 << "."
<< ipAddress3 << "." << ipAddress2 << "." << ipAddress1 << std::endl;
2022-05-05 20:55:28 +02:00
uint16_t portSwapped = EndianConverter::convertBigEndian(lastPort);
2022-08-08 12:32:06 +02:00
sif::info << "TmTcLwIpUdpBridge: Client IP Port " << (int)portSwapped << std::endl;
2021-07-12 21:21:03 +02:00
#else
2022-08-08 12:32:06 +02:00
sif::printInfo("TmTcLwIpUdpBridge: Client IP Address %d.%d.%d.%d\n", ipAddress4, ipAddress3,
ipAddress2, ipAddress1);
2022-05-05 20:55:28 +02:00
uint16_t portSwapped = EndianConverter::convertBigEndian(lastPort);
sif::printInfo("TmTcLwIpUdpBridge: Client IP Port: %d\n", portSwapped);
2021-07-12 21:21:03 +02:00
#endif
#endif
2022-05-05 20:55:28 +02:00
connectFlag = false;
}
2021-07-12 21:21:03 +02:00
#endif
2022-05-05 20:55:28 +02:00
return RETURN_OK;
2021-07-12 21:21:03 +02:00
}
2022-05-22 15:30:08 +02:00
ReturnValue_t TmTcLwIpUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
struct pbuf *p_tx = pbuf_alloc(PBUF_TRANSPORT, dataLen, PBUF_RAM);
2022-08-08 12:32:06 +02:00
if ((p_tx != nullptr) && (lastAdd.addr != IPADDR_TYPE_ANY) && (upcb != nullptr)) {
2022-05-05 20:55:28 +02:00
/* copy data to pbuf */
err_t err = pbuf_take(p_tx, (const char *)data, dataLen);
2022-05-05 20:55:28 +02:00
if (err != ERR_OK) {
pbuf_free(p_tx);
return err;
2021-07-12 21:21:03 +02:00
}
2022-05-05 20:55:28 +02:00
/* Connect to the remote client */
err = udp_connect(TmTcLwIpUdpBridge::upcb, &lastAdd, lastPort);
if (err != ERR_OK) {
pbuf_free(p_tx);
return err;
}
/* Tell the client that we have accepted it */
err = udp_send(TmTcLwIpUdpBridge::upcb, p_tx);
pbuf_free(p_tx);
if (err != ERR_OK) {
return err;
2021-07-12 21:21:03 +02:00
}
2022-05-05 20:55:28 +02:00
/* free the UDP connection, so we can accept new clients */
udp_disconnect(TmTcLwIpUdpBridge::upcb);
} else {
return RETURN_FAILED;
}
return RETURN_OK;
}
2021-07-12 21:21:03 +02:00
2022-08-08 12:32:06 +02:00
void TmTcLwIpUdpBridge::udp_server_receive_callback(void *arg, struct udp_pcb *upcb_,
struct pbuf *p, const ip_addr_t *addr,
2022-05-05 20:55:28 +02:00
u16_t port) {
2022-05-22 15:30:08 +02:00
auto udpBridge = reinterpret_cast<TmTcLwIpUdpBridge *>(arg);
2022-05-05 20:55:28 +02:00
if (udpBridge == nullptr) {
2021-07-12 21:21:03 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-08-08 12:32:06 +02:00
sif::warning << "TmTcLwIpUdpBridge::udp_server_receive_callback: Invalid UDP bridge!"
<< std::endl;
2021-07-12 21:21:03 +02:00
#else
2022-08-08 12:32:06 +02:00
sif::printWarning(
"TmTcLwIpUdpBridge::udp_server_receive_callback: Invalid "
"UDP bridge!\n");
2021-07-12 21:21:03 +02:00
#endif
2022-05-05 20:55:28 +02:00
}
/* allocate pbuf from RAM*/
struct pbuf *p_tx = pbuf_alloc(PBUF_TRANSPORT, p->len, PBUF_RAM);
2022-05-05 20:55:28 +02:00
if (p_tx != nullptr) {
2022-05-05 20:55:28 +02:00
if (udpBridge != nullptr) {
MutexGuard lg(udpBridge->bridgeLock);
udpBridge->upcb = upcb_;
udpBridge->lastAdd = *addr;
udpBridge->lastPort = port;
if (not udpBridge->comLinkUp()) {
udpBridge->registerCommConnect();
#if OBSW_TCPIP_UDP_WIRETAPPING == 1
2022-05-05 20:55:28 +02:00
udpBridge->connectFlag = true;
2021-07-12 21:21:03 +02:00
#endif
2022-05-05 20:55:28 +02:00
/* This should have already been done, but we will still do it */
udpBridge->physicalConnectStatusChange(true);
}
}
2022-05-22 15:30:08 +02:00
pbuf_take(p_tx, (char *)p->payload, p->len);
2022-05-05 20:55:28 +02:00
/* send the received data to the uart port */
2022-05-22 15:30:08 +02:00
char *data = reinterpret_cast<char *>(p_tx->payload);
2022-05-05 20:55:28 +02:00
*(data + p_tx->len) = '\0';
2021-07-12 21:21:03 +02:00
#if OBSW_TCPIP_UDP_WIRETAPPING == 1
udpBridge->printData(reinterpret_cast<uint8_t *>(p->payload), p->len);
2021-07-12 21:21:03 +02:00
#endif
2022-05-05 20:55:28 +02:00
store_address_t storeId;
2022-08-08 12:32:06 +02:00
ReturnValue_t returnValue =
udpBridge->tcStore->addData(&storeId, reinterpret_cast<uint8_t *>(p->payload), p->len);
2022-05-05 20:55:28 +02:00
if (returnValue != RETURN_OK) {
2021-07-12 21:21:03 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +02:00
sif::warning << "UDP Server: Data storage failed" << std::endl;
2021-07-12 21:21:03 +02:00
#endif
2022-05-05 20:55:28 +02:00
pbuf_free(p_tx);
return;
}
TmTcMessage message(storeId);
if (udpBridge->tmTcReceptionQueue->sendToDefault(&message) != RETURN_OK) {
2021-07-12 21:21:03 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +02:00
sif::warning << "TmTcLwIpUdpBridgw::udp_server_receive_callback:"
<< " Sending message to queue failed" << std::endl;
2021-07-12 21:21:03 +02:00
#endif
2022-05-05 20:55:28 +02:00
udpBridge->tcStore->deleteData(storeId);
2021-07-12 21:21:03 +02:00
}
2022-05-05 20:55:28 +02:00
}
/* Free the p_tx buffer */
pbuf_free(p_tx);
2021-07-12 21:21:03 +02:00
}
/* Caller must ensure thread-safety */
2022-05-05 20:55:28 +02:00
bool TmTcLwIpUdpBridge::comLinkUp() const { return communicationLinkUp; }
2021-07-12 21:21:03 +02:00
/* Caller must ensure thread-safety */
void TmTcLwIpUdpBridge::physicalConnectStatusChange(bool connect) {
2022-05-05 20:55:28 +02:00
if (connect) {
2022-05-22 15:30:08 +02:00
/* Physical connection does not mean there is a recipient to send packets
too. This will be done by the receive callback! */
2022-05-05 20:55:28 +02:00
physicalConnection = true;
} else {
physicalConnection = false;
/* If there is no physical connection, we can't send anything back */
registerCommDisconnect();
}
2021-07-12 21:21:03 +02:00
}