mbgtools-lx  4.2.8
mbgshowsignal.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbgshowsignal.c 1.11 2019/03/19 16:43:51 martin REL_M $
5  *
6  * Description:
7  * Main file for mbgshowsignal program which demonstrates how to
8  * access a Meinberg device via IOCTL calls and show the modulation
9  * signal (second marks) of a longwave receiver, e.g. for DCF77.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: mbgshowsignal.c $
13  * Revision 1.11 2019/03/19 16:43:51 martin
14  * Support force reading status port even if device provides no modulation signal.
15  * Revision 1.10 2018/11/15 12:12:36 martin
16  * Individual MBG_MICRO_VERSION codes are now obsolete.
17  * Revision 1.9 2017/07/05 18:31:14 martin
18  * New way to maintain version information.
19  * Support build under Windows.
20  * Update modulation status continuously.
21  * Show PZF correlation state.
22  * Use codes and inline functions from mbgerror.h.
23  * Proper return codes and exit codes.
24  * Revision 1.8 2009/09/29 15:02:15 martin
25  * Updated version number to 3.4.0.
26  * Revision 1.7 2009/07/24 09:50:09 martin
27  * Updated version number to 3.3.0.
28  * Revision 1.6 2009/06/19 12:38:52 martin
29  * Updated version number to 3.2.0.
30  * Revision 1.5 2009/03/19 17:04:26 martin
31  * Updated version number to 3.1.0.
32  * Updated copyright year to include 2009.
33  * Revision 1.4 2008/12/22 12:44:43 martin
34  * Changed ealier program name mbgdcfmod to mbgshowsignal.
35  * Updated description, copyright, revision number and string.
36  * Use unified functions from toolutil module.
37  * Accept device name(s) on the command line.
38  * Don't use printf() without format, which migth produce warnings
39  * with newer gcc versions.
40  * Revision 1.3 2007/07/24 09:32:11 martin
41  * Updated copyright to include 2007.
42  * Revision 1.2 2003/04/25 10:28:05 martin
43  * Use new functions from mbgdevio library.
44  * New program version v2.1.
45  * Revision 1.1 2001/09/17 15:08:09 martin
46  *
47  **************************************************************************/
48 
49 // include Meinberg headers
50 #include <mbgdevio.h>
51 #include <toolutil.h> // common utility functions
52 
53 // include system headers
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <time.h>
57 
58 
59 #define MBG_FIRST_COPYRIGHT_YEAR 2001
60 #define MBG_LAST_COPYRIGHT_YEAR 0 // use default
61 
62 static const char *pname = "mbgshowsignal";
63 
64 static int force_read;
65 
66 
67 
68 static /*HDR*/
70 {
71  static time_t prv_sys_t;
72  time_t sys_t;
73  PCPS_STATUS_PORT status_port; // current value of the clock's status port
74  PCPS_TIME t;
75  int signal;
76 
77  bool dev_has_pzf = mbg_chk_dev_has_pzf( dh ) == MBG_SUCCESS;
78 
79  int rc = mbg_get_status_port( dh, &status_port ); // read status port
80 
81  if ( mbg_cond_err_msg( rc, "mbg_get_status_port" ) )
82  return rc;
83 
84  // show signal only once per second
85  sys_t = time( NULL );
86 
87  if ( sys_t != prv_sys_t )
88  {
89  rc = mbg_get_time( dh, &t );
90 
91  if ( mbg_cond_err_msg( rc, "mbg_get_time" ) )
92  return rc;
93 
94  prv_sys_t = sys_t;
95  }
96 
97  // The current value of the modulation (second mark) is returned
98  // in a single bit of the status port, so ignore the other bits.
99  printf( "\rMod: %c", ( status_port & PCPS_ST_MOD ) ? '*' : '_' );
100 
101  signal = t.signal - PCPS_SIG_BIAS;
102 
103  if ( signal < 0 )
104  signal = 0;
105  else
106  if ( signal > PCPS_SIG_MAX )
107  signal = PCPS_SIG_MAX;
108 
109  printf( " Signal: %u%% ", signal * 100 / PCPS_SIG_MAX );
110 
111  if ( dev_has_pzf )
112  rc = mbg_show_pzf_corr_info( dh, 1 );
113 
114  printf( " " );
115 
116  return rc;
117 
118 } // show_modulation
119 
120 
121 
122 static /*HDR*/
123 int do_mbgshowsignal( MBG_DEV_HANDLE dh, const PCPS_DEV *p_dev )
124 {
125  int do_force_read = 0;
126  int rc = mbg_chk_dev_has_mod( dh );
127 
128  if ( mbg_rc_is_error( rc ) )
129  {
130  if ( ( rc != MBG_ERR_NOT_SUPP_BY_DEV ) || !force_read )
131  {
132  mbg_cond_err_msg_info( rc, "mbg_dev_has_mod",
133  "support monitoring signal modulation" );
134  return rc;
135  }
136  else
137  do_force_read = 1;
138  }
139 
140 
141  printf( "\nMonitoring signal modulation" );
142 
143  if ( do_force_read )
144  printf( " (forced)" );
145 
146  printf( ":\n" );
147 
148  for (;;)
149  {
150  rc = show_modulation( dh );
151 
152  if ( mbg_rc_is_error( rc ) )
153  break;
154  }
155 
156  return rc;
157 
158 } // do_mbgshowsignal
159 
161 
162 
163 
164 static /*HDR*/
165 void usage( void )
166 {
168  "This program displays the modulation signal of cards which receive\n"
169  "a slowly modulated input signal, e.g. the longwave signal from DCF77.\n"
170  );
172  mbg_print_opt_info( "-F", "force reading status port even if signal not supported" );
174  puts( "" );
175 
176 } // usage
177 
178 
179 
180 int main( int argc, char *argv[] )
181 {
182  int c;
183  int rc;
184 
186 
187  // check command line parameters
188  while ( ( c = getopt( argc, argv, "Fh?" ) ) != -1 )
189  {
190  switch ( c )
191  {
192  case 'F':
193  force_read = 1;
194  break;
195 
196  case 'h':
197  case '?':
198  default:
199  must_print_usage = 1;
200  }
201  }
202 
203  if ( must_print_usage )
204  {
205  usage();
206  return MBG_EXIT_CODE_USAGE;
207  }
208 
209  // Handle each of the specified devices.
210  rc = mbg_handle_devices( argc, argv, optind, do_mbgshowsignal, 0 );
211 
213 }
int mbg_handle_devices(int argc, char *argv[], int optind, MBG_DEV_HANDLER_FNC *fnc, int chk_dev_flags)
Main action handler that can be called by utility programs.
Definition: toolutil.c:655
int must_print_usage
int mbg_show_pzf_corr_info(MBG_DEV_HANDLE dh, int show_corr_step)
Retrieve and print PZF correlation info for a device which supports this.
Definition: toolutil.c:991
Requested action completed successfully.
Definition: mbgerror.h:631
bool mbg_cond_err_msg(int rc, const char *what)
Check if a value is an error code and print an associated error message.
Definition: mbgerror.c:714
int main(int argc, char *argv[])
uint8_t PCPS_STATUS_PORT
Type of the status register port.
Definition: pcpsdefs.h:431
_MBG_API_ATTR int _MBG_API mbg_get_time(MBG_DEV_HANDLE dh, PCPS_TIME *p)
Read a PCPS_TIME structure returning the current date/time/status.
Definition: mbgdevio.c:4491
Unable to handle requested action, usage printed.
Definition: mbgerror.h:632
#define mbg_rc_is_success(_rc)
Definition: mbgerror.h:618
static MBG_DEV_HANDLER_FNC do_mbgshowsignal
#define PCPS_SIG_MAX
Definition: pcpsdefs.h:1058
void mbg_print_device_options(void)
Print common info on how to specify devices on the command line.
Definition: toolutil.c:270
void mbg_print_usage_intro(const char *pname, const char *info)
Print usage intro to console.
Definition: toolutil.c:217
#define PCPS_ST_MOD
the raw demodulated DCF77 signal
Definition: pcpsdefs.h:454
#define PCPS_SIG_BIAS
Definition: pcpsdefs.h:1055
static void usage(void)
static const char * pname
Definition: mbgshowsignal.c:62
bool mbg_cond_err_msg_info(int rc, const char *what, const char *info)
Check if a value is an general or a "not supported" error code and print an associated message...
Definition: mbgerror.c:742
static int show_modulation(MBG_DEV_HANDLE dh)
Definition: mbgshowsignal.c:69
int MBG_DEV_HANDLER_FNC(MBG_DEV_HANDLE, const PCPS_DEV *)
The type of functions to called to handle a device in a specific way.
Definition: toolutil.h:144
#define MBG_SUCCESS
Error codes used with Meinberg devices and drivers.
Definition: mbgerror.h:259
MBG_CHK_SUPP_FNC mbg_chk_dev_has_mod
Check if a device provides a modulation signal.
Definition: mbgdevio.c:1797
#define MBG_LAST_COPYRIGHT_YEAR
Definition: mbgshowsignal.c:60
_MBG_API_ATTR int _MBG_API mbg_get_status_port(MBG_DEV_HANDLE dh, PCPS_STATUS_PORT *p)
Read the current state of the on-board PCPS_STATUS_PORT.
Definition: mbgdevio.c:4273
Action failed for specified device.
Definition: mbgerror.h:634
void mbg_print_help_options(void)
Print info on common program help arguments.
Definition: toolutil.c:257
PCPS_SIG_VAL signal
signal strength, see PCPS_SIG_VAL_DEFS
Definition: pcpsdefs.h:1141
Local calendar date and time, plus sync status.
Definition: pcpsdefs.h:1128
void mbg_print_opt_info(const char *opt_name, const char *opt_info)
Print info on a single program option / argument.
Definition: toolutil.c:236
#define MBG_ERR_NOT_SUPP_BY_DEV
Command or feature not supported by device.
Definition: mbgerror.h:286
#define MBG_FIRST_COPYRIGHT_YEAR
Definition: mbgshowsignal.c:59
#define mbg_rc_is_error(_rc)
Definition: mbgerror.h:617
void mbg_print_program_info(const char *pname, int first_year, int last_year)
Print program info to console.
Definition: toolutil.c:193
static int force_read
Definition: mbgshowsignal.c:64
MBG_CHK_SUPP_FNC mbg_chk_dev_has_pzf
Check if a device supports demodulation of the DCF77 PZF code.
Definition: mbgdevio.c:1092
Device info structure.
Definition: pcpsdev.h:1043