diff --git a/10_chardev/include/ioctl.h b/10_chardev/include/ioctl.h new file mode 100644 index 0000000..e0e11ad --- /dev/null +++ b/10_chardev/include/ioctl.h @@ -0,0 +1,13 @@ +#ifndef PCI_IOCTL_H +#define PCI_IOCTL_H +#include + +#define GREEN_LEN_ON _IO('q', 1) +#define GREEN_LEN_OFF _IO('q', 2) + +#define RED_LEN_ON _IO('q', 3) +#define RED_LEN_OFF _IO('q', 4) + +#define DEVICE_NAME "/dev/led" + +#endif // MY_IOCTL_H diff --git a/10_chardev/user/Makefile b/10_chardev/user/Makefile new file mode 100644 index 0000000..1698b75 --- /dev/null +++ b/10_chardev/user/Makefile @@ -0,0 +1,10 @@ +all: led_ioctl + +led_ioctl: led_ioctl.c + g++ -o $@ $< -L.-std=c++11 + +clean: + @rm -vf led_ioctl + +clear: clean + @rm -vf led_ioctl diff --git a/10_chardev/user/led_ioctl.c b/10_chardev/user/led_ioctl.c new file mode 100644 index 0000000..d5505eb --- /dev/null +++ b/10_chardev/user/led_ioctl.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../include/ioctl.h" + +using namespace std; + +int main(int argc, char *argv[]) +{ + int fd, i, ret; + char buf[4096*16]; + fd = 0; + + fprintf(stderr, "%s:%d: Usage: %s cmd\n", __FILE__, __LINE__, *argv); + fprintf(stderr, "1 cmd: gree len switch on\n"); + fprintf(stderr, "2 cmd: gree len switch off\n"); + fprintf(stderr, "3 cmd: red len switch on\n"); + fprintf(stderr, "4 cmd: red len switch off\n"); + + bool green_on_cmd = false; + bool red_on_cmd = false; + std::string file_name = DEVICE_NAME; + + if(argc >= 2) + { + int argument = atoi(argv[1]); + switch(argument) + { + case 1: + { + green_on_cmd = true; + break; + } + case 2: + { + green_on_cmd = false; + break; + } + case 3: + { + red_on_cmd = true; + break; + } + case 4: + { + red_on_cmd = false; + break; + } + } + } + + + fd = open(file_name.c_str(), O_RDWR); + if (fd == -1) { + fprintf(stderr, "%s:%d: Can't open %s\n", __FILE__, __LINE__, file_name.c_str()); + return 1; + } + + if(green_on_cmd ) + { + if (ioctl(fd, GREEN_LEN_ON) == -1) + { + perror("query_apps ioctl"); + } + } + else + { + if (ioctl(fd, GREEN_LEN_OFF) == -1) + { + perror("query_apps ioctl"); + } + } + + if(red_on_cmd ) + { + if (ioctl(fd, RED_LEN_ON) == -1) + { + perror("query_apps ioctl"); + } + } + else + { + if (ioctl(fd, RED_LEN_OFF) == -1) + { + perror("query_apps ioctl"); + } + } + + close(fd); + + return 0; +}