Microchip® Advanced Software Framework

exosite_demo/iot/http/http_client.c File Reference

HTTP client service.

Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.

#include "iot/http/http_client.h"
#include <string.h>
#include "driver/include/m2m_wifi.h"
#include "iot/stream_writer.h"
#include <stdio.h>
#include <errno.h>

Macros

#define DEFAULT_USER_AGENT   "atmel/1.0.2"
 
#define HTTP_CHUNKED_MAX_LENGTH   3 /*TCP MTU is 1400(0x578) */
 
#define MIN_SEND_BUFFER_SIZE   18 + HTTP_MAX_URI_LENGTH /* DELETE {URI} HTTP/1.1\r\n */
 

Enumerations

enum  http_client_req_state {
  STATE_INIT = 0,
  STATE_TRY_SOCK_CONNECT,
  STATE_SOCK_CONNECTED,
  STATE_REQ_SEND_HEADER,
  STATE_REQ_SEND_ENTITY,
  STATE_INIT = 0,
  STATE_TRY_SOCK_CONNECT,
  STATE_SOCK_CONNECTED,
  STATE_REQ_SEND_HEADER,
  STATE_REQ_SEND_ENTITY,
  STATE_INIT = 0,
  STATE_TRY_SOCK_CONNECT,
  STATE_SOCK_CONNECTED,
  STATE_REQ_SEND_HEADER,
  STATE_REQ_SEND_ENTITY
}
 
enum  http_client_resp_state {
  STATE_PARSE_HEADER,
  STATE_PARSE_ENTITY,
  STATE_PARSE_HEADER,
  STATE_PARSE_ENTITY,
  STATE_PARSE_HEADER,
  STATE_PARSE_ENTITY
}
 

Functions

void _http_client_clear_conn (struct http_client_module *const module, int reason)
 Clear the HTTP instance. More...
 
int _http_client_handle_entity (struct http_client_module *const module)
 parse the HTTP entity part. More...
 
int _http_client_handle_header (struct http_client_module *const module)
 Parse the HTTP header part. More...
 
int _http_client_handle_response (struct http_client_module *const module)
 Parse the input data from the socket. More...
 
void _http_client_move_buffer (struct http_client_module *const module, char *base)
 Move remain part of the buffer to the start position in the buffer. More...
 
static void _http_client_read_chuked_entity (struct http_client_module *const module)
 
int _http_client_read_wait (void *module, char *buffer, size_t buffer_len)
 
void _http_client_recv_packet (struct http_client_module *const module)
 Start receiving HTTP packet. More...
 
void _http_client_recved_packet (struct http_client_module *const module, int read_len)
 Perform the post processing of the received packet. More...
 
void _http_client_request (struct http_client_module *const module)
 Send HTTP request packet. More...
 
int _http_client_send_wait (void *module, char *buffer, size_t buffer_len)
 Sending the packet in blocking mode. More...
 
static int _hwerr_to_stderr (int err)
 change HW error type to standard error. More...
 
static int _is_ip (const char *host)
 
int http_client_close (struct http_client_module *const module)
 Force close HTTP connection. More...
 
int http_client_deinit (struct http_client_module *const module)
 Terminate HTTP client service. More...
 
void http_client_get_config_defaults (struct http_client_config *const config)
 Get default configuration of HTTP client module. More...
 
int http_client_init (struct http_client_module *const module, struct http_client_config *config)
 Initialize HTTP client service. More...
 
int http_client_register_callback (struct http_client_module *const module, http_client_callback_t callback)
 Register and enable the callback. More...
 
int http_client_send_request (struct http_client_module *const module, const char *url, enum http_method method, struct http_entity *const entity, const char *ext_header)
 Event handler of gethostbyname. More...
 
void http_client_socket_event_handler (SOCKET sock, uint8_t msg_type, void *msg_data)
 Event handler of socket event. More...
 
void http_client_socket_resolve_handler (uint8_t *doamin_name, uint32_t server_ip)
 Event handler of gethostbyname. More...
 
void http_client_timer_callback (struct sw_timer_module *const module, int timer_id, void *context, int period)
 Timer callback entry of HTTP client. More...
 
int http_client_unregister_callback (struct http_client_module *const module)
 Unregister callback. More...
 

