mirror of
https://github.com/meshtastic/firmware.git
synced 2025-02-26 22:33:24 +00:00
refactoring part 1: remove MeshPacketClient/Server layer
This commit is contained in:
parent
dd638499b6
commit
25d8be327d
@ -106,8 +106,8 @@ AudioThread *audioThread;
|
||||
|
||||
#if HAS_TFT
|
||||
#include "DeviceScreen.h"
|
||||
#include "sharedMem/MeshPacketClient.h"
|
||||
#include "sharedMem/MeshPacketServer.h"
|
||||
#include "sharedMem/PacketClient.h"
|
||||
#include "sharedMem/PacketServer.h"
|
||||
|
||||
void tft_task_handler(void *);
|
||||
|
||||
@ -674,9 +674,9 @@ void setup()
|
||||
#endif
|
||||
|
||||
#if HAS_TFT
|
||||
MeshPacketServer::init();
|
||||
PacketServer::init();
|
||||
deviceScreen = &DeviceScreen::create();
|
||||
deviceScreen->init(new MeshPacketClient);
|
||||
deviceScreen->init(new PacketClient);
|
||||
#endif
|
||||
|
||||
// Initialize the screen first so we can show the logo while we start up everything else.
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include "api/PacketAPI.h"
|
||||
#include "MeshService.h"
|
||||
#include "RadioInterface.h"
|
||||
#include "sharedMem/MeshPacketServer.h"
|
||||
|
||||
PacketAPI *packetAPI = nullptr;
|
||||
|
||||
@ -23,18 +22,11 @@ bool PacketAPI::receivePacket(void)
|
||||
isConnected = true;
|
||||
data_received = true;
|
||||
|
||||
// TODO: think about redesign or drop class MeshPacketServer
|
||||
meshtastic_ToRadio *mr;
|
||||
// if (typeid(*server) == typeid(MeshPacketServer)) {
|
||||
// dynamic_cast<MeshPacketServer*>(server)->receivePacket(*mr);
|
||||
// }
|
||||
// else {
|
||||
auto p = server->receivePacket()->move();
|
||||
int id = p->getPacketId();
|
||||
LOG_DEBUG("Received packet id=%u\n", id);
|
||||
// mr = (meshtastic_ToRadio*)&dynamic_cast<DataPacket<meshtastic_ToRadio>*>(p.get())->getData();
|
||||
mr = (meshtastic_ToRadio *)&static_cast<DataPacket<meshtastic_ToRadio> *>(p.get())->getData();
|
||||
//}
|
||||
|
||||
switch (mr->which_payload_variant) {
|
||||
case meshtastic_ToRadio_packet_tag: {
|
||||
|
@ -1,40 +0,0 @@
|
||||
#include "MeshPacketClient.h"
|
||||
|
||||
void MeshPacketClient::init(void)
|
||||
{
|
||||
PacketClient::init();
|
||||
}
|
||||
|
||||
MeshPacketClient::MeshPacketClient() {}
|
||||
|
||||
bool MeshPacketClient::connect(void)
|
||||
{
|
||||
return PacketClient::connect();
|
||||
}
|
||||
|
||||
bool MeshPacketClient::disconnect(void)
|
||||
{
|
||||
return PacketClient::disconnect();
|
||||
}
|
||||
|
||||
bool MeshPacketClient::isConnected(void)
|
||||
{
|
||||
return PacketClient::isConnected();
|
||||
}
|
||||
|
||||
bool MeshPacketClient::send(meshtastic_ToRadio &&to)
|
||||
{
|
||||
static uint32_t id = 0;
|
||||
return PacketClient::sendPacket(DataPacket<meshtastic_ToRadio>(++id, to));
|
||||
}
|
||||
|
||||
meshtastic_FromRadio MeshPacketClient::receive(void)
|
||||
{
|
||||
if (hasData()) {
|
||||
auto p = receivePacket();
|
||||
if (p) {
|
||||
return static_cast<DataPacket<meshtastic_FromRadio> *>(p->move().get())->getData();
|
||||
}
|
||||
}
|
||||
return meshtastic_FromRadio();
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "mesh-pb-constants.h"
|
||||
#include "sharedMem/PacketClient.h"
|
||||
|
||||
/**
|
||||
* @brief This is a wrapper class for the PaketClient to avoid dealing with DataPackets
|
||||
* in the application code.
|
||||
*
|
||||
*/
|
||||
class MeshPacketClient : public PacketClient
|
||||
{
|
||||
public:
|
||||
MeshPacketClient();
|
||||
virtual void init(void);
|
||||
virtual bool connect(void);
|
||||
virtual bool disconnect(void);
|
||||
virtual bool isConnected(void);
|
||||
|
||||
virtual bool send(meshtastic_ToRadio &&to);
|
||||
virtual meshtastic_FromRadio receive(void);
|
||||
};
|
@ -1,44 +0,0 @@
|
||||
#include "sharedMem/MeshPacketServer.h"
|
||||
#include "api/PacketAPI.h"
|
||||
#include "sharedMem/SharedQueue.h"
|
||||
|
||||
SharedQueue *sharedQueue = nullptr;
|
||||
|
||||
MeshPacketServer *meshPacketServer = nullptr;
|
||||
|
||||
void MeshPacketServer::init(void)
|
||||
{
|
||||
meshPacketServer = new MeshPacketServer;
|
||||
packetAPI = new PacketAPI(meshPacketServer);
|
||||
meshPacketServer->begin();
|
||||
}
|
||||
|
||||
MeshPacketServer::MeshPacketServer() {}
|
||||
|
||||
void MeshPacketServer::begin(void)
|
||||
{
|
||||
sharedQueue = new SharedQueue;
|
||||
PacketServer::begin(sharedQueue);
|
||||
}
|
||||
|
||||
bool MeshPacketServer::receivePacket(meshtastic_ToRadio &to)
|
||||
{
|
||||
// auto p = PacketServer::receivePacket<Packet::PacketPtr>()->move();
|
||||
auto p = PacketServer::receivePacket()->move();
|
||||
if (p) {
|
||||
// TODO: avoid data copy :(
|
||||
// to = dynamic_cast<DataPacket<meshtastic_ToRadio>*>(p.get())->getData();
|
||||
to = static_cast<DataPacket<meshtastic_ToRadio> *>(p.get())->getData();
|
||||
}
|
||||
return p != nullptr;
|
||||
}
|
||||
|
||||
bool MeshPacketServer::sendPacket(meshtastic_FromRadio &from)
|
||||
{
|
||||
return PacketServer::sendPacket(DataPacket<meshtastic_FromRadio>(from.id, from));
|
||||
}
|
||||
|
||||
bool MeshPacketServer::sendPacket(meshtastic_FromRadio &&from)
|
||||
{
|
||||
return PacketServer::sendPacket(DataPacket<meshtastic_FromRadio>(from.id, from));
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "mesh-pb-constants.h"
|
||||
#include "sharedMem/PacketServer.h"
|
||||
|
||||
/**
|
||||
* @brief This is a wrapper class for the PaketServer to avoid dealing with DataPackets
|
||||
* in the application code.
|
||||
*
|
||||
*/
|
||||
class MeshPacketServer : public PacketServer
|
||||
{
|
||||
public:
|
||||
MeshPacketServer();
|
||||
static void init(void);
|
||||
virtual void begin(void);
|
||||
virtual bool receivePacket(meshtastic_ToRadio &to);
|
||||
virtual bool sendPacket(meshtastic_FromRadio &from);
|
||||
virtual bool sendPacket(meshtastic_FromRadio &&from);
|
||||
};
|
||||
|
||||
extern MeshPacketServer *meshPacketServer;
|
@ -41,21 +41,25 @@ int PacketClient::connect(SharedQueue *_queue)
|
||||
return queue->serverQueueSize();
|
||||
}
|
||||
|
||||
Packet::PacketPtr PacketClient::receivePacket()
|
||||
bool PacketClient::send(meshtastic_ToRadio &&to)
|
||||
{
|
||||
assert(queue);
|
||||
if (queue->serverQueueSize() == 0)
|
||||
return {nullptr};
|
||||
return queue->clientReceive();
|
||||
if (available()) {
|
||||
static uint32_t id = 0;
|
||||
return queue->clientSend(DataPacket<meshtastic_ToRadio>(++id, to));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool PacketClient::sendPacket(Packet &&p)
|
||||
meshtastic_FromRadio PacketClient::receive(void)
|
||||
{
|
||||
assert(queue);
|
||||
if (queue->clientQueueSize() >= max_packet_queue_size)
|
||||
return false;
|
||||
queue->clientSend(std::move(p));
|
||||
return true;
|
||||
if (hasData()) {
|
||||
auto p = queue->clientReceive();
|
||||
if (p) {
|
||||
return static_cast<DataPacket<meshtastic_FromRadio> *>(p->move().get())->getData();
|
||||
}
|
||||
}
|
||||
return meshtastic_FromRadio();
|
||||
}
|
||||
|
||||
bool PacketClient::hasData() const
|
||||
|
@ -6,7 +6,7 @@
|
||||
class SharedQueue;
|
||||
|
||||
/**
|
||||
* @brief Generic client implementation to receive from and
|
||||
* @brief Client implementation to receive packets from and
|
||||
* send packets to the shared queue
|
||||
*
|
||||
*/
|
||||
@ -14,13 +14,12 @@ class PacketClient : public IClientBase
|
||||
{
|
||||
public:
|
||||
PacketClient();
|
||||
virtual void init(void);
|
||||
virtual bool connect(void);
|
||||
virtual bool disconnect(void);
|
||||
virtual bool isConnected(void);
|
||||
|
||||
virtual bool sendPacket(Packet &&p);
|
||||
virtual Packet::PacketPtr receivePacket();
|
||||
void init(void) override;
|
||||
bool connect(void) override;
|
||||
bool disconnect(void) override;
|
||||
bool isConnected(void) override;
|
||||
bool send(meshtastic_ToRadio &&to) override;
|
||||
meshtastic_FromRadio receive(void) override;
|
||||
|
||||
virtual bool hasData() const;
|
||||
virtual bool available() const;
|
||||
|
@ -1,11 +1,24 @@
|
||||
#include "sharedMem/PacketServer.h"
|
||||
#include "api/PacketAPI.h"
|
||||
#include "sharedMem/SharedQueue.h"
|
||||
#include <assert.h>
|
||||
|
||||
const uint32_t max_packet_queue_size = 50;
|
||||
|
||||
SharedQueue *sharedQueue = nullptr;
|
||||
|
||||
PacketServer *packetServer = nullptr;
|
||||
|
||||
PacketServer::PacketServer() : queue(nullptr) {}
|
||||
|
||||
void PacketServer::init(void)
|
||||
{
|
||||
packetServer = new PacketServer;
|
||||
packetAPI = new PacketAPI(packetServer);
|
||||
sharedQueue = new SharedQueue;
|
||||
packetServer->begin(sharedQueue);
|
||||
}
|
||||
|
||||
void PacketServer::begin(SharedQueue *_queue)
|
||||
{
|
||||
queue = _queue;
|
||||
|
@ -7,12 +7,13 @@ class SharedQueue;
|
||||
|
||||
/**
|
||||
* Generic server implementation (base class) for bidirectional task communication
|
||||
* Uses a queue that is shared with the
|
||||
* Uses a queue that is shared with the client
|
||||
*/
|
||||
class PacketServer
|
||||
{
|
||||
public:
|
||||
PacketServer();
|
||||
static void init(void);
|
||||
virtual void begin(SharedQueue *_queue);
|
||||
virtual bool sendPacket(Packet &&p);
|
||||
virtual Packet::PacketPtr receivePacket(void);
|
||||
|
Loading…
Reference in New Issue
Block a user