Microchip® Advanced Software Framework

winc1500/http_downloader_example/main21.c File Reference

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
Value:
"-- HTTP file downloader example --"STRING_EOL \
"-- "BOARD_NAME " --"STRING_EOL \
"-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
#define BOARD_NAME
Definition: app_init.h:140
#define STRING_EOL
Definition: winc1500/http_downloader_example/main21.c:119

Referenced by main().

static void add_state ( download_state  mask)
static

Add state parameter at download processing state.

Parameters
[in]maskCheck download_state.

References down_state.

Referenced by http_client_callback(), init_storage(), store_file_packet(), and wifi_cb().

static void clear_state ( download_state  mask)
static

Clear state parameter at download processing state.

Parameters
[in]maskCheck download_state.

References down_state.

Referenced by http_client_callback(), and wifi_cb().

static void configure_timer ( void  )
static

Configure Timer module.

References sw_timer_enable(), sw_timer_get_config_defaults(), sw_timer_init(), and swt_module_inst.

Referenced by main().

static void init_state ( void  )
static

Initialize download state to not ready.

References down_state, and NOT_READY.

Referenced by main().

static void init_storage ( void  )
static
static bool is_exist_file ( FIL fp,
const char *  file_path_name 
)
static

File existing check.

Parameters
[in]fpThe file pointer to check.
[in]file_path_nameThe file name to check.
Returns
true if this file name is exist, false otherwise.

References f_close(), f_open(), FA_OPEN_EXISTING, FR_OK, NULL, and ret.

Referenced by rename_to_unique().

static bool is_state_set ( download_state  mask)
inlinestatic

File download processing state check.

Parameters
[in]maskCheck download_state.
Returns
true if this state is set, false otherwise.

References down_state.

Referenced by http_client_callback(), main(), start_download(), store_file_packet(), and wifi_cb().

static bool rename_to_unique ( FIL fp,
char *  file_path_name,
uint8_t  max_len 
)
static

Make to unique file name.

Parameters
[in]fpThe file pointer to check.
[out]file_path_nameThe file name change to uniquely and changed name is returned to this buffer.
[in]max_lenMaximum file name length.
Returns
true if this file name is unique, false otherwise.

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 void resolve_cb ( uint8_t *  pu8DomainName,
uint32_t  u32ServerIP 
)
static

Callback for the gethostbyname function (DNS Resolution callback).

Parameters
[in]pu8DomainNameDomain name of the host.
[in]u32ServerIPServer 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 void socket_cb ( SOCKET  sock,
uint8_t  u8Msg,
void *  pvMsg 
)
static

Callback to get the data from socket.

Parameters
[in]socksocket handler.
[in]u8Msgsocket event type. Possible values are:
  • SOCKET_MSG_BIND
  • SOCKET_MSG_LISTEN
  • SOCKET_MSG_ACCEPT
  • SOCKET_MSG_CONNECT
  • SOCKET_MSG_RECV
  • SOCKET_MSG_SEND
  • SOCKET_MSG_SENDTO
  • SOCKET_MSG_RECVFROM
[in]pvMsgis a pointer to message structure. Existing types are:

References http_client_socket_event_handler().

Referenced by main().

static void start_download ( void  )
static
static void store_file_packet ( char *  data,
uint32_t  length 
)
static
static void wifi_cb ( uint8_t  u8MsgType,
void *  pvMsg 
)
static

Callback to get the Wi-Fi status update.

Parameters
[in]u8MsgTypetype of Wi-Fi notification. Possible types are:
[in]pvMsgA 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().

struct usart_module cdc_uart_module
static

UART module for debug.

download_state down_state = NOT_READY
static

File download processing state.

Referenced by add_state(), clear_state(), init_state(), and is_state_set().

FATFS fatfs
static

SD/MMC mount.

FIL file_object
static

File pointer for file download.

struct http_client_module http_client_module_inst

Instance of HTTP client module.

uint32_t http_file_size = 0
static

Http content length.

Referenced by http_client_callback(), and store_file_packet().

uint32_t received_file_size = 0
static

Receiving content length.

Referenced by http_client_callback(), and store_file_packet().

char save_file_name[MAIN_MAX_FILE_NAME_LENGTH+1] = "0:"
static

File name to download.

Referenced by store_file_packet().

struct sw_timer_module swt_module_inst

Instance of Timer module.