Mobil_surveillance_system 1
Functions | Variables
GPS.c File Reference

GPS source file. More...

#include "GPS.h"

Go to the source code of this file.

Functions

void GPS_executeGPS (void)
void GPS_setupGPS (void)
void GPS_setGPShttpPostCallbackFunction (tpfGPSHttpPostCallbackFunction pfCallbackGPRShttpPost)
void GPS_initGPS (void)
tBoolean GPS_isResponseBufferFull (void)
void GPS_clearResponseBuffer (void)
tBoolean GPS_isCommandSent (void)
void GPS_setDeadline (unsigned int uiDeadline)
tBoolean GPS_isDeadlinePassed (void)
void GPS_setState (tGPS_state newState)
void GPS_setNextState (tGPS_state newState)
tGPS_state GPS_getGPSstate (void)
void GPS_addCommand (const unsigned char *pcCommand)
void GPS_goToNextCommand (void)
tBoolean GPS_isCommandAvaiable (void)
void GPS_sendCommand (void)
void GPS_processCommand (void)
void GPS_appendResponse (const char *pcResponse)
tNMEA_messages GPS_NMEAProcess (char *pcNmeaPacket)
void GPS_clearGPSInfoPacket (tGPSInfo *pGPSInfoPacket)
void GPS_ProcessGPGGA (const char *pcFoundPacket, tGPSInfo *pGPSInfoPacket)

Variables

const unsigned char g_pcConfigureNmeaMessage [] = {0xA0, 0xA1, 0x00, 0x09, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x0D, 0x0A}
 Only GGA and RMC messages will be recived.
const unsigned char g_pcConfigureMessageType [] = {0xA0, 0xA1, 0x00, 0x03, 0x09, 0x01, 0x00, 0x08, 0x0D, 0x0A}
 Output message type will be NMEA.
const unsigned char g_pcConfigureSystemPowerMode [] = {0xA0, 0xA1, 0x00, 0x03, 0x0C, 0x01, 0x00, 0x0C, 0x0D, 0x0A}
 Power save mode.
const unsigned char g_pcConfigureSystemPositionRate [] = {0xA0, 0xA1, 0x00, 0x03, 0x0E, 0x10, 0x00, 0x0F, 0x0D, 0x0A}
 Update rate is 10Hz.
const unsigned char g_pcConfigureSystemRestart [] = {0xA0, 0xA1, 0x00, 0x0F, 0x01, 0x01, 0xD6, 0x07, 0x0C, 0x12, 0x08, 0x32, 0x29, 0xC4, 0x09, 0x70, 0x30, 0x64, 0x00, 0x35, 0x0D, 0x0A}
 Restart the gps and configure the basic properties.
const unsigned char g_pcConfigureSerialPort [] = {0xA0, 0xA1, 0x00, 0x04, 0x05, 0x00, 0x05, 0x00, 0x05, 0x0D, 0x0A}
 Configure boud rate to 9600 baud.
tGPS_state g_eGPSstate
 The current state of the GPS.
tGPS_state g_eGPSnextState
 The next state of the GPS, after waiting period expire.
tDateTime g_GPSdeadline = 0
 This variable can be use to decide to if the waiting period expired.
tGPSInfoArray g_GPSInfoArray
 Array of the GPSIfo packets.
tDinamicArray g_GPS_CommandQueue
 Dinamic array of GPS commands.
tpfGPSHttpPostCallbackFunction g_pfOnCallbackGPRShttpPost = NULL
 Function pointer to the GPRShttpPost function.
char * g_pcGPSResponseBuffer = NULL
 Buffer for the GPS answers.
char * g_pcGPSResponseBufferEnd = NULL
 This pointer indicates the end of the GPSResponseBuffer.
char g_nmeaPacket [PACKET_SIZE] = {'\0'}
 Array of nmeaPackets.
tGPSInfo g_GPSInfoGpsPacket = {'\0'}
 Global variable for the GPSInfoPacket.
tBoolean g_GPS_GPGGApart = 0
 Flag indicates if the first part of the GPSInfoGpsPacket has just received.
tBoolean g_GPS_GPRMCpart = 0
 Flag indicates if the second part of the GPSInfoGpsPacket has just received.

