howard/howard.h

138 lines
3.7 KiB
C

// booleans
#define TRUE (-1)
#define FALSE (0)
#define BADFD (-1)
// buffer lengths
#define BUFLEN 8196
#define BUFLENSMALL 255
#define BUFLENTINY 32
// Benestar sound meter
#define MIC_VID "64bd"
#define MIC_PID "74e3"
// mic constants
#define FLAG_DBCMODE (0b00010000) // use dBc (not dBa)
#define FLAG_MAXMODE (0b00100000) // get max db over interval
// fast = 125ms exp time avg
// slow = 1000ms exp time avg
#define FLAG_FASTMODE (0b01000000)
// ranges supported by sonometer
#define FLAG_RANGE_30_130 (0b00000000)
#define FLAG_RANGE_30_80 (0b00000001)
#define FLAG_RANGE_50_100 (0b00000010)
#define FLAG_RANGE_60_110 (0b00000011)
#define FLAG_RANGE_80_130 (0b00000100)
// ANSI stuff
char BOLD[BUFLENTINY];
char ITALIC[BUFLENTINY];
char STRIKE[BUFLENTINY];
char PLAIN[BUFLENTINY];
char UNDERLINE[BUFLENTINY];
char RED[BUFLENTINY];
char MAGENTA[BUFLENTINY];
char GREEN[BUFLENTINY];
char YELLOW[BUFLENTINY];
char BLUE[BUFLENTINY];
char CYAN[BUFLENTINY];
char GREY[BUFLENTINY];
// Output modes
#define OM_SHORT (1) // Print to stdout in one line
#define OM_LONG (2) // Print to stdout over multiple lines
#define OM_DB (4) // Write to influxdb
// Control strings
#define CMD_CAPTURE (0xb3) // take a reading
#define CMD_CONFIGURE (0x56) // apply settings
#define RES_ACK (0xc4) // ack for command
// what to do
enum run_mode {
M_PROBE, // measure noise level
M_TEST, // just check for sononeter then exit
M_TESTDB // just check influxdb access then exit
};
// how to measure sound
enum math_mode {
M_MAX,
M_AVG
};
// where to log
#define LOGFILE "/var/log/howard.log"
#define PIDFILE "/var/run/howard.pid"
// return codes for read_single_result()
enum read_status {
E_NOERROR=0, // all good
E_TIMEOUT=-1, // didn't receive all data in time
E_BADREAD=-2 // received too much or too little data
};
typedef struct {
int dirty;
time_t unixtime;
uint8_t flags;
double decibels_last;
double decibels_max;
double decibels_avg;
double decibels_tot;
int count;
int rangeidx;
char range[BUFLENSMALL];
char dbunits[BUFLENSMALL];
char checkmode[BUFLENSMALL];
char timenice[BUFLENSMALL];
} result_t;
enum influxcmdtype { I_READ, I_WRITE, I_PING };
// prototypes
int add_output_mode(char *m);
char *append(char *orig, char *new, int maxlen);
void cleanup(void);
void clear_results(result_t *data);
void colprintf( char *prefix, const char *col, char* format, va_list *args );
int configure_mic(void);
int daemonize(void);
struct hostent *dnslookup(char *hname);
void err( char* format, ... );
double extract_decibels(uint8_t *buf);
void generate_commands(void);
char *getprocfspath(int vid, int pid, char *retvar);
void handle_signal(int signum);
int hextoint(char *hex);
int hook_exits(void);
int influx_cmd(enum influxcmdtype cmdtype, char *cmd, char *retbuf);
int influx_httppost(char *hname, int port, char *header, char *body, char *retbuf);
int influx_init(char *hname, char *db, char *user, char *pass);
int influx_insert(char *cmd, char *retbuf);
int influx_ping(char *retbuf);
int influx_query(char *cmd, char *retbuf);
int influx_write_decibels(double decibels);
void info( char* format, ... );
int init_influxdb(void);
int init_mic(void);
int init_output(void);
int mic_docmd(hid_device *dev, uint8_t *cmd, int cmdlen, uint8_t *buf);
int output_results(result_t *data);
int parse_results(uint8_t *buf, result_t *data);
void pr(char *col, char* format, ... );
enum read_status read_single_result(hid_device *dev, uint8_t *retbuf);
void reopen_logfile(int signum);
int resetusb(char *path);
void set_ansi(int wantansi);
int tcpconnect(char *hname, int port);
void usage(void);
int validateargs(void);
void vinfo( char* format, ... );
void warn( char* format, ... );