فهرست منبع

Keep one active camera request and preserve a free HTTP socket

SybexXx 1 روز پیش
والد
کامیت
cfe8feea8c
1فایلهای تغییر یافته به همراه23 افزوده شده و 5 حذف شده
  1. 23 5
      code/components/jomjol_wlan/basic_auth.cpp

+ 23 - 5
code/components/jomjol_wlan/basic_auth.cpp

@@ -7,6 +7,7 @@
 #include "ClassLogFile.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/queue.h"
+#include "freertos/semphr.h"
 #include "freertos/task.h"
 #include <new>
 #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
 // lets a capture wait behind a live-stream request without blocking /info.
 static QueueHandle_t cameraAsyncRequestQueue = NULL;
+static SemaphoreHandle_t cameraAsyncRequestSlot = NULL;
 
 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
         // can release the socket and accept subsequent requests.
         httpd_req_async_handler_complete(work.request);
+        if (cameraAsyncRequestSlot != NULL) {
+            xSemaphoreGive(cameraAsyncRequestSlot);
+        }
     }
     vTaskDelete(NULL);
 }
@@ -69,15 +74,17 @@ esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
     if (req == NULL || handler == NULL) {
         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_sendstr(req, "Camera request worker is unavailable");
+        httpd_resp_sendstr(req, "Camera is busy with another capture or stream");
         return ESP_OK;
     }
 
     httpd_req_t *asyncRequest = NULL;
     const esp_err_t copyResult = httpd_req_async_handler_begin(req, &asyncRequest);
     if (copyResult != ESP_OK) {
+        xSemaphoreGive(cameraAsyncRequestSlot);
         httpd_resp_set_status(req, "503 Service Unavailable");
         httpd_resp_sendstr(req, "Could not queue camera request");
         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) {
         // Restore ownership of the socket before returning a busy response.
         httpd_req_async_handler_complete(asyncRequest);
+        xSemaphoreGive(cameraAsyncRequestSlot);
         httpd_resp_set_status(req, "503 Service Unavailable");
         httpd_resp_sendstr(req, "Camera request queue is full");
     }
@@ -143,10 +151,18 @@ void init_basic_auth() {
     }
 
     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,
-                                "Failed to create asynchronous camera request queue");
+                                "Failed to create asynchronous camera request queue/slot");
             return;
         }
 
@@ -155,6 +171,8 @@ void init_basic_auth() {
                                     NULL, 1) != pdPASS) {
             vQueueDelete(cameraAsyncRequestQueue);
             cameraAsyncRequestQueue = NULL;
+            vSemaphoreDelete(cameraAsyncRequestSlot);
+            cameraAsyncRequestSlot = NULL;
             LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
                                 "Failed to start asynchronous camera request worker");
         }