Main Page   Data Structures   File List   Data Fields   Globals  

fake_daq.c File Reference

A fake DAQ program for testing NSDS et al. More...

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <pthread.h>
#include <getopt.h>
#include "nsds_util.h"
#include "flog.h"

Go to the source code of this file.

Functions

int data_active_count (void)
 Return number of active channels.

bool data_channel_enabled (const int channel_id)
 Is a channel subscribed?

int data_channel_flag (const int channel_id, const bool subscribe)
 Mark a channel as subscribed or unsubscribed.

double data_generate (const int index, const double sin_period)
 Data generator. Sine wave of sorts.

void sighandler (int signal)
 Control-break handler. Sets global flag to indicate exit request.

void * daq_thread_main (void *arg)
 Data handling thread.

int daq_do_work (const int control_socket, const int data_socket)
 Main worker routine: read / parse / respond.

void daq_main_loop (const int daq_port)
 Open network socket, loop doing work until control-c.

int main (int argc, char *argv[])
 Init the network, talk to all.


Variables

const int DAQ_PORT = 55055
 Default TCP port to listen on.

const int TCP_Q_LEN = 1
 TCP queue length, number of queued connections allowed.

bool control_break
 Global boolean set by signal handler to force exit.

int sample_rate
 Fake data sample rate.

bool streaming_active
 Global boolean for IPC between threads.

const int SINE_PERIOD = 100
 Length (period) of the sine wave we generate.

