ex

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

Committer:
TMBOY
Date:
Tue Jul 18 16:54:45 2017 +0800
Revision:
47:9e361da97763
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 47:9e361da97763 1 // Copyright 2017 Baidu Inc. All Rights Reserved.
TMBOY 47:9e361da97763 2 // Author: Pan Haijun, Gang Chen(chengang12@baidu.com)
TMBOY 47:9e361da97763 3 //
TMBOY 47:9e361da97763 4 // This header file provide http client related data structures and APIs.
TMBOY 47:9e361da97763 5
TMBOY 47:9e361da97763 6 #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_HTTP_CLIENT_BAIDU_HTTP_CLIENT_C_H
TMBOY 47:9e361da97763 7 #define BAIDU_IOT_TINYDU_IOT_OS_SRC_HTTP_CLIENT_BAIDU_HTTP_CLIENT_C_H
TMBOY 47:9e361da97763 8
TMBOY 47:9e361da97763 9 #include <stdlib.h>
TMBOY 47:9e361da97763 10 #include <stdio.h>
TMBOY 47:9e361da97763 11
TMBOY 47:9e361da97763 12 #ifdef __cplusplus
TMBOY 47:9e361da97763 13 extern "C" {
TMBOY 47:9e361da97763 14 #endif
TMBOY 47:9e361da97763 15 //http client results
TMBOY 47:9e361da97763 16 typedef enum http_result {
TMBOY 47:9e361da97763 17 HTTP_OK = 0, // Success
TMBOY 47:9e361da97763 18 HTTP_PROCESSING, // Processing
TMBOY 47:9e361da97763 19 HTTP_PARSE, // url Parse error
TMBOY 47:9e361da97763 20 HTTP_DNS, // Could not resolve name
TMBOY 47:9e361da97763 21 HTTP_PRTCL, // Protocol error
TMBOY 47:9e361da97763 22 HTTP_NOTFOUND, // HTTP 404 Error
TMBOY 47:9e361da97763 23 HTTP_REFUSED, // HTTP 403 Error
TMBOY 47:9e361da97763 24 HTTP_ERROR, // HTTP xxx error
TMBOY 47:9e361da97763 25 HTTP_TIMEOUT, // Connection timeout
TMBOY 47:9e361da97763 26 HTTP_CONN, // Connection error
TMBOY 47:9e361da97763 27 HTTP_CLOSED, // Connection was closed by remote host
TMBOY 47:9e361da97763 28 HTTP_NOT_SUPPORT, // not supported feature
TMBOY 47:9e361da97763 29 HTTP_REDIRECTTION, //take a redirection when http header contains 'Location'
TMBOY 47:9e361da97763 30 HTTP_FAILED = -1,
TMBOY 47:9e361da97763 31 } e_http_result;
TMBOY 47:9e361da97763 32
TMBOY 47:9e361da97763 33 typedef enum http_meth {
TMBOY 47:9e361da97763 34 HTTP_GET,
TMBOY 47:9e361da97763 35 HTTP_POST,
TMBOY 47:9e361da97763 36 HTTP_PUT,
TMBOY 47:9e361da97763 37 HTTP_DELETE,
TMBOY 47:9e361da97763 38 HTTP_HEAD
TMBOY 47:9e361da97763 39 } e_http_meth;
TMBOY 47:9e361da97763 40
TMBOY 47:9e361da97763 41 typedef struct http_client_socket_ops {
TMBOY 47:9e361da97763 42 int n_handle;
TMBOY 47:9e361da97763 43 int (*init)(void* socket_args);
TMBOY 47:9e361da97763 44 int (*open)(int socket_handle);
TMBOY 47:9e361da97763 45 int (*connect)(int socket_handle, const char* host, const int port);
TMBOY 47:9e361da97763 46 void (*set_blocking)(int socket_handle, int blocking);
TMBOY 47:9e361da97763 47 void (*set_timeout)(int socket_handle, int timeout);
TMBOY 47:9e361da97763 48 int (*recv)(int socket_handle, void* data, unsigned size);
TMBOY 47:9e361da97763 49 int (*send)(int socket_handle, const void* data, unsigned size);
TMBOY 47:9e361da97763 50 int (*close)(int socket_handle);
TMBOY 47:9e361da97763 51 void (*destroy)(int socket_handle);
TMBOY 47:9e361da97763 52 } socket_ops;
TMBOY 47:9e361da97763 53
TMBOY 47:9e361da97763 54 //to tell data output callback user that if the current data block is first block or last block
TMBOY 47:9e361da97763 55 typedef enum data_pos {
TMBOY 47:9e361da97763 56 DATA_FIRST = 0x1,
TMBOY 47:9e361da97763 57 DATA_MID = 0x2,
TMBOY 47:9e361da97763 58 DATA_LAST = 0x4
TMBOY 47:9e361da97763 59 } e_data_pos;
TMBOY 47:9e361da97763 60
TMBOY 47:9e361da97763 61 /**
TMBOY 47:9e361da97763 62 *
TMBOY 47:9e361da97763 63 * DESC:
TMBOY 47:9e361da97763 64 * the callback to handler the media data download by http
TMBOY 47:9e361da97763 65 *
TMBOY 47:9e361da97763 66 * PARAM:
TMBOY 47:9e361da97763 67 * @param[in] p_user_ctx: usr ctx registed by user
TMBOY 47:9e361da97763 68 * @param[in] pos: to identify if it is data stream's start, or middle , or end of data stream
TMBOY 47:9e361da97763 69 * @param[in] buf: buffer stored media data
TMBOY 47:9e361da97763 70 * @param[in] len: data length in 'buf'
TMBOY 47:9e361da97763 71 * @param[in] type: data type to identify media or others
TMBOY 47:9e361da97763 72 *
TMBOY 47:9e361da97763 73 * @RETURN: negative number when failed
TMBOY 47:9e361da97763 74 */
TMBOY 47:9e361da97763 75 typedef int (*data_out_handler_cb)(void* p_user_ctx, e_data_pos pos,
TMBOY 47:9e361da97763 76 const char* buf, size_t len, const char* type);
TMBOY 47:9e361da97763 77 /**
TMBOY 47:9e361da97763 78 * DESC:
TMBOY 47:9e361da97763 79 * callback to check whether need to stop getting data
TMBOY 47:9e361da97763 80 *
TMBOY 47:9e361da97763 81 * PARAM: none
TMBOY 47:9e361da97763 82 *
TMBOY 47:9e361da97763 83 * @RETURN
TMBOY 47:9e361da97763 84 * 1: to stop; 0: no stop
TMBOY 47:9e361da97763 85 */
TMBOY 47:9e361da97763 86 typedef int (*check_stop_notify_cb_t)();
TMBOY 47:9e361da97763 87
TMBOY 47:9e361da97763 88 /**
TMBOY 47:9e361da97763 89 * DESC:
TMBOY 47:9e361da97763 90 * callback to reset MDM stop flag
TMBOY 47:9e361da97763 91 *
TMBOY 47:9e361da97763 92 * PARAM: none
TMBOY 47:9e361da97763 93 * RETURN: none
TMBOY 47:9e361da97763 94 */
TMBOY 47:9e361da97763 95 typedef void (*reset_stop_notify_cb_t)();
TMBOY 47:9e361da97763 96 /**
TMBOY 47:9e361da97763 97 * DESC:
TMBOY 47:9e361da97763 98 * sometimes other module need to know which url is used to download data.
TMBOY 47:9e361da97763 99 * this callback is used to get the url
TMBOY 47:9e361da97763 100 *
TMBOY 47:9e361da97763 101 * PARAM:
TMBOY 47:9e361da97763 102 * @param[in] url: the url used by http to download data
TMBOY 47:9e361da97763 103 *
TMBOY 47:9e361da97763 104 * RETURN: none
TMBOY 47:9e361da97763 105 */
TMBOY 47:9e361da97763 106 typedef void (*get_url_cb_t)(const char *url);
TMBOY 47:9e361da97763 107
TMBOY 47:9e361da97763 108 #define HC_CONTENT_TYPE_LEN_MAX 32 // the max length of "content type" section
TMBOY 47:9e361da97763 109
TMBOY 47:9e361da97763 110 typedef struct http_client_c {
TMBOY 47:9e361da97763 111 const char** pps_custom_headers;
TMBOY 47:9e361da97763 112 size_t sz_headers_count;
TMBOY 47:9e361da97763 113 int n_http_response_code; // http response code
TMBOY 47:9e361da97763 114 socket_ops ops; // socket operations
TMBOY 47:9e361da97763 115 data_out_handler_cb data_hdlr_cb; // callback for output data
TMBOY 47:9e361da97763 116 void* p_data_hdlr_ctx; // users args for data_hdlr_cb
TMBOY 47:9e361da97763 117 int n_http_content_len; // http content length
TMBOY 47:9e361da97763 118 char p_http_content_type[HC_CONTENT_TYPE_LEN_MAX]; // http content type
TMBOY 47:9e361da97763 119 char* p_location; // http header "Location"
TMBOY 47:9e361da97763 120 //a callback to check that stopping http client getting data or not
TMBOY 47:9e361da97763 121 check_stop_notify_cb_t check_stop_notify_cb;
TMBOY 47:9e361da97763 122 reset_stop_notify_cb_t reset_stop_notify_cb; // reset stop notify flag
TMBOY 47:9e361da97763 123 get_url_cb_t get_url_cb; // get the url which is used by http to get data
TMBOY 47:9e361da97763 124 int n_is_chunked;
TMBOY 47:9e361da97763 125 size_t sz_chunk_size;
TMBOY 47:9e361da97763 126 char* p_chunk_buf;
TMBOY 47:9e361da97763 127 } http_client_c;
TMBOY 47:9e361da97763 128
TMBOY 47:9e361da97763 129 typedef struct _http_client_statistic_s {
TMBOY 47:9e361da97763 130 size_t download_data_size;
TMBOY 47:9e361da97763 131 size_t upload_data_size;
TMBOY 47:9e361da97763 132 int error_count;
TMBOY 47:9e361da97763 133 int last_error_code;
TMBOY 47:9e361da97763 134 size_t recv_packet_longest_time;
TMBOY 47:9e361da97763 135 } http_client_statistic_t;
TMBOY 47:9e361da97763 136
TMBOY 47:9e361da97763 137 int http_client_init(http_client_c* p_client);
TMBOY 47:9e361da97763 138 void http_client_destroy(http_client_c* p_client);
TMBOY 47:9e361da97763 139 int http_client_init_socket_ops(http_client_c* p_client, socket_ops* p_ops, void* socket_args);
TMBOY 47:9e361da97763 140 void http_client_reg_data_hdlr_cb(http_client_c* p_client, data_out_handler_cb data_hdlr_cb,
TMBOY 47:9e361da97763 141 void* p_usr_ctx);
TMBOY 47:9e361da97763 142 void http_client_reg_cb_to_get_url(http_client_c* p_client, get_url_cb_t cb);
TMBOY 47:9e361da97763 143 void http_client_set_cust_headers(http_client_c* p_client, const char** headers, size_t pairs);
TMBOY 47:9e361da97763 144 e_http_result http_client_get(http_client_c* p_client, const char* url);
TMBOY 47:9e361da97763 145 int http_client_get_resp_code(http_client_c* p_client);
TMBOY 47:9e361da97763 146 void http_client_reg_stop_notify_cb(http_client_c* p_client, check_stop_notify_cb_t chk_stp_cb,
TMBOY 47:9e361da97763 147 reset_stop_notify_cb_t rst_stp_cb);
TMBOY 47:9e361da97763 148 /**
TMBOY 47:9e361da97763 149 * FUNC:
TMBOY 47:9e361da97763 150 * int get_http_statistic(http_client_c *p_client, socket_ops *p_ops, void *socket_args)
TMBOY 47:9e361da97763 151 *
TMBOY 47:9e361da97763 152 * DESC:
TMBOY 47:9e361da97763 153 * used to get statistic info of http module
TMBOY 47:9e361da97763 154 *
TMBOY 47:9e361da97763 155 * PARAM:
TMBOY 47:9e361da97763 156 * @param[out] statistic_result: buffer to accept the statistic info
TMBOY 47:9e361da97763 157 *
TMBOY 47:9e361da97763 158 */
TMBOY 47:9e361da97763 159 void get_http_statistic(http_client_statistic_t* statistic_result);
TMBOY 47:9e361da97763 160
TMBOY 47:9e361da97763 161 #ifdef __cplusplus
TMBOY 47:9e361da97763 162 }
TMBOY 47:9e361da97763 163 #endif
TMBOY 47:9e361da97763 164
TMBOY 47:9e361da97763 165 #endif //BAIDU_IOT_TINYDU_IOT_OS_SRC_HTTP_CLIENT_BAIDU_HTTP_CLIENT_C_H
TMBOY 47:9e361da97763 166