some tests with lint

This commit is contained in:
Wolfgang Hottgenroth 2020-09-04 17:17:47 +01:00
parent d4ce143298
commit a6ac9183cc

View File

@ -32,16 +32,15 @@ typedef enum {
} t_state; } t_state;
int serialFd; static bool verbose = false;
bool verbose = false; static bool loopActiveFlag = false;
bool loopActiveFlag = false;
void msleep(uint32_t t) { static void msleep(uint32_t t) {
usleep(t * 1000); usleep((useconds_t)(t * 1000));
} }
void infolog(const char *format, ...) { static void infolog(const char *format, ...) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
if (verbose) { if (verbose) {
@ -50,7 +49,7 @@ void infolog(const char *format, ...) {
va_end(ap); va_end(ap);
} }
void errlog(const char *format, ...) { static void errlog(const char *format, ...) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vfprintf(stderr, format, ap); vfprintf(stderr, format, ap);
@ -58,7 +57,7 @@ void errlog(const char *format, ...) {
} }
void ledRed(bool v) { static void ledRed(bool v) {
if (v) { if (v) {
digitalWrite(LED_RED, HIGH); digitalWrite(LED_RED, HIGH);
} else { } else {
@ -66,21 +65,22 @@ void ledRed(bool v) {
} }
} }
void ledGreen(bool v) { static void ledGreen(bool v) {
if (v) { if (v) {
digitalWrite(LED_GREEN, HIGH); digitalWrite(LED_GREEN, HIGH);
} else { } else {
digitalWrite(LED_GREEN, LOW); digitalWrite(LED_GREEN, LOW);
} }
} }
void frontendReset() {
static void frontendReset() {
digitalWrite(FRONTEND_RESET, LOW); digitalWrite(FRONTEND_RESET, LOW);
msleep(25); msleep(25);
digitalWrite(FRONTEND_RESET, HIGH); digitalWrite(FRONTEND_RESET, HIGH);
msleep(100); msleep(100);
} }
void loopControl(bool v) { static void loopControl(bool v) {
if (v) { if (v) {
digitalWrite(LOOP_ENABLE, HIGH); digitalWrite(LOOP_ENABLE, HIGH);
msleep(5); msleep(5);
@ -91,15 +91,15 @@ void loopControl(bool v) {
} }
} }
void frontendSample() { static void frontendSample() {
digitalWrite(FRONTEND_SAMPLE_HOLD, LOW); digitalWrite(FRONTEND_SAMPLE_HOLD, LOW);
} }
void frontendHold() { static void frontendHold() {
digitalWrite(FRONTEND_SAMPLE_HOLD, HIGH); digitalWrite(FRONTEND_SAMPLE_HOLD, HIGH);
} }
void loopStatusISR() { static void loopStatusISR() {
loopActiveFlag = digitalRead(LOOP_STATUS) == LOW; loopActiveFlag = digitalRead(LOOP_STATUS) == LOW;
if (! loopActiveFlag) { if (! loopActiveFlag) {
ledRed(true); ledRed(true);
@ -107,23 +107,22 @@ void loopStatusISR() {
} }
void myExit(int e) { static void deinit() {
ledRed(false); ledRed(false);
ledGreen(false); ledGreen(false);
loopControl(false); loopControl(false);
frontendSample(); frontendSample();
exit(e);
} }
void termHandler(int signum) static void termHandler(/*@unused@*/ int signum)
{ {
infolog("Termination requested via signal\n"); infolog("Termination requested via signal\n");
myExit(0); deinit();
exit(EXIT_SUCCESS);
} }
void init() { static void init() {
infolog("Register termination handler\n"); infolog("Register termination handler\n");
signal(SIGTERM, termHandler); signal(SIGTERM, termHandler);
signal(SIGINT, termHandler); signal(SIGINT, termHandler);
@ -160,7 +159,7 @@ void init() {
loopControl(false); loopControl(false);
} }
int openSerial(char *serialDevice, uint32_t speedNum) { static int openSerial(char *serialDevice, uint32_t speedNum) {
int fd = open(serialDevice, O_RDWR | O_NOCTTY | O_SYNC); int fd = open(serialDevice, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) { if (fd < 0) {
errlog("error %d opening serial device %s: %s\n", errlog("error %d opening serial device %s: %s\n",
@ -222,12 +221,12 @@ int openSerial(char *serialDevice, uint32_t speedNum) {
return fd; return fd;
} }
void closeSerial(int fd) { static void closeSerial(int fd) {
loopControl(false); loopControl(false);
close(fd); close(fd);
} }
t_longframe *request(int fd, uint8_t cmd, uint8_t addr) { static /*@null@*/ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
errno = 0; errno = 0;
t_longframe *frame = (t_longframe*) malloc(sizeof(t_longframe)); t_longframe *frame = (t_longframe*) malloc(sizeof(t_longframe));
@ -235,7 +234,7 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
errlog("unable to allocate memory for frame\n"); errlog("unable to allocate memory for frame\n");
return NULL; return NULL;
} }
frame->userdata = NULL; memset(frame, 0, sizeof(t_longframe));
uint8_t chksum = cmd + addr; uint8_t chksum = cmd + addr;
@ -249,15 +248,20 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
sendBuf[4] = 0x16; sendBuf[4] = 0x16;
write(fd, sendBuf, 5); write(fd, sendBuf, 5);
while (1) { while (true) {
int r; int r = 0;
if (ioctl(fd, TIOCSERGETLSR, &r) == -1) { if (ioctl(fd, TIOCSERGETLSR, &r) == -1) {
errlog("error %d getting TIOCSERGETLSR for fd %d: %s\n", errlog("error %d getting TIOCSERGETLSR for fd %d: %s\n",
errno, fd, strerror(errno)); errno, fd, strerror(errno));
errno = ERROR_APP_SPECIFIC_ERROR_FLAG | ERROR_TX_REG_UNACCESSIBLE; errno = ERROR_APP_SPECIFIC_ERROR_FLAG | ERROR_TX_REG_UNACCESSIBLE;
if (frame->userdata) {
free(frame->userdata);
}
free(frame);
frame = NULL;
return NULL; return NULL;
} }
if (r & TIOCSER_TEMT) { if ((r & TIOCSER_TEMT) != 0) {
break; break;
} }
} }
@ -300,6 +304,9 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
state = e_ERROR; state = e_ERROR;
} else { } else {
frame->length1 = c; frame->length1 = c;
if (frame->userdata) {
free(frame->userdata);
}
frame->userdata = (uint8_t*) malloc(frame->length1 - 3); frame->userdata = (uint8_t*) malloc(frame->length1 - 3);
if (! frame->userdata) { if (! frame->userdata) {
errlog("unable to allocate memory for userdata\n"); errlog("unable to allocate memory for userdata\n");
@ -391,13 +398,14 @@ t_longframe *request(int fd, uint8_t cmd, uint8_t addr) {
return frame; return frame;
} }
void printFrame(bool hexOut, t_longframe *frame) { static void printFrame(bool hexOut, t_longframe *frame) {
if (hexOut) { if (hexOut) {
fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x\n", fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x\n",
frame->start1, frame->length1, frame->length2, frame->start2, frame->start1, frame->length1, frame->length2, frame->start2,
frame->c, frame->a, frame->ci); frame->c, frame->a, frame->ci);
for (uint8_t i = 0; i < (frame->length1 - 3); i++) { uint8_t i = 0;
if (i && !(i % 16)) { for (i = 0; i < (frame->length1 - 3); i++) {
if ((i != 0) && ((i % 16) == 0)) {
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
fprintf(stderr, "%02x ", frame->userdata[i]); fprintf(stderr, "%02x ", frame->userdata[i]);
@ -409,7 +417,8 @@ void printFrame(bool hexOut, t_longframe *frame) {
fprintf(stdout, "%c%c%c%c%c%c%c", fprintf(stdout, "%c%c%c%c%c%c%c",
frame->start1, frame->length1, frame->length2, frame->start2, frame->start1, frame->length1, frame->length2, frame->start2,
frame->c, frame->a, frame->ci); frame->c, frame->a, frame->ci);
for (uint8_t i = 0; i < (frame->length1 - 3); i++) { uint8_t i = 0;
for (i = 0; i < (frame->length1 - 3); i++) {
fprintf(stdout, "%c", frame->userdata[i]); fprintf(stdout, "%c", frame->userdata[i]);
} }
fprintf(stdout, "%c%c", frame->chksum, frame->stop); fprintf(stdout, "%c%c", frame->chksum, frame->stop);
@ -470,11 +479,12 @@ int main(int argc, char *argv[]) {
int fd = openSerial(DEFAULT_SERIAL_DEVICE, 2400); int fd = openSerial(DEFAULT_SERIAL_DEVICE, 2400);
if (fd == -1) { if (fd == -1) {
errlog("unable to open device, fatal error\n"); errlog("unable to open device, fatal error\n");
myExit(-1); deinit();
exit(EXIT_FAILURE);
} }
while (1) { while (true) {
if (! loopActiveFlag) { if (! loopActiveFlag) {
errlog("loop is not active, enable it and delay\n"); errlog("loop is not active, enable it and delay\n");
loopControl(true); loopControl(true);
@ -531,7 +541,8 @@ int main(int argc, char *argv[]) {
infolog("closing device\n"); infolog("closing device\n");
closeSerial(fd); closeSerial(fd);
myExit(exitCode); deinit();
return exitCode;
} }