Microchip® Advanced Software Framework

winc1500/wifi_serial_example/main21.c File Reference

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
Value:
"-- WINC1500 Wi-Fi Serial example --"STRING_EOL \
"-- "BOARD_NAME " --"STRING_EOL \
"-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
#define BOARD_NAME
Definition: app_init.h:140
#define STRING_EOL
Definition: winc1500/wifi_serial_example/main21.c:115

Referenced by main().

typedef void(* cmd_handler)(void *pMsg)

Command handler function type.

anonymous enum

User command index.

Enumerator
CMD_ID_LOCAL_START 
CMD_ID_L_HELP 

Help command index.

CMD_ID_L_CONNECT 

Connect command index.

CMD_ID_L_DISCONNECT 

Close command index.

CMD_ID_LOCAL_END 
CMD_ID_REMOTE_START 
CMD_ID_R_CONTROL 

Control remote device command index.

CMD_ID_REMOTE_END 
CMD_ID_MAX 

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.

Parameters
[in]pMsgPointer to the remote IP address string.

References _htons, AF_INET, close(), connect(), MAIN_WIFI_M2M_SERVER_PORT, nmi_inet_addr(), printf, 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.

Parameters
[in]pMsgPointer to the option string.

References port_pin_set_output_level(), and printf.

void disconnect_cmd_handler ( void *  pMsg)

Socket close command handler function.

Close client socket.

Parameters
[in]pMsgNot used.

References close(), printf, 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, printf, 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.

Parameters
[in]pMsgNot used.

References printf.

uint8_t parse_command ( char *  buffer,
uint8_t  remote 
)

Check whether the message is a command and execute the command handler function.

Parameters
[in]bufferPointer to the input message buffer.
[in]remoteIf 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 void uart_callback ( const struct usart_module *const  module)
static

Callback of USART input.

Parameters
[in]moduleUSART module structure.

References MAIN_CHAT_BUFFER_SIZE, uart_buffer, uart_buffer_written, and uart_ch_buffer.

Referenced by configure_console().

static void wifi_cb ( uint8  msg_type,
void *  msg_data 
)
static

Callback to get the Wi-Fi status update.

Parameters
[in]msg_typeType of Wi-Fi notification. Possible types are:
[in]msg_dataA 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, printf, tstrM2mWifiStateChanged::u8CurrState, and wifi_connected.

Referenced by main().

struct usart_module cdc_uart_module
static

UART module for debug.

struct user_cmd cmd_list[CMD_ID_MAX]
static
Initial value:
= {
{"help", help_cmd_handler},
{"connect", connect_cmd_handler},
{"disconnect", disconnect_cmd_handler},
{"control", control_cmd_handler}
}
void control_cmd_handler(void *pMsg)
Remote control command handler function.
Definition: winc1500/wifi_serial_example/main21.c:298
void help_cmd_handler(void *pMsg)
Help command handler function.
Definition: winc1500/wifi_serial_example/main21.c:237
void disconnect_cmd_handler(void *pMsg)
Socket close command handler function.
Definition: winc1500/wifi_serial_example/main21.c:287
void connect_cmd_handler(void *pMsg)
Connection command handler function.
Definition: winc1500/wifi_serial_example/main21.c:249

User command list.

uint8_t gau8SocketBuffer[MAIN_CHAT_BUFFER_SIZE]
static

Socket buffer definition.

Referenced by socket_cb().

SOCKET tcp_client_socket = -1
static
uint8_t tcp_connected = 0
static

Flag for sock connection state.

SOCKET tcp_server_socket = -1
static

TCP server socket.

Referenced by main(), and socket_cb().

char uart_buffer[MAIN_CHAT_BUFFER_SIZE]
static

UART buffer.

Referenced by handle_input_message(), and uart_callback().

uint16_t uart_buffer_written = 0
static

Written size to UART buffer.

Referenced by handle_input_message(), and uart_callback().

uint16_t uart_ch_buffer
static

Buffer of a character from the serial.

Referenced by main(), and uart_callback().

uint8_t wifi_connected = M2M_WIFI_DISCONNECTED
static

Wi-Fi connection state.

Referenced by main(), and wifi_cb().