HTTP File Downloader Example.
Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
#include <errno.h>
#include "asf.h"
#include "main.h"
#include "stdio_serial.h"
#include "driver/include/m2m_wifi.h"
#include "socket/include/socket.h"
#include "iot/http/http_client.h"
Macros | |
#define | ADDITION_SIZE (NUMBRING_MAX + 1) /* '-' character is added before the number. */ |
#define | NUMBRING_MAX (3) |
#define | STRING_EOL "\r\n" |
#define | STRING_HEADER |
Functions | |
static void | add_state (download_state mask) |
Add state parameter at download processing state. More... | |
static void | clear_state (download_state mask) |
Clear state parameter at download processing state. More... | |
static void | configure_console (void) |
Configure UART console. More... | |
static void | configure_http_client (void) |
Configure HTTP client module. More... | |
static void | configure_timer (void) |
Configure Timer module. More... | |
static void | http_client_callback (struct http_client_module *module_inst, int type, union http_client_data *data) |
Callback of the HTTP client. More... | |
static void | init_state (void) |
Initialize download state to not ready. More... | |
static void | init_storage (void) |
Initialize SD/MMC storage. More... | |
static bool | is_exist_file (FIL *fp, const char *file_path_name) |
File existing check. More... | |
static bool | is_state_set (download_state mask) |
File download processing state check. More... | |
int | main (void) |
Main application function. More... | |
static bool | rename_to_unique (FIL *fp, char *file_path_name, uint8_t max_len) |
Make to unique file name. More... | |
static void | resolve_cb (uint8_t *pu8DomainName, uint32_t u32ServerIP) |
Callback for the gethostbyname function (DNS Resolution callback). More... | |
static void | socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg) |
Callback to get the data from socket. More... | |
static void | start_download (void) |
Start file download via HTTP connection. More... | |
static void | store_file_packet (char *data, uint32_t length) |
Store received packet to file. More... | |
static void | wifi_cb (uint8_t u8MsgType, void *pvMsg) |
Callback to get the Wi-Fi status update. More... | |
Variables | |
static struct usart_module | cdc_uart_module |
UART module for debug. More... | |
static download_state | down_state = NOT_READY |
File download processing state. More... | |
static FATFS | fatfs |
SD/MMC mount. More... | |
static FIL | file_object |
File pointer for file download. More... | |
struct http_client_module | http_client_module_inst |
Instance of HTTP client module. More... | |
static uint32_t | http_file_size = 0 |
Http content length. More... | |
static uint32_t | received_file_size = 0 |
Receiving content length. More... | |
static char | save_file_name [MAIN_MAX_FILE_NAME_LENGTH+1] = "0:" |
File name to download. More... | |
struct sw_timer_module | swt_module_inst |
Instance of Timer module. More... | |
#define ADDITION_SIZE (NUMBRING_MAX + 1) /* '-' character is added before the number. */ |
Referenced by rename_to_unique().
#define NUMBRING_MAX (3) |
Referenced by rename_to_unique().
#define STRING_EOL "\r\n" |
#define STRING_HEADER |
Referenced by main().
|
static |
Add state parameter at download processing state.
[in] | mask | Check download_state. |
References down_state.
Referenced by http_client_callback(), init_storage(), store_file_packet(), and wifi_cb().
|
static |
Clear state parameter at download processing state.
[in] | mask | Check download_state. |
References down_state.
Referenced by http_client_callback(), and wifi_cb().
|
static |
Configure UART console.
References usart_config::baudrate, cdc_uart_module, usart_config::mux_setting, usart_config::pinmux_pad0, usart_config::pinmux_pad1, usart_config::pinmux_pad2, usart_config::pinmux_pad3, usart_enable(), and usart_get_config_defaults().
|
static |
Configure HTTP client module.
References http_client_callback(), http_client_get_config_defaults(), http_client_init(), http_client_module_inst, http_client_register_callback(), MAIN_BUFFER_MAX_SIZE, printf, http_client_config::recv_buffer_size, ret, swt_module_inst, and http_client_config::timer_inst.
Referenced by main().
|
static |
Configure Timer module.
References sw_timer_enable(), sw_timer_get_config_defaults(), sw_timer_init(), and swt_module_inst.
Referenced by main().
|
static |
Callback of the HTTP client.
[in] | module_inst | Module instance of HTTP client module. |
[in] | type | Type of event. |
[in] | data | Data structure of the event. http_client_data |
References add_state(), CANCELED, clear_state(), COMPLETED, http_client_data_recv_response::content, http_client_data_recv_response::content_length, http_client_data_recv_chunked_data::data, http_client_data::disconnected, DOWNLOADING, f_close(), GET_REQUESTED, HTTP_CLIENT_CALLBACK_DISCONNECTED, HTTP_CLIENT_CALLBACK_RECV_CHUNKED_DATA, HTTP_CLIENT_CALLBACK_RECV_RESPONSE, HTTP_CLIENT_CALLBACK_REQUESTED, HTTP_CLIENT_CALLBACK_SOCK_CONNECTED, http_file_size, http_client_data_recv_chunked_data::is_complete, is_state_set(), http_client_data_recv_chunked_data::length, MAIN_BUFFER_MAX_SIZE, printf, http_client_data_disconnected::reason, received_file_size, http_client_data::recv_chunked_data, http_client_data::recv_response, http_client_data_recv_response::response_code, start_download(), and store_file_packet().
Referenced by configure_http_client().
|
static |
|
static |
Initialize SD/MMC storage.
References add_state(), CTRL_FAIL, CTRL_GOOD, CTRL_NO_PRESENT, f_mount(), FR_INVALID_DRIVE, LUN_ID_SD_MMC_0_MEM, printf, sd_mmc_check(), sd_mmc_init(), sd_mmc_test_unit_ready(), status, and STORAGE_READY.
Referenced by main().
File existing check.
[in] | fp | The file pointer to check. |
[in] | file_path_name | The file name to check. |
References f_close(), f_open(), FA_OPEN_EXISTING, FR_OK, NULL, and ret.
Referenced by rename_to_unique().
|
inlinestatic |
File download processing state check.
[in] | mask | Check download_state. |
References down_state.
Referenced by http_client_callback(), main(), start_download(), store_file_packet(), and wifi_cb().
int main | ( | void | ) |
Main application function.
Application entry point.
References CANCELED, COMPLETED, configure_console(), configure_http_client(), configure_timer(), init_state(), init_storage(), is_state_set(), M2M_SUCCESS, M2M_WIFI_CH_ALL, m2m_wifi_connect(), m2m_wifi_handle_events(), m2m_wifi_init(), MAIN_WLAN_AUTH, MAIN_WLAN_PSK, MAIN_WLAN_SSID, nm_bsp_init(), NULL, tstrWifiInitParam::pfAppWifiCb, printf, registerSocketCallback(), resolve_cb(), ret, socket_cb(), socketInit(), STRING_HEADER, sw_timer_task(), swt_module_inst, system_init(), and wifi_cb().
Make to unique file name.
[in] | fp | The file pointer to check. |
[out] | file_path_name | The file name change to uniquely and changed name is returned to this buffer. |
[in] | max_len | Maximum file name length. |
References ADDITION_SIZE, count, i, is_exist_file(), MAIN_MAX_FILE_EXT_LENGTH, MAIN_MAX_FILE_NAME_LENGTH, MAIN_ZERO_FMT, NULL, NUMBRING_MAX, and strncpy().
Referenced by store_file_packet().
|
static |
Callback for the gethostbyname function (DNS Resolution callback).
[in] | pu8DomainName | Domain name of the host. |
[in] | u32ServerIP | Server IPv4 address encoded in NW byte order format. If it is Zero, then the DNS resolution failed. |
References http_client_socket_resolve_handler(), IPV4_BYTE, and printf.
Referenced by main().
|
static |
Callback to get the data from socket.
[in] | sock | socket handler. |
[in] | u8Msg | socket event type. Possible values are:
|
[in] | pvMsg | is a pointer to message structure. Existing types are: |
References http_client_socket_event_handler().
Referenced by main().
|
static |
Start file download via HTTP connection.
References DOWNLOADING, GET_REQUESTED, http_client_module_inst, http_client_send_request(), HTTP_METHOD_GET, is_state_set(), MAIN_HTTP_FILE_URL, NULL, printf, STORAGE_READY, and WIFI_CONNECTED.
Referenced by http_client_callback(), and wifi_cb().
|
static |
Store received packet to file.
[in] | data | Packet data. |
[in] | length | Packet data length. |
References add_state(), CANCELED, COMPLETED, DOWNLOADING, f_close(), f_open(), f_write(), FA_CREATE_ALWAYS, FA_WRITE, FR_OK, http_file_size, is_state_set(), LUN_ID_SD_MMC_0_MEM, MAIN_HTTP_FILE_URL, MAIN_MAX_FILE_NAME_LENGTH, NULL, port_pin_set_output_level(), printf, received_file_size, rename_to_unique(), ret, and save_file_name.
Referenced by http_client_callback().
|
static |
Callback to get the Wi-Fi status update.
[in] | u8MsgType | type of Wi-Fi notification. Possible types are:
|
[in] | pvMsg | A pointer to a buffer containing the notification parameters (if any). It should be casted to the correct data type corresponding to the notification type. Existing types are:
|
References add_state(), clear_state(), DOWNLOADING, f_close(), GET_REQUESTED, is_state_set(), M2M_WIFI_CH_ALL, m2m_wifi_connect(), M2M_WIFI_CONNECTED, M2M_WIFI_DISCONNECTED, M2M_WIFI_REQ_DHCP_CONF, m2m_wifi_request_dhcp_client(), M2M_WIFI_RESP_CON_STATE_CHANGED, MAIN_WLAN_AUTH, MAIN_WLAN_PSK, MAIN_WLAN_SSID, printf, start_download(), tstrM2mWifiStateChanged::u8CurrState, and WIFI_CONNECTED.
Referenced by main().
|
static |
UART module for debug.
|
static |
File download processing state.
Referenced by add_state(), clear_state(), init_state(), and is_state_set().
|
static |
SD/MMC mount.
|
static |
File pointer for file download.
struct http_client_module http_client_module_inst |
Instance of HTTP client module.
|
static |
Http content length.
Referenced by http_client_callback(), and store_file_packet().
|
static |
Receiving content length.
Referenced by http_client_callback(), and store_file_packet().
|
static |
File name to download.
Referenced by store_file_packet().
struct sw_timer_module swt_module_inst |
Instance of Timer module.