Article 5DHJ9 ioctl() only works after iterating over data with printf function

ioctl() only works after iterating over data with printf function

by
scotty7
from LinuxQuestions.org on (#5DHJ9)
Hi everyone,

Noob here. I've been playing around with reading & writing i2c from userspace and I've come up against a weird issue.

I've created a variadic function to send a number of bytes to an i2c device. It works great if I include the 'debug printf for loop'. It iterates through the data I'm about to send, casts it as an integer (because I couldn't find a printf formatter for a short) and prints it out. For some reason if I comment out that loop, then the ioctl function will return an error.

I didn't think casting inside a printf statement would do anything to the original data, so the fact that it's messing with the code after it is odd to me. If anyone can shine some light on why this is happening I would appreciate it.

Code:bool i2c_write(short addr, short reg, short num, ...){

va_list data;
va_start(data, num);

size_t size = num+1;
unsigned char *send_buffer = malloc(size);

if(send_buffer == NULL){
printf("i2c_write: Could not allocate memory");
return false;
}

send_buffer[0] = reg;
for(int i = 0; i<num; i++){
send_buffer[i+1] = (unsigned char)va_arg(data, int);
}

//debug to see sent data
// for(int i = 0; i<size; i++){
// printf("send buffer [%d]: %d", i, (int)send_buffer[i]);
// }

va_end(data);

struct i2c_rdwr_ioctl_data burst;
struct i2c_msg i2c_message;

burst.nmsgs = 1;
burst.msgs = &i2c_message;

i2c_msg.addr = addr;
i2c_msg.buf = send_buffer;
i2c_msg.len = num+1;

int result = ioctl(i2c_fd, I2C_RDWR, &burst); //actually send the data

free(send_buffer);

if(result < num){ //check for the correct number of send bytes, doesn't seem to work atm
printf("i2c_write: data was not sent properly, %d bytes sent. %s",result,strerror(errno));
return false;
}else{
return true;
}
}latest?d=yIl2AUoC8zA latest?i=qwJm93pNmnk:BUSRbykKXM8:F7zBnMy latest?i=qwJm93pNmnk:BUSRbykKXM8:V_sGLiP latest?d=qj6IDK7rITs latest?i=qwJm93pNmnk:BUSRbykKXM8:gIN9vFwqwJm93pNmnk
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments