8000 Fixes for #331 · James-sjec/esp32-snippets@307868b · GitHub
[go: up one dir, main page]

Skip to content

Commit 307868b

Browse files
committed
Fixes for nkolban#331
1 parent ac27ff1 commit 307868b

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

cpp_utils/FreeRTOS.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,45 +168,70 @@ void FreeRTOS::Semaphore::giveFromISR() {
168168
/**
169169
* @brief Take a semaphore.
170170
* Take a semaphore and wait indefinitely.
171+
* @param [in] owner The new owner (for debugging)
172+
* @return True if we took the semaphore.
171173
*/
172-
void FreeRTOS::Semaphore::take(std::string owner)
174+
bool FreeRTOS::Semaphore::take(std::string owner)
173175
{
174176
ESP_LOGD(LOG_TAG, "Semaphore taking: %s for %s", toString().c_str(), owner.c_str());
177+
bool rc = false;
175178
if (m_usePthreads) {
176179
pthread_mutex_lock(&m_pthread_mutex);
177180
} else {
178-
xSemaphoreTake(m_semaphore, portMAX_DELAY);
181+
rc = ::xSemaphoreTake(m_semaphore, portMAX_DELAY);
179182
}
180183
m_owner = owner;
181-
ESP_LOGD(LOG_TAG, "Semaphore taken: %s", toString().c_str());
184+
if (rc) {
185+
ESP_LOGD(LOG_TAG, "Semaphore taken: %s", toString().c_str());
186+
} else {
187+
ESP_LOGE(LOG_TAG, "Semaphore NOT taken: %s", toString().c_str());
188+
}
189+
return rc;
182190
} // Semaphore::take
183191

184192

185193
/**
186194
* @brief Take a semaphore.
187195
* Take a semaphore but return if we haven't obtained it in the given period of milliseconds.
188196
* @param [in] timeoutMs Timeout in milliseconds.
197+
* @param [in] owner The new owner (for debugging)
198+
* @return True if we took the semaphore.
189199
*/
190-
void FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
200+
bool FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
191201

192202
ESP_LOGV(LOG_TAG, "Semaphore taking: %s for %s", toString().c_str(), owner.c_str());
193-
203+
bool rc = false;
194204
if (m_usePthreads) {
195-
assert(false);
205+
assert(false); // We apparently don't have a timed wait for pthreads.
196206
} else {
197-
xSemaphoreTake(m_semaphore, timeoutMs/portTICK_PERIOD_MS);
207+
rc = ::xSemaphoreTake(m_semaphore, timeoutMs/portTICK_PERIOD_MS);
198208
}
199209
m_owner = owner;
200-
ESP_LOGV(LOG_TAG, "Semaphore taken: %s", toString().c_str());
210+
if (rc) {
211+
ESP_LOGV(LOG_TAG, "Semaphore taken: %s", toString().c_str());
212+
} else {
213+
ESP_LOGE(LOG_TAG, "Semaphore NOT taken: %s", toString().c_str());
214+
}
215+
return rc;
201216
} // Semaphore::take
202217

218+
219+
220+
/**
221+
* @brief Create a string representation of the semaphore.
222+
* @return A string representation of the semaphore.
223+
*/
203224
std::string FreeRTOS::Semaphore::toString() {
204225
std::stringstream stringStream;
205226
stringStream << "name: "<< m_name << " (0x" << std::hex << std::setfill('0') << (uint32_t)m_semaphore << "), owner: " << m_owner;
206227
return stringStream.str();
207228
} // toString
208229

209230

231+
/**
232+
* @brief Set the name of the semaphore.
233+
* @param [in] name The name of the semaphore.
234+
*/
210235
void FreeRTOS::Semaphore::setName(std::string name) {
211236
m_name = name;
212237
} // setName

