在内核中, sysfs 属性一般是由 __ATTR 系列的宏来声明的,如对设备的使用 DEVICE_ATTR ,对总线使用 BUS_ATTR ,对驱动使用 DRIVER_ATTR ,对类别(class)使用 CLASS_ATTR, 这四个高级的宏来自于 <include/linux/device.h>, 都是以更低层的来自 <include/linux/sysfs.h> 中的 __ATTR/__ATRR_RO 宏实现。

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

实现办法,例一:

#include <linux/platform_device.h>

1)

static ssize_t rohm_proximity_show_debug(struct device* cd,struct device_attribute *attr, char* buf)
{
    ssize_t ret = 0;
    
    sprintf(buf, "ROHM Debug %d\n",debug_level);//将格式数据写到缓冲里面,此处为buf
    
    ret = strlen(buf) + 1;

return ret;
}

static ssize_t rohm_proximity_store_debug(struct device* cd, struct device_attribute *attr,
               const char* buf, size_t len)
{
    unsigned long on_off = simple_strtoul(buf, NULL, 10);//解析字符串cp 中 8,10,16 进制数字   ,返回值是解析的数字,endp 指向字符串起始处,base :进制
    debug_level = on_off;

printk("%s: debug_level=%d\n",__func__, debug_level);
    
    return len;
}

2)

static DEVICE_ATTR(debug, S_IRUGO | S_IWUSR, rohm_proximity_show_debug, rohm_proximity_store_debug);

3)

static int rohm_proximity_create_sysfs(struct platform_device *client)
{
    struct device *dev = &(client->dev);
    int err = 0;

PS_DBG("%s\n", __func__);

if ((err = device_create_file(dev, &dev_attr_control)))
        goto err_out;

if ((err = device_create_file(dev, &dev_attr_debug)))
        goto err_out;

return 0;

err_out:
    return err;
}

4)

static int rohm_proximity_probe(struct platform_device *pdev)

{

rohm_proximity_create_sysfs(pdev);

}

实现办法,例二:

1)

static ssize_t mc32x0_threshold_show(struct device *dev,
                     struct device_attribute *attr, char *buf)
{
#ifdef MC32X0_DEBUG
       printk("mcube %s\n",__FUNCTION__);
#endif

return sprintf(buf, "%d\n", mc32x0_get_threshold(dev));
}

static ssize_t mc32x0_threshold_store(struct device *dev,
                      struct device_attribute *attr,
                      const char *buf, size_t count)
{
    unsigned long threshold;

#ifdef MC32X0_DEBUG
       printk("mcube %s\n",__FUNCTION__);
#endif

threshold = simple_strtoul(buf, NULL,10);
        if (threshold >= 0 && threshold <= ABSMAX_8G) {
        mc32x0_set_threshold(dev, threshold);
        }

return count;
}

2)

static DEVICE_ATTR(threshold, 0666,    mc32x0_threshold_show, mc32x0_threshold_store);

static struct attribute *mc32x0_attributes[] = {
  //  &dev_attr_enable.attr,
   // &dev_attr_delay.attr,
  //  &dev_attr_position.attr,
    &dev_attr_threshold.attr,
};

3)

static struct attribute_group mc32x0_attribute_group = {
    .attrs = mc32x0_attributes
};

4)

static int mc32x0_probe(struct i2c_client *client, const struct i2c_device_id *id)

{

err = sysfs_create_group(&mc32x0->input->dev.kobj, &mc32x0_attribute_group);//.probe中生成

if (err < 0) {
        goto error_2;
    }

//sysfs_remove_group(&mc32x0->input->dev.kobj, &mc32x0_attribute_group);//.remove中移除

}

adb下输入:(以下节点为DEVICE_ATTR(debug,,)同为,位置在/sys/devices/platform/rohm_proximity/,在要/目录下find . -name "debug"查找)

#cat debug         //此端口下会输出"ROHM Debug XX",XX为此处值。注:另一adb端口cat /proc/kmsg不会显示此字符

#echo 01>debug //在另外一个adb端口下cat /proc/kmsg会显示"rohm_proximity_store_debug: debug_level=1"。

注:echo 01>debug值为1,echo 1>debug值为0,echo 0>debug无效.

此时已给此端口写1,再#cat debug,端口下会输出"ROHM Debug 1"

参考文档:http://blog.chinaunix.net/uid-26413351-id-3180609.html

