summaryrefslogtreecommitdiff
path: root/mbgreadtimestring.c
blob: 785a7b92921ab518b516725dda82e5a6a3243558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

/**************************************************************************
 *
 *  Sample program demonstrating how to read a serial time string
 *  from an external device.
 *
 *  Optionally, a request character can be sent before waiting
 *  for the time string.
 *
 *  The received time string is printed to stdout.
 *
 *  For makefiles and build environment setups check the associated
 *  subdirectories.
 *
 *  Comments can be sent to <martin.burnicki@meinberg.de>
 *
 *  Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
 *
 **************************************************************************/

#include <mbgserio.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MBG_NUMERIC_VERSION_STR "1.4"

// If building within a git repo, we prefer the version
// string from git, else the version defined above.

#if defined( MBG_VERSION_FROM_GIT )
  #define MBG_FULL_VERSION_STR  STRINGIFY( MBG_VERSION_FROM_GIT )
#else
  #define MBG_FULL_VERSION_STR  MBG_NUMERIC_VERSION_STR
#endif

static const char *pname = "mbgreadtimestring";
static const char *pcopyright = "(c) Meinberg 2009-2024";


/* other local variables */
static const char *target;
static int must_print_usage;
static int must_query;
static int quiet;
static ulong poll_timeout = 2000;  // ms

static const char *default_target = DEFAULT_SERIAL_DEVICE_NAME;

static MBGSERIO_DEV *p_sdev;

#if _USE_SERIAL_IO
  static uint32_t default_baudrate = 19200L;
  static const char *default_framing = "8N1";
  static uint32_t baudrate;
  static const char *framing;
  static int must_force_connection;
#endif



static /*HDR*/
int read_timestring( MBGSERIO_DEV *sdev, char *ts, int max_len )
{
  char *cp = ts;
  int received_stx = 0;

  for (;;)
  {
    int i;
    char buffer[80];
    int rc = mbgserio_read_wait( sdev, buffer, sizeof( buffer ) );

    if ( mbg_rc_is_error( rc ) )
    {
      fprintf(stderr, "Read failed: %s\n", mbg_strerror( rc ) );
      return rc;
    }


    // rc holds the number of bytes read.

    for ( i = 0; i < rc; i++ )
    {
      char c = buffer[i];

      switch ( c )
      {
        case 2:              // STX: ignore
          received_stx = 1;  // just remember we received it
          continue;

        case 3:              // ETX: ignore
          goto done;         // but done
      }

      if ( !received_stx )
        continue;

      if ( max_len == 0 )
        goto done;

      *cp++ = c;
      max_len--;
    }

  } while ( 0 );

done:
  return cp - ts;   // number of bytes saved

}  // read_timestring



/*HDR*/ static
void close_connection( void )
{
  mbgserio_close( &p_sdev );

} /* close_connection */



static /*HDR*/
void usage( const char *progname )
{
  printf( "Optionally send a request character, then read a time string\n"
          "from a Meinberg device connected via serial port.\n"
          "\n"
        );

  printf( "Usage: %s [-r] [-q] target\n"
          "\n",
          progname
        );

  printf( "  -r           Send a query/request character first\n"
        );

  printf( "  -q           Quiet, print nothing but the received time string\n"
        );

  printf( "\n"
          "Optional arguments:\n"
          "  -? or -h     Print this usage\n"
        );

#if _USE_SERIAL_IO
  printf( "  -b speed     serial port baud rate, default: %lu\n"
          "  -f xxx       serial port framing, default: %s\n"
          "  -F           force serial connection to %lu/%s\n",
          (long) default_baudrate,
          framing,
          (long) default_baudrate,
          framing
        );
#endif

  printf( "\n"
          "Target argument:\n"
        );
#if _USE_SERIAL_IO
  printf( "  can specify a serial port, default: %s\n",
          default_target
        );
#endif

  printf( "\n" );

}  // usage



// Do a very simple processing of command line parameters:

static /*HDR*/
int check_command_line( int argc, char *argv[] )
{
  int i;

  for ( i = 1; i < argc; i++ )
  {
    if ( strcmp( argv[i], "-r" ) == 0 )
    {
      must_query = 1;
      continue;
    }

    if ( strcmp( argv[i], "-q" ) == 0 )
    {
      quiet = 1;
      continue;
    }

    if ( strcmp( argv[i], "-?" ) == 0 )
    {
      must_print_usage = 1;
      continue;
    }

    if ( strcmp( argv[i], "-h" ) == 0 )
    {
      must_print_usage = 1;
      continue;
    }

    #if _USE_SERIAL_IO
      if ( strcmp( argv[i], "-b" ) == 0 )
      {
        if ( ++i < argc )
          baudrate = strtoul( argv[i], NULL, 10 );
        else
          must_print_usage = 1;

        continue;
      }
    #endif

    #if _USE_SERIAL_IO
      if ( strcmp( argv[i], "-f" ) == 0 )
      {
        if ( ++i < argc )
          framing = argv[i];
        else
          must_print_usage = 1;

        continue;
      }
    #endif

    #if _USE_SERIAL_IO
      if ( strcmp( argv[i], "-F" ) == 0 )
      {
        must_force_connection = 1;
        continue;
      }
    #endif

    if ( argv[i][0] != '-' )
      target = argv[i];
    else
      must_print_usage = 1;
  }

  if ( target == NULL )
    target = default_target;

  return 0;

}  // check_command_line



int main( int argc, char *argv[] )
{
  char ts[80];
  int rc;

  check_command_line( argc, argv );

  if ( !quiet )
    printf( "\n\n\n%s v%s %s\n\n", pname, MBG_FULL_VERSION_STR, pcopyright );

  #if _USE_SERIAL_IO
    baudrate = default_baudrate;
    framing = default_framing;
  #endif

  check_command_line( argc, argv );

  if ( must_print_usage )
  {
    if ( !quiet )
      usage( pname );

    return 1;
  }

  #if _USE_SERIAL_IO
    rc = mbgserio_open( &p_sdev, target );

    if ( mbg_rc_is_error( rc ) )
    {
      fprintf( stderr, "Failed to open %s: %s\n", target, mbg_strerror( rc ) );
      return 1;      // Error ...
    }

    mbgserio_set_parms( p_sdev, baudrate, framing );
    mbgserio_set_dev_poll_timeout( p_sdev, poll_timeout );

    if ( !quiet )
      printf( "Using %s with %li baud, %s\n",
              target, (long) baudrate, framing );

  #endif

  atexit( close_connection );

  // now send the new date and time to the device

  if ( must_query )
  {
    char request_char = '?';
    mbgserio_write( p_sdev, &request_char, sizeof( request_char ) );
  }

  rc = read_timestring( p_sdev, ts, sizeof( ts ) );

  if ( mbg_rc_is_success( rc ) )
  {
    ts[rc] = 0;  // terminating 0
    printf( "%s\n", ts );
  }

  return 0;

}  /* main */