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

This is an example about how to create a TCP client using the cellular 2g device.

/* @file main.c
* @author TST
* @version 0.1
* @date 28/01/2013
* @brief This is an example of how to create a TCP client 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 client working
* with a 2G network. IT makes a downloading form "google" website. This
* application uses a HTTP protocol(HEAD method) to do it.
*
* @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.
*
* @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
*
*/
/* Platform headers */
#include "tsmart.h"
/* This is the specific configuration for cellullar 2g, Set them */
/* 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>"
/* Google website address */
#define GOOGLE_IP "www.google.es"
#define GOOGLE_PORT 80
#define HEAD_SIZE 762 /* Google size head */
/* @brief vCELL2G_TCP_Client 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 create a TCP
* client. Then it uses a HTTP protocol to request the header of the google
* website using a HEAD method.
*
* @param pvParameters
*/
void vCELL2G_TCP_Client(void *pvParameters) {
/* Variables */
static uint8_t tcp_data[1000];
int16_t nb;
/* Debug message */
TSMART_UART_Send(&tsmart_uart2, "TCP CLIENT\r\n", strlen("TCP CLIENT\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 */
/* Stop stack */
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){
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(;;);
}
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 );
/* Create TCP client, target connection----> www.google.es, port: 80 */
tcp_client.id_session = 1;
tcp_client.remote_ip = GOOGLE_IP;
tcp_client.remote_port = GOOGLE_PORT;
if( TSMART_CELLULAR_2G_CreateTcpClient(&tsmart_cellular_2g, &tcp_client, 10000) != TSMART_PASS){
for(;;);
}
/* Connect */
for(;;);
}
/* Send HEAD request (HTTP method) */
sprintf(tcp_data, "HEAD / HTTP/1.1\r\nHost: www.google.es\r\nConnection: keep-alive\r\n\r\n\r\n");
nb = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, tcp_data, strlen(tcp_data));
if(nb != strlen(tcp_data)){
for(;;);
}
do{
/* Wait for answer form "google" (head size are 762 bytes)*/
memset(tcp_data, 0x00, sizeof(tcp_data));
TSMART_UART_Send(&tsmart_uart2, tcp_data, nb, 0);
}while(nb != 0);
/* Stop connection */
for(;;);
}
/* Close TCP Client session */
for(;;);
}
/* wait forever */
while(1);
}
/* @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[150];
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: cellular 2g and UART
*/
/* Initilialize cellular 2g */
return TSMART_FAIL;
}
/* 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;
/* Initialize UART. UART 1 is selected */
TSMART_UART_Init(&tsmart_uart2, &uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - CELL2G_TCP_Client task: This task turn on the cellular 2g, configures the device
* and download the header of "google" website using HTTP protocol.
*/
/* Create CELL2G_TCP_Client Task */
xReturn = xTaskCreate(vCELL2G_TCP_Client, "CEL2G_TCP_Client", 512, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}