HTTP File Downloader Example Implementation.
Copyright (c) 2018 Microchip Technology Inc. and its subsidiaries.
#include <errno.h>
#include "asf.h"
#include "httpdownloader.h"
#include "FreeRTOS.h"
#include "iot/http/http_client.h"
#include "osprintf.h"
#include "lwip/ip.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/sockets.h"
#include "os/include/net_init.h"
#include "os/include/m2m_wifi_ex.h"
#include "tinyservices.h"
#include "conf_wilc.h"
#include "ota_fw_update/ota_fw_update.h"
#include <string.h>
#include <stdio.h>
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 int | configure_http_client (void) |
Configure HTTP client module. More... | |
static void | configure_timer (void) |
Configure Timer module. More... | |
static int | dismantle_http_client (void) |
close HTTP client 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... | |
int | http_downloader (char *fileURL) |
http downloader function. More... | |
static void | init_state (void) |
Initialize download state to not ready. More... | |
static bool | is_state_set (download_state mask) |
File download processing state check. More... | |
void | setFirmwareDownload (int val) |
static void | start_download (char *fileURL) |
Start file download via HTTP connection. More... | |
static void | store_file_packet (char *data, uint32_t length) |
Ignore data, but calculate the total received size TODO: Store received packet to file. More... | |
Variables | |
static download_state | down_state = NOT_READY |
File download processing state. More... | |
char * | gfileURL |
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 int | is_firmware = 0 |
File name to download. More... | |
static uint32_t | received_file_size = 0 |
Receiving content length. More... | |
struct sw_timer_module | swt_module_inst |
Instance of Timer module. More... | |
|
static |
Add state parameter at download processing state.
[in] | mask | Check download_state. |
References down_state.
Referenced by http_client_callback(), and store_file_packet().
|
static |
Clear state parameter at download processing state.
[in] | mask | Check download_state. |
References down_state.
Referenced by http_client_callback().
|
static |
Configure HTTP client module.
References FAILURE, http_client_callback(), http_client_get_config_defaults(), http_client_init(), http_client_module_inst, http_client_register_callback(), MAIN_BUFFER_MAX_SIZE, osprintf(), http_client_config::recv_buffer_size, SUCCESS, swt_module_inst, and http_client_config::timer_inst.
Referenced by http_downloader().
|
static |
Configure Timer module.
References sw_timer_enable(), sw_timer_get_config_defaults(), sw_timer_init(), and swt_module_inst.
Referenced by http_downloader().
|
static |
close HTTP client module.
References FAILURE, http_client_close(), http_client_deinit(), http_client_module_inst, osprintf(), and SUCCESS.
Referenced by http_downloader().
|
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, GET_REQUESTED, gfileURL, 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, osprintf(), 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().
int http_downloader | ( | char * | fileURL | ) |
http downloader function.
References COMPLETED, configure_http_client(), configure_timer(), dismantle_http_client(), FAILURE, gfileURL, init_state(), is_state_set(), pvPortMalloc(), start_download(), SUCCESS, and vPortFree().
Referenced by wilc_task_1().
|
static |
Initialize download state to not ready.
References down_state, and NOT_READY.
Referenced by http_downloader().
|
inlinestatic |
File download processing state check.
[in] | mask | Check download_state. |
References down_state.
Referenced by http_client_callback(), http_downloader(), start_download(), and store_file_packet().
void setFirmwareDownload | ( | int | val | ) |
References is_firmware.
|
static |
Start file download via HTTP connection.
References CANCELED, COMPLETED, DOWNLOADING, GET_REQUESTED, http_client_module_inst, http_client_recv_response(), http_client_send_request(), HTTP_METHOD_GET, is_state_set(), NULL, and osprintf().
Referenced by http_client_callback(), and http_downloader().
|
static |
Ignore data, but calculate the total received size TODO: Store received packet to file.
[in] | data | Packet data. |
[in] | length | Packet data length. |
References add_state(), CANCELED, COMPLETED, DOWNLOADING, http_file_size, is_firmware, is_state_set(), NULL, osprintf(), received_file_size, and SUCCESS.
Referenced by http_client_callback().
|
static |
File download processing state.
Referenced by add_state(), clear_state(), init_state(), and is_state_set().
char* gfileURL |
Referenced by http_client_callback(), and http_downloader().
struct http_client_module http_client_module_inst |
Instance of HTTP client module.
Referenced by configure_http_client(), dismantle_http_client(), and start_download().
|
static |
Http content length.
Referenced by http_client_callback(), and store_file_packet().
|
static |
File name to download.
Referenced by setFirmwareDownload(), and store_file_packet().
|
static |
Receiving content length.
Referenced by http_client_callback(), and store_file_packet().
struct sw_timer_module swt_module_inst |
Instance of Timer module.
Referenced by configure_http_client(), and configure_timer().