[RK3128][Android7.1]通过kernel设备文件节点,打通android app与kernel间的通信

[RK3128][Android7.1]通过kernel设备文件节点,打通android app与kernel间的通信

Platform: Rockchip
OS: Android 7.1.2
Kernel: 3.10

需求:

通过在相关的kernel文件中建立文件节点,使adroid端app能直接与相关驱动文件交互,完成特定的驱动行为

实现:

第一步:实现驱动文件中建立文件节点

static ssize_t test_read_status(struct device* cd, struct device_attribute* attr,char *buf)
{
        printk(KERN_EMERG "line:%d,%s is called\n", __LINE__, __FUNCTION__);
	 return 0;
}

static ssize_t test_write_status(struct device* cd, struct device_attribute *attr,const char* buf, size_t len){
	
	
	int id;	
	//int dir = 1;
	sscanf(buf, "%d", &id);
	printk(KERN_ERR "test_write_status id=%d\n",id);
	
	return len;
}

static DEVICE_ATTR(test, S_IRUGO | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH| S_IWOTH, test_read_status, test_write_status);

static int test_create_sysfs(struct i2c_client *client)
{
	int err;
	struct device *dev = &(client->dev);
	printk(KERN_EMERG "line:%d,%s is called\n", __LINE__, __FUNCTION__);
	err = device_create_file(dev, &dev_attr_test);
	return err;
}

第二步:在app中操作文件节点

	private static final String SYS_FILE = "/sys/devices/20072000.i2c/i2c-0/0-0010/test";
    private static final File mFile = new File(SYS_FILE);

    public void Write2File(File file,String mode) {
        Log.d(TAG,"Write2File,write mode = "+mode);
        if((file == null) || (!file.exists()) || (mode == null)) return ;

        try {
            FileOutputStream fout = new FileOutputStream(file);
            PrintWriter pWriter = new PrintWriter(fout);
            pWriter.println(mode);
            pWriter.flush();
            pWriter.close();
            fout.close();
        } catch(IOException re) {
        	Log.d(TAG,"write error:"+re);
        	return;
        }
    }

	public void test(){
		Write2File(mFile, "1");
	}

你可能感兴趣的:(Rockchip)