#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. |
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.
Definition in file fake_daq.c.
|
Main worker routine: read / parse / respond.
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(). |
|
Open network socket, loop doing work until control-c.
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(). |
|
Data handling thread. Transient thread to send data out the data channel. Dead simple.
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(). |
|
Return number of active channels.
Definition at line 85 of file fake_daq.c. References chan_struct. Referenced by daq_do_work(), and daq_thread_main(). |
|
Is a channel subscribed? Locks mutex and checks array to see if a channel is subscribed.
Definition at line 122 of file fake_daq.c. References chan_struct, and NUM_CHANNELS. Referenced by daq_thread_main(). |
|
Mark a channel as subscribed or unsubscribed. Lock the mutex, set the bit, report if it changed.
Definition at line 168 of file fake_daq.c. References chan_struct, and NUM_CHANNELS. Referenced by daq_do_work(). |
|
Data generator. Sine wave of sorts.
Definition at line 250 of file fake_daq.c. Referenced by daq_thread_main(). |
|
Init the network, talk to all. Sets up messaging, installs signal handler, calls worker routine.
Definition at line 771 of file fake_daq.c. References control_break, daq_main_loop(), DAQ_PORT, NUM_CHANNELS, sample_rate, and sighandler(). |
|
Control-break handler. Sets global flag to indicate exit request.
Definition at line 275 of file fake_daq.c. References control_break. Referenced by main(). |
|
R/W mutex for struct.
Definition at line 75 of file fake_daq.c. |
|
Shared structure for subscribed data channels.
Referenced by data_active_count(), data_channel_enabled(), and data_channel_flag(). |
|
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(). |
|
Default TCP port to listen on.
Definition at line 54 of file fake_daq.c. Referenced by main(). |
|
Fake data sample rate.
Definition at line 63 of file fake_daq.c. Referenced by daq_thread_main(), and main(). |
|
Length (period) of the sine wave we generate.
Definition at line 69 of file fake_daq.c. Referenced by daq_thread_main(). |
|
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(). |
|
TCP queue length, number of queued connections allowed.
Definition at line 57 of file fake_daq.c. Referenced by daq_main_loop(). |