2021-07-12 21:21:03 +02:00
|
|
|
#include "TmTcLwIpUdpBridge.h"
|
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
#include <OBSWConfig.h>
|
2021-07-12 21:21:03 +02:00
|
|
|
#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 "ethernetif.h"
|
|
|
|
#include "udp_config.h"
|
2021-07-12 21:21:03 +02:00
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
TmTcLwIpUdpBridge::TmTcLwIpUdpBridge(object_id_t objectId, object_id_t ccsdsPacketDistributor,
|
|
|
|
object_id_t tmStoreId, object_id_t tcStoreId)
|
|
|
|
: TmTcBridge(objectId, ccsdsPacketDistributor, tmStoreId, tcStoreId) {
|
|
|
|
TmTcLwIpUdpBridge::lastAdd.addr = IPADDR_TYPE_ANY;
|
2021-07-12 21:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TmTcLwIpUdpBridge::~TmTcLwIpUdpBridge() {}
|
|
|
|
|
|
|
|
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(void) {
|
2022-05-05 20:55:28 +02:00
|
|
|
err_t err;
|
|
|
|
/* Create a new UDP control block */
|
|
|
|
TmTcLwIpUdpBridge::upcb = udp_new();
|
|
|
|
if (TmTcLwIpUdpBridge::upcb) {
|
|
|
|
/* 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 */
|
|
|
|
udp_recv(TmTcLwIpUdpBridge::upcb, &udp_server_receive_callback, (void*)this);
|
|
|
|
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 TCPIP_RECV_WIRETAPPING == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
if (connectFlag) {
|
|
|
|
uint32_t ipAddress = ((ip4_addr*)&lastAdd)->addr;
|
|
|
|
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-05-05 20:55:28 +02:00
|
|
|
sif::info << "TmTcLwIpUdpBridge: Client IP Address " << std::dec << ipAddress4 << "."
|
|
|
|
<< ipAddress3 << "." << ipAddress2 << "." << ipAddress1 << std::endl;
|
|
|
|
uint16_t portSwapped = EndianConverter::convertBigEndian(lastPort);
|
|
|
|
sif::info << "TmTcLwIpUdpBridge: Client IP Port " << (int)portSwapped << std::endl;
|
2021-07-12 21:21:03 +02:00
|
|
|
#else
|
2022-05-05 20:55:28 +02:00
|
|
|
sif::printInfo("TmTcLwIpUdpBridge: Client IP Address %d.%d.%d.%d\n", ipAddress4, ipAddress3,
|
|
|
|
ipAddress2, ipAddress1);
|
|
|
|
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-05 20:55:28 +02:00
|
|
|
ReturnValue_t TmTcLwIpUdpBridge::sendTm(const uint8_t* data, size_t dataLen) {
|
|
|
|
struct pbuf* p_tx = pbuf_alloc(PBUF_TRANSPORT, dataLen, PBUF_RAM);
|
|
|
|
if ((p_tx != nullptr) && (lastAdd.addr != IPADDR_TYPE_ANY) && (upcb != nullptr)) {
|
|
|
|
/* copy data to pbuf */
|
|
|
|
err_t err = pbuf_take(p_tx, (char*)data, dataLen);
|
|
|
|
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-05-05 20:55:28 +02:00
|
|
|
void TmTcLwIpUdpBridge::udp_server_receive_callback(void* arg, struct udp_pcb* upcb_,
|
|
|
|
struct pbuf* p, const ip_addr_t* addr,
|
|
|
|
u16_t port) {
|
|
|
|
struct pbuf* p_tx = nullptr;
|
|
|
|
auto udpBridge = reinterpret_cast<TmTcLwIpUdpBridge*>(arg);
|
|
|
|
if (udpBridge == nullptr) {
|
2021-07-12 21:21:03 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
sif::warning << "TmTcLwIpUdpBridge::udp_server_receive_callback: Invalid UDP bridge!"
|
|
|
|
<< std::endl;
|
2021-07-12 21:21:03 +02:00
|
|
|
#else
|
2022-05-05 20:55:28 +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*/
|
|
|
|
p_tx = pbuf_alloc(PBUF_TRANSPORT, p->len, PBUF_RAM);
|
|
|
|
|
|
|
|
if (p_tx != NULL) {
|
|
|
|
if (udpBridge != nullptr) {
|
|
|
|
MutexGuard lg(udpBridge->bridgeLock);
|
|
|
|
udpBridge->upcb = upcb_;
|
|
|
|
udpBridge->lastAdd = *addr;
|
|
|
|
udpBridge->lastPort = port;
|
|
|
|
if (not udpBridge->comLinkUp()) {
|
|
|
|
udpBridge->registerCommConnect();
|
2021-07-12 21:21:03 +02:00
|
|
|
#if TCPIP_RECV_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pbuf_take(p_tx, (char*)p->payload, p->len);
|
|
|
|
/* send the received data to the uart port */
|
|
|
|
char* data = reinterpret_cast<char*>(p_tx->payload);
|
|
|
|
*(data + p_tx->len) = '\0';
|
2021-07-12 21:21:03 +02:00
|
|
|
|
|
|
|
#if TCPIP_RECV_WIRETAPPING == 1
|
2022-05-05 20:55:28 +02:00
|
|
|
udpBridge->printData(p, data);
|
2021-07-12 21:21:03 +02:00
|
|
|
#endif
|
|
|
|
|
2022-05-05 20:55:28 +02:00
|
|
|
store_address_t storeId;
|
|
|
|
ReturnValue_t returnValue =
|
|
|
|
udpBridge->tcStore->addData(&storeId, reinterpret_cast<uint8_t*>(p->payload), p->len);
|
|
|
|
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) {
|
|
|
|
/* Physical connection does not mean there is a recipient to send packets too.
|
|
|
|
This will be done by the receive callback! */
|
|
|
|
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
|
|
|
}
|