Changeset 5208

Show
Ignore:
Timestamp:
04/24/08 14:40:24 (7 months ago)
Author:
khali
Message:

Move the functionality checks to a separate function, as is done in
i2cget already.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • i2c-tools/trunk/tools/i2cset.c

    r5207 r5208  
    4646} 
    4747 
     48static int check_funcs(int file, int i2cbus, int size, int pec) 
     49{ 
     50        unsigned long funcs; 
     51 
     52        /* check adapter functionality */ 
     53        if (ioctl(file, I2C_FUNCS, &funcs) < 0) { 
     54                fprintf(stderr, "Error: Could not get the adapter " 
     55                        "functionality matrix: %s\n", strerror(errno)); 
     56                return -1; 
     57        } 
     58 
     59        switch (size) { 
     60        case I2C_SMBUS_BYTE_DATA: 
     61                if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { 
     62                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     63                                "not have byte write capability\n", i2cbus); 
     64                        return -1; 
     65                } 
     66                break; 
     67 
     68        case I2C_SMBUS_WORD_DATA: 
     69                if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { 
     70                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     71                                "not have word write capability\n", i2cbus); 
     72                        return -1; 
     73                } 
     74                break; 
     75        } 
     76 
     77        if (pec 
     78         && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) { 
     79                fprintf(stderr, "Warning: Adapter for i2c bus %d does " 
     80                        "not seem to support PEC\n", i2cbus); 
     81        } 
     82 
     83        return 0; 
     84} 
     85 
    4886int main(int argc, char *argv[]) 
    4987{ 
     
    5290        int value, daddress, vmask = 0; 
    5391        char filename[20]; 
    54         unsigned long funcs; 
    5592        int pec = 0; 
    5693        int flags = 0; 
     
    129166 
    130167        file = open_i2c_dev(i2cbus, filename, 0); 
    131         if (file < 0) { 
    132                 exit(1); 
    133         } 
    134  
    135         /* check adapter functionality */ 
    136         if (ioctl(file, I2C_FUNCS, &funcs) < 0) { 
    137                 fprintf(stderr, "Error: Could not get the adapter " 
    138                         "functionality matrix: %s\n", strerror(errno)); 
    139                 exit(1); 
    140         } 
    141  
    142         switch (size) { 
    143         case I2C_SMBUS_BYTE_DATA: 
    144                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { 
    145                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    146                                 "not have byte write capability\n", i2cbus); 
    147                         exit(1); 
    148                 } 
    149                 break; 
    150  
    151         case I2C_SMBUS_WORD_DATA: 
    152                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { 
    153                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    154                                 "not have word write capability\n", i2cbus); 
    155                         exit(1); 
    156                 } 
    157                 break; 
    158         } 
    159  
    160         if (set_slave_addr(file, address, force) < 0) 
     168        if (file < 0 
     169         || check_funcs(file, i2cbus, size, pec) 
     170         || set_slave_addr(file, address, force)) 
    161171                exit(1); 
    162172 
     
    225235        } 
    226236 
    227         if (pec) { 
    228                 if (ioctl(file, I2C_PEC, 1) < 0) { 
    229                         fprintf(stderr, "Error: Could not set PEC: %s\n", 
    230                                 strerror(errno)); 
    231                         close(file); 
    232                         exit(1); 
    233                 } 
    234                 if (!(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) { 
    235                         fprintf(stderr, "Warning: Adapter for i2c bus %d does " 
    236                                 "not seem to actually support PEC\n", i2cbus); 
    237                 } 
     237        if (pec && ioctl(file, I2C_PEC, 1) < 0) { 
     238                fprintf(stderr, "Error: Could not set PEC: %s\n", 
     239                        strerror(errno)); 
     240                close(file); 
     241                exit(1); 
    238242        } 
    239243