1 // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <protocomm.h>
18 
19 #define PROTOCOMM_HTTPD_DEFAULT_CONFIG() {   \
20     .port           = 80,                    \
21     .stack_size     = 4096,                  \
22     .task_priority  = tskIDLE_PRIORITY + 5,  \
23 }
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  * @brief   Config parameters for protocomm HTTP server
31  */
32 typedef struct {
33 
34     uint16_t port;          /*!< Port on which the HTTP server will listen */
35 
36     /**
37      * Stack size of server task, adjusted depending
38      * upon stack usage of endpoint handler
39      */
40     size_t   stack_size;
41     unsigned task_priority; /*!< Priority of server task */
42 } protocomm_http_server_config_t; /*!< HTTP Server Configuration, if HTTP Server has not been started already */
43 
44 /** Protocomm HTTPD Configuration Data
45  */
46 typedef union {
47     /** HTTP Server Handle, if ext_handle_provided is set to true
48      */
49     void *handle;
50 
51     /** HTTP Server Configuration, if a server is not already active
52      */
53     protocomm_http_server_config_t config;
54 } protocomm_httpd_config_data_t;
55 
56 /**
57  * @brief   Config parameters for protocomm HTTP server
58  */
59 typedef struct {
60     /** Flag to indicate of an external HTTP Server Handle has been provided.
61      * In such as case, protocomm will use the same HTTP Server and not start
62      * a new one internally.
63      */
64     bool ext_handle_provided;
65     /** Protocomm HTTPD Configuration Data */
66     protocomm_httpd_config_data_t data;
67 } protocomm_httpd_config_t;
68 
69 /**
70  * @brief   Start HTTPD protocomm transport
71  *
72  * This API internally creates a framework to allow endpoint registration and security
73  * configuration for the protocomm.
74  *
75  * @note    This is a singleton. ie. Protocomm can have multiple instances, but only
76  *          one instance can be bound to an HTTP transport layer.
77  *
78  * @param[in] pc        Protocomm instance pointer obtained from protocomm_new()
79  * @param[in] config    Pointer to config structure for initializing HTTP server
80  *
81  * @return
82  *  - ESP_OK : Success
83  *  - ESP_ERR_INVALID_ARG : Null arguments
84  *  - ESP_ERR_NOT_SUPPORTED : Transport layer bound to another protocomm instance
85  *  - ESP_ERR_INVALID_STATE : Transport layer already bound to this protocomm instance
86  *  - ESP_ERR_NO_MEM : Memory allocation for server resource failed
87  *  - ESP_ERR_HTTPD_* : HTTP server error on start
88  */
89 esp_err_t protocomm_httpd_start(protocomm_t *pc, const protocomm_httpd_config_t *config);
90 
91 /**
92  * @brief   Stop HTTPD protocomm transport
93  *
94  * This API cleans up the HTTPD transport protocomm and frees all the handlers registered
95  * with the protocomm.
96  *
97  * @param[in] pc    Same protocomm instance that was passed to protocomm_httpd_start()
98  *
99  * @return
100  *  - ESP_OK : Success
101  *  - ESP_ERR_INVALID_ARG : Null / incorrect protocomm instance pointer
102  */
103 esp_err_t protocomm_httpd_stop(protocomm_t *pc);
104 
105 #ifdef __cplusplus
106 }
107 #endif
108