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

This is an example about how to receive a file 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 receive a file using the cellular 2G device.
*
* The cellular 2G in use is a SL6087 2G module manufactured by Sierra wireless.
* This example receives a file to remote FTP server using the FTP protocol working
* with a 2G network.
*
*
* @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.
*
* And the remote FTP parameters:
* - IP ----------> Remote FTP IP address.
* - Folder ------> Enabled folder to receive a FTP file.
* - User --------> FTP user.
* - Password ----> FTP password.
*
* User shall set them in the definitions below.
*
*
*/
/* TSmarT header */
#include "tsmart.h"
/* UART for Debug */
#define DEBUG_UART tsmart_uart1
/* Choose one */
//#define FILE_IN_RAM
#define FILE_IN_FLASH
/* Specific configuration for cellullar 2g, Set definitions */
/* 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>"
/* Debug uart parameters*/
#define APP_RX_BUFFER_SZ 100
static uint8_t app_rx_buf[APP_RX_BUFFER_SZ];
/* Remote FTP server parameters */
#define FTP_SERVER_IP "<SET REMOTE FTP ADDRESS>" /* IP address of the remote FTP server */
#define FTP_SERVER_PORT 21 /* Remote FTP server port. Port = 21 is the most usual port, change it if your FTP server works with other port */
#define FTP_SERVER_USER "<SET USER>" /* User */
#define FTP_SERVER_PASSWORD "<SET PASSWORD>" /* Password */
#define FTP_SERVER_FOLDER "<SET FOLDER>" /* Folder. i.e.: "./cel" */
#define FTP_SERVER_BINARY 0
#define FTP_SERVER_PASIVE 1
#define FTP_SERVER_FILE_NAME "<SET FILE>" /* File to Download. i.e.: "test.txt" */
#ifdef FILE_IN_FLASH
/* Location file in flash memory */
#define FILE_FLASH_ADDR 0x08080000 /* Flash address */
#define MAX_SIZE_FILE 512000 /* Max File Size */
#endif
#ifdef FILE_IN_RAM
/* FTP buffer */
#define FTP_RX_BUFFER_SZ 100
static uint8_t ftp_rx_buf[FTP_RX_BUFFER_SZ];
#endif
/* @brief vCEL2G_FTP_send 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 FTP
* client. Then it receives a file to the remote FTP server and the client will be
* closed.
*
*
* @param pvParameters
*/
void vCELLULAR_2G(void *pvParameters) {
/* Variables */
static uint8_t debug_buffer[100];
int32_t nb;
/* Debug message */
TSMART_UART_Send(&DEBUG_UART, "Start Receive FTP Test\r\n", strlen("Start Receive FTP Test\r\n"), 1000/portTICK_RATE_MS);
/* Turn On. It takes some seconds */
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(;;);
}
/* Stop stack */
/* Start stack */
for(;;);
}
/* Configure TCP/IP Stack */
/* 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(&DEBUG_UART, "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);
/* Debug message */
TSMART_UART_Send(&DEBUG_UART, ".", strlen("."), 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(&DEBUG_UART, "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(debug_buffer, "IP: %d.%d.%d.%d\r\n", ip_address.ip0, ip_address.ip1, ip_address.ip2, ip_address.ip3);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
TSMART_UART_Send(&DEBUG_UART, "Ready\r\n", strlen("Ready\r\n"), 1000/portTICK_RATE_MS);
/* Open FTP client */
ftp_client.id_session = 1;
ftp_client.remote_ip = FTP_SERVER_IP;
ftp_client.remote_port = FTP_SERVER_PORT;
ftp_client.user = FTP_SERVER_USER;
ftp_client.password = FTP_SERVER_PASSWORD;
ftp_client.file_type = FTP_SERVER_BINARY;
ftp_client.mode = FTP_SERVER_PASIVE;
if( TSMART_CELLULAR_2G_CreateFtpClient(&tsmart_cellular_2g, &ftp_client, 20000) != TSMART_PASS){
for(;;);
}
/* Debug message */
TSMART_UART_Send(&DEBUG_UART, "Downloading file...\r\n", strlen("Downloading file...\r\n"), 1000/portTICK_RATE_MS);
#ifdef FILE_IN_RAM
file_location.ptr_store = (uint32_t)ftp_rx_buf;
file_location.size = FTP_RX_BUFFER_SZ;
#endif
#ifdef FILE_IN_FLASH
file_location.ptr_store = FILE_FLASH_ADDR;
file_location.size = MAX_SIZE_FILE;
#endif
nb = TSMART_CELLULAR_2G_ReceiveFtpFile(&tsmart_cellular_2g, ftp_client.id_session, FTP_SERVER_FOLDER, FTP_SERVER_FILE_NAME, &file_location);
if(nb < 0){
for(;;);
}
/* Debug message */
sprintf(debug_buffer, "Data received: %d\r\n", nb);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Close FTP client */
for(;;);
}
/* wait forever */
while(1);
}
/* @brief Notification task
*
* This task execute "TSMART_CELLULAR_2G_ReceiveNotifications" funtion in a loop,
* reading the new notifications.
*
* This task should have a high priority in the system to avoid lost notifications,
* not only about new connections, incoming SMSs or new TCP client data too.
*
* @param pvParameters
*/
void vCELLULAR_2G_NOTIF(void *pvParameters){
/* Variables */
static uint8_t tcp_data[1000];
int16_t nb;
int16_t counter_bytes = 0;
for(;;){
/* Debug message */
TSMART_UART_Send(&DEBUG_UART, "Waiting for notification\r\n", strlen("Waiting for notification\r\n"), 1000);
switch(notif.notif_type.notif_type){
TSMART_UART_Send(&DEBUG_UART, "SMS received\r\n", strlen("SMS received\r\n"), 1000);
break;
sprintf(tcp_data, "NOTIF------>New connection: %d\r\n", notif.new_conn.id_session);
TSMART_UART_Send(&DEBUG_UART, tcp_data, strlen(tcp_data), 1000);
break;
sprintf(tcp_data, "NOTIF------> NEW DATA CME: %d\r\n", notif.new_data.id_session);
TSMART_UART_Send(&DEBUG_UART, tcp_data, strlen(tcp_data), 1000);
/* Connect */
for(;;);
}
/* Get data */
counter_bytes = 0;
memset(tcp_data, 0x00, sizeof(tcp_data));
/* Wait for data. if nb = 0 ===> connection is shoutdown */
TSMART_UART_Send(&DEBUG_UART, "------->", strlen("------->"), 1000);
do{
nb = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, &tcp_data[counter_bytes], portMAX_DELAY);
TSMART_UART_Send(&DEBUG_UART, tcp_data, nb, 1000);
TSMART_UART_Send(&DEBUG_UART, "\r\n", strlen("\r\n"), 1000);
/* Stop connection */
for(;;);
}
}
break;
TSMART_UART_Send(&DEBUG_UART, "NOTIF------>Connection was shout down\r\n", strlen("NOTIF------>Connection was shout down\r\n"), 1000);
break;
TSMART_UART_Send(&DEBUG_UART, "NOTIF------>Close connection\r\n", strlen("NOTIF------>Close connection\r\n"), 1000);
/* Clean buffer */
/* Connect */
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "Cleaned: ", strlen("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(&DEBUG_UART, tcp_data, nb, 1000);
TSMART_UART_Send(&DEBUG_UART, "\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(&DEBUG_UART, 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(&DEBUG_UART, tcp_data, strlen(tcp_data), 1000/portTICK_RATE_MS);
break;
default:
break;
}
}/* end for */
}/* End task */
/*@brief Initialize resources for application
*
* This function initialize the HW and SW resources.
*
*/
int32_t init() {
/* Variables */
static tsmart_uart_config_t tsmart_uart_config;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* Uncomment for Debug */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Initialize CELLULAR 2G */
return TSMART_FAIL;
}
/* Initialize Internal flash for receiving FTP files */
if (TSMART_INT_FLASH_Init() != FLASH_COMPLETE){
return TSMART_FAIL;
}
/* Initialize Debug UART */
tsmart_uart_config.baud_rate = 115200;
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
tsmart_uart_config.rx_buf_sz = APP_RX_BUFFER_SZ;
tsmart_uart_config.rx_buf = app_rx_buf;
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&DEBUG_UART, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create Cellular 2g task */
if(xTaskCreate(vCELLULAR_2G, "CELLULAR_2G", 1024, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create Celullar 2g Notification task */
if(xTaskCreate(vCELLULAR_2G_NOTIF, "CELLULAR_2G_NOTIF", 1024, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}