Microchip® Advanced Software Framework

main21.c File Reference

WINC1500 Send Email Example.

Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.

#include "asf.h"
#include "main.h"
#include "bsp/include/nm_bsp.h"
#include "driver/include/m2m_wifi.h"
#include "socket/include/socket.h"

Macros

#define STRING_EOL   "\r\n"
 
#define STRING_HEADER
 

Functions

static void close_socket (void)
 Close socket function. More...
 
static void configure_console (void)
 Configure UART console. More...
 
void ConvertToBase64 (char *pcOutStr, const char *pccInStr, int iLen)
 
static void generateBase64Key (char *input, char *basekey)
 Generates Base64 Key needed for authentication. More...
 
int main (void)
 Main application function. More...
 
static void resolve_cb (uint8_t *hostName, uint32_t hostIp)
 Callback function of IP address. More...
 
static void retry_smtp_server (void)
 Retry SMTP server function. More...
 
static int8_t smtpConnect (void)
 Creates and connects to an unsecure socket to be used for SMTP. More...
 
static void smtpSendRecv (long socket, char *cmd, char *cmdparam, char *respBuf)
 Sends an SMTP command and provides the server response. More...
 
static int8_t smtpStateHandler (void)
 SMTP state handler. More...
 
static void socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg)
 Callback function of TCP client socket. 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 bool gbConnectedWifi = false
 Wi-Fi status variable. More...
 
static bool gbHostIpByName = false
 Get host IP status variable. More...
 
char gcHandlerBuffer [MAIN_SMTP_BUF_LEN]
 Handler buffer definition. More...
 
char gcPasswordBasekey [128]
 Password basekey definition. More...
 
char gcSendRecvBuffer [MAIN_SMTP_BUF_LEN]
 Send and receive buffer definition. More...
 
char gcUserBasekey [128]
 Username basekey definition. More...
 
int8_t gs8EmailError = MAIN_EMAIL_ERROR_NONE
 SMTP email error information. More...
 
uint32_t gu32HostIp = 0
 IP address of host. More...
 
uint8_t gu8RetryCount = 0
 Retry count. More...
 
uint8_t gu8SmtpStatus = SMTP_INIT
 SMTP information. More...
 
uint8_t gu8SocketStatus = SocketInit
 
static SOCKET tcp_client_socket = -1
 TCP client socket handler. More...
 

#define STRING_EOL   "\r\n"
#define STRING_HEADER
Value:
"-- WINC1500 send email example --"STRING_EOL \
"-- "BOARD_NAME " --"STRING_EOL \
"-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
#define BOARD_NAME
Name string macro.
Definition: samw25_xplained_pro.h:62
#define STRING_EOL
Definition: main21.c:104

Referenced by main().

static void close_socket ( void  )
static

Close socket function.

Returns
None.

References close(), and tcp_client_socket.

Referenced by main(), and retry_smtp_server().

void ConvertToBase64 ( char *  pcOutStr,
const char *  pccInStr,
int  iLen 
)

References g_ccB64Tbl.

Referenced by generateBase64Key().

static void generateBase64Key ( char *  input,
char *  basekey 
)
static

Generates Base64 Key needed for authentication.

Parameters
[in]inputis the string to be converted to base64.
[in]basekey1is the base64 converted output.
Returns
None.

References ConvertToBase64().

Referenced by socket_cb().

static void resolve_cb ( uint8_t *  hostName,
uint32_t  hostIp 
)
static

Callback function of IP address.

Parameters
[in]hostNameDomain name.
[in]hostIpServer IP.
Returns
None.

References gbHostIpByName, gu32HostIp, and IPV4_BYTE.

Referenced by main().

static void retry_smtp_server ( void  )
static

Retry SMTP server function.

Returns
None.

References close_socket(), delay_ms, gbHostIpByName, gu8SmtpStatus, gu8SocketStatus, m2m_wifi_disconnect(), MAIN_WAITING_TIME, SMTP_INIT, and SocketInit.

Referenced by main().

static int8_t smtpConnect ( void  )
static

Creates and connects to an unsecure socket to be used for SMTP.

Parameters
[in]None.
Returns
SOCK_ERR_NO_ERROR if success, -1 if socket create error, SOCK_ERR_INVALID if socket connect error.

References _htons, AF_INET, close(), connect(), gu32HostIp, MAIN_GMAIL_HOST_PORT, in_addr::s_addr, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_port, SOCK_ERR_INVALID, SOCK_ERR_NO_ERROR, SOCK_STREAM, socket(), SOCKET_FLAGS_SSL, and tcp_client_socket.

Referenced by main().

static void smtpSendRecv ( long  socket,
char *  cmd,
char *  cmdparam,
char *  respBuf 
)
static

Sends an SMTP command and provides the server response.

Parameters
[in]socketis the socket descriptor to be used for sending.
[in]cmdis the string of the command.
[in]cmdparais the command parameter.
[in]respBufis a pointer to the SMTP response from the server.
Returns
None.

References cSmtpCrlf, gcSendRecvBuffer, MAIN_SMTP_BUF_LEN, NULL, recv(), and send().

Referenced by smtpStateHandler().

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.
[in]pvMsgA pointer to a buffer containing the notification parameters.
Returns
None.

References gbConnectedWifi, gbHostIpByName, gethostbyname(), 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_GMAIL_HOST_NAME, MAIN_WLAN_AUTH, MAIN_WLAN_PSK, MAIN_WLAN_SSID, and tstrM2mWifiStateChanged::u8CurrState.

Referenced by main().

struct usart_module cdc_uart_module
static

UART module for debug.

Referenced by configure_console().

bool gbConnectedWifi = false
static

Wi-Fi status variable.

Referenced by main(), and wifi_cb().

bool gbHostIpByName = false
static

Get host IP status variable.

Referenced by main(), resolve_cb(), retry_smtp_server(), and wifi_cb().

char gcHandlerBuffer[MAIN_SMTP_BUF_LEN]

Handler buffer definition.

Referenced by smtpStateHandler(), and socket_cb().

char gcPasswordBasekey[128]

Password basekey definition.

Referenced by smtpStateHandler(), and socket_cb().

char gcSendRecvBuffer[MAIN_SMTP_BUF_LEN]

Send and receive buffer definition.

Referenced by smtpSendRecv().

char gcUserBasekey[128]

Username basekey definition.

Referenced by smtpStateHandler(), and socket_cb().

int8_t gs8EmailError = MAIN_EMAIL_ERROR_NONE

SMTP email error information.

Referenced by main(), and socket_cb().

uint32_t gu32HostIp = 0

IP address of host.

Referenced by resolve_cb(), and smtpConnect().

uint8_t gu8RetryCount = 0

Retry count.

Referenced by main().

uint8_t gu8SmtpStatus = SMTP_INIT

SMTP information.

Referenced by retry_smtp_server(), smtpStateHandler(), and socket_cb().

uint8_t gu8SocketStatus = SocketInit

Referenced by main(), retry_smtp_server(), and socket_cb().

SOCKET tcp_client_socket = -1
static

TCP client socket handler.

Referenced by close_socket(), main(), smtpConnect(), smtpStateHandler(), and socket_cb().