zoukankan      html  css  js  c++  java
  • 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" 中完成的

  • 相关阅读:
    Zookeeper
    激活函数
    线程池
    用rand()和srand()产生伪随机数的方法总结 (转贴)
    连接SDE数据库代码
    ProgressBar
    ArcEngine+OpenGL之二系统平台搭建
    我所知道的ArcObjects开发(转)
    oracle wm_concat函数 乱码
    修改32位的AutoCAD2012,使其能在64位系统上安装
  • 原文地址:https://www.cnblogs.com/gooogleman/p/2582889.html
Copyright © 2011-2022 走看看