API  2.3.0
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
cellular_2g_tcp_server.c

This is an example about how to create a TCP server using the GPRS device.

/* @file main.c
* @author TST
* @version 0.1
* @date 29/01/2013
* @brief This is an example of how to create a TCP server using the
* cellular 2g device.
*
* The cellular 2G in use is a SL6087 2G module manufactured by Sierra wireless.
* This example show the user how to create and manage a TCP server (2G network)
* performing a echo application.
*
* @note To run this application it's necessary to set some network parameters:
* - PIN code -------> Personal identification number.
* - APN ------------> Access Point Name.
* - Login ----------> Network user name.
* - Password -------> Network password.
* User shall set them in the definitions below.
*
* A TCP client is necessary to run this application. Users can use a Telnet
* protocol for this application, mainly because it's very easy to find one.
* Telnet is a network protocol used on the Internet or local area networks
* to provide a bidirectional interactive text-oriented communications facility
* using a virtual terminal connection. Telnet runs over the Transmission
* Control Protocol (TCP) then user can send tcp data to a TSmoTe or a TSgaTe
* in an easy way. The TCP port used is 13.
* Example:
* telnet <your GPRS IP> 13
*
* @note This application uses the debug UART to display debug information.
* To use the debug UARTit, users should set the next configuration in their
* serial communication program:
* - Baud rate -----> 115200
* - Word length ---> 8 bits
* - Parity --------> none
* - Stop bit ------> 1
* - Flow control --> none
*
* @note
* - Use the Debug UART to know the current IP address of the cellular 2g device.
* - Receive and send information can take a few seconds.
*
*/
/* Platform headers */
#include "tsmart.h"
/* Specific configuration for cellullar 2g, Set definitions with your netowrk parameters: */
/* Set SIM configuration */
#define PIN_CODE "<SET PIN CODE>"
/* Access point network parameters */
#define APN "<SET APN>"
#define LOGIN "<SET LOGIN>"
#define PASSWORD "<SET PASSWORD>"
/* Server parameters */
#define TCP_SERVER_ID_SESSION 1 /* Id session, could be 1 to 8 */
#define TCP_SERVER_PORT 13 /* Port, range: 1-65536 */
#define TCP_SERVER_MAX_ID 8 /* Max. ID to be manage by the server */
#define TCP_SERVER_MIN_ID 2 /* Min. ID to be manage by the server */
/* @brief vCELL2G_TCP_Server task
*
* This task turns on and sets the stack TCP/IP and the network parameters for
* the cellular 2g device for this application. After that it read the current
* IP address ans it displays it using a debug UART. Finally it creates a TCP
* server. Once a TCP server is created this examples is enable to receive data
* form a external TCP client.
*
* @param pvParameters
*/
void vCELL2G_TCP_Server(void *pvParameters) {
/* Variables */
static uint8_t tcp_data[1000];
int16_t nb;
int16_t counter_bytes;
/* Debug message */
TSMART_UART_Send(&tsmart_uart2, "TCP SERVER\r\n", strlen("TCP SERVER\r\n"), 1000/portTICK_RATE_MS);
/* Turn On */
if(TSMART_CELLULAR_2G_TurnOn(&tsmart_cellular_2g, PIN_CODE) != TSMART_PASS){
for(;;);
}
/* Configure module */
if( TSMART_CELLULAR_2G_ConfigDevice(&tsmart_cellular_2g, &device_config) != TSMART_PASS){
for(;;);
}
/* Start stack */
for(;;);
}
/* Stack TCP/IP configuration */
tcp_ip_stack_config.ip_ttl = 64;
tcp_ip_stack_config.ip_frag_timeo = 60;
tcp_ip_stack_config.tcp_maxinitwin = 0;
tcp_ip_stack_config.tcp_rexmt_max = 64;
tcp_ip_stack_config.tcp_rexmt_maxcnt = 12;
if(TSMART_CELLULAR_2G_ConfigTcpIpStack(&tsmart_cellular_2g, &tcp_ip_stack_config) != TSMART_PASS){
/* Stop stack */
for(;;);
}
/* Start stack */
for(;;);
}
/* Set again */
if(TSMART_CELLULAR_2G_ConfigTcpIpStack(&tsmart_cellular_2g, &tcp_ip_stack_config) != TSMART_PASS){
for(;;);
}
}
/* Bearer parameters */
bearer_config.apn = APN;
bearer_config.login = LOGIN;
bearer_config.password = PASSWORD;
bearer_config.time_to_connect = 1200;
if(TSMART_CELLULAR_2G_ConfigBearer(&tsmart_cellular_2g, &bearer_config) != TSMART_PASS){
for(;;);
}
/* Start Bearer */
for(;;);
}
/* Debug message */
TSMART_UART_Send(&tsmart_uart2, "Configuration OK\r\nSearching network", strlen("Configuration OK\r\nSearching network"), 1000/portTICK_RATE_MS);
do{
/* Wait to register in GPRS network */
/* Wait for 1 second to try again */
vTaskDelay(1000/portTICK_RATE_MS);
do{
/* Check Attach */
/* Wait for 1 second to try again */
vTaskDelay(1000/portTICK_RATE_MS);
}while( attach != TSMART_CELLULAR_2G_ATTACHED );
/* Debug message */
TSMART_UART_Send(&tsmart_uart2, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Read IP */
if( TSMART_CELLULAR_2G_ReadIp(&tsmart_cellular_2g, &ip_address) != TSMART_PASS){
for(;;);
}
/* Debug message */
sprintf(tcp_data, "IP: %d.%d.%d.%d\r\n", ip_address.ip0, ip_address.ip1, ip_address.ip2, ip_address.ip3);
TSMART_UART_Send(&tsmart_uart2, tcp_data, strlen(tcp_data), 1000/portTICK_RATE_MS);
/* Open TCP server */
tcp_server.id_session = TCP_SERVER_ID_SESSION;
tcp_server.port = TCP_SERVER_PORT;
tcp_server.min_id_child = TCP_SERVER_MIN_ID;
tcp_server.max_id_child = TCP_SERVER_MAX_ID;
if(TSMART_CELLULAR_2G_CreateTcpServer(&tsmart_cellular_2g, &tcp_server, 10000) != TSMART_PASS){
for(;;);
}
for(;;){
/* Debug message */
switch(notif.notif_type.notif_type){
sprintf(tcp_data, "NOTIF------>New connection: %d\r\n", notif.new_conn.id_session);
TSMART_UART_Send(&tsmart_uart2, tcp_data, strlen(tcp_data), 1000);
break;
sprintf(tcp_data, "NOTIF------> NEW DATA: %d\r\n", notif.new_data.id_session);
TSMART_UART_Send(&tsmart_uart2, tcp_data, strlen(tcp_data), 1000);
/* Connect */
for(;;);
}
/* Get data */
counter_bytes = 0;
memset(tcp_data, 0x00, sizeof(tcp_data));
/* Wait for data */
TSMART_UART_Send(&tsmart_uart2, "------->", strlen("------->"), 1000);
do{
nb = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, &tcp_data[counter_bytes], portMAX_DELAY);
TSMART_UART_Send(&tsmart_uart2, tcp_data, nb, 1000);
TSMART_UART_Send(&tsmart_uart2, "\r\n", strlen("\r\n"), 1000);
/* Stop connection */
for(;;);
}
break;
TSMART_UART_Send(&tsmart_uart2, "NOTIF------>Connection was shout down\r\n", strlen("NOTIF------>Connection was shout down\r\n"), 1000);
break;
TSMART_UART_Send(&tsmart_uart2, "NOTIF------>Close connection\r\n", strlen("NOTIF------>Close connection\r\n"), 1000);
/* Clean buffer: when a close connection is received, its possible to have
* information(data or shoutdown notification) in the buffer. So it's
* mandatory to clean the buffer before close any connection. */
/* Connect */
for(;;);
}
TSMART_UART_Send(&tsmart_uart2, "Buffer Cleaned ", strlen("Buffer Cleaned "), 1000);
memset(tcp_data, 0x00, sizeof(tcp_data));
nb = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, tcp_data, 1000/portTICK_RATE_MS);
TSMART_UART_Send(&tsmart_uart2, tcp_data, nb, 1000);
TSMART_UART_Send(&tsmart_uart2, "\r\n", strlen("\r\n"), 1000);
/* Stop connection */
for(;;);
}
/* Close TCP Client session */
for(;;);
}
break;
sprintf(tcp_data, "NOTIF------>ERROR CME: %d\r\n", notif.cme.cme);
TSMART_UART_Send(&tsmart_uart2, tcp_data, strlen(tcp_data), 1000);
/* Debug message */
sprintf(tcp_data, "IP: %d.%d.%d.%d\r\n", ip_address.ip0, ip_address.ip1, ip_address.ip2, ip_address.ip3);
TSMART_UART_Send(&tsmart_uart2, tcp_data, strlen(tcp_data), 1000/portTICK_RATE_MS);
break;
default:
break;
}
}
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_uart_config_t uart_config;
static uint8_t uart_buf[300];
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: cellular 2g and UART
*/
/* Initilialize cellular 2g */
return TSMART_FAIL;
}
/* Initialize debug UART */
/* UART configuration. */
/* Baud rate */
uart_config.baud_rate = 115200;
/* Flow control */
uart_config.flow_control = TSMART_UART_FC_NONE;
/* Parity bit */
uart_config.parity_bit = TSMART_UART_PARITY_NONE;
/* Word length */
uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
/* Stop bit */
uart_config.stop_bit = TSMART_UART_STOPBITS_1;
/* Buffer size for our UART */
uart_config.rx_buf_sz = 150;
/* Buffer for our UART */
uart_config.rx_buf = uart_buf;
/* UART DMA */
uart_config.use_dma = TSMART_UART_DMA_RX_TX;
/* UART time out */
uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&tsmart_uart2, &uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - CELL2G_TCP_Server task: This task turn on the cellular 2g, configures
* the device, open a TCP server and listen for
* receiving new information.
*/
/* Create CELL2G_TCP_Server Task */
xReturn = xTaskCreate(vCELL2G_TCP_Server, "CELL2G_TCP_Server", 512, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}