본문 바로가기
임베디드/KERNEL

test_dev3.c test_app3.c 메뉴띄우기 switch

by sj0020 2020. 12. 21.

1. test_dev3.c 

Makefile

#linux kernel source
KDIR = /home/sss/linux
obj-m := test_dev3.o

default:
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KDIR) M=$$PWD modules
	
clean:
	$(MAKE) -C $(KDIR) M=$$PWD clean

test_dev3.c

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>

#define IOCTRL_LED_ON   100
#define IOCTRL_LED_OFF   101
#define IOCTRL_EXIT      102

#define BUFF_SIZE      1024
#define MAJOR_NUMBER   258
#define DEVICE_NAME      "my_device3"

static char *buffer = NULL;   //커널에서 사용 버퍼
static int sz_data = 0;      // data size

//장치파일 열기
static int my_open(struct inode *inode, struct file *filp){
   printk("device driver open! \n");
   return 0;
}

//장치 파일 닫기
static int my_release(struct inode *inode, struct file *filp)
{
   printk("<Ko> device driver release! \n");
   return 0;
}
 
//장치파일에 데이터 쓰기
static ssize_t my_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
   printk("<Ko> write to buffer! %s\n", buf);
   if(BUFF_SIZE < count){
      sz_data = BUFF_SIZE;
   }
   sz_data = count;
   strncpy(buffer, buf, sz_data);
   printk("<Ko> buffer : %s, %d\n",buffer, sz_data);
   return count;
}

//장치 파일에서 데이터 읽어 옴.
static ssize_t my_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
   printk(KERN_DEBUG "<Ko> from, buffer %s\n", buffer);
   // App의 read()로 보내는 buffer (사용자 값)
   if(!strcmp(buffer, "1")){
      copy_to_user(buf, "LED ON", 7);
   }
   else if(!strcmp(buffer,"2")){
      copy_to_user(buf,"LED OFF",8);
   }
   return sz_data;
}

static long my_ioctl(struct file *flip, unsigned int cmd, unsigned long arg){
   printk(KERN_DEBUG "<Ko> cmd : %d \n" ,cmd);
   printk(KERN_DEBUG "IOCTRL_LED_ON : %d \n" ,IOCTRL_LED_ON);
   
   switch(cmd){
   case IOCTRL_LED_ON:
      printk(KERN_INFO "LED ON in kernel\n");
      break;
   case IOCTRL_LED_OFF:
      printk(KERN_INFO "LED OFF in kernel\n");
      break;
   case IOCTRL_EXIT:
      printk(KERN_INFO "App exit\n");
      break;
   default:
      printk(KERN_INFO "unknown menu!\n");
      break;
   }
   return 0;
}
      

// 파일 동작 함수 연결
static struct file_operations vd_fops = {
   .owner = THIS_MODULE,
   .read = my_read,
   .write = my_write,
   .open = my_open,
   .release = my_release,
   .unlocked_ioctl = my_ioctl
};

// cat//proc//devices -> 디바이스 드라이버 MAJOR_NUMBER 확인
// $sudo mknod /dev/my_device c 257 0
// $sudo chmod 666 /dev/my_device
int __init my_init(void) {
   register_chrdev(MAJOR_NUMBER, DEVICE_NAME,&vd_fops);
   buffer = (char*)kmalloc(BUFF_SIZE, GFP_KERNEL);
   memset(buffer, 0, BUFF_SIZE);
   printk(KERN_DEBUG "<Ko> device driver init \n");
   return 0;
}

void __exit my_exit(void){
   unregister_chrdev(MAJOR_NUMBER, DEVICE_NAME);
   kfree(buffer);
   printk(KERN_DEBUG "<Ko> device driver 해제 - 커널 해제 \n");
}

module_init(my_init);
module_exit(my_exit);
MODULE_AUTHOR("Ko");
MODULE_LICENSE("Rack");

 

 

2. test_app3.c

Makefile

# app Makefile
CC = gcc
OBJECT = test_app3.o
TARGET = test_app3
CFLAGS = -g -Wall

$(TARGET) : $(OBJECT)
	$(CC) -o $(TARGET) $(OBJECT)

clean:
	rm -f $(OBJECT) $(TARGET)

test_app3.c

#include <unistd.h>
//#include <fcntl.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>


typedef enum {
	IOCTRL_LED_ON = 100,
	IOCTRL_LED_OFF,
	PROGRAM_EXIT
} ioctrlCmd_t;

int main(int argc, char **argv) {
   int dev = 0;
   //char buff[1024] = { 0,};
   int menu = 0;
   dev = open("/dev/my_device3", O_RDWR); //open은 한번만 열면 됨
   while(1){
	   printf("--------------------\nKERNEL Module Control 1.0\n--------------------\n");
	   printf("1. LED ON\n2. LED OFF\n3. EXIT\n");
	   printf("-------------------\n");
	   printf("menu : ");
	   scanf("%d", &menu);
	   switch(menu +99){
			case IOCTRL_LED_ON:
				ioctl(dev, IOCTRL_LED_ON);
				break;
			case IOCTRL_LED_OFF:
				ioctl(dev, IOCTRL_LED_OFF);
				break;
			case PROGRAM_EXIT:
				printf("Exit BYE\n");
				ioctl(dev, PROGRAM_EXIT);
				close(dev);
				exit(0);
	   }
   }
   
   return 0;
}

 

'임베디드 > KERNEL' 카테고리의 다른 글

라즈베리파이 gpio _led  (0) 2020.12.24
GPIO 컨트롤  (0) 2020.12.24
test app 메뉴 형식으로 뜨게 수정  (0) 2020.12.21
디바이스드라이버 LED ON/OFF  (0) 2020.12.18
-fstack-protector-strong’ 오류  (0) 2020.12.18