|
| 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_security.h> |
| 18 | +#include <esp_err.h> |
| 19 | + |
| 20 | +#ifdef __cplusplus |
| 21 | +extern "C" { |
| 22 | +#endif |
| 23 | + |
| 24 | +/** |
| 25 | + * @brief Function prototype for protocomm endpoint handler |
| 26 | + */ |
| 27 | +typedef esp_err_t (*protocomm_req_handler_t)( |
| 28 | + uint32_t session_id, /*!< Session ID for identifying protocomm client */ |
| 29 | + const uint8_t *inbuf, /*!< Pointer to user provided input data buffer */ |
| 30 | + ssize_t inlen, /*!< Length o the input buffer */ |
| 31 | + uint8_t **outbuf, /*!< Pointer to output buffer allocated by handler */ |
| 32 | + ssize_t *outlen, /*!< Length of the allocated output buffer */ |
| 33 | + void *priv_data /*!< Private data passed to the handler (NULL if not used) */ |
| 34 | +); |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief This structure corresponds to a unique instance of protocomm |
| 38 | + * returned when the API `protocomm_new()` is called. The remaining |
| 39 | + * Protocomm APIs require this object as the first parameter. |
| 40 | + * |
| 41 | + * @note Structure of the protocomm object is kept private |
| 42 | + */ |
| 43 | +typedef struct protocomm protocomm_t; |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Create a new protocomm instance |
| 47 | + * |
| 48 | + * This API will return a new dynamically allocated protocomm instance |
| 49 | + * with all elements of the protocomm_t structure initialized to NULL. |
| 50 | + * |
| 51 | + * @return |
| 52 | + * - protocomm_t* : On success |
| 53 | + * - NULL : No memory for allocating new instance |
| 54 | + */ |
| 55 | +protocomm_t *protocomm_new(); |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Delete a protocomm instance |
| 59 | + * |
| 60 | + * This API will deallocate a protocomm instance that was created |
| 61 | + * using `protocomm_new()`. |
| 62 | + * |
| 63 | + * @param[in] pc Pointer to the protocomm instance to be deleted |
| 64 | + */ |
| 65 | +void protocomm_delete(protocomm_t *pc); |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief Add endpoint request handler for a protocomm instance |
| 69 | + * |
| 70 | + * This API will bind an endpoint handler function to the specified |
| 71 | + * endpoint name, along with any private data that needs to be pass to |
| 72 | + * the handler at the time of call. |
| 73 | + * |
| 74 | + * @note |
| 75 | + * - An endpoint must be bound to a valid protocomm instance, |
| 76 | + * created using `protocomm_new()`. |
| 77 | + * - This function internally calls the registered `add_endpoint()` |
| 78 | + * function of the selected transport which is a member of the |
| 79 | + * protocomm_t instance structure. |
| 80 | + * |
| 81 | + * @param[in] pc Pointer to the protocomm instance |
| 82 | + * @param[in] ep_name Endpoint identifier(name) string |
| 83 | + * @param[in] h Endpoint handler function |
| 84 | + * @param[in] priv_data Pointer to private data to be passed as a |
| 85 | + * parameter to the handler function on call. |
| 86 | + * Pass NULL if not needed. |
| 87 | + * |
| 88 | + * @return |
| 89 | + * - ESP_OK : Success |
| 90 | + * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists |
| 91 | + * - ESP_ERR_NO_MEM : Error allocating endpoint resource |
| 92 | + * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments |
| 93 | + */ |
| 94 | +esp_err_t protocomm_add_endpoint(protocomm_t *pc, const char *ep_name, |
| 95 | + protocomm_req_handler_t h, void *priv_data); |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Remove endpoint request handler for a protocomm instance |
| 99 | + * |
| 100 | + * This API will remove a registered endpoint handler identified by |
| 101 | + * an endpoint name. |
| 102 | + * |
| 103 | + * @note |
| 104 | + * - This function internally calls the registered `remove_endpoint()` |
| 105 | + * function which is a member of the protocomm_t instance structure. |
| 106 | + * |
| 107 | + * @param[in] pc Pointer to the protocomm instance |
| 108 | + * @param[in] ep_name Endpoint identifier(name) string |
| 109 | + * |
| 110 | + * @return |
| 111 | + * - ESP_OK : Success |
| 112 | + * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist |
| 113 | + * - ESP_ERR_INVALID_ARG : Null instance/name arguments |
| 114 | + */ |
| 115 | +esp_err_t protocomm_remove_endpoint(protocomm_t *pc, const char *ep_name); |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Calls the registered handler of an endpoint session |
| 119 | + * for processing incoming data and generating the response |
| 120 | + * |
| 121 | + * @note |
| 122 | + * - An endpoint must be bound to a valid protocomm instance, |
| 123 | + * created using `protocomm_new()`. |
| 124 | + * - Resulting output buffer must be deallocated by the caller. |
| 125 | + * |
| 126 | + * @param[in] pc Pointer to the protocomm instance |
| 127 | + * @param[in] ep_name Endpoint identifier(name) string |
| 128 | + * @param[in] session_id Unique ID for a communication session |
| 129 | + * @param[in] inbuf Input buffer contains input request data which is to be |
| 130 | + * processed by the registered handler |
| 131 | + * @param[in] inlen Length of the input buffer |
| 132 | + * @param[out] outbuf Pointer to internally allocated output buffer, |
| 133 | + * where the resulting response data output from |
| 134 | + * the registered handler is to be stored |
| 135 | + * @param[out] outlen Buffer length of the allocated output buffer |
| 136 | + * |
| 137 | + * @return |
| 138 | + * - ESP_OK : Request handled successfully |
| 139 | + * - ESP_FAIL : Internal error in execution of registered handler |
| 140 | + * - ESP_ERR_NO_MEM : Error allocating internal resource |
| 141 | + * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist |
| 142 | + * - ESP_ERR_INVALID_ARG : Null instance/name arguments |
| 143 | + */ |
| 144 | +esp_err_t protocomm_req_handle(protocomm_t *pc, const char *ep_name, uint32_t session_id, |
| 145 | + const uint8_t *inbuf, ssize_t inlen, |
| 146 | + uint8_t **outbuf, ssize_t *outlen); |
| 147 | + |
| 148 | +/** |
| 149 | + * @brief Add endpoint security for a protocomm instance |
| 150 | + * |
| 151 | + * This API will bind a security session establisher to the specified |
| 152 | + * endpoint name, along with any proof of possession that may be required |
| 153 | + * for authenticating a session client. |
| 154 | + * |
| 155 | + * @note |
| 156 | + * - An endpoint must be bound to a valid protocomm instance, |
| 157 | + * created using `protocomm_new()`. |
| 158 | + * - The choice of security can be any `protocomm_security_t` instance. |
| 159 | + * Choices `protocomm_security0` and `protocomm_security1` are readily available. |
| 160 | + * |
| 161 | + * @param[in] pc Pointer to the protocomm instance |
| 162 | + * @param[in] ep_name Endpoint identifier(name) string |
| 163 | + * @param[in] sec Pointer to endpoint security instance |
| 164 | + * @param[in] pop Pointer to proof of possession for authenticating a client |
| 165 | + * |
| 166 | + * @return |
| 167 | + * - ESP_OK : Success |
| 168 | + * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists |
| 169 | + * - ESP_ERR_INVALID_STATE : Security endpoint already set |
| 170 | + * - ESP_ERR_NO_MEM : Error allocating endpoint resource |
| 171 | + * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments |
| 172 | + */ |
| 173 | +esp_err_t protocomm_set_security(protocomm_t *pc, const char *ep_name, |
| 174 | + const protocomm_security_t *sec, |
| 175 | + const protocomm_security_pop_t *pop); |
| 176 | + |
| 177 | +/** |
| 178 | + * @brief Remove endpoint security for a protocomm instance |
| 179 | + * |
| 180 | + * This API will remove a registered security endpoint identified by |
| 181 | + * an endpoint name. |
| 182 | + * |
| 183 | + * @param[in] pc Pointer to the protocomm instance |
| 184 | + * @param[in] ep_name Endpoint identifier(name) string |
| 185 | + * |
| 186 | + * @return |
| 187 | + * - ESP_OK : Success |
| 188 | + * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist |
| 189 | + * - ESP_ERR_INVALID_ARG : Null instance/name arguments |
| 190 | + */ |
| 191 | +esp_err_t protocomm_unset_security(protocomm_t *pc, const char *ep_name); |
| 192 | + |
| 193 | +/** |
| 194 | + * @brief Set endpoint for version verification |
| 195 | + * |
| 196 | + * This API can be used for setting an application specific protocol |
| 197 | + * version which can be verified by clients through the endpoint. |
| 198 | + * |
| 199 | + * @note |
| 200 | + * - An endpoint must be bound to a valid protocomm instance, |
| 201 | + * created using `protocomm_new()`. |
| 202 | +
|
| 203 | + * @param[in] pc Pointer to the protocomm instance |
| 204 | + * @param[in] ep_name Endpoint identifier(name) string |
| 205 | + * @param[in] version Version identifier(name) string |
| 206 | + * |
| 207 | + * @return |
| 208 | + * - ESP_OK : Success |
| 209 | + * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists |
| 210 | + * - ESP_ERR_INVALID_STATE : Version endpoint already set |
| 211 | + * - ESP_ERR_NO_MEM : Error allocating endpoint resource |
| 212 | + * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments |
| 213 | + */ |
| 214 | +esp_err_t protocomm_set_version(protocomm_t *pc, const char *ep_name, |
| 215 | + const char *version); |
| 216 | + |
| 217 | +/** |
| 218 | + * @brief Remove version verification endpoint from a protocomm instance |
| 219 | + * |
| 220 | + * This API will remove a registered version endpoint identified by |
| 221 | + * an endpoint name. |
| 222 | + * |
| 223 | + * @param[in] pc Pointer to the protocomm instance |
| 224 | + * @param[in] ep_name Endpoint identifier(name) string |
| 225 | + * |
| 226 | + * @return |
| 227 | + * - ESP_OK : Success |
| 228 | + * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist |
| 229 | + * - ESP_ERR_INVALID_ARG : Null instance/name arguments |
| 230 | + */ |
| 231 | +esp_err_t protocomm_unset_version(protocomm_t *pc, const char *ep_name); |
| 232 | + |
| 233 | +#ifdef __cplusplus |
| 234 | +} |
| 235 | +#endif |
0 commit comments