Changeset 18 in ntrip for trunk/ntripserver/NtripLinuxServer.c


Ignore:
Timestamp:
Apr 19, 2005, 1:28:17 PM (19 years ago)
Author:
stoecker
Message:

larger rework

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ntripserver/NtripLinuxServer.c

    r16 r18  
    11/*
    2  * main.c
     2 * NtripServerLinux.c
    33 *
    44 * Copyright (c) 2003...2005
     
    4141 */
    4242
    43 /* $Id: NtripLinuxServer.c,v 1.7 2005/02/17 09:22:23 stoecker Exp $
    44  * Changes -  Version 0.7
    45  * Thu Sep 22 08:10:45  2003    actina AG <http://www.actina.de>
    46  *
    47  *         Steffen Tschirpke <St.Tschirpke@actina.de>
    48  *         * main.c
     43/* $Id: NtripLinuxServer.c,v 1.8 2005/03/21 13:02:47 stoecker Exp $
     44 * Changes - Version 0.7
     45 * Sep 22 2003  Steffen Tschirpke <St.Tschirpke@actina.de>
    4946 *           - socket support
    5047 *           - command line option handling
    5148 *           - error handling
    5249 *           - help screen
     50 *
     51 * Changes - Version 0.9
     52 * Feb 15 2005  Dirk Stoecker <soft@dstoecker.de>
     53 *           - some minor updates, fixed serial baudrate settings
     54 *
     55 * Changes - Version 0.10
     56 * Apr 05 2005  Dirk Stoecker <soft@dstoecker.de>
     57 *           - some cleanup and miscellaneous fixes
     58 *           - replaced non-working simulate with file input (stdin)
     59 *           - TCP sending now somewhat more stable
     60 *           - cleanup of error handling
    5361 */
    5462
    55 /* Changes - Version 0.9
    56  * Feb 15 2005  Dirk Stoecker <soft@dstoecker.de>
    57  */
    58  
     63#include <ctype.h>
     64#include <errno.h>
     65#include <fcntl.h>
     66#include <getopt.h>
     67#include <netdb.h>
    5968#include <stdio.h>
     69#include <stdlib.h>
    6070#include <string.h>
    6171#include <unistd.h>
    62 #include <stdlib.h>
     72#include <arpa/inet.h>
     73#include <netinet/in.h>
     74#include <sys/socket.h>
     75#include <sys/termios.h>
    6376#include <sys/types.h>
    64 #include <sys/socket.h>
    65 #include <netinet/in.h>
    66 #include <arpa/inet.h>
    67 #include <netdb.h>
    68 #include <getopt.h>
    69 #include <sys/termios.h>
    70 #include <fcntl.h>
    71 #include "NtripServerLinux.h"
    72 
    73 #define VERSION "NTRIP NtripServerLinux/0.9"
    74 
    75 #define SIMULATE  0
    76 #define SERIAL    1
    77 #define TCPSOCKET 2
     77
     78#ifndef MSG_DONTWAIT
     79#define MSG_DONTWAIT 0 /* prevent compiler errors */
     80#endif
     81#ifndef O_EXLOCK
     82#define O_EXLOCK 0 /* prevent compiler errors */
     83#endif
     84
     85enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3 };
     86
     87#define VERSION         "NTRIP NtripServerLinux/0.10"
     88#define BUFSZ           1024
    7889
    7990/* default socket source */
    80 #define SERV_HOST_ADDR "127.0.0.1"
    81 #define SERV_TCP_PORT 1025
     91#define SERV_HOST_ADDR  "127.0.0.1"
     92#define SERV_TCP_PORT   1025
    8293
    8394/* default destination */
    84 #define NTRIP_CASTER "www.euref-ip.net"
    85 #define NTRIP_PORT 80
    86 
    87 int verbose = 0;
    88 int simulate = 0;
    89 int tickinterval = 1;
    90 int ttybaud = 19200;
    91 char * ttyport = "/dev/gps";
    92 int mode = 0;
     95#define NTRIP_CASTER    "www.euref-ip.net"
     96#define NTRIP_PORT      80
     97
     98int ttybaud             = 19200;
     99char *ttyport           = "/dev/gps";
     100char *filepath          = "/dev/stdin";
     101enum MODE mode          = INFILE;
    93102
    94103/* Forward references */
    95 int openserial (u_char * tty, int blocksz, int ttybaud);
    96 int send_receive_loop(int socket, int fd);
    97 void usage (int);
     104int openserial(u_char * tty, int blocksz, int ttybaud);
     105void send_receive_loop(int socket, int fd);
     106void usage(int);
    98107
    99108/*
     
    115124*/
    116125
    117 int main (int argc, char ** argv)
     126int main(int argc, char **argv)
    118127{
    119   u_char       * ttyin = ttyport;
    120   char         * logfilepath = NULL;
    121   FILE         * fp = NULL;
    122   int            c, gpsfd = -1;
    123   int            size = 2048;           //for setting send buffer size
    124  
     128  u_char *ttyin = ttyport;
     129  int c, gpsfd = -1;
     130  int size = 2048;              /* for setting send buffer size */
     131
    125132  unsigned int out_port = 0;
    126133  unsigned int in_port = 0;
    127   char         *mountpoint = NULL;
    128   char         *password = NULL;
    129   int          sock_id;
    130   char         szSendBuffer[BUFSZ];
    131   int          nBufferBytes;
    132   int          sockfd;
     134  char *mountpoint = NULL;
     135  char *password = "";
     136  int sock_id;
     137  char szSendBuffer[BUFSZ];
     138  int nBufferBytes;
    133139
    134140  struct hostent *inhost;
    135141  struct hostent *outhost;
    136  
     142
    137143  struct sockaddr_in in_addr;
    138144  struct sockaddr_in out_addr;
     
    140146  if(!(outhost = gethostbyname(NTRIP_CASTER)))
    141147  {
    142     fprintf(stderr, "host %s unknown\n\n", NTRIP_CASTER);
     148    fprintf(stderr, "WARNING: host %s unknown\n", NTRIP_CASTER);
    143149  }
    144150  else
     
    148154  }
    149155
    150   //get and check program arguments
    151   if (argc <=1)
    152     {
     156  /* get and check program arguments */
     157  if(argc <= 1)
     158  {
     159    usage(2);
     160    exit(1);
     161  }
     162  while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:")) != EOF)
     163  {
     164    switch (c)
     165    {
     166    case 'M':
     167      mode = atoi(optarg);
     168      if((mode == 0) || (mode > 3))
     169      {
     170        fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
     171        usage(-1);
     172      }
     173      break;
     174    case 'i':                  /* gps serial ttyin */
     175      ttyin = optarg;
     176      break;
     177    case 'b':                  /* serial ttyin speed */
     178      ttybaud = atoi(optarg);
     179      if(ttybaud <= 1)
     180      {
     181        fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", optarg);
     182        usage(1);
     183      }
     184      break;
     185    case 'a':                  /* http server IP address A.B.C.D */
     186      outhost = gethostbyname(optarg);
     187      if(outhost == NULL)
     188      {
     189        fprintf(stderr, "ERROR: host %s unknown\n", optarg);
     190        usage(-2);
     191      }
     192      memset((char *) &out_addr, 0x00, sizeof(out_addr));
     193      memcpy(&out_addr.sin_addr, outhost->h_addr, outhost->h_length);
     194      break;
     195    case 'p':                  /* http server port */
     196      out_port = atoi(optarg);
     197      if(out_port <= 1)
     198      {
     199        fprintf(stderr, "ERROR: can't convert %s to a valid HTTP server port\n",
     200          optarg);
     201        usage(1);
     202      }
     203      break;
     204    case 'm':                  /* http server mountpoint */
     205      mountpoint = optarg;
     206      break;
     207    case 'f':                  /* datastream from file */
     208      filepath = optarg;
     209      break;
     210    case 'c':                  /* password */
     211      password = optarg;
     212      break;
     213    case 'H':                  /* host */
     214      inhost = gethostbyname(optarg);
     215      if(inhost == NULL)
     216      {
     217        fprintf(stderr, "ERROR: host %s unknown\n", optarg);
     218        usage(-2);
     219      }
     220      memset((char *) &in_addr, 0x00, sizeof(in_addr));
     221      memcpy(&in_addr.sin_addr, inhost->h_addr, inhost->h_length);
     222      break;
     223    case 'P':                  /* port */
     224      in_port = atoi(optarg);
     225      if(in_port <= 1)
     226      {
     227        fprintf(stderr, "ERROR: can't convert %s to a valid receiver port\n",
     228          optarg);
     229        usage(1);
     230      }
     231      break;
     232    case 'h':                  /* help */
     233    case '?':
     234      usage(0);
     235      break;
     236    default:
    153237      usage(2);
    154       exit(1);
    155     }
    156   while ((c = getopt (argc, argv, "M:i:h:b:p:s:a:m:c:H:P:")) != EOF)
    157     {
    158       switch (c)
    159         {
    160         case 'M':
    161           mode = atoi(optarg);
    162           if ((mode == 0) || (mode > 3))
    163             {
    164               fprintf (stderr, "can't convert %s to a valid mode\n\n", optarg);
    165               usage(-1);
    166             }
    167           break;
    168         case 'i':               /* gps serial ttyin */
    169           ttyin = optarg;
    170           break;
    171         case 'b':               /* serial ttyin speed */
    172           ttybaud = atoi (optarg);
    173           if (ttybaud <= 1)
    174             {
    175               fprintf (stderr, "can't convert %s to valid ttyin speed\n\n", optarg);
    176               usage(1);
    177             }
    178           break;
    179         case 'a':               /* http server IP address A.B.C.D*/
    180           outhost = gethostbyname(optarg);
    181           if (outhost == NULL)
    182             {
    183               fprintf (stderr, "host %s unknown\n\n", optarg);
    184               usage(-2);
    185             }
    186           memset((char *) &out_addr, 0x00, sizeof(out_addr));
    187           memcpy(&out_addr.sin_addr, outhost->h_addr, outhost->h_length);
    188           break;
    189         case 'p':               /* http server port*/
    190           out_port = atoi(optarg);
    191           if (out_port <= 1)
    192             {
    193               fprintf (stderr, "can't convert %s to a valid http server port\n\n", optarg);
    194               usage(1);
    195             }
    196           break;
    197         case 'm':               /* http server mountpoint*/
    198           mountpoint = optarg;
    199           break;
    200         case 's':               /* simulate datastream from file */
    201           logfilepath = optarg;
    202           break;
    203         case 'c':               /* password */
    204           password=optarg;
    205           break;
    206         case 'H':               /* host */
    207           inhost = gethostbyname(optarg);
    208           if (inhost == NULL)
    209             {
    210               /* TODO Errorhandling/Debugging - Output */
    211               fprintf (stderr, "host %s unknown\n\n", optarg);
    212               usage(-2);
    213             }
    214           memset((char *) &in_addr, 0x00, sizeof(in_addr));
    215           memcpy(&in_addr.sin_addr, inhost->h_addr, inhost->h_length);
    216           break;
    217         case 'P':               /* port */
    218           in_port = atoi(optarg);
    219           if (in_port <= 1)
    220             {
    221               /* TODO Errorhandling/Debugging - Output */
    222               fprintf (stderr, "can't convert %s to a valid receiver port\n\n", optarg);
    223               usage(1);
    224             }
    225           break;
    226         case 'h':               /* help */
    227         case '?':
    228           usage(0);
    229           break;
    230         default:
    231           usage(2);
    232           break;
    233         }
    234      
    235       if (in_port <= 0 )
    236         {
    237           in_port = SERV_TCP_PORT;
    238         }
    239       if (out_port <= 0 )
    240         {
    241           out_port = NTRIP_PORT;
    242         }
    243     }
    244  
     238      break;
     239    }
     240
     241    if(in_port <= 0)
     242    {
     243      in_port = SERV_TCP_PORT;
     244    }
     245    if(out_port <= 0)
     246    {
     247      out_port = NTRIP_PORT;
     248    }
     249  }
     250
    245251  argc -= optind;
    246252  argv += optind;
    247  
    248   if (argc > 0)
    249     {
    250       /* TODO Errorhandling/Debugging - Output */
    251       fprintf (stderr, "Extra args on command line.\n");
    252       for (; argc > 0; argc--)
    253         {
    254           fprintf (stderr, " %s", *argv++);
    255         }
    256       fprintf (stderr, "\n");
    257       usage (1);                /* never returns */
    258     }
    259  
    260   if ((mode ==SIMULATE) && (tickinterval <= 0))
    261     {
    262       /* TODO Errorhandling/Debugging - Output */
    263       fprintf (stderr, "-t parameter must be positive in simulator mode\n");
    264       exit (1);
    265     }
    266  
    267   if (verbose >= 1)
    268     fprintf (stderr, "%s\n", VERSION);
    269   if (mountpoint == NULL)
    270     {
    271       /* TODO Errorhandling/Debugging - Output */
    272       fprintf(stderr,"missing mountpoint arguement\n");
    273       exit(1);
    274     }
    275   //print program arguments on screen
    276   /* TODO Errorhandling/Debugging - Output */
    277   // printf("\ncaster IP :\t%s\nport :\t%d\nmountpoint :\t%s\npassword :\t%s\n\n",address,port,mountpoint,(password==NULL)?"none":"yes");
    278   if (password == NULL)
    279     {
    280       password="\0";                           
    281     }
    282  
     253
     254  if(argc > 0)
     255  {
     256    fprintf(stderr, "ERROR: Extra args on command line: ");
     257    for(; argc > 0; argc--)
     258    {
     259      fprintf(stderr, " %s", *argv++);
     260    }
     261    fprintf(stderr, "\n");
     262    usage(1);                   /* never returns */
     263  }
     264
     265  if(mountpoint == NULL)
     266  {
     267    fprintf(stderr, "ERROR: Missing mountpoint argument\n");
     268    exit(1);
     269  }
     270  if(!password[0])
     271  {
     272    fprintf(stderr, "WARNING: Missing password argument - are you really sure?\n");
     273  }
     274
    283275  switch (mode)
    284     {
    285     case SIMULATE:
    286       {
    287         fp = fopen (logfilepath, "r");
    288         if (!fp)
    289           {
    290             /* TODO Errorhandling/Debugging - Output */
    291             perror ("fopen logfile");
    292             exit (1);
    293           }
    294       }
    295       break;
    296     case SERIAL:          //open serial port
    297       {
    298         gpsfd = openserial (ttyin, 1, ttybaud);
    299         fp = fdopen (gpsfd, "r");
    300         if (!fp)
    301           {
    302             /* TODO Errorhandling/Debugging - Output */
    303             perror ("fdopen gps");
    304             exit (1);
    305           }
    306       }
    307       break;
    308     case TCPSOCKET:
    309       {
    310         in_addr.sin_family      = AF_INET;
    311         in_addr.sin_port        = htons(in_port); 
    312        
    313         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    314           {
    315             /* TODO Errorhandling/Debugging - Output */
    316             fprintf (stderr, "can't create socket\n");
    317             exit(1);
    318           }
    319        
    320         /* TODO Errorhandling/Debugging - Output */
    321         printf("connecting input ...\n");
    322         fprintf (stderr, "socket input:\n");
    323         fprintf (stderr, "\tHost: %s\n", inet_ntoa(in_addr.sin_addr));
    324         fprintf (stderr, "\tPort: %d\n", in_port);
    325        
    326         if (connect(sockfd, (struct sockaddr *) &in_addr, sizeof(in_addr)) < 0)
    327           {
    328             /* TODO Errorhandling/Debugging - Output */
    329             fprintf (stderr, "can't connect input to %s at port %d\n", inet_ntoa(in_addr.sin_addr), in_port);
    330             //  exit(1);
    331           }
    332         gpsfd = sockfd;
    333       }
    334       break;
    335     default:
    336       usage(-1);
    337       break;
    338     }
    339  
     276  {
     277  case INFILE:
     278    {
     279      gpsfd = open(filepath, O_RDONLY);
     280      if(!gpsfd)
     281      {
     282        perror("ERROR: opening input file");
     283        exit(1);
     284      }
     285      /* set blocking mode in case it was not set (seems to be sometimes for fifo's) */
     286      fcntl(gpsfd, F_SETFL, 0);
     287      printf("file input: file = %s\n", filepath);
     288    }
     289    break;
     290  case SERIAL:                 /* open serial port */
     291    {
     292      gpsfd = openserial(ttyin, 1, ttybaud);
     293      if(gpsfd < 0)
     294      {
     295        exit(1);
     296      }
     297      printf("serial input: device = %s, speed = %d\n", ttyin, ttybaud);
     298    }
     299    break;
     300  case TCPSOCKET:
     301    {
     302      in_addr.sin_family = AF_INET;
     303      in_addr.sin_port = htons(in_port);
     304
     305      if((gpsfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
     306      {
     307        fprintf(stderr, "ERROR: can't create socket\n");
     308        exit(1);
     309      }
     310
     311      printf("socket input: host = %s, port = %d\n",
     312        inet_ntoa(in_addr.sin_addr), in_port);
     313
     314      if(connect(gpsfd, (struct sockaddr *) &in_addr, sizeof(in_addr)) < 0)
     315      {
     316        fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
     317          inet_ntoa(in_addr.sin_addr), in_port);
     318        exit(1);
     319      }
     320    }
     321    break;
     322  default:
     323    usage(-1);
     324    break;
     325  }
     326
    340327  out_addr.sin_family = AF_INET;
    341   out_addr.sin_port = htons((u_short)(out_port));
    342 
    343   // ----- main part -----
     328  out_addr.sin_port = htons((u_short) (out_port));
     329
     330  /* ----- main part ----- */
    344331  while(1)
    345     {
    346       //create socket
    347       if ((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    348         {
    349           /* TODO Errorhandling/Debugging - Output */
    350           printf("ERROR : could not create socket\n");
    351           // sleep(5);
    352           exit(2);
    353         }
    354       //connect to caster
    355       printf("connecting output ...\n");
    356       fprintf (stderr, "caster output:\n");
    357       fprintf (stderr, "\tHost: %s\n", inet_ntoa(out_addr.sin_addr));
    358       fprintf (stderr, "\tPort: %d\n", out_port);
    359       if (connect(sock_id, (struct sockaddr *) &out_addr, sizeof(out_addr)) < 0)
    360         {
    361           /* TODO Errorhandling/Debugging - Output */
    362           fprintf (stderr, "can't connect output to %s at port %d\n", inet_ntoa(out_addr.sin_addr), out_port);
    363           close(sock_id);
    364           // sleep(5);
    365           exit(3);
    366         }
    367        
    368       /* TODO Errorhandling/Debugging - Output */
    369       printf("connection succesfull\n");
    370       //set socket buffer size
    371       setsockopt(sock_id,SOL_SOCKET,SO_SNDBUF,(const char *) &size,sizeof(const char *));
    372       //send message to caster
    373       szSendBuffer[0] = '\0';
    374       sprintf(szSendBuffer,"SOURCE %s /%s\r\n",password,mountpoint);
    375       strcat(szSendBuffer,"Source-Agent: ");
    376       strcat(szSendBuffer,VERSION);
    377       strcat(szSendBuffer, "\r\n");
    378       strcat(szSendBuffer, "\r\n");
    379       strcat(szSendBuffer, "\0");
    380       nBufferBytes=strlen(szSendBuffer);
    381       if ((send(sock_id, szSendBuffer, nBufferBytes, 0)) != nBufferBytes)
    382         {
    383           /* TODO Errorhandling/Debugging - Output */
    384           fprintf(stderr, "ERROR : could not send to caster\n");               
    385           close(sock_id);
    386           sleep(5);
    387           exit(0);
    388         }
    389       //check caster's response
    390       nBufferBytes=recv(sock_id,szSendBuffer,sizeof(szSendBuffer),0);
    391       szSendBuffer[nBufferBytes]='\0';
    392       if(strcmp(szSendBuffer,"OK\r\n"))
    393         {
    394           /* TODO Errorhandling/Debugging - Output */
    395           fprintf(stderr,"caster's reply is not OK\n");
    396           close(sock_id);
    397           sleep(5);
    398           exit(0);
    399         }
    400       send_receive_loop(sock_id, gpsfd);
    401     }
     332  {
     333    /* create socket */
     334    if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
     335    {
     336      fprintf(stderr, "ERROR : could not create socket\n");
     337      exit(2);
     338    }
     339    /* connect to caster */
     340    fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
     341      inet_ntoa(out_addr.sin_addr), out_port, mountpoint);
     342    if(connect(sock_id, (struct sockaddr *) &out_addr, sizeof(out_addr)) < 0)
     343    {
     344      fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
     345        inet_ntoa(out_addr.sin_addr), out_port);
     346      close(sock_id);
     347      exit(3);
     348    }
     349
     350    /* set socket buffer size */
     351    setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
     352      sizeof(const char *));
     353    /* send message to caster */
     354    szSendBuffer[0] = '\0';
     355    sprintf(szSendBuffer, "SOURCE %s /%s\r\n", password, mountpoint);
     356    strcat(szSendBuffer, "Source-Agent: ");
     357    strcat(szSendBuffer, VERSION);
     358    strcat(szSendBuffer, "\r\n");
     359    strcat(szSendBuffer, "\r\n");
     360    strcat(szSendBuffer, "\0");
     361    nBufferBytes = strlen(szSendBuffer);
     362    if((send(sock_id, szSendBuffer, nBufferBytes, 0)) != nBufferBytes)
     363    {
     364      fprintf(stderr, "ERROR: could not send to caster\n");
     365      close(sock_id);
     366      sleep(5);
     367      exit(0);
     368    }
     369    /* check caster's response */
     370    nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
     371    szSendBuffer[nBufferBytes] = '\0';
     372    if(strcmp(szSendBuffer, "OK\r\n"))
     373    {
     374      char *a;
     375      fprintf(stderr, "ERROR: caster's reply is not OK : ");
     376      for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
     377      {
     378        fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
     379      }
     380      fprintf(stderr, "\n");
     381      close(sock_id);
     382      sleep(5);
     383      exit(0);
     384    }
     385    printf("connection succesfull\n");
     386    send_receive_loop(sock_id, gpsfd);
     387  }
    402388  exit(0);
    403389}
    404390
    405 int send_receive_loop(int socket, int fd)
     391void send_receive_loop(int socket, int fd)
    406392{
    407   char buffer[BUFSZ]={0};
    408   int nBufferBytes=0;
    409   //data transmission
     393  char buffer[BUFSZ] = { 0 };
     394  int nBufferBytes = 0, i;
     395  /* data transmission */
    410396  printf("transfering data ...\n");
    411397  while(1)
    412     {
    413       // recieving data
     398  {
     399    if(!nBufferBytes)
     400    {
     401      /* receiving data */
    414402      nBufferBytes = read(fd, buffer, BUFSZ);
    415       if (nBufferBytes<=0)
    416         {
    417           /* TODO Errorhandling/Debugging - Output */
    418           printf("ERROR : no data recieved from serial port or socket\n");
    419         }
    420       // send data
    421       if((send(socket, buffer, nBufferBytes, MSG_DONTWAIT)) != nBufferBytes)
    422         {                                       //MSG_DONTWAIT  : send works in non-blocking mode
    423           /* TODO Errorhandling/Debugging - Output */
    424           fprintf(stderr,"ERROR : could not send data\n");             
    425           close(socket);
    426           sleep(5);
    427           exit(4);
    428         }
    429     }
     403      if(!nBufferBytes)
     404      {
     405        printf("WARNING: no data received from input\n");
     406        continue;
     407      }
     408      else if(nBufferBytes < 0)
     409      {
     410        perror("ERROR: reading input failed");
     411        exit(1);
     412      }
     413    }
     414    /* send data */
     415    if((i = send(socket, buffer, nBufferBytes, MSG_DONTWAIT)) != nBufferBytes)
     416    {
     417      if(i < 0 && errno != EAGAIN)
     418      {
     419        perror("WARNING: could not send data - retry connection");
     420        close(socket);
     421        sleep(5);
     422        return;
     423      }
     424      else if(i)
     425      {
     426        memmove(buffer, buffer+i, nBufferBytes-i);
     427        nBufferBytes -= i;
     428      }
     429    }
     430    else
     431    {
     432      nBufferBytes = 0;
     433    }
     434  }
    430435}
    431436
     
    450455 */
    451456
    452 int openserial (u_char * tty, int blocksz, int ttybaud)
     457int openserial(u_char * tty, int blocksz, int ttybaud)
    453458{
    454   int             fd;
    455   struct termios  termios;
    456  
    457   fd = open(tty, O_RDWR | O_NONBLOCK
    458 #ifdef O_EXLOCK                 /* for Linux */
    459             | O_EXLOCK
    460 #endif
    461             );
    462   if (fd < 0) {
    463     perror("open");
     459  int fd;
     460  struct termios termios;
     461
     462  fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
     463  if(fd < 0)
     464  {
     465    perror("ERROR: opening serial connection");
    464466    return (-1);
    465467  }
    466   if (tcgetattr(fd, &termios) < 0) {
    467    
    468     perror("tcgetattr");
     468  if(tcgetattr(fd, &termios) < 0)
     469  {
     470    perror("ERROR: get serial attributes");
    469471    return (-1);
    470472  }
    471473  termios.c_iflag = 0;
    472   termios.c_oflag = 0;        /* (ONLRET) */
     474  termios.c_oflag = 0;          /* (ONLRET) */
    473475  termios.c_cflag = CS8 | CLOCAL | CREAD;
    474476  termios.c_lflag = 0;
    475477  {
    476     int   cnt;
    477     for (cnt = 0; cnt < NCCS; cnt++)
     478    int cnt;
     479    for(cnt = 0; cnt < NCCS; cnt++)
    478480      termios.c_cc[cnt] = -1;
    479481  }
    480482  termios.c_cc[VMIN] = blocksz;
    481483  termios.c_cc[VTIME] = 2;
    482  
     484
    483485#if (B4800 != 4800)
    484486  /*
    485487   * Not every system has speed settings equal to absolute speed value.
    486488   */
    487  
     489
    488490  switch (ttybaud)
    489     {
    490     case 300:
    491       ttybaud = B300;
    492       break;
    493     case 1200:
    494       ttybaud = B1200;
    495       break;
    496     case 2400:
    497       ttybaud = B2400;
    498       break;
    499     case 4800:
    500       ttybaud = B4800;
    501       break;
    502     case 9600:
    503       ttybaud = B9600;
    504       break;
    505     case 19200:
    506       ttybaud = B19200;
    507       break;
    508     case 38400:
    509       ttybaud = B38400;
    510       break;
     491  {
     492  case 300:
     493    ttybaud = B300;
     494    break;
     495  case 1200:
     496    ttybaud = B1200;
     497    break;
     498  case 2400:
     499    ttybaud = B2400;
     500    break;
     501  case 4800:
     502    ttybaud = B4800;
     503    break;
     504  case 9600:
     505    ttybaud = B9600;
     506    break;
     507  case 19200:
     508    ttybaud = B19200;
     509    break;
     510  case 38400:
     511    ttybaud = B38400;
     512    break;
    511513#ifdef B57600
    512     case 57600:
    513       ttybaud = B57600;
    514       break;
     514  case 57600:
     515    ttybaud = B57600;
     516    break;
    515517#endif
    516518#ifdef B115200
    517     case 115200:
    518       ttybaud = B115200;
    519       break;
     519  case 115200:
     520    ttybaud = B115200;
     521    break;
    520522#endif
    521523#ifdef B230400
    522     case 230400:
    523       ttybaud = B230400;
    524       break;
     524  case 230400:
     525    ttybaud = B230400;
     526    break;
    525527#endif
    526     default:
    527       fprintf(stderr, "Baud settings not useful, using 19200\n");
    528       ttybaud = B19200;
    529       break;
    530     }
     528  default:
     529    fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
     530    ttybaud = B19200;
     531    break;
     532  }
    531533#endif
    532  
    533   if (cfsetispeed(&termios, ttybaud) != 0)
    534     {
    535       perror("cfsetispeed");
    536       return (-1);
    537     }
    538   if (cfsetospeed(&termios, ttybaud) != 0)
    539     {
    540       perror("cfsetospeed");
    541       return (-1);
    542     }
    543   if (tcsetattr(fd, TCSANOW, &termios) < 0)
    544     {
    545       perror("tcsetattr");
    546       return (-1);
    547     }
    548 #if 1                           /* WANT_BLOCKING_READ */
    549   if (fcntl(fd, F_SETFL, 0) == -1)
    550     {
    551       perror("fcntl: set nonblock");
    552     }
    553 #endif
     534
     535  if(cfsetispeed(&termios, ttybaud) != 0)
     536  {
     537    perror("ERROR: setting serial speed with cfsetispeed");
     538    return (-1);
     539  }
     540  if(cfsetospeed(&termios, ttybaud) != 0)
     541  {
     542    perror("ERROR: setting serial speed with cfsetospeed");
     543    return (-1);
     544  }
     545  if(tcsetattr(fd, TCSANOW, &termios) < 0)
     546  {
     547    perror("ERROR: setting serial attributes");
     548    return (-1);
     549  }
     550  if(fcntl(fd, F_SETFL, 0) == -1)
     551  {
     552    perror("WARNING: setting blocking mode failed");
     553  }
    554554  return (fd);
    555555}
     
    570570 */
    571571
    572 void usage (int rc)
     572void usage(int rc)
    573573{
    574   fprintf (stderr, "%s: application\n",VERSION);
    575   fprintf (stderr, "Usage: %s [OPTIONS]\n",VERSION);
    576   fprintf (stderr, "  Options are:\n");
    577   fprintf (stderr, "    -a caster name or address (default: %s)\n", NTRIP_CASTER);
    578   fprintf (stderr, "    -p caster port (default: %d)\n", NTRIP_PORT);
    579   fprintf (stderr, "    -m caster mountpoint\n");
    580   fprintf (stderr, "    -c password for caster login\n");
    581   fprintf (stderr, "    -h|? print this help screen\n");
    582   fprintf (stderr, "    -M <mode>  sets the mode\n");
    583   fprintf (stderr, "               (1=serial, 2=tcpsocket, 3=simulate)\n");
    584   fprintf (stderr, "  Mode = simulate:\n");
    585   fprintf (stderr, "    -s file, simulate data stream by reading log file\n");
    586   fprintf (stderr, "       default/current setting is %s\n",  (simulate ? "enabled" : "disabled"));
    587   fprintf (stderr, "  Mode = serial:\n");
    588   fprintf (stderr, "    -b baud_rate, sets serial input baud rate\n");
    589   fprintf (stderr, "       default/current value is %d\n", ttybaud);
    590   fprintf (stderr, "    -i input_device, sets name of serial input device\n");
    591   fprintf (stderr, "       default/current value is %s\n", ttyport);
    592   fprintf (stderr, "       (normally a symbolic link to /dev/tty\?\?)\n");
    593   fprintf (stderr, "  Mode = tcpsocket:\n");
    594   fprintf (stderr, "    -P receiver port (default: 1025)\n");
    595   fprintf (stderr, "    -H hostname of TCP server (default: 127.0.0.1)\n");
    596   fprintf (stderr, "    \n");
    597   exit (rc);
     574  fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
     575  fprintf(stderr, "  Options are:\n");
     576  fprintf(stderr, "    -a caster name or address (default: %s)\n",
     577    NTRIP_CASTER);
     578  fprintf(stderr, "    -p caster port (default: %d)\n", NTRIP_PORT);
     579  fprintf(stderr, "    -m caster mountpoint\n");
     580  fprintf(stderr, "    -c password for caster login\n");
     581  fprintf(stderr, "    -h|? print this help screen\n");
     582  fprintf(stderr, "    -M <mode>  sets the mode\n");
     583  fprintf(stderr, "               (1=serial, 2=tcpsocket, 3=file)\n");
     584  fprintf(stderr, "  Mode = file:\n");
     585  fprintf(stderr, "    -s file, simulate data stream by reading log file\n");
     586  fprintf(stderr, "       default/current setting is %s\n", filepath);
     587  fprintf(stderr, "  Mode = serial:\n");
     588  fprintf(stderr, "    -b baud_rate, sets serial input baud rate\n");
     589  fprintf(stderr, "       default/current value is %d\n", ttybaud);
     590  fprintf(stderr, "    -i input_device, sets name of serial input device\n");
     591  fprintf(stderr, "       default/current value is %s\n", ttyport);
     592  fprintf(stderr, "       (normally a symbolic link to /dev/tty\?\?)\n");
     593  fprintf(stderr, "  Mode = tcpsocket:\n");
     594  fprintf(stderr, "    -P receiver port (default: 1025)\n");
     595  fprintf(stderr, "    -H hostname of TCP server (default: 127.0.0.1)\n");
     596  fprintf(stderr, "    \n");
     597  exit(rc);
    598598}
Note: See TracChangeset for help on using the changeset viewer.