24 #include "../client/pps-client.h"
26 #define MSG_WAIT_TIME 990000
27 #define SECS_PER_HOUR 3600
29 #define MAX_NOT_READY 60
33 extern struct ppsFiles
f;
38 static struct serialLocalVars {
42 bool badTimeConversion;
50 int lastSerialTimeDif;
78 memset(scnbuf, 0, 10);
81 memset(&gmt, 0,
sizeof(
struct tm));
83 char *active, *ctmp2, *ctmp4;
89 float ftmp1, ftmp3, ftmp5, ftmp6;
92 char *pstr = (
char *)msgbuf;
95 pstr = strstr(pstr,
"$GPRMC");
98 char *pNext = strstr(pstr,
"\n");
102 sscanf(pstr,
"$GPRMC,%2d%2d%2d.%d,%1c,%f,%1c,%f,%1c,%f,%f,%2d%2d%2d,", &gmt.tm_hour, &gmt.tm_min, &gmt.tm_sec,
103 &frac, active, &ftmp1, ctmp2, &ftmp3, ctmp4, &ftmp5, &ftmp6, &gmt.tm_mday, &gmt.tm_mon, &gmt.tm_year);
105 if (active[0] ==
'A'){
110 *gmt0Seconds = timegm(&gmt);
112 if (*gmt0Seconds == -1){
113 s.badTimeConversion =
true;
118 s.badGPRMCmsg =
true;
123 s.badGPRMCmsg =
true;
144 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
145 int sfd = open(tcp->
gmtTime_file, O_CREAT | O_WRONLY, mode);
147 sprintf(tcp->
strbuf,
"saveTimestamps() Could not create/open gmtTime file %s\n", tcp->
gmtTime_file);
153 memset(sbuf, 0, 100);
154 sprintf(sbuf,
"%d %d %d\n", gmt0Seconds, gmtSeconds, tv_usec);
156 int rv = write(sfd, sbuf, 100);
160 sprintf(tcp->
strbuf,
"saveTimestamps() write to gmtTime file failed\n");
178 time_t gmt0Seconds = 0;
184 sprintf(tcp->
strbuf,
"saveGPSTime(): No GPRMC message was recieved from the serial port in 60 seconds\n");
189 else if (s.noGPRMCmsg ==
false && s.badGPRMCmsg ==
false && s.badTimeConversion ==
false){
191 if (captureTime < 500){
194 printf(
"read_save() gmt0Seconds: %ld captureTime: %d\n", gmt0Seconds, captureTime);
230 int rfd = open(tcp->
serialPort, O_RDONLY | O_NOCTTY);
238 int captureTime, nRead, timeToWait;
240 for (
int i = 0; i < 1000; i++){
241 nRead = read(rfd, s.msgbuf, 9950);
248 s.noGPRMCmsg =
false;
249 s.bufferIsEmpty =
false;
250 s.badGPRMCmsg =
false;
252 gettimeofday(&tv1, NULL);
254 s.gmtSeconds = (int)tv1.tv_sec;
256 int timeToStart = 1000000 - tv1.tv_usec + 100;
259 memset(s.msgbuf, 0, 10000 *
sizeof(
char));
263 nRead = read(rfd, s.msgbuf, readNum);
266 printf(
"\nsaveGPSTime() Number of serial port lines read: %d\n", nRead);
268 s.ptimestr = strstr(s.msgbuf,
"$GPRMC");
270 if (s.ptimestr != NULL &&
271 strlen(s.ptimestr) < 150){
272 #ifdef DEBUG // not then messages have been delayed and are not reliable.
273 printf(
"\ns.ptimestr: %s\n", s.ptimestr);
274 printf(
"saveGPSTime() s.gmtSeconds: %d usec: %d\n", s.gmtSeconds, (
int)tv1.tv_usec);
276 gettimeofday(&tv1, NULL);
277 captureTime = (int)tv1.tv_usec;
283 s.bufferIsEmpty =
true;
286 gettimeofday(&tv1, NULL);
287 int readEnd = tv1.tv_usec;
288 timeToWait = 1000000 - readEnd - 10000;
317 time_t gmt0Seconds, gmtSeconds;
325 memset(s.timeDiff, 0,
VERIFY_NUM *
sizeof(
int));
326 memset(s.diffCount, 0,
VERIFY_NUM *
sizeof(
int));
329 memset(sbuf, 0, 100);
331 if (stat(
f.gmtTime_file, &statbuf) == 0){
332 sfd = open(
f.gmtTime_file, O_RDONLY);
335 sprintf(
g.logbuf,
"makeSerialTimeQuery(): Serial port GPS time data has resumed\n");
343 s.notReadyCount += 1;
348 sprintf(
g.logbuf,
"makeSerialTimeQuery(): Serial port GPS time data has stopped\n");
355 int sz = read(sfd, sbuf, 100);
358 remove(
f.gmtTime_file);
363 remove(
f.gmtTime_file);
368 remove(
f.gmtTime_file);
372 sscanf(sbuf,
"%ld %ld %d\n", &gmt0Seconds, &gmtSeconds, &captureTime);
374 s.timeDiff[idx] = (int)(gmt0Seconds - gmtSeconds);
376 int maxDiffCount = 0, timeDiff = 0;
382 if ((s.timeDiff[i] != 0) && (s.timeDiff[i] == s.timeDiff[j])){
389 if (s.diffCount[i] > maxDiffCount){
390 maxDiffCount = s.diffCount[i];
391 timeDiff = s.timeDiff[i];
396 sprintf(
g.logbuf,
"makeSerialTimeQuery() Time error: %d seconds. The error will be corrected within 1 minute.\n", timeDiff);
403 printf(
"makeSerialTimeQuery() s.timeDiff[%d]: %d timeDiff %d maxDiffCount: %d\n", idx, s.timeDiff[idx], timeDiff, maxDiffCount);
418 memset(&s, 0,
sizeof(
struct serialLocalVars));
420 int buflen = strlen(
g.serialPort);
421 s.serialPort =
new char[buflen + 1];
422 strcpy(s.serialPort,
g.serialPort);
424 s.threadIsBusy[0] =
false;
435 int rv = pthread_attr_init(&(tcp->
attr));
437 sprintf(
g.logbuf,
"Can't init pthread_attr_t object: %s\n", strerror(errno));
438 writeToLog(
g.logbuf,
"allocInitializeSerialThread()");
444 sprintf(
g.logbuf,
"Can't set pthread_attr_setstacksize(): %s\n", strerror(errno));
445 writeToLog(
g.logbuf,
"allocInitializeSerialThread()");
449 rv = pthread_attr_setdetachstate(&(tcp->
attr), PTHREAD_CREATE_DETACHED);
451 sprintf(
g.logbuf,
"Can't set pthread_attr_t object state: %s\n", strerror(errno));
452 writeToLog(
g.logbuf,
"allocInitializeSerialThread()");
465 pthread_attr_destroy(&(tcp->
attr));