Mobil_surveillance_system 1
Data Structures | Defines | Enumerations | Functions
GPRS.h File Reference

GPRS header file. More...

#include "Bitmanip.h"
#include "Dinamic_Array.h"
#include "Time.h"
#include "UART2.h"
#include "GPS.h"
#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  tCommandInstance
 GPRS command type. More...

Defines

#define AT_COMMAND_AT   "AT\r\n"
 Definations of at commands.
#define AT_COMMAND_ECHO_OFF   "ATE0\r\n"
#define AT_COMMAND_RESTART   "AT+CFUN=1\r\n"
#define AT_COMMAND_ENABLE_ERROR_MESSAGES   "AT+CMEE=1\r\n"
#define AT_COMMAND_START_WIP_PACKET   "AT+WOPEN=1\r\n"
#define AT_COMMAND_ENABLE_CLIP   "AT+CLIP=1\r\n"
#define AT_COMMAND_SELECT_CHARACTERSET   "AT+CSCS=\"GSM\"\r\n"
#define AT_COMMAND_START_IP_STACK   "AT+WIPCFG=1\r\n"
#define AT_COMMAND_CLOSE_IP_STACK   "AT+WIPCFG=0\r\n"
#define AT_COMMAND_CONF_IP_STACK   "AT+WIPCFG?\r\n"
#define AT_COMMAND_OPEN_BEARER   "AT+WIPBR=1,6\r\n"
#define AT_COMMAND_CLOSE_BEARER   "AT+WIPBR=0,6\r\n"
#define AT_COMMAND_START_BEARER   "AT+WIPBR=4,6,0\r\n"
#define AT_COMMAND_SET_TCP_PARAMETERS   "AT+WIPOPT=2,1,2,51,1600\r\n"
#define AT_COMMAND_SET_TCP_IN_PARAMETERS   "AT+WIPOPT=2,1,2,50,1600\r\n"
#define AT_COMMAND_SWITCH_TO_AT_MODE   "+++"
#define AT_COMMAND_CLOSE_IP   "AT+WIPCLOSE=2,1\r\n"
#define AT_COMMAND_OPEN_TCP_SOCKET   "AT+WIPDATA=2,1,1\r\n"
#define AT_COMMAND_CLOSE_TCP_SOCKET   "AT+WIPCLOSE=2,1\r\n"
#define AT_COMMAND_DUMP_BR   "AT+WIPBR?\r\n"
#define GPRS_RESPONSE_BUFFER_SIZE   1024
 size of the buffer that contians the response of the gprs module
#define ATTEMPT_NUMBER   5
 number of the attempts to send one command to the gprs module
#define PIN   "5003"
 pin of used sim card
#define GPRS_ETX_CHAR   '\x03'
 this character indicate the end of the answear received from the server

Enumerations

enum  tGPRS_state {
  GPRS_Uninitialized = 0, GPRS_EscapeToCommandMode, GPRS_RestartGSM, GPRS_EchoOff,
  GPRS_ErrorOn, GPRS_AddPin, GPRS_OpenGSM, GPRS_ChangeCharSet,
  GPRS_Initialized, GPRS_OpenGPRS, GPRS_OpenBearer, GPRS_SetConnectionPoint,
  GPRS_AddUsername, GPRS_AddPassword, GPRS_ConnectToBearer, GPRS_ConnectedToBearer,
  GPRS_WIPCreate, GPRS_WIPData, GPRS_SendingData, GPRS_Connected,
  GPRS_EscapeFromDataMode, GPRS_WIPClose, GPRS_DisconnectingFromBearer, GPRS_CloseGPRS,
  GPRS_Waiting
}

Functions

void GPRS_initGPRS (void)
void GPRS_executeGPRS (void)
void GPRS_processResponse (void)
void GPRS_addCommand (char *pcCommand, tDateTime sTimeoutDuration)
void GPRS_processCommand (void)
void GPRS_setState (tGPRS_state newState)
void GPRS_initCommandQueue (tDinamicArray *pDinamicArray)
void GPRS_appendResponse (const char *pcResponse)
void GPRS_clearResponseBuffer (void)
void GPRS_wait (tDateTime sWaitDuration, tGPRS_state eNextState)
tBoolean GPRS_isDeadlinePassed (void)
void GPRS_connectToBearer (void)
void GPRS_disconnectFromBearer (void)
void GPRS_httpPost (char *pcHost, char *pcPort, char *pcURL, char *pcData, unsigned int uiDataLength)

