MakerLabs ACM
sh2lib.h
Go to the documentation of this file.
1 // Copyright 2017 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 #ifndef __ESP_EXAMPLE_SH2_LIB_H_
15 #define __ESP_EXAMPLE_SH2_LIB_H_
16 
17 #include "esp_tls.h"
18 #include <nghttp2/nghttp2.h>
19 
20 /*
21  * This is a thin API wrapper over nghttp2 that offers simplified APIs for usage
22  * in the application. The intention of this wrapper is to act as a stepping
23  * stone to quickly get started with using the HTTP/2 client. Since the focus is
24  * on simplicity, not all the features of HTTP/2 are supported through this
25  * wrapper. Once you are fairly comfortable with nghttp2, feel free to directly
26  * use nghttp2 APIs or make changes to sh2lib.c for realising your objectives.
27  *
28  * TODO:
29  * - Allowing to query response code, content-type etc in the receive callback
30  * - A simple function for per-stream header callback
31  */
35 struct sh2lib_handle {
36  nghttp2_session *http2_sess;
37  char *hostname;
38  struct esp_tls *http2_tls;
39  void *userdata;
40 };
41 
43 #define DATA_RECV_RST_STREAM 1
44 
45 #define DATA_RECV_FRAME_COMPLETE 2
46 
64 typedef int (*sh2lib_frame_data_header_cb_t)(struct sh2lib_handle *handle, const char *name, size_t namelen, const char *value, size_t valuelen, int flags);
65 typedef int (*sh2lib_frame_data_recv_cb_t)(struct sh2lib_handle *handle, const char *data, size_t len, int flags);
66 
83 typedef int (*sh2lib_putpost_data_cb_t)(struct sh2lib_handle *handle, char *data, size_t len, uint32_t *data_flags);
84 
85 typedef struct {
90 
106 int sh2lib_connect(struct sh2lib_handle *hd, const char *uri, mbedtls_x509_crt* cacerts);
107 
117 void sh2lib_free(struct sh2lib_handle *hd);
118 
137 int sh2lib_do_get(struct sh2lib_handle *hd, const char *path, sh2lib_callbacks* callbacks);
138 
159 int sh2lib_do_post(struct sh2lib_handle *hd, const char *path,
160  sh2lib_callbacks* callbacks);
161 
182 int sh2lib_do_put(struct sh2lib_handle *hd, const char *path,
183  sh2lib_callbacks* callbacks);
184 
199 int sh2lib_execute(struct sh2lib_handle *hd);
200 
201 #define SH2LIB_MAKE_NV(NAME, VALUE, FLAGS) \
202  { \
203  (uint8_t *)NAME, (uint8_t *)VALUE, strlen(NAME), strlen(VALUE), FLAGS \
204  }
205 
232 int sh2lib_do_get_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen, sh2lib_callbacks* callbacks);
233 
263 int sh2lib_do_putpost_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen,
264  sh2lib_callbacks* callbacks);
265 
266 #endif /* ! __ESP_EXAMPLE_SH2_LIB_H_ */
sh2lib_frame_data_recv_cb_t data_recv_cb
Definition: sh2lib.h:88
int(* sh2lib_putpost_data_cb_t)(struct sh2lib_handle *handle, char *data, size_t len, uint32_t *data_flags)
Function Prototype for callback to send data in PUT/POST.
Definition: sh2lib.h:83
sh2lib_putpost_data_cb_t send_cb
Definition: sh2lib.h:86
Handle for working with sh2lib APIs.
Definition: sh2lib.h:35
Definition: sh2lib.h:85
struct esp_tls * http2_tls
Definition: sh2lib.h:38
int sh2lib_do_get_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen, sh2lib_callbacks *callbacks)
Setup an HTTP GET request stream with custom name-value pairs.
sh2lib_frame_data_header_cb_t header_recv_cb
Definition: sh2lib.h:87
static spi_device_handle_t handle
Definition: u8g2_esp32_hal.cpp:14
void sh2lib_free(struct sh2lib_handle *hd)
Free a sh2lib handle.
const path
Definition: oauth-server.js:26
char * hostname
Definition: sh2lib.h:37
int sh2lib_do_get(struct sh2lib_handle *hd, const char *path, sh2lib_callbacks *callbacks)
Setup an HTTP GET request stream.
int sh2lib_do_put(struct sh2lib_handle *hd, const char *path, sh2lib_callbacks *callbacks)
Setup an HTTP PUT request stream.
int sh2lib_connect(struct sh2lib_handle *hd, const char *uri, mbedtls_x509_crt *cacerts)
Connect to a URI using HTTP/2.
int sh2lib_execute(struct sh2lib_handle *hd)
Execute send/receive on an HTTP/2 connection.
int sh2lib_do_putpost_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen, sh2lib_callbacks *callbacks)
Setup an HTTP PUT/POST request stream with custom name-value pairs.
int(* sh2lib_frame_data_recv_cb_t)(struct sh2lib_handle *handle, const char *data, size_t len, int flags)
Definition: sh2lib.h:65
nghttp2_session * http2_sess
Definition: sh2lib.h:36
void * userdata
Definition: sh2lib.h:39
int sh2lib_do_post(struct sh2lib_handle *hd, const char *path, sh2lib_callbacks *callbacks)
Setup an HTTP POST request stream.
int(* sh2lib_frame_data_header_cb_t)(struct sh2lib_handle *handle, const char *name, size_t namelen, const char *value, size_t valuelen, int flags)
Function Prototype for data receive callback.
Definition: sh2lib.h:64