Statistics
| Revision:

root / logic / trunk / src / drv_comm / drv_comm.c @ 71

History | View | Annotate | Download (2.21 KB)

1
/*
2
* ioctl.c โˆ’ the process to use ioctl's to control the kernel module
3
*
4
* Until now we could have used cat for input and output. But now
5
* we need to do ioctl's, which require writing our own process.
6
*/
7
/*
8
* device specifics, such as ioctl numbers and the
9
* major device file.
10
*/
11
#include "drv_comm.h"
12
#include "generator.h"
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <fcntl.h>            /* open */
16
#include <unistd.h>            /* exit */
17
//#include <sys/ioctl.h>  /* ioctl */
18
/*
19
* Functions for the ioctl calls
20
*/
21
ioctl_set_msg(int file_desc, char *message)
22
{
23
        int ret_val;
24
        ret_val = ioctl(file_desc, IOCTL_SET_MSG, message);
25
        if (ret_val < 0) {
26
                printf("ioctl_set_msg failed:%d\n", ret_val);
27
                exit(-1);
28
        }
29
}
30

    
31
ioctl_get_msg(int file_desc)
32
{
33
        int ret_val;
34
        char message[100];
35
        /*
36
        * Warning โˆ’ this is dangerous because we don't tell
37
        * the kernel how far it's allowed to write, so it
38
        * might overflow the buffer. In a real production
39
        * program, we would have used two ioctls โˆ’ one to tell
40
        * the kernel the buffer length and another to give
41
        * it the buffer to fill
42
        */
43
        ret_val = ioctl(file_desc, IOCTL_GET_MSG, message);
44
        if (ret_val < 0) {
45
                printf("ioctl_get_msg failed:%d\n", ret_val);
46
                exit(-1);
47
        }
48
        //printf("get_msg message:%s\n", message);
49
}
50

    
51
int ioctl_get_nth_byte(int file_desc)
52
{
53
        int i, val;
54
        char c;
55

    
56
        val=0;
57
        i = 0;
58
        do {
59
                c = ioctl(file_desc, IOCTL_GET_NTH_BYTE, i++);
60
                if (c < 0) {
61
                        printf("ioctl_get_nth_byte failed at the %d'th byte:\n",i);
62
                        exit(-1);
63
                }
64
        val=val*10+atoi(&c);
65
        } while (c != 0);
66
        return (int)val/10; //because string termination character also gets read in loop we have to divide by 10
67
}
68

    
69
int get_device_value(const char *dev)
70
{
71
    //TODO use mutex?
72
    int file_desc, ret_val;
73

    
74
        char *msg = int_to_str(generate_value());
75

    
76
        file_desc = open(dev, 0);
77

    
78
        if (file_desc < 0) {
79
                printf("Can't open device file: %s\n", dev);
80
                ret_val = generate_value(); // TODO fake value in case driver doesnt work
81
        }
82
        else{
83
                //printf("Open device file: %s\n", dev);
84
        //ioctl_set_msg(file_desc, msg);
85
        ret_val = ioctl_get_nth_byte(file_desc);
86
        }
87

    
88
        close(file_desc);
89
        ret_val = ret_val+((random()%3-1)*5); //+- 5 (conversion to fload needed later)
90
        printf("Returning val %d\n", ret_val);
91

    
92
    return ret_val;
93
}
94