cpp_utils/FreeRTOS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class FreeRTOS {
3636
void give(uint32_t value);
3737
void giveFromISR();
3838
void setName(std::string name);
39-
void take(std::string owner="<Unknown>");
40-
void take(uint32_t timeoutMs, std::string owner="<Unknown>");
39+
bool take(std::string owner="<Unknown>");
40+
bool take(uint32_t timeoutMs, std::string owner="<Unknown>");
4141
std::string toString();
4242
uint32_t wait(std::string owner="<Unknown>");
4343

cpp_utils/HttpServer.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ class HttpServerTask: public Task {
169169
}
170170
catch(std::exception &e) {
171171
ESP_LOGE("HttpServerTask", "Caught an exception waiting for new client!");
172+
m_pHttpServer->m_semaphoreServerStarted.give(); // Release the semaphore .. we are now no longer running.
172173
return;
173174
}
174175

175-
ESP_LOGD("HttpServerTask", "HttpServer listening on port %d has received a new client connection; sockFd=%d", m_pHttpServer->getPort(), clientSocket.getFD());
176+
ESP_LOGD("HttpServerTask", "HttpServer that was listening on port %d has received a new client connection; sockFd=%d", m_pHttpServer->getPort(), clientSocket.getFD());
176177

177178
HttpRequest request(clientSocket); // Build the HTTP Request from the socket.
178179
if (request.isWebsocket()) { // If this is a WebSocket
@@ -414,11 +415,20 @@ void HttpServer::start(uint16_t portNumber, bool useSSL) {
414415
// Design:
415416
// The start of the HTTP server should be as fast as possible.
416417
ESP_LOGD(LOG_TAG, ">> start: port: %d, useSSL: %d", portNumber, useSSL);
418+
419+
// Take the semaphore that says that we are now running. If we are already running, then end here as
420+
// there is nothing further to do.
421+
if (m_semaphoreServerStarted.take(100, "start") == false) {
422+
ESP_LOGD(LOG_TAG, "<< start: Already running");
423+
return;
424+
}
425+
417426
m_useSSL = useSSL;
418427
m_portNumber = portNumber;
419428

420429
HttpServerTask* pHttpServerTask = new HttpServerTask("HttpServerTask");
421430
pHttpServerTask->start(this);
431+
ESP_LOGD(LOG_TAG, "<< start");
422432
} // start
423433

424434

@@ -430,7 +440,8 @@ void HttpServer::stop() {
430440
// that is listening for incoming connections. That will then shutdown all the other
431441
// activities.
432442
ESP_LOGD(LOG_TAG, ">> stop");
433-
m_socket.close();
443+
m_socket.close(); // Close the socket that is being used to watch for incoming requests.
444+
m_semaphoreServerStarted.wait("stop"); // Wait for the server to stop.
434445
ESP_LOGD(LOG_TAG, "<< stop");
435446
} // stop
436447

cpp_utils/HttpServer.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "SockServ.h"
1717
#include "HttpRequest.h"
1818
#include "HttpResponse.h"
19-
//#include "SockServ.h"
19+
#include "FreeRTOS.h"
2020
#include <regex>
2121

2222
class HttpServerTask;
@@ -74,15 +74,15 @@ class HttpServer {
7474
HttpRequest* pHttpRequest,
7575
HttpResponse* pHttpResponse)
7676
);
77+
uint32_t getClientTimeout(); // Get client's socket timeout
7778
size_t getFileBufferSize(); // Get the current size of the file buffer.
7879
uint16_t getPort(); // Get the port on which the Http server is listening.
7980
std::string getRootPath(); // Get the root of the file system path.
8081
bool getSSL(); // Are we using SSL?
82+
void setClientTimeout(uint32_t timeout); // Set client's socket timeout
8183
void setDirectoryListing(bool use); // Should we list the content of directories?
8284
void setFileBufferSize(size_t fileBufferSize); // Set the size of the file buffer
8385
void setRootPath(std::string path); // Set the root of the file system path.
84-
void setClientTimeout(uint32_t timeout); // Set client's socket timeout
85-
uint32_t getClientTimeout(); // Get client's socket timeout
8686
void start(uint16_t portNumber, bool useSSL=false);
8787
void stop(); // Stop a previously started server.
8888

@@ -97,7 +97,8 @@ class HttpServer {
9797
std::string m_rootPath; // Root path into the file system.
9898
Socket m_socket;
9999
bool m_useSSL; // Is this server listening on an HTTPS port?
100-
uint32_t m_clientTimeout; // Default Timeout
100+
uint32_t m_clientTimeout; // Default Timeout
101+
FreeRTOS::Semaphore m_semaphoreServerStarted = FreeRTOS::Semaphore("ServerStarted");
101102
}; // HttpServer
102103

103104
#endif /* COMPONENTS_CPP_UTILS_HTTPSERVER_H_ */

0 commit comments

Comments
 (0)
0