Detailed Description

GPRS header file.

This file contains the prototypes of the GPRS unit functions.

Definition in file GPRS.h.


Enumeration Type Documentation

GPRS state type

Definition at line 74 of file GPRS.h.

{

    GPRS_Uninitialized = 0,
    GPRS_EscapeToCommandMode,
    GPRS_RestartGSM,
    GPRS_EchoOff,
    GPRS_ErrorOn,
    GPRS_AddPin,
    GPRS_OpenGSM,
    GPRS_ChangeCharSet,

    GPRS_Initialized,       // 8


    GPRS_OpenGPRS,
    GPRS_OpenBearer,
    GPRS_SetConnectionPoint,
    GPRS_AddUsername,
    GPRS_AddPassword,
    GPRS_ConnectToBearer,

    GPRS_ConnectedToBearer, // 15

    GPRS_WIPCreate,
    GPRS_WIPData,
    GPRS_SendingData,
    GPRS_Connected,
    GPRS_EscapeFromDataMode,
    GPRS_WIPClose,
    GPRS_DisconnectingFromBearer,
    GPRS_CloseGPRS,         // 23

    GPRS_Waiting

}tGPRS_state;

Function Documentation

void GPRS_addCommand ( char *  pcCommand,
tDateTime  sTimeoutDuration 
)
Parameters:
pcCommand
sTimeoutDuration

Definition at line 533 of file GPRS.c.

{
    // this memory space set free after the command has been sent in the GPRS_processCommand function
    tCommandInstance* ptCommandInstanceTmp = (tCommandInstance*)malloc(sizeof(tCommandInstance)*1);
    ptCommandInstanceTmp->pcCommand = pcCommand;
    ptCommandInstanceTmp->sWaitingDuration = sTimeoutDuration;
    // put the command into the queue
    Dinamic_Array_Add(&g_GPRS_CommandQueue, ptCommandInstanceTmp);
}
void GPRS_appendResponse ( const char *  pcResponse)
Parameters:
pcResponse

Definition at line 471 of file GPRS.c.

{
    unsigned int uiLength = strlen(pcResponse);
    if(uiLength + g_pcGPRSResponseBufferEnd < g_pcGPRSResponseBuffer + GPRS_RESPONSE_BUFFER_SIZE){
        
        strcat(g_pcGPRSResponseBufferEnd, pcResponse);
        g_pcGPRSResponseBufferEnd += uiLength;
    }
    
    else{
        
        // buffer is full 
    }   
}
void GPRS_httpPost ( char *  pcHost,
char *  pcPort,
char *  pcURL,
char *  pcData,
unsigned int  uiDataLength 
)
Parameters:
pcHost
pcPort
pcURL
pcData
uiDataLength

Definition at line 510 of file GPRS.c.

{
    
    if ( g_eGPRSstate != GPRS_ConnectedToBearer ) {
        
        //  do nothing until we aren't connected    
        return;
    }
    GPRS_setState( GPRS_WIPCreate );
    sprintf( g_pcCreateConnectionCommand, "AT+WIPCREATE=2,1,\"%s\",%s\r\n", pcHost, pcPort );
    GPRS_addCommand( g_pcCreateConnectionCommand, 0 * SECOND );
    // set the globel variables according the function paramters given
    g_pcHost = pcHost;
    g_pcPort = pcPort;
    g_pcURL = pcURL;
    g_pcData = pcData;
    g_uiDataLength = uiDataLength;
}
void GPRS_initCommandQueue ( tDinamicArray pDinamicArray)
Parameters:
pDinamicArray

Definition at line 463 of file GPRS.c.

{
    Dinamic_Array_Init(pDinamicArray);
}
tBoolean GPRS_isDeadlinePassed ( void  )
Returns:

Definition at line 554 of file GPRS.c.

{
    tDateTime Now;
    Time_getCurrentDateTime(&Now);
    return ((Now > g_GPRSdeadline )? 1 : 0);
}
void GPRS_processResponse ( void  )

