4141
4242#include <stdio.h>
4343#include <string.h>
44-
4544void print_json (cbor_item_t * item );
4645
4746void print_json_string (const cbor_item_t * item )
@@ -51,6 +50,22 @@ void print_json_string(const cbor_item_t *item)
5150 printf ("\"%.*s\"" , (int )length , str );
5251}
5352
53+ void print_json_array (const cbor_item_t * item )
54+ {
55+ printf ("[" );
56+ size_t size = cbor_array_size (item );
57+ cbor_item_t * * handle = cbor_array_handle (item );
58+
59+ for (size_t i = 0 ; i < size ; ++ i )
60+ {
61+ print_json (handle [i ]);
62+ if (i < size - 1 )
63+ printf (", " );
64+ }
65+
66+ printf ("]" );
67+ }
68+
5469void print_json_map (const cbor_item_t * item )
5570{
5671 printf ("{" );
@@ -69,36 +84,92 @@ void print_json_map(const cbor_item_t *item)
6984 printf ("}" );
7085}
7186
87+ void print_json_number (const cbor_item_t * item )
88+ {
89+ if (cbor_isa_uint (item )) {
90+ printf ("%lu" , cbor_get_uint64 (item ));
91+ } else if (cbor_isa_negint (item )) {
92+ printf ("-%lu" , cbor_get_uint64 (item ) + 1 );
93+ } else if (cbor_isa_float_ctrl (item )) {
94+ if (cbor_float_get_width (item ) == CBOR_FLOAT_64 ) {
95+ printf ("%f" , cbor_float_get_float8 (item ));
96+ } else if (cbor_float_get_width (item ) == CBOR_FLOAT_32 ) {
97+ printf ("%f" , cbor_float_get_float4 (item ));
98+ } else if (cbor_float_get_width (item ) == CBOR_FLOAT_16 ) {
99+ printf ("%f" , cbor_float_get_float2 (item ));
100+ }
101+ }
102+ }
103+
72104void print_json_bool (const cbor_item_t * item )
73105{
74106 printf (cbor_is_bool (item ) && cbor_ctrl_value (item ) ? "true" : "false" );
75107}
76108
109+ void print_json_null (const cbor_item_t * item )
110+ {
111+ printf ("null" );
112+ }
113+
77114void print_json (cbor_item_t * item )
78115{
116+ if (!item ) {
117+ printf ("null" );
118+ return ;
119+ }
120+
79121 if (cbor_isa_map (item ))
80122 {
81123 print_json_map (item );
82124 }
125+ else if (cbor_isa_array (item ))
126+ {
127+ print_json_array (item );
128+ }
83129 else if (cbor_isa_string (item ))
84130 {
85131 print_json_string (item );
86132 }
133+ else if (cbor_isa_uint (item ) || cbor_isa_negint (item ))
134+ {
135+ print_json_number (item );
136+ }
137+ else if (cbor_isa_float_ctrl (item ))
138+ {
139+ // Check if it's a control value (null, undefined, true, false)
140+ if (cbor_float_get_width (item ) == CBOR_FLOAT_0 ) {
141+ uint8_t ctrl = cbor_ctrl_value (item );
142+ if (ctrl == 20 ) {
143+ printf ("false" );
144+ } else if (ctrl == 21 ) {
145+ printf ("true" );
146+ } else if (ctrl == 22 ) {
147+ printf ("null" );
148+ } else if (ctrl == 23 ) {
149+ printf ("undefined" );
150+ } else {
151+ printf ("null" ); // unknown control value
152+ }
153+ } else {
154+ print_json_number (item );
155+ }
156+ }
87157 else if (cbor_is_bool (item ))
88158 {
89159 print_json_bool (item );
90160 }
91161 else
92162 {
93- printf ("null" ); // fallback for unsupported types
163+ printf ("null" ); // fallback for truly unsupported types
94164 }
95165}
96166
97- static LY_ERR lydcbor_parse_subtree (struct lyd_cbor_ctx * lydctx , struct lyd_node * parent ,
167+ static LY_ERR lydcbor_subtree_r (struct lyd_cbor_ctx * lydctx , struct lyd_node * parent ,
98168 struct lyd_node * * first_p , struct ly_set * parsed , const cbor_item_t * cbor_obj );
99169
100170/**
101- * @brief Free the CBOR parser context
171+ * @brief Free the CBOR data parser context
172+ * CBOR implementation of lyd_ctx_free_clb().
102173 *
103174 * @param[in] lydctx Data parser context to free.
104175 */
@@ -372,6 +443,7 @@ lydcbor_parse_terminal(struct lyd_cbor_ctx *lydctx, const struct lysc_node *snod
372443 return LY_EVALID ;
373444 }
374445
446+ lyd_hash (node );
375447 /* Insert into tree */
376448 ret = lyd_insert_sibling (* first_p , node , first_p );
377449 if (ret )
@@ -400,6 +472,7 @@ lydcbor_parse_container(struct lyd_cbor_ctx *lydctx, const struct lysc_node *sno
400472 return LY_EVALID ;
401473 }
402474
475+ lyd_hash (node );
403476 /* Insert into tree first */
404477 ret = lyd_insert_sibling (* first_p , node , first_p );
405478 if (ret )
@@ -416,7 +489,7 @@ lydcbor_parse_container(struct lyd_cbor_ctx *lydctx, const struct lysc_node *sno
416489 if (cbor_isa_map (cbor_value ) && cbor_map_size (cbor_value ) > 0 )
417490 {
418491 struct lyd_node * child_first = NULL ;
419- ret = lydcbor_parse_subtree (lydctx , node , & child_first , parsed , cbor_value );
492+ ret = lydcbor_subtree_r (lydctx , node , & child_first , parsed , cbor_value );
420493 if (ret )
421494 {
422495 return ret ;
@@ -432,6 +505,27 @@ lydcbor_parse_container(struct lyd_cbor_ctx *lydctx, const struct lysc_node *sno
432505 return LY_SUCCESS ;
433506}
434507
508+ /* Helper function to check if CBOR item is null/undefined */
509+ static ly_bool
510+ lydcbor_is_null (const cbor_item_t * item )
511+ {
512+ if (!item ) {
513+ return 1 ;
514+ }
515+
516+ /* Check for CBOR null primitive */
517+ if (cbor_isa_float_ctrl (item )) {
518+ if (cbor_float_get_width (item ) == CBOR_FLOAT_0 ) {
519+ uint8_t ctrl = cbor_ctrl_value (item );
520+ if (ctrl == 22 || ctrl == 23 ) { /* null or undefined */
521+ return 1 ;
522+ }
523+ }
524+ }
525+
526+ return 0 ;
527+ }
528+
435529static LY_ERR
436530lydcbor_parse_list_array (struct lyd_cbor_ctx * lydctx , const struct lysc_node * snode ,
437531 const cbor_item_t * array_item , struct lyd_node * * first_p , struct ly_set * parsed )
@@ -491,7 +585,7 @@ lydcbor_parse_list_array(struct lyd_cbor_ctx *lydctx, const struct lysc_node *sn
491585 ret = LY_EVALID ;
492586 goto cleanup ;
493587 }
494-
588+ lyd_hash ( node );
495589 /* Insert the list node */
496590 ret = lyd_insert_sibling (* first_p , node , first_p );
497591 LY_CHECK_GOTO (ret , cleanup );
@@ -501,7 +595,7 @@ lydcbor_parse_list_array(struct lyd_cbor_ctx *lydctx, const struct lysc_node *sn
501595
502596 /* Parse list entry content */
503597 struct lyd_node * child_first = NULL ;
504- ret = lydcbor_parse_subtree (lydctx , node , & child_first , parsed , item );
598+ ret = lydcbor_subtree_r (lydctx , node , & child_first , parsed , item );
505599 LY_CHECK_GOTO (ret , cleanup );
506600
507601 /* Link children to list entry */
@@ -529,7 +623,6 @@ lydcbor_parse_list(struct lyd_cbor_ctx *lydctx, const struct lysc_node *snode,
529623
530624 if (cbor_isa_array (cbor_value ))
531625 {
532- /* Array of list entries */
533626 ret = lydcbor_parse_list_array (lydctx , snode , cbor_value , first_p , parsed );
534627 }
535628 else if (cbor_isa_map (cbor_value ))
@@ -540,6 +633,7 @@ lydcbor_parse_list(struct lyd_cbor_ctx *lydctx, const struct lysc_node *snode,
540633 ret = lyd_create_inner (snode , & node );
541634 LY_CHECK_RET (ret );
542635
636+ lyd_hash (node );
543637 /* Insert into tree */
544638 ret = lyd_insert_sibling (* first_p , node , first_p );
545639 if (ret )
@@ -554,7 +648,7 @@ lydcbor_parse_list(struct lyd_cbor_ctx *lydctx, const struct lysc_node *snode,
554648
555649 /* Parse list entry content */
556650 struct lyd_node * child_first = NULL ;
557- ret = lydcbor_parse_subtree (lydctx , node , & child_first , parsed , cbor_value );
651+ ret = lydcbor_subtree_r (lydctx , node , & child_first , parsed , cbor_value );
558652 if (ret )
559653 {
560654 return ret ;
@@ -591,6 +685,7 @@ lydcbor_parse_any(struct lyd_cbor_ctx *lydctx, const struct lysc_node *snode,
591685 return LY_EVALID ;
592686 }
593687
688+ lyd_hash (node );
594689 /* Insert into tree */
595690 ret = lyd_insert_sibling (* first_p , node , first_p );
596691 if (ret )
@@ -927,7 +1022,8 @@ lydcbor_parse_leaflist_array(struct lyd_cbor_ctx *lydctx, const struct lysc_node
9271022 ret = LY_EVALID ;
9281023 goto cleanup ;
9291024 }
930-
1025+
1026+ lyd_hash (node );
9311027 /* Insert the node */
9321028 ret = lyd_insert_sibling (* first_p , node , first_p );
9331029 LY_CHECK_GOTO (ret , cleanup );
@@ -946,12 +1042,12 @@ lydcbor_parse_leaflist_array(struct lyd_cbor_ctx *lydctx, const struct lysc_node
9461042}
9471043
9481044static LY_ERR
949- lydcbor_parse_subtree (struct lyd_cbor_ctx * lydctx , struct lyd_node * parent ,
1045+ lydcbor_subtree_r (struct lyd_cbor_ctx * lydctx , struct lyd_node * parent ,
9501046 struct lyd_node * * first_p , struct ly_set * parsed , const cbor_item_t * cbor_obj )
9511047{
9521048 LY_ERR ret = LY_SUCCESS ;
9531049
954- printf ("Entering lydcbor_parse_subtree \n" );
1050+ printf ("Entering lydcbor_subtree_r \n" );
9551051 printf ("CBOR object:\n" );
9561052 print_json (cbor_obj );
9571053 printf ("\n" );
@@ -992,6 +1088,11 @@ lydcbor_parse_subtree(struct lyd_cbor_ctx *lydctx, struct lyd_node *parent,
9921088 goto cleanup ;
9931089 }
9941090
1091+ if (lydcbor_is_null (value_item )) {
1092+ // Skip null values - don't create any nodes
1093+ continue ; // or return LY_SUCCESS depending on your loop structure
1094+ }
1095+
9951096 /* Get key string */
9961097 LY_CHECK_GOTO (ret = lydcbor_get_key_string (lydctx , key_item , & key_str , & key_len ), cleanup );
9971098
@@ -1099,6 +1200,10 @@ lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st
10991200 lydctx -> int_opts = int_opts ;
11001201 lydctx -> ext = ext ;
11011202
1203+ /* find the operation node if it exists already */
1204+ LY_CHECK_GOTO (ret = lyd_parser_find_operation (parent , int_opts , & lydctx -> op_node ), cleanup );
1205+
1206+
11021207 /*
11031208 * Loads CBOR data from the current input buffer.
11041209 *
@@ -1131,7 +1236,7 @@ lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st
11311236 then write functions to parse them accordingly. If not then continue below */
11321237
11331238 /* Parse the CBOR structure */
1134- ret = lydcbor_parse_subtree (lydctx , parent , first_p , parsed , cbor_data );
1239+ ret = lydcbor_subtree_r (lydctx , parent , first_p , parsed , cbor_data );
11351240
11361241cleanup :
11371242 if (cbor_data )
0 commit comments