前言
最近做的项目需要实现在android系统中hal层与内核驱动进行简单的通信。查找过需要方法例如:管道、共享内存、信号等等,都无法实现可能是本人太菜打开方式不对。
下面介绍一种相对简单的方法,利用proc文件系统,在内核驱动程序中创建proc文件,就可以在hal层程序中读取内核驱动的信息。
代码实现
驱动层代码,部分函数和变量命名与本人项目有关,可以自行修改
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int micophone_proc_show(struct seq_file *m,void *v)
{
if(mico_ctl.hph_rl>4000||mico_ctl.hph_rr>40000)
seq_printf(m,"%s\n","yes"); //使用命令cat /proc/(创建的proc文件名)即可在终端打印出来
else
seq_printf(m,"%s\n","no");
return 0;
}
static int micophone_status_open(struct inode *inode,struct file *file)
{
return single_open(file,micophone_proc_show,NULL);
}
static ssize_t micophone_status_write(struct file *filp,const char __user *buff,
size_t count,loff_t *loff)
{
return 0;
}
static ssize_t micophone_status_read(struct file *filp,char __user *buff,
size_t count,loff_t *loff)
{
if(mico_ctl.hph_rl>4000||mico_ctl.hph_rr>40000)
{
buff[0] = 'y';
buff[1] = 'e';
buff[2] = 's';
buff[3] = '\0';
}
else
{
buff[0] = 'n';
buff[1] = 'o';
buff[2] = '\0';
}
return 0;
}
struct file_operations micophone_proc_fops = {
.read = micophone_status_read,
.write = micophone_status_write,
.open = micophone_status_open,
.owner = THIS_MODULE,
};
struct proc_dir_entry * micophone_proc;
void proc_for_micophone(void)
{
micophone_proc = proc_create("micophone_status",0777,NULL,&micophone_proc_fops); //0777参数我也不知道什么意思,有兴趣可以查查
if(!micophone_proc)
{
pr_err("%s: create_proc_entry fail!\n", __func__);
return;
}
pr_debug("%s: create_proc_entry succeed!\n", __func__);
}
hal层代码
int get_micophone_status(void)
{
int fd;
char buff[10];
fd = open("/proc/micophone_status",O_RDONLY|O_NDELAY);
read(fd,buff,10);
ALOGD("%s: micophone_status is %s\n", __func__, buff);
close(fd);
return((strcmp(buff,"yes")==0)?1:0);
}
这样就可以在hal层中读取内核驱动的信息,修改hardware层的代码后只编译内核是无法得到想要的结果的,因为hardware层的代码被编译成库文件添加到文件系统的镜像中,所以要是用编译命令:
mmm+(需要编译的hardware模块地址) //是用mmm工具编译前需要先编译整个android源码
make snod //可以在不重新编译system.img的前提下更新镜像
就可以得到更改hardware后的system.img,最后就是烧写啦!