@@ -604,29 +604,30 @@ static int do_write_op_item(struct lwm2m_message *msg, struct record *rec)
604604 int ret ;
605605 uint8_t created = 0U ;
606606 struct cbor_in_fmt_data * fd ;
607+ /* Composite op - name with basename */
608+ char name [sizeof ("65535/999" )] = { 0 }; /* Null terminated name */
609+ int len = 0 ;
610+ char fqn [MAX_RESOURCE_LEN + 1 ] = {0 };
607611
608612 fd = engine_get_in_user_data (& msg -> in );
609613 if (!fd ) {
610614 return - EINVAL ;
611615 }
612616
613- /* Composite op - name with basename */
617+ /* If there's no name then the basename forms the path */
614618 if (rec -> _record_n_present ) {
615- char name [sizeof ("65535/999" )]; /* Null terminated name */
616- int len = MIN (sizeof (name ) - 1 , rec -> _record_n ._record_n .len );
617- char fqn [MAX_RESOURCE_LEN + 1 ] = {0 };
618-
619+ len = MIN (sizeof (name ) - 1 , rec -> _record_n ._record_n .len );
619620 snprintk (name , len + 1 , "%s" , rec -> _record_n ._record_n .value );
621+ }
620622
621- /* Form fully qualified path name */
622- snprintk (fqn , sizeof (fqn ), "%s%s" , fd -> basename , name );
623+ /* Form fully qualified path name */
624+ snprintk (fqn , sizeof (fqn ), "%s%s" , fd -> basename , name );
623625
624- /* Set path on record basis */
625- ret = lwm2m_string_to_path (fqn , & msg -> path , '/' );
626- if (ret < 0 ) {
627- __ASSERT_NO_MSG (false);
628- return ret ;
629- }
626+ /* Set path on record basis */
627+ ret = lwm2m_string_to_path (fqn , & msg -> path , '/' );
628+ if (ret < 0 ) {
629+ __ASSERT_NO_MSG (false);
630+ return ret ;
630631 }
631632
632633 fd -> current = rec ;
@@ -830,33 +831,50 @@ int do_write_op_senml_cbor(struct lwm2m_message *msg)
830831 dret = cbor_decode_lwm2m_senml (ICTX_BUF_R_PTR (& msg -> in ), ICTX_BUF_R_LEFT_SZ (& msg -> in ),
831832 & fd -> dcd , & decoded_sz );
832833
833- if (dret == ZCBOR_SUCCESS ) {
834- msg -> in .offset += decoded_sz ;
834+ if (dret != ZCBOR_SUCCESS ) {
835+ ret = - EBADMSG ;
836+ goto error ;
837+ }
838+
839+ msg -> in .offset += decoded_sz ;
835840
836- for (int idx = 0 ; idx < fd -> dcd ._lwm2m_senml__record_count ; idx ++ ) {
841+ for (int idx = 0 ; idx < fd -> dcd ._lwm2m_senml__record_count ; idx ++ ) {
837842
838- struct record * rec = & fd -> dcd ._lwm2m_senml__record [idx ];
843+ struct record * rec = & fd -> dcd ._lwm2m_senml__record [idx ];
839844
840- /* Basename applies for current and succeeding records */
841- if (rec -> _record_bn_present ) {
842- int len = MIN (sizeof (fd -> basename ) - 1 ,
843- rec -> _record_bn ._record_bn .len );
845+ /* Basename applies for current and succeeding records */
846+ if (rec -> _record_bn_present ) {
847+ int len = MIN (sizeof (fd -> basename ) - 1 ,
848+ rec -> _record_bn ._record_bn .len );
844849
845- snprintk (fd -> basename , len + 1 , "%s" ,
846- rec -> _record_bn ._record_bn .value );
847- }
850+ snprintk (fd -> basename , len + 1 , "%s" , rec -> _record_bn ._record_bn .value );
851+ goto write ;
852+ }
853+
854+ /* Keys' lexicographic order differ from the default */
855+ for (int jdx = 0 ; jdx < rec -> _record__key_value_pair_count ; jdx ++ ) {
856+ struct key_value_pair * kvp =
857+ & (rec -> _record__key_value_pair [jdx ]._record__key_value_pair );
848858
849- ret = do_write_op_item (msg , rec );
859+ if (kvp -> _key_value_pair_key == lwm2m_senml_cbor_key_bn ) {
860+ int len = MIN (sizeof (fd -> basename ) - 1 ,
861+ kvp -> _key_value_pair ._value_tstr .len );
850862
851- /* Write isn't supposed to fail */
852- if ( ret < 0 ) {
863+ snprintk ( fd -> basename , len + 1 , "%s" ,
864+ kvp -> _key_value_pair . _value_tstr . value );
853865 break ;
854866 }
855867 }
856- } else {
857- ret = - EBADMSG ;
868+ write :
869+ ret = do_write_op_item (msg , rec );
870+
871+ /* Write isn't supposed to fail */
872+ if (ret < 0 ) {
873+ break ;
874+ }
858875 }
859876
877+ error :
860878 clear_in_fmt_data (msg );
861879
862880 return ret < 0 ? ret : decoded_sz ;
0 commit comments