summaryrefslogtreecommitdiff
path: root/src/external/bsd/meinberg/dist/mbgshowsignal/mbgshowsignal.c
blob: 308fec85017501c65f62bda0ed9592bd92676094 (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

/**************************************************************************
 *
 *  $Id: mbgshowsignal.c 1.8 2009/09/29 15:02:15 martin REL_M $
 *
 *  Description:
 *    Main file for mbgshowsignal program which demonstrates how to
 *    access a Meinberg device via IOCTL calls and show the modulation
 *    signal (second marks) of a longwave receiver, e.g. for DCF77.
 *
 * -----------------------------------------------------------------------
 *  $Log: mbgshowsignal.c $
 *  Revision 1.8  2009/09/29 15:02:15  martin
 *  Updated version number to 3.4.0.
 *  Revision 1.7  2009/07/24 09:50:09  martin
 *  Updated version number to 3.3.0.
 *  Revision 1.6  2009/06/19 12:38:52  martin
 *  Updated version number to 3.2.0.
 *  Revision 1.5  2009/03/19 17:04:26  martin
 *  Updated version number to 3.1.0.
 *  Updated copyright year to include 2009.
 *  Revision 1.4  2008/12/22 12:44:43  martin
 *  Changed ealier program name mbgdcfmod to mbgshowsignal.
 *  Updated description, copyright, revision number and string.
 *  Use unified functions from toolutil module.
 *  Accept device name(s) on the command line.
 *  Don't use printf() without format, which migth produce warnings
 *  with newer gcc versions.
 *  Revision 1.3  2007/07/24 09:32:11  martin
 *  Updated copyright to include 2007.
 *  Revision 1.2  2003/04/25 10:28:05  martin
 *  Use new functions from mbgdevio library.
 *  New program version v2.1.
 *  Revision 1.1  2001/09/17 15:08:09  martin
 *
 **************************************************************************/

// include Meinberg headers
#include <mbgdevio.h>
#include <pcpsutil.h>
#include <toolutil.h>  // common utility functions

// include system headers
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


static const char *pname = "mbgshowsignal";
static const char *pversion = "v3.4.0";
static const char *pcopyright = "(c) Meinberg 2001-2009";



static /*HDR*/
int show_modulation( MBG_DEV_HANDLE dh )
{
  static time_t prv_sys_t;
  time_t sys_t;
  uchar status_port;   // current value of the clock's status port
  PCPS_TIME t;
  int signal;
  int rc = mbg_get_status_port( dh, &status_port );  // read status port

  if ( mbg_ioctl_err( rc, "mbg_get_status_port" ) )
    return -1;

  // show signal only once per second
  sys_t = time( NULL );

  if ( sys_t == prv_sys_t )
    return 0;

  prv_sys_t = sys_t;

  rc = mbg_get_time( dh, &t );

  if ( mbg_ioctl_err( rc, "mbg_get_time" ) )
    return -1;


  // The current value of the modulation (second mark) is returned
  // in a single bit of the status port, so ignore the other bits.
  printf( "\rMod: %c", ( status_port & PCPS_ST_MOD ) ? '*' : '_' );

  signal = t.signal - PCPS_SIG_BIAS;

  if ( signal < 0 )
    signal = 0;
  else
    if ( signal > PCPS_SIG_MAX )
      signal = PCPS_SIG_MAX;

  printf( "  Signal: %u%%   ", signal * 100 / PCPS_SIG_MAX );

  return 0;

}  // show_modulation



static /*HDR*/
int do_mbgshowsignal( MBG_DEV_HANDLE dh, const PCPS_DEV *p_dev )
{
  int has_mod = 0;
  int rc = mbg_dev_has_mod( dh, &has_mod );

  if ( mbg_ioctl_err( rc, "mbg_dev_has_mod" ) )
    goto fail;

  if ( !has_mod )
  {
    printf( "This device does not support monitoring signal modulation.\n" );
    goto done;
  }

  printf( "\nMonitoring signal modulation:\n" );

  for (;;)
    if ( show_modulation( dh ) < 0 )
      goto fail;

done:
  return 0;

fail:
  return -1;

}  // do_mbgshowsignal



static /*HDR*/
void usage( void )
{
  mbg_print_usage_intro( pname,
    "This program displays the modulation signal of cards which receive\n"
    "a slowly modulated input signal, e.g. the longwave signal from DCF77.\n"
  );
  mbg_print_help_options();
  mbg_print_device_options();
  puts( "" );

}  // usage



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

  mbg_print_program_info( pname, pversion, pcopyright );

  // check command line parameters
  while ( ( c = getopt( argc, argv, "h?" ) ) != -1 )
  {
    switch ( c )
    {
      case 'h':
      case '?':
      default:
        must_print_usage = 1;
    }
  }

  if ( must_print_usage )
  {
    usage();
    return 1;
  }

  // The function below checks which devices have been specified
  // on the command, and for each device
  // - tries to open the device
  // - shows basic device info
  // - calls the function passed as last parameter
  rc = mbg_check_devices( argc, argv, optind, do_mbgshowsignal );

  return abs( rc );
}