Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK10: Add userspace app #161

Open
wants to merge 1 commit into
base: Vitaliy.Zhyrko
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions 10_chardev/include/ioctl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PCI_IOCTL_H
#define PCI_IOCTL_H
#include <linux/ioctl.h>

#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
10 changes: 10 additions & 0 deletions 10_chardev/user/Makefile
Original file line number Diff line number Diff line change
@@ -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
100 changes: 100 additions & 0 deletions 10_chardev/user/led_ioctl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#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;
}