WINC1500 Wi-Fi Serial example.
Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
#include "asf.h"
#include "main.h"
#include "driver/include/m2m_wifi.h"
#include "socket/include/socket.h"
Data Structures | |
struct | user_cmd |
User command structure. More... | |
Macros | |
Strings for printing banner. | |
#define | STRING_EOL "\r\n" |
#define | STRING_HEADER |
Macro for printing local/remote messages. | |
#define | PRINT_LOCAL_MSG(msg) printf("[Local] %s\r\n", msg) |
Local message printing. More... | |
#define | PRINT_REMOTE_MSG(msg) printf("\t[Remote] %s\r\n", msg) |
Remote message printing. More... | |
Command indicator. | |
#define | INDICATOR_STRING_LEN 2 |
Length of command indicator string. More... | |
#define | LOCAL_CMD_INDICATOR "<<" |
Indicator string for local command. More... | |
#define | REMOTE_CMD_INDICATOR ">>" |
Indicator string for remote command. More... | |
Typedefs | |
typedef void(* | cmd_handler )(void *pMsg) |
Command handler function type. More... | |
Enumerations | |
enum | { CMD_ID_LOCAL_START = 0, CMD_ID_L_HELP = CMD_ID_LOCAL_START, CMD_ID_L_CONNECT, CMD_ID_L_DISCONNECT, CMD_ID_LOCAL_END, CMD_ID_REMOTE_START = CMD_ID_LOCAL_END, CMD_ID_R_CONTROL = CMD_ID_REMOTE_START, CMD_ID_REMOTE_END, CMD_ID_MAX = CMD_ID_REMOTE_END } |
User command index. More... | |
Functions | |
static void | configure_console (void) |
Configure UART console with callback function. More... | |
void | connect_cmd_handler (void *pMsg) |
Connection command handler function. More... | |
void | control_cmd_handler (void *pMsg) |
Remote control command handler function. More... | |
void | disconnect_cmd_handler (void *pMsg) |
Socket close command handler function. More... | |
void | handle_input_message (void) |
Parse input message from serial interface. More... | |
void | help_cmd_handler (void *pMsg) |
Help command handler function. More... | |
int | main (void) |
Main application function. More... | |
uint8_t | parse_command (char *buffer, uint8_t remote) |
Check whether the message is a command and execute the command handler function. More... | |
static void | socket_cb (SOCKET sock, uint8_t u8Msg, void *pvMsg) |
Callback to get the Socket event. More... | |
static void | uart_callback (const struct usart_module *const module) |
Callback of USART input. More... | |
static void | wifi_cb (uint8 msg_type, void *msg_data) |
Callback to get the Wi-Fi status update. More... | |
Variables | |
static struct usart_module | cdc_uart_module |
UART module for debug. More... | |
static struct user_cmd | cmd_list [CMD_ID_MAX] |
User command list. More... | |
static uint8_t | gau8SocketBuffer [MAIN_CHAT_BUFFER_SIZE] |
Socket buffer definition. More... | |
static SOCKET | tcp_client_socket = -1 |
TCP client socket. More... | |
static uint8_t | tcp_connected = 0 |
Flag for sock connection state. More... | |
static SOCKET | tcp_server_socket = -1 |
TCP server socket. More... | |
static char | uart_buffer [MAIN_CHAT_BUFFER_SIZE] |
UART buffer. More... | |
static uint16_t | uart_buffer_written = 0 |
Written size to UART buffer. More... | |
static uint16_t | uart_ch_buffer |
Buffer of a character from the serial. More... | |
static uint8_t | wifi_connected = M2M_WIFI_DISCONNECTED |
Wi-Fi connection state. More... | |
#define INDICATOR_STRING_LEN 2 |
Length of command indicator string.
Referenced by handle_input_message(), and socket_cb().
#define LOCAL_CMD_INDICATOR "<<" |
Indicator string for local command.
Referenced by handle_input_message().
#define PRINT_LOCAL_MSG | ( | msg | ) | printf("[Local] %s\r\n", msg) |
Local message printing.
Referenced by handle_input_message().
#define PRINT_REMOTE_MSG | ( | msg | ) | printf("\t[Remote] %s\r\n", msg) |
Remote message printing.
Referenced by socket_cb().
#define REMOTE_CMD_INDICATOR ">>" |
Indicator string for remote command.
Referenced by handle_input_message(), and socket_cb().
#define STRING_EOL "\r\n" |
#define STRING_HEADER |
Referenced by main().
typedef void(* cmd_handler)(void *pMsg) |
Command handler function type.
anonymous enum |
|
static |
Configure UART console with callback function.
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, uart_callback(), USART_CALLBACK_BUFFER_RECEIVED, usart_enable(), usart_enable_callback(), usart_get_config_defaults(), and usart_register_callback().
Referenced by main().
void connect_cmd_handler | ( | void * | pMsg | ) |
Connection command handler function.
Retrieve IP address from the argument, create TCP client socket and connect to the remote device with the IP.
[in] | pMsg | Pointer to the remote IP address string. |
References _htons, AF_INET, close(), connect(), MAIN_WIFI_M2M_SERVER_PORT, nmi_inet_addr(), in_addr::s_addr, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_port, SOCK_STREAM, socket(), and tcp_client_socket.
void control_cmd_handler | ( | void * | pMsg | ) |
Remote control command handler function.
[in] | pMsg | Pointer to the option string. |
References port_pin_set_output_level().
void disconnect_cmd_handler | ( | void * | pMsg | ) |
Socket close command handler function.
Close client socket.
[in] | pMsg | Not used. |
References close(), tcp_client_socket, and tcp_connected.
Referenced by socket_cb().
void handle_input_message | ( | void | ) |
Parse input message from serial interface.
Check whether the message is a command or send the message to the remote device.
References i, INDICATOR_STRING_LEN, LOCAL_CMD_INDICATOR, MAIN_CHAT_BUFFER_SIZE, parse_command(), PRINT_LOCAL_MSG, REMOTE_CMD_INDICATOR, send(), tcp_client_socket, tcp_connected, uart_buffer, and uart_buffer_written.
Referenced by main().
void help_cmd_handler | ( | void * | pMsg | ) |
Help command handler function.
Print usage message.
[in] | pMsg | Not used. |
int main | ( | void | ) |
Main application function.
Application entry point. Initialize board and WINC1500 Wi-Fi module. Read input data from serial interface and sent it to the remote device.
References _htons, AF_INET, bind(), cdc_uart_module, configure_console(), handle_input_message(), M2M_SUCCESS, M2M_WIFI_CH_ALL, m2m_wifi_connect(), M2M_WIFI_CONNECTED, m2m_wifi_handle_events(), m2m_wifi_init(), MAIN_WIFI_M2M_SERVER_PORT, MAIN_WLAN_AUTH, MAIN_WLAN_PSK, MAIN_WLAN_SSID, nm_bsp_init(), NULL, tstrWifiInitParam::pfAppWifiCb, registerSocketCallback(), in_addr::s_addr, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_port, SOCK_STREAM, socket(), socket_cb(), socketInit(), STRING_HEADER, system_init(), tcp_server_socket, uart_ch_buffer, usart_read_job(), wifi_cb(), and wifi_connected.
uint8_t parse_command | ( | char * | buffer, |
uint8_t | remote | ||
) |
Check whether the message is a command and execute the command handler function.
[in] | buffer | Pointer to the input message buffer. |
[in] | remote | If local device command then 0 otherwise 1. |
References user_cmd::cmd_handler, CMD_ID_LOCAL_END, CMD_ID_LOCAL_START, CMD_ID_MAX, CMD_ID_REMOTE_END, CMD_ID_REMOTE_START, i, and MAIN_CHAT_BUFFER_SIZE.
Referenced by handle_input_message(), and socket_cb().
|
static |
Callback to get the Socket event.
[in] | sock | Socket descriptor. |
[in] | u8Msg | Type of Socket notification. Possible types are: |
[in] | pvMsg | A structure contains notification informations. |
References accept(), close(), disconnect_cmd_handler(), gau8SocketBuffer, INDICATOR_STRING_LEN, listen(), NULL, parse_command(), PRINT_REMOTE_MSG, tstrSocketRecvMsg::pu8Buffer, recv(), REMOTE_CMD_INDICATOR, tstrSocketRecvMsg::s16BufferSize, tstrSocketConnectMsg::s8Error, tstrSocketAcceptMsg::sock, SOCKET_MSG_ACCEPT, SOCKET_MSG_BIND, SOCKET_MSG_CONNECT, SOCKET_MSG_LISTEN, SOCKET_MSG_RECV, SOCKET_MSG_SEND, tstrSocketBindMsg::status, tstrSocketListenMsg::status, tcp_client_socket, tcp_connected, and tcp_server_socket.
Referenced by main().
|
static |
Callback of USART input.
[in] | module | USART module structure. |
References MAIN_CHAT_BUFFER_SIZE, uart_buffer, uart_buffer_written, and uart_ch_buffer.
Referenced by configure_console().
|
static |
Callback to get the Wi-Fi status update.
[in] | msg_type | Type of Wi-Fi notification. Possible types are:
|
[in] | msg_data | 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 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().
|
static |
UART module for debug.
|
static |
User command list.
|
static |
Socket buffer definition.
Referenced by socket_cb().
|
static |
TCP client socket.
Referenced by connect_cmd_handler(), disconnect_cmd_handler(), handle_input_message(), and socket_cb().
|
static |
Flag for sock connection state.
Referenced by disconnect_cmd_handler(), handle_input_message(), and socket_cb().
|
static |
TCP server socket.
Referenced by main(), and socket_cb().
|
static |
UART buffer.
Referenced by handle_input_message(), and uart_callback().
|
static |
Written size to UART buffer.
Referenced by handle_input_message(), and uart_callback().
|
static |
Buffer of a character from the serial.
Referenced by main(), and uart_callback().
|
static |