Mobil_surveillance_system 1
Functions | Variables
GPRS.c File Reference

GPRS source file. More...

#include "GPRS.h"

Go to the source code of this file.

Functions

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

Variables

tGPRS_state g_eGPRSstate
 The current state of the GPRS.
tGPRS_state g_eGPRSnextState
 The next state after the waiting period expired.
tDateTime g_GPRSdeadline = 0
 This variable can be use to decide to if the waiting period expired.
char * g_pcGPRSResponseBuffer = NULL
 Buffer for the GPS answers.
char * g_pcGPRSResponseBufferEnd = NULL
 This pointer indicates the end of the GPSResponseBuffer.
tDinamicArray g_GPRS_CommandQueue = {NULL}
 Dinamic array of the GPRS command.
char g_pcPinCommand [15]
 Buffer for the pincode.
char g_pcBearerCommand [100]
 Buffer for the name of the bearer.
char g_pcUsernameCommand [50]
 Buffer for the user name.
char g_pcPasswordCommand [50]
 Buffer for the password.
char g_pcCreateConnectionCommand [255]
 Bufffer for the connection command.
char g_pcFileCommand [1024]
 Buffer for the start stack command.
char * g_pcHost
 This variable can be set by the GPRS_httpPost function. Address of the host we want to connect.
char * g_pcPort
 This variable can be set by the GPRS_httpPost function. Number of the port we want to connect.
char * g_pcURL
 This variable can be set by the GPRS_httpPost function. The URL of the server.
char * g_pcData
 This variable can be set bt the GPRS_httpPost functin. Data we want to send to the server.
unsigned int g_uiDataLength
 This variable can be set bt the GPRS_httpPost functin. Length of the data we want to sent to the server.
tBoolean g_iShouldDisconnect
 if this variable set the gprs module will disconnecet from the bearer

Detailed Description

GPRS source file.

Definition in file GPRS.c.


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