Browse Source

Refactor the handling of the I2CBUS parameter. This ensures more

consistency accross the i2c tools.


git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@5193 7894878c-1315-0410-8ee3-d5d059ff63e0
tags/v3.0.2
Jean Delvare 18 years ago
parent
commit
eda5918d0f
  1. 24
      tools/i2cbusses.c
  2. 1
      tools/i2cbusses.h
  3. 11
      tools/i2cdetect.c
  4. 10
      tools/i2cdump.c
  5. 6
      tools/i2cget.c
  6. 6
      tools/i2cset.c

24
tools/i2cbusses.c

@ -25,6 +25,7 @@
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
@ -217,6 +218,29 @@ done:
"and also modprobed your i2c bus drivers\n");
}
/*
* Parse an I2CBUS command line argument and return the corresponding
* bus number, or a negative value if the bus is invalid.
*/
int lookup_i2c_bus(const char *i2cbus_arg)
{
long i2cbus;
char *end;
i2cbus = strtol(i2cbus_arg, &end, 0);
if (*end || !*i2cbus_arg) {
fprintf(stderr, "Error: I2CBUS argument not a number!\n");
return -1;
}
if (i2cbus < 0 || i2cbus > 0xff) {
fprintf(stderr, "Error: I2CBUS argument out of range "
"(0-255)!\n");
return -2;
}
return i2cbus;
}
int open_i2c_dev(const int i2cbus, char *filename, const int quiet)
{
int file;

1
tools/i2cbusses.h

@ -24,6 +24,7 @@
void print_i2c_busses(int procfmt);
int lookup_i2c_bus(const char *i2cbus_arg);
int open_i2c_dev(const int i2cbus, char *filename, const int quiet);
int set_slave_addr(int file, int address, int force);

11
tools/i2cdetect.c

@ -235,15 +235,8 @@ int main(int argc, char *argv[])
help();
exit(1);
}
i2cbus = strtol(argv[flags+1], &end, 0);
if (*end) {
fprintf(stderr, "Error: I2CBUS argument not a number!\n");
help();
exit(1);
}
if ((i2cbus < 0) || (i2cbus > 0xff)) {
fprintf(stderr, "Error: I2CBUS argument out of range "
"(0-255)!\n");
i2cbus = lookup_i2c_bus(argv[flags+1]);
if (i2cbus < 0) {
help();
exit(1);
}

10
tools/i2cdump.c

@ -91,14 +91,8 @@ int main(int argc, char *argv[])
help();
exit(1);
}
i2cbus = strtol(argv[flags+1], &end, 0);
if (*end) {
fprintf(stderr, "Error: First argument not a number!\n");
help();
exit(1);
}
if (i2cbus < 0 || i2cbus > 0xff) {
fprintf(stderr, "Error: I2CBUS argument out of range!\n");
i2cbus = lookup_i2c_bus(argv[flags+1]);
if (i2cbus < 0) {
help();
exit(1);
}

6
tools/i2cget.c

@ -187,11 +187,9 @@ int main(int argc, char *argv[])
if (argc - flags < 3)
help();
i2cbus = strtol(argv[flags+1], &end, 0);
if (*end || i2cbus < 0 || i2cbus > 0x3f) {
fprintf(stderr, "Error: I2CBUS argument invalid!\n");
i2cbus = lookup_i2c_bus(argv[flags+1]);
if (i2cbus < 0)
help();
}
address = strtol(argv[flags+2], &end, 0);
if (*end || address < 3 || address > 0x77) {

6
tools/i2cset.c

@ -78,11 +78,9 @@ int main(int argc, char *argv[])
if (argc < flags + 5)
help();
i2cbus = strtol(argv[flags+1], &end, 0);
if (*end || i2cbus < 0 || i2cbus > 0x3f) {
fprintf(stderr, "Error: I2CBUS argument invalid!\n");
i2cbus = lookup_i2c_bus(argv[flags+1]);
if (i2cbus < 0)
help();
}
address = strtol(argv[flags+2], &end, 0);
if (*end || address < 0 || address > 0x7f) {

Loading…
Cancel
Save