ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers baidu_http_client_c.h Source File

baidu_http_client_c.h

00001 // Copyright 2017 Baidu Inc. All Rights Reserved.
00002 // Author: Pan Haijun, Gang Chen(chengang12@baidu.com)
00003 //
00004 // This header file provide http client related data structures and APIs.
00005 
00006 #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_HTTP_CLIENT_BAIDU_HTTP_CLIENT_C_H
00007 #define BAIDU_IOT_TINYDU_IOT_OS_SRC_HTTP_CLIENT_BAIDU_HTTP_CLIENT_C_H
00008 
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015 //http client results
00016 typedef enum http_result {
00017     HTTP_OK = 0,        // Success
00018     HTTP_PROCESSING,    // Processing
00019     HTTP_PARSE,         // url Parse error
00020     HTTP_DNS,           // Could not resolve name
00021     HTTP_PRTCL,         // Protocol error
00022     HTTP_NOTFOUND,      // HTTP 404 Error
00023     HTTP_REFUSED,       // HTTP 403 Error
00024     HTTP_ERROR,         // HTTP xxx error
00025     HTTP_TIMEOUT,       // Connection timeout
00026     HTTP_CONN,          // Connection error
00027     HTTP_CLOSED,        // Connection was closed by remote host
00028     HTTP_NOT_SUPPORT,   // not supported feature
00029     HTTP_REDIRECTTION,  //take a redirection when http header contains 'Location'
00030     HTTP_FAILED = -1,
00031 } e_http_result;
00032 
00033 typedef enum http_meth {
00034     HTTP_GET,
00035     HTTP_POST,
00036     HTTP_PUT,
00037     HTTP_DELETE,
00038     HTTP_HEAD
00039 } e_http_meth;
00040 
00041 typedef struct http_client_socket_ops {
00042     int  n_handle;
00043     int  (*init)(void* socket_args);
00044     int  (*open)(int socket_handle);
00045     int  (*connect)(int socket_handle, const char* host, const int port);
00046     void (*set_blocking)(int socket_handle, int blocking);
00047     void (*set_timeout)(int socket_handle, int timeout);
00048     int  (*recv)(int socket_handle, void* data, unsigned size);
00049     int  (*send)(int socket_handle, const void* data, unsigned size);
00050     int  (*close)(int socket_handle);
00051     void (*destroy)(int socket_handle);
00052 } socket_ops;
00053 
00054 //to tell data output callback user that if the current data block is first block or last block
00055 typedef enum data_pos {
00056     DATA_FIRST  = 0x1,
00057     DATA_MID    = 0x2,
00058     DATA_LAST   = 0x4
00059 } e_data_pos;
00060 
00061 /**
00062  *
00063  * DESC:
00064  * the callback to handler the media data download by http
00065  *
00066  * PARAM:
00067  * @param[in] p_user_ctx: usr ctx registed by user
00068  * @param[in] pos:  to identify if it is data stream's start, or middle , or end of data stream
00069  * @param[in] buf:   buffer stored media data
00070  * @param[in] len:   data length in 'buf'
00071  * @param[in] type: data type to identify media or others
00072  *
00073  * @RETURN: negative number when failed
00074  */
00075 typedef int (*data_out_handler_cb)(void* p_user_ctx, e_data_pos pos,
00076                                    const char* buf, size_t len, const char* type);
00077 /**
00078  * DESC:
00079  * callback to check whether need to stop getting data
00080  *
00081  * PARAM: none
00082  *
00083  * @RETURN
00084  * 1: to stop; 0: no stop
00085  */
00086 typedef int (*check_stop_notify_cb_t)();
00087 
00088 /**
00089  * DESC:
00090  * callback to reset MDM stop flag
00091  *
00092  * PARAM: none
00093  * RETURN: none
00094  */
00095 typedef void (*reset_stop_notify_cb_t)();
00096 /**
00097  * DESC:
00098  * sometimes other module need to know which url is used to download data.
00099  * this callback is used to get the url
00100  *
00101  * PARAM:
00102  * @param[in] url: the url used by http to download data
00103  *
00104  * RETURN: none
00105  */
00106 typedef void (*get_url_cb_t)(const char *url);
00107 
00108 #define HC_CONTENT_TYPE_LEN_MAX 32 // the max length of "content type" section
00109 
00110 typedef struct http_client_c {
00111     const char**           pps_custom_headers;
00112     size_t                 sz_headers_count;
00113     int                    n_http_response_code; // http response code
00114     socket_ops             ops;                  // socket operations
00115     data_out_handler_cb    data_hdlr_cb;         // callback for output data
00116     void*                  p_data_hdlr_ctx;      // users args for data_hdlr_cb
00117     int                    n_http_content_len;   // http content length
00118     char                   p_http_content_type[HC_CONTENT_TYPE_LEN_MAX]; // http content type
00119     char*                  p_location;           // http header "Location"
00120     //a callback to check that stopping http client getting data or not
00121     check_stop_notify_cb_t check_stop_notify_cb;
00122     reset_stop_notify_cb_t reset_stop_notify_cb; // reset stop notify flag
00123     get_url_cb_t           get_url_cb;           // get the url which is used by http to get data
00124     int                    n_is_chunked;
00125     size_t                 sz_chunk_size;
00126     char*                  p_chunk_buf;
00127 } http_client_c;
00128 
00129 typedef struct _http_client_statistic_s {
00130     size_t download_data_size;
00131     size_t upload_data_size;
00132     int    error_count;
00133     int    last_error_code;
00134     size_t recv_packet_longest_time;
00135 } http_client_statistic_t;
00136 
00137 int http_client_init(http_client_c* p_client);
00138 void http_client_destroy(http_client_c* p_client);
00139 int http_client_init_socket_ops(http_client_c* p_client, socket_ops* p_ops, void* socket_args);
00140 void http_client_reg_data_hdlr_cb(http_client_c* p_client, data_out_handler_cb data_hdlr_cb,
00141                                   void* p_usr_ctx);
00142 void http_client_reg_cb_to_get_url(http_client_c* p_client, get_url_cb_t cb);
00143 void http_client_set_cust_headers(http_client_c* p_client, const char** headers, size_t pairs);
00144 e_http_result http_client_get(http_client_c* p_client, const char* url);
00145 int http_client_get_resp_code(http_client_c* p_client);
00146 void http_client_reg_stop_notify_cb(http_client_c* p_client, check_stop_notify_cb_t chk_stp_cb,
00147                                     reset_stop_notify_cb_t rst_stp_cb);
00148 /**
00149  * FUNC:
00150  * int get_http_statistic(http_client_c *p_client, socket_ops *p_ops, void *socket_args)
00151  *
00152  * DESC:
00153  * used to get statistic info of http module
00154  *
00155  * PARAM:
00156  * @param[out] statistic_result: buffer to accept the statistic info
00157  *
00158  */
00159 void get_http_statistic(http_client_statistic_t* statistic_result);
00160 
00161 #ifdef __cplusplus
00162 }
00163 #endif
00164 
00165 #endif //BAIDU_IOT_TINYDU_IOT_OS_SRC_HTTP_CLIENT_BAIDU_HTTP_CLIENT_C_H
00166