static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); 的作用

在 老罗的android例程里面有

static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);

/*读取设备属性val*/  

  • static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {  
  •     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);          
  •   
  •     return __hello_get_val(hdev, buf);  
  • }  
  •   
  • /*写设备属性val*/  
  • static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {   
  •     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);    
  •       
  •     return __hello_set_val(hdev, buf, count);  
  • }  

就这样就可以在串口 terminal 改变/显示val 变量的值

# cat hello
0
# echo '5' > hello
# # echo '5' > hello                  # cat hello
5

 

原来是static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); 

的作用

原型是#define DEVICE_ATTR(_name, _mode, _show, _store) \

struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

函数宏DEVICE_ATTR内封装的是__ATTR(_name,_mode,_show,_stroe)方法,_show表示的是读方法,_stroe表示的是写方法。

当我们将数据 echo 到接口中时,在上层实际上完成了一次 write 操作,对应到 kernel ,调用了驱动中的 “store”。同理,当我们cat 一个 接口时则会调用 “show” 。到这里,只是简单的建立了 android 层到 kernel 的桥梁,真正实现对硬件操作的,还是在 "show" 和 "store" 中完成的

转载于:https://www.cnblogs.com/gooogleman/archive/2012/07/09/2582889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值