int fflush(FILE *f) { if (!f) { int r = __stdout_used ? fflush(__stdout_used) : 0; for (f=*__ofl_lock(); f; f=f->next) if (f->wpos > f->wbase) r |= fflush(f); __ofl_unlock(); return r; } FLOCK(f); /* If writing, flush output */ if (f->wpos > f->wbase) { f->write(f, 0, 0); if (!f->wpos) { FUNLOCK(f); return EOF; } } /* If reading, sync position, per POSIX */ if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); /* Clear read and write modes */ f->wpos = f->wbase = f->wend = 0; f->rpos = f->rend = 0; FUNLOCK(f); return 0; }
wint_t ungetwc(wint_t c, FILE* f) { unsigned char mbc[MB_LEN_MAX]; int l = 1; locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; FLOCK(f); if (f->mode <= 0) fwide(f, 1); *ploc = f->locale; if (!f->rpos) __toread(f); if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF || (!isascii(c) && (l = wctomb((void*)mbc, c)) < 0)) { FUNLOCK(f); *ploc = loc; return WEOF; } if (isascii(c)) *--f->rpos = c; else memcpy(f->rpos -= l, mbc, l); f->flags &= ~F_EOF; FUNLOCK(f); *ploc = loc; return c; }
size_t fread(void *destv, size_t size, size_t nmemb, FILE *f) { unsigned char *dest = destv; size_t len = size*nmemb, l = len, k; /* Never touch the file if length is zero.. */ if (!l) return 0; FLOCK(f); if (f->rend - f->rpos > 0) { /* First exhaust the buffer. */ k = MIN(f->rend - f->rpos, l); memcpy(dest, f->rpos, k); f->rpos += k; dest += k; l -= k; } /* Read the remainder directly */ for (; l; l-=k, dest+=k) { k = __toread(f) ? 0 : f->read(f, dest, l); if (k+1<=1) { FUNLOCK(f); return (len-l)/size; } } FUNLOCK(f); return nmemb; }
int feof(FILE *f) { FLOCK(f); int ret = !!(f->flags & F_EOF); FUNLOCK(f); return ret; }
wint_t fputwc(wchar_t c, FILE *f) { FLOCK(f); c = __fputwc_unlocked(c, f); FUNLOCK(f); return c; }
int fgetc(FILE *f) { int c; FLOCK(f); c = getc_unlocked(f); FUNLOCK(f); return c; }
off_t __ftello(FILE *f) { off_t pos; FLOCK(f); pos = __ftello_unlocked(f); FUNLOCK(f); return pos; }
int puts(const char *s) { int r; FLOCK(stdout); r = -(fputs(s, stdout) < 0 || putc_unlocked('\n', stdout) < 0); FUNLOCK(stdout); return r; }
int __fseeko(FILE *f, off_t off, int whence) { int result; FLOCK(f); result = __fseeko_unlocked(f, off, whence); FUNLOCK(f); return result; }
size_t fwrite(const void *src, size_t size, size_t nmemb, FILE *f) { size_t k, l = size*nmemb; if (!l) return l; FLOCK(f); k = __fwritex(src, l, f); FUNLOCK(f); return k==l ? nmemb : k/size; }
int fflush(FILE* f) { int r; if (f) { FLOCK(f); r = __fflush_unlocked(f); FUNLOCK(f); return r; } r = fflush(stdout); for (f = *__ofl_lock(); f; f = f->next) { FLOCK(f); if (f->wpos > f->wbase) r |= __fflush_unlocked(f); FUNLOCK(f); } __ofl_unlock(); return r; }
int fflush(FILE *f) { int r; if (f) { FLOCK(f); r = __fflush_unlocked(f); FUNLOCK(f); return r; } r = __stdout_used ? fflush(__stdout_used) : 0; OFLLOCK(); for (f=libc.ofl_head; f; f=f->next) { FLOCK(f); if (f->wpos > f->wbase) r |= __fflush_unlocked(f); FUNLOCK(f); } OFLUNLOCK(); return r; }
int main(int argc,char *argv[]) { int status; int rem; int offset; int len; char buffer[32]; char wgetbuf[1024]; int fd; const char *port = "/dev/ttyS0"; FILE *f; time_t t; t=time(NULL); // default to ttyS0 or invoke with 'linux_nmeap <other serial device>' if (argc == 2) { port = argv[1]; } readconf(argc, argv); /* --------------------------------------- */ /* open the serial port device */ /* using default 9600 baud for most GPS */ /* --------------------------------------- */ fd = openPort(getparam("gps_port"), atoi(getparam("baudrate"))); if (fd < 0) { /* open failed */ printf("openPort %d\n",fd); return fd; } /* ---------------------------------------*/ /*STEP 2 : initialize the nmea context */ /* ---------------------------------------*/ status = nmeap_init(&nmea,(void *)&user_data); if (status != 0) { printf("nmeap_init %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 3 : add standard GPGGA parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 4 : add standard GPRMC parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 5 : process input until done */ /* -------------------------------------- */ for(;;) { /* ---------------------------------------*/ /*STEP 6 : get a buffer of input */ /* -------------------------------------- */ len = rem = read(fd,buffer,sizeof(buffer)); if (len <= 0) { perror("read"); break; } /* ----------------------------------------------*/ /*STEP 7 : process input until buffer is used up */ /* --------------------------------------------- */ offset = 0; while(rem > 0) { /* --------------------------------------- */ /*STEP 8 : pass it to the parser */ /* status indicates whether a complete msg */ /* arrived for this byte */ /* NOTE : in addition to the return status */ /* the message callout will be fired when */ /* a complete message is processed */ /* --------------------------------------- */ status = nmeap_parseBuffer(&nmea,&buffer[offset],&rem); offset += (len - rem); /* ---------------------------------------*/ /*STEP 9 : process the return code */ /* DON"T NEED THIS IF USING CALLOUTS */ /* PICK ONE OR THE OTHER */ /* -------------------------------------- */ switch(status) { case NMEAP_GPGGA: printf("-------------switch\n"); print_gga(&gga); printf("-------------\n"); break; case NMEAP_GPRMC: printf("-------------switch\n"); print_rmc(&rmc); printf("-------------\n"); break; default: break; } if (!(f=fopen(getparam("kml_file"), "w"))) { perror("fopen"); exit(1); }; FLOCK(f); fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\ "<kml xmlns=\"http://earth.google.com/kml/2.2\">\n"\ "<Document>\n"\ "<name>location.kml</name>\n"\ " <Style id=\"location\">\n"\ " <IconStyle>\n"\ " <color>%s</color>\n"\ " <scale>%s</scale>\n"\ " <Icon>\n"\ " <href>%s</href>\n"\ " </Icon>\n"\ " </IconStyle>\n"\ " <LabelStyle>\n"\ " <color>%s</color>\n"\ " <scale>%s</scale>\n"\ " </LabelStyle>\n"\ " <ListStyle>\n"\ " </ListStyle>\n"\ " </Style>\n"\ " <Placemark>\n"\ " <name>%s</name>\n"\ " <LookAt>\n"\ " <longitude>%.6f</longitude>\n"\ " <latitude>%.6f</latitude>\n"\ " <altitude>%.0f</altitude>\n"\ " <range>%s</range>\n"\ " <tilt>%s</tilt>\n"\ " <heading>%s</heading>\n"\ " </LookAt>\n"\ " <styleUrl>#location</styleUrl>\n"\ " <description>Date: %.6lu \n"\ " Time: %.6lu \n"\ " Speed: %.1f</description>\n"\ " <Point>\n"\ " <coordinates>%.6f,%.6f,%.0f</coordinates>\n"\ " </Point>\n"\ " </Placemark>\n"\ "</Document>\n"\ "</kml>\n" , getparam("icon_color"), getparam("icon_scale"), getparam("icon_normal"), getparam("label_color"), getparam("label_scale"), getparam("object_name"), mylon, mylat, myalt, getparam("look_range"), getparam("look_tilt"), getparam("look_heading"), mydat, mytim, myspd, mylon, mylat, myalt); if (myval && mylat != 0 && mylon != 0) { int interval= atoi(getparam("interval")); if (!interval) interval = 30; if ( t+interval < time(NULL) ) { snprintf(wgetbuf, sizeof(wgetbuf), "wget 'http://trackme.org.ua/gps/flygps?ver=1&id=%s&lat=%f&lng=%f&alt=%.0f&speed=%f' -q -O /dev/null && gpio set A1 0 > /dev/null ; usleep 200000 ; gpio set A1 1 > /dev/null", getparam("object_name"), mylat, mylon, myalt, myspd); system(wgetbuf); t = time(NULL); } } fflush(f); FUNLOCK(f); fclose(f); } } /* close the serial port */ close(fd); return 0; }