0

I am calculating a interrupt time in dev.c kernel source code as below :

extern double InterruptTime;
InterruptTime = ktime_get_real();   //timestamp

I am writing a data from kernel space to user space using procfs and using the below api in kernel space for sending data to the user space. PROCFS.c :

struct device {
double array[100];
}chr_arr;

ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
int len;
chr_arr.array =InterruptTime;         // Is this possible ??
len = count >= strlen(chr_arr.array) ? strlen(chr_arr.array) : count;
*offset += len; 
    if (*offset >= strlen(chr_arr.array))
        return 0;

    if (copy_to_user(buf,chr_arr.array,len))
        return -EFAULT;

    return len;
}

is it possible to read the InterruptTime from dev.c in PROCFS.c as shown above ?? how the data sent from above kernel code will be received on the user side (i.e InterruptTime)??

user3458454
  • 291
  • 1
  • 4
  • 20
  • What do you return to the user space code? Only a single `char` per read? – JimmyB Apr 17 '14 at 10:59
  • InterruptTime is the timestamp in my code. I want to return the InterruptTime back to the user. – user3458454 Apr 17 '14 at 11:05
  • So what type is `InterruptTime`? `extern char InterruptTime` suggests it's only a single char. Do you buffer multiple chars in your chr_arr or only a single one? - Or, more specifically, what is the `sizeof(InterruptTime)`? – JimmyB Apr 17 '14 at 11:08
  • the InterruptTime is of type double; so I modified my code above. is it the right way to access the InterruptTime (which I calculated from dev.c program by procfs.c program) ?? how to receive the InterruptTime on the user side ? – user3458454 Apr 17 '14 at 11:19

1 Answers1

0

I'm not quite sure yet, whether you need to only provide a single value of InterruptTime or if you need to buffer those values as they are generated for the user-space code to retrieve later. Without the extra effort of buffering and for simplicity, I'd suggest something along these lines:

ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset)
{

  if ( count < sizeof(InterruptTime) ) {
    // Not enough space provided.
    return 0; // Or some error code maybe.
  }

  if (copy_to_user(buf,&InterruptTime,sizeof(InterruptTime)) {
    return -EFAULT;
  } else {
    return sizeof(InterruptTime); // Number of bytes we copied.
  }

}

(Note that this is not very clean and should be improved by buffering data to allow for reads of arbitrary size, correct error handling, &c.)

Then, in user-space, you'd do something like

#include <fcntl.h>
...

int fd = open("/proc/...", O_RDONLY);

double timestamp;

loff_t dummyOffset;

if ( read( fd, &timestamp, sizeof(timestamp) ) != sizeof(timestamp) ) {
  // Failure.
} else {
  // Use value in timestamp...
}

...
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • thank you very much but the InterruptTime calculated in dev.c program and I am using ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset) in PROCFS.c program to 1 : I want to read the InterruptTime from dev.c and later transferring to kernel. Is it the right way to access the InterruptTime from PROCFS.c program ?? – user3458454 Apr 17 '14 at 12:12
  • when ever kernel receives the interrupt. The InterruptTime will be calculated and later sent it to the user via PROCFS.c program. I think as per your program : I dont want to create an array to store the interrupt time. instead we can access it directly the interrupt time from dev.c - is it right ? – user3458454 Apr 17 '14 at 12:14