日期:11月23日
一、题目
1. 属性相关解析API和获取相关属性信息API实现
代码实现:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
struct device_node *node;
struct property* pr;
int len,i,ret;
unsigned int val;
unsigned int array[2];
const char* str;
static int __init mycdev_init(void)
{
node = of_find_node_by_path("/mynode@0x12345678");
if(node == NULL)
{
printk("通过路径解析设备树节点失败\n");
return -EFAULT;
}
printk("成功解析到设备树节点\n");
node = of_find_node_by_name(NULL,"mynode");
if(node == NULL)
{
printk("通过节点解析设备树节点设备失败\n");
return -EFAULT;
}
printk("成功解析到设备树节点\n");
/*
node = of_find_compatible_node(NULL,NULL,"hqyj");
if(node == NULL)
{
printk("通过compatible获取节点信息失败\n");
return -EFAULT;
}
*/
//解析后的节点属性通过节点结构体的properities成员访问
printk("name=%s,value=%s\n",node->properties->name,(char*)node->properties->value);
printk("name=%s,value=%s\n",node->properties->next->name,(char*)node->properties->next->value);
//整型数据需要实现小端字节序转换成大端字节序再输出
printk("name=%s,value=%#x,%#x\n",node->properties->next->next->name,__be32_to_cpup((int*)node->properties->next->next->value),__be32_to_cpup((int*)node->properties->next->next->value+1));
pr = of_find_property(node,"astring",&len);
if(pr == NULL)
{
printk("属性解析失败\n");
return -EFAULT;
}
printk("name=%s,value=%s\n",pr->name,(char*)pr->value);
for(i=0;i<len;i++)
printk("name=%s value=%#x\n",pr->name,*((char*)pr->value+1));
//获取u32的值
ret = of_property_read_u32_index(node,"uint",1,&val);
if(ret<0)
{
printk("获取u32值失败\n");
return -EFAULT;
}
printk("value=%#x\n",val);
ret = of_property_read_variable_u32_array(node,"uint",array,2,2);
if(ret < 0 )
{
printk("获取u32数组失败\n");
return -EFAULT;
}
printk("value:%#x %#x\n",array[0],array[1]);
ret = of_property_read_string(node,"astring",&str);
if(ret<0)
{
printk("获取字符串失败\n");
return -EFAULT;
}
printk("value=%s\n",str);
return 0;
}
static void __exit mycdev_exit(void)
{
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");