瑞芯微Rockchip开发者社区
直播中

张飞雄

7年用户 985经验值
私信 关注
[问答]

如何通过kernel设备文件节点打通android app与kernel间的通信?

如何通过kernel设备文件节点打通android app与kernel间的通信

回帖(1)

殷夏

2022-3-4 14:11:40
                    
[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 calledn", __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=%dn",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 calledn", __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");

}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分