2022-05-30 22:46:58 +10:00
|
|
|
// booleans
|
|
|
|
#define TRUE (-1)
|
|
|
|
#define FALSE (0)
|
|
|
|
#define BADFD (-1)
|
|
|
|
|
|
|
|
// buffer lengths
|
|
|
|
#define BUFLEN 8196
|
|
|
|
#define BUFLENSMALL 255
|
|
|
|
|
|
|
|
// 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
|
|
|
|
#define BOLD "\x1b[1m"
|
|
|
|
#define ITALIC "\x1b[3m"
|
|
|
|
#define STRIKE "\x1b[9m"
|
|
|
|
#define PLAIN "\x1b[0m"
|
|
|
|
#define UNDERLINE "\x1b[4m"
|
|
|
|
#define RED "\x1b[31m"
|
|
|
|
#define MAGENTA "\x1b[35m"
|
|
|
|
#define GREEN "\x1b[32m"
|
|
|
|
#define YELLOW "\x1b[33m"
|
|
|
|
#define BLUE "\x1b[34m"
|
|
|
|
#define CYAN "\x1b[36m"
|
|
|
|
#define GREY "\x1b[2;37m"
|
|
|
|
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
int rangeidx;
|
|
|
|
char range[BUFLENSMALL];
|
|
|
|
char dbunits[BUFLENSMALL];
|
|
|
|
char checkmode[BUFLENSMALL];
|
|
|
|
char timenice[BUFLENSMALL];
|
|
|
|
} result_t;
|
2021-11-28 18:42:03 +11:00
|
|
|
enum influxcmdtype { I_READ, I_WRITE, I_PING };
|
|
|
|
|
2022-05-30 22:46:58 +10:00
|
|
|
|
|
|
|
// prototypes
|
|
|
|
int add_output_mode(char *m);
|
2022-05-29 22:28:22 +10:00
|
|
|
char *append(char *orig, char *new, int maxlen);
|
|
|
|
void cleanup(void);
|
2022-05-30 22:46:58 +10:00
|
|
|
void clear_results(result_t *data);
|
|
|
|
void colprintf( char *prefix, const char *col, char* format, va_list *args );
|
|
|
|
int configure_mic(void);
|
|
|
|
struct hostent *dnslookup(char *hname);
|
2021-11-27 16:58:52 +11:00
|
|
|
void err( char* format, ... );
|
2022-05-30 22:46:58 +10:00
|
|
|
double extract_decibels(uint8_t *buf);
|
|
|
|
void generate_commands(void);
|
|
|
|
char *getprocfspath(int vid, int pid, char *retvar);
|
|
|
|
void handle_signal(int signum);
|
2021-11-27 16:58:52 +11:00
|
|
|
int hextoint(char *hex);
|
2021-11-28 18:42:03 +11:00
|
|
|
int influx_cmd(enum influxcmdtype cmdtype, char *cmd, char *retbuf);
|
2022-05-30 22:46:58 +10:00
|
|
|
int influx_httppost(char *hname, int port, char *header, char *body, char *retbuf);
|
|
|
|
int influx_init(char *hname, char *db, char *user, char *pass);
|
2021-11-28 18:42:03 +11:00
|
|
|
int influx_insert(char *cmd, char *retbuf);
|
|
|
|
int influx_ping(char *retbuf);
|
2022-05-30 22:46:58 +10:00
|
|
|
int influx_query(char *cmd, char *retbuf);
|
2021-11-28 18:42:03 +11:00
|
|
|
int influx_write_decibels(double decibels);
|
2022-05-30 22:46:58 +10:00
|
|
|
void info( char* format, ... );
|
|
|
|
int init_influxdb(void);
|
|
|
|
int init_mic(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);
|
|
|
|
int resetusb(char *path);
|
|
|
|
int tcpconnect(char *hname, int port);
|
|
|
|
void usage(void);
|
|
|
|
int validateargs(void);
|
|
|
|
void vinfo( char* format, ... );
|
|
|
|
void warn( char* format, ... );
|