Detailed Description

GPS source file.

This file contains the definations of the GPS unit's functions.

Definition in file GPS.c.


Function Documentation

void GPS_addCommand ( const unsigned char *  pcCommand)
Parameters:
pcCommand

Definition at line 317 of file GPS.c.

{
    // TODO kell e mely masolat ?
    Dinamic_Array_Add(&g_GPS_CommandQueue, pcCommand);
}
void GPS_appendResponse ( const char *  pcResponse)
Parameters:
pcResponse

Definition at line 364 of file GPS.c.

{
    unsigned int uiLength = strlen(pcResponse);
    if(uiLength + g_pcGPSResponseBufferEnd < g_pcGPSResponseBuffer + GPS_RESPONSE_BUFFER_SIZE){
        
        strcat(g_pcGPSResponseBufferEnd, pcResponse);
        g_pcGPSResponseBufferEnd += uiLength;
    }
    
    else{
        
        // buffer is full 
    }   
}
void GPS_clearGPSInfoPacket ( tGPSInfo pGPSInfoPacket)
Parameters:
pGPSInfoPacket

Definition at line 446 of file GPS.c.

{
    tGPSInfo g_GPSInfoBlank = {0}; 
    *pGPSInfoPacket = g_GPSInfoBlank;
}
void GPS_executeGPS ( void  )

Todo:
Http post utani teendok.
  • ha a kuldes sikeres volt akkor a queue-t torolni
  • callback-ben mindig tesztelni, hogy connectolva vagyunk-e a bearer-hez
  • ha nem akkor connectToBearer, ha igen akkor httpPost

Definition at line 87 of file GPS.c.

{
    if (g_eGPSstate < GPS_Inizialized){
        
        GPS_setupGPS();
    }   
    
    else if ( g_eGPSstate == GPS_InitError){
        
        // lid 2.3 pin led
        Ports_setPort2Pin(LED_NUMBER_3);
        
        _disable_interrupts();
        // stop cpu
        _bis_SR_register(LPM4_bits);    
    }
    
    else if ( g_eGPSstate == GPS_Inizialized){
        
        tNMEA_messages test;
        // start parsing the nmea sentences
        test = GPS_NMEAProcess(g_nmeaPacket);
                
        if(test == NMEA_GPGGA){
                    
            GPS_ProcessGPGGA(g_nmeaPacket, &g_GPSInfoGpsPacket);
            g_GPS_GPGGApart = 1;
        }
                
        else if(test == NMEA_GPRMC){
                    
            g_GPS_GPRMCpart = 1;
        }
                
        if(g_GPS_GPGGApart && g_GPS_GPRMCpart){
                    
            g_GPS_GPGGApart = 0;
            g_GPS_GPRMCpart = 0;
            // put the received packet into the queue
            GPSpacket_pushGPSInfoArray(&g_GPSInfoArray, &g_GPSInfoGpsPacket);
            GPS_clearGPSInfoPacket(&g_GPSInfoGpsPacket);
            
        }
        
        // if we have any data in the queue make an attempt to post through GPRS
        if(g_GPSInfoArray.uiIdx){
            
            if(g_pfOnCallbackGPRShttpPost){
                
                g_pfOnCallbackGPRShttpPost("host", "3010", "url", (char*)GPSpacket_getGPSInfoArray(&g_GPSInfoArray), sizeof(tGPSInfo) * g_GPSInfoArray.uiCurrentSize);
            }
            
        }
        
    }
}
tGPS_state GPS_getGPSstate ( void  )
Returns:

Definition at line 309 of file GPS.c.

{
    return g_eGPSstate;
}
tBoolean GPS_isCommandAvaiable ( void  )
Returns:

Definition at line 334 of file GPS.c.

{
    return ((g_GPS_CommandQueue.uiSize == 0) ? 1 : 0);
}
tBoolean GPS_isCommandSent ( void  )
Returns:

Definition at line 265 of file GPS.c.

{
    return (UART1_isWritingDone()); 
}
tBoolean GPS_isDeadlinePassed ( void  )
Returns:

Definition at line 283 of file GPS.c.

