Original project changes: Improvements of the TCP client CAN logger fix by using a semaphore instead of a wait loop.

This commit is contained in:
Carsten Schmiemann 2022-10-03 12:25:13 +02:00
parent 70575cd193
commit de5abbbace
2 changed files with 13 additions and 21 deletions

View file

@ -34,7 +34,6 @@ static const char *TAG = "canlog-tcpclient";
#include "canformat.h"
#include "canlog_tcpclient.h"
#include "ovms_config.h"
#include "ovms_peripherals.h"
canlog_tcpclient* MyCanLogTcpClient = NULL;
@ -165,14 +164,7 @@ bool canlog_tcpclient::Open()
if (mg_connect_opt(mgr, m_path.c_str(), tcMongooseHandler, opts) != NULL)
{
// Wait 10s max for connection establishment...
m_connecting = true;
int timeout = 10000000;
const int step = 10000; // check every 10 ms
while (m_connecting && timeout > 0)
{
timeout -= step;
usleep(step);
}
m_connecting.Take(pdMS_TO_TICKS(10*1000));
return m_isopen;
}
else
@ -229,7 +221,6 @@ void canlog_tcpclient::MongooseHandler(struct mg_connection *nc, int ev, void *p
{
int *success = (int*)p;
ESP_LOGV(TAG, "MongooseHandler(MG_EV_CONNECT=%d)",*success);
m_connecting = false;
if (*success == 0)
{ // Connection successful
OvmsRecMutexLock lock(&m_cmmutex);
@ -250,6 +241,7 @@ void canlog_tcpclient::MongooseHandler(struct mg_connection *nc, int ev, void *p
ESP_LOGE(TAG, "Connection failed to %s", m_path.c_str());
m_isopen = false;
}
m_connecting.Give();
}
break;
case MG_EV_CLOSE:
@ -259,7 +251,6 @@ void canlog_tcpclient::MongooseHandler(struct mg_connection *nc, int ev, void *p
OvmsRecMutexLock lock(&m_cmmutex);
ESP_LOGE(TAG, "Disconnected from %s", m_path.c_str());
m_isopen = false;
m_connecting = false;
auto k = m_connmap.find(nc);
if (k != m_connmap.end())
{

View file

@ -31,6 +31,7 @@
#include "canlog.h"
#include "ovms_netmanager.h"
#include "ovms_mutex.h"
#include "ovms_semaphore.h"
class canlog_tcpclient : public canlog
{
@ -48,7 +49,7 @@ class canlog_tcpclient : public canlog
public:
std::string m_path;
bool m_connecting;
OvmsSemaphore m_connecting;
};
#endif // __CANLOG_TCP_CLIENT_H__