/* test.c Copyright (C) 2011 Pali Rohár This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #define __u8 unsigned char #define __u16 unsigned short int /* * Data for SMBus Messages */ #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */ union i2c_smbus_data { __u8 byte; __u16 word; __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */ /* and one more for PEC */ }; /* This is the structure as used in the I2C_SMBUS ioctl call */ struct i2c_smbus_ioctl_data { char read_write; __u8 command; int size; union i2c_smbus_data *data; }; int main() { int fd, ret; struct i2c_smbus_ioctl_data ioctl_data; union i2c_smbus_data data; printf("\ncall open(\"/dev/i2c-2\", %d);\n", O_RDWR); fd = open("/dev/i2c-2", O_RDWR); printf("fd=%d\n", fd); printf("\ncall ioctl(%d, 0x705, 0x55);\n", fd); ret = ioctl(fd, 0x705, 0x55); printf("ret=%d\n", ret); printf("\ncall ioctl(%d, 0x703, 0x55);\n", fd); ret = ioctl(fd, 0x703, 0x55); printf("ret=%d\n", ret); ioctl_data.read_write = 1; ioctl_data.command = 0x0B; /* Capacity in % */ ioctl_data.size = 2 /* I2C_SMBUS_BYTE_DATA */; ioctl_data.data = &data; printf("\ncall ioctl(%d, 0x720, %p);\n", fd, &ioctl_data); ret = ioctl(fd, 0x720, &ioctl_data); printf("ret=%d\n", ret); printf("\ncall close(%d);\n", fd); ret = close(fd); printf("ret=%d\n", ret); printf("value=%d\n", ioctl_data.data->byte); return 0; }