Mobil_surveillance_system 1
GPRS.c
Go to the documentation of this file.
00001 #include "GPRS.h"
00002 
00004 
00009 //*****************************************************************************
00010 //
00011 // Globale variables
00012 //
00013 //*****************************************************************************
00014 
00015 
00017 tGPRS_state g_eGPRSstate;
00018 
00020 tGPRS_state g_eGPRSnextState;
00021 
00023 tDateTime g_GPRSdeadline = 0;
00024 
00026 char* g_pcGPRSResponseBuffer = NULL;
00027 
00029 char* g_pcGPRSResponseBufferEnd = NULL;
00030 
00032 tDinamicArray g_GPRS_CommandQueue = {NULL};
00033 
00035 char g_pcPinCommand[15];
00036 
00038 char g_pcBearerCommand[100];
00039 
00041 char g_pcUsernameCommand[50];
00042 
00044 char g_pcPasswordCommand[50];
00045 
00047 char g_pcCreateConnectionCommand[255];
00048 
00050 char g_pcFileCommand[1024];
00051 
00053 char* g_pcHost;
00054 
00056 char* g_pcPort;
00057 
00059 char* g_pcURL;
00060 
00062 char* g_pcData;
00063 
00065 unsigned int g_uiDataLength;
00066 
00068 tBoolean g_iShouldDisconnect;
00069 
00070 //*****************************************************************************
00071 //
00072 // Function definations
00073 //
00074 //*****************************************************************************
00075 
00076 
00078 void GPRS_initGPRS(void)
00080 {
00081     g_pcGPRSResponseBuffer = (char*)calloc('\0', sizeof(char)* GPRS_RESPONSE_BUFFER_SIZE);
00082     GPRS_clearResponseBuffer();
00083     GPRS_initCommandQueue(&g_GPRS_CommandQueue);
00084     GPRS_addCommand( AT_COMMAND_AT, 1 * SECOND );
00085     sprintf( g_pcPinCommand, "AT+CPIN=%s\r\n", PIN );
00086     sprintf( g_pcBearerCommand, "AT+WIPBR=2,6,11,\"%s\"\r\n", "vitamax.internet.vodafone.net" );
00087     sprintf( g_pcUsernameCommand, "AT+WIPBR=2,6,0,\"%s\"\r\n", "" );
00088     sprintf( g_pcPasswordCommand, "AT+WIPBR=2,6,1,\"%s\"\r\n", "" );
00089     g_pcHost = g_pcPort = g_pcURL = g_pcData = 0;
00090     g_iShouldDisconnect = 0;
00091     UART2_setAppendResponseCallbackFunction(&GPRS_appendResponse);
00092     GPS_setGPShttpPostCallbackFunction(&GPRS_httpPost);
00093     GPRS_setState(GPRS_Uninitialized);
00094 }
00095 
00096 
00098 void GPRS_executeGPRS(void)
00100 {
00101     switch(g_eGPRSstate){
00102         
00103         case GPRS_Waiting:
00104         
00105             if (GPRS_isDeadlinePassed()) {
00106                 
00107                 GPRS_setState(g_eGPRSnextState);
00108             }   
00109             
00110         break;
00111         
00112         case GPRS_Uninitialized:
00113         
00114             GPRS_setState( GPRS_EscapeToCommandMode );
00115             GPRS_addCommand( AT_COMMAND_SWITCH_TO_AT_MODE, 0 * SECOND );
00116             
00117         break;
00118         
00119         case GPRS_EscapeToCommandMode:
00120         
00121             GPRS_setState( GPRS_RestartGSM );
00122             GPRS_addCommand( AT_COMMAND_RESTART, 0 * SECOND );
00123             GPRS_wait( 1 * SECOND, g_eGPRSstate );
00124         
00125         break;
00126         
00127         case GPRS_EscapeFromDataMode:
00128         
00129             GPRS_setState( GPRS_WIPClose );
00130             GPRS_addCommand( AT_COMMAND_CLOSE_TCP_SOCKET, 0 * SECOND );
00131             
00132         break;
00133         
00134         case GPRS_ConnectedToBearer:
00135         
00136             if ( g_iShouldDisconnect ) {
00137                 GPRS_setState( GPRS_DisconnectingFromBearer );
00138                 GPRS_addCommand( AT_COMMAND_CLOSE_BEARER, 0 * SECOND );
00139 
00140                 g_iShouldDisconnect = 0;
00141             }
00142         
00143         break;
00144         
00145         default:
00146         
00147             GPRS_processCommand();
00148     }
00149     
00150     GPRS_processResponse();
00151 }
00152 
00153 
00155 void GPRS_processCommand(void)
00157 {
00158     if(g_GPRS_CommandQueue.uiSize){
00159          
00160         tCommandInstance* pTmpCommandInstance = (tCommandInstance*) g_GPRS_CommandQueue.pArray[ 0 ];
00161         
00162         UART2_write(pTmpCommandInstance->pcCommand, strlen(pTmpCommandInstance->pcCommand));
00163         if(pTmpCommandInstance->sWaitingDuration){
00164             
00165             GPRS_wait(pTmpCommandInstance->sWaitingDuration, g_eGPRSstate);
00166             
00167         }
00168         
00169         // kozvetetten a pArray 0. eleme is felszabadul szoval nem lesz memory lake 
00170         free(pTmpCommandInstance);
00171         Dinamic_Array_Delete(&g_GPRS_CommandQueue, 0);
00172         
00173     }
00174 }
00175 
00176 
00178 void GPRS_processResponse(void)
00180 {
00181     static unsigned char ucAttempt = 0;
00182     
00183     switch ( g_eGPRSstate ) {
00184         // init gprs module
00185         case GPRS_RestartGSM :
00186         
00187             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00188                 GPRS_clearResponseBuffer();
00189                 GPRS_setState( GPRS_EchoOff );
00190                 GPRS_addCommand( AT_COMMAND_ECHO_OFF, 0 * SECOND );
00191             }
00192             
00193         break;
00194         
00195         case GPRS_EchoOff :
00196         
00197             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00198                 GPRS_clearResponseBuffer();
00199                 GPRS_setState( GPRS_ErrorOn );
00200                 GPRS_addCommand( AT_COMMAND_ENABLE_ERROR_MESSAGES, 0 * SECOND );
00201             }
00202             
00203         break;
00204         
00205         case GPRS_ErrorOn :
00206         
00207             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00208                 GPRS_clearResponseBuffer();
00209                 GPRS_setState( GPRS_AddPin );
00210                 GPRS_addCommand( g_pcPinCommand, 0 * SECOND );
00211             }
00212             
00213         break;
00214         
00215         case GPRS_AddPin :
00216         
00217             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00218                 GPRS_clearResponseBuffer();
00219                 GPRS_setState( GPRS_OpenGSM );
00220                 GPRS_addCommand( AT_COMMAND_START_WIP_PACKET, 0 * SECOND );
00221             }
00222             
00223         break;
00224         
00225         case GPRS_OpenGSM :
00226         
00227             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00228                 GPRS_clearResponseBuffer();
00229                 GPRS_setState( GPRS_ChangeCharSet );
00230                 GPRS_addCommand( AT_COMMAND_SELECT_CHARACTERSET, 0 * SECOND );
00231             }
00232             
00233         break;
00234         
00235         case GPRS_ChangeCharSet :
00236         
00237             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00238                 GPRS_clearResponseBuffer();
00239                 GPRS_setState( GPRS_Initialized );
00240             }
00241             
00242         break;
00243         // at this point the GPRS module is ready to connect to the bearer
00244         // the program can be here in two case:
00245         //                                      - gprs module is inizialized after starting and waiting for the first connection
00246         //                                      - we were connected but GPRS_disconectFromTheBearer has been called
00247         case GPRS_Initialized :
00248         // do nothing until someone wants to connecting to the the bearer
00249         break;
00250         // start connecting to the bearer, call GPRS_connectTotheBearer function
00251         case GPRS_OpenGPRS :
00252         
00253             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00254                 GPRS_clearResponseBuffer();
00255                 GPRS_setState( GPRS_OpenBearer );
00256                 GPRS_addCommand( AT_COMMAND_OPEN_BEARER, 0 * SECOND );
00257             }
00258             
00259         break;
00260         
00261         case GPRS_OpenBearer :
00262         
00263             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00264                 GPRS_clearResponseBuffer();
00265                 GPRS_setState( GPRS_SetConnectionPoint );
00266                 GPRS_addCommand( g_pcBearerCommand, 0 * SECOND );
00267             }
00268             
00269         break;
00270         
00271         case GPRS_SetConnectionPoint :
00272         
00273             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00274                 GPRS_clearResponseBuffer();
00275                 GPRS_setState( GPRS_AddUsername );
00276                 GPRS_addCommand( g_pcUsernameCommand, 0 * SECOND );
00277             }
00278             
00279         break;
00280         
00281         case GPRS_AddUsername :
00282         
00283             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00284                 GPRS_clearResponseBuffer();
00285                 GPRS_setState( GPRS_AddPassword );
00286                 GPRS_addCommand( g_pcPasswordCommand, 0 * SECOND );
00287             }
00288             
00289         break;
00290         
00291         case GPRS_AddPassword :
00292         
00293             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00294                 GPRS_clearResponseBuffer();
00295                 GPRS_setState( GPRS_ConnectToBearer );
00296                 GPRS_addCommand( AT_COMMAND_START_BEARER, 0 * SECOND );
00297                 // 30 is the number of the attempts of reconection to the bearer
00298                 ucAttempt = 30;
00299             }
00300             
00301         break;
00302         
00303         case GPRS_ConnectToBearer :
00304         
00305             // if during the connection everything went well 
00306             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00307                 GPRS_clearResponseBuffer();
00308                 //  retrive the number of the connection attempts
00309                 ucAttempt = 30;
00310                 GPRS_setState( GPRS_ConnectedToBearer );
00311             }
00312             // if failed to connect to the bearer
00313             if ( strstr( g_pcGPRSResponseBuffer, "ERROR" ) ) {
00314                 GPRS_clearResponseBuffer();
00315                 // try until ucAttempt number didn't reached zero
00316                 if ( --ucAttempt > 0 ) {
00317                     GPRS_wait( 2 * SECOND, g_eGPRSstate );
00318                     // send again the start command
00319                     GPRS_addCommand( AT_COMMAND_START_BEARER, 0 * SECOND );
00320                 // if we don't have any attempt left
00321                 } else {
00322                     GPRS_setState( GPRS_CloseGPRS );
00323                     GPRS_addCommand( AT_COMMAND_CLOSE_IP_STACK, 0 * SECOND );
00324                     // TODO ezt a hibat lekezelni
00325                 }
00326             }
00327             
00328         break;
00329         
00330         // we're connected to the bearer
00331         // the program can be here in two case:
00332         //                                   - we have just connected to the bearer at very first time
00333         //                                   - tcp/ip stack is closed by WIPCLOSE at command
00334         case GPRS_ConnectedToBearer :
00335         // waiting to post something, call GPRS_HTTPPost function
00336         break;
00337         // start posting
00338         case GPRS_WIPCreate :
00339         
00340             if ( strstr( g_pcGPRSResponseBuffer, "+WIPREADY:" ) ) {
00341                 GPRS_clearResponseBuffer();
00342                 //  retrive the number of the connection attempts
00343                 ucAttempt = 30;
00344                 GPRS_setState( GPRS_WIPData );
00345                 GPRS_addCommand( AT_COMMAND_OPEN_TCP_SOCKET, 0 * SECOND );
00346             }
00347             if ( strstr( g_pcGPRSResponseBuffer, "ERROR" ) ) {
00348                 
00349                 GPRS_clearResponseBuffer();
00350                 
00351                 if ( --ucAttempt > 0 ) {
00352                     GPRS_wait( 2 * SECOND, g_eGPRSstate );
00353                     // send again the g_pcCreateConnectionCommand command (this command has been set by the GPRS_HTTPPost function)
00354                     GPRS_addCommand( g_pcCreateConnectionCommand, 0 * SECOND );
00355                 } 
00356                 // if we have don't have more attempt left, close the stack 
00357                 else {
00358                     GPRS_setState( GPRS_WIPClose );
00359                     GPRS_addCommand( AT_COMMAND_CLOSE_TCP_SOCKET, 0 * SECOND );
00360                     // TODO ezt a hibat lekezelni
00361                 }
00362             }
00363         break;
00364         
00365         case GPRS_WIPData :
00366         
00367             if ( strstr( g_pcGPRSResponseBuffer, "CONNECT" ) ) {
00368                 
00369                 GPRS_clearResponseBuffer();
00370                 GPRS_setState( GPRS_SendingData );
00371                 sprintf( g_pcFileCommand, 
00372                         "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", 
00373                         g_pcURL, g_pcHost, strlen( g_pcData ) );
00374                 GPRS_addCommand( g_pcFileCommand, 1 * SECOND ); 
00375                 
00376             }
00377             if ( strstr( g_pcGPRSResponseBuffer, "ERROR" ) ) {
00378                 
00379                 // if any error occures during posting close the stack and go back to the GPRS_ConnectedToBearer state
00380                 GPRS_clearResponseBuffer();
00381                 GPRS_setState( GPRS_EscapeFromDataMode );
00382                 GPRS_addCommand( AT_COMMAND_SWITCH_TO_AT_MODE, 1 * SECOND );
00384             }
00385         break;
00386         
00387         case GPRS_SendingData:
00388         
00389             GPRS_clearResponseBuffer();
00390             GPRS_setState( GPRS_Connected );
00391             // g_pcData contains the inforamtion we want to post and we past to the GPRS_HTTPPost function
00392             UART2_write(g_pcData, g_uiDataLength);
00393         
00394         case GPRS_Connected :
00395         
00396             if ( strchr( g_pcGPRSResponseBuffer, GPRS_ETX_CHAR ) && strstr( g_pcGPRSResponseBuffer, "SHUTDOWN" ) ) {
00397 
00401                 GPRS_clearResponseBuffer();
00402                 // close the tcp/ip stack and go back to the GPRS_ConnectedToBearer state
00403                 GPRS_setState( GPRS_EscapeFromDataMode );
00404                 GPRS_addCommand( AT_COMMAND_SWITCH_TO_AT_MODE, 1 * SECOND );
00405             }
00406 
00407         break;
00408         
00409         case GPRS_WIPClose :
00410         
00411             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00412                 // after this the tcp/ip stack is closed 
00413                 // go back to the GPRS_ConnectedToBearer state
00414                 GPRS_clearResponseBuffer();
00415                 GPRS_setState( GPRS_ConnectedToBearer );
00416             }
00417             
00418         break;
00419         // we can reached this point only that case if GPRS_disconnectFromTheBearer function is has been called explicitly
00420         case GPRS_DisconnectingFromBearer :
00421         
00422             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00423                 GPRS_setState( GPRS_CloseGPRS );
00424                 GPRS_addCommand( AT_COMMAND_CLOSE_IP_STACK, 0 * SECOND );
00425             }
00426         break;
00427         
00428         case GPRS_CloseGPRS :
00429         
00430             if ( strstr( g_pcGPRSResponseBuffer, "OK" ) ) {
00431                 GPRS_setState( GPRS_Initialized );
00432             }
00433             
00434         break;
00435     }
00436 }
00437 
00438 
00440 void GPRS_connectToBearer(void)
00442 {
00443     // do nothing until we are not in the GPRS_Initialized state
00444     if ( g_eGPRSstate != GPRS_Initialized ) {
00445         
00446         return;
00447     }
00448 
00449     GPRS_setState( GPRS_OpenGPRS );
00450     GPRS_addCommand( AT_COMMAND_START_IP_STACK, 0 * SECOND );
00451 }
00452 
00453 
00455 void GPRS_disconnectFromBearer(void)
00457 {
00458     g_iShouldDisconnect = 1;
00459 }
00460 
00461 
00463 void GPRS_initCommandQueue(tDinamicArray* pDinamicArray)
00465 {
00466     Dinamic_Array_Init(pDinamicArray);
00467 }
00468 
00469 
00471 void GPRS_appendResponse(const char* pcResponse)
00473 {
00474     unsigned int uiLength = strlen(pcResponse);
00475     if(uiLength + g_pcGPRSResponseBufferEnd < g_pcGPRSResponseBuffer + GPRS_RESPONSE_BUFFER_SIZE){
00476         
00477         strcat(g_pcGPRSResponseBufferEnd, pcResponse);
00478         g_pcGPRSResponseBufferEnd += uiLength;
00479     }
00480     
00481     else{
00482         
00483         // buffer is full 
00484     }   
00485 }
00486 
00487 
00489 void GPRS_clearResponseBuffer(void)
00491 {
00492     g_pcGPRSResponseBufferEnd = g_pcGPRSResponseBuffer;
00493     *g_pcGPRSResponseBufferEnd = 0;
00494 }
00495 
00496 
00498 void GPRS_wait(tDateTime sWaitDuration, tGPRS_state eNextState)
00500 {
00501         
00502     Time_getCurrentDateTime( &g_GPRSdeadline );
00503     g_GPRSdeadline += sWaitDuration;
00504     g_eGPRSnextState = eNextState;
00505     g_eGPRSstate = GPRS_Waiting;
00506 }
00507 
00508 
00510 void GPRS_httpPost( char* pcHost, char* pcPort, char* pcURL, char* pcData, unsigned int uiDataLength)
00512 {
00513     
00514     if ( g_eGPRSstate != GPRS_ConnectedToBearer ) {
00515         
00516         //  do nothing until we aren't connected    
00517         return;
00518     }
00519     GPRS_setState( GPRS_WIPCreate );
00520     sprintf( g_pcCreateConnectionCommand, "AT+WIPCREATE=2,1,\"%s\",%s\r\n", pcHost, pcPort );
00521     GPRS_addCommand( g_pcCreateConnectionCommand, 0 * SECOND );
00522     // set the globel variables according the function paramters given
00523     g_pcHost = pcHost;
00524     g_pcPort = pcPort;
00525     g_pcURL = pcURL;
00526     g_pcData = pcData;
00527     g_uiDataLength = uiDataLength;
00528 }
00529 
00530 
00531 
00533 void GPRS_addCommand(char* pcCommand, tDateTime sTimeoutDuration)
00535 {
00536     // this memory space set free after the command has been sent in the GPRS_processCommand function
00537     tCommandInstance* ptCommandInstanceTmp = (tCommandInstance*)malloc(sizeof(tCommandInstance)*1);
00538     ptCommandInstanceTmp->pcCommand = pcCommand;
00539     ptCommandInstanceTmp->sWaitingDuration = sTimeoutDuration;
00540     // put the command into the queue
00541     Dinamic_Array_Add(&g_GPRS_CommandQueue, ptCommandInstanceTmp);
00542 }
00543 
00544 
00546 void GPRS_setState(tGPRS_state newState)
00548 {
00549     g_eGPRSstate = newState;
00550 }
00551 
00552 
00554 tBoolean GPRS_isDeadlinePassed(void)
00556 {
00557     tDateTime Now;
00558     Time_getCurrentDateTime(&Now);
00559     return ((Now > g_GPRSdeadline )? 1 : 0);
00560 }
 All Data Structures Files Functions Variables Typedefs Enumerations Defines