fdhwlib  2.0.25
/home/kopmann/git-mirror/fdhwlib/fdhwlib/gpsoncore/gpsserver.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           gpsserver.h  -  description
00003                              -------------------
00004     begin                : Mon Dec 9 2002
00005     copyright            : (C) 2002 by Andreas Kopmann
00006     email                : kopmann@ipe.fzk.de
00007  ***************************************************************************/
00008 
00009 
00010 #ifndef GPSSERVER_H
00011 #define GPSSERVER_H
00012 
00013 #include <string>
00014 #include <termios.h>
00015 
00016 // Version control
00017 // Initial release of the GPS Clock, operating from
00018 // mid of 2003 until march 2004.
00019 //#define GPS_VER 100
00020 //#define GPS_MIN_VER 100
00021 
00022 // Version 3.4, changed coding of the GPS Clock state.
00023 // Now the two signals "inhibit" (bit 1) and "exttrig"
00024 // (bit 0) code the four states "normal" (00),
00025 // "exttrig" (01), "veto" (10), "stop" (11).
00026 //#define GPS_VER 340
00027 //#define GPS_MIN_VER 340
00028 
00029 
00030 //#define GPS_VER 350 // ???
00031 //#define GPS_MIN_VER 350
00032 
00033 // GPS Clock sends status message in case of changes in the
00034 // status register, 28-04-2004
00035 //#define GPS_VER 360
00036 //#define GPS_MIN_VER 360
00037 
00038 
00039 // Enable/Disable of signal inputs, Status Messgage changed.
00040 // Lidar frequency changed to 333Hz (?)
00041 // 13-10-2004 ak
00042 //
00043 //#define GPS_VER 400
00044 //#define GPS_MIN_VER 360
00045 
00046 
00047 // Lidar frequency changed to 1000Hz (again).
00048 // Known bug:
00049 //   - The gate generators could not be set individually
00050 // 24-04-2006 ak
00051 //
00052 //#define GPS_VER 500
00053 //#define GPS_MIN_VER 360
00054 
00055 // Lidar frequency 0 / 20 / 334 / 1000Hz (again).
00056 //- a problem with using several GGs at the same time (independently) 
00057 //  has been removed
00058 //- the new version allows to set the Lidar frequency
00059 //  to 0 Hz (=off), 20 Hz, 334 Hz and (new !) 1 kHz.
00060 
00061 // 05-12-2006 ak
00062 //
00063 //#define GPS_VER 600
00064 //#define GPS_MIN_VER 400
00065 
00066 // DT 11/2008
00067 //
00068 // An improper behaviour of generators in case of following settings:
00069 // generator repeat rate > 0 (every 2,3 ... sec) and pulse start time = 0
00070 
00071 // 16-07-2010 thjm
00072 //
00073 #define GPS_VER 700
00074 #define GPS_MIN_VER 400
00075 
00076 
00077 /* Version number of the GPS protocol
00078  */
00079 //#define GPS_PROT_VER 100 // Initial version
00080 #define GPS_PROT_VER 110 // Extented command gettime - transfer both counters
00081 //#define GPS_PROT_VER 120 // Extended get/set generator parameter by status flag
00082 
00083 
00084 #define DEVICE1   "/dev/oncore.serial.%d"   
00085 #define GPSSERVER_PORT 5198
00086 
00087 
00088 #define GPSTEST_MAX 10
00089 #define GPSCMD_MAX 50
00090 
00091 
00092 #include <akutil/simpleserver.h>
00093 #include "gpsclock.h"
00094 
00095 #include <sys/types.h>
00096 
00097 typedef signed char s_char;
00098 typedef u_int32_t   u_int32;
00099 
00100 
00101 /*
00102  * Structure interface between the reference clock support
00103  * ntp_refclock.c and the driver utility routines
00104  */
00105 #define MAXSTAGE  60  /* max median filter stages  */
00106 #define NSTAGE    5  /* default median filter stages */
00107 #define BMAX    128  /* max timecode length */
00108 #define GMT    0  /* I hope nobody sees this */
00109 #define MAXDIAL    60  /* max length of modem dial strings */
00110 
00111 /*
00112  * Line discipline flags. These require line discipline or streams
00113  * modules to be installed/loaded in the kernel. If specified, but not
00114  * installed, the code runs as if unspecified.
00115  */
00116 #define LDISC_STD  0x0  /* standard */
00117 #define LDISC_CLK  0x1  /* tty_clk \n intercept */
00118 #define LDISC_CLKPPS  0x2  /* tty_clk \377 intercept */
00119 #define LDISC_ACTS  0x4  /* tty_clk #* intercept */
00120 #define LDISC_CHU  0x8  /* depredated */
00121 #define LDISC_PPS  0x10  /* ppsclock, ppsapi */
00122 #define LDISC_RAW  0x20  /* raw binary */
00123 
00124 struct refclockproc {
00125   //struct  refclockio io;  /* I/O handler structure */
00126   caddr_t  unitptr;  /* pointer to unit structure */
00127   u_char  leap;    /* leap/synchronization code */
00128   u_char  currentstatus;  /* clock status */
00129   u_char  lastevent;  /* last exception event */
00130   u_char  type;    /* clock type */
00131   const char *clockdesc;  /* clock description */
00132 
00133   char  a_lastcode[BMAX]; /* last timecode received */
00134   u_short  lencode;  /* length of last timecode */
00135 
00136   int  year;    /* year of eternity */
00137   int  day;    /* day of year */
00138   int mday;   /* day of month */
00139   int month;  /* month */
00140   int  hour;    /* hour of day */
00141   int  minute;    /* minute of hour */
00142   int  second;    /* second of minute */
00143   int  msec;    /* millisecond of second */
00144   int  usec;    /* microsecond of second (alt) */
00145   u_long nsec;   /* nanoseconds */
00146   u_long  yearstart;  /* beginning of year */
00147   int  coderecv;  /* put pointer */
00148   int  codeproc;  /* get pointer */
00149   //l_fp  lastref;  /* timecode timestamp */
00150   //l_fp  lastrec;  /* local timestamp */
00151   double  offset;    /* mean offset */
00152   double  disp;    /* sample dispersion */
00153   double  jitter;    /* jitter (mean squares) */
00154   double  filter[MAXSTAGE]; /* median filter */
00155 
00156   /*
00157    * Configuration data
00158    */
00159   double  fudgetime1;  /* fudge time1 */
00160   double  fudgetime2;  /* fudge time2 */
00161   u_int32  refid;    /* reference identifier */
00162   u_char  sloppyclockflag; /* fudge flags */
00163 
00164   /*
00165    * Status tallies
00166     */
00167   u_long  timestarted;  /* time we started this */
00168   u_long  polls;    /* polls sent */
00169   u_long  noreply;  /* no replies to polls */
00170   u_long  badformat;  /* bad format reply */
00171   u_long  baddata;  /* bad data reply */
00172 };
00173 
00174 
00180 struct peer {
00181   struct peer *next;  /* pointer to next association */
00182   struct peer *ass_next;  /* link pointer in associd hash */
00183   //struct sockaddr_in srcadr; /* address of remote host */
00184   //struct interface *dstadr; /* pointer to address on local host */
00185   //associd_t associd;  /* association ID */
00186   u_char  version;  /* version number */
00187   u_char  hmode;    /* local association mode */
00188   u_char  hpoll;    /* local poll interval */
00189   u_char  kpoll;    /* last poll interval */
00190   u_char  minpoll;  /* min poll interval */
00191   u_char  maxpoll;  /* max poll interval */
00192   u_char  burst;    /* packets remaining in burst */
00193   u_int  flags;    /* association flags */
00194   u_char  cast_flags;  /* additional flags */
00195   u_int  flash;    /* protocol error test tally bits */
00196   u_char  last_event;  /* last peer error code */
00197   u_char  num_events;  /* number of error events */
00198   u_char  ttlmax;    /* max ttl/refclock mode */
00199 
00200   /*
00201    * Variables used by reference clock support
00202    */
00203   struct refclockproc *procptr; /* refclock structure pointer */
00204   u_char  refclktype;  /* reference clock type */
00205   u_char  refclkunit;  /* reference clock unit number */
00206   u_char  sstclktype;  /* clock type for system status word */
00207 
00208   /*
00209    * Variables set by received packet
00210    */
00211   u_char  leap;    /* local leap indicator */
00212   u_char  pmode;    /* remote association mode */
00213   u_char  stratum;  /* remote stratum */
00214   s_char  precision;  /* remote clock precision */
00215   u_char  ppoll;    /* remote poll interval */
00216   u_int32  refid;    /* remote reference ID */
00217   //l_fp  reftime;  /* update epoch */
00218 
00219   /*
00220    * Variables used by authenticated client
00221    */
00222   //keyid_t keyid;    /* current key ID */
00223 #ifdef AUTOKEY
00224 #define clear_to_zero assoc
00225   associd_t assoc;  /* peer association ID */
00226   u_int32  crypto;    /* peer status word */
00227 #ifdef PUBKEY
00228   struct  value pubkey;  /* public key */
00229   struct  value certif;  /* certificate */
00230   u_char  *keystr;  /* host name */
00231 #endif /* PUBKEY */
00232   keyid_t  pkeyid;    /* previous key ID */
00233   keyid_t  hcookie;  /* host cookie */
00234   struct cookie pcookie;  /* peer cookie */
00235   struct autokey recauto;  /* autokey */
00236   u_int32  cmmd;    /* peer command */
00237   /*
00238    * Variables used by authenticated server
00239    */
00240   keyid_t  *keylist;  /* session key ID list */
00241   int  keynumber;  /* current key number */
00242   struct autokey sndauto;  /* autokey */
00243 #else /* AUTOKEY */
00244 #define clear_to_zero status
00245 #endif /* AUTOKEY */
00246 
00247   /*
00248    * Ephemeral state variables
00249    */
00250   u_char  status;    /* peer status */
00251   u_char  pollsw;    /* what it says */
00252   u_char  ttl;            /* ttl for manycast mode */
00253   u_char  reach;    /* reachability register */
00254   u_char  unreach;  /* unreachable count */
00255   u_long  epoch;    /* reference epoch */
00256   u_short  filter_nextpt;  /* index into filter shift register */
00257 //  double  filter_delay[NTP_SHIFT]; /* delay shift register */
00258 //  double  filter_offset[NTP_SHIFT]; /* offset shift register */
00259 //  double  filter_disp[NTP_SHIFT]; /* dispersion shift register */
00260 //  u_long  filter_epoch[NTP_SHIFT]; /* epoch shift register */
00261 //  u_char  filter_order[NTP_SHIFT]; /* filter sort index */
00262 //  l_fp  org;    /* originate time stamp */
00263 //  l_fp  rec;    /* receive time stamp */
00264 //  l_fp  xmt;    /* transmit time stamp */
00265   double  offset;    /* peer clock offset */
00266   double  delay;    /* peer roundtrip delay */
00267   double  jitter;    /* peer jitter (squares) */
00268   double  disp;    /* peer dispersion */
00269   double  estbdelay;  /* clock offset to broadcast server */
00270 
00271   /*
00272    * Variables set by received packet
00273    */
00274   double  rootdelay;  /* roundtrip delay to primary clock */
00275   double  rootdispersion;  /* dispersion to primary clock */
00276 
00277   /*
00278    * End of clear-to-zero area
00279    */
00280   u_long  update;    /* receive epoch */
00281 #define end_clear_to_zero update
00282   u_long  outdate;  /* send time last packet */
00283   u_long  nextdate;  /* send time next packet */
00284   u_long  nextaction;  /* peer local activity timeout (refclocks mainly) */
00285 //  void (*action) P((struct peer *)); /* action timeout function */
00286   /*
00287    * Statistic counters
00288    */
00289   u_long  timereset;  /* time stat counters were reset */
00290   u_long  timereceived;  /* last packet received time */
00291   u_long  timereachable;  /* last reachable/unreachable time */
00292 
00293   u_long  sent;    /* packets sent */
00294   u_long  received;  /* packets received */
00295   u_long  processed;  /* packets processed by the protocol */
00296   u_long  badauth;  /* packets cryptosum failed */
00297   u_long  bogusorg;  /* packets bogus origin */
00298   u_long  oldpkt;    /* packets duplicate packet */
00299   u_long  seldisptoolarge; /* packets dispersion to large*/
00300   u_long  selbroken;  /* not used */
00301 };
00302 
00303 /*
00304  * Values for peer.leap, sys_leap
00305  */
00306 #define  LEAP_NOWARNING  0x0  /* normal, no leap second warning */
00307 #define  LEAP_ADDSECOND  0x1  /* last minute of day has 61 seconds */
00308 #define  LEAP_DELSECOND  0x2  /* last minute of day has 59 seconds */
00309 #define  LEAP_NOTINSYNC  0x3  /* overload, clock is free running */
00310 
00311 
00312 
00313 enum receive_state {
00314   ONCORE_NO_IDEA,
00315   ONCORE_ID_SENT,
00316   ONCORE_RESET_SENT,
00317   ONCORE_TEST_SENT,
00318   ONCORE_INIT,
00319   ONCORE_ALMANAC,
00320   ONCORE_RUN
00321 };
00322 
00323 enum site_survey_state {
00324   ONCORE_SS_UNKNOWN,
00325   ONCORE_SS_TESTING,
00326   ONCORE_SS_HW,
00327   ONCORE_SS_SW,
00328   ONCORE_SS_SAVE,
00329   ONCORE_SS_DONE
00330 };
00331 
00332 
00337 enum oncore_model {
00338   ONCORE_BASIC,
00339   ONCORE_PVT6,
00340   ONCORE_VP,
00341   ONCORE_UT,
00342   ONCORE_UTPLUS,
00343   ONCORE_GT,
00344   ONCORE_GTPLUS,
00345   ONCORE_SL,
00346   ONCORE_M12,
00347   ONCORE_UNKNOWN
00348 };
00349 
00350 
00357 struct RSM {
00358   u_char  posn0D;
00359   u_char  posn2D;
00360   u_char  posn3D;
00361   u_char  bad_almanac;
00362   u_char  bad_fix;
00363 };
00364 
00365 
00371 enum posn_mode {
00372   MODE_UNKNOWN,
00373   MODE_0D,
00374   MODE_2D,
00375   MODE_3D
00376 };
00377 
00378 
00379 struct secTiming{
00380   unsigned int gpsSec; // Name of the second
00381   unsigned int oncoreSec; // Oncore counter
00382   unsigned int augerSec; // Second counter of the timing unit
00383   unsigned int pcSec;  // Second counter of the PC while reading
00384   int pcMiliSec;        // Arival of the message
00385   int leapSec;          // Leap seconds
00386 
00387   int phase;            // Phase
00388   //int mSec;             // ms
00389   //int uSec;             // us
00390   //int n125Sec;          // 12.5 ns
00391   int count;            // counter [12.5ns]
00392   int sawtooth;         // neagtive sawtooth parameter
00393 
00394   double T10;           // (Real) length of the "10MHz" signal [ns]
00395   double offset;        // Offset between 1pps signal and "10MHz" signal [ns]
00396 
00397   int quality;          // Quality of the values
00398                         // ???
00399   int ptrCapture;       // Pointer to the first capture timestamp
00400                         // in the capture buffer
00401   int nCapture;         // Number of capture values during this second
00402 
00403 };
00404 
00405 
00406 
00407 struct posData {
00408   int latitude;         //< Latutude
00409   int longitude;        //< Longitude
00410   int height;         //< Height
00411   std::string name;          //< Name
00412   int id;                    //< Id of the eyestation (1..4)
00413 };
00414 
00415 #define T_BUFFER_LEN 100    //< Number of elements in the timing buffer
00416 #define T_CAPTURE_LEN 100   //< Number of time stamps in the capture buffer
00417 #define POS_DATABASE_LEN 10 //< Number of elements in the position data base
00418 
00419 
00420 
00422 enum deviceId {
00423    GPSSERVER,
00424    GPSCLOCKMAX,
00425    GPSCLOCKMIN,
00426    GPSCLOCK,
00427    ONCORE
00428 };
00429 
00430 
00431 struct cmd_desc {
00432   const char *name;
00433   u_char     *data;
00434   int        len;   
00435 };
00436 
00437 
00438 
00439 
00440 class shell;
00441 class cmds;
00442 class GpsClock;
00443 class SimpleSocket;
00444 class procDuration;
00445 
00605 /* Site survey parameters (not active at the moment):
00606   *  - startup Define the startup mode. Known modes are listen , set
00607   *    position name and survey position name.
00608   *    <pre>
00609   *    startup = listen | set <name> | survey < name >
00610   *    </pre>
00611   *  - selftest Selftest at startup.
00612   *
00613   *  - nSurvey Number of measurements used for the site survey
00614   *  - noHwSurvey The standard survey method is hardware, force software survey
00615   *  - resetBeforeSurvey Perform gpsclock reset before starting with the
00616   *    site survey
00617   *
00618   *
00619   *
00620   */
00621   
00622 
00681 class GpsServer : public SimpleServer  {
00682 public: 
00683   GpsServer(char *inifile = "FE.ini");
00684   GpsServer(char *inifile, int port);
00685   ~GpsServer();
00686 
00687 
00688   bool noParameters;
00689 
00691   int handle_timeout();
00692 
00694    void setParameter();
00695 
00697   void requestStatus();
00698   
00700   void initGpsserver();
00701 
00703   void getServerVersion(int& ver,int *min_ver=NULL,int *max_ver=NULL);
00704   
00707   void setServerVersion(int ver, const char *buildnote=0);
00708 
00712   void oncore_requestPositionHoldMode();
00713  
00718   void oncore_enablePositionHoldMode();
00719 
00721   void oncore_disablePositionHoldMode();
00722  
00736   void run(int debugLevel = 0, int unit = -1, int runDaemon = 0);
00737 
00740   int read_from_client(int fd);
00741 
00742   using SimpleServer::executeCmd;
00743   
00745   void executeCmd(int client, short cmd, unsigned int *arg, short n);
00746 
00748   int read_from_keyboard();
00749 
00750 //private:
00751 public:
00752 
00753   int refclock_open(char *dev,  int speed,  int lflags);
00754   
00755   int refclock_ioctl(int fd, int flags);
00756 
00758   int refclock_close();
00759   
00760   int oncore_start();
00761 
00762   void oncore_sendmsg(u_char *ptr, size_t len);
00763 
00764   void auger_sendmsg(u_char *ptr, size_t len);
00765 
00766 
00768   void oncore_setTimezone(int tz);
00769 
00771   void oncore_getTimezone();
00772   
00774   void oncore_selftest();
00775  
00777   void oncore_getId();
00778 
00782   void oncore_reset();
00783 
00785   void auger_captureMonitor();
00786 
00790   void oncore_setInitialTime();
00791 
00795   void oncore_setInitialPosition(char *pos);
00796 
00797         
00798 //protected:
00799 public:
00800 
00804 static u_char oncore_cmd_Ad[];
00805 static u_char oncore_cmd_Ae[];
00806 static u_char oncore_cmd_Af[];
00807 static u_char oncore_cmd_Ga[];
00808 
00813 static u_char oncore_cmd_At0[];  /* Posn Hold off */
00814 static u_char oncore_cmd_At1[];  /* Posn Hold on  */
00815 static u_char oncore_cmd_At2[];  /* Start Site Survey */
00816 static u_char oncore_cmd_At[];  /* Request Site Survey state */
00817 
00821 static u_char oncore_cmd_As[];
00822 static u_char oncore_cmd_Asx[];
00823 static u_char oncore_cmd_Au[];
00824 
00825 static u_char oncore_cmd_Av0[];
00826 static u_char oncore_cmd_Av1[];
00827 
00828 static u_char oncore_cmd_Gd0[];  /* 3D */
00829 static u_char oncore_cmd_Gd1[];  /* 0D */
00830 static u_char oncore_cmd_Gd2[];  /* 2D */
00831 
00835 static u_char oncore_cmd_Aw[];  // Request time mode
00836 static u_char oncore_cmd_Aw0[];  // Set to GPS
00837 static u_char oncore_cmd_Aw1[];  // Set to UTC
00838 
00839 static u_char oncore_cmd_Aa[];  // Request initial time
00840 
00841 static u_char oncore_cmd_Aa0[];  // Set initial time
00842 
00843 static u_char oncore_cmd_Ab[];  // Request time zone
00844 
00845 static u_char oncore_cmd_Ab0[];  // Set time zone UTC
00846 
00847 static u_char oncore_cmd_Ab_set[];  // Set time zone
00848 
00849 static u_char oncore_cmd_Ac[];  // Set initial date
00850                                       
00851 
00855 static u_char oncore_cmd_Be[];
00856 static u_char oncore_cmd_Be0[]; // oupt response message once (polled)
00857 
00861 static u_char oncore_cmd_Ay[];
00862 static u_char oncore_cmd_Ayx[];
00863 
00867 static u_char oncore_cmd_Az[];
00868 static u_char oncore_cmd_Azx[];
00869 
00873 static u_char oncore_cmd_AB[];
00874 
00878 static u_char oncore_cmd_Bb[];
00879 
00884 static u_char oncore_cmd_Bj[];
00885 static u_char oncore_cmd_Gj[];
00886 
00890 static u_char oncore_cmd_Cf[];
00891 
00895 static u_char oncore_cmd_Cg[];
00896 
00900 static u_char oncore_cmd_Cj[];
00901 
00906 static u_char oncore_cmd_Ea[];
00907 static u_char oncore_cmd_Ba[];
00908 static u_char oncore_cmd_Ha[];
00909 static u_char oncore_cmd_Ea0[];
00910 static u_char oncore_cmd_Ea1[];
00911 static u_char oncore_cmd_Ba0[];
00912 
00916 static u_char oncore_cmd_Ek[];  /* just turn off */
00917 
00925 static u_char oncore_cmd_En[];
00926 static u_char oncore_cmd_En0[];
00927 static u_char oncore_cmd_EnDef[];
00928 static u_char oncore_cmd_Bn[];
00929 static u_char oncore_cmd_Bn0[];
00930 
00934 static u_char oncore_cmd_Ca[];  /*  6 Chan */
00935 static u_char oncore_cmd_Fa[];  /*  8 Chan */
00936 static u_char oncore_cmd_Ia[];  /* 12 Chan */
00937 
00938 
00939 // UTC offset status message
00940 static u_char oncore_cmd_Bo0[]; // output UTC offset once (polled)
00941 static u_char oncore_cmd_Bo1[]; // output UTC offset every time it is updated
00942 
00943 
00944 // Auger specific functions
00945 
00967 static u_char auger_cmd_gen_parameter[]; // Set begin/end of trigger time window
00968 
00969 static u_char auger_cmd_gen_read[];
00970 
00998 static u_char auger_cmd_A[]; // Request time window control register
00999 
01007 static u_char auger_cmd_S[]; //
01008 static u_char auger_cmd_S400[]; // version 4.0
01009 
01010 
01020 static u_char auger_cmd_B[]; // Set begin of comm. time window
01021 
01025 static u_char auger_cmd_E[]; // Set end of comm. time window
01026 
01027 
01033 static u_char auger_cmd_F[];    // Enable sending timing information
01034 
01037 static u_char auger_cmd_D[];   //  Disable sending timing information
01038 
01039 
01047 static u_char auger_cmd_T[]; // Set second counter
01048 
01054 static u_char auger_cmd_G[]; // Request second counter
01055 
01056 
01057 static u_char auger_cmd_I[]; // Set software inhibit
01058 static u_char auger_cmd_R[]; // Release software inhibit
01059 
01060 
01061 static u_char auger_cmd_C[]; // Get next captured time stamp
01062 static u_char auger_cmd_N[];  // Get number of captured pulses
01063 
01064 
01065 static u_char auger_cmd_P[];  // Set frequency of internal generators ???
01066 
01067 //private:
01068 public:
01069 
01070 
01071 
01072 static struct msg_desc oncore_messages[];
01076 static struct msg_desc auger_messages[];
01077 
01078 
01080 static struct cmd_desc cmd_list[];
01081 
01083   int getNCmds();
01084 
01085 private:
01086 
01087   void receiveMessages();
01088 
01089 
01093   void oncore_receive(
01094               //struct recvbuf *rbufp
01095               char *buf,
01096               int len
01097        );
01098 
01104   void oncore_consume(
01105               //struct instance *instance
01106       );
01107 
01108 
01114   void oncore_update_state();     
01115       
01119   void oncore_msg_any(
01120                // struct instance *instance,
01121                u_char *buf,
01122               size_t len,
01123               int idx
01124           );
01125 
01129   void auger_msg_any(
01130                // struct instance *instance,
01131                u_char *buf,
01132               size_t len,
01133               int idx
01134           );
01135 
01136 
01137 
01138 public:
01139 
01140   void display(FILE *fout, char *line, int helpLevel);
01141 
01142   //void displayMsg(unsigned char *buf, int len);
01143   
01144 
01148   void oncore_msg_Cb(
01149          //struct instance *instance,
01150          u_char *buf,
01151          size_t len
01152         );
01153 
01154 
01165   void oncore_msg_Cj(
01166                //struct instance *instance,
01167                u_char *buf,
01168                size_t len
01169           );
01170 
01176   void oncore_msg_Cj_id(
01177                //struct instance *instance,
01178                u_char *buf,
01179                size_t len
01180           );
01181 
01182   void oncore_msg_Cj_init(
01183               //struct instance *instance,
01184               u_char *buf,
01185               size_t len
01186           );
01187 
01188 
01193   void oncore_msg_Cf(
01194               //struct instance *instance,
01195               u_char *buf,
01196               size_t len
01197          );
01198 
01199 
01200 
01215   void oncore_msg_CaFaIa(
01216                 //struct instance *instance,
01217                 u_char *buf,
01218                 size_t len
01219          );
01220 
01221 
01223   void oncore_msg_BaEaHa(
01224                 //struct instance *instance,
01225                 u_char *buf,
01226                 size_t len
01227         );
01228 
01229 
01230   void oncore_msg_BnEn(
01231                 //struct instance *instance,
01232                 u_char *buf,
01233                 size_t len
01234         );
01235 
01236 
01237   void oncore_get_timestamp(
01238                //struct instance *instance,
01239                long dt1,  /* tick offset THIS time step */
01240                long dt2  /* tick offset NEXT time step */
01241        );
01242 
01243 
01248   void oncore_msg_At(
01249                //struct instance *instance,
01250                u_char *buf,
01251                size_t len
01252          );
01253 
01254 
01264   void oncore_msg_Bj(
01265                //struct instance *instance,
01266                u_char *buf,
01267                size_t len
01268          );
01269 
01270 
01272    void oncore_msg_Gj(
01273                //struct instance *instance,
01274                u_char *buf,
01275                size_t len
01276          );
01277 
01278 
01282   void oncore_msg_As(
01283                //struct instance *instance,
01284                u_char *buf,
01285                size_t len
01286            );
01287 
01288 
01289    void oncore_print_As(
01290                //struct instance *instance
01291           );
01292 
01293 
01295   void oncore_msg_Aa(
01296                //struct instance *instance,
01297                u_char *buf,
01298                size_t len
01299            );
01300 
01302   void oncore_msg_Ad(
01303                //struct instance *instance,
01304                u_char *buf,
01305                size_t len
01306            );
01307 
01309   void oncore_msg_Ae(
01310                //struct instance *instance,
01311                u_char *buf,
01312                size_t len
01313            );
01314 
01316   void oncore_msg_Af(
01317                //struct instance *instance,
01318                u_char *buf,
01319                size_t len
01320            );
01321            
01322 
01328   void oncore_msg_Ay(
01329                //struct instance *instance,
01330                u_char *buf,
01331                size_t len
01332         );
01333 
01334 
01335 
01339   void oncore_msg_Az(
01340                // struct instance *instance,
01341                u_char *buf,
01342                size_t len
01343        );
01344 
01345 
01346   void oncore_msg_Sz(
01347                //struct instance *instance,
01348                u_char *buf,
01349                size_t len
01350       );
01351 
01356   bool auger_msg_checkFormat(u_char **buf, size_t *len, char cmd, int reorder=1);
01357 
01358 
01361   void auger_msg_A(u_char *buf, size_t);
01362 
01367   void auger_msg_C(u_char *buf, size_t);
01368 
01369 
01379   void auger_msg_F(u_char *buf, size_t);
01380 
01386   void auger_msg_G(u_char *buf, size_t);
01387 
01395   void auger_msg_N(u_char *buf, size_t);
01396 
01399   void auger_msg_gen_read(u_char *buf, size_t);
01400 
01401   void displayBuffer();
01402 
01403 
01404   void oncore_msg_Bo(u_char *buf, size_t);
01405   void oncore_msg_Ab(u_char *buf, size_t);
01406   void oncore_msg_Aw(u_char *buf, size_t);
01407 
01409   void oncore_msg_Ch(u_char *buf, size_t);
01410   
01411 
01415   void auger_set_time(unsigned int sec);
01416 
01420   void auger_set_time();
01421 
01425   void auger_set_com_start(int milisec);
01426 
01430   void auger_set_com_end(int milisec);
01431 
01435   void auger_set_com_window(int tStart, int tEnd);
01436 
01440   void auger_set_control(int status);
01441 
01445   void auger_set_control(int resetCapture, int lidarFreq,
01446                          int genMode1, int genMode2, int genMode3, int genMode4,
01447                          int inMode1, int inMode2, int inMode3, int inMode4,
01448                          int genStat1, int genStat2, int genStat3, int genStat4, 
01449                          int inStat1, int inStat2, int inStat3, int inStat4);
01450 
01451 
01459   void auger_reset_capture_ram();
01460 
01470   void auger_set_lidar_freq(int freq);
01471 
01482   void auger_set_generator_mode(int no, int mode, int stat);
01483 
01484   void auger_set_input_mode(int no, int mode, int stat=-1);
01485 
01488   void auger_set_generator_parameter(int no, unsigned int startSec, int startSubSec,
01489                  int stopSubSec, int period, int period2, int n);
01490 
01493   void auger_set_generator_parameter(int no, unsigned int startSec, int startSubSec,
01494                  int stopSubSec, int period, int period2, int n, int mode, int stat);
01495 
01496 
01498   void changeReceiveState(enum receive_state state);
01499 
01501   void changeSiteSurveyState(enum site_survey_state state);
01502 
01504   void oncore_sendAlmanac(char *file = 0);
01505 
01506     
01507   
01508 //private:
01509 
01510 
01511 
01515   //fd_set activefds;
01516   //int maxactivefd;
01517 
01519   int  rcvptr;
01520   u_char  rcvbuf[500];
01521 
01523   int pps_assert;
01524   int pps_hardpps;
01525 
01526 
01527   int satsTracked; //< Number of tracked satelites
01528 
01529   // --- All variables from struct instance ---
01530   std::string device;
01531   int  unit;    
01532   std::string host; //< Hostname where the master gpsserver is running
01533   struct  refclockproc *pp;
01534   struct  peer *peer;
01535 
01536   int  ttyfd;    
01537   int  ppsfd;    
01538   SimpleSocket *master; 
01539   int useSocket;
01540   int masterfd;  
01543   struct termios oncoreTio;
01544 
01546   struct termios bufferedTio;
01547     
01548       
01549   int  statusfd;  
01550 #ifdef HAVE_PPSAPI
01551   pps_handle_t pps_h;
01552   pps_params_t pps_p;
01553 #endif
01554   enum receive_state o_state;    
01555   enum posn_mode mode;      
01556   enum site_survey_state site_survey;  
01558   int  Bj_day;
01559 
01560   u_long  delay;    
01561   long  offset;   
01563   u_char  *shmem;
01564   char  *shmem_fname;
01565   u_int  shmem_Cb;
01566   u_int  shmem_Ba;
01567   u_int  shmem_Ea;
01568   u_int  shmem_Ha;
01569   u_char  shmem_first;
01570   u_char  shmem_reset;
01571   u_char  shmem_Posn;
01572 
01573   double  ss_lat;   //< site survey variable
01574   double  ss_long;  //< site survey variable
01575   double  ss_ht;    //< site survey variable
01576   double  dH;
01577   int  ss_count;     //< actual number of positions added for site survey
01578   u_char  posn_set;
01579   int ss_mode_active; //< Flag to show automatic survey mode
01580   int ss_reset;       //< Do a reset before starting site survey?!
01581   int ss_disable_hw;  //< Do not use hardware survey - force software survey
01582 
01583   int receivedAlmanac; // Indicate that the almanac has been received
01584 
01585 
01586   enum oncore_model model;
01587   u_int  version;
01588   u_int  revision;
01589 
01590   u_char  chan;    
01591   s_char  traim;    
01592   u_char  traim_delay;  
01594   struct  RSM rsm;  
01595   u_char  printed;
01596   u_char  polled;
01597   int  pollcnt;
01598   u_int  ev_serial;
01599   int  Rcvptr;
01600   u_char  Rcvbuf[500];
01601   u_char  Ea[160];  
01602   u_char  LastEa[160];
01603   u_char  En[70];   
01604   u_char  Cj[300];
01605   u_char  As;
01606   u_char  Ay;
01607   u_char  Az;
01608   u_char  have_dH;
01609   u_char  init_type;
01610   s_char  saw_tooth;
01611   u_int  timeout;  
01612   u_char  count;    
01613   s_char  assert;
01614   u_int  saw_At;
01615   // --- end of struct instance ----
01616 
01617 
01619   enum startupModeType {
01620     LISTEN,
01621     SET,
01622     SURVEY,
01623     RESET
01624   };  
01625 
01627   enum startupModeType startupMode;
01628 
01629   
01634   unsigned int oncoreUtcSecond;
01638   unsigned int oncoreUtcSecTmp;
01642   unsigned int oncoreGpsSecond; // GPS second counter from oncore
01645   int oncoreDiff;
01647   unsigned int oncoreError;
01651   unsigned int augerSecond;
01654   int augerDiff;  
01655   int leapSecs; // Difference between UTC and GPS time [s]
01656   int newLeapSecs; // ???
01657   int timeMode; // 0 GPS/UT1, 1 UTC
01658   int timeZone; // [min] Time zone of the time in the messages
01659 
01660   int setAugerCounter; // Flag enbaled when setting is required
01661   int checkAugerCounter; // Flag to check the auger counter for a while
01662   int nChecks;  // Number of checks
01663   int nFailed;  // Number of failed checks
01664 
01665   int debug;
01666   int debugStored;
01667   
01668   
01669   
01670   int displayChar;
01671 
01674   struct secTiming tBuffer[T_BUFFER_LEN];
01675   int ptrTBuffer;
01676 
01680   struct captureTimeStamp tCapture[2][T_CAPTURE_LEN];
01682   int ptrTCapture[2];
01683 
01688   int useCaptureProxy;
01689 
01691   std::string captureProxyPort;
01692 
01694   std::string captureProxyFormat;
01695   
01698   int captureProxyEdge;  
01699  
01701   int fdCaptureProxy;
01702 
01704   int displayCaptureMonitor;
01705 
01707   int displayCaptureMonitorStored;
01708 
01710   double captureDelay;
01711 
01712   int adjustSecCounter;            //< Adjust the second counter (0 false, 1 once, 2 if necessary)
01713   int adjustActive;               //< Adjustment in progress
01714   int nErrorSecCounter;           //< NUmber of errors in the second counter
01715 
01716   unsigned int augerStatus;      //< Status register of the auger part
01717 
01718   int lidarStartup;               //< Set default lidar frequency at startup of gpsserver (0 no, 1 set)
01719   int lidarSetFreq;               //< Flag to indicate that the frequency should be set once
01720   int lidarDefaultFreq;           //< Lidar default frequency set at startup
01721   int lidarTriggerFreq;           //< Frequency of the LIDAR clock (0, 20, 334, 1000Hz)
01722   static int lidarModeFreq[];     //< Lidar frequencies
01723   
01724   int capturedN;        //< Number of captured time stamps to readout
01725   unsigned char capturedLeft;     //< Left capture timestamps
01726   
01729   bool cmdASend;
01730   
01734   int cmdAReceived;              
01735   
01736   int lastEdge[2];                  //< Last edge of capture inputs
01737 
01738   int genMode[4];                 //< Generator mode (0 veto, 1 external trigger)
01739   int genStat[4];                 //< Generator state (0 stopped, 1 active)
01740   unsigned int genStartSec[4];   //< Generator start second [s]
01741   int genStartSubSec[4];          //< Generator start subsecond [us]
01742   int genEndSubSec[4];            //< Generator start subsecond [us]
01743   int genPeriod[4];               //< Generator periond length [s]
01744   int genPeriod2[4];              //< Generator periond length [min]
01745   int genN[4];                    //< Number of pulses
01746 
01747   int inMode[4];                  //< Input mode (0 veto, 1 external trigger)
01748   int inStat[4];                  //< Input enabled (0 off, 1 on (default))
01749 
01750   bool isInhibit;                 //< Is inhibit of the timing unit active?
01751 
01752 
01753   int augerVersion;                    //< Version of the auger FPGA design (*100)
01754 
01755   int serverVersion;           //< Number of the gpsserver (has to be set by calling program)
01756   std::string serverBuildnote;      //< Build information
01757 
01758 
01759 
01760   // (G)UI parameter
01761   int num; //< Numerical input
01762   int sign; //< Sign of the numerical input
01763 
01764 
01765   int nLogInterval; //< Interval of the loging output
01766   int nLogIntervalStored;
01767   int nShowData; //< Show n lines of log data
01768   bool showHeader; //< Display header for the log messages
01769 
01770   int tPCLastMessage; //< PC time of the last F-messages
01771   unsigned int nSecMessages; //< Number of messages in a second interval
01772 
01773   int pos_id;        //< Id of the location (1 .. 4 eye stations!)
01774   std::string pos_name;   //< Name of the location
01775   int pos_lat;  //< Latitude in GPS Clock format
01776   int pos_long; //< Longitude in GPS Clock format
01777   int pos_ht;   //< Heigth in GPS Clock format
01778   int ss_pos_average; //< Number of samples required for site survey?!
01779 
01780 
01781   int maxStartupRetries; //< Number of retries to open the gpsclock interface
01782 
01783   struct posData posDatabase[POS_DATABASE_LEN]; //< Postition data base
01784   int posDatabaseEntries; //< Number of entries found in the database
01785   int posRadius;     //< Size of the position?!
01786 
01787   int selftest;       //< Flag to perform or omit a selftest
01788 
01789   std::string initial_pos;
01790   
01791   GpsClock gpsclock; //< Gps clock routines
01792 
01793 
01794   int oncore_positionMode; //< Position mode (from At message)
01795 
01796   
01797   
01803   int findCoordinates(char *name, double *latitude, double *longitude, double *height);
01804 
01811   int findPositionName(int *id, std::string *name, double latitude, double longitude, double height);
01812 
01813   int findPositionName(int *id, char *name, double latitude, double longitude, double height);
01814 
01815 
01819   int findPositionId(int *id, char *name);
01820 
01821   int findPositionName(int id, std::string *name);
01822   
01823   std::string inifile; //< Keep the inifile to be able to add the results from the site survey
01824 
01825 
01828   fd_set raw_data_fd_set;
01829   
01830   
01832   std::string line;
01833 
01835   shell *sh;
01836 
01838   cmds *instrSet[1];
01839   
01844   u_char almanac[34][28];
01845   
01847   int nAlmanacRead;
01848   
01850   int nAlmanacWrite;
01851 
01852   // === Internal communications ===
01853 
01855   unsigned int tPC_Ea;
01856   
01857   enum cmd {
01858     OnCj, // receiver id
01859     OnFa, // selftest
01860     OnCf, // reset
01861     OnAt, // position mode
01862   };
01863 
01864   enum test {
01865     TestConnection,
01866     TestReceiver,
01867     TestInitialization,
01868     InitSetPosition,
01869     InitListen,
01870     GPSTEST_NUM  // Must be the last entry to get the number of tests
01871   };  
01872 
01873   enum EGpsLoggingMode {
01874     kLogNone = 0,             // none at all
01875     kLogConnection = 0x01,    // each connect/disconnect
01876     kLogGenerator = 0x02,     // 'gpsset gen ...' commands
01877     kLogInput = 0x04,         // 'gpsset input' and 'gpsset lidar' commands
01878     kLogInhibit = 0x08,       // 'gpsset inhibit' and 'gpsset relinhibit' cmds
01879     kLogSeconds = 0x10,       //
01880   };
01881   
01882   int gpsLoggingMode;
01883   
01884   int cmdPending[GPSCMD_MAX];  
01885 
01886   
01888   unsigned short receiverStatus;
01889 
01890   
01896   int testPhase[GPSTEST_MAX];
01897 
01899   unsigned int testStart[GPSTEST_MAX];
01900   
01901 
01902   // === Test functions ===
01904   void startTest(enum test);
01905 
01907   void testStatus();
01908 
01910   void testHandler();
01911   
01913   void displayTestHeader(FILE *fout, char *name);
01914 
01916   FILE *ftest;
01917     
01924   void testConnection();
01925 
01926   
01933   void testReceiver();
01934 
01935   
01952    void testInitialization();
01953 
01954 
01957    void initSetPosition();
01958 
01960    void initListen();
01961       
01962 
01964    void oncore_setPosition(char *pos_name);        
01965     
01966 };
01967 
01968 #endif