1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00

move WiFiESP::loop() to core 1

This commit is contained in:
Harald Barth 2022-08-16 12:25:49 +02:00
parent 162e1f9d3e
commit b9c1e779ae
7 changed files with 37 additions and 33 deletions

View File

@ -47,7 +47,6 @@
#ifdef CD_HANDLE_RING
// wifi or ethernet ring streams with multiple client types
RingStream * CommandDistributor::ring=0;
// byte CommandDistributor::ringClient=NO_CLIENT;
CommandDistributor::clientType CommandDistributor::clients[8]={
NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE};
@ -57,7 +56,6 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream
if (Diag::WIFI && Diag::CMD)
DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer);
ring=stream;
// ringClient=stream->peekTargetMark();
// First check if the client is not known
// yet and in that case determinine type
@ -78,7 +76,6 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream
else if (clients[clientId] == WITHROTTLE_TYPE)
WiThrottle::getThrottle(clientId)->parse(ring, buffer);
// ringClient=NO_CLIENT;
}
void CommandDistributor::forget(byte clientId) {
@ -100,24 +97,24 @@ void CommandDistributor::broadcastToClients(clientType type) {
// before merging broadcasts in the ring, then reinstate it in case
// the process continues to output to its client.
if (ring) {
if ((rememberClient = ring->peekTargetMark()) != NO_CLIENT) {
DIAG(F("CD precommit client %d"), rememberClient);
ring->commit();
}
/* loop through ring clients */
for (byte clientId=0; clientId<sizeof(clients); clientId++) {
if (clients[clientId]==type) {
DIAG(F("CD mark client %d"), clientId);
ring->mark(clientId);
ring->print(broadcastBufferWriter->getString());
DIAG(F("CD commit client %d"), clientId);
if ((rememberClient = ring->peekTargetMark()) != RingStream::NO_CLIENT) {
//DIAG(F("CD precommit client %d"), rememberClient);
ring->commit();
}
}
if (ring->peekTargetMark()!=NO_CLIENT) {
DIAG(F("CD postmark client %d"), rememberClient);
ring->mark(rememberClient);
}
/* loop through ring clients */
for (byte clientId=0; clientId<sizeof(clients); clientId++) {
if (clients[clientId]==type) {
//DIAG(F("CD mark client %d"), clientId);
ring->mark(clientId);
ring->print(broadcastBufferWriter->getString());
//DIAG(F("CD commit client %d"), clientId);
ring->commit();
}
}
if (ring->peekTargetMark()!=RingStream::NO_CLIENT) {
//DIAG(F("CD postmark client %d"), rememberClient);
ring->mark(rememberClient);
}
}
#endif
}

View File

@ -47,8 +47,6 @@ private:
static StringBuffer * broadcastBufferWriter;
#ifdef CD_HANDLE_RING
static RingStream * ring;
static const byte NO_CLIENT=255; //XXX remove later
// static byte ringClient;
static clientType clients[8];
#endif
};

View File

@ -143,6 +143,10 @@ void loop()
#if WIFI_ON
WifiInterface::loop();
#endif //WIFI_ON
#else //ARDUINO_ARCH_ESP32
#ifndef WIFI_TASK_ON_CORE0
WifiESP::loop();
#endif
#endif //ARDUINO_ARCH_ESP32
#if ETHERNET_ON
EthernetInterface::loop();

View File

@ -143,7 +143,7 @@ int RingStream::freeSpace() {
// mark start of message with client id (0...9)
void RingStream::mark(uint8_t b) {
DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID());
//DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID());
_ringClient = b;
_mark=_pos_write;
write(b); // client id
@ -165,14 +165,14 @@ void RingStream::info() {
bool RingStream::commit() {
_flashInsert=NULL; // prepared for first read
if (_overflow) {
DIAG(F("RingStream(%d) commit(%d) OVERFLOW"),_len, _count);
//DIAG(F("RingStream(%d) commit(%d) OVERFLOW"),_len, _count);
// just throw it away
_pos_write=_mark;
_overflow=false;
return false; // commit failed
}
if (_count==0) {
DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID());
//DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID());
// ignore empty response
_pos_write=_mark;
_ringClient = NO_CLIENT; //XXX make else clause later
@ -188,7 +188,7 @@ bool RingStream::commit() {
{ char s[_count+2];
strncpy(s, (const char*)&(_buffer[_mark+1]), _count);
s[_count]=0;
DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s);
//DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s);
}
_ringClient = NO_CLIENT;
return true; // commit worked

View File

@ -52,6 +52,7 @@ class RingStream : public Print {
if ((_pos_read==_pos_write) && !_overflow) return -1; // empty
return _buffer[_pos_read];
};
static const byte NO_CLIENT=255;
private:
int _len;
int _pos_write;
@ -61,7 +62,6 @@ class RingStream : public Print {
int _count;
byte * _buffer;
char * _flashInsert;
static const byte NO_CLIENT=255; // must be same as in CommandDistributor
byte _ringClient = NO_CLIENT;
};

View File

@ -611,12 +611,12 @@ byte WiThrottle::stashClient;
char WiThrottle::stashThrottleChar;
void WiThrottle::getLocoCallback(int16_t locoid) {
DIAG(F("LocoCallback mark client %d"), stashClient);
//DIAG(F("LocoCallback mark client %d"), stashClient);
stashStream->mark(stashClient);
if (locoid<=0) {
StringFormatter::send(stashStream,F("HMNo loco found on prog track\n"));
DIAG(F("LocoCallback commit (noloco)"));
//DIAG(F("LocoCallback commit (noloco)"));
stashStream->commit(); // done here, commit and return
return;
}
@ -627,7 +627,7 @@ void WiThrottle::getLocoCallback(int16_t locoid) {
locoid = locoid ^ LONG_ADDR_MARKER; // remove marker bit to get real long addr
if (locoid <= HIGHEST_SHORT_ADDR ) { // out of range for long addr
StringFormatter::send(stashStream,F("HMLong addr %d <= %d unsupported\n"), locoid, HIGHEST_SHORT_ADDR);
DIAG(F("LocoCallback commit (error)"));
//DIAG(F("LocoCallback commit (error)"));
stashStream->commit(); // done here, commit and return
return;
}
@ -641,7 +641,7 @@ void WiThrottle::getLocoCallback(int16_t locoid) {
stashInstance->multithrottle(stashStream, (byte *)addcmd);
TrackManager::setMainPower(POWERMODE::ON);
TrackManager::setJoin(true); // <1 JOIN> so we can drive loco away
DIAG(F("LocoCallback commit success"));
//DIAG(F("LocoCallback commit success"));
stashStream->commit();
CommandDistributor::broadcastPower();

View File

@ -95,14 +95,15 @@ public:
static std::vector<NetworkClient> clients; // a list to hold all clients
static WiFiServer *server = NULL;
static RingStream *outboundRing = new RingStream(2048);
//static RingStream *eventRing = new RingStream(2048);
static bool APmode = false;
#ifdef WIFI_TASK_ON_CORE0
void wifiLoop(void *){
for(;;){
WifiESP::loop();
}
}
#endif
bool WifiESP::setup(const char *SSid,
const char *password,
@ -196,6 +197,7 @@ bool WifiESP::setup(const char *SSid,
server->begin();
// server started here
#ifdef WIFI_TASK_ON_CORE0
//start loop task
if (pdPASS != xTaskCreatePinnedToCore(
wifiLoop, /* Task function. */
@ -211,7 +213,10 @@ bool WifiESP::setup(const char *SSid,
// report server started after wifiLoop creation
// when everything looks good
DIAG(F("Server up port %d"),port);
DIAG(F("Server starting (core 0) port %d"),port);
#else
DIAG(F("Server will be started on port %d"),port);
#endif
return true;
}
@ -261,7 +266,7 @@ void WifiESP::loop() {
cmd[len]=0;
outboundRing->mark(clientId);
CommandDistributor::parse(clientId,cmd,outboundRing);
if (outboundRing->peekTargetMark()!=255) //XXX fix 255 later
if (outboundRing->peekTargetMark()!=RingStream::NO_CLIENT)
outboundRing->commit();
}
}