1+ #include <debug/debug_stdio.h>
12#include <drivers/dt/dt.h>
3+ #include <drivers/dt/dt_node.h>
24#include <stdbigos/bitutils.h>
3- #include <stdbigos/sbi.h>
45#include <stdbigos/string.h>
56
67#include "dt_alloc.h"
7- #include "dt_node.h"
88#include "dt_parser.h"
99
10- static void sbi_puts (const char * str ) {
11- while (* str ) sbi_debug_console_write_byte (* str ++ );
12- }
13-
14- // There has to be a better way to do this
15- static void gap (u8 size ) {
16- for (u8 i = 0 ; i < size ; i ++ ) sbi_puts ("\t" );
17- }
18-
19- dt_node_t * dt_get_root (void ) {
20- return root_node ;
21- }
22-
2310static dt_node_t * find_child_by_name (dt_node_t * parent , const char * name ) {
2411 for (dt_node_t * child = parent -> first_child ; child ; child = child -> next_sibling ) {
2512 if (strcmp (child -> name , name ) == 0 ) {
@@ -30,6 +17,8 @@ static dt_node_t* find_child_by_name(dt_node_t* parent, const char* name) {
3017}
3118
3219dt_node_t * dt_node_find (const char * path ) {
20+ dt_node_t * root_node = dt_get_root ();
21+
3322 if (!path || path [0 ] != '/' || !root_node )
3423 return nullptr ;
3524
@@ -77,10 +66,12 @@ int dt_node_child_count(const dt_node_t* n) {
7766 return c ;
7867}
7968
80- dt_node_t * dt_node_get_child (const dt_node_t * n , int idx ) {
81- dt_node_t * ch = n ? n -> first_child : NULL ;
82- for (; ch && idx > 0 ; ch = ch -> next_sibling , idx -- );
83- return ch ;
69+ dt_node_t * dt_get_next_child (const dt_node_t * node ) {
70+ if (!node )
71+ return nullptr ;
72+
73+ // If there's no next sibling, it will automatically return a nullptr, as per documentation
74+ return node -> next_sibling ;
8475}
8576
8677dt_prop_t * dt_find_prop (const dt_node_t * node , const char * name ) {
@@ -125,22 +116,21 @@ int dt_prop_read_u64(const dt_node_t* node, const char* name, u64* out) {
125116void dt_print_props (const dt_node_t * node , u8 depth ) {
126117 dt_prop_t * next = node -> props ;
127118 while (next ) {
128- gap (depth );
129- sbi_puts (next -> name );
130- sbi_puts ("\n" );
119+ DEBUG_PUTGAP (depth );
120+ DEBUG_PRINTF ("%s\n" , next -> name );
131121 next = next -> next_prop ;
132122 }
133123}
134124
125+ // Use only in debug preset
135126void dt_print_tree (const dt_node_t * node , u8 depth ) {
136- gap (depth );
137- sbi_puts ("NODE: " );
138- sbi_puts (node -> name );
139- sbi_puts ("\n" );
140- gap (depth );
141- sbi_puts ("PROPERTIES:\n" );
127+ DEBUG_PUTGAP (depth );
128+ DEBUG_PRINTF ("NODE: %s\n" , node -> name );
129+ DEBUG_PUTGAP (depth );
130+
131+ DEBUG_PRINTF ("PROPERTIES:\n" );
142132 dt_print_props (node , depth + 1 );
143- sbi_puts ( "\n" );
133+ DEBUG_PUTC ( '\n' );
144134 dt_node_t * next = node -> first_child ;
145135 while (next ) {
146136 dt_print_tree (next , depth + 1 );
0 commit comments