Variables

static struct http_client_modulemodule_ref_inst [TCP_SOCK_MAX] = {NULL,}
 Global reference of HTTP client instance. More...
 

#define DEFAULT_USER_AGENT   "atmel/1.0.2"
#define HTTP_CHUNKED_MAX_LENGTH   3 /*TCP MTU is 1400(0x578) */

Referenced by _http_client_request().

#define MIN_SEND_BUFFER_SIZE   18 + HTTP_MAX_URI_LENGTH /* DELETE {URI} HTTP/1.1\r\n */

Enumerator
STATE_INIT 
STATE_TRY_SOCK_CONNECT 
STATE_SOCK_CONNECTED 
STATE_REQ_SEND_HEADER 
STATE_REQ_SEND_ENTITY 
STATE_INIT 
STATE_TRY_SOCK_CONNECT 
STATE_SOCK_CONNECTED 
STATE_REQ_SEND_HEADER 
STATE_REQ_SEND_ENTITY 
STATE_INIT 
STATE_TRY_SOCK_CONNECT 
STATE_SOCK_CONNECTED 
STATE_REQ_SEND_HEADER 
STATE_REQ_SEND_ENTITY 
Enumerator
STATE_PARSE_HEADER 
STATE_PARSE_ENTITY 
STATE_PARSE_HEADER 
STATE_PARSE_ENTITY 
STATE_PARSE_HEADER 
STATE_PARSE_ENTITY 

int _http_client_handle_response ( struct http_client_module *const  module)

Parse the input data from the socket.

Parameters
[in]moduleModule instance of HTTP.

References _http_client_handle_entity(), _http_client_handle_header(), http_client_module::resp, http_client_resp::state, STATE_PARSE_ENTITY, and STATE_PARSE_HEADER.

Referenced by _http_client_recved_packet().

void _http_client_move_buffer ( struct http_client_module *const  module,
char *  base 
)

Move remain part of the buffer to the start position in the buffer.

Parameters
[in]moduleModule instance of HTTP.
[in]baseCurrent position of the buffer pointer.

References buffer, http_client_module::config, http_client_config::recv_buffer, and http_client_module::recved_size.

Referenced by _http_client_handle_entity(), _http_client_handle_header(), and _http_client_read_chuked_entity().

int _http_client_read_wait ( void *  module,
char *  buffer,
size_t  buffer_len 
)
void _http_client_recv_packet ( struct http_client_module *const  module)
void _http_client_recved_packet ( struct http_client_module *const  module,
int  read_len 
)

Perform the post processing of the received packet.

Parameters
[in]moduleModule instance of MQTT.
[in]read_lenRead size from the recv function.

References _http_client_handle_response(), http_client_module::config, http_client_module::recved_size, sw_timer_disable_callback(), http_client_config::timeout, http_client_module::timer_id, and http_client_config::timer_inst.

Referenced by http_client_socket_event_handler().

int _http_client_send_wait ( void *  module,
char *  buffer,
size_t  buffer_len 
)

Sending the packet in blocking mode.

Parameters
[in]moduleModule instance of HTTP.
[in]bufferBuffer to be sent.
[in]buffer_lenBuffer size.
Returns
0 Function success.
otherwise Function failed.

References http_client_module::config, i, m2m_wifi_handle_events(), NULL, printf, http_client_module::req, result, send(), http_client_module::sending, http_client_module::sock, http_client_req::state, STATE_SOCK_CONNECTED, sw_timer_task(), and http_client_config::timer_inst.

Referenced by _http_client_request().

static int _is_ip ( const char *  host)
static
void http_client_timer_callback ( struct sw_timer_module *const  module,
int  timer_id,
void *  context,
int  period 
)

Timer callback entry of HTTP client.

Parameters
[in]timer_idID of timer.
[in]contextPrivate data of timer.
[in]contextPeriod of timer handle.

References _http_client_clear_conn(), module_inst, and NULL.

Referenced by http_client_init().

struct http_client_module* module_ref_inst[TCP_SOCK_MAX] = {NULL,}
static

Global reference of HTTP client instance.

Socket callback interface has not user private data. So it needed reference to HTTP client module instance.