/* aprsString.h */

#ifndef APRSSTRING
#define APRSSTRING
#define MAXPKT 256
#define MAXPATH 22    /* longest path we can parse */
#define MAXISPATH 18  /* Largest Internet System path allowed (CALL + DEST + 8 digi + qAc + N other things :) */
#define MAXRFPATH 10 
#define MAXISHOPS (MAXISPATH-MAXRFPATH-1)  


using namespace std;

#include <string>
#include "constant.h"

//-----------------------------------

//Constants for EchoMask.  Each aprsSting object has this variable.
//These allow each Internet listen port to filter
//data it needs to send.

 static const echomask_t srcTNC = 1;            //data from TNC
 static const echomask_t srcUSER = 2;           //data from any logged on internet user
 static const echomask_t srcUSERVALID = 4;      //data from validated internet user
 static const echomask_t srcIGATE = 8;          //data from another IGATE
 static const echomask_t srcSYSTEM = 16;        //data from this server program
 static const echomask_t srcUDP = 32;           //data from UDP port
 static const echomask_t srcHISTORY = 64;       //data fetched from history list
 static const echomask_t src3RDPARTY = 128;     //data is a 3rd party station to station message
 static const echomask_t srcSTATS = 0x100;      //data is server statistics
 static const echomask_t srcBEACON = 0x200;     //data is from internal beacon for ID
//
 static const echomask_t wantREJECTED = 0x400;   //user wants rejected packets
 static const echomask_t wantSRCHEADER = 0x0800;//User wants Source info header prepended to data
 static const echomask_t wantHTML = 0x1000;     //User wants html server statistics (added in version 2.1.2)
 static const echomask_t wantRAW = 0x2000;      //User wants raw data only
 static const echomask_t sendDUPS = 0x4000;     //if set then don't filter duplicates
 static const echomask_t sendHISTORY = 0x8000;  //if set then history list is sent on connect
 static const echomask_t wantECHO = 0x10000;    //Echo users data back to him
 static const echomask_t omniPortBit = 0x80000000;  //User defines his own echo mask
 //
 static const int APRSMSGACK = 31;        //These three are for the msgType variable
 static const int APRSMSGTEXT = 32;       // and indicate what kind of message it is.
 static const int APRSMSGQUERY = 33; 
 static const int APRSMSGSERVER = 34;    //SERVER command embedded in a message
//-------------------------------------

 //Packet types
 static const int APRSMIC_E = 20;
 static const int APRSOBJECT = 9;
 static const int COMMENT = 10;
 static const int APRSID = 11;  //Station ID packet eg: WA4DSY>ID......
 static const int CONTROL = 12; //User control command to configure his port
 static const int NMEA = 100;
 static const int APRS3RDPARTY = 8;
 static const int APRSREFORMATTED = 8;
 static const int APRSQUERY = 7;
 static const int APRSSTATUS = 6;
 static const int APRSTCPXX = 5;
 static const int APRSLOGON = 4;
 static const int APRSMSG = 3;
 static const int APRSWX = 2;
 static const int APRSPOS = 1;
 static const int APRSUNKNOWN = 0;
 static const int APRSREJECT = -1;

 //Command Types

 static const int CMDNULL = 0;
 static const int CMDPORTFILTER = 1;
 static const int CMDLOCK = 2;
 



       
 static const int MAXWORDS = 8;

 static const unsigned int ttlSize = 5; //String length of a TTL path element
 //Return codes from decrementTTL()
 static const int ttlExpired = -1;
 static const int ttlNotPresent = 1;
 static const int ttlSuccess = 0;
 static const int TTLSIZE = 5;      //Length of TTL or Internet Injection point string

 extern int ttlDefault;    //Make ttlDefault available to other modules
 
class aprsString: public string
{  
 
      
  
public:  
   
   static int NN;             //Tells how many objects exist
   static long objCount;      //Total of objects created (used as ID)
   static pthread_mutex_t* pmtxCounters;
   long ID;                   //Unique ID number for this object
   int instances ;             //Number of pointers to this object that exist in user queues
   int tcpxx;                 //TRUE = TCPXX or TCPXX* was in the path
   int reformatted;           //TRUE = packet has been reformatted into 3rd party format
   int normalized;            //TRUE = packet has been converted to normal from 3rd party
   BOOL cci;                  //TRUE = converted from CallSign,I to qAR.
   time_t timestamp;          // time it was created
   int ttl;                   // time to live (minutes) in history list
   INT32 hash;                // 32 bit hash code of this packet
   time_t timeRF;             // time this was sent on RF (0 = never)
   aprsString* next;          //Only used if this is part of a linked list
   aprsString* last;          // ditto..
   int localSource;           //TRUE = source station is local
   int nogate;                //TRUE = do not igate this packet
   int aprsType;              //Type of packet (comment, 3rdparty etc.)
   int cmdType;               //User command code
   BOOL valid_ax25;           //Validated as a good ax25 packet with PATH and DATA parts
   int sourceSock;            //tcpip socket this came from
   int pathSize;              //Length of the path part in bytes
   int thirdPartyPathSize;    //Length of the 3rd party path in bytes
   echomask_t EchoMask;       //Set a bit to indicate where this object came from
   long FilterMask;           //Mask for USER selected regional data streams
   int dest;                  //Where it's going, destTNC or destINET

