驱动
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
unsigned int major;
char kbuf[128]={0};
unsigned int *vir_rcc;
//定义等待队头
wait_queue_head_t wq_head;
//定义数据是否准备好的标志变量
int condition=0;
struct class *cls;
struct device *dev;
int i;int mycdev_open(struct inode *inode, struct file *file)
{// int a=inode->i_rdev;// file->private_data=(void *)MINOR(a);printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{//将进程切换为休眠态wait_event_interruptible(wq_head,condition); long ret;ret=copy_to_user(ubuf,kbuf,size);if(ret){printk("copy_to_user filed\n");return -EIO;}condition=0;return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{long ret;ret=copy_from_user(kbuf,ubuf,size);if(ret){printk("copy_from_user filed\n");return -EIO;}condition=1;//唤醒休眠的进程wake_up_interruptible(&wq_head);//判断用户输入的数据,进行不同的硬件逻辑操作return 0;
}//封装poll方法
__poll_t mycdev_poll(struct file *file,struct poll_table_struct *wait)
{__poll_t mask=0;poll_wait(file,&wq_head,wait);if(condition){mask=POLLIN;}return mask;
}
int mycdev_close (struct inode *inode, struct file *file)
{printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);return 0;}
struct file_operations fops={.open=mycdev_open,.release=mycdev_close,.read=mycdev_read,.write=mycdev_write,.poll=mycdev_poll,// .unlocked_ioctl=mycdev_ioctl,
};
static int __init mycdev_init(void)
{//初始化等待队列头init_waitqueue_head(&wq_head);major=register_chrdev(0,"myled",&fops);if(major<0){printk("注册字符设备驱动失败");return major;}printk("注册字符设备驱动成功major=%d\n",major);//映射led1物理地址//向上提交目录cls = class_create(THIS_MODULE,"myled");if(IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功\n");//向上提交设备节点信息for (i=0;i<3;i++){dev=device_create(cls, NULL, MKDEV(major,i),NULL,"myled%d",i);if(IS_ERR(dev)){printk("向上提交设备节点信息失败\n");return -PTR_ERR(dev);}}printk("向上提交设备节点成功\n");return 0;
}
static void __exit mycdev_exit(void)
{for(i=0;i<3;i++){device_destroy(cls,MKDEV(major,i));}class_destroy(cls);unregister_chrdev(major,"myled");}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用程序
#include<stdio.h>
#include<sqlite3.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include<sys/epoll.h>
#define PROT 6666
#define IP "192.168.250.100"
int main(int argc, char const *argv[])
{int fds,newfds;//创建流失套接字fds = socket(AF_INET,SOCK_STREAM,0);{if(fds<0){perror("socket");printf("__%d__\n",__LINE__);return -1;}}//设置允许端口快速复用int reuse = 1;if(setsockopt(fds, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) <0){perror("setsockopt");return -1;}printf("setsockopt success __%d__\n",__LINE__);//定义服务器信息struct sockaddr_in sin;struct sockaddr_in cin;socklen_t clen = sizeof(cin);sin.sin_family = AF_INET;sin.sin_port = htons(PROT);sin.sin_addr.s_addr = inet_addr(IP);//绑定服务器信息if(bind(fds,(struct sockaddr*)&sin,sizeof(sin))<0){perror("bind");printf("__%d__\n",__LINE__);return -1;}//创建监听队列if(listen(fds,128)<0){perror("listen");printf("__%d__",__LINE__);return -1;}int epfd;struct epoll_event event;struct epoll_event events[10];char buf[128]={0};epfd=epoll_create(1);if(epfd<0){printf("epoll_create filed\n");exit(-1);}//读事件event.events=EPOLLIN;event.data.fd=fds;if(epoll_ctl(epfd,EPOLL_CTL_ADD,fds,&event)<0){printf("epoll_ctl add filed\n"); }while(1){int ret=epoll_wait(epfd,events,10,-1);if(ret<0){printf("epoll_wait filed\n");exit(-1);}int i;for(i=0;i<ret;i++){if(events[i].data.fd==fds){printf("1\n");newfds=accept(fds,(struct sockaddr *)&cin,&clen);printf("2\n");if(newfds<0){perror("accept");exit(-1);}printf("---[%s:%d]连接成功-\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));event.events=EPOLLIN;event.data.fd=newfds;if(epoll_ctl(epfd,EPOLL_CTL_ADD,newfds,&event)<0){printf("epoll_ctl add filed\n"); }}}}return 0;
}