Microchip® Advanced Software Framework

main21.c File Reference

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
Value:
"-- Certificate downloader via OTA (HTTPS) example --"STRING_EOL \
"-- "BOARD_NAME " --"STRING_EOL \
"-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
#define BOARD_NAME
Name string macro.
Definition: samd21_xplained_pro.h:65
#define STRING_EOL
Definition: main21.c:150

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 certificate_download(), http_client_callback(), store_file_packet(), store_file_packet_complete(), and wifi_cb().

static uint8 certificate_download ( void  )
static
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 certificate_download(), http_client_callback(), store_file_packet_complete(), 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

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().

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 certificate_download(), http_client_callback(), main(), start_download(), store_file_packet(), and wifi_cb().

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(), and IPV4_BYTE.

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 store_file_packet ( char *  data,
uint32_t  length 
)
static

Store received packet to file.

Parameters
[in]dataPacket data.
[in]lengthPacket data length.

References add_state(), certificate_bufer, DOWNLOADING, http_file_size, is_state_set(), NULL, received_file_size, store_file_packet_complete(), and total_size.

Referenced by http_client_callback().

static void store_file_packet_complete ( void  )
static

Complete file received.

Parameters
[in]dataPacket data.
[in]lengthPacket data length.

References add_state(), cert_idx, cert_start_idx, certificate_bufer, clear_state(), COMPLETED, HTTP_DOWNLOAD_INITIATED, tstrFileInfo::pu8FileData, received_file_size, and tstrFileInfo::u32FileSz.

Referenced by http_client_callback(), and store_file_packet().

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, 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, tstrM2mWifiStateChanged::u8CurrState, and WIFI_CONNECTED.

Referenced by main().

struct usart_module cdc_uart_module
static

UART module for debug.

Referenced by configure_console().

uint8 cert_idx = 0
static
uint32_t cert_start_idx =0
static
uint8_t certificate_bufer[CERTIFICATES_BUFFER_SIZE]
static

Stores all the certificates.

Referenced by store_file_packet(), and store_file_packet_complete().

download_state down_state = NOT_READY
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.

Referenced by configure_http_client(), and start_download().

uint32_t http_file_size = 0
static

Http content length.

Referenced by http_client_callback(), and store_file_packet().

char http_url[MAIN_MAX_FILE_NAME_LENGTH]
static

http URL

Referenced by certificate_download(), and start_download().

uint32_t received_file_size = 0
static

Receiving content length.

Referenced by http_client_callback(), store_file_packet(), and store_file_packet_complete().

struct sw_timer_module swt_module_inst

Instance of Timer module.

Referenced by configure_http_client(), configure_timer(), and main().

uint32_t total_size = 0
static

Certificates size.

Referenced by m2m_ssl_cb(), and store_file_packet().