   size_type gtIdx;           //character position of the ">" symbol
   size_type pIdx;            //Character position of the ":" symbol
   size_type opIdx;           //Character position of the "(" symbol
   size_type cpIdx;           //Character position of the ")" symbol
   size_type dataIdx;         //index of data part of this packet, usually pIdx + 1
   int IjpIdx;                //index to path element that has IS Injection point ( qAc )
   int IjpOffset;             //Offset in characters from start of packet of the Injection point
   
  
   int allowdup;              //TRUE or FALSE, object is a duplicate (for redundent ACKs)
   int msgType;               //Indicates if message is an ACK or just text
   //BOOL Expired;              //True indicates this packet has expired (time-to-Live is zero)
                
   BOOL AEA;

   string ax25Source;         //ax25 source
   string ax25Dest ;          //ax25 destination
   string stsmDest;           //Station to Station Message destination
   string path;               //Raw ax25 path  (up to but not including the colon)
   string data;               //Raw ax25 data (all following first colon)
   string ax25Path[MAXPATH];
   string thirdPartyPath[MAXPATH];
   string words[MAXWORDS];
   string user;
   string pass;
   string pgmName;
   string pgmVers;
   string peer;
   string call;
   string icall;              //Callsign precceding the "I" in old uiview packets
   string query;              //APRS Query string
   string acknum;             //The "{nnn" at the end of messages
   string raw;                //copy of complete raw packet preserved here
   string srcHeader;          //Source IP and user call header. format example: !192.168.1.1:N0CALL!
    
   
   
   

   
  
   
   aprsString(const char *cp, int s,int e);
   aprsString(const char *cp, int s,int e, const char* szPeer, const char *userCall);
   aprsString(const char *cp);
   aprsString(string& as);
   aprsString(aprsString& as);
   ~aprsString(void);

   void parseAPRS(void);
   void parseLogon(void);
   BOOL parseCommand(void);
   BOOL parsePortFilter(void);
   void getMsgText(string& msg);
   void print(ostream& os);
   string getAX25Source(void);
   string getAX25Dest(void);
   const char* getChar(void);
   echomask_t getEchoMask(void);
   void setEchoMask(echomask_t m);

   //Tells if char string cp is in the ax25 path
   unsigned queryPath(const string& s, int start = 0, int stop=-1, unsigned n = npos);     
   
   BOOL changePath(const char* newPath, const char* oldPath); //Change one path element
   BOOL addPath(const char* cp, char c = ' ');
   BOOL addPath(string s, char c = ' ');
   void trimPath(int n);         //Remove last n path elements
   BOOL queryLocal(void);        //returns true if source call is local
   BOOL igated(void);            //Returns TRUE if packet has been igated to RF
   BOOL thirdPartyReformat(const char *call);   //Convert to 3rd party format
   BOOL thirdPartyToNormal(void);   //Convert 3rd party to normal
   void mic_e_Reformat(aprsString** posit, aprsString** telemetry);
   void printN(void);
   void addInstance(void);
   void removeInstance(void);
   INT32 gethash(INT32 seed = 0);
   BOOL hasBeenDigipeated(const string& s);
   BOOL hasBeenIgated(void);
   BOOL igated(const string& s) ;
   BOOL pathUsed(const string& s);
   void cutUnusedPath(void) ;
   int decrementTTL(void);
   char getPktSource(void);            //get char code for packet source ( C I R U X Z )
   void addIIPPE( char c = ' ');       //add IIPPE  to path             
   
   string removeI(void);
   void createIIPPE(char* out, int size, char c = ' ') ; // Create Internet Injection Point Path Element
   int getIsHops(void);
   int getMaxHops(void) ;
   int changeIIPPE(const char* qs); 
   int findInjectionPoint(void);
   BOOL loopCheck(const string& ucall);   //TRUE if ucall is in IS path. * is ignored
    
   static int getObjCount(void);

private:
   void constructorSetUp(const char *cp, int s, int e);
   BOOL AEAtoTAPR(string& s, string& rs);
   


   


} ;

#endif
