Certificate Downloader via OTA(HTTPS) Example.
Copyright (c) 2017-2021 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"
#include "root_tls_cert/root_setup.h"
#include "root_tls_cert/tls_setup.h"
#include "programmer/programmer_apis.h"
Macros | |
#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 sint8 | burn_certificates (void) |
Program certificates to WINC1500 memory. More... | |
static uint8 | certificate_download (void) |
initiate https download and write the cerficates to WINC. 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) |
Checking the USART buffer. More... | |
static bool | is_state_set (download_state mask) |
File download processing state check. More... | |
int | main (void) |
Main application function. 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 | store_file_packet_complete (void) |
Complete file received. 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 uint8 | cert_idx = 0 |
static uint32_t | cert_start_idx =0 |
static uint8_t | certificate_bufer [CERTIFICATES_BUFFER_SIZE] |
Stores all the certificates. More... | |
static download_state | down_state = NOT_READY |
File download processing state. 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 char | http_url [MAIN_MAX_FILE_NAME_LENGTH] |
http URL More... | |
static uint32_t | received_file_size = 0 |
Receiving content length. More... | |
tstrFileInfo | root_tls_certs [NUM_OF_ROOT_TLS_CHAIN_CERTIFICATES] |
struct sw_timer_module | swt_module_inst |
Instance of Timer module. More... | |
static uint32_t | total_size = 0 |
Certificates size. More... | |
#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 certificate_download(), http_client_callback(), store_file_packet(), store_file_packet_complete(), and wifi_cb().
|
static |
Program certificates to WINC1500 memory.
References m2m_wifi_deinit(), m2m_wifi_download_mode(), NULL, NUM_OF_ROOT_TLS_CHAIN_CERTIFICATES, certFileInfo::numOfChainCert, printf, ret, ROOT_CERT, root_tls_certs_name, TLS_ECC_CERT, TLS_RSA_CERT, WriteRootCertificate(), and WriteTlsCertificate().
Referenced by certificate_download().
|
static |
initiate https download and write the cerficates to WINC.
References add_state(), burn_certificates(), cert_idx, clear_state(), DOWNLOADING, GET_REQUESTED, HTTP_DOWNLOAD_INITIATED, http_url, is_state_set(), M2M_SUCCESS, MAIN_HTTP_FILE_URL, NUM_OF_ROOT_TLS_CHAIN_CERTIFICATES, printf, root_tls_certs_name, start_download(), and WIFI_CONNECTED.
Referenced by main().
|
static |
Clear state parameter at download processing state.
[in] | mask | Check download_state. |
References down_state.
Referenced by certificate_download(), http_client_callback(), store_file_packet_complete(), 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, http_client_config::timer_inst, and http_client_config::tls.
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, GET_REQUESTED, HTTP_CLIENT_CALLBACK_DISCONNECTED, HTTP_CLIENT_CALLBACK_RECV_CHUNKED_DATA, HTTP_CLIENT_CALLBACK_RECV_NO_CONTENT_LENGTH_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(), store_file_packet(), and store_file_packet_complete().
Referenced by configure_http_client().
|
static |
Checking the USART buffer.
Finding the new line character(
or
) in the USART buffer. If buffer was overflowed, Sending the buffer. Initialize download state to not ready.
References down_state, and NOT_READY.
Referenced by main().
|
inlinestatic |
File download processing state check.
[in] | mask | Check download_state. |
References down_state.
Referenced by certificate_download(), http_client_callback(), main(), start_download(), store_file_packet(), and wifi_cb().
int main | ( | void | ) |
Main application function.
Application entry point.
References CANCELED, certificate_download(), configure_console(), configure_http_client(), configure_timer(), init_state(), 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().
|
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 http_client_module::config, DOWNLOADING, GET_REQUESTED, http_client_module_inst, http_client_send_request(), HTTP_METHOD_GET, http_url, is_state_set(), MAIN_HTTP_PORT_NUMBER, NULL, http_client_config::port, printf, and WIFI_CONNECTED.
Referenced by certificate_download(), and http_client_callback().
|
static |
Store received packet to file.
[in] | data | Packet data. |
[in] | length | Packet data length. |
References add_state(), certificate_bufer, DOWNLOADING, http_file_size, is_state_set(), length, NULL, printf, received_file_size, store_file_packet_complete(), and total_size.
Referenced by http_client_callback().
|
static |
Complete file received.
[in] | data | Packet data. |
[in] | length | Packet data length. |
References add_state(), cert_idx, cert_start_idx, certificate_bufer, clear_state(), COMPLETED, HTTP_DOWNLOAD_INITIATED, printf, tstrFileInfo::pu8FileData, received_file_size, and tstrFileInfo::u32FileSz.
Referenced by http_client_callback(), and store_file_packet().
|
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, 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, tstrM2mWifiStateChanged::u8CurrState, and WIFI_CONNECTED.
Referenced by main().
|
static |
UART module for debug.
|
static |
Referenced by certificate_download(), and store_file_packet_complete().
|
static |
Referenced by store_file_packet_complete().
|
static |
Stores all the certificates.
Referenced by store_file_packet(), and store_file_packet_complete().
|
static |
File download processing state.
Referenced by add_state(), clear_state(), init_state(), and is_state_set().
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 |
http URL
Referenced by certificate_download(), and start_download().
|
static |
Receiving content length.
Referenced by http_client_callback(), store_file_packet(), and store_file_packet_complete().
tstrFileInfo root_tls_certs[NUM_OF_ROOT_TLS_CHAIN_CERTIFICATES] |
struct sw_timer_module swt_module_inst |
Instance of Timer module.
|
static |
Certificates size.
Referenced by at_ble_service_define(), dma_abort_job(), DMAC_Handler(), m2m_ssl_cb(), and store_file_packet().