From 816738fff33ffe3d0654be755c25626b3f4d2772 Mon Sep 17 00:00:00 2001 From: Gergő J. Miklós Date: Sat, 17 Oct 2020 05:38:18 +0200 Subject: i2sensors: error handling --- src/main.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 3ba3da3..b9eb4d0 100644 --- a/src/main.c +++ b/src/main.c @@ -91,21 +91,23 @@ __uint8_t *xchg_data (__uint8_t *buf, __uint16_t wrlen, __uint16_t waitlen, _ if( funcs & I2C_FUNC_I2C ){ #endif if(write(devicef, buf, 1) != 1){ //# write one byte to device - fprintf (stderr, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + fprintf (stdout, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); } usleep(waitlen*1000); //# Wait 10ms for reading if(read(devicef, buf, rdlen) != rdlen) { //# read the result - fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + fprintf (stdout, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + exit(EXIT_FAILURE); } #ifdef WITH_I2C_SMBUS } else if ( funcs && I2C_FUNC_SMBUS_WORD_DATA ){ if(i2c_smbus_write_byte(devicef, buf[0] ) < 0){ //# write one byte to device - fprintf (stderr, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + fprintf (stdout, "I2C write error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); } usleep(waitlen*1000); //# Wait 10ms for reading if((data = i2c_smbus_read_word_data(devicef, buf[0])) < 0) { //# read the result - fprintf (stderr, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + fprintf (stdout, "I2C read error: [NO: %d, MSG: %s] at %s, line %d. (function: %s)\n", errno, strerror(errno), __FILE__, __LINE__, __func__); + exit(EXIT_FAILURE); } else { buf[0] = data & 0x0FF; buf[1] = (data >> 8) & 0x0FF; @@ -158,7 +160,7 @@ int main( int argc, char *argv[] ){ } if (ioctl(devicef, I2C_FUNCS, &funcs) < 0) { //Get i2c device features - fprintf (stderr, "\n\tAdapter function error [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__); + fprintf (stdout, "\n\tAdapter function error [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__); exit(EXIT_FAILURE); } @@ -170,7 +172,7 @@ int main( int argc, char *argv[] ){ if (ioctl(devicef, I2C_SLAVE_FORCE, addr) < 0){ //# Seeking/Opening the device on the Bus //printf("\n\tCan't open i2c DEVICE, errno: %d\n\n",errno); - fprintf (stderr, "\n\tCan't open i2c DEVICE! [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__); + fprintf (stdout, "\n\tCan't open i2c DEVICE! [ERR: %d, MSG: %s] at %s, line %d. \n", errno, strerror(errno), __FILE__, __LINE__); exit(EXIT_FAILURE); } } else { -- cgit v1.2.3