Changeset 653 in ntrip


Ignore:
Timestamp:
Jan 4, 2008, 4:23:06 PM (15 years ago)
Author:
stuerze
Message:

lines for windows compatibility added

Location:
trunk/ntripserver
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ntripserver/README

    r548 r653  
    8484- make debug (for debugging purposes).
    8585
    86 The exacutable will show up as ntripserver.
    87 
     86To compile the source code on a Windows system where a mingw gcc
     87compiler is available, you may like to run the following command:
     88
     89- gcc -Wall -W -O3 -DWINDOWSVERSION ntripserver.c -DNDEBUG
     90  -o ntripserver -lwsock32, or
     91- mingw32-make, or
     92- mingw32-make debug
     93
     94The exacutable will show up as ntripserver on Linux
     95or ntripserver.exe on a Windows system.
    8896
    8997Usage
  • trunk/ntripserver/makefile

    r489 r653  
    11#!/usr/bin/make
    2 # $Id: makefile,v 1.6 2007/08/30 07:45:59 stoecker Exp $
     2# $Id: makefile,v 1.7 2007/08/30 14:57:34 stuerze Exp $
     3
     4ifdef windir
     5CC   = gcc
     6OPTS = -Wall -W -O3 -DWINDOWSVERSION
     7LIBS = -lwsock32
     8else
     9OPTS = -Wall -W -O3
     10endif
    311
    412ntripserver: ntripserver.c
    5         $(CC) -Wall -W -O3 $? -DNDEBUG -o $@
     13        $(CC) $(OPTS) $? -DNDEBUG -o $@ $(LIBS)
    614
    715debug: ntripserver.c
    8         $(CC) -Wall -W -O3 $? -o ntripserver
     16        $(CC) $(OPTS) $? -o ntripserver $(LIBS)
    917
    1018clean:
  • trunk/ntripserver/ntripserver.c

    r598 r653  
    11/*
    2  * $Id: ntripserver.c,v 1.35 2007/08/30 16:40:51 stoecker Exp $
     2 * $Id: ntripserver.c,v 1.36 2007/12/14 07:23:44 stoecker Exp $
    33 *
    44 * Copyright (c) 2003...2007
     
    3737
    3838/* CVS revision and version */
    39 static char revisionstr[] = "$Revision: 1.35 $";
    40 static char datestr[]     = "$Date: 2007/08/30 16:40:51 $";
     39static char revisionstr[] = "$Revision: 1.36 $";
     40static char datestr[]     = "$Date: 2007/12/14 07:23:44 $";
    4141
    4242#include <ctype.h>
     
    4444#include <fcntl.h>
    4545#include <getopt.h>
    46 #include <netdb.h>
    47 #include <signal.h>
    4846#include <stdio.h>
    4947#include <stdlib.h>
    5048#include <string.h>
     49#include <sys/time.h>
     50#include <sys/types.h>
     51#include <time.h>
     52#include <signal.h>
    5153#include <unistd.h>
    52 #include <arpa/inet.h>
    53 #include <netinet/in.h>
    54 #include <sys/socket.h>
    55 #include <sys/termios.h>
    56 #include <sys/types.h>
    57 #include <sys/time.h>
     54
     55#ifdef WINDOWSVERSION
     56  #include <winsock2.h>
     57  #include <io.h>
     58  #include <sys/stat.h>
     59  #include <windows.h>
     60  typedef SOCKET sockettype;
     61  typedef u_long in_addr_t;
     62  typedef size_t socklen_t;
     63  typedef u_short uint16_t;
     64#else
     65  typedef int sockettype;
     66  #include <arpa/inet.h>
     67  #include <sys/socket.h>
     68  #include <netinet/in.h>
     69  #include <netdb.h>
     70  #include <sys/termios.h>
     71  #define closesocket(sock) close(sock)
     72  #define INVALID_HANDLE_VALUE -1
     73  #define INVALID_SOCKET -1
     74#endif
    5875
    5976#ifndef COMPILEDATE
    6077#define COMPILEDATE " built " __DATE__
    6178#endif
     79
     80#define ALARMTIME (2*60)
    6281
    6382#ifndef MSG_DONTWAIT
     
    85104#define NTRIP_PORT      2101
    86105
    87 /* default sisnet source */
    88106#define SISNET_SERVER   "131.176.49.142"
    89107#define SISNET_PORT     7777
    90 
    91 #define ALARMTIME       60
    92108
    93109#define RTP_VERSION     2
     
    99115static enum MODE inputmode     = INFILE;
    100116static int sisnet              = 31;
    101 static int gpsfd               = -1;
    102 static int socket_tcp          = -1;
    103 static int socket_udp          = -1;
     117static int gps_file            = -1;
     118static sockettype gps_socket   = INVALID_SOCKET;
     119static sockettype socket_tcp   = INVALID_SOCKET;
     120static sockettype socket_udp   = INVALID_SOCKET;
     121#ifndef WINDOWSVERSION
     122static int gps_serial          = INVALID_HANDLE_VALUE;
     123static int sigpipe_received    = 0;
     124#else
     125HANDLE gps_serial              = INVALID_HANDLE_VALUE;
     126#endif
     127static int sigalarm_received   = 0;
    104128static int sigint_received     = 0;
    105 static int sigalarm_received   = 0;
    106 static int sigpipe_received    = 0;
    107129static int reconnect_sec       = 1;
    108130
    109131
    110132/* Forward references */
    111 static int  openserial(const char * tty, int blocksz, int baud);
    112 static void send_receive_loop(int sock, int fd, int outmode,
     133static void send_receive_loop(sockettype sock, int outmode,
    113134  struct sockaddr * pcasterRTP, socklen_t length);
    114135static void usage(int, char *);
    115136static int  encode(char *buf, int size, const char *user, const char *pwd);
    116 static int  send_to_caster(char *input, int socket, int input_size);
     137static int  send_to_caster(char *input, sockettype socket, int input_size);
    117138static void close_session(const char *caster_addr, const char *mountpoint,
    118139  int cseq, int session, char *rtsp_ext, int fallback);
    119140static int  reconnect(int rec_sec, int rec_sec_max);
    120 
    121 /* Signal Handling */
    122141static void handle_sigint(int sig);
     142static void setup_signal_handler(int sig, void (*handler)(int));
     143#ifndef WINDOWSVERSION
     144static int  openserial(const char * tty, int blocksz, int baud);
     145static void handle_sigpipe(int sig);
    123146static void handle_alarm(int sig);
    124 static void handle_sigpipe(int sig);
    125 static void setup_signal_handler(int sig, void (*handler)(int));
     147#else
     148static HANDLE openserial(const char * tty, int baud);
     149#endif
     150
    126151
    127152/*
     
    230255  }
    231256
     257  /* setup signal handler for CTRL+C */
     258  setup_signal_handler(SIGINT, handle_sigint);
     259#ifndef WINDOWSVERSION
     260  /* setup signal handler for boken pipe */
     261  setup_signal_handler(SIGPIPE, handle_sigpipe);
    232262  /* setup signal handler for timeout */
    233263  setup_signal_handler(SIGALRM, handle_alarm);
    234264  alarm(ALARMTIME);
    235 
    236   /* setup signal handler for CTRL+C */
    237   setup_signal_handler(SIGINT, handle_sigint);
     265#else
     266  /* winsock initialization */
     267  WSADATA wsaData;
     268  if (WSAStartup(MAKEWORD(1,1), &wsaData))
     269  {
     270    fprintf(stderr, "Could not init network access.\n");
     271    return 20;
     272  }
     273#endif
    238274 
    239 /* setup signal handler for boken pipe */
    240   setup_signal_handler(SIGPIPE, handle_sigpipe);
    241 
    242275  /* get and check program arguments */
    243276  if(argc <= 1)
     
    497530  {
    498531    int input_init = 1;
     532    if(sigint_received) break;
    499533    /*** InputMode handling ***/
    500534    switch(inputmode)
     
    502536    case INFILE:
    503537      {
    504         if((gpsfd = open(filepath, O_RDONLY)) < 0)
     538        if((gps_file = open(filepath, O_RDONLY)) < 0)
    505539        {
    506540          perror("ERROR: opening input file");
    507541          exit(1);
    508542        }
     543#ifndef WINDOWSVERSION
    509544        /* set blocking inputmode in case it was not set
    510545          (seems to be sometimes for fifo's) */
    511         fcntl(gpsfd, F_SETFL, 0);
     546        fcntl(gps_file, F_SETFL, 0);
     547#endif
    512548        printf("file input: file = %s\n", filepath);
    513549      }
     
    515551    case SERIAL: /* open serial port */
    516552      {
    517         gpsfd = openserial(ttyport, 1, ttybaud);
    518         if(gpsfd < 0)
    519         {
    520           exit(1);
    521         }
     553#ifndef WINDOWSVERSION
     554        gps_serial = openserial(ttyport, 1, ttybaud);
     555#else
     556        gps_serial = openserial(ttyport, ttybaud);
     557#endif
     558        if(gps_serial == INVALID_HANDLE_VALUE) exit(1);
    522559        printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
    523560      }
     
    547584        }
    548585
    549         if((gpsfd = socket(AF_INET, inputmode == UDPSOCKET
    550         ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
     586        if((gps_socket = socket(AF_INET, inputmode == UDPSOCKET
     587        ? SOCK_DGRAM : SOCK_STREAM, 0)) == INVALID_SOCKET)
    551588        {
    552589          fprintf(stderr,
     
    571608        if(bindmode)
    572609        {
    573           if(bind(gpsfd, (struct sockaddr *) &caster, sizeof(caster)) < 0)
     610          if(bind(gps_socket, (struct sockaddr *) &caster, sizeof(caster)) < 0)
    574611          {
    575612            fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
     
    579616          }
    580617        } /* connect to input-caster or proxy server*/
    581         else if(connect(gpsfd, (struct sockaddr *)&caster, sizeof(caster)) < 0)
     618        else if(connect(gps_socket, (struct sockaddr *)&caster, sizeof(caster)) < 0)
    582619        {
    583620          fprintf(stderr, "WARNING: can't connect input to %s at port %d\n",
     
    592629
    593630          /* set socket buffer size */
    594           setsockopt(gpsfd, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
     631          setsockopt(gps_socket, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
    595632            sizeof(const char *));
    596633          if(stream_user && stream_password)
     
    634671            "\r\n", get_extension, stream_name, AGENTSTRING, revisionstr);
    635672          }
    636           if((send(gpsfd, szSendBuffer, (size_t)nBufferBytes, 0))
     673          if((send(gps_socket, szSendBuffer, (size_t)nBufferBytes, 0))
    637674          != nBufferBytes)
    638675          {
     
    644681          /* check Source caster's response */
    645682          while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
    646           && (nBufferBytes += recv(gpsfd, szSendBuffer,
     683          && (nBufferBytes += recv(gps_socket, szSendBuffer,
    647684          sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
    648685          {
     
    683720            while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
    684721            {
    685               if((send(gpsfd, buffer, (size_t)i, 0)) != i)
     722              if((send(gps_socket, buffer, (size_t)i, 0)) != i)
    686723              {
    687724                perror("WARNING: sending init file");
     
    715752        i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
    716753          : "AUTH,%s,%s", sisnetuser, sisnetpassword);
    717         if((send(gpsfd, buffer, (size_t)i, 0)) != i)
     754        if((send(gps_socket, buffer, (size_t)i, 0)) != i)
    718755        {
    719756          perror("WARNING: sending authentication for SISNeT data server");
     
    722759        }
    723760        i = sisnet >= 30 ? 7 : 5;
    724         if((j = recv(gpsfd, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
     761        if((j = recv(gps_socket, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
    725762        {
    726763          fprintf(stderr, "WARNING: SISNeT connect failed:");
     
    738775        if(sisnet >= 31)
    739776        {
    740           if((send(gpsfd, "START\r\n", 7, 0)) != i)
     777          if((send(gps_socket, "START\r\n", 7, 0)) != i)
    741778          {
    742779            perror("WARNING: sending Sisnet start command");
     
    760797        {
    761798          fprintf(stderr, "Sending user ID for receiver...\n");
    762           nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
     799          nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
    763800          strcpy(szSendBuffer, recvrid);
    764801          strcat(szSendBuffer,"\r\n");
    765           if(send(gpsfd,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
     802          if(send(gps_socket,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
    766803          {
    767804            perror("WARNING: sending user ID for receiver");
     
    781818        {
    782819          fprintf(stderr, "Sending user password for receiver...\n");
    783           nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
     820          nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
    784821          strcpy(szSendBuffer, recvrpwd);
    785822          strcat(szSendBuffer,"\r\n");
    786           if(send(gpsfd, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
     823          if(send(gps_socket, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
    787824          {
    788825            perror("WARNING: sending user password for receiver");
     
    803840    while((input_init) && (output_init))
    804841    {
    805       if((sigint_received) || (sigalarm_received) || (sigpipe_received)) break;
    806 
     842#ifndef WINDOWSVERSION
     843      if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
     844#else
     845      if((sigalarm_received) || (sigint_received)) break;
     846#endif
    807847      if(!(he = gethostbyname(outhost)))
    808848      {
     
    814854
    815855      /* create socket */
    816       if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) < 0)
     856      if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    817857      {
    818858        perror("ERROR: tcp socket");
     
    884924          }
    885925#endif
    886           send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
     926          send_receive_loop(socket_tcp, outputmode, NULL, 0);
     927          input_init = output_init = 0;
    887928          break;
    888929        case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
     
    952993          }
    953994#endif
    954           send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
     995          send_receive_loop(socket_tcp, outputmode, NULL, 0);
     996          input_init = output_init = 0;
    955997          break;
    956998        case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
    957           if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) < 0)
     999          if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)
    9581000          {
    9591001            perror("ERROR: udp socket");
     
    10871129              || (nBufferBytes < 0))
    10881130              {
    1089                 fprintf(stderr, "ERROR: Destination caster request to long\n");
     1131                    fprintf(stderr, "ERROR: Destination caster request to long\n");
    10901132                reconnect_sec_max = 0;
    10911133                output_init = 0;
     
    10941136              if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
    10951137              {
    1096                  output_init = 0;
    1097                  break;
     1138                output_init = 0;
     1139                    break;
    10981140              }
    10991141            }
     
    11191161              cseq = 2;
    11201162              len = (socklen_t)sizeof(casterRTP);
    1121               send_receive_loop(socket_udp, gpsfd, outputmode, (struct sockaddr *)&casterRTP,
     1163              send_receive_loop(socket_udp, outputmode, (struct sockaddr *)&casterRTP,
    11221164              (socklen_t)len);
    11231165              break;
     
    11251167            else{break;}
    11261168          }
     1169          input_init = output_init = 0;
    11271170          break;
    11281171      }     
    11291172    }
    11301173    close_session(casterouthost, mountpoint, cseq, session, rtsp_extension, 0);
    1131     if((!sigint_received) && (reconnect_sec_max))
    1132     {
    1133        reconnect_sec = reconnect(reconnect_sec, reconnect_sec_max);
    1134     }
     1174    if((reconnect_sec_max)  && (!sigint_received))
     1175      reconnect_sec = reconnect(reconnect_sec, reconnect_sec_max);
    11351176    else inputmode = LAST;
    11361177  }
     
    11381179}
    11391180
    1140 static void send_receive_loop(int sock, int fd, int outmode, struct sockaddr* pcasterRTP,
     1181static void send_receive_loop(sockettype sock, int outmode, struct sockaddr* pcasterRTP,
    11411182socklen_t length)
    11421183{
    1143   int nodata = 0;
    1144   char buffer[BUFSZ] = { 0 };
    1145   char sisnetbackbuffer[200];
    1146   char szSendBuffer[BUFSZ] = "";
    1147   int nBufferBytes = 0;
     1184  int      nodata = 0;
     1185  char     buffer[BUFSZ] = { 0 };
     1186  char     sisnetbackbuffer[200];
     1187  char     szSendBuffer[BUFSZ] = "";
     1188  int      nBufferBytes = 0;
    11481189
    11491190   /* RTSP / RTP Mode */
    1150   int    isfirstpacket = 1;
    1151   struct timeval now;
    1152   struct timeval last = {0,0};
     1191  int      isfirstpacket = 1;
     1192  struct   timeval now;
     1193  struct   timeval last = {0,0};
    11531194  long int sendtimediff;
    1154   int rtpseq = 0;
    1155   int rtpssrc = 0;
    1156   int rtptime = 0;
     1195  int      rtpseq = 0;
     1196  int      rtpssrc = 0;
     1197  int      rtptime = 0;
    11571198
    11581199  /* data transmission */
    11591200  fprintf(stderr,"transfering data ...\n");
    1160   int send_recv_success = 0;
     1201  int  send_recv_success = 0;
     1202#ifdef WINDOWSVERSION
     1203  time_t nodata_begin = 0, nodata_current = 0; 
     1204#endif
    11611205  while(1)
    11621206  {
    11631207    if(send_recv_success < 3) send_recv_success++; 
    1164     if(!nodata) alarm(ALARMTIME);
    1165     else nodata = 0;
    1166 
     1208    if(!nodata)
     1209    {
     1210#ifndef WINDOWSVERSION
     1211      alarm(ALARMTIME);
     1212#else
     1213      time(&nodata_begin);
     1214#endif
     1215    }
     1216    else
     1217    {
     1218      nodata = 0;
     1219#ifdef WINDOWSVERSION
     1220      time(&nodata_current);
     1221      if(difftime(nodata_current, nodata_begin) >= ALARMTIME)
     1222      {
     1223        sigalarm_received = 1;
     1224        fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
     1225      }
     1226#endif
     1227    }
    11671228    /* signal handling*/
    1168     if((sigint_received) || (sigpipe_received) || (sigalarm_received)) break;
    1169    
     1229#ifdef WINDOWSVERSION
     1230    if((sigalarm_received) || (sigint_received)) break;
     1231#else
     1232    if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
     1233#endif
    11701234    if(!nBufferBytes)
    11711235    {
     
    11791243        memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
    11801244        i = (sisnet >= 30 ? 5 : 3);
    1181         if((send(gpsfd, "MSG\r\n", i, 0)) != i)
     1245        if((send(gps_socket, "MSG\r\n", i, 0)) != i)
    11821246        {
    11831247          perror("WARNING: sending SISNeT data request failed");
     
    11861250      }
    11871251      /*** receiving data ****/
    1188       nBufferBytes = read(fd, buffer, sizeof(buffer));
     1252      if(inputmode == INFILE)
     1253        nBufferBytes = read(gps_file, buffer, sizeof(buffer));
     1254      else if(inputmode == SERIAL)
     1255      {
     1256#ifndef WINDOWSVERSION       
     1257        nBufferBytes = read(gps_serial, buffer, sizeof(buffer));
     1258#else
     1259        DWORD nRead = 0; 
     1260        if(!ReadFile(gps_serial, buffer, sizeof(buffer), &nRead, NULL))
     1261        {
     1262          fprintf(stderr,"ERROR: reading serial input failed\n");
     1263          return;
     1264        }
     1265        nBufferBytes = (int)nRead;
     1266#endif
     1267      }
     1268      else
     1269        nBufferBytes = recv(gps_socket, buffer, sizeof(buffer), 0);
    11891270      if(!nBufferBytes)
    11901271      {
    1191         printf("WARNING: no data received from input\n");
     1272        fprintf(stderr, "WARNING: no data received from input\n");
     1273        nodata = 1;
     1274#ifndef WINDOWSVERSION
    11921275        sleep(3);
    1193         nodata = 1;
     1276#else
     1277        Sleep(3*1000);
     1278#endif
    11941279        continue;
    11951280      }
     
    13401425 *     tty     : pointer to    : A zero-terminated string containing the device
    13411426 *               unsigned char   name of the appropriate serial port.
    1342  *     blocksz : integer       : Block size for port I/O
     1427 *     blocksz : integer       : Block size for port I/O  (ifndef WINDOWSVERSION)
    13431428 *     baud :    integer       : Baud rate for port I/O
    13441429 *
    13451430 * Return Value:
    13461431 *     The function returns a file descriptor for the opened port if successful.
    1347  *     The function returns -1 in the event of an error.
     1432 *     The function returns -1 / INVALID_HANDLE_VALUE in the event of an error.
    13481433 *
    13491434 * Remarks:
    13501435 *
    13511436 ********************************************************************/
    1352 
     1437#ifndef WINDOWSVERSION
    13531438static int openserial(const char * tty, int blocksz, int baud)
    13541439{
    1355   int fd;
    13561440  struct termios termios;
    13571441
    1358   fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
    1359   if(fd < 0)
     1442/*** opening the serial port ***/
     1443  gps_serial = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
     1444  if(gps_serial < 0)
    13601445  {
    13611446    perror("ERROR: opening serial connection");
    13621447    return (-1);
    13631448  }
    1364   if(tcgetattr(fd, &termios) < 0)
     1449
     1450/*** configuring the serial port ***/
     1451  if(tcgetattr(gps_serial, &termios) < 0)
    13651452  {
    13661453    perror("ERROR: get serial attributes");
     
    13811468#if (B4800 != 4800)
    13821469/* Not every system has speed settings equal to absolute speed value. */
    1383 
    13841470  switch (baud)
    13851471  {
     
    14371523    return (-1);
    14381524  }
    1439   if(tcsetattr(fd, TCSANOW, &termios) < 0)
     1525  if(tcsetattr(gps_serial, TCSANOW, &termios) < 0)
    14401526  {
    14411527    perror("ERROR: setting serial attributes");
    14421528    return (-1);
    14431529  }
    1444   if(fcntl(fd, F_SETFL, 0) == -1)
     1530  if(fcntl(gps_serial, F_SETFL, 0) == -1)
    14451531  {
    14461532    perror("WARNING: setting blocking inputmode failed");
    14471533  }
    1448   return (fd);
     1534  return (gps_serial);
     1535}
     1536#else
     1537static HANDLE openserial(const char * tty, int baud)
     1538{
     1539  DCB dcb; 
     1540  COMMTIMEOUTS cmt;
     1541
     1542/*** opening the serial port ***/
     1543  gps_serial = CreateFile(tty, GENERIC_READ | GENERIC_WRITE, 0, 0,
     1544    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
     1545  if(gps_serial == INVALID_HANDLE_VALUE)
     1546  {
     1547    fprintf(stderr, "ERROR: opening serial connection\n");
     1548    return (INVALID_HANDLE_VALUE);
     1549  }
     1550/*** configuring the serial port ***/
     1551  FillMemory(&dcb, sizeof(dcb), 0);
     1552  dcb.DCBlength = sizeof(dcb);
     1553  if(!GetCommState(gps_serial, &dcb))
     1554  {
     1555    fprintf(stderr, "ERROR: get serial attributes\n");
     1556    return (INVALID_HANDLE_VALUE);
     1557  }
     1558  switch (baud)
     1559  {
     1560  case 110:
     1561    baud = CBR_110;
     1562    break;
     1563  case 300:
     1564    baud = CBR_300;
     1565    break;
     1566  case 600:
     1567    baud = CBR_600;
     1568    break;
     1569  case 1200:
     1570    baud = CBR_1200;
     1571    break;
     1572  case 2400:
     1573    baud = CBR_2400;
     1574    break;
     1575  case 4800:
     1576    baud = CBR_4800;
     1577    break;
     1578  case 9600:
     1579    baud = CBR_9600;
     1580    break;
     1581  case 14400:
     1582    baud = CBR_14400;
     1583    break;
     1584  case 19200:
     1585    baud = CBR_19200;
     1586    break;
     1587  case 38400:
     1588    baud = CBR_38400;
     1589    break;
     1590  case 56000:
     1591    baud = CBR_56000;
     1592    break;
     1593  case 57600:
     1594    baud = CBR_57600;
     1595    break;
     1596  case 115200:
     1597    baud = CBR_115200;
     1598    break;
     1599  case 128000:
     1600    baud = CBR_128000;
     1601    break;
     1602  case 256000:
     1603    baud = CBR_256000;
     1604    break;
     1605  default:
     1606    fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
     1607    baud = CBR_19200;
     1608    break;
     1609  }
     1610  dcb.BaudRate = baud;
     1611  dcb.ByteSize = 8;
     1612  dcb.StopBits = ONESTOPBIT;
     1613  dcb.Parity   = NOPARITY;
     1614  if(!GetCommState(gps_serial, &dcb))
     1615  {
     1616    fprintf(stderr, "ERROR: get serial attributes\n");
     1617    return (INVALID_HANDLE_VALUE);
     1618  }
     1619  FillMemory(&cmt, sizeof(cmt), 0);
     1620  cmt.ReadIntervalTimeout = 1000;
     1621  cmt.ReadTotalTimeoutMultiplier = 1;
     1622  cmt.ReadTotalTimeoutConstant = 0;
     1623  if(!SetCommTimeouts(gps_serial, &cmt))
     1624  {
     1625    fprintf(stderr, "ERROR: set serial timeouts\n");
     1626    return (INVALID_HANDLE_VALUE);
     1627  }
     1628  return (gps_serial);
    14491629} /* openserial */
     1630#endif
    14501631
    14511632/********************************************************************
     
    14631644*
    14641645*********************************************************************/
    1465 static
    14661646#ifdef __GNUC__
    14671647__attribute__ ((noreturn))
     
    15701750}
    15711751
     1752#ifndef WINDOWSVERSION
    15721753#ifdef __GNUC__
    15731754static void handle_alarm(int sig __attribute__((__unused__)))
     
    15851766static void handle_sigpipe(int sig)
    15861767#endif /* __GNUC__ */
    1587 {sigpipe_received = 1;}
     1768{
     1769  sigpipe_received = 1;
     1770}
     1771#endif /* WINDOWSVERSION */
    15881772
    15891773static void setup_signal_handler(int sig, void (*handler)(int))
     
    16021786  return;
    16031787} /* setupsignal_handler */
     1788
    16041789
    16051790/********************************************************************
     
    16601845 * send message to caster                                           *
    16611846*********************************************************************/
    1662 static int send_to_caster(char *input, int socket, int input_size)
     1847static int send_to_caster(char *input, sockettype socket, int input_size)
    16631848{
    16641849 int send_error = 1;
     
    16881873  rec_sec *= 2;
    16891874  if (rec_sec > rec_sec_max) rec_sec = rec_sec_max;
     1875#ifndef WINDOWSVERSION
    16901876  sleep(rec_sec);
     1877  sigpipe_received = 0;
     1878#else
     1879  Sleep(rec_sec*1000);
     1880#endif
    16911881  sigalarm_received = 0;
    1692   sigpipe_received = 0;
    16931882  return rec_sec;
    16941883} /* reconnect */
     
    17041893  char send_buf[BUFSZ];
    17051894
    1706   if((gpsfd != -1) && (!fallback))
    1707   {
    1708     if(close(gpsfd) == -1)
    1709     {
    1710       perror("ERROR: close input device ");
    1711       exit(0);
    1712     }
    1713     else
    1714     {
    1715       gpsfd = -1;
     1895  if(!fallback)
     1896  {
     1897    if((gps_socket != INVALID_SOCKET) &&
     1898       ((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET) ||
     1899       (inputmode == CASTER)    || (inputmode == SISNET)))
     1900    {
     1901      if(closesocket(gps_socket) == -1)
     1902      {
     1903        perror("ERROR: close input device ");
     1904        exit(0);
     1905      }
     1906      else
     1907      {
     1908        gps_socket = -1;
    17161909#ifndef NDEBUG 
    1717     fprintf(stderr, "close input device: successful\n");
    1718 #endif
    1719     }
    1720   }
    1721 
    1722   if(socket_udp  != -1)
     1910        fprintf(stderr, "close input device: successful\n");
     1911#endif
     1912      }
     1913    }
     1914    else if((gps_serial != INVALID_HANDLE_VALUE) && (inputmode == SERIAL))
     1915    {
     1916#ifndef WINDOWSVERSION
     1917      if(close(gps_serial) == INVALID_HANDLE_VALUE)
     1918      {
     1919        perror("ERROR: close input device ");
     1920        exit(0);
     1921      }
     1922#else
     1923      if(!CloseHandle(gps_serial))
     1924      {
     1925        fprintf(stderr, "ERROR: close input device ");
     1926        exit(0);
     1927      }
     1928#endif
     1929      else
     1930      {
     1931        gps_serial = INVALID_HANDLE_VALUE;
     1932#ifndef NDEBUG 
     1933        fprintf(stderr, "close input device: successful\n");
     1934#endif
     1935      }
     1936    }
     1937    else if((gps_file != -1) && (inputmode == INFILE))
     1938    {
     1939      if(close(gps_file) == -1)
     1940      {
     1941        perror("ERROR: close input device ");
     1942        exit(0);
     1943      }
     1944      else
     1945      {
     1946        gps_file = -1;
     1947#ifndef NDEBUG 
     1948        fprintf(stderr, "close input device: successful\n");
     1949#endif
     1950      }
     1951    }
     1952  }
     1953
     1954  if(socket_udp  != INVALID_SOCKET)
    17231955  {
    17241956    if(cseq == 2)
     
    17421974#endif
    17431975    }
    1744     if(close(socket_udp)==-1)
     1976    if(closesocket(socket_udp)==-1)
    17451977    {
    17461978      perror("ERROR: close udp socket");
     
    17561988  }
    17571989
    1758   if(socket_tcp != -1)
    1759   {
    1760     if(close(socket_tcp) == -1)
     1990  if(socket_tcp != INVALID_SOCKET)
     1991  {
     1992    if(closesocket(socket_tcp) == -1)
    17611993    {
    17621994      perror("ERROR: close tcp socket");
Note: See TracChangeset for help on using the changeset viewer.