Hello!
New to cpputest here. I am attempting to write a simple I2C driver for the Beaglebone black, and I hope to use CppUTest in this project to ensure my device-specific drivers do not fall victim to bugs in my underlying I2C implementation.
My question is, does anyone have any hints for me to mock out linux system calls? This driver will require ioctl(), open(), read(), and write(). Ideally I'd like to mock these out, as I am cross-compiling on my dev system (does not have the I2C bus file handle), for a system that has the I2C file handle. It looks like I would have to use the mock_c() functions, but there isn't very much documentation on how to use them. Additionally ioctl() has an unsigned long type, which means I would have to write a custom comparator as far as I understand.
Alternatively, I could also mock out the functions in <linux/i2c_dev.h> which allow for more complex sequences of reads and writes, but I figured it would be best to start with the simple.
A sample i2c transaction would be as follows, to give you guys a better idea of the types of things I'd have to unit test:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
// Open the i2c bus file descriptor
int g_i2cFile;
if((g_i2cFile = open(“dev/i2c-3″, O_RDWR)) < 0){
// ERROR HANDLING
perror(“Failed to open the i2c bus”);
return -1;
}
// Set the device address on the bus
int address = 0×19;
if (ioctl(g_i2cFile, I2C_SLAVE, address) < 0) {
perror(“i2cSetAddress”);
exit(1);
}
Example of a write:
// Write data to the device
unsigned char I2C_WR_Buf[MAX_BUFFER_SIZE];
I2C_WR_Buf[0] = Reg_ADDR;
I2C_WR_Buf[1] = Data;
if(write(g_i2cFile, I2C_WR_Buf,2) != 2) {
perror(“Write Error”);
}
// Close the handle
close(g_i2cFile);
Example of a read:
unsigned char I2C_WR_Buf[MAX_BUFFER_SIZE];
unsigned char I2C_RD_Buf[MAX_BUFFER_SIZE];
I2C_WR_Buf[0] = Reg_ADDR;
i2cSetAddress(DEVICE_ADDR);
if(write(g_i2cFile, I2C_WR_Buf, 1) != 1) {
perror(“Write Error”);
}
i2cSetAddress(DEVICE_ADDR);
if(read(g_i2cFile, I2C_RD_Buf, n) !=n){
perror(“Read Error”);
}
// Close the handle
close(g_i2cFile);
Thanks for any help you can provide me!