DEVICE_ATTR实例分析相关推荐

  1. Android从驱动到应用开发实例分析

    Android从驱动到应用开发实例分析 1. 第一个android应用程序 Android应用一般包含一个源代码目录src.一个资源目录res.一个配置文件AndroidManifest.xml.和一 ...

  2. gpgpu-sim卡分配程序设计实例分析

    gpgpu-sim卡分配程序设计实例分析 运行代码地址:https://github.com/gpgpu-sim/gpgpu-sim_distribution 一.概述 此文件包含有关安装.生成和运行 ...

  3. python多功能电子钟_python gui - PyQt4 精彩实例分析之电子钟

    PyQt4 精彩实例分析之电子钟,当然在写实例之前要先安装PyQt4模块.from PyQt4.QtGui import * from PyQt4.QtCore import * import sys ...

  4. RPC-原理及RPC实例分析

    还有就是:RPC支持的BIO,NIO的理解 (1)BIO: Blocking IO;同步阻塞: (2)NIO:Non-Blocking IO, 同步非阻塞; 参考:IO多路复用,同步,异步,阻塞和非阻 ...

  5. python asyncio教程_python中使用asyncio实现异步IO实例分析

    1.说明 Python实现异步IO非常简单,asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接 ...

  6. 马歇尔·赫伯特:人工智能的前沿技术与实例分析

    来源:中国人工智能学会 2017年12月11日,国际知名机器人专家.美国卡耐基梅隆大学机器人研究所所长马歇尔·赫伯特(Martial Hebert)教授和首席科学家大卫·伯恩(David Bourne ...

  7. python怎么处理数据_python中scrapy处理项目数据的实例分析

    在我们处理完数据后,习惯把它放在原有的位置,但是这样也会出现一定的隐患.如果因为新数据的加入或者其他种种原因,当我们再次想要启用这个文件的时候,小伙伴们就会开始着急却怎么也翻不出来,似乎也没有其他更好 ...

  8. Android10.0 Binder通信原理(四)-Native-C\C++实例分析

    摘要:本节主要来讲解Android10.0 Binder的Native层实例流程 阅读本文大约需要花费35分钟. 文章首发微信公众号:IngresGe 专注于Android系统级源码分析,Androi ...

  9. Android Touch事件原理加实例分析

    Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...

最新文章

  1. cookie 免密登录_python
  2. Cache总义Cache用法之页面声明
  3. 案例分享|突破卡脖子技术,研制协作机器人核心零部件的方向与思考
  4. [剑指offer] 替换空格
  5. FreeBSD Top States
  6. 数据结构二叉树的所有基本功能实现。(C++版)
  7. jQuery 事件绑定方法(bind hover toggle live.... )、删除事件方法(unbind, die)及 事件对象
  8. 信息系统项目管理师-项目沟通管理与干系人管理核心知识点思维脑图
  9. 等式数量--hash算法之除留余数法
  10. 从控件开发的角度看几个editor控件,Freetextbox,radtoolbar,abouteditor,cuteeditor
  11. oracle 存储同步,Oracle数据库知识——存储过程篇
  12. PHP删除数组中的空值
  13. Flex与Servlet之间数据的交互
  14. 数据资产盘点的流程和方法
  15. 扇贝python_扇贝编程(python)手机版-扇贝编程app下载v1.1.30-汉化新世纪
  16. CDEC 2019中国数字智能生态大会暨第十二届中国软件渠道大会 北京站 | 参会指南...
  17. Linux基础第一章:基础知识与基础命令(第三部分)
  18. 网络安全实验2 扫描器X-SCANNER应用实验
  19. .NET框架设计—常被忽视的C#设计技巧
  20. sub html编辑器,目前前端开发必备编辑器有哪几款呢?

热门文章

  1. 036_PageHeader页头
  2. 007_SpringBoot文件上传
  3. 008_Queue消息模式发送映射消息
  4. JAVA共有几种窗体布局方式_在Java GUI程序开发中常见的三种布局管理器是什么
  5. mysql 业务账户_mysql的事务
  6. 字符串html在线互转,将string 的字符串转换为HTML的两种方法
  7. socket编程listen函数限制连接数的解决方案_网络编程——服务器篇
  8. http路径转file会变成反斜杠_PHP session反序列化漏洞
  9. Mongodb部署及使用
  10. ValueAnimator API 介绍