.task_priority = tskIDLE_PRIORITY + 5, \
}
-/**
- * @brief Config parameters for protocomm HTTP server
- */
+
+
+/** Protocomm HTTP Server Configuration */
typedef struct {
+
uint16_t port; /*!< Port on which the http server will listen */
/**
*/
size_t stack_size;
unsigned task_priority; /*!< Priority of server task */
+} protocomm_http_server_config_t; /*!< HTTP Server Configuration, if HTTP Server has not been started already */
+
+/** Protocomm HTTPD Configuration Data */
+typedef union {
+ /** HTTP Server Handle, if ext_handle_provided is set to true */
+ void *handle;
+ /** HTTP Server Configuration, if a server is not already active */
+ protocomm_http_server_config_t config;
+} protocomm_httpd_config_data_t;
+
+/**
+ * @brief Config parameters for protocomm HTTP server
+ */
+typedef struct {
+ /** Flag to indicate of an external HTTP Server Handle has been provided.
+ * In such as case, protocomm will use the same HTTP Server and not start
+ * a new one internally.
+ */
+ bool ext_handle_provided;
+ /** Protocomm HTTPD Configuration Data */
+ protocomm_httpd_config_data_t data;
} protocomm_httpd_config_t;
/**
static const char *TAG = "protocomm_httpd";
static protocomm_t *pc_httpd; /* The global protocomm instance for HTTPD */
+static bool pc_ext_httpd_handle_provided;
static uint32_t session_id = PROTOCOMM_NO_SESSION_ID;
#define MAX_REQ_BODY_LEN 4096
return ret;
}
-esp_err_t protocomm_httpd_add_endpoint(const char *ep_name,
+static esp_err_t protocomm_httpd_add_endpoint(const char *ep_name,
protocomm_req_handler_t req_handler,
void *priv_data)
{
return ESP_ERR_NOT_SUPPORTED;
}
- /* Private data will point to the HTTP server handle */
- pc->priv = calloc(1, sizeof(httpd_handle_t));
- if (!pc->priv) {
- ESP_LOGE(TAG, "Malloc failed for HTTP server handle");
- return ESP_ERR_NO_MEM;
- }
-
- /* Configure the HTTP server */
- httpd_config_t server_config = HTTPD_DEFAULT_CONFIG();
- server_config.server_port = config->port;
- server_config.stack_size = config->stack_size;
- server_config.task_priority = config->task_priority;
- server_config.lru_purge_enable = true;
- server_config.max_open_sockets = 1;
+ if (config->ext_handle_provided) {
+ if (config->data.handle) {
+ pc->priv = config->data.handle;
+ pc_ext_httpd_handle_provided = true;
+ } else {
+ return ESP_ERR_INVALID_ARG;
+ }
+ } else {
+ /* Private data will point to the HTTP server handle */
+ pc->priv = calloc(1, sizeof(httpd_handle_t));
+ if (!pc->priv) {
+ ESP_LOGE(TAG, "Malloc failed for HTTP server handle");
+ return ESP_ERR_NO_MEM;
+ }
- esp_err_t err;
- if ((err = httpd_start((httpd_handle_t *)pc->priv, &server_config)) != ESP_OK) {
- ESP_LOGE(TAG, "Failed to start http server: %s", esp_err_to_name(err));
- free(pc->priv);
- return err;
+ /* Configure the HTTP server */
+ httpd_config_t server_config = HTTPD_DEFAULT_CONFIG();
+ server_config.server_port = config->data.config.port;
+ server_config.stack_size = config->data.config.stack_size;
+ server_config.task_priority = config->data.config.task_priority;
+ server_config.lru_purge_enable = true;
+ server_config.max_open_sockets = 1;
+
+ esp_err_t err;
+ if ((err = httpd_start((httpd_handle_t *)pc->priv, &server_config)) != ESP_OK) {
+ ESP_LOGE(TAG, "Failed to start http server: %s", esp_err_to_name(err));
+ free(pc->priv);
+ return err;
+ }
}
-
pc->add_endpoint = protocomm_httpd_add_endpoint;
pc->remove_endpoint = protocomm_httpd_remove_endpoint;
pc_httpd = pc;
esp_err_t protocomm_httpd_stop(protocomm_t *pc)
{
if ((pc != NULL) && (pc == pc_httpd)) {
- httpd_handle_t *server_handle = (httpd_handle_t *) pc_httpd->priv;
- httpd_stop(*server_handle);
- free(server_handle);
+ if (!pc_ext_httpd_handle_provided) {
+ httpd_handle_t *server_handle = (httpd_handle_t *) pc_httpd->priv;
+ httpd_stop(*server_handle);
+ free(server_handle);
+ } else {
+ pc_ext_httpd_handle_provided = false;
+ }
pc_httpd->priv = NULL;
pc_httpd = NULL;
return ESP_OK;