Mobil_surveillance_system 1
Defines | Typedefs | Enumerations | Functions
GPS.h File Reference

GPS header file. More...

#include "Bitmanip.h"
#include "UART1.h"
#include "Time.h"
#include "Dinamic_Array.h"
#include "GPSpacket.h"
#include "Ports.h"

Go to the source code of this file.

Defines

#define ATTEMPT_NUMBER   5
#define GPS_RESPONSE_BUFFER_SIZE   1024
#define PACKET_SIZE   80

Typedefs

typedef void(* tpfGPSHttpPostCallbackFunction )(char *, char *, char *, char *, unsigned int)

Enumerations

enum  tGPS_state {
  GPS_Uninizialized = 0, GPS_SendCommand, GPS_CommandSent, GPS_StarWaitingForTheAnswer,
  GPS_WaitingForTheAnswer, GPS_InitError, GPS_Inizialized
}
enum  tNMEA_messages {
  NMEA_NODATA = 0, NMEA_UNKNOWN, NMEA_GPGGA, NMEA_GPGLL,
  NMEA_GPGSA, NMEA_GPGSV, NMEA_GPRMC, NMEA_GPVTG,
  NMEA_ACK, NMEA_NACK
}

Functions

void GPS_executeGPS (void)
void GPS_initGPS (void)
void GPS_appendResponse (const char *pcResponse)
void GPS_setupGPS (void)
tBoolean GPS_isCommandSent (void)
tBoolean GPS_isResponseBufferFull (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_setGPShttpPostCallbackFunction (tpfGPSHttpPostCallbackFunction pfCallbackGPRShttpPost)
void GPS_addCommand (const unsigned char *pcCommand)
void GPS_goToNextCommand (void)
tBoolean GPS_isCommandAvaiable (void)
void GPS_clearResponseBuffer (void)
void GPS_clearGPSInfoPacket (tGPSInfo *pGPSInfoPacket)
void GPS_sendCommand (void)
void GPS_processCommand (void)
tNMEA_messages GPS_NMEAProcess (char *cpNmeaPacket)
void GPS_ProcessGPGGA (const char *pcFoundPacket, tGPSInfo *pGPSInfoPacket)

Detailed Description

GPS header file.

This file contains the prototypes of the GPS unit functions.

Definition in file GPS.h.


Typedef Documentation

typedef void(* tpfGPSHttpPostCallbackFunction)(char *, char *, char *, char *, unsigned int)

Callback functions functionpointer defination

Definition at line 77 of file GPS.h.


Enumeration Type Documentation

enum tGPS_state

Various states of the GPS module

Definition at line 46 of file GPS.h.

            {
    
    GPS_Uninizialized = 0,
    GPS_SendCommand,
    GPS_CommandSent,
    GPS_StarWaitingForTheAnswer,
    GPS_WaitingForTheAnswer,
    GPS_InitError,
    GPS_Inizialized
    
}tGPS_state;

Types of NMEA messages

Definition at line 60 of file GPS.h.

            {
    
    NMEA_NODATA = 0,                // No data. Packet not available.
    NMEA_UNKNOWN,                   // Packet received but is not known.
    NMEA_GPGGA,                     // Global Positinoning System fix data 
    NMEA_GPGLL,                     // Geographic position - latitude/longitude
    NMEA_GPGSA,                     // GNSS DOP and Active Satellites
    NMEA_GPGSV,                     // GNSS Satellites in view
    NMEA_GPRMC,                     // Recomended Minimum Specific GNSS Data
    NMEA_GPVTG,                     // Course Over Ground and Ground Speed
    NMEA_ACK,                       // ACK response from the gps module
    NMEA_NACK                       // NACK response from the gps module
    
}tNMEA_messages;

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