当前位置:首页 > 公众号精选 > 嵌入式客栈
[导读]前言 今天在一个群里面看到的一个朋友提交,说of_property_read_string 这个函数有两个定义,到底是用了哪个呢? 所以这篇文章就说下这个函数。 函数引用的头文件 引用的头文件位置在 \kernel-4.4\include\linux\of.h 其中一个是 extern int of_property_read

前言

今天在一个群里面看到的一个朋友提交,说of_property_read_string 这个函数有两个定义,到底是用了哪个呢?

所以这篇文章就说下这个函数。

函数引用的头文件

引用的头文件位置在

\kernel-4.4\include\linux\of.h

其中一个是

extern int of_property_read_string(struct device_node *np,
				   const char *propname,
				   const char **out_string);

还有一个是

static inline int of_property_read_string(struct device_node *np,
					  const char *propname,
					  const char **out_string)
{
	return -ENOSYS;
}

但是并不是两个都用到,他们用了一个宏  CONFIG_OF 来选择

CONFIG_OF 宏有什么用?

这个宏的解释是

Open Firmware. This was invented long time ago when Apple was producing laptops based on PowerPC CPUs. Openfirmware provides a good description of the devices connected to the platform. In Linux kernel the part that works with device data is called Device Tree (DT). More details in theUsage model.

他的作用是

Openfirmware provides a good description of the devices connected to the platform

他提供了一种更好的方式来连接设备和驱动。

他是名字是

called Device Tree (DT)

DTS,那很明显了,开了这个宏,就表示使用了DTS设备树的方式来连接设备和驱动程序。

of_property_read_string 函数本体

函数位置

"./drivers/of/base.c"

函数原型

/**
 * of_property_read_string - Find and read a string from a property
 * @np:         device node from which the property value is to be read.
 * @propname:   name of the property to be searched.
 * @out_string: pointer to null terminated return string, modified only if
 *              return value is 0.
 *
 * Search for a property in a device tree node and retrieve a null
 * terminated string value (pointer to data, not a copy). Returns 0 on
 * success, -EINVAL if the property does not exist, -ENODATA if property
 * does not have a value, and -EILSEQ if the string is not null-terminated
 * within the length of the property data.
 *
 * The out_string pointer is modified only if a valid string can be decoded.
 */
int of_property_read_string(struct device_node *np, const char *propname,
                                const char **out_string)
{
        struct property *prop = of_find_property(np, propname, NULL);
        if (!prop)
                return -EINVAL;
        if (!prop->value)
                return -ENODATA;
        if (strnlen(prop->value, prop->length) >= prop->length)
                return -EILSEQ;
        *out_string = prop->value;
        return 0;
}
EXPORT_SYMBOL_GPL(of_property_read_string);

函数的作用:

返回propname对应dts节点对应的值。

使用方式:

传入np,就是设备树的节点,然后返回 "clock-output-names" 字符串对应的值,存入clk_name 里面。

of_property_read_string 函数剖析

int of_property_read_string(struct device_node *np, const char *propname,
                                const char **out_string)
{
        struct property *prop = of_find_property(np, propname, NULL);
        if (!prop)
                return -EINVAL;
        if (!prop->value)
                return -ENODATA;
        if (strnlen(prop->value, prop->length) >= prop->length)
                return -EILSEQ;
        *out_string = prop->value;
        return 0;
}
EXPORT_SYMBOL_GPL(of_property_read_string);
  • of_find_property 这个是找到这个dts节点,怎么找,可以再去这个函数分析一下。
  • strnlen功能「获取字符串实际字符个数,不包括结尾的'\0';如果实际个数 <= 第二个参数,则返回字符串实际字符个数,否则返回第二个参数。」
  • prop->length 是之前预设的一个值,strnlen正常情况返回的就是字符串的长度 减1「去掉\n字符」。
  • *out_string = prop->value 这里就是二级指针起到作用了,没有重新分配内存,直接把指针指向字符串位置。

我们再看看prop 的结构体,就一目了然了。

struct property {
	char	*name;
	int	length;
	void	*value;
	struct property *next;
	unsigned long _flags;
	unsigned int unique_id;
	struct bin_attribute attr;
};

关于二级指针举个例子

#include "stdio.h"

char *str = "helloworld";
char **p = NULL;
int main(void)
{
	p = &str;
	printf("%s\n",*p);
	return (0);
}


===========




免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

  首先说一下继承的好处:   1.提高了代码的复用性   2.让类和类之间产生了关系(关系是多态的前提)   将对像中的内容不断的向上抽取,就形成了关系,就有了继承,就

关键字: Android string

我们平时使用C++开发过程中或多或少都会使用std::string,但您了解string具体是如何实现的吗,这里程序喵给大家从源码角度分析一下。 读完本文相信您可以回答以下问题: ▼ string的常见的实现方式有几种?...

关键字: string 源码 C++

字符串拼接是个常用的功能,经常性使用String做字符串拼接,当拼接次数多的时候,使用String方法会消耗大量的性能和时间,因为每次String拼接时都会建立一个新的对象,随着拼接次数的增多,性能消

关键字: java string

#include #include #include using namespace std; #define INFINITY 65535//无边时的权值 #define MAX_VERTE

关键字: string 算法

I know this is starting to look like a half-baked tutorial in assembly, but there's actually a reas

关键字: byte string

通过用static来定义方法或成员,为我们编程提供了某种便利,从某种程度上可以说它类似于C语言中的全局函数和全局变量。但是,并不是说有了这种便利,你便可以随处使用,如果那样的话,你便需要认真考虑一下自

关键字: java string

1、字符串Unicode字符串有一个结构体定义如下:typedef struct _UNICODE_STRING {  USHORT Length; //字符串的长度(字节数)  USHORT Max

关键字: string 字符串

面向对象是C++的重要特性. 但是c++在c的基础上新增加的几点优化也是很耀眼的 就const直接可以取代c中的#define 以下几点很重要,学不好后果也也很严重 const 1. 限定符声明变量

关键字: string 编译器

Fill()功能建立一个由指定字符串填充的指定长度的字符串。语法Fill   (   chars,   n   )参数chars:string类型,指定用于重复填充的字符串n:long类型,指定由该函

关键字: null string

//定义游标DECLARE C1 CURSOR FOR//取值select aln_cd,sum(cargo_aln_fare)  from fare_daily_viewwhere aln_cd &

关键字: c string
关闭
关闭