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" 中完成的

  • 相关阅读:
    函数的一些应用
    关于javascript的一些知识以及循环
    <记录学习>京东页面最后一天HTML以及css遇到的问题
    <记录学习>(前三天)京东页面各种注意点
    银行账号输入格式代码
    CSS兼容性常见问题总结
    移动端实现摇一摇并振动
    LESS使用方法简介(装逼神器)
    H5移动端性能优化
    BFC,IFC,GFC,FFC的定义及功能
  • 原文地址:https://www.cnblogs.com/senior-engineer/p/4922644.html
Copyright © 2011-2022 走看看