struct {
   bool   active [NUM_CHANNELS]
chan_struct
 Shared structure for subscribed data channels.


Detailed Description

A fake DAQ program for testing NSDS et al.

Note:
Requires the flog messaging library.
This has gotten a bit more useful. Currently streams NUM_CHANNELS fake data channels of sinusoidal data at 20Hz with a period of 100 samples. Threaded, with data on the second thread. If you replace the data_generate routine, this would make a decent skeleton DAQ program. Data rate is selectable on the command line.

Robust against most network failures - knows to restart, etc. Simple signal handler a la the driver as well.

Note that this uses integer channel IDs, where LabVIEW often has multicharacter descriptive strings like "RoomTempCelsius". That makes this code much much simpler, and for this application simplicity wins. For more useful testing, you should get labview and its fake DAQ (or real!) code working anyway.

If you're using this as skeleton code for another DAQ system, be aware than you can name a DAQ channel as any printable ASCII string. It does complicate the parsing of open-port and close-port requests, though.

Date:
9/5/02
Author:
Paul Hubbard

Definition in file fake_daq.c.


Function Documentation

int daq_do_work const int    control_socket,
const int    data_socket
 

Main worker routine: read / parse / respond.

Date:
9/5/02
Author:
Paul Hubbard
Return values:
0  OK, != 0 means error
Parameters:
control_socket  FD of control connection
data_socket  FD of data connection Read commands, respond as required. Starts data thread if necessary.

Definition at line 439 of file fake_daq.c.

References daq_thread_main(), data_active_count(), data_channel_flag(), MAX_CMD_LEN, NUM_CHANNELS, pthread_setconcurrency(), streaming_active, tcp_nl_read(), and tcp_nl_write().

Referenced by daq_main_loop().

void daq_main_loop const int    daq_port
 

Open network socket, loop doing work until control-c.

Date:
9/5/02
Note:
Opens server socket
This handles the dropped connections and other TCP errors, mostly by restarting the connects. Also kills data thread if connections die.

Definition at line 684 of file fake_daq.c.

References control_break, daq_do_work(), streaming_active, tcp_close(), tcp_peername(), TCP_Q_LEN, and tcp_socket_make().

Referenced by main().

void* daq_thread_main void *    arg
 

Data handling thread.

Transient thread to send data out the data channel. Dead simple.

Parameters:
arg  Ptr to file descriptor to use for socket
Return values:
Null  always

Definition at line 294 of file fake_daq.c.

References control_break, data_active_count(), data_channel_enabled(), data_generate(), DATUM_LEN, gen_timestamp(), NUM_CHANNELS, sample_rate, SINE_PERIOD, streaming_active, tcp_nl_write(), and TSTAMP_LEN.

Referenced by daq_do_work().

int data_active_count void   
 

Return number of active channels.

Return values:
Number  active, <0 if error
Date:
9/27/02

Definition at line 85 of file fake_daq.c.

References chan_struct.

Referenced by daq_do_work(), and daq_thread_main().

bool data_channel_enabled const int    channel_id
 

Is a channel subscribed?

Locks mutex and checks array to see if a channel is subscribed.

Parameters:
channel_id  Channel ID to check
Return values:
True  or false, false if error
Note:
uses mutex lock on chan_struct
Date:
9/24/02

Definition at line 122 of file fake_daq.c.

References chan_struct, and NUM_CHANNELS.

Referenced by daq_thread_main().

int data_channel_flag const int    channel_id,
const bool    subscribe
 

Mark a channel as subscribed or unsubscribed.

Lock the mutex, set the bit, report if it changed.

Note:
Idempotent - call multiple times w/no harm done.
Parameters:
channel_id  Channel ID in question
subscribe  if true, subscribe, if false un-sub
Return values:
0  if OK, non-zero if error
Note:
Uses chan_struct and mutex therein
Date:
9/24/02

Definition at line 168 of file fake_daq.c.

References chan_struct, and NUM_CHANNELS.

Referenced by daq_do_work().

double data_generate const int    index,
const double    sin_period
 

Data generator. Sine wave of sorts.

Date:
9/16/02
Return values:
sin  (x), scaled to period
Parameters:
index  0 to N-1
sin_period  Period of function, cannot be zero!

Definition at line 250 of file fake_daq.c.

Referenced by daq_thread_main().

int main int    argc,
char *    argv[]
 

Init the network, talk to all.

Sets up messaging, installs signal handler, calls worker routine.

Note:
Driver port passed on command line
Ditto w/sample rate
Parameters:
argc  Argc as passed by shell
argv  Argv as passed by shell
Returns:
0 All is good Non-zero All is not good

Definition at line 771 of file fake_daq.c.

References control_break, daq_main_loop(), DAQ_PORT, NUM_CHANNELS, sample_rate, and sighandler().

void sighandler int    signal
 

Control-break handler. Sets global flag to indicate exit request.

Note:
Assumes installed in interrupt chain by someone else
Parameters:
signal  Signal number
Date:
3/25/02

Definition at line 275 of file fake_daq.c.

References control_break.

Referenced by main().


Variable Documentation

bool active[NUM_CHANNELS]
 

R/W mutex for struct.

Definition at line 75 of file fake_daq.c.

struct { ... } chan_struct
 

Shared structure for subscribed data channels.

Referenced by data_active_count(), data_channel_enabled(), and data_channel_flag().

bool control_break
 

Global boolean set by signal handler to force exit.

Definition at line 60 of file fake_daq.c.

Referenced by daq_main_loop(), daq_thread_main(), main(), and sighandler().

const int DAQ_PORT = 55055
 

Default TCP port to listen on.

Definition at line 54 of file fake_daq.c.

Referenced by main().

int sample_rate
 

Fake data sample rate.

Definition at line 63 of file fake_daq.c.

Referenced by daq_thread_main(), and main().

const int SINE_PERIOD = 100
 

Length (period) of the sine wave we generate.

Definition at line 69 of file fake_daq.c.

Referenced by daq_thread_main().

bool streaming_active
 

Global boolean for IPC between threads.

Definition at line 66 of file fake_daq.c.

Referenced by daq_do_work(), daq_main_loop(), and daq_thread_main().

const int TCP_Q_LEN = 1
 

TCP queue length, number of queued connections allowed.

Definition at line 57 of file fake_daq.c.

Referenced by daq_main_loop().


Generated on Fri Dec 6 14:33:16 2002 for NSDS Driver by doxygen1.2.18