{
    tDateTime Now;
    Time_getCurrentDateTime(&Now);
    return ((Now > g_GPSdeadline )? 1 : 0);
}
tBoolean GPS_isResponseBufferFull ( void  )
Returns:

Definition at line 248 of file GPS.c.

{
    return ((strlen(g_pcGPSResponseBuffer) == (GPS_RESPONSE_BUFFER_SIZE-1)) ? 1: 0);
}
tNMEA_messages GPS_NMEAProcess ( char *  cpNmeaPacket)
Parameters:
cpNmeaPacket
Returns:

Definition at line 383 of file GPS.c.

{
    // clear nmeapacket
    memset(pcNmeaPacket, '\0', strlen(pcNmeaPacket));
    // no data will send
    tNMEA_messages foundpacket = NMEA_NODATA;
    tBoolean startFlag = 0;
    // if the buffer is empty we don't have to do anything...
    if(g_pcGPSResponseBuffer != g_pcGPSResponseBufferEnd){
        // pharse until the all character in the buffer has been tested.
        while(*g_pcGPSResponseBuffer++){
            if(*g_pcGPSResponseBuffer == '$'){
                startFlag = 1;
                break;
            }
            else{
                continue;
            }
        }
    } 
    unsigned uiAvail = strlen(g_pcGPSResponseBuffer);
    unsigned char i;
    if(startFlag){
        // decrase uiAvail $ character dosen't count...
        for(i = 1; i < (uiAvail - 1); i++){
            if((g_pcGPSResponseBuffer[i] == '\r') && (g_pcGPSResponseBuffer[i+1] == '\n')){
                unsigned char j;
                for(j = 0; j < (i - 1); j++){
                    *pcNmeaPacket++ = g_pcGPSResponseBuffer[j];
                }
                // Add a null termination to the string
                *pcNmeaPacket = 0;
                foundpacket = NMEA_UNKNOWN;
                break;
            }
        }
        if(foundpacket){
            if(!strncmp(pcNmeaPacket, "GPGGA", 5)){
                // if we've found a correct packet we can clear the response buffer
                GPS_clearResponseBuffer();
                foundpacket = NMEA_GPGGA;
            }
            else if(!strncmp(pcNmeaPacket, "GPRMC", 5)){
                // if we've found a correct packet we can clear the response buffer
                GPS_clearResponseBuffer();
                foundpacket = NMEA_GPRMC;
            }
            // default  foundpacket = NMEA_NODATA   
        }
        else if(GPS_isResponseBufferFull()){
            // if we did'nt find packet and the buffer is full
            // we're logjammed, flush entire buffer
            GPS_clearResponseBuffer();
        }
    }
    // return the type of the found pakcet
    // if no packet has found the function will return with NMEA_NODATA
    return foundpacket;
}
void GPS_ProcessGPGGA ( const char *  pcFoundPacket,
tGPSInfo pGPSInfoPacket 
)
Parameters:
pcFoundPacket
pGPSInfoPacket

Definition at line 455 of file GPS.c.

