Now Here is the webserver implementation that I have:
Initializing the webserver:
- esp_err_t WEBSERVER_Start(void)
- {
- ESP_LOGI(TAG, "Starting Webserver");
- esp_err_t retValue = ESP_FAIL;
- httpd_handle_t server = NULL;
- httpd_config_t config = HTTPD_DEFAULT_CONFIG();
- httpd_uri_t http_uri[] =
- {
- {
- .uri = "/api/progress",
- .method = HTTP_GET,
- .handler = FirmwareProgressGetHandler,
- .user_ctx = NULL,
- },
- {
- .uri = "/api/upload",
- .method = HTTP_POST,
- .handler = FirmwareUploadPostHandler,
- .user_ctx = NULL,
- },
- .
- .
- .
- /*
- * Handler for serving the webpage files
- */
- {
- .uri = "/*",
- .method = HTTP_GET,
- .handler = WebServer,
- .user_ctx = "/www_0",//TODO: Set base path based on OTA_0 or OTA_1
- }
- };
- mAsyncQueue = xQueueCreate(10, sizeof(async_req_t));
- if (NULL != mAsyncQueue)
- {
- retValue = (pdTRUE == xTaskCreate( AsyncRequestHandler, "Async Request", 4096, NULL, configMAX_PRIORITIES - 1, NULL)) ?
- ESP_OK : ESP_FAIL;
- }
- if (ESP_OK == retValue)
- {
- config.uri_match_fn = httpd_uri_match_wildcard;
- config.stack_size = 10 * 1024;
- httpd_start(&server, &config);
- for (int i = 0; i < (sizeof(http_uri) / sizeof(http_uri[0])); ++i)
- {
- httpd_register_uri_handler(server, &http_uri[i]);
- }
- httpd_register_err_handler(server, HTTPD_404_NOT_FOUND, Error404handler);
- }
- return retValue;
- }
- static void AsyncRequestHandler(void* param)
- {
- async_req_t asyncReq;
- while (1)
- {
- BaseType_t recvCheck = xQueueReceive(mAsyncQueue, &asyncReq, portMAX_DELAY);
- if (recvCheck != pdTRUE)
- {
- ESP_LOGE(TAG, "EVENT Receive error");
- }
- else
- {
- ESP_LOGI(TAG, "Received Async Request");
- asyncReq.handler(asyncReq.req);
- httpd_req_async_handler_complete(asyncReq.req);
- }
- }
- }
- static esp_err_t FirmwareUploadPostHandler(httpd_req_t* req)
- {
- esp_err_t retValue = ESP_FAIL;
- httpd_req_t* asyncReq = NULL;
- retValue = httpd_req_async_handler_begin(req, &asyncReq);
- async_req_t async =
- {
- .handler = FirmwareUpload,
- .req = asyncReq,
- };
- ESP_LOGI(TAG, "FirmwareUpload Post Handler");
- if (xQueueSend(mAsyncQueue, &async, pdMS_TO_TICKS(100)) == false)
- {
- ESP_LOGE(TAG, "worker queue is full");
- httpd_req_async_handler_complete(asyncReq); // cleanup
- return ESP_FAIL;
- }
- return retValue;
- }
- static esp_err_t FirmwareUpload(httpd_req_t* req)
- {
- ESP_LOGI(TAG, "Firmware Upload");
- esp_err_t retValue = ESP_FAIL;
- int bytesRecv = 0;
- int bytesReqRecv = -1;
- int reqRecReturn = 0;
- int totalBytesRemaining = req->content_len;
- int timeoutRetry = 3;
- httpd_err_code_t errorCode = HTTPD_500_INTERNAL_SERVER_ERROR;
- char buffer[BUFFER_SIZE] = {0};
- char* bufferPtr = buffer;
- //while (totalBytesRemaining > 0) ***Commented out to see print out of TEST to get see what first read is ***
- {
- bytesRecv = 0;
- bytesReqRecv = (bytesReqRecv != 0) ? (3 * sizeof(uint32_t) + 16) :
- (totalBytesRemaining < BUFFER_SIZE) ? totalBytesRemaining : BUFFER_SIZE;
- bufferPtr = buffer;
- while (bytesRecv != bytesReqRecv)
- {
- reqRecReturn = httpd_req_recv(req, bufferPtr, bytesReqRecv - bytesRecv);
- if ((HTTPD_SOCK_ERR_TIMEOUT == reqRecReturn) && (0 < timeoutRetry))
- {
- --timeoutRetry;
- ESP_LOGW(TAG, "Recv TIMEOUT retrying %d more time(s)", timeoutRetry);
- }
- else if ((HTTPD_SOCK_ERR_TIMEOUT == reqRecReturn) && (0 >= timeoutRetry))
- {
- ESP_LOGE(TAG, "Recv TIMEOUT");
- break;
- }
- else if (HTTPD_SOCK_ERR_INVALID == reqRecReturn)
- {
- ESP_LOGE(TAG, "Invalid sock");
- break;
- }
- else if (HTTPD_SOCK_ERR_FAIL == reqRecReturn)
- {
- ESP_LOGE(TAG, "sock failed");
- break;
- }
- else if (0 == reqRecReturn)
- {
- ESP_LOGW(TAG, "Received 0 bytes");
- }
- else if (0 < reqRecReturn)
- {
- timeoutRetry = 3;
- bytesRecv += reqRecReturn;
- bufferPtr += reqRecReturn;
- ESP_LOGV(TAG, "Received: %4d of %4d", bytesRecv, bytesReqRecv);
- }
- else
- {
- ESP_LOGW(TAG, "Unknown Received %d", reqRecReturn);
- }
- }
- printf("TEST - %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
- buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
- if (bytesRecv != bytesReqRecv)
- {
- retValue = ESP_FAIL;
- ESP_LOGE(TAG, "Failed to recv expected data - %d/%d", bytesRecv, bytesReqRecv);
- totalBytesRemaining = -1;
- }
- else
- {
- *** Operate on received data ***
- totalBytesRemaining = (ESP_OK == retValue) ? totalBytesRemaining - bytesRecv : -1;
- bytesReqRecv = 0;
- }
- }//END commented out loop to get one iteration
- return retValue;
- #endif
- }
I have seen it close to 0x1080, 0x500(ish) and it is not consistent that I have seen on how far in it goes on the same transfer of data.
Any help would be appreciated.
Please let me know if there is any other information needed to help resolve my issue.
Thank you
it