1

Trying to make a read function, that not only adds the numbers together. But also counts how many numbers were added.

Needs to be used in such script:

for i in 1 2 4 8 16 32 64; do
   echo $i > /dev/summer0
   sleep 60
done

But also just using a test case.

Here is the kernel module code:

/* the memory of the device
*/
int total;

/* Global variables declared as staic, so are global within the file.*/
static char *msg_Ptr;
static char msg[BUF_LEN];
static int n;

/* called after the device is opened
*/
int device_open(struct inode *inode, struct file *file)
{
    printk("\nsummer device is open\n");
    total = 0;

    printk(msg);
    msg_Ptr = msg;

    return 0;
}

/* Read function */
static ssize_t device_read(struct file *filep, char *buffer, size_t length, loff_t *offset)
{    
    /* Number of bytes actually written to the buffer */
    int bytes_read = 0;

    /* If we're at the end of the message, return 0 signifying end of file */
    if (*msg_Ptr == 0) return 0;

    /* Actually put the data into the buffer */
    while (length && *msg_Ptr) 
    {
        /* The buffer is in the user data segment, not the kernel segment;
        *  assignment won't work. We have to use put_use which copies data from 
        *  the kernel data segment to the user data segment. */
        __put_user(*(msg_Ptr++), buffer++);

        length++;
        n++;
    }
    /* Most read functions return the number of bytes put into the buffer */
    return n;
}

/* handling of ioctl events
*/
long device_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
{
    int number;
    switch(ioctl_num)
    {
        case SUMMER_SET:
            __get_user(number, (int*)ioctl_param);
            total += number;
            break;
        case SUMMER_GET:
            __put_user(total, (int*)ioctl_param);
            break;
    }
    return 0;
}

/* table of event handlers of the device
*/
struct file_operations fops =
{
    read   : device_read,
    //write  : device_write,
    open   : device_open,
    release: device_close,
    unlocked_ioctl : device_ioctl
};

As for the testcase

int n;    
int main(void)
{
    int sum;
    char buffer[BUF_LEN];

    /* open character device   */
    int fd = open("/dev/summer0", O_RDWR);  // open device
    if(fd < 0)
    {
        printf("no device found\n");
        return 0;
    }

    int nc = read(fd,buffer,4,n);

    for(;;)
    {
        int num;
        printf("Number (exit with 0) = ");
        while(scanf("%d", &num) != 1)
            ;
        printf("-- %d --\n", num);
        if(num <= 0) break;

        /* use ioctl to pass a value to the character device     */
        ioctl(fd, SUMMER_SET, &num);
        n++;
    }

    /* use ioctl to get a value from the character device   */
    ioctl(fd, SUMMER_GET, &sum);

    printf("#char = %d\n", nc);
    printf("Result = %d\n", sum);
    printf("Buffer: %s\n" , buffer);
    close(fd);                         // close device
    return 0;
}

The program is keeping track of the amount of numbers entered during a single test, but it is supposed to keep track of the numbers until I install the kernel module.

5
  • What you try to do is not clear... Can you describe what you do (ie: 1- modprobe, 2- shell code, 3- c program...) what you get as result and what you wanted to do? In short a minimal reproducible example
    – Mathieu
    May 13, 2016 at 11:25
  • You define msg and msg_Ptr but you never write anything into the associated memory
    – Mathieu
    May 13, 2016 at 11:39
  • you have total=0 in device_open, so you reset the value each time when opening the device May 13, 2016 at 12:18
  • The global idea of the program is that it doesn't only sums the numbers entered, and also gives me a count of the amount of numbers entered in total. May 13, 2016 at 12:27
  • You need to clarify -- possibly to yourself; certainly to SO -- how you want this to behave. The C code resets total every time the device is opened, and communicates through ioctl codes. The script example communicates through writing to the device (which doesn't appear to have a handler) and appears to assume the total / count is accumulated over several such writes.
    – TripeHound
    May 13, 2016 at 12:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.