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 |
Referenced by main().
|
static |
Close socket function.
References close(), and tcp_client_socket.
Referenced by main(), and retry_smtp_server().
|
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().
Referenced by main().
void ConvertToBase64 | ( | char * | pcOutStr, |
const char * | pccInStr, | ||
int | iLen | ||
) |
References g_ccB64Tbl.
Referenced by generateBase64Key().
|
static |
Generates Base64 Key needed for authentication.
[in] | input | is the string to be converted to base64. |
[in] | basekey1 | is the base64 converted output. |
References ConvertToBase64().
Referenced by socket_cb().
int main | ( | void | ) |
Main application function.
Initialize system, UART console, network then start function of SMTP email client.
References close_socket(), configure_console(), gbConnectedWifi, gbHostIpByName, gs8EmailError, gu8RetryCount, gu8SocketStatus, M2M_SUCCESS, M2M_WIFI_CH_ALL, m2m_wifi_connect(), m2m_wifi_handle_events(), m2m_wifi_init(), MAIN_EMAIL_ERROR_INIT, MAIN_EMAIL_ERROR_NONE, MAIN_RETRY_COUNT, MAIN_WLAN_AUTH, MAIN_WLAN_PSK, MAIN_WLAN_SSID, nm_bsp_init(), NULL, tstrWifiInitParam::pfAppWifiCb, port_pin_set_output_level(), registerSocketCallback(), resolve_cb(), retry_smtp_server(), smtpConnect(), smtpStateHandler(), SOCK_ERR_NO_ERROR, socket_cb(), SocketComplete, SocketConnect, SocketError, SocketInit, socketInit(), SocketWaiting, STRING_HEADER, system_init(), tcp_client_socket, and wifi_cb().
|
static |
Callback function of IP address.
[in] | hostName | Domain name. |
[in] | hostIp | Server IP. |
References gbHostIpByName, gu32HostIp, and IPV4_BYTE.
Referenced by main().
|
static |
Retry SMTP server function.
References close_socket(), delay_ms, gbHostIpByName, gu8SmtpStatus, gu8SocketStatus, m2m_wifi_disconnect(), MAIN_WAITING_TIME, SMTP_INIT, and SocketInit.
Referenced by main().
|
static |
Creates and connects to an unsecure socket to be used for SMTP.
[in] | None. |
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 |
Sends an SMTP command and provides the server response.
[in] | socket | is the socket descriptor to be used for sending. |
[in] | cmd | is the string of the command. |
[in] | cmdpara | is the command parameter. |
[in] | respBuf | is a pointer to the SMTP response from the server. |
References cSmtpCrlf, gcSendRecvBuffer, MAIN_SMTP_BUF_LEN, NULL, recv(), and send().
Referenced by smtpStateHandler().
|
static |
SMTP state handler.
[in] | None. |
References cSmtpCrlf, cSmtpData, cSmtpDataEnd, cSmtpFrom, cSmtpMailFrom, cSmtpQuit, cSmtpRcpt, cSmtpSubject, cSmtpTo, gcHandlerBuffer, gcPasswordBasekey, gcUserBasekey, gu8SmtpStatus, MAIN_EMAIL_ERROR_FAILED, MAIN_EMAIL_ERROR_NONE, MAIN_EMAIL_MSG, MAIN_EMAIL_SUBJECT, MAIN_FROM_ADDRESS, MAIN_RECIPIENT_RFC, MAIN_SENDER_RFC, MAIN_TO_ADDRESS, NULL, send(), SMTP_AUTH, SMTP_AUTH_PASSWORD, SMTP_AUTH_USERNAME, SMTP_DATA, SMTP_ERROR, SMTP_FROM, SMTP_HELO, SMTP_MESSAGE_BODY, SMTP_MESSAGE_CRLF, SMTP_MESSAGE_DATAEND, SMTP_MESSAGE_FROM, SMTP_MESSAGE_SUBJECT, SMTP_MESSAGE_TO, SMTP_QUIT, SMTP_RCPT, smtpSendRecv(), and tcp_client_socket.
Referenced by main().
|
static |
Callback function of TCP client socket.
[in] | sock | socket handler. |
[in] | u8Msg | Type of Socket notification |
[in] | pvMsg | A structure contains notification informations. |
References cSmtpCodeAuthReply, cSmtpCodeAuthSuccess, cSmtpCodeIntermedReply, cSmtpCodeOkReply, cSmtpCodeReady, gcHandlerBuffer, gcPasswordBasekey, gcUserBasekey, generateBase64Key(), gs8EmailError, gu8SmtpStatus, gu8SocketStatus, MAIN_EMAIL_ERROR_AUTH, MAIN_EMAIL_ERROR_AUTH_PASSWORD, MAIN_EMAIL_ERROR_AUTH_USERNAME, MAIN_EMAIL_ERROR_DATA, MAIN_EMAIL_ERROR_FROM, MAIN_EMAIL_ERROR_HELO, MAIN_EMAIL_ERROR_INIT, MAIN_EMAIL_ERROR_MESSAGE, MAIN_EMAIL_ERROR_RCPT, MAIN_FROM_ADDRESS, MAIN_FROM_PASSWORD, MAIN_SMTP_BUF_LEN, tstrSocketRecvMsg::pu8Buffer, recv(), tstrSocketRecvMsg::s16BufferSize, tstrSocketConnectMsg::s8Error, SMTP_AUTH, SMTP_AUTH_PASSWORD, SMTP_AUTH_USERNAME, SMTP_DATA, SMTP_ERROR, SMTP_FROM, SMTP_HELO, SMTP_INIT, SMTP_MESSAGE_BODY, SMTP_MESSAGE_CRLF, SMTP_MESSAGE_DATAEND, SMTP_MESSAGE_FROM, SMTP_MESSAGE_SUBJECT, SMTP_MESSAGE_TO, SMTP_QUIT, SMTP_RCPT, SOCK_ERR_NO_ERROR, SOCKET_MSG_CONNECT, SOCKET_MSG_RECV, SOCKET_MSG_SEND, SocketComplete, SocketConnect, SocketError, SocketWaiting, and tcp_client_socket.
Referenced by main().
|
static |
Callback to get the Wi-Fi status update.
[in] | u8MsgType | Type of Wi-Fi notification. |
[in] | pvMsg | A pointer to a buffer containing the notification parameters. |
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().
|
static |
UART module for debug.
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.
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 |
|
static |
TCP client socket handler.
Referenced by close_socket(), main(), smtpConnect(), smtpStateHandler(), and socket_cb().