Todo:
Jelezni valahogy, hogy sikertelen volt a post.
Todo:
A szervertol kapott valasz parsolasa, hogy megfelelo valasz erkezett-e.
char * pcStr = GPRS_HTTPParseResponse( g_pcGPRS_ResponseBuffer );

Definition at line 178 of file GPRS.c.

{
    static unsigned char ucAttempt = 0;
    
    switch ( g_eGPRSstate ) {
        // init gprs module
        case GPRS_RestartGSM :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_EchoOff );
                GPRS_addCommand( AT_COMMAND_ECHO_OFF, 0 * SECOND );
            }
            
        break;
        
        case GPRS_EchoOff :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_ErrorOn );
                GPRS_addCommand( AT_COMMAND_ENABLE_ERROR_MESSAGES, 0 * SECOND );
            }
            
        break;
        
        case GPRS_ErrorOn :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_AddPin );
                GPRS_addCommand( g_pcPinCommand, 0 * SECOND );
            }
            
        break;
        
        case GPRS_AddPin :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_OpenGSM );
                GPRS_addCommand( AT_COMMAND_START_WIP_PACKET, 0 * SECOND );
            }
            
        break;
        
        case GPRS_OpenGSM :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_ChangeCharSet );
                GPRS_addCommand( AT_COMMAND_SELECT_CHARACTERSET, 0 * SECOND );
            }
            
        break;
        
        case GPRS_ChangeCharSet :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_Initialized );
            }
            
        break;
        // at this point the GPRS module is ready to connect to the bearer
        // the program can be here in two case:
        //                                      - gprs module is inizialized after starting and waiting for the first connection
        //                                      - we were connected but GPRS_disconectFromTheBearer has been called
        case GPRS_Initialized :
        // do nothing until someone wants to connecting to the the bearer
        break;
        // start connecting to the bearer, call GPRS_connectTotheBearer function
        case GPRS_OpenGPRS :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_OpenBearer );
                GPRS_addCommand( AT_COMMAND_OPEN_BEARER, 0 * SECOND );
            }
            
        break;
        
        case GPRS_OpenBearer :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_SetConnectionPoint );
                GPRS_addCommand( g_pcBearerCommand, 0 * SECOND );
            }
            
        break;
        
        case GPRS_SetConnectionPoint :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_AddUsername );
                GPRS_addCommand( g_pcUsernameCommand, 0 * SECOND );
            }
            
        break;
        
        case GPRS_AddUsername :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_AddPassword );
                GPRS_addCommand( g_pcPasswordCommand, 0 * SECOND );
            }
            
        break;
        
        case GPRS_AddPassword :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_ConnectToBearer );
                GPRS_addCommand( AT_COMMAND_START_BEARER, 0 * SECOND );
                // 30 is the number of the attempts of reconection to the bearer
                ucAttempt = 30;
            }
            
        break;
        
        case GPRS_ConnectToBearer :
        
            // if during the connection everything went well 
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_clearResponseBuffer();
                //  retrive the number of the connection attempts
                ucAttempt = 30;
                GPRS_setState( GPRS_ConnectedToBearer );
            }
            // if failed to connect to the bearer
            if ( strstr( g_pcGPRSResponseBuffer, "ERROR" ) ) {
                GPRS_clearResponseBuffer();
                // try until ucAttempt number didn't reached zero
                if ( --ucAttempt > 0 ) {
                    GPRS_wait( 2 * SECOND, g_eGPRSstate );
                    // send again the start command
                    GPRS_addCommand( AT_COMMAND_START_BEARER, 0 * SECOND );
                // if we don't have any attempt left
                } else {
                    GPRS_setState( GPRS_CloseGPRS );
                    GPRS_addCommand( AT_COMMAND_CLOSE_IP_STACK, 0 * SECOND );
                    // TODO ezt a hibat lekezelni
                }
            }
            
        break;
        
        // we're connected to the bearer
        // the program can be here in two case:
        //                                   - we have just connected to the bearer at very first time
        //                                   - tcp/ip stack is closed by WIPCLOSE at command
        case GPRS_ConnectedToBearer :
        // waiting to post something, call GPRS_HTTPPost function
        break;
        // start posting
        case GPRS_WIPCreate :
        
            if ( strstr( g_pcGPRSResponseBuffer, "+WIPREADY:" ) ) {
                GPRS_clearResponseBuffer();
                //  retrive the number of the connection attempts
                ucAttempt = 30;
                GPRS_setState( GPRS_WIPData );
                GPRS_addCommand( AT_COMMAND_OPEN_TCP_SOCKET, 0 * SECOND );
            }
            if ( strstr( g_pcGPRSResponseBuffer, "ERROR" ) ) {
                
                GPRS_clearResponseBuffer();
                
                if ( --ucAttempt > 0 ) {
                    GPRS_wait( 2 * SECOND, g_eGPRSstate );
                    // send again the g_pcCreateConnectionCommand command (this command has been set by the GPRS_HTTPPost function)
                    GPRS_addCommand( g_pcCreateConnectionCommand, 0 * SECOND );
                } 
                // if we have don't have more attempt left, close the stack 
                else {
                    GPRS_setState( GPRS_WIPClose );
                    GPRS_addCommand( AT_COMMAND_CLOSE_TCP_SOCKET, 0 * SECOND );
                    // TODO ezt a hibat lekezelni
                }
            }
        break;
        
        case GPRS_WIPData :
        
            if ( strstr( g_pcGPRSResponseBuffer, "CONNECT" ) ) {
                
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_SendingData );
                sprintf( g_pcFileCommand, 
                        "POST %s HTTP/1.1\r\nHost: %s\r\nAccept: text/html\r\nConnection: close\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n", 
                        g_pcURL, g_pcHost, strlen( g_pcData ) );
                GPRS_addCommand( g_pcFileCommand, 1 * SECOND ); 
                
            }
            if ( strstr( g_pcGPRSResponseBuffer, "ERROR" ) ) {
                
                // if any error occures during posting close the stack and go back to the GPRS_ConnectedToBearer state
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_EscapeFromDataMode );
                GPRS_addCommand( AT_COMMAND_SWITCH_TO_AT_MODE, 1 * SECOND );
            }
        break;
        
        case GPRS_SendingData:
        
            GPRS_clearResponseBuffer();
            GPRS_setState( GPRS_Connected );
            // g_pcData contains the inforamtion we want to post and we past to the GPRS_HTTPPost function
            UART2_write(g_pcData, g_uiDataLength);
        
        case GPRS_Connected :
        
            if ( strchr( g_pcGPRSResponseBuffer, GPRS_ETX_CHAR ) && strstr( g_pcGPRSResponseBuffer, "SHUTDOWN" ) ) {

                GPRS_clearResponseBuffer();
                // close the tcp/ip stack and go back to the GPRS_ConnectedToBearer state
                GPRS_setState( GPRS_EscapeFromDataMode );
                GPRS_addCommand( AT_COMMAND_SWITCH_TO_AT_MODE, 1 * SECOND );
            }

        break;
        
        case GPRS_WIPClose :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                // after this the tcp/ip stack is closed 
                // go back to the GPRS_ConnectedToBearer state
                GPRS_clearResponseBuffer();
                GPRS_setState( GPRS_ConnectedToBearer );
            }
            
        break;
        // we can reached this point only that case if GPRS_disconnectFromTheBearer function is has been called explicitly
        case GPRS_DisconnectingFromBearer :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_setState( GPRS_CloseGPRS );
                GPRS_addCommand( AT_COMMAND_CLOSE_IP_STACK, 0 * SECOND );
            }
        break;
        
        case GPRS_CloseGPRS :
        
            if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
                GPRS_setState( GPRS_Initialized );
            }
            
        break;
    }
}
void GPRS_setState ( tGPRS_state  newState)
Parameters:
newState

Definition at line 546 of file GPRS.c.

{
    g_eGPRSstate = newState;
}
void GPRS_wait ( tDateTime  sWaitDuration,
tGPRS_state  eNextState 
)
Parameters:
sWaitDuration
eNextState

Definition at line 498 of file GPRS.c.

{
        
    Time_getCurrentDateTime( &g_GPRSdeadline );
    g_GPRSdeadline += sWaitDuration;
    g_eGPRSnextState = eNextState;
    g_eGPRSstate = GPRS_Waiting;
}
 All Data Structures Files Functions Variables Typedefs Enumerations Defines