-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice_tree.c
More file actions
86 lines (65 loc) · 2.25 KB
/
device_tree.c
File metadata and controls
86 lines (65 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdlib.h>
#include <string.h>
#include "device_tree.h"
DeviceTreeProperty* device_tree_first_property(DeviceTreeNode* child) {
return child->properties;
}
DeviceTreeProperty* device_tree_next_property(DeviceTreeProperty* property) {
return (DeviceTreeProperty*)((char*)property + sizeof(DeviceTreeProperty) + (((property->length & 0x7fffffff) + 3) & -4)); // Properties are 4 byte aligned
}
DeviceTreeNode* device_tree_first_child(DeviceTreeNode* node) {
DeviceTreeProperty* property = device_tree_first_property(node);
for (int i = 0; i < node->nProperties; i++) {
property = device_tree_next_property(property);
}
return (DeviceTreeNode*)property;
}
DeviceTreeNode* device_tree_next_child(DeviceTreeNode* child) {
DeviceTreeNode* _child = device_tree_first_child(child);
for (int i = 0; i < child->nChildren; i++) {
_child = device_tree_next_child(_child);
}
return _child;
}
DeviceTreeProperty* device_tree_get_property(DeviceTreeNode* node, char* property_name) {
DeviceTreeProperty* property = node->properties;
for (int i = 0; i < node->nProperties; i++) {
char* name = property->name;
if (!strcmp(name, property_name)) {
return property;
}
property = device_tree_next_property(property);
}
return NULL;
}
DeviceTreeNode* device_tree_get_child(DeviceTreeNode* node, char* child_name) {
DeviceTreeNode* child = device_tree_first_child(node);
for (int i = 0; i < node->nChildren; i++) {
char* name = device_tree_get_property(child, "name")->value;
if (!strcmp(name, child_name)) {
return child;
}
child = device_tree_next_child(child);
}
return NULL;
}
DeviceTreeNode* device_tree_lookup_entry(DeviceTreeNode* node, char* path) {
if (path == NULL) {
return NULL;
}
if (path[0] == '/') {
path++;
}
DeviceTreeNode* entry = node;
char* temp_path = strdup(path);
char* path_component = strtok(temp_path, "/");
while (path_component) {
entry = device_tree_get_child(entry, path_component);
if (entry == NULL) {
return NULL;
}
path_component = strtok(NULL, "/");
}
free(temp_path);
return entry;
}