|
@@ -7,6 +7,7 @@
|
|
|
#include "ClassLogFile.h"
|
|
#include "ClassLogFile.h"
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
|
#include "freertos/queue.h"
|
|
#include "freertos/queue.h"
|
|
|
|
|
+#include "freertos/semphr.h"
|
|
|
#include "freertos/task.h"
|
|
#include "freertos/task.h"
|
|
|
#include <new>
|
|
#include <new>
|
|
|
#include <cstring>
|
|
#include <cstring>
|
|
@@ -28,6 +29,7 @@ basic_auth_info_t basic_auth_info = { NULL, NULL };
|
|
|
// long-running camera responses outside the HTTP server task. A short queue
|
|
// long-running camera responses outside the HTTP server task. A short queue
|
|
|
// lets a capture wait behind a live-stream request without blocking /info.
|
|
// lets a capture wait behind a live-stream request without blocking /info.
|
|
|
static QueueHandle_t cameraAsyncRequestQueue = NULL;
|
|
static QueueHandle_t cameraAsyncRequestQueue = NULL;
|
|
|
|
|
+static SemaphoreHandle_t cameraAsyncRequestSlot = NULL;
|
|
|
|
|
|
|
|
struct AsyncCameraHttpRequest
|
|
struct AsyncCameraHttpRequest
|
|
|
{
|
|
{
|
|
@@ -58,6 +60,9 @@ static void async_camera_http_request_task(void *argument)
|
|
|
// Complete the async request on every worker exit path so the HTTP server
|
|
// Complete the async request on every worker exit path so the HTTP server
|
|
|
// can release the socket and accept subsequent requests.
|
|
// can release the socket and accept subsequent requests.
|
|
|
httpd_req_async_handler_complete(work.request);
|
|
httpd_req_async_handler_complete(work.request);
|
|
|
|
|
+ if (cameraAsyncRequestSlot != NULL) {
|
|
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
vTaskDelete(NULL);
|
|
vTaskDelete(NULL);
|
|
|
}
|
|
}
|
|
@@ -69,15 +74,17 @@ esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
|
|
|
if (req == NULL || handler == NULL) {
|
|
if (req == NULL || handler == NULL) {
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
}
|
|
}
|
|
|
- if (cameraAsyncRequestQueue == NULL) {
|
|
|
|
|
|
|
+ if (cameraAsyncRequestQueue == NULL || cameraAsyncRequestSlot == NULL ||
|
|
|
|
|
+ xSemaphoreTake(cameraAsyncRequestSlot, 0) != pdTRUE) {
|
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
- httpd_resp_sendstr(req, "Camera request worker is unavailable");
|
|
|
|
|
|
|
+ httpd_resp_sendstr(req, "Camera is busy with another capture or stream");
|
|
|
return ESP_OK;
|
|
return ESP_OK;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
httpd_req_t *asyncRequest = NULL;
|
|
httpd_req_t *asyncRequest = NULL;
|
|
|
const esp_err_t copyResult = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
const esp_err_t copyResult = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
|
if (copyResult != ESP_OK) {
|
|
if (copyResult != ESP_OK) {
|
|
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
httpd_resp_sendstr(req, "Could not queue camera request");
|
|
httpd_resp_sendstr(req, "Could not queue camera request");
|
|
|
return ESP_OK;
|
|
return ESP_OK;
|
|
@@ -94,6 +101,7 @@ esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
|
|
|
if (xQueueSend(cameraAsyncRequestQueue, &work, 0) != pdTRUE) {
|
|
if (xQueueSend(cameraAsyncRequestQueue, &work, 0) != pdTRUE) {
|
|
|
// Restore ownership of the socket before returning a busy response.
|
|
// Restore ownership of the socket before returning a busy response.
|
|
|
httpd_req_async_handler_complete(asyncRequest);
|
|
httpd_req_async_handler_complete(asyncRequest);
|
|
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
httpd_resp_sendstr(req, "Camera request queue is full");
|
|
httpd_resp_sendstr(req, "Camera request queue is full");
|
|
|
}
|
|
}
|
|
@@ -143,10 +151,18 @@ void init_basic_auth() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (cameraAsyncRequestQueue == NULL) {
|
|
if (cameraAsyncRequestQueue == NULL) {
|
|
|
- cameraAsyncRequestQueue = xQueueCreate(2, sizeof(AsyncCameraHttpRequest));
|
|
|
|
|
- if (cameraAsyncRequestQueue == NULL) {
|
|
|
|
|
|
|
+ cameraAsyncRequestQueue = xQueueCreate(1, sizeof(AsyncCameraHttpRequest));
|
|
|
|
|
+ cameraAsyncRequestSlot = xSemaphoreCreateBinary();
|
|
|
|
|
+ if (cameraAsyncRequestSlot != NULL) {
|
|
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (cameraAsyncRequestQueue == NULL || cameraAsyncRequestSlot == NULL) {
|
|
|
|
|
+ if (cameraAsyncRequestQueue != NULL) {
|
|
|
|
|
+ vQueueDelete(cameraAsyncRequestQueue);
|
|
|
|
|
+ cameraAsyncRequestQueue = NULL;
|
|
|
|
|
+ }
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
|
- "Failed to create asynchronous camera request queue");
|
|
|
|
|
|
|
+ "Failed to create asynchronous camera request queue/slot");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -155,6 +171,8 @@ void init_basic_auth() {
|
|
|
NULL, 1) != pdPASS) {
|
|
NULL, 1) != pdPASS) {
|
|
|
vQueueDelete(cameraAsyncRequestQueue);
|
|
vQueueDelete(cameraAsyncRequestQueue);
|
|
|
cameraAsyncRequestQueue = NULL;
|
|
cameraAsyncRequestQueue = NULL;
|
|
|
|
|
+ vSemaphoreDelete(cameraAsyncRequestSlot);
|
|
|
|
|
+ cameraAsyncRequestSlot = NULL;
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
|
"Failed to start asynchronous camera request worker");
|
|
"Failed to start asynchronous camera request worker");
|
|
|
}
|
|
}
|