{
    // temporaly buffer to store the part of the packet wants to be converted
    char cpTmpBuffer[20] = {'\0'};
    // index for array is pointed by the pcFoundPacket
    unsigned int i;
    // for the strtod functions
    char* endptr;
    // start parsing just after "GPGA"
    i = 6;
    // an attempt to reject the empty packet
    if(pcFoundPacket[i] == ',' && pcFoundPacket[i+1] == ','){
        return;
    }
    // Increase the value of index in order to the first digit of the hours will be addressed.
    i++;
    // get the hour from the packet
    strncpy(cpTmpBuffer,&pcFoundPacket[i], 2);
    pGPSInfoPacket->currentTime.ucHour = (unsigned char)atoi(cpTmpBuffer);
    // clear the temporaly buffer
    memset(cpTmpBuffer, sizeof(cpTmpBuffer),0);
    // Step forward to the place where the min begins.
    i+=2;
    // get the minute from the packet 
    strncpy(cpTmpBuffer,&pcFoundPacket[i], 2); 
    pGPSInfoPacket->currentTime.ucMin = (unsigned char)atoi(cpTmpBuffer);
    // clear the temporaly buffer
    memset(cpTmpBuffer, sizeof(cpTmpBuffer),0);
    // Step forward to the place where the sec begins.  
    i+=2; 
    // get the sec from the packet 
    strncpy(cpTmpBuffer,&pcFoundPacket[i], 2);    
    pGPSInfoPacket->currentTime.ucSec = (unsigned char)atoi(cpTmpBuffer);
    // clear the temporaly buffer
    memset(cpTmpBuffer, sizeof(cpTmpBuffer),0);
    // Step forward to the place where the msec begins.
    i+=3;   
    // get the msec from the packet     
    strncpy(cpTmpBuffer,&pcFoundPacket[i], 3);    
    pGPSInfoPacket->currentTime.ucSec = atoi(cpTmpBuffer);  
    // clear the temporaly buffer
    memset(cpTmpBuffer, sizeof(cpTmpBuffer),0);                                                      
    // next field: latitude
    while(pcFoundPacket[i++] != ',');
    // get latitude
    pGPSInfoPacket->currentPosition.latitude = strtod(&pcFoundPacket[i], &endptr);
    // Step forward where the latitude indicator begins.
    i++;
    // if the direction is South set the forth bit of the direction variable
    if(pcFoundPacket[i] == 'S'){
        SET_BIT(pGPSInfoPacket->currentPosition.altitudeFractPartAndDir, 3);
    }
    // if the direction is North clear the forth bit of the direction variable
    else{
        CLEAR_BIT(pGPSInfoPacket->currentPosition.altitudeFractPartAndDir, 3);
    }
    // get longitude
    pGPSInfoPacket->currentPosition.longitude = strtod(&pcFoundPacket[i], &endptr);
    // if the direction is West set the fifth bit of the direction variable
    // increase the endptr by one because now it's pointing to the comma
    if(*(endptr+1) == 'W'){
        SET_BIT(pGPSInfoPacket->currentPosition.altitudeFractPartAndDir, 4);
    }
    // if the direction is East clear it
    else{
        CLEAR_BIT(pGPSInfoPacket->currentPosition.altitudeFractPartAndDir, 4);
    }
    // next field: gps gualitiy indicator, we don't need it, so skip it
    while(pcFoundPacket[i++] != ',');
    // next field: satellites used, we don't need it, so skip it
    while(pcFoundPacket[i++] != ',');
    // next field: HDOP, we don't need it, so skip it
    while(pcFoundPacket[i++] != ',');
    // next field: altitude
    while(pcFoundPacket[i++] != ',');
    // now we're pointing to the first figure of the altitude
    i++;
    unsigned char j = 0;
    while(pcFoundPacket[i] != '.'){
        // copy the intiger part of the altitude to the temporaly buffer
        cpTmpBuffer[j++] = pcFoundPacket[i++];
    }
    // get the intiger part of the altitude
    pGPSInfoPacket->currentPosition.altitudeFractPartAndDir = atoi(cpTmpBuffer);
    // clear the temporaly buffer
    memset(cpTmpBuffer, sizeof(cpTmpBuffer),0);
    // now we're pointig the fractional part of the altitude
    i++;
    // get the franctional part of the altitude
    OR(pGPSInfoPacket->currentPosition.altitudeFractPartAndDir, (unsigned char)atoi(&pcFoundPacket[i]));
}
void GPS_setDeadline ( unsigned int  uiDeadline)
Parameters:
uiDeadline

Definition at line 273 of file GPS.c.

{
    tDateTime Now;
    Time_getCurrentDateTime(&Now);
    g_GPSdeadline = Now + uiDeadline; 
}
void GPS_setGPShttpPostCallbackFunction ( tpfGPSHttpPostCallbackFunction  pfCallbackGPRShttpPost)
Parameters:
pfCallbackGPRShttpPost

Definition at line 219 of file GPS.c.

{
    g_pfOnCallbackGPRShttpPost = pfCallbackGPRShttpPost;
}
void GPS_setNextState ( tGPS_state  newState)
Parameters:
newState

Definition at line 301 of file GPS.c.

{
    g_eGPSnextState = newState;
}
void GPS_setState ( tGPS_state  newState)
Parameters:
newState

Definition at line 293 of file GPS.c.

{
    g_eGPSstate = newState;
}
 All Data Structures Files Functions Variables Typedefs Enumerations Defines